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