]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Ported KIcon to QIcon
[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* openParentAction = 0;
204 QAction* openParentInNewWindowAction = 0;
205 QAction* openParentInNewTabAction = 0;
206 QAction* addToPlacesAction = 0;
207 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
208
209 if (m_selectedItems.count() == 1) {
210 if (m_fileInfo.isDir()) {
211 // setup 'Create New' menu
212 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
213 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
214 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
215 newFileMenu->checkUpToDate();
216 newFileMenu->setPopupFiles(m_fileInfo.url());
217 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
218 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
219 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
220
221 QMenu* menu = newFileMenu->menu();
222 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
223 menu->setIcon(QIcon::fromTheme("document-new"));
224 addMenu(menu);
225 addSeparator();
226
227 // insert 'Open in new window' and 'Open in new tab' entries
228 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
229 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
230
231 // insert 'Add to Places' entry
232 if (!placeExists(m_fileInfo.url())) {
233 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
234 i18nc("@action:inmenu Add selected folder to places",
235 "Add to Places"));
236 }
237
238 addSeparator();
239 } else if (m_baseUrl.protocol().contains("search") || m_baseUrl.protocol().contains("timeline")) {
240 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
241 i18nc("@action:inmenu",
242 "Open Path"),
243 this);
244 addAction(openParentAction);
245
246 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
247 i18nc("@action:inmenu",
248 "Open Path in New Window"),
249 this);
250 addAction(openParentInNewWindowAction);
251
252 openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"),
253 i18nc("@action:inmenu",
254 "Open Path in New Tab"),
255 this);
256 addAction(openParentInNewTabAction);
257
258 addSeparator();
259 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
260 // insert 'Open in new window' and 'Open in new tab' entries
261 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
262 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
263
264 addSeparator();
265 }
266 } else {
267 bool selectionHasOnlyDirs = true;
268 foreach (const KFileItem& item, m_selectedItems) {
269 const KUrl& url = DolphinView::openItemAsFolderUrl(item);
270 if (url.isEmpty()) {
271 selectionHasOnlyDirs = false;
272 break;
273 }
274 }
275
276 if (selectionHasOnlyDirs) {
277 // insert 'Open in new tab' entry
278 addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs"));
279 addSeparator();
280 }
281 }
282
283 insertDefaultItemActions(selectedItemsProps);
284
285 addSeparator();
286
287 KFileItemActions fileItemActions;
288 fileItemActions.setItemListProperties(selectedItemsProps);
289 addServiceActions(fileItemActions);
290
291 addFileItemPluginActions();
292
293 addVersionControlPluginActions();
294
295 // insert 'Copy To' and 'Move To' sub menus
296 if (GeneralSettings::showCopyMoveMenu()) {
297 m_copyToMenu.setItems(m_selectedItems);
298 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
299 m_copyToMenu.addActionsTo(this);
300 }
301
302 // insert 'Properties...' entry
303 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
304 addAction(propertiesAction);
305
306 QAction* activatedAction = exec(m_pos);
307 if (activatedAction) {
308 if (activatedAction == addToPlacesAction) {
309 const KUrl selectedUrl(m_fileInfo.url());
310 if (selectedUrl.isValid()) {
311 PlacesItemModel model;
312 const QString text = selectedUrl.fileName();
313 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
314 model.appendItemToGroup(item);
315 }
316 } else if (activatedAction == openParentAction) {
317 m_command = OpenParentFolder;
318 } else if (activatedAction == openParentInNewWindowAction) {
319 m_command = OpenParentFolderInNewWindow;
320 } else if (activatedAction == openParentInNewTabAction) {
321 m_command = OpenParentFolderInNewTab;
322 }
323 }
324 }
325
326 void DolphinContextMenu::openViewportContextMenu()
327 {
328 // setup 'Create New' menu
329 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
330 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
331 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
332 newFileMenu->checkUpToDate();
333 newFileMenu->setPopupFiles(m_baseUrl);
334 addMenu(newFileMenu->menu());
335 addSeparator();
336
337 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
338 // "open_in_new_tab" here, as the current selection should get ignored.
339 addAction(m_mainWindow->actionCollection()->action("new_window"));
340 addAction(m_mainWindow->actionCollection()->action("new_tab"));
341
342 // Insert 'Add to Places' entry if exactly one item is selected
343 QAction* addToPlacesAction = 0;
344 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
345 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
346 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
347 }
348
349 addSeparator();
350
351 QAction* pasteAction = createPasteAction();
352 addAction(pasteAction);
353 addSeparator();
354
355 // Insert service actions
356 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
357 KFileItemActions fileItemActions;
358 fileItemActions.setItemListProperties(baseUrlProperties);
359 addServiceActions(fileItemActions);
360
361 addFileItemPluginActions();
362
363 addVersionControlPluginActions();
364
365 addCustomActions();
366
367 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
368 addAction(propertiesAction);
369
370 addShowMenuBarAction();
371
372 QAction* action = exec(m_pos);
373 if (addToPlacesAction && (action == addToPlacesAction)) {
374 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
375 if (container->url().isValid()) {
376 PlacesItemModel model;
377 PlacesItem* item = model.createPlacesItem(container->placesText(),
378 container->url());
379 model.appendItemToGroup(item);
380 }
381 }
382 }
383
384 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
385 {
386 const KActionCollection* collection = m_mainWindow->actionCollection();
387
388 // Insert 'Cut', 'Copy' and 'Paste'
389 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
390 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
391 addAction(createPasteAction());
392
393 addSeparator();
394
395 // Insert 'Rename'
396 QAction* renameAction = collection->action("rename");
397 addAction(renameAction);
398
399 // Insert 'Move to Trash' and/or 'Delete'
400 if (properties.supportsDeleting()) {
401 const bool showDeleteAction = (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
402 !properties.isLocal());
403 const bool showMoveToTrashAction = (properties.isLocal() &&
404 properties.supportsMoving());
405
406 if (showDeleteAction && showMoveToTrashAction) {
407 delete m_removeAction;
408 m_removeAction = 0;
409 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
410 addAction(m_mainWindow->actionCollection()->action("delete"));
411 } else if (showDeleteAction && !showMoveToTrashAction) {
412 addAction(m_mainWindow->actionCollection()->action("delete"));
413 } else {
414 if (!m_removeAction) {
415 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
416 }
417 addAction(m_removeAction);
418 m_removeAction->update();
419 }
420 }
421 }
422
423 void DolphinContextMenu::addShowMenuBarAction()
424 {
425 const KActionCollection* ac = m_mainWindow->actionCollection();
426 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
427 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
428 addSeparator();
429 addAction(showMenuBar);
430 }
431 }
432
433 bool DolphinContextMenu::placeExists(const KUrl& url) const
434 {
435 PlacesItemModel model;
436
437 const int count = model.count();
438 for (int i = 0; i < count; ++i) {
439 const KUrl placeUrl = model.placesItem(i)->url();
440 if (placeUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
441 return true;
442 }
443 }
444
445 return false;
446 }
447
448 QAction* DolphinContextMenu::createPasteAction()
449 {
450 QAction* action = 0;
451 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
452 if (isDir && (m_selectedItems.count() == 1)) {
453 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileInfo.url());
454 action = new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
455 action->setEnabled(pasteInfo.first);
456 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
457 } else {
458 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
459 }
460
461 return action;
462 }
463
464 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
465 {
466 if (!m_selectedItemsProperties) {
467 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
468 }
469 return *m_selectedItemsProperties;
470 }
471
472 KFileItem DolphinContextMenu::baseFileItem()
473 {
474 if (!m_baseFileItem) {
475 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
476 }
477 return *m_baseFileItem;
478 }
479
480 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
481 {
482 fileItemActions.setParentWidget(m_mainWindow);
483
484 // insert 'Open With...' action or sub menu
485 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
486
487 // insert 'Actions' sub menu
488 fileItemActions.addServiceActionsTo(this);
489 }
490
491 void DolphinContextMenu::addFileItemPluginActions()
492 {
493 KFileItemListProperties props;
494 if (m_selectedItems.isEmpty()) {
495 props.setItems(KFileItemList() << baseFileItem());
496 } else {
497 props = selectedItemsProperties();
498 }
499
500 QString commonMimeType = props.mimeType();
501 if (commonMimeType.isEmpty()) {
502 commonMimeType = QLatin1String("application/octet-stream");
503 }
504
505 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
506 if (pluginServices.isEmpty()) {
507 return;
508 }
509
510 const KConfig config("kservicemenurc", KConfig::NoGlobals);
511 const KConfigGroup showGroup = config.group("Show");
512
513 foreach (const KService::Ptr& service, pluginServices) {
514 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
515 // The plugin has been disabled
516 continue;
517 }
518
519 // Old API (kdelibs-4.6.0 only)
520 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
521 if (plugin) {
522 plugin->setParent(this);
523 addActions(plugin->actions(props, m_mainWindow));
524 }
525 // New API (kdelibs >= 4.6.1)
526 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
527 if (abstractPlugin) {
528 abstractPlugin->setParent(this);
529 addActions(abstractPlugin->actions(props, m_mainWindow));
530 }
531 }
532 }
533
534 void DolphinContextMenu::addVersionControlPluginActions()
535 {
536 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
537 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
538 if (!versionControlActions.isEmpty()) {
539 foreach (QAction* action, versionControlActions) {
540 addAction(action);
541 }
542 addSeparator();
543 }
544 }
545
546 void DolphinContextMenu::addCustomActions()
547 {
548 foreach (QAction* action, m_customActions) {
549 addAction(action);
550 }
551 }
552