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