]>
cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "treeviewcontextmenu.h"
23 #include <kiconloader.h>
24 #include <kio/deletejob.h>
26 #include <konqmimedata.h>
27 #include <konq_operations.h>
29 #include <kpropertiesdialog.h>
31 #include "renamedialog.h"
33 #include <QApplication>
36 TreeViewContextMenu::TreeViewContextMenu(QWidget
* parent
,
37 KFileItem
* fileInfo
) :
42 TreeViewContextMenu::~TreeViewContextMenu()
45 void TreeViewContextMenu::open()
47 Q_ASSERT(m_fileInfo
!= 0);
49 KMenu
* popup
= new KMenu(m_parent
);
51 // insert 'Cut', 'Copy' and 'Paste'
52 QAction
* cutAction
= new QAction(KIcon("edit-cut"), i18n("Cut"), this);
53 connect(cutAction
, SIGNAL(triggered()), this, SLOT(cut()));
55 QAction
* copyAction
= new QAction(KIcon("edit-copy"), i18n("Copy"), this);
56 connect(copyAction
, SIGNAL(triggered()), this, SLOT(copy()));
58 QAction
* pasteAction
= new QAction(KIcon("edit-paste"), i18n("Paste"), this);
59 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
60 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
61 pasteAction
->setEnabled(!pasteData
.isEmpty());
62 connect(pasteAction
, SIGNAL(triggered()), this, SLOT(paste()));
64 popup
->addAction(cutAction
);
65 popup
->addAction(copyAction
);
66 popup
->addAction(pasteAction
);
67 popup
->addSeparator();
70 QAction
* renameAction
= new QAction(i18n("Rename..."), this);
71 connect(renameAction
, SIGNAL(triggered()), this, SLOT(rename()));
72 popup
->addAction(renameAction
);
74 // insert 'Move to Trash' and (optionally) 'Delete'
75 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
76 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
77 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
78 const KUrl
& url
= m_fileInfo
->url();
79 if (url
.isLocalFile()) {
80 QAction
* moveToTrashAction
= new QAction(KIcon("edit-trash"), i18n("Move To Trash"), this);
81 connect(moveToTrashAction
, SIGNAL(triggered()), this, SLOT(moveToTrash()));
82 popup
->addAction(moveToTrashAction
);
84 showDeleteCommand
= true;
87 if (showDeleteCommand
) {
88 QAction
* deleteAction
= new QAction(KIcon("edit-delete"), i18n("Delete"), this);
89 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(deleteItem()));
90 popup
->addAction(deleteAction
);
93 popup
->addSeparator();
95 // insert 'Properties...' entry
96 QAction
* propertiesAction
= new QAction(i18n("Properties..."), this);
97 connect(this, SIGNAL(triggered()), this, SLOT(showProperties()));
98 popup
->addAction(propertiesAction
);
100 popup
->exec(QCursor::pos());
101 popup
->deleteLater();
104 void TreeViewContextMenu::cut()
106 QMimeData
* mimeData
= new QMimeData();
108 kdeUrls
.append(m_fileInfo
->url());
109 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, KUrl::List(), true);
110 QApplication::clipboard()->setMimeData(mimeData
);
113 void TreeViewContextMenu::copy()
115 QMimeData
* mimeData
= new QMimeData();
117 kdeUrls
.append(m_fileInfo
->url());
118 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, KUrl::List(), false);
119 QApplication::clipboard()->setMimeData(mimeData
);
122 void TreeViewContextMenu::paste()
124 QClipboard
* clipboard
= QApplication::clipboard();
125 const QMimeData
* mimeData
= clipboard
->mimeData();
127 const KUrl::List source
= KUrl::List::fromMimeData(mimeData
);
128 const KUrl
& dest
= m_fileInfo
->url();
129 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
130 KonqOperations::copy(m_parent
, KonqOperations::MOVE
, source
, dest
);
133 KonqOperations::copy(m_parent
, KonqOperations::COPY
, source
, dest
);
137 void TreeViewContextMenu::rename()
139 const KUrl
& oldUrl
= m_fileInfo
->url();
140 RenameDialog
dialog(oldUrl
);
141 if (dialog
.exec() == QDialog::Accepted
) {
142 const QString
& newName
= dialog
.newName();
143 if (!newName
.isEmpty()) {
144 KUrl newUrl
= oldUrl
;
145 newUrl
.setFileName(newName
);
146 KonqOperations::rename(m_parent
, oldUrl
, newUrl
);
151 void TreeViewContextMenu::moveToTrash()
153 KonqOperations::del(m_parent
, KonqOperations::TRASH
, m_fileInfo
->url());
156 void TreeViewContextMenu::deleteItem()
158 KonqOperations::del(m_parent
, KonqOperations::DEL
, m_fileInfo
->url());
161 void TreeViewContextMenu::showProperties()
163 new KPropertiesDialog(m_fileInfo
->url());
166 #include "treeviewcontextmenu.moc"