]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
implement renaming, moving to trash and deleting for the treeview panel
[dolphin.git] / src / treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "treeviewcontextmenu.h"
22
23 #include <kiconloader.h>
24 #include <kmenu.h>
25 #include <konqmimedata.h>
26 #include <konq_operations.h>
27 #include <klocale.h>
28 #include <kpropertiesdialog.h>
29
30 #include "renamedialog.h"
31
32 #include <QApplication>
33 #include <QClipboard>
34
35 TreeViewContextMenu::TreeViewContextMenu(QWidget* parent,
36 KFileItem* fileInfo) :
37 m_parent(parent),
38 m_fileInfo(fileInfo)
39 {
40 }
41
42 TreeViewContextMenu::~TreeViewContextMenu()
43 {
44 }
45
46 void TreeViewContextMenu::open()
47 {
48 Q_ASSERT(m_fileInfo != 0);
49
50 KMenu* popup = new KMenu(m_parent);
51
52 // insert 'Cut', 'Copy' and 'Paste'
53 QAction* cutAction = new QAction(KIcon("edit-cut"), i18n("Cut"), this);
54 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
55
56 QAction* copyAction = new QAction(KIcon("edit-copy"), i18n("Copy"), this);
57 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
58
59 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18n("Paste"), this);
60 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
61 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
62 pasteAction->setEnabled(!pasteData.isEmpty());
63 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
64
65 popup->addAction(cutAction);
66 popup->addAction(copyAction);
67 popup->addAction(pasteAction);
68 popup->addSeparator();
69
70 // insert 'Rename'
71 QAction* renameAction = new QAction(i18n("Rename"), this);
72 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
73 popup->addAction(renameAction);
74
75 // insert 'Move to Trash' and (optionally) 'Delete'
76 const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals);
77 const KConfigGroup kdeConfig(globalConfig, "KDE");
78 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
79 const KUrl& url = m_fileInfo->url();
80 if (url.isLocalFile()) {
81 QAction* moveToTrashAction = new QAction(KIcon("edit-trash"), i18n("Move To Trash"), this);
82 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
83 popup->addAction(moveToTrashAction);
84 }
85 else {
86 showDeleteCommand = true;
87 }
88
89 if (showDeleteCommand) {
90 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18n("Delete"), this);
91 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
92 popup->addAction(deleteAction);
93 }
94
95 popup->addSeparator();
96
97 // insert 'Properties...' entry
98 QAction* propertiesAction = new QAction(i18n("Properties..."), this);
99 connect(this, SIGNAL(triggered()), this, SLOT(showProperties()));
100 popup->addAction(propertiesAction);
101
102 popup->exec(QCursor::pos());
103 popup->deleteLater();
104 }
105
106 void TreeViewContextMenu::cut()
107 {
108 QMimeData* mimeData = new QMimeData();
109 KUrl::List kdeUrls;
110 kdeUrls.append(m_fileInfo->url());
111 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), true);
112 QApplication::clipboard()->setMimeData(mimeData);
113 }
114
115 void TreeViewContextMenu::copy()
116 {
117 QMimeData* mimeData = new QMimeData();
118 KUrl::List kdeUrls;
119 kdeUrls.append(m_fileInfo->url());
120 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), false);
121 QApplication::clipboard()->setMimeData(mimeData);
122 }
123
124 void TreeViewContextMenu::paste()
125 {
126 QClipboard* clipboard = QApplication::clipboard();
127 const QMimeData* mimeData = clipboard->mimeData();
128
129 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
130 const KUrl& dest = m_fileInfo->url();
131 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
132 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
133 clipboard->clear();
134 }
135 else {
136 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
137 }
138 }
139
140 void TreeViewContextMenu::rename()
141 {
142 const KUrl& oldUrl = m_fileInfo->url();
143 RenameDialog dialog(oldUrl);
144 if (dialog.exec() == QDialog::Accepted) {
145 const QString& newName = dialog.newName();
146 if (!newName.isEmpty()) {
147 KUrl newUrl = oldUrl.upUrl();
148 newUrl.addPath(newName);
149 KonqOperations::rename(m_parent, oldUrl, newUrl);
150 }
151 }
152 }
153
154 void TreeViewContextMenu::moveToTrash()
155 {
156 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo->url());
157 }
158
159 void TreeViewContextMenu::deleteItem()
160 {
161 KonqOperations::askDeleteConfirmation(m_fileInfo->url(),
162 KonqOperations::DEL,
163 KonqOperations::FORCE_CONFIRMATION,
164 m_parent);
165 }
166
167 void TreeViewContextMenu::showProperties()
168 {
169 new KPropertiesDialog(m_fileInfo->url());
170 }
171
172 #include "treeviewcontextmenu.moc"