]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
KConfig* API overhaul. only cosmetics, so don't panic.
[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 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
110 popup->addAction(propertiesAction);
111
112 if (popup->exec(QCursor::pos()) == emptyTrashAction) {
113 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted."));
114 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
115 text,
116 QString(),
117 KGuiItem(i18nc("@action:button", "Empty Trash"),
118 KIcon("user-trash"))
119 ) == KMessageBox::Continue;
120 if (del) {
121 KonqOperations::emptyTrash(m_mainWindow);
122 }
123 }
124
125 popup->deleteLater();
126 }
127
128 void DolphinContextMenu::openTrashItemContextMenu()
129 {
130 Q_ASSERT(m_context & TrashContext);
131 Q_ASSERT(m_context & ItemContext);
132
133 KMenu* popup = new KMenu(m_mainWindow);
134
135 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
136 popup->addAction(restoreAction);
137
138 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
139 popup->addAction(deleteAction);
140
141 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
142 popup->addAction(propertiesAction);
143
144 if (popup->exec(QCursor::pos()) == restoreAction) {
145 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
146 }
147
148 popup->deleteLater();
149 }
150
151 void DolphinContextMenu::openItemContextMenu()
152 {
153 Q_ASSERT(!m_fileInfo.isNull());
154
155 KMenu* popup = new KMenu(m_mainWindow);
156 insertDefaultItemActions(popup);
157
158 popup->addSeparator();
159
160 // insert 'Bookmark This Folder' entry if exactly one item is selected
161 QAction* addToPlacesAction = 0;
162 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
163 addToPlacesAction = popup->addAction(KIcon("bookmark-folder"),
164 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
165 }
166
167 // Insert 'Open With...' sub menu
168 QVector<KService::Ptr> openWithVector;
169 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
170
171 // Insert 'Actions' sub menu
172 KonqMenuActions menuActions;
173 menuActions.setItems(m_selectedItems);
174 if (menuActions.addActionsTo(popup))
175 popup->addSeparator();
176
177 // insert 'Properties...' entry
178 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
179 popup->addAction(propertiesAction);
180
181 QAction* activatedAction = popup->exec(QCursor::pos());
182
183 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
184 const KUrl selectedUrl(m_fileInfo.url());
185 if (selectedUrl.isValid()) {
186 DolphinSettings::instance().placesModel()->addPlace(selectedUrl.fileName(),
187 selectedUrl);
188 }
189 } else if (openWithActions.contains(activatedAction)) {
190 // one of the 'Open With' items has been selected
191 if (openWithActions.last() == activatedAction) {
192 // the item 'Other...' has been selected
193 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
194 } else {
195 int id = openWithActions.indexOf(activatedAction);
196 KService::Ptr servicePtr = openWithVector[id];
197 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
198 }
199 }
200
201 openWithVector.clear();
202 popup->deleteLater();
203 }
204
205 void DolphinContextMenu::openViewportContextMenu()
206 {
207 Q_ASSERT(m_fileInfo.isNull());
208 KMenu* popup = new KMenu(m_mainWindow);
209
210 // setup 'Create New' menu
211 KNewMenu* newMenu = m_mainWindow->newMenu();
212 newMenu->slotCheckUpToDate();
213 newMenu->setPopupFiles(m_baseUrl);
214 popup->addMenu(newMenu->menu());
215 popup->addSeparator();
216
217 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
218 popup->addAction(pasteAction);
219
220 // setup 'View Mode' menu
221 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
222
223 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
224 viewModeMenu->addAction(iconsMode);
225
226 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
227 viewModeMenu->addAction(detailsMode);
228
229 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
230 viewModeMenu->addAction(columnsMode);
231
232 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
233 viewModeMenu->addAction(previewsMode);
234
235 popup->addMenu(viewModeMenu);
236
237 popup->addSeparator();
238
239 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-folder"),
240 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
241 popup->addSeparator();
242
243 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
244
245 QAction* action = popup->exec(QCursor::pos());
246 if (action == propertiesAction) {
247 const KUrl& url = m_mainWindow->activeViewContainer()->url();
248 KPropertiesDialog dialog(url);
249 dialog.exec();
250 } else if (action == addToPlacesAction) {
251 const KUrl& url = m_mainWindow->activeViewContainer()->url();
252 if (url.isValid()) {
253 DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url);
254 }
255 }
256
257 popup->deleteLater();
258 }
259
260 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
261 {
262 Q_ASSERT(popup != 0);
263 const KActionCollection* collection = m_mainWindow->actionCollection();
264
265 // insert 'Cut', 'Copy' and 'Paste'
266 QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut));
267 QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy));
268 QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste));
269
270 popup->addAction(cutAction);
271 popup->addAction(copyAction);
272 popup->addAction(pasteAction);
273 popup->addSeparator();
274
275 // insert 'Rename'
276 QAction* renameAction = collection->action("rename");
277 popup->addAction(renameAction);
278
279 // insert 'Move to Trash' and (optionally) 'Delete'
280 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
281 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
282 const KUrl& url = m_mainWindow->activeViewContainer()->url();
283 if (url.isLocalFile()) {
284 QAction* moveToTrashAction = collection->action("move_to_trash");
285 popup->addAction(moveToTrashAction);
286 } else {
287 showDeleteCommand = true;
288 }
289
290 if (showDeleteCommand) {
291 QAction* deleteAction = collection->action("delete");
292 popup->addAction(deleteAction);
293 }
294 }
295
296 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
297 QVector<KService::Ptr>& openWithVector)
298 {
299 // Parts of the following code have been taken
300 // from the class KonqOperations located in
301 // libqonq/konq_operations.h of Konqueror.
302 // (Copyright (C) 2000 David Faure <faure@kde.org>)
303
304 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
305 // are listed which are registered to open the item. As last entry "Other..." will be
306 // attached which allows to select a custom application. If no applications are registered
307 // no sub menu is created at all, only "Open With..." will be offered.
308 bool insertOpenWithItems = true;
309 const QString contextMimeType(m_fileInfo.mimetype());
310
311 QListIterator<KFileItem> mimeIt(m_selectedItems);
312 while (insertOpenWithItems && mimeIt.hasNext()) {
313 KFileItem item = mimeIt.next();
314 insertOpenWithItems = (contextMimeType == item.mimetype());
315 }
316
317 QList<QAction*> openWithActions;
318 if (insertOpenWithItems) {
319 // fill the 'Open with' sub menu with application types
320 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url());
321 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
322 "Application",
323 "Type == 'Application'");
324 if (offers.count() > 0) {
325 KService::List::Iterator it;
326 KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With"));
327 for (it = offers.begin(); it != offers.end(); ++it) {
328 // The offer list from the KTrader returns duplicate
329 // application entries. Although this seems to be a configuration
330 // problem outside the scope of Dolphin, duplicated entries just
331 // will be skipped here.
332 const QString appName((*it)->name());
333 if (!containsEntry(openWithMenu, appName)) {
334 const KIcon icon((*it)->icon());
335 QAction* action = openWithMenu->addAction(icon, appName);
336 openWithVector.append(*it);
337 openWithActions << action;
338 }
339 }
340
341 openWithMenu->addSeparator();
342 QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other..."));
343
344 openWithActions << action;
345 popup->addMenu(openWithMenu);
346 } else {
347 // No applications are registered, hence just offer
348 // a "Open With..." item instead of a sub menu containing
349 // only one entry.
350 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
351 openWithActions << action;
352 }
353 } else {
354 // At least one of the selected items has a different MIME type. In this case
355 // just show a disabled "Open With..." entry.
356 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
357 action->setEnabled(false);
358 }
359
360 return openWithActions;
361 }
362
363 bool DolphinContextMenu::containsEntry(const KMenu* menu,
364 const QString& entryName) const
365 {
366 Q_ASSERT(menu != 0);
367
368 const QList<QAction*> list = menu->actions();
369 const uint count = list.count();
370 for (uint i = 0; i < count; ++i) {
371 const QAction* action = list.at(i);
372 if (action->text() == entryName) {
373 return true;
374 }
375 }
376
377 return false;
378 }
379
380 #include "dolphincontextmenu.moc"