]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
7b8f1db30bb7e88b1d565e98ffffc2bd757f70ee
[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 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup);
169 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
170 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
171 m_popup->addAction(emptyTrashAction);
172
173 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
174 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
175
176 // Don't show if url is already in places
177 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
178 addToPlacesAction->setVisible(false);
179 }
180
181 addCustomActions();
182
183 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
184 m_popup->addAction(propertiesAction);
185
186 addShowMenubarAction();
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 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
214 m_popup->addAction(restoreAction);
215
216 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
217 m_popup->addAction(deleteAction);
218
219 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
220 m_popup->addAction(propertiesAction);
221
222 if (m_popup->exec(QCursor::pos()) == restoreAction) {
223 KUrl::List selectedUrls;
224 foreach (const KFileItem &item, m_selectedItems) {
225 selectedUrls.append(item.url());
226 }
227
228 KonqOperations::restoreTrashedItems(selectedUrls, m_mainWindow);
229 }
230 }
231
232 void DolphinContextMenu::openItemContextMenu()
233 {
234 Q_ASSERT(!m_fileInfo.isNull());
235
236 QAction* openParentInNewWindowAction = 0;
237 QAction* openParentInNewTabAction = 0;
238 QAction* addToPlacesAction = 0;
239 if (m_selectedItems.count() == 1) {
240 if (m_fileInfo.isDir()) {
241 // setup 'Create New' menu
242 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup, m_mainWindow);
243 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
244 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
245 newFileMenu->checkUpToDate();
246 newFileMenu->setPopupFiles(m_fileInfo.url());
247 newFileMenu->setEnabled(selectedItemsProperties().supportsWriting());
248
249 KMenu* menu = newFileMenu->menu();
250 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
251 menu->setIcon(KIcon("document-new"));
252 m_popup->addMenu(menu);
253 m_popup->addSeparator();
254
255 // insert 'Open in new window' and 'Open in new tab' entries
256 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
257 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
258
259 // insert 'Add to Places' entry
260 if (!placeExists(m_fileInfo.url())) {
261 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
262 i18nc("@action:inmenu Add selected folder to places",
263 "Add to Places"));
264 }
265
266 m_popup->addSeparator();
267 } else if (m_baseUrl.protocol().contains("search")) {
268 openParentInNewWindowAction = new QAction(KIcon("window-new"),
269 i18nc("@action:inmenu",
270 "Open Path in New Window"),
271 this);
272 m_popup->addAction(openParentInNewWindowAction);
273
274 openParentInNewTabAction = new QAction(KIcon("tab-new"),
275 i18nc("@action:inmenu",
276 "Open Path in New Tab"),
277 this);
278 m_popup->addAction(openParentInNewTabAction);
279
280 m_popup->addSeparator();
281 }
282 }
283
284 insertDefaultItemActions();
285
286 m_popup->addSeparator();
287
288 KFileItemActions fileItemActions;
289 fileItemActions.setItemListProperties(selectedItemsProperties());
290 addServiceActions(fileItemActions);
291
292 addFileItemPluginActions();
293
294 addVersionControlPluginActions();
295
296 // insert 'Copy To' and 'Move To' sub menus
297 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
298 m_copyToMenu.setItems(m_selectedItems);
299 m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting());
300 m_copyToMenu.addActionsTo(m_popup);
301 }
302
303 // insert 'Properties...' entry
304 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
305 m_popup->addAction(propertiesAction);
306
307 QAction* activatedAction = m_popup->exec(QCursor::pos());
308 if (activatedAction != 0) {
309 if (activatedAction == addToPlacesAction) {
310 const KUrl selectedUrl(m_fileInfo.url());
311 if (selectedUrl.isValid()) {
312 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
313 selectedUrl);
314 }
315 } else if (activatedAction == openParentInNewWindowAction) {
316 m_command = OpenParentFolderInNewWindow;
317 } else if (activatedAction == openParentInNewTabAction) {
318 m_command = OpenParentFolderInNewTab;
319 }
320 }
321 }
322
323 void DolphinContextMenu::openViewportContextMenu()
324 {
325 // setup 'Create New' menu
326 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
327 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
328 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
329 newFileMenu->checkUpToDate();
330 newFileMenu->setPopupFiles(m_baseUrl);
331 m_popup->addMenu(newFileMenu->menu());
332 m_popup->addSeparator();
333
334 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
335 // "open_in_new_tab" here, as the current selection should get ignored.
336 m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
337 m_popup->addAction(m_mainWindow->actionCollection()->action("new_tab"));
338
339 // Insert 'Add to Places' entry if exactly one item is selected
340 QAction* addToPlacesAction = 0;
341 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
342 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
343 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
344 }
345
346 m_popup->addSeparator();
347
348 QAction* pasteAction = createPasteAction();
349 m_popup->addAction(pasteAction);
350 m_popup->addSeparator();
351
352 // Insert service actions
353 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
354 KFileItemActions fileItemActions;
355 fileItemActions.setItemListProperties(baseUrlProperties);
356 addServiceActions(fileItemActions);
357
358 addFileItemPluginActions();
359
360 addVersionControlPluginActions();
361
362 addCustomActions();
363
364 QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
365 propertiesAction->setIcon(KIcon("document-properties"));
366
367 addShowMenubarAction();
368
369 QAction* action = m_popup->exec(QCursor::pos());
370 if (action == propertiesAction) {
371 const KUrl& url = m_mainWindow->activeViewContainer()->url();
372
373 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
374 dialog->setAttribute(Qt::WA_DeleteOnClose);
375 dialog->show();
376 } else if ((addToPlacesAction != 0) && (action == addToPlacesAction)) {
377 const KUrl& url = m_mainWindow->activeViewContainer()->url();
378 if (url.isValid()) {
379 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
380 }
381 }
382 }
383
384 void DolphinContextMenu::insertDefaultItemActions()
385 {
386 const KActionCollection* collection = m_mainWindow->actionCollection();
387
388 // Insert 'Cut', 'Copy' and 'Paste'
389 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
390 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
391 m_popup->addAction(createPasteAction());
392
393 m_popup->addSeparator();
394
395 // Insert 'Rename'
396 QAction* renameAction = collection->action("rename");
397 m_popup->addAction(renameAction);
398
399 // Insert 'Move to Trash' and/or 'Delete'
400 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
401 m_popup->addAction(collection->action("move_to_trash"));
402 m_popup->addAction(collection->action("delete"));
403 } else {
404 m_popup->addAction(m_removeAction);
405 updateRemoveAction();
406 }
407 }
408
409 void DolphinContextMenu::addShowMenubarAction()
410 {
411 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
412 if (!m_mainWindow->menuBar()->isVisible()) {
413 m_popup->addSeparator();
414 m_popup->addAction(showMenuBar);
415 }
416 }
417
418 QString DolphinContextMenu::placesName(const KUrl& url) const
419 {
420 QString name = url.fileName();
421 if (name.isEmpty()) {
422 name = url.host();
423 }
424 return name;
425 }
426
427 bool DolphinContextMenu::placeExists(const KUrl& url) const
428 {
429 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
430 const int count = placesModel->rowCount();
431
432 for (int i = 0; i < count; ++i) {
433 const QModelIndex index = placesModel->index(i, 0);
434
435 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
436 return true;
437 }
438 }
439 return false;
440 }
441
442 QAction* DolphinContextMenu::createPasteAction()
443 {
444 QAction* action = 0;
445 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
446 if (isDir && (m_selectedItems.count() == 1)) {
447 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
448 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
449 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
450 action->setEnabled(!pasteData.isEmpty() && selectedItemsProperties().supportsWriting());
451 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
452 } else {
453 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
454 }
455
456 return action;
457 }
458
459 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties()
460 {
461 if (m_selectedItemsProperties == 0) {
462 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
463 }
464 return *m_selectedItemsProperties;
465 }
466
467 KFileItem DolphinContextMenu::baseFileItem()
468 {
469 if (m_baseFileItem == 0) {
470 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
471 }
472 return *m_baseFileItem;
473 }
474
475 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
476 {
477 fileItemActions.setParentWidget(m_mainWindow);
478
479 // insert 'Open With...' action or sub menu
480 fileItemActions.addOpenWithActionsTo(m_popup, "DesktopEntryName != 'dolphin'");
481
482 // insert 'Actions' sub menu
483 fileItemActions.addServiceActionsTo(m_popup);
484 }
485
486 void DolphinContextMenu::addFileItemPluginActions()
487 {
488 KFileItemListProperties props;
489 if (m_selectedItems.isEmpty()) {
490 props.setItems(KFileItemList() << baseFileItem());
491 } else {
492 props = selectedItemsProperties();
493 }
494
495 QString commonMimeType = props.mimeType();
496 if (commonMimeType.isEmpty()) {
497 commonMimeType = QLatin1String("application/octet-stream");
498 }
499
500 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
501 if (pluginServices.isEmpty()) {
502 return;
503 }
504
505 const KConfig config("kservicemenurc", KConfig::NoGlobals);
506 const KConfigGroup showGroup = config.group("Show");
507
508 foreach (const KSharedPtr<KService>& service, pluginServices) {
509 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
510 // The plugin has been disabled
511 continue;
512 }
513
514 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
515 if (plugin == 0) {
516 continue;
517 }
518
519 plugin->setParent(m_popup);
520 const QList<QAction*> actions = plugin->actions(props, m_mainWindow);
521 foreach (QAction* action, actions) {
522 m_popup->addAction(action);
523 }
524 }
525 }
526
527 void DolphinContextMenu::addVersionControlPluginActions()
528 {
529 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
530 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
531 if (!versionControlActions.isEmpty()) {
532 foreach (QAction* action, versionControlActions) {
533 m_popup->addAction(action);
534 }
535 m_popup->addSeparator();
536 }
537 }
538
539 void DolphinContextMenu::addCustomActions()
540 {
541 foreach (QAction* action, m_customActions) {
542 m_popup->addAction(action);
543 }
544 }
545
546 void DolphinContextMenu::updateRemoveAction()
547 {
548 const KActionCollection* collection = m_mainWindow->actionCollection();
549 const bool moveToTrash = selectedItemsProperties().isLocal() && !m_shiftPressed;
550
551 // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
552 // This is only done until the original action has been shown at least once. To
553 // bypass this issue, the text and &-shortcut is applied manually.
554 const QAction* action = 0;
555 if (moveToTrash) {
556 action = collection->action("move_to_trash");
557 m_removeAction->setText(i18nc("@action:inmenu", "&Move to Trash"));
558 } else {
559 action = collection->action("delete");
560 m_removeAction->setText(i18nc("@action:inmenu", "&Delete"));
561 }
562 m_removeAction->setIcon(action->icon());
563 m_removeAction->setShortcuts(action->shortcuts());
564 }
565
566 #include "dolphincontextmenu.moc"