1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphincontextmenu.h"
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"
30 #include <kactioncollection.h>
31 #include <kfileplacesmodel.h>
32 #include <kdesktopfile.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <konq_menuactions.h>
44 #include <konq_popupmenuinformation.h>
46 #include <kpropertiesdialog.h>
48 #include <kstandardaction.h>
49 #include <kstandarddirs.h>
51 #include <QtGui/QApplication>
52 #include <QtGui/QClipboard>
53 #include <QtCore/QDir>
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
56 const KFileItem
& fileInfo
,
57 const KUrl
& baseUrl
) :
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();
71 DolphinContextMenu::~DolphinContextMenu()
73 delete m_capabilities
;
77 void DolphinContextMenu::open()
79 // get the context information
80 if (m_baseUrl
.protocol() == "trash") {
81 m_context
|= TrashContext
;
84 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
85 m_context
|= ItemContext
;
86 // TODO: handle other use cases like devices + desktop files
89 // open the corresponding popup for the context
90 if (m_context
& TrashContext
) {
91 if (m_context
& ItemContext
) {
92 openTrashItemContextMenu();
94 openTrashContextMenu();
96 } else if (m_context
& ItemContext
) {
97 openItemContextMenu();
99 Q_ASSERT(m_context
== NoContext
);
100 openViewportContextMenu();
104 void DolphinContextMenu::openTrashContextMenu()
106 Q_ASSERT(m_context
& TrashContext
);
108 KMenu
* popup
= new KMenu(m_mainWindow
);
110 addShowMenubarAction(popup
);
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
);
117 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
118 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
120 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
121 popup
->addAction(propertiesAction
);
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
,
129 KGuiItem(i18nc("@action:button", "Empty Trash"),
131 ) == KMessageBox::Continue
;
133 KonqOperations::emptyTrash(m_mainWindow
);
135 } else if (action
== addToPlacesAction
) {
136 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
138 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
142 popup
->deleteLater();
145 void DolphinContextMenu::openTrashItemContextMenu()
147 Q_ASSERT(m_context
& TrashContext
);
148 Q_ASSERT(m_context
& ItemContext
);
150 KMenu
* popup
= new KMenu(m_mainWindow
);
152 addShowMenubarAction(popup
);
154 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
155 popup
->addAction(restoreAction
);
157 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
158 popup
->addAction(deleteAction
);
160 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
161 popup
->addAction(propertiesAction
);
163 if (popup
->exec(QCursor::pos()) == restoreAction
) {
164 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
167 popup
->deleteLater();
170 void DolphinContextMenu::openItemContextMenu()
172 Q_ASSERT(!m_fileInfo
.isNull());
174 KMenu
* popup
= new KMenu(m_mainWindow
);
175 addShowMenubarAction(popup
);
176 insertDefaultItemActions(popup
);
178 popup
->addSeparator();
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"));
187 // Insert 'Open With...' sub menu
188 QVector
<KService::Ptr
> openWithVector
;
189 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
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();
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();
208 // insert 'Properties...' entry
209 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
210 popup
->addAction(propertiesAction
);
212 QAction
* activatedAction
= popup
->exec(QCursor::pos());
214 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
215 const KUrl
selectedUrl(m_fileInfo
.url());
216 if (selectedUrl
.isValid()) {
217 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
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
);
226 int id
= openWithActions
.indexOf(activatedAction
);
227 KService::Ptr servicePtr
= openWithVector
[id
];
228 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
232 openWithVector
.clear();
233 popup
->deleteLater();
236 void DolphinContextMenu::openViewportContextMenu()
238 KMenu
* popup
= new KMenu(m_mainWindow
);
240 addShowMenubarAction(popup
);
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();
249 QAction
* pasteAction
= createPasteAction();
250 popup
->addAction(pasteAction
);
252 // setup 'View Mode' menu
253 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
255 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
256 viewModeMenu
->addAction(iconsMode
);
258 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
259 viewModeMenu
->addAction(detailsMode
);
261 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
262 viewModeMenu
->addAction(columnsMode
);
264 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
265 viewModeMenu
->addAction(previewsMode
);
267 popup
->addMenu(viewModeMenu
);
269 popup
->addSeparator();
271 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
272 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
273 popup
->addSeparator();
275 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
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
);
282 } else if (action
== addToPlacesAction
) {
283 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
285 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
289 popup
->deleteLater();
292 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
294 Q_ASSERT(popup
!= 0);
295 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
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();
302 popup
->addAction(cutAction
);
303 popup
->addAction(copyAction
);
304 popup
->addAction(pasteAction
);
305 popup
->addSeparator();
308 QAction
* renameAction
= collection
->action("rename");
309 popup
->addAction(renameAction
);
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
);
319 showDeleteCommand
= true;
322 if (showDeleteCommand
) {
323 QAction
* deleteAction
= collection
->action("delete");
324 popup
->addAction(deleteAction
);
328 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
329 QVector
<KService::Ptr
>& openWithVector
)
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>)
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());
343 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
344 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
345 KFileItem item
= mimeIt
.next();
346 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
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(),
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
;
373 openWithMenu
->addSeparator();
374 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
376 openWithActions
<< action
;
377 popup
->addMenu(openWithMenu
);
379 // No applications are registered, hence just offer
380 // a "Open With..." item instead of a sub menu containing
382 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
383 openWithActions
<< action
;
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);
392 return openWithActions
;
395 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
396 const QString
& entryName
) const
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
) {
412 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
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();
425 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
427 QString name
= url
.fileName();
428 if (name
.isEmpty()) {
434 QAction
* DolphinContextMenu::createPasteAction()
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()));
444 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
450 FileItemCapabilities
& DolphinContextMenu::capabilities()
452 if (m_capabilities
== 0) {
453 m_capabilities
= new FileItemCapabilities(m_selectedItems
);
455 return *m_capabilities
;
458 #include "dolphincontextmenu.moc"