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