]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
allow to show/hide the menubar
[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()) {
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("folder-bookmarks"),
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("folder-bookmarks"),
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 Q_ASSERT(m_fileInfo.isNull());
223 KMenu* popup = new KMenu(m_mainWindow);
224
225 addShowMenubarAction(popup);
226
227 // setup 'Create New' menu
228 KNewMenu* newMenu = m_mainWindow->newMenu();
229 newMenu->slotCheckUpToDate();
230 newMenu->setPopupFiles(m_baseUrl);
231 popup->addMenu(newMenu->menu());
232 popup->addSeparator();
233
234 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
235 popup->addAction(pasteAction);
236
237 // setup 'View Mode' menu
238 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
239
240 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
241 viewModeMenu->addAction(iconsMode);
242
243 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
244 viewModeMenu->addAction(detailsMode);
245
246 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
247 viewModeMenu->addAction(columnsMode);
248
249 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
250 viewModeMenu->addAction(previewsMode);
251
252 popup->addMenu(viewModeMenu);
253
254 popup->addSeparator();
255
256 QAction* addToPlacesAction = popup->addAction(KIcon("folder-bookmarks"),
257 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
258 popup->addSeparator();
259
260 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
261
262 QAction* action = popup->exec(QCursor::pos());
263 if (action == propertiesAction) {
264 const KUrl& url = m_mainWindow->activeViewContainer()->url();
265 KPropertiesDialog dialog(url);
266 dialog.exec();
267 } else if (action == addToPlacesAction) {
268 const KUrl& url = m_mainWindow->activeViewContainer()->url();
269 if (url.isValid()) {
270 DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url);
271 }
272 }
273
274 popup->deleteLater();
275 }
276
277 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
278 {
279 Q_ASSERT(popup != 0);
280 const KActionCollection* collection = m_mainWindow->actionCollection();
281
282 // insert 'Cut', 'Copy' and 'Paste'
283 QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut));
284 QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy));
285 QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste));
286
287 popup->addAction(cutAction);
288 popup->addAction(copyAction);
289 popup->addAction(pasteAction);
290 popup->addSeparator();
291
292 // insert 'Rename'
293 QAction* renameAction = collection->action("rename");
294 popup->addAction(renameAction);
295
296 // insert 'Move to Trash' and (optionally) 'Delete'
297 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
298 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
299 const KUrl& url = m_mainWindow->activeViewContainer()->url();
300 if (url.isLocalFile()) {
301 QAction* moveToTrashAction = collection->action("move_to_trash");
302 popup->addAction(moveToTrashAction);
303 } else {
304 showDeleteCommand = true;
305 }
306
307 if (showDeleteCommand) {
308 QAction* deleteAction = collection->action("delete");
309 popup->addAction(deleteAction);
310 }
311 }
312
313 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
314 QVector<KService::Ptr>& openWithVector)
315 {
316 // Parts of the following code have been taken
317 // from the class KonqOperations located in
318 // libqonq/konq_operations.h of Konqueror.
319 // (Copyright (C) 2000 David Faure <faure@kde.org>)
320
321 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
322 // are listed which are registered to open the item. As last entry "Other..." will be
323 // attached which allows to select a custom application. If no applications are registered
324 // no sub menu is created at all, only "Open With..." will be offered.
325 bool insertOpenWithItems = true;
326 const QString contextMimeType(m_fileInfo.mimetype());
327
328 QListIterator<KFileItem> mimeIt(m_selectedItems);
329 while (insertOpenWithItems && mimeIt.hasNext()) {
330 KFileItem item = mimeIt.next();
331 insertOpenWithItems = (contextMimeType == item.mimetype());
332 }
333
334 QList<QAction*> openWithActions;
335 if (insertOpenWithItems) {
336 // fill the 'Open with' sub menu with application types
337 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url());
338 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
339 "Application",
340 "Type == 'Application'");
341 if (offers.count() > 0) {
342 KService::List::Iterator it;
343 KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With"));
344 for (it = offers.begin(); it != offers.end(); ++it) {
345 // The offer list from the KTrader returns duplicate
346 // application entries. Although this seems to be a configuration
347 // problem outside the scope of Dolphin, duplicated entries just
348 // will be skipped here.
349 const QString appName((*it)->name());
350 if (!containsEntry(openWithMenu, appName)) {
351 const KIcon icon((*it)->icon());
352 QAction* action = openWithMenu->addAction(icon, appName);
353 openWithVector.append(*it);
354 openWithActions << action;
355 }
356 }
357
358 openWithMenu->addSeparator();
359 QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other..."));
360
361 openWithActions << action;
362 popup->addMenu(openWithMenu);
363 } else {
364 // No applications are registered, hence just offer
365 // a "Open With..." item instead of a sub menu containing
366 // only one entry.
367 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
368 openWithActions << action;
369 }
370 } else {
371 // At least one of the selected items has a different MIME type. In this case
372 // just show a disabled "Open With..." entry.
373 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
374 action->setEnabled(false);
375 }
376
377 return openWithActions;
378 }
379
380 bool DolphinContextMenu::containsEntry(const KMenu* menu,
381 const QString& entryName) const
382 {
383 Q_ASSERT(menu != 0);
384
385 const QList<QAction*> list = menu->actions();
386 const uint count = list.count();
387 for (uint i = 0; i < count; ++i) {
388 const QAction* action = list.at(i);
389 if (action->text() == entryName) {
390 return true;
391 }
392 }
393
394 return false;
395 }
396
397 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
398 {
399 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
400 if (!m_mainWindow->menuBar()->isVisible()) {
401 // TODO: it should not be necessary to uncheck the menu
402 // bar action, but currently the action states don't get
403 // updated if the menu is disabled
404 showMenuBar->setChecked(false);
405 menu->addAction(showMenuBar);
406 menu->addSeparator();
407 }
408 }
409
410 #include "dolphincontextmenu.moc"