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