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