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