]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Have "Add to Places" in the context menu for the trash too.
[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 "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "dolphinviewcontainer.h"
27
28 #include <kactioncollection.h>
29 #include <kfileplacesmodel.h>
30 #include <kdesktopfile.h>
31 #include <kglobal.h>
32 #include <kiconloader.h>
33 #include <kio/netaccess.h>
34 #include <kmenu.h>
35 #include <kmessagebox.h>
36 #include <kmimetypetrader.h>
37 #include <knewmenu.h>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
40 #include <konq_menuactions.h>
41 #include <klocale.h>
42 #include <kpropertiesdialog.h>
43 #include <krun.h>
44 #include <kstandardaction.h>
45 #include <kstandarddirs.h>
46
47 #include <QtGui/QApplication>
48 #include <QtGui/QClipboard>
49 #include <QtCore/QDir>
50
51 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
52 const KFileItem& fileInfo,
53 const KUrl& baseUrl) :
54 m_mainWindow(parent),
55 m_fileInfo(fileInfo),
56 m_baseUrl(baseUrl),
57 m_context(NoContext)
58 {
59 // The context menu either accesses the URLs of the selected items
60 // or the items itself. To increase the performance both lists are cached.
61 DolphinView* view = m_mainWindow->activeViewContainer()->view();
62 m_selectedUrls = view->selectedUrls();
63 m_selectedItems = view->selectedItems();
64 }
65
66 DolphinContextMenu::~DolphinContextMenu()
67 {
68 }
69
70 void DolphinContextMenu::open()
71 {
72 // get the context information
73 if (m_baseUrl.protocol() == "trash") {
74 m_context |= TrashContext;
75 }
76
77 if (!m_fileInfo.isNull()) {
78 m_context |= ItemContext;
79 // TODO: handle other use cases like devices + desktop files
80 }
81
82 // open the corresponding popup for the context
83 if (m_context & TrashContext) {
84 if (m_context & ItemContext) {
85 openTrashItemContextMenu();
86 } else {
87 openTrashContextMenu();
88 }
89 } else if (m_context & ItemContext) {
90 openItemContextMenu();
91 } else {
92 Q_ASSERT(m_context == NoContext);
93 openViewportContextMenu();
94 }
95 }
96
97
98 void DolphinContextMenu::openTrashContextMenu()
99 {
100 Q_ASSERT(m_context & TrashContext);
101
102 KMenu* popup = new KMenu(m_mainWindow);
103
104 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup);
105 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
106 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
107 popup->addAction(emptyTrashAction);
108
109 popup->addSeparator();
110
111 QAction* addToPlacesAction = popup->addAction(KIcon("folder-bookmarks"),
112 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
113 popup->addSeparator();
114
115 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
116 popup->addAction(propertiesAction);
117
118 QAction *action = popup->exec(QCursor::pos());
119 if (action == emptyTrashAction) {
120 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted."));
121 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
122 text,
123 QString(),
124 KGuiItem(i18nc("@action:button", "Empty Trash"),
125 KIcon("user-trash"))
126 ) == KMessageBox::Continue;
127 if (del) {
128 KonqOperations::emptyTrash(m_mainWindow);
129 }
130 } else if (action == addToPlacesAction) {
131 const KUrl& url = m_mainWindow->activeViewContainer()->url();
132 if (url.isValid()) {
133 DolphinSettings::instance().placesModel()->addPlace(i18n("Trash"), url);
134 }
135 }
136
137 popup->deleteLater();
138 }
139
140 void DolphinContextMenu::openTrashItemContextMenu()
141 {
142 Q_ASSERT(m_context & TrashContext);
143 Q_ASSERT(m_context & ItemContext);
144
145 KMenu* popup = new KMenu(m_mainWindow);
146
147 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
148 popup->addAction(restoreAction);
149
150 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
151 popup->addAction(deleteAction);
152
153 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
154 popup->addAction(propertiesAction);
155
156 if (popup->exec(QCursor::pos()) == restoreAction) {
157 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
158 }
159
160 popup->deleteLater();
161 }
162
163 void DolphinContextMenu::openItemContextMenu()
164 {
165 Q_ASSERT(!m_fileInfo.isNull());
166
167 KMenu* popup = new KMenu(m_mainWindow);
168 insertDefaultItemActions(popup);
169
170 popup->addSeparator();
171
172 // insert 'Bookmark This Folder' entry if exactly one item is selected
173 QAction* addToPlacesAction = 0;
174 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
175 addToPlacesAction = popup->addAction(KIcon("folder-bookmarks"),
176 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
177 }
178
179 // Insert 'Open With...' sub menu
180 QVector<KService::Ptr> openWithVector;
181 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
182
183 // Insert 'Actions' sub menu
184 KonqMenuActions menuActions;
185 menuActions.setItems(m_selectedItems);
186 if (menuActions.addActionsTo(popup))
187 popup->addSeparator();
188
189 // insert 'Properties...' entry
190 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
191 popup->addAction(propertiesAction);
192
193 QAction* activatedAction = popup->exec(QCursor::pos());
194
195 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
196 const KUrl selectedUrl(m_fileInfo.url());
197 if (selectedUrl.isValid()) {
198 DolphinSettings::instance().placesModel()->addPlace(selectedUrl.fileName(),
199 selectedUrl);
200 }
201 } else if (openWithActions.contains(activatedAction)) {
202 // one of the 'Open With' items has been selected
203 if (openWithActions.last() == activatedAction) {
204 // the item 'Other...' has been selected
205 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
206 } else {
207 int id = openWithActions.indexOf(activatedAction);
208 KService::Ptr servicePtr = openWithVector[id];
209 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
210 }
211 }
212
213 openWithVector.clear();
214 popup->deleteLater();
215 }
216
217 void DolphinContextMenu::openViewportContextMenu()
218 {
219 Q_ASSERT(m_fileInfo.isNull());
220 KMenu* popup = new KMenu(m_mainWindow);
221
222 // setup 'Create New' menu
223 KNewMenu* newMenu = m_mainWindow->newMenu();
224 newMenu->slotCheckUpToDate();
225 newMenu->setPopupFiles(m_baseUrl);
226 popup->addMenu(newMenu->menu());
227 popup->addSeparator();
228
229 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
230 popup->addAction(pasteAction);
231
232 // setup 'View Mode' menu
233 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
234
235 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
236 viewModeMenu->addAction(iconsMode);
237
238 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
239 viewModeMenu->addAction(detailsMode);
240
241 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
242 viewModeMenu->addAction(columnsMode);
243
244 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
245 viewModeMenu->addAction(previewsMode);
246
247 popup->addMenu(viewModeMenu);
248
249 popup->addSeparator();
250
251 QAction* addToPlacesAction = popup->addAction(KIcon("folder-bookmarks"),
252 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
253 popup->addSeparator();
254
255 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
256
257 QAction* action = popup->exec(QCursor::pos());
258 if (action == propertiesAction) {
259 const KUrl& url = m_mainWindow->activeViewContainer()->url();
260 KPropertiesDialog dialog(url);
261 dialog.exec();
262 } else if (action == addToPlacesAction) {
263 const KUrl& url = m_mainWindow->activeViewContainer()->url();
264 if (url.isValid()) {
265 DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url);
266 }
267 }
268
269 popup->deleteLater();
270 }
271
272 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
273 {
274 Q_ASSERT(popup != 0);
275 const KActionCollection* collection = m_mainWindow->actionCollection();
276
277 // insert 'Cut', 'Copy' and 'Paste'
278 QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut));
279 QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy));
280 QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste));
281
282 popup->addAction(cutAction);
283 popup->addAction(copyAction);
284 popup->addAction(pasteAction);
285 popup->addSeparator();
286
287 // insert 'Rename'
288 QAction* renameAction = collection->action("rename");
289 popup->addAction(renameAction);
290
291 // insert 'Move to Trash' and (optionally) 'Delete'
292 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
293 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
294 const KUrl& url = m_mainWindow->activeViewContainer()->url();
295 if (url.isLocalFile()) {
296 QAction* moveToTrashAction = collection->action("move_to_trash");
297 popup->addAction(moveToTrashAction);
298 } else {
299 showDeleteCommand = true;
300 }
301
302 if (showDeleteCommand) {
303 QAction* deleteAction = collection->action("delete");
304 popup->addAction(deleteAction);
305 }
306 }
307
308 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
309 QVector<KService::Ptr>& openWithVector)
310 {
311 // Parts of the following code have been taken
312 // from the class KonqOperations located in
313 // libqonq/konq_operations.h of Konqueror.
314 // (Copyright (C) 2000 David Faure <faure@kde.org>)
315
316 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
317 // are listed which are registered to open the item. As last entry "Other..." will be
318 // attached which allows to select a custom application. If no applications are registered
319 // no sub menu is created at all, only "Open With..." will be offered.
320 bool insertOpenWithItems = true;
321 const QString contextMimeType(m_fileInfo.mimetype());
322
323 QListIterator<KFileItem> mimeIt(m_selectedItems);
324 while (insertOpenWithItems && mimeIt.hasNext()) {
325 KFileItem item = mimeIt.next();
326 insertOpenWithItems = (contextMimeType == item.mimetype());
327 }
328
329 QList<QAction*> openWithActions;
330 if (insertOpenWithItems) {
331 // fill the 'Open with' sub menu with application types
332 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url());
333 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
334 "Application",
335 "Type == 'Application'");
336 if (offers.count() > 0) {
337 KService::List::Iterator it;
338 KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With"));
339 for (it = offers.begin(); it != offers.end(); ++it) {
340 // The offer list from the KTrader returns duplicate
341 // application entries. Although this seems to be a configuration
342 // problem outside the scope of Dolphin, duplicated entries just
343 // will be skipped here.
344 const QString appName((*it)->name());
345 if (!containsEntry(openWithMenu, appName)) {
346 const KIcon icon((*it)->icon());
347 QAction* action = openWithMenu->addAction(icon, appName);
348 openWithVector.append(*it);
349 openWithActions << action;
350 }
351 }
352
353 openWithMenu->addSeparator();
354 QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other..."));
355
356 openWithActions << action;
357 popup->addMenu(openWithMenu);
358 } else {
359 // No applications are registered, hence just offer
360 // a "Open With..." item instead of a sub menu containing
361 // only one entry.
362 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
363 openWithActions << action;
364 }
365 } else {
366 // At least one of the selected items has a different MIME type. In this case
367 // just show a disabled "Open With..." entry.
368 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
369 action->setEnabled(false);
370 }
371
372 return openWithActions;
373 }
374
375 bool DolphinContextMenu::containsEntry(const KMenu* menu,
376 const QString& entryName) const
377 {
378 Q_ASSERT(menu != 0);
379
380 const QList<QAction*> list = menu->actions();
381 const uint count = list.count();
382 for (uint i = 0; i < count; ++i) {
383 const QAction* action = list.at(i);
384 if (action->text() == entryName) {
385 return true;
386 }
387 }
388
389 return false;
390 }
391
392 #include "dolphincontextmenu.moc"