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