]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Fix memory leak. QMenu::addAction(QMenu*) does not take ownership.
[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 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 addRevisionControlActions(popup);
228
229 // insert 'Copy To' and 'Move To' sub menus
230 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
231 m_copyToMenu.setItems(m_selectedItems);
232 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
233 m_copyToMenu.addActionsTo(popup);
234 popup->addSeparator();
235 }
236
237 // insert 'Properties...' entry
238 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
239 popup->addAction(propertiesAction);
240
241 QAction* activatedAction = popup->exec(QCursor::pos());
242
243 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
244 const KUrl selectedUrl(m_fileInfo.url());
245 if (selectedUrl.isValid()) {
246 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
247 selectedUrl);
248 }
249 }
250
251 popup->deleteLater();
252 }
253
254 void DolphinContextMenu::openViewportContextMenu()
255 {
256 KMenu* popup = new KMenu(m_mainWindow);
257
258 addShowMenubarAction(popup);
259
260 // setup 'Create New' menu
261 KNewMenu* newMenu = m_mainWindow->newMenu();
262 newMenu->slotCheckUpToDate();
263 newMenu->setPopupFiles(m_baseUrl);
264 popup->addMenu(newMenu->menu());
265 popup->addSeparator();
266
267 QAction* pasteAction = createPasteAction();
268 popup->addAction(pasteAction);
269
270 // setup 'View Mode' menu
271 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"), popup);
272
273 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
274 viewModeMenu->addAction(iconsMode);
275
276 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
277 viewModeMenu->addAction(detailsMode);
278
279 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
280 viewModeMenu->addAction(columnsMode);
281
282 popup->addMenu(viewModeMenu);
283
284 popup->addSeparator();
285
286 addRevisionControlActions(popup);
287
288 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
289 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
290
291 addCustomActions(popup);
292
293 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
294 propertiesAction->setIcon(KIcon("document-properties"));
295 QAction* action = popup->exec(QCursor::pos());
296 if (action == propertiesAction) {
297 const KUrl& url = m_mainWindow->activeViewContainer()->url();
298
299 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
300 dialog->setAttribute(Qt::WA_DeleteOnClose);
301 dialog->show();
302 } else if (action == addToPlacesAction) {
303 const KUrl& url = m_mainWindow->activeViewContainer()->url();
304 if (url.isValid()) {
305 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
306 }
307 }
308
309 popup->deleteLater();
310 }
311
312 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
313 {
314 Q_ASSERT(popup != 0);
315 const KActionCollection* collection = m_mainWindow->actionCollection();
316
317 // insert 'Cut', 'Copy' and 'Paste'
318 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
319 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
320 QAction* pasteAction = createPasteAction();
321
322 popup->addAction(cutAction);
323 popup->addAction(copyAction);
324 popup->addAction(pasteAction);
325 popup->addSeparator();
326
327 // insert 'Rename'
328 QAction* renameAction = collection->action("rename");
329 popup->addAction(renameAction);
330
331 // insert 'Move to Trash' and (optionally) 'Delete'
332 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
333 KConfigGroup configGroup(globalConfig, "KDE");
334 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
335
336 const KUrl& url = m_mainWindow->activeViewContainer()->url();
337 if (url.isLocalFile()) {
338 QAction* moveToTrashAction = collection->action("move_to_trash");
339 popup->addAction(moveToTrashAction);
340 } else {
341 showDeleteCommand = true;
342 }
343
344 if (showDeleteCommand) {
345 QAction* deleteAction = collection->action("delete");
346 popup->addAction(deleteAction);
347 }
348 }
349
350 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
351 {
352 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
353 if (!m_mainWindow->menuBar()->isVisible()) {
354 menu->addAction(showMenuBar);
355 menu->addSeparator();
356 }
357 }
358
359 QString DolphinContextMenu::placesName(const KUrl& url) const
360 {
361 QString name = url.fileName();
362 if (name.isEmpty()) {
363 name = url.host();
364 }
365 return name;
366 }
367
368 QAction* DolphinContextMenu::createPasteAction()
369 {
370 QAction* action = 0;
371 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
372 if (isDir && (m_selectedItems.count() == 1)) {
373 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
374 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
375 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
376 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
377 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
378 } else {
379 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
380 }
381
382 return action;
383 }
384
385 KFileItemListProperties& DolphinContextMenu::capabilities()
386 {
387 if (m_capabilities == 0) {
388 m_capabilities = new KFileItemListProperties(m_selectedItems);
389 }
390 return *m_capabilities;
391 }
392
393 void DolphinContextMenu::addRevisionControlActions(KMenu* menu)
394 {
395 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
396 const QList<QAction*> revControlActions = view->revisionControlActions(m_selectedItems);
397 if (!revControlActions.isEmpty()) {
398 foreach (QAction* action, revControlActions) {
399 menu->addAction(action);
400 }
401 menu->addSeparator();
402 }
403 }
404
405 void DolphinContextMenu::addCustomActions(KMenu* menu)
406 {
407 foreach (QAction* action, m_customActions) {
408 menu->addAction(action);
409 }
410 }
411
412 #include "dolphincontextmenu.moc"