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