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