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