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 "editbookmarkdialog.h"
28 #include <kactioncollection.h>
29 #include <kbookmarkmanager.h>
30 #include <kbookmark.h>
31 #include <kdesktopfile.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
39 #include <konq_operations.h>
41 #include <kpropertiesdialog.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
48 DolphinContextMenu::DolphinContextMenu(DolphinView
* parent
,
49 KFileItem
* fileInfo
) :
50 m_dolphinView(parent
),
56 void DolphinContextMenu::open()
58 // get the context information
59 const KUrl
& url
= m_dolphinView
->url();
60 if (url
.protocol() == "trash") {
61 m_context
|= TrashContext
;
64 if (m_fileInfo
!= 0) {
65 m_context
|= ItemContext
;
67 // TODO: handle other use cases like devices + desktop files
69 const KUrl::List urls = m_dolphinView->selectedUrls();
71 KUrl::List::const_iterator it = urls.begin();
72 KUrl::List::const_iterator end = urls.end();
74 const KUrl& url = *it;
80 // open the corresponding popup for the context
81 if (m_context
& TrashContext
) {
82 if (m_context
& ItemContext
) {
83 openTrashItemContextMenu();
86 openTrashContextMenu();
89 else if (m_context
& ItemContext
) {
90 openItemContextMenu();
93 Q_ASSERT(m_context
== NoContext
);
94 openViewportContextMenu();
98 DolphinContextMenu::~DolphinContextMenu()
102 void DolphinContextMenu::openTrashContextMenu()
104 Q_ASSERT(m_context
& TrashContext
);
106 KMenu
* popup
= new KMenu(m_dolphinView
);
108 QAction
* emptyTrashAction
= new QAction(KIcon("user-trash"), i18n("Emtpy Trash"), popup
);
109 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
110 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
111 popup
->addAction(emptyTrashAction
);
113 DolphinMainWindow
* mainWindow
= m_dolphinView
->mainWindow();
114 QAction
* propertiesAction
= mainWindow
->actionCollection()->action("properties");
115 popup
->addAction(propertiesAction
);
117 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
118 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
119 const bool del
= KMessageBox::warningContinueCancel(mainWindow
,
122 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
123 ) == KMessageBox::Continue
;
125 KonqOperations::emptyTrash(m_dolphinView
);
129 popup
->deleteLater();
132 void DolphinContextMenu::openTrashItemContextMenu()
134 Q_ASSERT(m_context
& TrashContext
);
135 Q_ASSERT(m_context
& ItemContext
);
137 KMenu
* popup
= new KMenu(m_dolphinView
);
139 DolphinMainWindow
* mainWindow
= m_dolphinView
->mainWindow();
140 QAction
* restoreAction
= new QAction(i18n("Restore"), m_dolphinView
);
141 popup
->addAction(restoreAction
);
143 QAction
* deleteAction
= mainWindow
->actionCollection()->action("delete");
144 popup
->addAction(deleteAction
);
146 QAction
* propertiesAction
= mainWindow
->actionCollection()->action("properties");
147 popup
->addAction(propertiesAction
);
149 if (popup
->exec(QCursor::pos()) == restoreAction
) {
150 const KUrl::List urls
= m_dolphinView
->selectedUrls();
151 KonqOperations::restoreTrashedItems(urls
, m_dolphinView
);
154 popup
->deleteLater();
157 void DolphinContextMenu::openItemContextMenu()
159 Q_ASSERT(m_fileInfo
!= 0);
161 KMenu
* popup
= new KMenu(m_dolphinView
);
162 insertDefaultItemActions(popup
);
164 DolphinMainWindow
* mainWindow
= m_dolphinView
->mainWindow();
165 const KUrl::List urls
= m_dolphinView
->selectedUrls();
167 // insert 'Bookmark this folder' entry if exactly one item is selected
168 QAction
* bookmarkAction
= 0;
169 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
170 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
173 // Insert 'Open With...' sub menu
174 QVector
<KService::Ptr
> openWithVector
;
175 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
177 // Insert 'Actions' sub menu
178 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
179 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
180 popup
->addSeparator();
182 // insert 'Properties...' entry
183 QAction
* propertiesAction
= mainWindow
->actionCollection()->action("properties");
184 popup
->addAction(propertiesAction
);
186 QAction
* activatedAction
= popup
->exec(QCursor::pos());
188 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
189 const KUrl
selectedUrl(m_fileInfo
->url());
190 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
191 selectedUrl
.fileName(),
194 if (!bookmark
.isNull()) {
195 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
196 KBookmarkGroup root
= manager
->root();
197 root
.addBookmark(manager
, bookmark
);
198 manager
->emitChanged(root
);
201 else if (serviceActions
.contains(activatedAction
)) {
202 // one of the 'Actions' items has been selected
203 int id
= serviceActions
.indexOf(activatedAction
);
204 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
206 else if (openWithActions
.contains(activatedAction
)) {
207 // one of the 'Open With' items has been selected
208 if (openWithActions
.last() == activatedAction
) {
209 // the item 'Other...' has been selected
210 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
213 int id
= openWithActions
.indexOf(activatedAction
);
214 KService::Ptr servicePtr
= openWithVector
[id
];
215 KRun::run(*servicePtr
, urls
, m_dolphinView
);
219 openWithVector
.clear();
220 actionsVector
.clear();
221 popup
->deleteLater();
224 void DolphinContextMenu::openViewportContextMenu()
226 Q_ASSERT(m_fileInfo
== 0);
227 DolphinMainWindow
* mainWindow
= m_dolphinView
->mainWindow();
228 KMenu
* popup
= new KMenu(m_dolphinView
);
230 // setup 'Create New' menu
231 KNewMenu
* newMenu
= mainWindow
->newMenu();
232 newMenu
->slotCheckUpToDate();
233 newMenu
->setPopupFiles(m_dolphinView
->url());
234 popup
->addMenu(newMenu
->menu());
235 popup
->addSeparator();
237 QAction
* pasteAction
= mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
238 popup
->addAction(pasteAction
);
240 // setup 'View Mode' menu
241 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
243 QAction
* iconsMode
= mainWindow
->actionCollection()->action("icons");
244 viewModeMenu
->addAction(iconsMode
);
246 QAction
* detailsMode
= mainWindow
->actionCollection()->action("details");
247 viewModeMenu
->addAction(detailsMode
);
249 QAction
* previewsMode
= mainWindow
->actionCollection()->action("previews");
250 viewModeMenu
->addAction(previewsMode
);
252 popup
->addMenu(viewModeMenu
);
253 popup
->addSeparator();
255 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
256 popup
->addSeparator();
258 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
260 QAction
* activatedAction
= popup
->exec(QCursor::pos());
261 if (activatedAction
== propertiesAction
) {
262 new KPropertiesDialog(mainWindow
->activeView()->url());
264 else if (activatedAction
== bookmarkAction
) {
265 const KUrl
& url
= mainWindow
->activeView()->url();
266 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
270 if (!bookmark
.isNull()) {
271 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
272 KBookmarkGroup root
= manager
->root();
273 root
.addBookmark(manager
, bookmark
);
274 manager
->emitChanged(root
);
278 popup
->deleteLater();
281 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
283 Q_ASSERT(popup
!= 0);
285 DolphinMainWindow
* mainWindow
= m_dolphinView
->mainWindow();
286 const KUrl::List urls
= m_dolphinView
->selectedUrls();
288 // insert 'Cut', 'Copy' and 'Paste'
289 const KStandardAction::StandardAction actionNames
[] = {
290 KStandardAction::Cut
,
291 KStandardAction::Copy
,
292 KStandardAction::Paste
295 const int count
= sizeof(actionNames
) / sizeof(KStandardAction::StandardAction
);
296 for (int i
= 0; i
< count
; ++i
) {
297 QAction
* action
= mainWindow
->actionCollection()->action(KStandardAction::stdName(actionNames
[i
]));
299 popup
->addAction(action
);
302 popup
->addSeparator();
305 QAction
* renameAction
= mainWindow
->actionCollection()->action("rename");
306 popup
->addAction(renameAction
);
308 // insert 'Move to Trash' and (optionally) 'Delete'
309 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
310 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
311 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
312 const KUrl
& url
= mainWindow
->activeView()->url();
313 if (url
.isLocalFile()) {
314 QAction
* moveToTrashAction
= mainWindow
->actionCollection()->action("move_to_trash");
315 popup
->addAction(moveToTrashAction
);
318 showDeleteCommand
= true;
321 if (showDeleteCommand
) {
322 QAction
* deleteAction
= mainWindow
->actionCollection()->action("delete");
323 popup
->addAction(deleteAction
);
327 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
328 QVector
<KService::Ptr
>& openWithVector
)
330 // Parts of the following code have been taken
331 // from the class KonqOperations located in
332 // libqonq/konq_operations.h of Konqueror.
333 // (Copyright (C) 2000 David Faure <faure@kde.org>)
335 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
336 // are listed which are registered to open the item. As last entry "Other..." will be
337 // attached which allows to select a custom application. If no applications are registered
338 // no sub menu is created at all, only "Open With..." will be offered.
339 const KFileItemList list
= m_dolphinView
->selectedItems();
341 bool insertOpenWithItems
= true;
342 const QString
contextMimeType(m_fileInfo
->mimetype());
344 QListIterator
<KFileItem
*> mimeIt(list
);
345 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
346 KFileItem
* item
= mimeIt
.next();
347 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
350 QList
<QAction
*> openWithActions
;
351 if (insertOpenWithItems
) {
352 // fill the 'Open with' sub menu with application types
353 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
354 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
356 "Type == 'Application'");
357 if (offers
.count() > 0) {
358 KService::List::Iterator it
;
359 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
360 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
361 // The offer list from the KTrader returns duplicate
362 // application entries. Although this seems to be a configuration
363 // problem outside the scope of Dolphin, duplicated entries just
364 // will be skipped here.
365 const QString
appName((*it
)->name());
366 if (!containsEntry(openWithMenu
, appName
)) {
367 const KIcon
icon((*it
)->icon());
368 QAction
*action
= openWithMenu
->addAction(icon
, appName
);
369 openWithVector
.append(*it
);
370 openWithActions
<< action
;
374 openWithMenu
->addSeparator();
375 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
377 openWithActions
<< action
;
378 popup
->addSeparator();
379 popup
->addMenu(openWithMenu
);
382 // No applications are registered, hence just offer
383 // a "Open With..." item instead of a sub menu containing
385 QAction
* action
= popup
->addAction(i18n("Open With..."));
386 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 popup
->addSeparator();
393 QAction
* action
= popup
->addAction(i18n("Open With..."));
394 action
->setEnabled(false);
397 return openWithActions
;
400 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
401 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
403 // Parts of the following code have been taken
404 // from the class KonqOperations located in
405 // libqonq/konq_operations.h of Konqueror.
406 // (Copyright (C) 2000 David Faure <faure@kde.org>)
408 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
410 QList
<QAction
*> serviceActions
;
412 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
415 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
418 filters
<< "*.desktop";
419 dir
.setNameFilters(filters
);
420 QStringList entries
= dir
.entryList(QDir::Files
);
422 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
423 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
424 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
425 //const QStringList types = cfg.readListEntry("ServiceTypes");
427 types
= cfg
.readEntry("ServiceTypes", types
);
428 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
429 // check whether the mime type is equal or whether the
430 // mimegroup (e. g. image/*) is supported
433 if ((*it
) == "all/allfiles") {
434 // The service type is valid for all files, but not for directories.
435 // Check whether the selected items only consist of files...
436 const KFileItemList list
= m_dolphinView
->selectedItems();
438 QListIterator
<KFileItem
*> mimeIt(list
);
440 while (insert
&& mimeIt
.hasNext()) {
441 KFileItem
* item
= mimeIt
.next();
442 insert
= !item
->isDir();
447 // Check whether the MIME types of all selected files match
448 // to the mimetype of the service action. As soon as one MIME
449 // type does not match, no service menu is shown at all.
450 const KFileItemList list
= m_dolphinView
->selectedItems();
452 QListIterator
<KFileItem
*> mimeIt(list
);
454 while (insert
&& mimeIt
.hasNext()) {
455 KFileItem
* item
= mimeIt
.next();
456 const QString
mimeType(item
->mimetype());
457 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
459 insert
= (*it
== mimeType
) ||
460 ((*it
).right(1) == "*") &&
461 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
468 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
469 if (!submenuName
.isEmpty()) {
470 menu
= new KMenu(submenuName
);
471 actionsMenu
->addMenu(menu
);
474 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
475 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
477 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
478 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
479 KDEDesktopMimeType::Service service
= (*serviceIt
);
480 if (!service
.m_strIcon
.isEmpty()) {
481 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
483 serviceActions
<< action
;
486 QAction
*action
= menu
->addAction(service
.m_strName
);
487 serviceActions
<< action
;
489 actionsVector
.append(service
);
497 const int itemsCount
= actionsMenu
->actions().count();
498 if (itemsCount
== 0) {
499 // no actions are available at all, hence show the "Actions"
501 actionsMenu
->setEnabled(false);
504 if (itemsCount
== 1) {
505 // Exactly one item is available. Instead of showing a sub menu with
506 // only one item, show the item directly in the root menu.
507 if (menu
== actionsMenu
) {
508 // The item is an action, hence show the action in the root menu.
509 const QList
<QAction
*> actions
= actionsMenu
->actions();
510 Q_ASSERT(actions
.count() == 1);
512 const QString text
= actions
[0]->text();
513 const QIcon icon
= actions
[0]->icon();
515 QAction
* action
= popup
->addAction(text
);
516 serviceActions
.clear();
517 serviceActions
<< action
;
520 QAction
* action
= popup
->addAction(icon
, text
);
521 serviceActions
.clear();
522 serviceActions
<< action
;
526 // The item is a sub menu, hence show the sub menu in the root menu.
527 popup
->addMenu(menu
);
529 actionsMenu
->deleteLater();
533 popup
->addMenu(actionsMenu
);
536 return serviceActions
;
539 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
540 const QString
& entryName
) const
544 const QList
<QAction
*> list
= menu
->actions();
545 const uint count
= list
.count();
546 for (uint i
= 0; i
< count
; ++i
) {
547 const QAction
* action
= list
.at(i
);
548 if (action
->text() == entryName
) {