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