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