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