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