]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Move the "Add to Places" action upwards when used on the viewport to stay consistent...
[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 <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobal.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
36 #include <kmenu.h>
37 #include <kmenubar.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
40 #include <kmodifierkeyinfo.h>
41 #include <knewfilemenu.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <kfileitemactions.h>
45 #include <klocale.h>
46 #include <kpropertiesdialog.h>
47 #include <kstandardaction.h>
48 #include <kstandarddirs.h>
49
50 #include <QtGui/QApplication>
51 #include <QtGui/QClipboard>
52 #include <QtCore/QDir>
53
54 #include "views/dolphinview.h"
55 #include "views/viewmodecontroller.h"
56
57 K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
58
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
60 const KFileItem& fileInfo,
61 const KUrl& baseUrl) :
62 m_mainWindow(parent),
63 m_capabilities(0),
64 m_fileInfo(fileInfo),
65 m_baseUrl(baseUrl),
66 m_context(NoContext),
67 m_copyToMenu(parent),
68 m_customActions(),
69 m_popup(new KMenu(m_mainWindow)),
70 m_command(None),
71 m_shiftPressed(false),
72 m_removeAction(0)
73 {
74 // The context menu either accesses the URLs of the selected items
75 // or the items itself. To increase the performance both lists are cached.
76 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
77 m_selectedUrls = view->selectedUrls();
78 m_selectedItems = view->selectedItems();
79
80 if (m_keyInfo != 0) {
81 if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
82 m_shiftPressed = true;
83 }
84 connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)),
85 this, SLOT(slotKeyModifierPressed(Qt::Key, bool)));
86 }
87
88 m_removeAction = new QAction(this);
89 connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
90 }
91
92 DolphinContextMenu::~DolphinContextMenu()
93 {
94 delete m_capabilities;
95 m_capabilities = 0;
96 }
97
98 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
99 {
100 m_customActions = actions;
101 }
102
103 DolphinContextMenu::Command DolphinContextMenu::open()
104 {
105 // get the context information
106 if (m_baseUrl.protocol() == QLatin1String("trash")) {
107 m_context |= TrashContext;
108 }
109
110 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
111 m_context |= ItemContext;
112 // TODO: handle other use cases like devices + desktop files
113 }
114
115 // open the corresponding popup for the context
116 if (m_context & TrashContext) {
117 if (m_context & ItemContext) {
118 openTrashItemContextMenu();
119 } else {
120 openTrashContextMenu();
121 }
122 } else if (m_context & ItemContext) {
123 openItemContextMenu();
124 } else {
125 Q_ASSERT(m_context == NoContext);
126 openViewportContextMenu();
127 }
128
129 return m_command;
130 }
131
132 void DolphinContextMenu::initializeModifierKeyInfo()
133 {
134 // Access m_keyInfo, so that it gets instantiated by
135 // K_GLOBAL_STATIC
136 KModifierKeyInfo* keyInfo = m_keyInfo;
137 Q_UNUSED(keyInfo);
138 }
139
140 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
141 {
142 m_shiftPressed = (key == Qt::Key_Shift) && pressed;
143 updateRemoveAction();
144 }
145
146 void DolphinContextMenu::slotRemoveActionTriggered()
147 {
148 const KActionCollection* collection = m_mainWindow->actionCollection();
149 if (m_shiftPressed) {
150 collection->action("delete")->trigger();
151 } else {
152 collection->action("move_to_trash")->trigger();
153 }
154 }
155
156 void DolphinContextMenu::openTrashContextMenu()
157 {
158 Q_ASSERT(m_context & TrashContext);
159
160 addShowMenubarAction();
161
162 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup.data());
163 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
164 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
165 m_popup->addAction(emptyTrashAction);
166
167 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
168 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
169
170 // Don't show if url is already in places
171 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
172 addToPlacesAction->setVisible(false);
173 }
174
175 addCustomActions();
176
177 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
178 m_popup->addAction(propertiesAction);
179
180 QAction *action = m_popup->exec(QCursor::pos());
181 if (action == emptyTrashAction) {
182 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
183 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
184 text,
185 QString(),
186 KGuiItem(i18nc("@action:button", "Empty Trash"),
187 KIcon("user-trash"))
188 ) == KMessageBox::Continue;
189 if (del) {
190 KonqOperations::emptyTrash(m_mainWindow);
191 }
192 } else if (action == addToPlacesAction) {
193 const KUrl& url = m_mainWindow->activeViewContainer()->url();
194 if (url.isValid()) {
195 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
196 }
197 }
198 }
199
200 void DolphinContextMenu::openTrashItemContextMenu()
201 {
202 Q_ASSERT(m_context & TrashContext);
203 Q_ASSERT(m_context & ItemContext);
204
205 addShowMenubarAction();
206
207 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
208 m_popup->addAction(restoreAction);
209
210 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
211 m_popup->addAction(deleteAction);
212
213 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
214 m_popup->addAction(propertiesAction);
215
216 if (m_popup->exec(QCursor::pos()) == restoreAction) {
217 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
218 }
219 }
220
221 void DolphinContextMenu::openItemContextMenu()
222 {
223 Q_ASSERT(!m_fileInfo.isNull());
224
225 QAction* openParentInNewWindowAction = 0;
226 QAction* openParentInNewTabAction = 0;
227 QAction* addToPlacesAction = 0;
228 if (m_selectedUrls.count() == 1) {
229 if (m_fileInfo.isDir()) {
230 // setup 'Create New' menu
231 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
232 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
233 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
234 newFileMenu->checkUpToDate();
235 newFileMenu->setPopupFiles(m_fileInfo.url());
236 newFileMenu->setEnabled(capabilities().supportsWriting());
237
238 KMenu* menu = newFileMenu->menu();
239 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
240 menu->setIcon(KIcon("document-new"));
241 m_popup->addMenu(menu);
242 m_popup->addSeparator();
243
244 // insert 'Open in new window' and 'Open in new tab' entries
245 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
246 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
247
248 // insert 'Add to Places' entry
249 if (!placeExists(m_fileInfo.url())) {
250 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
251 i18nc("@action:inmenu Add selected folder to places",
252 "Add to Places"));
253 }
254
255 m_popup->addSeparator();
256 } else if (m_baseUrl.protocol().contains("search")) {
257 openParentInNewWindowAction = new QAction(KIcon("window-new"),
258 i18nc("@action:inmenu",
259 "Open Path in New Window"),
260 this);
261 m_popup->addAction(openParentInNewWindowAction);
262
263 openParentInNewTabAction = new QAction(KIcon("tab-new"),
264 i18nc("@action:inmenu",
265 "Open Path in New Tab"),
266 this);
267 m_popup->addAction(openParentInNewTabAction);
268
269 m_popup->addSeparator();
270 }
271 }
272
273 addShowMenubarAction();
274 insertDefaultItemActions();
275
276 m_popup->addSeparator();
277
278 KFileItemActions fileItemActions;
279 fileItemActions.setItemListProperties(capabilities());
280 addServiceActions(fileItemActions);
281
282 addVersionControlActions();
283
284 // insert 'Copy To' and 'Move To' sub menus
285 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
286 m_copyToMenu.setItems(m_selectedItems);
287 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
288 m_copyToMenu.addActionsTo(m_popup.data());
289 }
290
291 // insert 'Properties...' entry
292 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
293 m_popup->addAction(propertiesAction);
294
295 QAction* activatedAction = m_popup->exec(QCursor::pos());
296 if (activatedAction != 0) {
297 if (activatedAction == addToPlacesAction) {
298 const KUrl selectedUrl(m_fileInfo.url());
299 if (selectedUrl.isValid()) {
300 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
301 selectedUrl);
302 }
303 } else if (activatedAction == openParentInNewWindowAction) {
304 m_command = OpenParentFolderInNewWindow;
305 } else if (activatedAction == openParentInNewTabAction) {
306 m_command = OpenParentFolderInNewTab;
307 }
308 }
309 }
310
311 void DolphinContextMenu::openViewportContextMenu()
312 {
313 addShowMenubarAction();
314
315 // setup 'Create New' menu
316 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
317 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
318 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
319 newFileMenu->checkUpToDate();
320 newFileMenu->setPopupFiles(m_baseUrl);
321 m_popup->addMenu(newFileMenu->menu());
322 m_popup->addSeparator();
323
324 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
325 // "open_in_new_tab" here, as the current selection should get ignored.
326 m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
327 m_popup->addAction(m_mainWindow->actionCollection()->action("new_tab"));
328
329 // Insert 'Add to Places' entry if exactly one item is selected
330 QAction* addToPlacesAction = 0;
331 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
332 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
333 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
334 }
335
336 m_popup->addSeparator();
337
338 QAction* pasteAction = createPasteAction();
339 m_popup->addAction(pasteAction);
340 m_popup->addSeparator();
341
342 // insert service actions
343 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
344 const KFileItemListProperties baseUrlProperties(KFileItemList() << item);
345 KFileItemActions fileItemActions;
346 fileItemActions.setItemListProperties(baseUrlProperties);
347 addServiceActions(fileItemActions);
348
349 addVersionControlActions();
350
351 addCustomActions();
352
353 QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
354 propertiesAction->setIcon(KIcon("document-properties"));
355 QAction* action = m_popup->exec(QCursor::pos());
356 if (action == propertiesAction) {
357 const KUrl& url = m_mainWindow->activeViewContainer()->url();
358
359 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
360 dialog->setAttribute(Qt::WA_DeleteOnClose);
361 dialog->show();
362 } else if ((addToPlacesAction != 0) && (action == addToPlacesAction)) {
363 const KUrl& url = m_mainWindow->activeViewContainer()->url();
364 if (url.isValid()) {
365 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
366 }
367 }
368 }
369
370 void DolphinContextMenu::insertDefaultItemActions()
371 {
372 const KActionCollection* collection = m_mainWindow->actionCollection();
373
374 // Insert 'Cut', 'Copy' and 'Paste'
375 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
376 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
377 m_popup->addAction(createPasteAction());
378
379 m_popup->addSeparator();
380
381 // Insert 'Rename'
382 QAction* renameAction = collection->action("rename");
383 m_popup->addAction(renameAction);
384
385 // Insert 'Move to Trash' and/or 'Delete'
386 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
387 m_popup->addAction(collection->action("move_to_trash"));
388 m_popup->addAction(collection->action("delete"));
389 } else {
390 m_popup->addAction(m_removeAction);
391 updateRemoveAction();
392 }
393 }
394
395 void DolphinContextMenu::addShowMenubarAction()
396 {
397 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
398 if (!m_mainWindow->menuBar()->isVisible()) {
399 m_popup->addAction(showMenuBar);
400 m_popup->addSeparator();
401 }
402 }
403
404 QString DolphinContextMenu::placesName(const KUrl& url) const
405 {
406 QString name = url.fileName();
407 if (name.isEmpty()) {
408 name = url.host();
409 }
410 return name;
411 }
412
413 bool DolphinContextMenu::placeExists(const KUrl& url) const
414 {
415 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
416 const int count = placesModel->rowCount();
417
418 for (int i = 0; i < count; ++i) {
419 const QModelIndex index = placesModel->index(i, 0);
420
421 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
422 return true;
423 }
424 }
425 return false;
426 }
427
428 QAction* DolphinContextMenu::createPasteAction()
429 {
430 QAction* action = 0;
431 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
432 if (isDir && (m_selectedItems.count() == 1)) {
433 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
434 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
435 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
436 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
437 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
438 } else {
439 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
440 }
441
442 return action;
443 }
444
445 KFileItemListProperties& DolphinContextMenu::capabilities()
446 {
447 if (m_capabilities == 0) {
448 m_capabilities = new KFileItemListProperties(m_selectedItems);
449 }
450 return *m_capabilities;
451 }
452
453 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
454 {
455 fileItemActions.setParentWidget(m_mainWindow);
456
457 // insert 'Open With...' action or sub menu
458 fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'");
459
460 // insert 'Actions' sub menu
461 fileItemActions.addServiceActionsTo(m_popup.data());
462 }
463
464 void DolphinContextMenu::addVersionControlActions()
465 {
466 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
467 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
468 if (!versionControlActions.isEmpty()) {
469 foreach (QAction* action, versionControlActions) {
470 m_popup->addAction(action);
471 }
472 m_popup->addSeparator();
473 }
474 }
475
476 void DolphinContextMenu::addCustomActions()
477 {
478 foreach (QAction* action, m_customActions) {
479 m_popup->addAction(action);
480 }
481 }
482
483 void DolphinContextMenu::updateRemoveAction()
484 {
485 const KActionCollection* collection = m_mainWindow->actionCollection();
486 const bool moveToTrash = capabilities().isLocal() && !m_shiftPressed;
487 const QAction* action = moveToTrash ? collection->action("move_to_trash") : collection->action("delete");
488 m_removeAction->setText(action->text());
489 m_removeAction->setIcon(action->icon());
490 m_removeAction->setShortcuts(action->shortcuts());
491 }
492
493 #include "dolphincontextmenu.moc"