]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Internal cleanup: Remove public method selectedUrls() from DolphinView to keep the...
[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 "dolphinnewfilemenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kdesktopfile.h>
31 #include <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.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 <kmodifierkeyinfo.h>
41 #include <knewfilemenu.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <kfileitemactions.h>
45 #include <klocale.h>
46 #include <kpropertiesdialog.h>
47 #include <kstandardaction.h>
48 #include <kstandarddirs.h>
49
50 #include <QtGui/QApplication>
51 #include <QtGui/QClipboard>
52 #include <QtCore/QDir>
53
54 #include "views/dolphinview.h"
55 #include "views/viewmodecontroller.h"
56
57 K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
58
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
60 const KFileItem& fileInfo,
61 const KUrl& baseUrl) :
62 m_mainWindow(parent),
63 m_capabilities(0),
64 m_fileInfo(fileInfo),
65 m_baseUrl(baseUrl),
66 m_context(NoContext),
67 m_copyToMenu(parent),
68 m_customActions(),
69 m_popup(new KMenu(m_mainWindow)),
70 m_command(None),
71 m_shiftPressed(false),
72 m_removeAction(0)
73 {
74 // The context menu either accesses the URLs of the selected items
75 // or the items itself. To increase the performance both lists are cached.
76 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
77 m_selectedItems = view->selectedItems();
78 foreach (const KFileItem &item, m_selectedItems) {
79 m_selectedUrls.append(item.url());
80 }
81
82 if (m_keyInfo != 0) {
83 if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
84 m_shiftPressed = true;
85 }
86 connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)),
87 this, SLOT(slotKeyModifierPressed(Qt::Key, bool)));
88 }
89
90 m_removeAction = new QAction(this);
91 connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
92 }
93
94 DolphinContextMenu::~DolphinContextMenu()
95 {
96 delete m_capabilities;
97 m_capabilities = 0;
98 }
99
100 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
101 {
102 m_customActions = actions;
103 }
104
105 DolphinContextMenu::Command DolphinContextMenu::open()
106 {
107 // get the context information
108 if (m_baseUrl.protocol() == QLatin1String("trash")) {
109 m_context |= TrashContext;
110 }
111
112 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
113 m_context |= ItemContext;
114 // TODO: handle other use cases like devices + desktop files
115 }
116
117 // open the corresponding popup for the context
118 if (m_context & TrashContext) {
119 if (m_context & ItemContext) {
120 openTrashItemContextMenu();
121 } else {
122 openTrashContextMenu();
123 }
124 } else if (m_context & ItemContext) {
125 openItemContextMenu();
126 } else {
127 Q_ASSERT(m_context == NoContext);
128 openViewportContextMenu();
129 }
130
131 return m_command;
132 }
133
134 void DolphinContextMenu::initializeModifierKeyInfo()
135 {
136 // Access m_keyInfo, so that it gets instantiated by
137 // K_GLOBAL_STATIC
138 KModifierKeyInfo* keyInfo = m_keyInfo;
139 Q_UNUSED(keyInfo);
140 }
141
142 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
143 {
144 m_shiftPressed = (key == Qt::Key_Shift) && pressed;
145 updateRemoveAction();
146 }
147
148 void DolphinContextMenu::slotRemoveActionTriggered()
149 {
150 const KActionCollection* collection = m_mainWindow->actionCollection();
151 if (m_shiftPressed) {
152 collection->action("delete")->trigger();
153 } else {
154 collection->action("move_to_trash")->trigger();
155 }
156 }
157
158 void DolphinContextMenu::openTrashContextMenu()
159 {
160 Q_ASSERT(m_context & TrashContext);
161
162 addShowMenubarAction();
163
164 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup.data());
165 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
166 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
167 m_popup->addAction(emptyTrashAction);
168
169 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
170 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
171
172 // Don't show if url is already in places
173 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
174 addToPlacesAction->setVisible(false);
175 }
176
177 addCustomActions();
178
179 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
180 m_popup->addAction(propertiesAction);
181
182 QAction *action = m_popup->exec(QCursor::pos());
183 if (action == emptyTrashAction) {
184 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
185 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
186 text,
187 QString(),
188 KGuiItem(i18nc("@action:button", "Empty Trash"),
189 KIcon("user-trash"))
190 ) == KMessageBox::Continue;
191 if (del) {
192 KonqOperations::emptyTrash(m_mainWindow);
193 }
194 } else if (action == addToPlacesAction) {
195 const KUrl& url = m_mainWindow->activeViewContainer()->url();
196 if (url.isValid()) {
197 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
198 }
199 }
200 }
201
202 void DolphinContextMenu::openTrashItemContextMenu()
203 {
204 Q_ASSERT(m_context & TrashContext);
205 Q_ASSERT(m_context & ItemContext);
206
207 addShowMenubarAction();
208
209 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
210 m_popup->addAction(restoreAction);
211
212 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
213 m_popup->addAction(deleteAction);
214
215 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
216 m_popup->addAction(propertiesAction);
217
218 if (m_popup->exec(QCursor::pos()) == restoreAction) {
219 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
220 }
221 }
222
223 void DolphinContextMenu::openItemContextMenu()
224 {
225 Q_ASSERT(!m_fileInfo.isNull());
226
227 QAction* openParentInNewWindowAction = 0;
228 QAction* openParentInNewTabAction = 0;
229 QAction* addToPlacesAction = 0;
230 if (m_selectedUrls.count() == 1) {
231 if (m_fileInfo.isDir()) {
232 // setup 'Create New' menu
233 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
234 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
235 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
236 newFileMenu->checkUpToDate();
237 newFileMenu->setPopupFiles(m_fileInfo.url());
238 newFileMenu->setEnabled(capabilities().supportsWriting());
239
240 KMenu* menu = newFileMenu->menu();
241 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
242 menu->setIcon(KIcon("document-new"));
243 m_popup->addMenu(menu);
244 m_popup->addSeparator();
245
246 // insert 'Open in new window' and 'Open in new tab' entries
247 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
248 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
249
250 // insert 'Add to Places' entry
251 if (!placeExists(m_fileInfo.url())) {
252 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
253 i18nc("@action:inmenu Add selected folder to places",
254 "Add to Places"));
255 }
256
257 m_popup->addSeparator();
258 } else if (m_baseUrl.protocol().contains("search")) {
259 openParentInNewWindowAction = new QAction(KIcon("window-new"),
260 i18nc("@action:inmenu",
261 "Open Path in New Window"),
262 this);
263 m_popup->addAction(openParentInNewWindowAction);
264
265 openParentInNewTabAction = new QAction(KIcon("tab-new"),
266 i18nc("@action:inmenu",
267 "Open Path in New Tab"),
268 this);
269 m_popup->addAction(openParentInNewTabAction);
270
271 m_popup->addSeparator();
272 }
273 }
274
275 addShowMenubarAction();
276 insertDefaultItemActions();
277
278 m_popup->addSeparator();
279
280 KFileItemActions fileItemActions;
281 fileItemActions.setItemListProperties(capabilities());
282 addServiceActions(fileItemActions);
283
284 addVersionControlActions();
285
286 // insert 'Copy To' and 'Move To' sub menus
287 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
288 m_copyToMenu.setItems(m_selectedItems);
289 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
290 m_copyToMenu.addActionsTo(m_popup.data());
291 }
292
293 // insert 'Properties...' entry
294 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
295 m_popup->addAction(propertiesAction);
296
297 QAction* activatedAction = m_popup->exec(QCursor::pos());
298 if (activatedAction != 0) {
299 if (activatedAction == addToPlacesAction) {
300 const KUrl selectedUrl(m_fileInfo.url());
301 if (selectedUrl.isValid()) {
302 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
303 selectedUrl);
304 }
305 } else if (activatedAction == openParentInNewWindowAction) {
306 m_command = OpenParentFolderInNewWindow;
307 } else if (activatedAction == openParentInNewTabAction) {
308 m_command = OpenParentFolderInNewTab;
309 }
310 }
311 }
312
313 void DolphinContextMenu::openViewportContextMenu()
314 {
315 addShowMenubarAction();
316
317 // setup 'Create New' menu
318 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
319 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
320 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
321 newFileMenu->checkUpToDate();
322 newFileMenu->setPopupFiles(m_baseUrl);
323 m_popup->addMenu(newFileMenu->menu());
324 m_popup->addSeparator();
325
326 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
327 // "open_in_new_tab" here, as the current selection should get ignored.
328 m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
329 m_popup->addAction(m_mainWindow->actionCollection()->action("new_tab"));
330
331 // Insert 'Add to Places' entry if exactly one item is selected
332 QAction* addToPlacesAction = 0;
333 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
334 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
335 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
336 }
337
338 m_popup->addSeparator();
339
340 QAction* pasteAction = createPasteAction();
341 m_popup->addAction(pasteAction);
342 m_popup->addSeparator();
343
344 // insert service actions
345 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
346 const KFileItemListProperties baseUrlProperties(KFileItemList() << item);
347 KFileItemActions fileItemActions;
348 fileItemActions.setItemListProperties(baseUrlProperties);
349 addServiceActions(fileItemActions);
350
351 addVersionControlActions();
352
353 addCustomActions();
354
355 QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
356 propertiesAction->setIcon(KIcon("document-properties"));
357 QAction* action = m_popup->exec(QCursor::pos());
358 if (action == propertiesAction) {
359 const KUrl& url = m_mainWindow->activeViewContainer()->url();
360
361 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
362 dialog->setAttribute(Qt::WA_DeleteOnClose);
363 dialog->show();
364 } else if ((addToPlacesAction != 0) && (action == addToPlacesAction)) {
365 const KUrl& url = m_mainWindow->activeViewContainer()->url();
366 if (url.isValid()) {
367 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
368 }
369 }
370 }
371
372 void DolphinContextMenu::insertDefaultItemActions()
373 {
374 const KActionCollection* collection = m_mainWindow->actionCollection();
375
376 // Insert 'Cut', 'Copy' and 'Paste'
377 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
378 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
379 m_popup->addAction(createPasteAction());
380
381 m_popup->addSeparator();
382
383 // Insert 'Rename'
384 QAction* renameAction = collection->action("rename");
385 m_popup->addAction(renameAction);
386
387 // Insert 'Move to Trash' and/or 'Delete'
388 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
389 m_popup->addAction(collection->action("move_to_trash"));
390 m_popup->addAction(collection->action("delete"));
391 } else {
392 m_popup->addAction(m_removeAction);
393 updateRemoveAction();
394 }
395 }
396
397 void DolphinContextMenu::addShowMenubarAction()
398 {
399 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
400 if (!m_mainWindow->menuBar()->isVisible()) {
401 m_popup->addAction(showMenuBar);
402 m_popup->addSeparator();
403 }
404 }
405
406 QString DolphinContextMenu::placesName(const KUrl& url) const
407 {
408 QString name = url.fileName();
409 if (name.isEmpty()) {
410 name = url.host();
411 }
412 return name;
413 }
414
415 bool DolphinContextMenu::placeExists(const KUrl& url) const
416 {
417 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
418 const int count = placesModel->rowCount();
419
420 for (int i = 0; i < count; ++i) {
421 const QModelIndex index = placesModel->index(i, 0);
422
423 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
424 return true;
425 }
426 }
427 return false;
428 }
429
430 QAction* DolphinContextMenu::createPasteAction()
431 {
432 QAction* action = 0;
433 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
434 if (isDir && (m_selectedItems.count() == 1)) {
435 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
436 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
437 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
438 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
439 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
440 } else {
441 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
442 }
443
444 return action;
445 }
446
447 KFileItemListProperties& DolphinContextMenu::capabilities()
448 {
449 if (m_capabilities == 0) {
450 m_capabilities = new KFileItemListProperties(m_selectedItems);
451 }
452 return *m_capabilities;
453 }
454
455 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
456 {
457 fileItemActions.setParentWidget(m_mainWindow);
458
459 // insert 'Open With...' action or sub menu
460 fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'");
461
462 // insert 'Actions' sub menu
463 fileItemActions.addServiceActionsTo(m_popup.data());
464 }
465
466 void DolphinContextMenu::addVersionControlActions()
467 {
468 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
469 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
470 if (!versionControlActions.isEmpty()) {
471 foreach (QAction* action, versionControlActions) {
472 m_popup->addAction(action);
473 }
474 m_popup->addSeparator();
475 }
476 }
477
478 void DolphinContextMenu::addCustomActions()
479 {
480 foreach (QAction* action, m_customActions) {
481 m_popup->addAction(action);
482 }
483 }
484
485 void DolphinContextMenu::updateRemoveAction()
486 {
487 const KActionCollection* collection = m_mainWindow->actionCollection();
488 const bool moveToTrash = capabilities().isLocal() && !m_shiftPressed;
489 const QAction* action = moveToTrash ? collection->action("move_to_trash") : collection->action("delete");
490 m_removeAction->setText(action->text());
491 m_removeAction->setIcon(action->icon());
492 m_removeAction->setShortcuts(action->shortcuts());
493 }
494
495 #include "dolphincontextmenu.moc"