]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Fix the edit-menu action disabling/enabling bug in dolphinpart finally! Also, move...
[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
29 #include <kactioncollection.h>
30 #include <kfileplacesmodel.h>
31 #include <kdesktopfile.h>
32 #include <kglobal.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmenu.h>
36 #include <kmenubar.h>
37 #include <kmessagebox.h>
38 #include <kmimetypetrader.h>
39 #include <knewmenu.h>
40 #include <konqmimedata.h>
41 #include <konq_fileitemcapabilities.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 // TODO: port to menuActions.addOpenWithActionsTo(popup);
189 QVector<KService::Ptr> openWithVector;
190 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
191
192 KonqPopupMenuInformation popupInfo;
193 popupInfo.setItems(m_selectedItems);
194 popupInfo.setReadOnly(!capabilities().supportsMoving());
195 popupInfo.setParentWidget(m_mainWindow);
196
197 // Insert 'Actions' sub menu
198 KonqMenuActions menuActions;
199 menuActions.setPopupMenuInfo(popupInfo);
200 if (menuActions.addActionsTo(popup)) {
201 popup->addSeparator();
202 }
203
204 // Insert 'Copy To' and 'Move To' sub menus
205 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
206 m_copyToMenu.setItems(m_selectedItems);
207 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
208 m_copyToMenu.addActionsTo(popup);
209 popup->addSeparator();
210 }
211
212 // insert 'Properties...' entry
213 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
214 popup->addAction(propertiesAction);
215
216 QAction* activatedAction = popup->exec(QCursor::pos());
217
218 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
219 const KUrl selectedUrl(m_fileInfo.url());
220 if (selectedUrl.isValid()) {
221 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
222 selectedUrl);
223 }
224 } else if (openWithActions.contains(activatedAction)) {
225 // one of the 'Open With' items has been selected
226 if (openWithActions.last() == activatedAction) {
227 // the item 'Other...' has been selected
228 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
229 } else {
230 int id = openWithActions.indexOf(activatedAction);
231 KService::Ptr servicePtr = openWithVector[id];
232 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
233 }
234 }
235
236 openWithVector.clear();
237 popup->deleteLater();
238 }
239
240 void DolphinContextMenu::openViewportContextMenu()
241 {
242 KMenu* popup = new KMenu(m_mainWindow);
243
244 addShowMenubarAction(popup);
245
246 // setup 'Create New' menu
247 KNewMenu* newMenu = m_mainWindow->newMenu();
248 newMenu->slotCheckUpToDate();
249 newMenu->setPopupFiles(m_baseUrl);
250 popup->addMenu(newMenu->menu());
251 popup->addSeparator();
252
253 QAction* pasteAction = createPasteAction();
254 popup->addAction(pasteAction);
255
256 // setup 'View Mode' menu
257 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
258
259 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
260 viewModeMenu->addAction(iconsMode);
261
262 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
263 viewModeMenu->addAction(detailsMode);
264
265 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
266 viewModeMenu->addAction(columnsMode);
267
268 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
269 viewModeMenu->addAction(previewsMode);
270
271 popup->addMenu(viewModeMenu);
272
273 popup->addSeparator();
274
275 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
276 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
277 popup->addSeparator();
278
279 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
280
281 QAction* action = popup->exec(QCursor::pos());
282 if (action == propertiesAction) {
283 const KUrl& url = m_mainWindow->activeViewContainer()->url();
284 KPropertiesDialog dialog(url, m_mainWindow);
285 dialog.exec();
286 } else if (action == addToPlacesAction) {
287 const KUrl& url = m_mainWindow->activeViewContainer()->url();
288 if (url.isValid()) {
289 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
290 }
291 }
292
293 popup->deleteLater();
294 }
295
296 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
297 {
298 Q_ASSERT(popup != 0);
299 const KActionCollection* collection = m_mainWindow->actionCollection();
300
301 // insert 'Cut', 'Copy' and 'Paste'
302 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
303 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
304 QAction* pasteAction = createPasteAction();
305
306 popup->addAction(cutAction);
307 popup->addAction(copyAction);
308 popup->addAction(pasteAction);
309 popup->addSeparator();
310
311 // insert 'Rename'
312 QAction* renameAction = collection->action("rename");
313 popup->addAction(renameAction);
314
315 // insert 'Move to Trash' and (optionally) 'Delete'
316 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
317 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
318 const KUrl& url = m_mainWindow->activeViewContainer()->url();
319 if (url.isLocalFile()) {
320 QAction* moveToTrashAction = collection->action("move_to_trash");
321 popup->addAction(moveToTrashAction);
322 } else {
323 showDeleteCommand = true;
324 }
325
326 if (showDeleteCommand) {
327 QAction* deleteAction = collection->action("delete");
328 popup->addAction(deleteAction);
329 }
330 }
331
332 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
333 QVector<KService::Ptr>& openWithVector)
334 {
335 // Parts of the following code have been taken
336 // from the class KonqOperations located in
337 // libqonq/konq_operations.h of Konqueror.
338 // (Copyright (C) 2000 David Faure <faure@kde.org>)
339
340 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
341 // are listed which are registered to open the item. As last entry "Other..." will be
342 // attached which allows to select a custom application. If no applications are registered
343 // no sub menu is created at all, only "Open With..." will be offered.
344 bool insertOpenWithItems = true;
345 const QString contextMimeType(m_fileInfo.mimetype());
346
347 QListIterator<KFileItem> mimeIt(m_selectedItems);
348 while (insertOpenWithItems && mimeIt.hasNext()) {
349 KFileItem item = mimeIt.next();
350 insertOpenWithItems = (contextMimeType == item.mimetype());
351 }
352
353 QList<QAction*> openWithActions;
354 if (insertOpenWithItems) {
355 // fill the 'Open with' sub menu with application types
356 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url());
357 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
358 "Application",
359 "Type == 'Application'");
360 if (offers.count() > 0) {
361 KService::List::Iterator it;
362 KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With"));
363 for (it = offers.begin(); it != offers.end(); ++it) {
364 // The offer list from the KTrader returns duplicate
365 // application entries. Although this seems to be a configuration
366 // problem outside the scope of Dolphin, duplicated entries just
367 // will be skipped here.
368 const QString appName((*it)->name());
369 if (!containsEntry(openWithMenu, appName)) {
370 const KIcon icon((*it)->icon());
371 QAction* action = openWithMenu->addAction(icon, appName);
372 openWithVector.append(*it);
373 openWithActions << action;
374 }
375 }
376
377 openWithMenu->addSeparator();
378 QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other..."));
379
380 openWithActions << action;
381 popup->addMenu(openWithMenu);
382 } else {
383 // No applications are registered, hence just offer
384 // a "Open With..." item instead of a sub menu containing
385 // only one entry.
386 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
387 openWithActions << action;
388 }
389 } else {
390 // At least one of the selected items has a different MIME type. In this case
391 // just show a disabled "Open With..." entry.
392 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
393 action->setEnabled(false);
394 }
395
396 return openWithActions;
397 }
398
399 bool DolphinContextMenu::containsEntry(const KMenu* menu,
400 const QString& entryName) const
401 {
402 Q_ASSERT(menu != 0);
403
404 const QList<QAction*> list = menu->actions();
405 const uint count = list.count();
406 for (uint i = 0; i < count; ++i) {
407 const QAction* action = list.at(i);
408 if (action->text() == entryName) {
409 return true;
410 }
411 }
412
413 return false;
414 }
415
416 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
417 {
418 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
419 if (!m_mainWindow->menuBar()->isVisible()) {
420 // TODO: it should not be necessary to uncheck the menu
421 // bar action, but currently the action states don't get
422 // updated if the menu is disabled
423 showMenuBar->setChecked(false);
424 menu->addAction(showMenuBar);
425 menu->addSeparator();
426 }
427 }
428
429 QString DolphinContextMenu::placesName(const KUrl& url) const
430 {
431 QString name = url.fileName();
432 if (name.isEmpty()) {
433 name = url.host();
434 }
435 return name;
436 }
437
438 QAction* DolphinContextMenu::createPasteAction()
439 {
440 QAction* action = 0;
441 if ((m_selectedItems.count() == 1) && m_fileInfo.isDir()) {
442 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
443 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
444 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
445 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
446 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
447 } else {
448 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
449 }
450
451 return action;
452 }
453
454 KonqFileItemCapabilities& DolphinContextMenu::capabilities()
455 {
456 if (m_capabilities == 0) {
457 m_capabilities = new KonqFileItemCapabilities(m_selectedItems);
458 }
459 return *m_capabilities;
460 }
461
462 #include "dolphincontextmenu.moc"