]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Fix lack of error handling in RMB/Move To... (e.g. when src is root-owned) because...
[dolphin.git] / src / dolphincontextmenu.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 "dolphincontextmenu.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinnewmenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinview.h"
27 #include "dolphinviewcontainer.h"
28 #include "dolphin_generalsettings.h"
29
30 #include <kactioncollection.h>
31 #include <kfileplacesmodel.h>
32 #include <kdesktopfile.h>
33 #include <kglobal.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
36 #include <kmenu.h>
37 #include <kmenubar.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
40 #include <knewmenu.h>
41 #include <konqmimedata.h>
42 #include <konq_fileitemcapabilities.h>
43 #include <konq_operations.h>
44 #include <konq_menuactions.h>
45 #include <konq_popupmenuinformation.h>
46 #include <klocale.h>
47 #include <kpropertiesdialog.h>
48 #include <krun.h>
49 #include <kstandardaction.h>
50 #include <kstandarddirs.h>
51
52 #include <QtGui/QApplication>
53 #include <QtGui/QClipboard>
54 #include <QtCore/QDir>
55
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
57 const KFileItem& fileInfo,
58 const KUrl& baseUrl) :
59 m_mainWindow(parent),
60 m_capabilities(0),
61 m_fileInfo(fileInfo),
62 m_baseUrl(baseUrl),
63 m_context(NoContext),
64 m_copyToMenu(parent)
65 {
66 // The context menu either accesses the URLs of the selected items
67 // or the items itself. To increase the performance both lists are cached.
68 DolphinView* view = m_mainWindow->activeViewContainer()->view();
69 m_selectedUrls = view->selectedUrls();
70 m_selectedItems = view->selectedItems();
71 }
72
73 DolphinContextMenu::~DolphinContextMenu()
74 {
75 delete m_capabilities;
76 m_capabilities = 0;
77 }
78
79 void DolphinContextMenu::open()
80 {
81 // get the context information
82 if (m_baseUrl.protocol() == "trash") {
83 m_context |= TrashContext;
84 }
85
86 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
87 m_context |= ItemContext;
88 // TODO: handle other use cases like devices + desktop files
89 }
90
91 // open the corresponding popup for the context
92 if (m_context & TrashContext) {
93 if (m_context & ItemContext) {
94 openTrashItemContextMenu();
95 } else {
96 openTrashContextMenu();
97 }
98 } else if (m_context & ItemContext) {
99 openItemContextMenu();
100 } else {
101 Q_ASSERT(m_context == NoContext);
102 openViewportContextMenu();
103 }
104 }
105
106 void DolphinContextMenu::openTrashContextMenu()
107 {
108 Q_ASSERT(m_context & TrashContext);
109
110 KMenu* popup = new KMenu(m_mainWindow);
111
112 addShowMenubarAction(popup);
113
114 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup);
115 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
116 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
117 popup->addAction(emptyTrashAction);
118
119 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
120 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
121
122 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
123 popup->addAction(propertiesAction);
124
125 QAction *action = popup->exec(QCursor::pos());
126 if (action == emptyTrashAction) {
127 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
128 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
129 text,
130 QString(),
131 KGuiItem(i18nc("@action:button", "Empty Trash"),
132 KIcon("user-trash"))
133 ) == KMessageBox::Continue;
134 if (del) {
135 KonqOperations::emptyTrash(m_mainWindow);
136 }
137 } else if (action == addToPlacesAction) {
138 const KUrl& url = m_mainWindow->activeViewContainer()->url();
139 if (url.isValid()) {
140 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
141 }
142 }
143
144 popup->deleteLater();
145 }
146
147 void DolphinContextMenu::openTrashItemContextMenu()
148 {
149 Q_ASSERT(m_context & TrashContext);
150 Q_ASSERT(m_context & ItemContext);
151
152 KMenu* popup = new KMenu(m_mainWindow);
153
154 addShowMenubarAction(popup);
155
156 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
157 popup->addAction(restoreAction);
158
159 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
160 popup->addAction(deleteAction);
161
162 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
163 popup->addAction(propertiesAction);
164
165 if (popup->exec(QCursor::pos()) == restoreAction) {
166 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
167 }
168
169 popup->deleteLater();
170 }
171
172 void DolphinContextMenu::openItemContextMenu()
173 {
174 Q_ASSERT(!m_fileInfo.isNull());
175
176 KMenu* popup = new KMenu(m_mainWindow);
177 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
178 // setup 'Create New' menu
179 DolphinNewMenu* newMenu = new DolphinNewMenu(popup, m_mainWindow);
180 newMenu->slotCheckUpToDate();
181 newMenu->setPopupFiles(m_fileInfo.url());
182 newMenu->setEnabled(capabilities().supportsWriting());
183
184 KMenu* menu = newMenu->menu();
185 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
186 menu->setIcon(KIcon("document-new"));
187 popup->addMenu(newMenu->menu());
188 popup->addSeparator();
189
190 // insert 'Open in new window' and 'Open in new tab' entries
191 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
192 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
193 popup->addSeparator();
194 }
195 addShowMenubarAction(popup);
196 insertDefaultItemActions(popup);
197
198 popup->addSeparator();
199
200 // insert 'Bookmark This Folder' entry if exactly one item is selected
201 QAction* addToPlacesAction = 0;
202 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
203 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
204 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
205 }
206
207 KonqPopupMenuInformation popupInfo;
208 popupInfo.setItems(m_selectedItems);
209 popupInfo.setParentWidget(m_mainWindow);
210 KonqMenuActions menuActions;
211 menuActions.setPopupMenuInfo(popupInfo);
212
213 // Insert 'Open With...' action or sub menu
214 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
215
216 // Insert 'Actions' sub menu
217 if (menuActions.addActionsTo(popup)) {
218 popup->addSeparator();
219 }
220
221 // Insert 'Copy To' and 'Move To' sub menus
222 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
223 m_copyToMenu.setItems(m_selectedItems);
224 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
225 m_copyToMenu.addActionsTo(popup);
226 popup->addSeparator();
227 }
228
229 // insert 'Properties...' entry
230 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
231 popup->addAction(propertiesAction);
232
233 QAction* activatedAction = popup->exec(QCursor::pos());
234
235 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
236 const KUrl selectedUrl(m_fileInfo.url());
237 if (selectedUrl.isValid()) {
238 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
239 selectedUrl);
240 }
241 }
242
243 popup->deleteLater();
244 }
245
246 void DolphinContextMenu::openViewportContextMenu()
247 {
248 KMenu* popup = new KMenu(m_mainWindow);
249
250 addShowMenubarAction(popup);
251
252 // setup 'Create New' menu
253 KNewMenu* newMenu = m_mainWindow->newMenu();
254 newMenu->slotCheckUpToDate();
255 newMenu->setPopupFiles(m_baseUrl);
256 popup->addMenu(newMenu->menu());
257 popup->addSeparator();
258
259 QAction* pasteAction = createPasteAction();
260 popup->addAction(pasteAction);
261
262 // setup 'View Mode' menu
263 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
264
265 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
266 viewModeMenu->addAction(iconsMode);
267
268 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
269 viewModeMenu->addAction(detailsMode);
270
271 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
272 viewModeMenu->addAction(columnsMode);
273
274 popup->addMenu(viewModeMenu);
275
276 popup->addSeparator();
277
278 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
279 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
280 popup->addSeparator();
281
282 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
283
284 QAction* action = popup->exec(QCursor::pos());
285 if (action == propertiesAction) {
286 const KUrl& url = m_mainWindow->activeViewContainer()->url();
287
288 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
289 dialog->setAttribute(Qt::WA_DeleteOnClose);
290 dialog->show();
291 } else if (action == addToPlacesAction) {
292 const KUrl& url = m_mainWindow->activeViewContainer()->url();
293 if (url.isValid()) {
294 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
295 }
296 }
297
298 popup->deleteLater();
299 }
300
301 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
302 {
303 Q_ASSERT(popup != 0);
304 const KActionCollection* collection = m_mainWindow->actionCollection();
305
306 // insert 'Cut', 'Copy' and 'Paste'
307 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
308 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
309 QAction* pasteAction = createPasteAction();
310
311 popup->addAction(cutAction);
312 popup->addAction(copyAction);
313 popup->addAction(pasteAction);
314 popup->addSeparator();
315
316 // insert 'Rename'
317 QAction* renameAction = collection->action("rename");
318 popup->addAction(renameAction);
319
320 // insert 'Move to Trash' and (optionally) 'Delete'
321 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
322 KConfigGroup configGroup(globalConfig, "KDE");
323 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
324
325 const KUrl& url = m_mainWindow->activeViewContainer()->url();
326 if (url.isLocalFile()) {
327 QAction* moveToTrashAction = collection->action("move_to_trash");
328 popup->addAction(moveToTrashAction);
329 } else {
330 showDeleteCommand = true;
331 }
332
333 if (showDeleteCommand) {
334 QAction* deleteAction = collection->action("delete");
335 popup->addAction(deleteAction);
336 }
337 }
338
339 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
340 {
341 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
342 if (!m_mainWindow->menuBar()->isVisible()) {
343 menu->addAction(showMenuBar);
344 menu->addSeparator();
345 }
346 }
347
348 QString DolphinContextMenu::placesName(const KUrl& url) const
349 {
350 QString name = url.fileName();
351 if (name.isEmpty()) {
352 name = url.host();
353 }
354 return name;
355 }
356
357 QAction* DolphinContextMenu::createPasteAction()
358 {
359 QAction* action = 0;
360 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
361 if (isDir && (m_selectedItems.count() == 1)) {
362 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
363 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
364 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
365 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
366 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
367 } else {
368 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
369 }
370
371 return action;
372 }
373
374 KonqFileItemCapabilities& DolphinContextMenu::capabilities()
375 {
376 if (m_capabilities == 0) {
377 m_capabilities = new KonqFileItemCapabilities(m_selectedItems);
378 }
379 return *m_capabilities;
380 }
381
382 #include "dolphincontextmenu.moc"