]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Fix regression that creating a sub-folder is not possible
[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 "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kdesktopfile.h>
31 #include <kfileitemactionplugin.h>
32 #include <kabstractfileitemactionplugin.h>
33 #include <kfileitemactions.h>
34 #include <kfileitemlistproperties.h>
35 #include <kfileplacesmodel.h>
36 #include <kglobal.h>
37 #include <KIconLoader>
38 #include <kio/netaccess.h>
39 #include <kmenu.h>
40 #include <kmenubar.h>
41 #include <kmessagebox.h>
42 #include <kmimetypetrader.h>
43 #include <kmodifierkeyinfo.h>
44 #include <KNewFileMenu>
45 #include <konqmimedata.h>
46 #include <konq_operations.h>
47 #include <kservice.h>
48 #include <KLocale>
49 #include <kpropertiesdialog.h>
50 #include <kstandardaction.h>
51 #include <kstandarddirs.h>
52
53 #include <QtGui/QApplication>
54 #include <QtGui/QClipboard>
55 #include <QtCore/QDir>
56
57 #include "views/dolphinview.h"
58 #include "views/viewmodecontroller.h"
59
60 K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
61
62 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
63 const KFileItem& fileInfo,
64 const KUrl& baseUrl) :
65 m_mainWindow(parent),
66 m_fileInfo(fileInfo),
67 m_baseUrl(baseUrl),
68 m_baseFileItem(0),
69 m_selectedItems(),
70 m_selectedItemsProperties(0),
71 m_context(NoContext),
72 m_copyToMenu(parent),
73 m_customActions(),
74 m_popup(0),
75 m_command(None),
76 m_shiftPressed(false),
77 m_removeAction(0)
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 if (m_keyInfo) {
85 if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
86 m_shiftPressed = true;
87 }
88 connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)),
89 this, SLOT(slotKeyModifierPressed(Qt::Key, bool)));
90 }
91
92 m_removeAction = new QAction(this);
93 connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
94
95 m_popup = new KMenu(m_mainWindow);
96 }
97
98 DolphinContextMenu::~DolphinContextMenu()
99 {
100 delete m_selectedItemsProperties;
101 m_selectedItemsProperties = 0;
102
103 delete m_popup;
104 m_popup = 0;
105 }
106
107 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
108 {
109 m_customActions = actions;
110 }
111
112 DolphinContextMenu::Command DolphinContextMenu::open()
113 {
114 // get the context information
115 if (m_baseUrl.protocol() == QLatin1String("trash")) {
116 m_context |= TrashContext;
117 }
118
119 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
120 m_context |= ItemContext;
121 // TODO: handle other use cases like devices + desktop files
122 }
123
124 // open the corresponding popup for the context
125 if (m_context & TrashContext) {
126 if (m_context & ItemContext) {
127 openTrashItemContextMenu();
128 } else {
129 openTrashContextMenu();
130 }
131 } else if (m_context & ItemContext) {
132 openItemContextMenu();
133 } else {
134 Q_ASSERT(m_context == NoContext);
135 openViewportContextMenu();
136 }
137
138 return m_command;
139 }
140
141 void DolphinContextMenu::initializeModifierKeyInfo()
142 {
143 // Access m_keyInfo, so that it gets instantiated by
144 // K_GLOBAL_STATIC
145 KModifierKeyInfo* keyInfo = m_keyInfo;
146 Q_UNUSED(keyInfo);
147 }
148
149 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
150 {
151 m_shiftPressed = (key == Qt::Key_Shift) && pressed;
152 updateRemoveAction();
153 }
154
155 void DolphinContextMenu::slotRemoveActionTriggered()
156 {
157 const KActionCollection* collection = m_mainWindow->actionCollection();
158 if (m_shiftPressed) {
159 collection->action("delete")->trigger();
160 } else {
161 collection->action("move_to_trash")->trigger();
162 }
163 }
164
165 void DolphinContextMenu::openTrashContextMenu()
166 {
167 Q_ASSERT(m_context & TrashContext);
168
169 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup);
170 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
171 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
172 m_popup->addAction(emptyTrashAction);
173
174 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
175 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
176
177 // Don't show if url is already in places
178 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
179 addToPlacesAction->setVisible(false);
180 }
181
182 addCustomActions();
183
184 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
185 m_popup->addAction(propertiesAction);
186
187 addShowMenubarAction();
188
189 QAction *action = m_popup->exec(QCursor::pos());
190 if (action == emptyTrashAction) {
191 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
192 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
193 text,
194 QString(),
195 KGuiItem(i18nc("@action:button", "Empty Trash"),
196 KIcon("user-trash"))
197 ) == KMessageBox::Continue;
198 if (del) {
199 KonqOperations::emptyTrash(m_mainWindow);
200 }
201 } else if (action == addToPlacesAction) {
202 const KUrl& url = m_mainWindow->activeViewContainer()->url();
203 if (url.isValid()) {
204 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
205 }
206 }
207 }
208
209 void DolphinContextMenu::openTrashItemContextMenu()
210 {
211 Q_ASSERT(m_context & TrashContext);
212 Q_ASSERT(m_context & ItemContext);
213
214 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
215 m_popup->addAction(restoreAction);
216
217 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
218 m_popup->addAction(deleteAction);
219
220 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
221 m_popup->addAction(propertiesAction);
222
223 if (m_popup->exec(QCursor::pos()) == restoreAction) {
224 KUrl::List selectedUrls;
225 foreach (const KFileItem &item, m_selectedItems) {
226 selectedUrls.append(item.url());
227 }
228
229 KonqOperations::restoreTrashedItems(selectedUrls, m_mainWindow);
230 }
231 }
232
233 void DolphinContextMenu::openItemContextMenu()
234 {
235 Q_ASSERT(!m_fileInfo.isNull());
236
237 QAction* openParentInNewWindowAction = 0;
238 QAction* openParentInNewTabAction = 0;
239 QAction* addToPlacesAction = 0;
240 if (m_selectedItems.count() == 1) {
241 if (m_fileInfo.isDir()) {
242 // setup 'Create New' menu
243 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow);
244 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
245 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
246 newFileMenu->checkUpToDate();
247 newFileMenu->setPopupFiles(m_fileInfo.url());
248 newFileMenu->setEnabled(selectedItemsProperties().supportsWriting());
249 connect(newFileMenu, SIGNAL(fileCreated(KUrl)), newFileMenu, SLOT(deleteLater()));
250 connect(newFileMenu, SIGNAL(directoryCreated(KUrl)), newFileMenu, SLOT(deleteLater()));
251
252 KMenu* menu = newFileMenu->menu();
253 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
254 menu->setIcon(KIcon("document-new"));
255 m_popup->addMenu(menu);
256 m_popup->addSeparator();
257
258 // insert 'Open in new window' and 'Open in new tab' entries
259 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
260 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
261
262 // insert 'Add to Places' entry
263 if (!placeExists(m_fileInfo.url())) {
264 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
265 i18nc("@action:inmenu Add selected folder to places",
266 "Add to Places"));
267 }
268
269 m_popup->addSeparator();
270 } else if (m_baseUrl.protocol().contains("search")) {
271 openParentInNewWindowAction = new QAction(KIcon("window-new"),
272 i18nc("@action:inmenu",
273 "Open Path in New Window"),
274 this);
275 m_popup->addAction(openParentInNewWindowAction);
276
277 openParentInNewTabAction = new QAction(KIcon("tab-new"),
278 i18nc("@action:inmenu",
279 "Open Path in New Tab"),
280 this);
281 m_popup->addAction(openParentInNewTabAction);
282
283 m_popup->addSeparator();
284 }
285 }
286
287 insertDefaultItemActions();
288
289 m_popup->addSeparator();
290
291 KFileItemActions fileItemActions;
292 fileItemActions.setItemListProperties(selectedItemsProperties());
293 addServiceActions(fileItemActions);
294
295 addFileItemPluginActions();
296
297 addVersionControlPluginActions();
298
299 // insert 'Copy To' and 'Move To' sub menus
300 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
301 m_copyToMenu.setItems(m_selectedItems);
302 m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting());
303 m_copyToMenu.addActionsTo(m_popup);
304 }
305
306 // insert 'Properties...' entry
307 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
308 m_popup->addAction(propertiesAction);
309
310 QAction* activatedAction = m_popup->exec(QCursor::pos());
311 if (activatedAction) {
312 if (activatedAction == addToPlacesAction) {
313 const KUrl selectedUrl(m_fileInfo.url());
314 if (selectedUrl.isValid()) {
315 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
316 selectedUrl);
317 }
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->showHiddenFiles());
332 newFileMenu->checkUpToDate();
333 newFileMenu->setPopupFiles(m_baseUrl);
334 m_popup->addMenu(newFileMenu->menu());
335 m_popup->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 m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
340 m_popup->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 = m_popup->addAction(KIcon("bookmark-new"),
346 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
347 }
348
349 m_popup->addSeparator();
350
351 QAction* pasteAction = createPasteAction();
352 m_popup->addAction(pasteAction);
353 m_popup->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_popup->addAction(i18nc("@action:inmenu", "Properties"));
368 propertiesAction->setIcon(KIcon("document-properties"));
369
370 addShowMenubarAction();
371
372 QAction* action = m_popup->exec(QCursor::pos());
373 if (action == propertiesAction) {
374 const KUrl& url = m_mainWindow->activeViewContainer()->url();
375
376 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
377 dialog->setAttribute(Qt::WA_DeleteOnClose);
378 dialog->show();
379 } else if (addToPlacesAction && (action == addToPlacesAction)) {
380 const KUrl& url = m_mainWindow->activeViewContainer()->url();
381 if (url.isValid()) {
382 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
383 }
384 }
385 }
386
387 void DolphinContextMenu::insertDefaultItemActions()
388 {
389 const KActionCollection* collection = m_mainWindow->actionCollection();
390
391 // Insert 'Cut', 'Copy' and 'Paste'
392 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
393 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
394 m_popup->addAction(createPasteAction());
395
396 m_popup->addSeparator();
397
398 // Insert 'Rename'
399 QAction* renameAction = collection->action("rename");
400 m_popup->addAction(renameAction);
401
402 // Insert 'Move to Trash' and/or 'Delete'
403 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
404 m_popup->addAction(collection->action("move_to_trash"));
405 m_popup->addAction(collection->action("delete"));
406 } else {
407 m_popup->addAction(m_removeAction);
408 updateRemoveAction();
409 }
410 }
411
412 void DolphinContextMenu::addShowMenubarAction()
413 {
414 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
415 if (!m_mainWindow->menuBar()->isVisible()) {
416 m_popup->addSeparator();
417 m_popup->addAction(showMenuBar);
418 }
419 }
420
421 QString DolphinContextMenu::placesName(const KUrl& url) const
422 {
423 QString name = url.fileName();
424 if (name.isEmpty()) {
425 name = url.host();
426 }
427 return name;
428 }
429
430 bool DolphinContextMenu::placeExists(const KUrl& url) const
431 {
432 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
433 const int count = placesModel->rowCount();
434
435 for (int i = 0; i < count; ++i) {
436 const QModelIndex index = placesModel->index(i, 0);
437
438 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
439 return true;
440 }
441 }
442 return false;
443 }
444
445 QAction* DolphinContextMenu::createPasteAction()
446 {
447 QAction* action = 0;
448 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
449 if (isDir && (m_selectedItems.count() == 1)) {
450 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
451 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
452 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
453 action->setEnabled(!pasteData.isEmpty() && selectedItemsProperties().supportsWriting());
454 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
455 } else {
456 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
457 }
458
459 return action;
460 }
461
462 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties()
463 {
464 if (!m_selectedItemsProperties) {
465 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
466 }
467 return *m_selectedItemsProperties;
468 }
469
470 KFileItem DolphinContextMenu::baseFileItem()
471 {
472 if (!m_baseFileItem) {
473 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
474 }
475 return *m_baseFileItem;
476 }
477
478 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
479 {
480 fileItemActions.setParentWidget(m_mainWindow);
481
482 // insert 'Open With...' action or sub menu
483 fileItemActions.addOpenWithActionsTo(m_popup, "DesktopEntryName != 'dolphin'");
484
485 // insert 'Actions' sub menu
486 fileItemActions.addServiceActionsTo(m_popup);
487 }
488
489 void DolphinContextMenu::addFileItemPluginActions()
490 {
491 KFileItemListProperties props;
492 if (m_selectedItems.isEmpty()) {
493 props.setItems(KFileItemList() << baseFileItem());
494 } else {
495 props = selectedItemsProperties();
496 }
497
498 QString commonMimeType = props.mimeType();
499 if (commonMimeType.isEmpty()) {
500 commonMimeType = QLatin1String("application/octet-stream");
501 }
502
503 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
504 if (pluginServices.isEmpty()) {
505 return;
506 }
507
508 const KConfig config("kservicemenurc", KConfig::NoGlobals);
509 const KConfigGroup showGroup = config.group("Show");
510
511 foreach (const KSharedPtr<KService>& service, pluginServices) {
512 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
513 // The plugin has been disabled
514 continue;
515 }
516
517 // Old API (kdelibs-4.6.0 only)
518 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
519 if (plugin) {
520 plugin->setParent(m_popup);
521 m_popup->addActions(plugin->actions(props, m_mainWindow));
522 }
523 // New API (kdelibs >= 4.6.1)
524 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
525 if (abstractPlugin) {
526 abstractPlugin->setParent(m_popup);
527 m_popup->addActions(abstractPlugin->actions(props, m_mainWindow));
528 }
529 }
530 }
531
532 void DolphinContextMenu::addVersionControlPluginActions()
533 {
534 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
535 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
536 if (!versionControlActions.isEmpty()) {
537 foreach (QAction* action, versionControlActions) {
538 m_popup->addAction(action);
539 }
540 m_popup->addSeparator();
541 }
542 }
543
544 void DolphinContextMenu::addCustomActions()
545 {
546 foreach (QAction* action, m_customActions) {
547 m_popup->addAction(action);
548 }
549 }
550
551 void DolphinContextMenu::updateRemoveAction()
552 {
553 const KActionCollection* collection = m_mainWindow->actionCollection();
554 const bool moveToTrash = selectedItemsProperties().isLocal() && !m_shiftPressed;
555
556 // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
557 // This is only done until the original action has been shown at least once. To
558 // bypass this issue, the text and &-shortcut is applied manually.
559 const QAction* action = 0;
560 if (moveToTrash) {
561 action = collection->action("move_to_trash");
562 m_removeAction->setText(i18nc("@action:inmenu", "&Move to Trash"));
563 } else {
564 action = collection->action("delete");
565 m_removeAction->setText(i18nc("@action:inmenu", "&Delete"));
566 }
567 m_removeAction->setIcon(action->icon());
568 m_removeAction->setShortcuts(action->shortcuts());
569 }
570
571 #include "dolphincontextmenu.moc"