]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
ccfd290e007ab879f407ba4181ee388e68fc36ea
[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 "dolphinviewcontainer.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphinremoveaction.h"
28
29 #include <KActionCollection>
30 #include <KDesktopFile>
31 #include <kfileitemactionplugin.h>
32 #include <kabstractfileitemactionplugin.h>
33 #include <KFileItemActions>
34 #include <KFileItemListProperties>
35 #include <KGlobal>
36 #include <KIconLoader>
37 #include <KIO/RestoreJob>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
40 #include <KJobUiDelegate>
41 #include <KJobWidgets>
42 #include <KMenu>
43 #include <KMenuBar>
44 #include <KMessageBox>
45 #include <KMimeTypeTrader>
46 #include <KMimeType>
47 #include <KNewFileMenu>
48 #include <konq_operations.h>
49 #include <KService>
50 #include <KLocale>
51 #include <KPropertiesDialog>
52 #include <KStandardAction>
53 #include <KToolBar>
54
55 #include <panels/places/placesitem.h>
56 #include <panels/places/placesitemmodel.h>
57
58 #include <QApplication>
59 #include <QClipboard>
60 #include <QDir>
61
62 #include "views/dolphinview.h"
63 #include "views/viewmodecontroller.h"
64
65 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
66 const QPoint& pos,
67 const KFileItem& fileInfo,
68 const KUrl& baseUrl) :
69 KMenu(parent),
70 m_pos(pos),
71 m_mainWindow(parent),
72 m_fileInfo(fileInfo),
73 m_baseUrl(baseUrl),
74 m_baseFileItem(0),
75 m_selectedItems(),
76 m_selectedItemsProperties(0),
77 m_context(NoContext),
78 m_copyToMenu(parent),
79 m_customActions(),
80 m_command(None),
81 m_removeAction(0)
82 {
83 // The context menu either accesses the URLs of the selected items
84 // or the items itself. To increase the performance both lists are cached.
85 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
86 m_selectedItems = view->selectedItems();
87 }
88
89 DolphinContextMenu::~DolphinContextMenu()
90 {
91 delete m_selectedItemsProperties;
92 m_selectedItemsProperties = 0;
93 }
94
95 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
96 {
97 m_customActions = actions;
98 }
99
100 DolphinContextMenu::Command DolphinContextMenu::open()
101 {
102 // get the context information
103 if (m_baseUrl.protocol() == QLatin1String("trash")) {
104 m_context |= TrashContext;
105 }
106
107 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
108 m_context |= ItemContext;
109 // TODO: handle other use cases like devices + desktop files
110 }
111
112 // open the corresponding popup for the context
113 if (m_context & TrashContext) {
114 if (m_context & ItemContext) {
115 openTrashItemContextMenu();
116 } else {
117 openTrashContextMenu();
118 }
119 } else if (m_context & ItemContext) {
120 openItemContextMenu();
121 } else {
122 Q_ASSERT(m_context == NoContext);
123 openViewportContextMenu();
124 }
125
126 return m_command;
127 }
128
129 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
130 {
131 if (m_removeAction && ev->key() == Qt::Key_Shift) {
132 m_removeAction->update();
133 }
134 KMenu::keyPressEvent(ev);
135 }
136
137 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
138 {
139 if (m_removeAction && ev->key() == Qt::Key_Shift) {
140 m_removeAction->update();
141 }
142 KMenu::keyReleaseEvent(ev);
143 }
144
145 void DolphinContextMenu::openTrashContextMenu()
146 {
147 Q_ASSERT(m_context & TrashContext);
148
149 QAction* emptyTrashAction = new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
150 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
151 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
152 addAction(emptyTrashAction);
153
154 addCustomActions();
155
156 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
157 addAction(propertiesAction);
158
159 addShowMenuBarAction();
160
161 if (exec(m_pos) == emptyTrashAction) {
162 KIO::JobUiDelegate uiDelegate;
163 uiDelegate.setWindow(m_mainWindow);
164 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
165 KIO::Job* job = KIO::emptyTrash();
166 KJobWidgets::setWindow(job, m_mainWindow);
167 job->ui()->setAutoErrorHandlingEnabled(true);
168 }
169 }
170 }
171
172 void DolphinContextMenu::openTrashItemContextMenu()
173 {
174 Q_ASSERT(m_context & TrashContext);
175 Q_ASSERT(m_context & ItemContext);
176
177 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
178 addAction(restoreAction);
179
180 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
181 addAction(deleteAction);
182
183 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
184 addAction(propertiesAction);
185
186 if (exec(m_pos) == restoreAction) {
187 KUrl::List selectedUrls;
188 foreach (const KFileItem &item, m_selectedItems) {
189 selectedUrls.append(item.url());
190 }
191
192 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
193 KJobWidgets::setWindow(job, m_mainWindow);
194 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
195 }
196 }
197
198 void DolphinContextMenu::openItemContextMenu()
199 {
200 Q_ASSERT(!m_fileInfo.isNull());
201
202 QAction* openParentAction = 0;
203 QAction* openParentInNewWindowAction = 0;
204 QAction* openParentInNewTabAction = 0;
205 QAction* addToPlacesAction = 0;
206 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
207
208 if (m_selectedItems.count() == 1) {
209 if (m_fileInfo.isDir()) {
210 // setup 'Create New' menu
211 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
212 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
213 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
214 newFileMenu->checkUpToDate();
215 newFileMenu->setPopupFiles(m_fileInfo.url());
216 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
217 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
218 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
219
220 QMenu* menu = newFileMenu->menu();
221 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
222 menu->setIcon(QIcon::fromTheme("document-new"));
223 addMenu(menu);
224 addSeparator();
225
226 // insert 'Open in new window' and 'Open in new tab' entries
227 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
228 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
229
230 // insert 'Add to Places' entry
231 if (!placeExists(m_fileInfo.url())) {
232 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
233 i18nc("@action:inmenu Add selected folder to places",
234 "Add to Places"));
235 }
236
237 addSeparator();
238 } else if (m_baseUrl.protocol().contains("search") || m_baseUrl.protocol().contains("timeline")) {
239 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
240 i18nc("@action:inmenu",
241 "Open Path"),
242 this);
243 addAction(openParentAction);
244
245 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
246 i18nc("@action:inmenu",
247 "Open Path in New Window"),
248 this);
249 addAction(openParentInNewWindowAction);
250
251 openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"),
252 i18nc("@action:inmenu",
253 "Open Path in New Tab"),
254 this);
255 addAction(openParentInNewTabAction);
256
257 addSeparator();
258 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
259 // insert 'Open in new window' and 'Open in new tab' entries
260 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
261 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
262
263 addSeparator();
264 }
265 } else {
266 bool selectionHasOnlyDirs = true;
267 foreach (const KFileItem& item, m_selectedItems) {
268 const KUrl& url = DolphinView::openItemAsFolderUrl(item);
269 if (url.isEmpty()) {
270 selectionHasOnlyDirs = false;
271 break;
272 }
273 }
274
275 if (selectionHasOnlyDirs) {
276 // insert 'Open in new tab' entry
277 addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs"));
278 addSeparator();
279 }
280 }
281
282 insertDefaultItemActions(selectedItemsProps);
283
284 addSeparator();
285
286 KFileItemActions fileItemActions;
287 fileItemActions.setItemListProperties(selectedItemsProps);
288 addServiceActions(fileItemActions);
289
290 addFileItemPluginActions();
291
292 addVersionControlPluginActions();
293
294 // insert 'Copy To' and 'Move To' sub menus
295 if (GeneralSettings::showCopyMoveMenu()) {
296 m_copyToMenu.setItems(m_selectedItems);
297 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
298 m_copyToMenu.addActionsTo(this);
299 }
300
301 // insert 'Properties...' entry
302 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
303 addAction(propertiesAction);
304
305 QAction* activatedAction = exec(m_pos);
306 if (activatedAction) {
307 if (activatedAction == addToPlacesAction) {
308 const KUrl selectedUrl(m_fileInfo.url());
309 if (selectedUrl.isValid()) {
310 PlacesItemModel model;
311 const QString text = selectedUrl.fileName();
312 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
313 model.appendItemToGroup(item);
314 }
315 } else if (activatedAction == openParentAction) {
316 m_command = OpenParentFolder;
317 } else if (activatedAction == openParentInNewWindowAction) {
318 m_command = OpenParentFolderInNewWindow;
319 } else if (activatedAction == openParentInNewTabAction) {
320 m_command = OpenParentFolderInNewTab;
321 }
322 }
323 }
324
325 void DolphinContextMenu::openViewportContextMenu()
326 {
327 // setup 'Create New' menu
328 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
329 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
330 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
331 newFileMenu->checkUpToDate();
332 newFileMenu->setPopupFiles(m_baseUrl);
333 addMenu(newFileMenu->menu());
334 addSeparator();
335
336 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
337 // "open_in_new_tab" here, as the current selection should get ignored.
338 addAction(m_mainWindow->actionCollection()->action("new_window"));
339 addAction(m_mainWindow->actionCollection()->action("new_tab"));
340
341 // Insert 'Add to Places' entry if exactly one item is selected
342 QAction* addToPlacesAction = 0;
343 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
344 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
345 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
346 }
347
348 addSeparator();
349
350 QAction* pasteAction = createPasteAction();
351 addAction(pasteAction);
352 addSeparator();
353
354 // Insert service actions
355 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
356 KFileItemActions fileItemActions;
357 fileItemActions.setItemListProperties(baseUrlProperties);
358 addServiceActions(fileItemActions);
359
360 addFileItemPluginActions();
361
362 addVersionControlPluginActions();
363
364 addCustomActions();
365
366 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
367 addAction(propertiesAction);
368
369 addShowMenuBarAction();
370
371 QAction* action = exec(m_pos);
372 if (addToPlacesAction && (action == addToPlacesAction)) {
373 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
374 if (container->url().isValid()) {
375 PlacesItemModel model;
376 PlacesItem* item = model.createPlacesItem(container->placesText(),
377 container->url());
378 model.appendItemToGroup(item);
379 }
380 }
381 }
382
383 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
384 {
385 const KActionCollection* collection = m_mainWindow->actionCollection();
386
387 // Insert 'Cut', 'Copy' and 'Paste'
388 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
389 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
390 addAction(createPasteAction());
391
392 addSeparator();
393
394 // Insert 'Rename'
395 QAction* renameAction = collection->action("rename");
396 addAction(renameAction);
397
398 // Insert 'Move to Trash' and/or 'Delete'
399 if (properties.supportsDeleting()) {
400 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
401 !properties.isLocal());
402 const bool showMoveToTrashAction = (properties.isLocal() &&
403 properties.supportsMoving());
404
405 if (showDeleteAction && showMoveToTrashAction) {
406 delete m_removeAction;
407 m_removeAction = 0;
408 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
409 addAction(m_mainWindow->actionCollection()->action("delete"));
410 } else if (showDeleteAction && !showMoveToTrashAction) {
411 addAction(m_mainWindow->actionCollection()->action("delete"));
412 } else {
413 if (!m_removeAction) {
414 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
415 }
416 addAction(m_removeAction);
417 m_removeAction->update();
418 }
419 }
420 }
421
422 void DolphinContextMenu::addShowMenuBarAction()
423 {
424 const KActionCollection* ac = m_mainWindow->actionCollection();
425 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
426 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
427 addSeparator();
428 addAction(showMenuBar);
429 }
430 }
431
432 bool DolphinContextMenu::placeExists(const KUrl& url) const
433 {
434 PlacesItemModel model;
435
436 const int count = model.count();
437 for (int i = 0; i < count; ++i) {
438 const KUrl placeUrl = model.placesItem(i)->url();
439 if (placeUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
440 return true;
441 }
442 }
443
444 return false;
445 }
446
447 QAction* DolphinContextMenu::createPasteAction()
448 {
449 QAction* action = 0;
450 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
451 if (isDir && (m_selectedItems.count() == 1)) {
452 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileInfo.url());
453 action = new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
454 action->setEnabled(pasteInfo.first);
455 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
456 } else {
457 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
458 }
459
460 return action;
461 }
462
463 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
464 {
465 if (!m_selectedItemsProperties) {
466 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
467 }
468 return *m_selectedItemsProperties;
469 }
470
471 KFileItem DolphinContextMenu::baseFileItem()
472 {
473 if (!m_baseFileItem) {
474 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
475 }
476 return *m_baseFileItem;
477 }
478
479 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
480 {
481 fileItemActions.setParentWidget(m_mainWindow);
482
483 // insert 'Open With...' action or sub menu
484 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
485
486 // insert 'Actions' sub menu
487 fileItemActions.addServiceActionsTo(this);
488 }
489
490 void DolphinContextMenu::addFileItemPluginActions()
491 {
492 KFileItemListProperties props;
493 if (m_selectedItems.isEmpty()) {
494 props.setItems(KFileItemList() << baseFileItem());
495 } else {
496 props = selectedItemsProperties();
497 }
498
499 QString commonMimeType = props.mimeType();
500 if (commonMimeType.isEmpty()) {
501 commonMimeType = QLatin1String("application/octet-stream");
502 }
503
504 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
505 if (pluginServices.isEmpty()) {
506 return;
507 }
508
509 const KConfig config("kservicemenurc", KConfig::NoGlobals);
510 const KConfigGroup showGroup = config.group("Show");
511
512 foreach (const KService::Ptr& service, pluginServices) {
513 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
514 // The plugin has been disabled
515 continue;
516 }
517
518 // Old API (kdelibs-4.6.0 only)
519 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
520 if (plugin) {
521 plugin->setParent(this);
522 addActions(plugin->actions(props, m_mainWindow));
523 }
524 // New API (kdelibs >= 4.6.1)
525 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
526 if (abstractPlugin) {
527 abstractPlugin->setParent(this);
528 addActions(abstractPlugin->actions(props, m_mainWindow));
529 }
530 }
531 }
532
533 void DolphinContextMenu::addVersionControlPluginActions()
534 {
535 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
536 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
537 if (!versionControlActions.isEmpty()) {
538 foreach (QAction* action, versionControlActions) {
539 addAction(action);
540 }
541 addSeparator();
542 }
543 }
544
545 void DolphinContextMenu::addCustomActions()
546 {
547 foreach (QAction* action, m_customActions) {
548 addAction(action);
549 }
550 }
551