]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
show the actions of a revision control plugin in the context 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 "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 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.count() > 0)) {
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 addCustomActions(popup);
128
129 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
130 popup->addAction(propertiesAction);
131
132 QAction *action = popup->exec(QCursor::pos());
133 if (action == emptyTrashAction) {
134 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
135 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
136 text,
137 QString(),
138 KGuiItem(i18nc("@action:button", "Empty Trash"),
139 KIcon("user-trash"))
140 ) == KMessageBox::Continue;
141 if (del) {
142 KonqOperations::emptyTrash(m_mainWindow);
143 }
144 } else if (action == addToPlacesAction) {
145 const KUrl& url = m_mainWindow->activeViewContainer()->url();
146 if (url.isValid()) {
147 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
148 }
149 }
150
151 popup->deleteLater();
152 }
153
154 void DolphinContextMenu::openTrashItemContextMenu()
155 {
156 Q_ASSERT(m_context & TrashContext);
157 Q_ASSERT(m_context & ItemContext);
158
159 KMenu* popup = new KMenu(m_mainWindow);
160
161 addShowMenubarAction(popup);
162
163 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
164 popup->addAction(restoreAction);
165
166 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
167 popup->addAction(deleteAction);
168
169 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
170 popup->addAction(propertiesAction);
171
172 if (popup->exec(QCursor::pos()) == restoreAction) {
173 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
174 }
175
176 popup->deleteLater();
177 }
178
179 void DolphinContextMenu::openItemContextMenu()
180 {
181 Q_ASSERT(!m_fileInfo.isNull());
182
183 KMenu* popup = new KMenu(m_mainWindow);
184 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
185 // setup 'Create New' menu
186 DolphinNewMenu* newMenu = new DolphinNewMenu(popup, m_mainWindow);
187 newMenu->slotCheckUpToDate();
188 newMenu->setPopupFiles(m_fileInfo.url());
189 newMenu->setEnabled(capabilities().supportsWriting());
190
191 KMenu* menu = newMenu->menu();
192 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
193 menu->setIcon(KIcon("document-new"));
194 popup->addMenu(newMenu->menu());
195 popup->addSeparator();
196
197 // insert 'Open in new window' and 'Open in new tab' entries
198 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
199 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
200 popup->addSeparator();
201 }
202 addShowMenubarAction(popup);
203 insertDefaultItemActions(popup);
204
205 popup->addSeparator();
206
207 // insert 'Bookmark This Folder' entry if exactly one item is selected
208 QAction* addToPlacesAction = 0;
209 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
210 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
211 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
212 }
213
214 KonqMenuActions menuActions;
215 menuActions.setParentWidget(m_mainWindow);
216 menuActions.setItemListProperties(m_selectedItems);
217
218 // insert 'Open With...' action or sub menu
219 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
220
221 // insert 'Actions' sub menu
222 if (menuActions.addActionsTo(popup)) {
223 popup->addSeparator();
224 }
225
226 // insert revision control actions
227 DolphinView* view = m_mainWindow->activeViewContainer()->view();
228 const QList<QAction*> revControlActions = view->revisionControlActions(m_selectedItems);
229 if (revControlActions.count() > 0) {
230 foreach (QAction* action, revControlActions) {
231 popup->addAction(action);
232 }
233 popup->addSeparator();
234 }
235
236 // insert 'Copy To' and 'Move To' sub menus
237 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
238 m_copyToMenu.setItems(m_selectedItems);
239 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
240 m_copyToMenu.addActionsTo(popup);
241 popup->addSeparator();
242 }
243
244 // insert 'Properties...' entry
245 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
246 popup->addAction(propertiesAction);
247
248 QAction* activatedAction = popup->exec(QCursor::pos());
249
250 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
251 const KUrl selectedUrl(m_fileInfo.url());
252 if (selectedUrl.isValid()) {
253 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
254 selectedUrl);
255 }
256 }
257
258 popup->deleteLater();
259 }
260
261 void DolphinContextMenu::openViewportContextMenu()
262 {
263 KMenu* popup = new KMenu(m_mainWindow);
264
265 addShowMenubarAction(popup);
266
267 // setup 'Create New' menu
268 KNewMenu* newMenu = m_mainWindow->newMenu();
269 newMenu->slotCheckUpToDate();
270 newMenu->setPopupFiles(m_baseUrl);
271 popup->addMenu(newMenu->menu());
272 popup->addSeparator();
273
274 QAction* pasteAction = createPasteAction();
275 popup->addAction(pasteAction);
276
277 // setup 'View Mode' menu
278 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
279
280 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
281 viewModeMenu->addAction(iconsMode);
282
283 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
284 viewModeMenu->addAction(detailsMode);
285
286 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
287 viewModeMenu->addAction(columnsMode);
288
289 popup->addMenu(viewModeMenu);
290
291 popup->addSeparator();
292
293 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
294 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
295
296 addCustomActions(popup);
297
298 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
299 propertiesAction->setIcon(KIcon("document-properties"));
300 QAction* action = popup->exec(QCursor::pos());
301 if (action == propertiesAction) {
302 const KUrl& url = m_mainWindow->activeViewContainer()->url();
303
304 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
305 dialog->setAttribute(Qt::WA_DeleteOnClose);
306 dialog->show();
307 } else if (action == addToPlacesAction) {
308 const KUrl& url = m_mainWindow->activeViewContainer()->url();
309 if (url.isValid()) {
310 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
311 }
312 }
313
314 popup->deleteLater();
315 }
316
317 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
318 {
319 Q_ASSERT(popup != 0);
320 const KActionCollection* collection = m_mainWindow->actionCollection();
321
322 // insert 'Cut', 'Copy' and 'Paste'
323 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
324 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
325 QAction* pasteAction = createPasteAction();
326
327 popup->addAction(cutAction);
328 popup->addAction(copyAction);
329 popup->addAction(pasteAction);
330 popup->addSeparator();
331
332 // insert 'Rename'
333 QAction* renameAction = collection->action("rename");
334 popup->addAction(renameAction);
335
336 // insert 'Move to Trash' and (optionally) 'Delete'
337 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
338 KConfigGroup configGroup(globalConfig, "KDE");
339 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
340
341 const KUrl& url = m_mainWindow->activeViewContainer()->url();
342 if (url.isLocalFile()) {
343 QAction* moveToTrashAction = collection->action("move_to_trash");
344 popup->addAction(moveToTrashAction);
345 } else {
346 showDeleteCommand = true;
347 }
348
349 if (showDeleteCommand) {
350 QAction* deleteAction = collection->action("delete");
351 popup->addAction(deleteAction);
352 }
353 }
354
355 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
356 {
357 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
358 if (!m_mainWindow->menuBar()->isVisible()) {
359 menu->addAction(showMenuBar);
360 menu->addSeparator();
361 }
362 }
363
364 QString DolphinContextMenu::placesName(const KUrl& url) const
365 {
366 QString name = url.fileName();
367 if (name.isEmpty()) {
368 name = url.host();
369 }
370 return name;
371 }
372
373 QAction* DolphinContextMenu::createPasteAction()
374 {
375 QAction* action = 0;
376 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
377 if (isDir && (m_selectedItems.count() == 1)) {
378 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
379 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
380 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
381 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
382 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
383 } else {
384 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
385 }
386
387 return action;
388 }
389
390 KFileItemListProperties& DolphinContextMenu::capabilities()
391 {
392 if (m_capabilities == 0) {
393 m_capabilities = new KFileItemListProperties(m_selectedItems);
394 }
395 return *m_capabilities;
396 }
397
398 void DolphinContextMenu::addCustomActions(KMenu* menu)
399 {
400 foreach (QAction* action, m_customActions) {
401 menu->addAction(action);
402 }
403 }
404
405 #include "dolphincontextmenu.moc"