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"
28 #include <kactioncollection.h>
29 #include <kfileplacesmodel.h>
30 #include <kdesktopfile.h>
32 #include <kiconloader.h>
33 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
41 #include <konq_menuactions.h>
43 #include <kpropertiesdialog.h>
45 #include <kstandardaction.h>
46 #include <kstandarddirs.h>
48 #include <QtGui/QApplication>
49 #include <QtGui/QClipboard>
50 #include <QtCore/QDir>
52 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
53 const KFileItem
& fileInfo
,
54 const KUrl
& baseUrl
) :
60 // The context menu either accesses the URLs of the selected items
61 // or the items itself. To increase the performance both lists are cached.
62 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
63 m_selectedUrls
= view
->selectedUrls();
64 m_selectedItems
= view
->selectedItems();
67 DolphinContextMenu::~DolphinContextMenu()
71 void DolphinContextMenu::open()
73 // get the context information
74 if (m_baseUrl
.protocol() == "trash") {
75 m_context
|= TrashContext
;
78 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
79 m_context
|= ItemContext
;
80 // TODO: handle other use cases like devices + desktop files
83 // open the corresponding popup for the context
84 if (m_context
& TrashContext
) {
85 if (m_context
& ItemContext
) {
86 openTrashItemContextMenu();
88 openTrashContextMenu();
90 } else if (m_context
& ItemContext
) {
91 openItemContextMenu();
93 Q_ASSERT(m_context
== NoContext
);
94 openViewportContextMenu();
98 void DolphinContextMenu::openTrashContextMenu()
100 Q_ASSERT(m_context
& TrashContext
);
102 KMenu
* popup
= new KMenu(m_mainWindow
);
104 addShowMenubarAction(popup
);
106 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
107 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
108 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
109 popup
->addAction(emptyTrashAction
);
111 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
112 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
114 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
115 popup
->addAction(propertiesAction
);
117 QAction
*action
= popup
->exec(QCursor::pos());
118 if (action
== emptyTrashAction
) {
119 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
120 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
123 KGuiItem(i18nc("@action:button", "Empty Trash"),
125 ) == KMessageBox::Continue
;
127 KonqOperations::emptyTrash(m_mainWindow
);
129 } else if (action
== addToPlacesAction
) {
130 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
132 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
136 popup
->deleteLater();
139 void DolphinContextMenu::openTrashItemContextMenu()
141 Q_ASSERT(m_context
& TrashContext
);
142 Q_ASSERT(m_context
& ItemContext
);
144 KMenu
* popup
= new KMenu(m_mainWindow
);
146 addShowMenubarAction(popup
);
148 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
149 popup
->addAction(restoreAction
);
151 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
152 popup
->addAction(deleteAction
);
154 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
155 popup
->addAction(propertiesAction
);
157 if (popup
->exec(QCursor::pos()) == restoreAction
) {
158 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
161 popup
->deleteLater();
164 void DolphinContextMenu::openItemContextMenu()
166 Q_ASSERT(!m_fileInfo
.isNull());
168 KMenu
* popup
= new KMenu(m_mainWindow
);
169 addShowMenubarAction(popup
);
170 insertDefaultItemActions(popup
);
172 popup
->addSeparator();
174 // insert 'Bookmark This Folder' entry if exactly one item is selected
175 QAction
* addToPlacesAction
= 0;
176 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
177 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
178 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
181 // Insert 'Open With...' sub menu
182 QVector
<KService::Ptr
> openWithVector
;
183 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
185 // Insert 'Actions' sub menu
186 KonqMenuActions menuActions
;
187 menuActions
.setItems(m_selectedItems
);
188 if (menuActions
.addActionsTo(popup
))
189 popup
->addSeparator();
191 // insert 'Properties...' entry
192 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
193 popup
->addAction(propertiesAction
);
195 QAction
* activatedAction
= popup
->exec(QCursor::pos());
197 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
198 const KUrl
selectedUrl(m_fileInfo
.url());
199 if (selectedUrl
.isValid()) {
200 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
203 } else if (openWithActions
.contains(activatedAction
)) {
204 // one of the 'Open With' items has been selected
205 if (openWithActions
.last() == activatedAction
) {
206 // the item 'Other...' has been selected
207 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
209 int id
= openWithActions
.indexOf(activatedAction
);
210 KService::Ptr servicePtr
= openWithVector
[id
];
211 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
215 openWithVector
.clear();
216 popup
->deleteLater();
219 void DolphinContextMenu::openViewportContextMenu()
221 KMenu
* popup
= new KMenu(m_mainWindow
);
223 addShowMenubarAction(popup
);
225 // setup 'Create New' menu
226 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
227 newMenu
->slotCheckUpToDate();
228 newMenu
->setPopupFiles(m_baseUrl
);
229 popup
->addMenu(newMenu
->menu());
230 popup
->addSeparator();
232 QAction
* pasteAction
= createPasteAction();
233 popup
->addAction(pasteAction
);
235 // setup 'View Mode' menu
236 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
238 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
239 viewModeMenu
->addAction(iconsMode
);
241 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
242 viewModeMenu
->addAction(detailsMode
);
244 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
245 viewModeMenu
->addAction(columnsMode
);
247 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
248 viewModeMenu
->addAction(previewsMode
);
250 popup
->addMenu(viewModeMenu
);
252 popup
->addSeparator();
254 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
255 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
256 popup
->addSeparator();
258 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
260 QAction
* action
= popup
->exec(QCursor::pos());
261 if (action
== propertiesAction
) {
262 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
263 KPropertiesDialog
dialog(url
, m_mainWindow
);
265 } else if (action
== addToPlacesAction
) {
266 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
268 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
272 popup
->deleteLater();
275 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
277 Q_ASSERT(popup
!= 0);
278 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
280 // insert 'Cut', 'Copy' and 'Paste'
281 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
282 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
283 QAction
* pasteAction
= createPasteAction();
285 popup
->addAction(cutAction
);
286 popup
->addAction(copyAction
);
287 popup
->addAction(pasteAction
);
288 popup
->addSeparator();
291 QAction
* renameAction
= collection
->action("rename");
292 popup
->addAction(renameAction
);
294 // insert 'Move to Trash' and (optionally) 'Delete'
295 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
296 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
297 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
298 if (url
.isLocalFile()) {
299 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
300 popup
->addAction(moveToTrashAction
);
302 showDeleteCommand
= true;
305 if (showDeleteCommand
) {
306 QAction
* deleteAction
= collection
->action("delete");
307 popup
->addAction(deleteAction
);
311 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
312 QVector
<KService::Ptr
>& openWithVector
)
314 // Parts of the following code have been taken
315 // from the class KonqOperations located in
316 // libqonq/konq_operations.h of Konqueror.
317 // (Copyright (C) 2000 David Faure <faure@kde.org>)
319 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
320 // are listed which are registered to open the item. As last entry "Other..." will be
321 // attached which allows to select a custom application. If no applications are registered
322 // no sub menu is created at all, only "Open With..." will be offered.
323 bool insertOpenWithItems
= true;
324 const QString
contextMimeType(m_fileInfo
.mimetype());
326 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
327 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
328 KFileItem item
= mimeIt
.next();
329 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
332 QList
<QAction
*> openWithActions
;
333 if (insertOpenWithItems
) {
334 // fill the 'Open with' sub menu with application types
335 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
336 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
338 "Type == 'Application'");
339 if (offers
.count() > 0) {
340 KService::List::Iterator it
;
341 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
342 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
343 // The offer list from the KTrader returns duplicate
344 // application entries. Although this seems to be a configuration
345 // problem outside the scope of Dolphin, duplicated entries just
346 // will be skipped here.
347 const QString
appName((*it
)->name());
348 if (!containsEntry(openWithMenu
, appName
)) {
349 const KIcon
icon((*it
)->icon());
350 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
351 openWithVector
.append(*it
);
352 openWithActions
<< action
;
356 openWithMenu
->addSeparator();
357 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
359 openWithActions
<< action
;
360 popup
->addMenu(openWithMenu
);
362 // No applications are registered, hence just offer
363 // a "Open With..." item instead of a sub menu containing
365 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
366 openWithActions
<< action
;
369 // At least one of the selected items has a different MIME type. In this case
370 // just show a disabled "Open With..." entry.
371 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
372 action
->setEnabled(false);
375 return openWithActions
;
378 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
379 const QString
& entryName
) const
383 const QList
<QAction
*> list
= menu
->actions();
384 const uint count
= list
.count();
385 for (uint i
= 0; i
< count
; ++i
) {
386 const QAction
* action
= list
.at(i
);
387 if (action
->text() == entryName
) {
395 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
397 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
398 if (!m_mainWindow
->menuBar()->isVisible()) {
399 // TODO: it should not be necessary to uncheck the menu
400 // bar action, but currently the action states don't get
401 // updated if the menu is disabled
402 showMenuBar
->setChecked(false);
403 menu
->addAction(showMenuBar
);
404 menu
->addSeparator();
408 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
410 QString name
= url
.fileName();
411 if (name
.isEmpty()) {
417 QAction
* DolphinContextMenu::createPasteAction()
420 if ((m_selectedItems
.count() == 1) && m_fileInfo
.isDir()) {
421 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
422 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
423 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
424 action
->setEnabled(!pasteData
.isEmpty());
425 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
427 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
433 #include "dolphincontextmenu.moc"