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"
29 #include <kactioncollection.h>
30 #include <kfileplacesmodel.h>
31 #include <kdesktopfile.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
37 #include <kmessagebox.h>
38 #include <kmimetypetrader.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>
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 // TODO: port to menuActions.addOpenWithActionsTo(popup);
189 QVector
<KService::Ptr
> openWithVector
;
190 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
192 KonqPopupMenuInformation popupInfo
;
193 popupInfo
.setItems(m_selectedItems
);
194 popupInfo
.setReadOnly(!capabilities().supportsMoving());
195 popupInfo
.setParentWidget(m_mainWindow
);
197 // Insert 'Actions' sub menu
198 KonqMenuActions menuActions
;
199 menuActions
.setPopupMenuInfo(popupInfo
);
200 if (menuActions
.addActionsTo(popup
)) {
201 popup
->addSeparator();
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();
212 // insert 'Properties...' entry
213 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
214 popup
->addAction(propertiesAction
);
216 QAction
* activatedAction
= popup
->exec(QCursor::pos());
218 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
219 const KUrl
selectedUrl(m_fileInfo
.url());
220 if (selectedUrl
.isValid()) {
221 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
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
);
230 int id
= openWithActions
.indexOf(activatedAction
);
231 KService::Ptr servicePtr
= openWithVector
[id
];
232 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
236 openWithVector
.clear();
237 popup
->deleteLater();
240 void DolphinContextMenu::openViewportContextMenu()
242 KMenu
* popup
= new KMenu(m_mainWindow
);
244 addShowMenubarAction(popup
);
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();
253 QAction
* pasteAction
= createPasteAction();
254 popup
->addAction(pasteAction
);
256 // setup 'View Mode' menu
257 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
259 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
260 viewModeMenu
->addAction(iconsMode
);
262 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
263 viewModeMenu
->addAction(detailsMode
);
265 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
266 viewModeMenu
->addAction(columnsMode
);
268 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
269 viewModeMenu
->addAction(previewsMode
);
271 popup
->addMenu(viewModeMenu
);
273 popup
->addSeparator();
275 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
276 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
277 popup
->addSeparator();
279 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
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
);
286 } else if (action
== addToPlacesAction
) {
287 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
289 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
293 popup
->deleteLater();
296 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
298 Q_ASSERT(popup
!= 0);
299 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
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();
306 popup
->addAction(cutAction
);
307 popup
->addAction(copyAction
);
308 popup
->addAction(pasteAction
);
309 popup
->addSeparator();
312 QAction
* renameAction
= collection
->action("rename");
313 popup
->addAction(renameAction
);
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
);
323 showDeleteCommand
= true;
326 if (showDeleteCommand
) {
327 QAction
* deleteAction
= collection
->action("delete");
328 popup
->addAction(deleteAction
);
332 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
333 QVector
<KService::Ptr
>& openWithVector
)
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>)
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());
347 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
348 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
349 KFileItem item
= mimeIt
.next();
350 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
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(),
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
;
377 openWithMenu
->addSeparator();
378 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
380 openWithActions
<< action
;
381 popup
->addMenu(openWithMenu
);
383 // No applications are registered, hence just offer
384 // a "Open With..." item instead of a sub menu containing
386 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
387 openWithActions
<< action
;
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);
396 return openWithActions
;
399 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
400 const QString
& entryName
) const
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
) {
416 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
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();
429 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
431 QString name
= url
.fileName();
432 if (name
.isEmpty()) {
438 QAction
* DolphinContextMenu::createPasteAction()
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()));
448 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
454 KonqFileItemCapabilities
& DolphinContextMenu::capabilities()
456 if (m_capabilities
== 0) {
457 m_capabilities
= new KonqFileItemCapabilities(m_selectedItems
);
459 return *m_capabilities
;
462 #include "dolphincontextmenu.moc"