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_operations.h>
42 #include <konq_menuactions.h>
44 #include <kpropertiesdialog.h>
46 #include <kstandardaction.h>
47 #include <kstandarddirs.h>
49 #include <QtGui/QApplication>
50 #include <QtGui/QClipboard>
51 #include <QtCore/QDir>
53 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
54 const KFileItem
& fileInfo
,
55 const KUrl
& baseUrl
) :
61 // The context menu either accesses the URLs of the selected items
62 // or the items itself. To increase the performance both lists are cached.
63 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
64 m_selectedUrls
= view
->selectedUrls();
65 m_selectedItems
= view
->selectedItems();
68 DolphinContextMenu::~DolphinContextMenu()
72 void DolphinContextMenu::open()
74 // get the context information
75 if (m_baseUrl
.protocol() == "trash") {
76 m_context
|= TrashContext
;
79 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
80 m_context
|= ItemContext
;
81 // TODO: handle other use cases like devices + desktop files
84 // open the corresponding popup for the context
85 if (m_context
& TrashContext
) {
86 if (m_context
& ItemContext
) {
87 openTrashItemContextMenu();
89 openTrashContextMenu();
91 } else if (m_context
& ItemContext
) {
92 openItemContextMenu();
94 Q_ASSERT(m_context
== NoContext
);
95 openViewportContextMenu();
99 void DolphinContextMenu::openTrashContextMenu()
101 Q_ASSERT(m_context
& TrashContext
);
103 KMenu
* popup
= new KMenu(m_mainWindow
);
105 addShowMenubarAction(popup
);
107 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
108 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
109 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
110 popup
->addAction(emptyTrashAction
);
112 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
113 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
115 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
116 popup
->addAction(propertiesAction
);
118 QAction
*action
= popup
->exec(QCursor::pos());
119 if (action
== emptyTrashAction
) {
120 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
121 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
124 KGuiItem(i18nc("@action:button", "Empty Trash"),
126 ) == KMessageBox::Continue
;
128 KonqOperations::emptyTrash(m_mainWindow
);
130 } else if (action
== addToPlacesAction
) {
131 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
133 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
137 popup
->deleteLater();
140 void DolphinContextMenu::openTrashItemContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
143 Q_ASSERT(m_context
& ItemContext
);
145 KMenu
* popup
= new KMenu(m_mainWindow
);
147 addShowMenubarAction(popup
);
149 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
150 popup
->addAction(restoreAction
);
152 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
153 popup
->addAction(deleteAction
);
155 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
156 popup
->addAction(propertiesAction
);
158 if (popup
->exec(QCursor::pos()) == restoreAction
) {
159 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
162 popup
->deleteLater();
165 void DolphinContextMenu::openItemContextMenu()
167 Q_ASSERT(!m_fileInfo
.isNull());
169 KMenu
* popup
= new KMenu(m_mainWindow
);
170 addShowMenubarAction(popup
);
171 insertDefaultItemActions(popup
);
173 popup
->addSeparator();
175 // insert 'Bookmark This Folder' entry if exactly one item is selected
176 QAction
* addToPlacesAction
= 0;
177 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
178 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
179 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
182 // Insert 'Open With...' sub menu
183 QVector
<KService::Ptr
> openWithVector
;
184 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
186 // Insert 'Actions' sub menu
187 KonqMenuActions menuActions
;
188 menuActions
.setItems(m_selectedItems
);
189 if (menuActions
.addActionsTo(popup
)) {
190 popup
->addSeparator();
193 // Insert 'Copy To' and 'Move To' sub menus
194 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
195 m_copyToMenu
.setItems(m_selectedItems
);
196 m_copyToMenu
.addActionsTo(popup
);
197 popup
->addSeparator();
200 // insert 'Properties...' entry
201 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
202 popup
->addAction(propertiesAction
);
204 QAction
* activatedAction
= popup
->exec(QCursor::pos());
206 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
207 const KUrl
selectedUrl(m_fileInfo
.url());
208 if (selectedUrl
.isValid()) {
209 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
212 } else if (openWithActions
.contains(activatedAction
)) {
213 // one of the 'Open With' items has been selected
214 if (openWithActions
.last() == activatedAction
) {
215 // the item 'Other...' has been selected
216 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
218 int id
= openWithActions
.indexOf(activatedAction
);
219 KService::Ptr servicePtr
= openWithVector
[id
];
220 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
224 openWithVector
.clear();
225 popup
->deleteLater();
228 void DolphinContextMenu::openViewportContextMenu()
230 KMenu
* popup
= new KMenu(m_mainWindow
);
232 addShowMenubarAction(popup
);
234 // setup 'Create New' menu
235 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
236 newMenu
->slotCheckUpToDate();
237 newMenu
->setPopupFiles(m_baseUrl
);
238 popup
->addMenu(newMenu
->menu());
239 popup
->addSeparator();
241 QAction
* pasteAction
= createPasteAction();
242 popup
->addAction(pasteAction
);
244 // setup 'View Mode' menu
245 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
247 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
248 viewModeMenu
->addAction(iconsMode
);
250 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
251 viewModeMenu
->addAction(detailsMode
);
253 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
254 viewModeMenu
->addAction(columnsMode
);
256 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
257 viewModeMenu
->addAction(previewsMode
);
259 popup
->addMenu(viewModeMenu
);
261 popup
->addSeparator();
263 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
264 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
265 popup
->addSeparator();
267 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
269 QAction
* action
= popup
->exec(QCursor::pos());
270 if (action
== propertiesAction
) {
271 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
272 KPropertiesDialog
dialog(url
, m_mainWindow
);
274 } else if (action
== addToPlacesAction
) {
275 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
277 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
281 popup
->deleteLater();
284 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
286 Q_ASSERT(popup
!= 0);
287 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
289 // insert 'Cut', 'Copy' and 'Paste'
290 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
291 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
292 QAction
* pasteAction
= createPasteAction();
294 popup
->addAction(cutAction
);
295 popup
->addAction(copyAction
);
296 popup
->addAction(pasteAction
);
297 popup
->addSeparator();
300 QAction
* renameAction
= collection
->action("rename");
301 popup
->addAction(renameAction
);
303 // insert 'Move to Trash' and (optionally) 'Delete'
304 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
305 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
306 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
307 if (url
.isLocalFile()) {
308 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
309 popup
->addAction(moveToTrashAction
);
311 showDeleteCommand
= true;
314 if (showDeleteCommand
) {
315 QAction
* deleteAction
= collection
->action("delete");
316 popup
->addAction(deleteAction
);
320 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
321 QVector
<KService::Ptr
>& openWithVector
)
323 // Parts of the following code have been taken
324 // from the class KonqOperations located in
325 // libqonq/konq_operations.h of Konqueror.
326 // (Copyright (C) 2000 David Faure <faure@kde.org>)
328 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
329 // are listed which are registered to open the item. As last entry "Other..." will be
330 // attached which allows to select a custom application. If no applications are registered
331 // no sub menu is created at all, only "Open With..." will be offered.
332 bool insertOpenWithItems
= true;
333 const QString
contextMimeType(m_fileInfo
.mimetype());
335 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
336 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
337 KFileItem item
= mimeIt
.next();
338 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
341 QList
<QAction
*> openWithActions
;
342 if (insertOpenWithItems
) {
343 // fill the 'Open with' sub menu with application types
344 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
345 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
347 "Type == 'Application'");
348 if (offers
.count() > 0) {
349 KService::List::Iterator it
;
350 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
351 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
352 // The offer list from the KTrader returns duplicate
353 // application entries. Although this seems to be a configuration
354 // problem outside the scope of Dolphin, duplicated entries just
355 // will be skipped here.
356 const QString
appName((*it
)->name());
357 if (!containsEntry(openWithMenu
, appName
)) {
358 const KIcon
icon((*it
)->icon());
359 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
360 openWithVector
.append(*it
);
361 openWithActions
<< action
;
365 openWithMenu
->addSeparator();
366 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
368 openWithActions
<< action
;
369 popup
->addMenu(openWithMenu
);
371 // No applications are registered, hence just offer
372 // a "Open With..." item instead of a sub menu containing
374 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
375 openWithActions
<< action
;
378 // At least one of the selected items has a different MIME type. In this case
379 // just show a disabled "Open With..." entry.
380 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
381 action
->setEnabled(false);
384 return openWithActions
;
387 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
388 const QString
& entryName
) const
392 const QList
<QAction
*> list
= menu
->actions();
393 const uint count
= list
.count();
394 for (uint i
= 0; i
< count
; ++i
) {
395 const QAction
* action
= list
.at(i
);
396 if (action
->text() == entryName
) {
404 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
406 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
407 if (!m_mainWindow
->menuBar()->isVisible()) {
408 // TODO: it should not be necessary to uncheck the menu
409 // bar action, but currently the action states don't get
410 // updated if the menu is disabled
411 showMenuBar
->setChecked(false);
412 menu
->addAction(showMenuBar
);
413 menu
->addSeparator();
417 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
419 QString name
= url
.fileName();
420 if (name
.isEmpty()) {
426 QAction
* DolphinContextMenu::createPasteAction()
429 if ((m_selectedItems
.count() == 1) && m_fileInfo
.isDir()) {
430 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
431 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
432 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
433 action
->setEnabled(!pasteData
.isEmpty());
434 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
436 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
442 #include "dolphincontextmenu.moc"