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