]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
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(DolphinMainWindow
* parent
,
58 if (viewType
== ItemsView
) {
59 // The context menu either accesses the URLs of the selected items
60 // or the items itself. To increase the performance both lists are cached.
61 DolphinView
* view
= m_mainWindow
->activeView();
62 m_selectedUrls
= view
->selectedUrls();
63 m_selectedItems
= view
->selectedItems();
65 else if (fileInfo
!= 0) {
66 m_selectedUrls
.append(fileInfo
->url());
67 m_selectedItems
.append(fileInfo
);
71 DolphinContextMenu::~DolphinContextMenu()
75 void DolphinContextMenu::open()
77 // get the context information
78 if (m_baseUrl
.protocol() == "trash") {
79 m_context
|= TrashContext
;
82 if (m_fileInfo
!= 0) {
83 m_context
|= ItemContext
;
84 // TODO: handle other use cases like devices + desktop files
87 // open the corresponding popup for the context
88 if (m_context
& TrashContext
) {
89 if (m_context
& ItemContext
) {
90 openTrashItemContextMenu();
93 openTrashContextMenu();
96 else if (m_context
& ItemContext
) {
97 openItemContextMenu();
100 Q_ASSERT(m_context
== NoContext
);
101 openViewportContextMenu();
105 void DolphinContextMenu::cut()
110 void DolphinContextMenu::copy()
115 void DolphinContextMenu::paste()
120 void DolphinContextMenu::rename()
125 void DolphinContextMenu::moveToTrash()
130 void DolphinContextMenu::deleteItem()
135 void DolphinContextMenu::showProperties()
137 new KPropertiesDialog(m_fileInfo
->url());
140 void DolphinContextMenu::openTrashContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
144 KMenu
* popup
= new KMenu(m_mainWindow
);
146 QAction
* emptyTrashAction
= new QAction(KIcon("user-trash"), i18n("Emtpy Trash"), popup
);
147 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
148 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
149 popup
->addAction(emptyTrashAction
);
151 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
152 popup
->addAction(propertiesAction
);
154 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
155 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
156 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
159 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
160 ) == KMessageBox::Continue
;
162 KonqOperations::emptyTrash(m_mainWindow
);
166 popup
->deleteLater();
169 void DolphinContextMenu::openTrashItemContextMenu()
171 Q_ASSERT(m_context
& TrashContext
);
172 Q_ASSERT(m_context
& ItemContext
);
174 KMenu
* popup
= new KMenu(m_mainWindow
);
176 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
177 popup
->addAction(restoreAction
);
179 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
180 popup
->addAction(deleteAction
);
182 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
183 popup
->addAction(propertiesAction
);
185 if (popup
->exec(QCursor::pos()) == restoreAction
) {
186 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
189 popup
->deleteLater();
192 void DolphinContextMenu::openItemContextMenu()
194 Q_ASSERT(m_fileInfo
!= 0);
196 KMenu
* popup
= new KMenu(m_mainWindow
);
197 insertDefaultItemActions(popup
);
199 popup
->addSeparator();
201 // insert 'Bookmark this folder' entry if exactly one item is selected
202 QAction
* bookmarkAction
= 0;
203 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
204 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark folder"));
207 // Insert 'Open With...' sub menu
208 QVector
<KService::Ptr
> openWithVector
;
209 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
211 // Insert 'Actions' sub menu
212 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
213 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
214 popup
->addSeparator();
216 // insert 'Properties...' entry
217 QAction
* propertiesAction
= 0;
218 if (m_viewType
== SidebarView
) {
219 propertiesAction
= new QAction(i18n("Properties..."), this);
220 connect(this, SIGNAL(triggered()), this, SLOT(showProperties()));
223 propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
225 popup
->addAction(propertiesAction
);
227 QAction
* activatedAction
= popup
->exec(QCursor::pos());
229 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
230 const KUrl
selectedUrl(m_fileInfo
->url());
231 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
232 selectedUrl
.fileName(),
235 if (!bookmark
.isNull()) {
236 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
237 KBookmarkGroup root
= manager
->root();
238 root
.addBookmark(manager
, bookmark
);
239 manager
->emitChanged(root
);
242 else if (serviceActions
.contains(activatedAction
)) {
243 // one of the 'Actions' items has been selected
244 int id
= serviceActions
.indexOf(activatedAction
);
245 KDEDesktopMimeType::executeService(m_selectedUrls
, actionsVector
[id
]);
247 else if (openWithActions
.contains(activatedAction
)) {
248 // one of the 'Open With' items has been selected
249 if (openWithActions
.last() == activatedAction
) {
250 // the item 'Other...' has been selected
251 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
254 int id
= openWithActions
.indexOf(activatedAction
);
255 KService::Ptr servicePtr
= openWithVector
[id
];
256 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
260 openWithVector
.clear();
261 actionsVector
.clear();
262 popup
->deleteLater();
265 void DolphinContextMenu::openViewportContextMenu()
267 Q_ASSERT(m_fileInfo
== 0);
268 KMenu
* popup
= new KMenu(m_mainWindow
);
270 // setup 'Create New' menu
271 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
272 newMenu
->slotCheckUpToDate();
273 newMenu
->setPopupFiles(m_baseUrl
);
274 popup
->addMenu(newMenu
->menu());
275 popup
->addSeparator();
277 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
278 popup
->addAction(pasteAction
);
280 // setup 'View Mode' menu
281 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
283 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
284 viewModeMenu
->addAction(iconsMode
);
286 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
287 viewModeMenu
->addAction(detailsMode
);
289 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
290 viewModeMenu
->addAction(previewsMode
);
292 popup
->addMenu(viewModeMenu
);
293 popup
->addSeparator();
295 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
296 popup
->addSeparator();
298 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
300 QAction
* activatedAction
= popup
->exec(QCursor::pos());
301 if (activatedAction
== propertiesAction
) {
302 new KPropertiesDialog(m_mainWindow
->activeView()->url());
304 else if (activatedAction
== bookmarkAction
) {
305 const KUrl
& url
= m_mainWindow
->activeView()->url();
306 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
310 if (!bookmark
.isNull()) {
311 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
312 KBookmarkGroup root
= manager
->root();
313 root
.addBookmark(manager
, bookmark
);
314 manager
->emitChanged(root
);
318 popup
->deleteLater();
321 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
323 Q_ASSERT(popup
!= 0);
324 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
325 const bool insertSidebarActions
= (m_viewType
== SidebarView
);
327 // insert 'Cut', 'Copy' and 'Paste'
328 QAction
* cutAction
= 0;
329 QAction
* copyAction
= 0;
330 QAction
* pasteAction
= 0;
331 if (insertSidebarActions
) {
332 cutAction
= new QAction(KIcon("edit-cut"), i18n("Cut"), this);
333 connect(cutAction
, SIGNAL(triggered()), this, SLOT(cut()));
335 copyAction
= new QAction(KIcon("edit-copy"), i18n("Copy"), this);
336 connect(copyAction
, SIGNAL(triggered()), this, SLOT(copy()));
338 const QAction
* menuPasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
339 pasteAction
= new QAction(KIcon("edit-paste"), menuPasteAction
->text(), this);
340 pasteAction
->setEnabled(menuPasteAction
->isEnabled());
341 connect(pasteAction
, SIGNAL(triggered()), this, SLOT(paste()));
344 cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
345 copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
346 pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
349 popup
->addAction(cutAction
);
350 popup
->addAction(copyAction
);
351 popup
->addAction(pasteAction
);
352 popup
->addSeparator();
355 QAction
* renameAction
= 0;
356 if (insertSidebarActions
) {
357 renameAction
= new QAction(i18n("Rename"), this);
358 connect(renameAction
, SIGNAL(triggered()), this, SLOT(paste()));
361 collection
->action("rename");
363 popup
->addAction(renameAction
);
365 // insert 'Move to Trash' and (optionally) 'Delete'
366 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
367 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
368 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
369 const KUrl
& url
= insertSidebarActions
? m_fileInfo
->url():
370 m_mainWindow
->activeView()->url();
371 if (url
.isLocalFile()) {
372 QAction
* moveToTrashAction
= 0;
373 if (insertSidebarActions
) {
374 moveToTrashAction
= new QAction(KIcon("edit-trash"), i18n("Move To Trash"), this);
375 connect(moveToTrashAction
, SIGNAL(triggered()), this, SLOT(moveToTrash()));
378 moveToTrashAction
= collection
->action("move_to_trash");
380 popup
->addAction(moveToTrashAction
);
383 showDeleteCommand
= true;
386 if (showDeleteCommand
) {
387 QAction
* deleteAction
= 0;
388 if (insertSidebarActions
) {
389 deleteAction
= new QAction(KIcon("edit-delete"), i18n("Delete"), this);
390 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(deleteItem()));
393 deleteAction
= collection
->action("delete");
395 popup
->addAction(deleteAction
);
399 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
400 QVector
<KService::Ptr
>& openWithVector
)
402 // Parts of the following code have been taken
403 // from the class KonqOperations located in
404 // libqonq/konq_operations.h of Konqueror.
405 // (Copyright (C) 2000 David Faure <faure@kde.org>)
407 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
408 // are listed which are registered to open the item. As last entry "Other..." will be
409 // attached which allows to select a custom application. If no applications are registered
410 // no sub menu is created at all, only "Open With..." will be offered.
411 bool insertOpenWithItems
= true;
412 const QString
contextMimeType(m_fileInfo
->mimetype());
414 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
415 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
416 KFileItem
* item
= mimeIt
.next();
417 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
420 QList
<QAction
*> openWithActions
;
421 if (insertOpenWithItems
) {
422 // fill the 'Open with' sub menu with application types
423 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
424 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
426 "Type == 'Application'");
427 if (offers
.count() > 0) {
428 KService::List::Iterator it
;
429 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
430 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
431 // The offer list from the KTrader returns duplicate
432 // application entries. Although this seems to be a configuration
433 // problem outside the scope of Dolphin, duplicated entries just
434 // will be skipped here.
435 const QString
appName((*it
)->name());
436 if (!containsEntry(openWithMenu
, appName
)) {
437 const KIcon
icon((*it
)->icon());
438 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
439 openWithVector
.append(*it
);
440 openWithActions
<< action
;
444 openWithMenu
->addSeparator();
445 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
447 openWithActions
<< action
;
448 popup
->addMenu(openWithMenu
);
451 // No applications are registered, hence just offer
452 // a "Open With..." item instead of a sub menu containing
454 QAction
* action
= popup
->addAction(i18n("Open With..."));
455 openWithActions
<< action
;
459 // At least one of the selected items has a different MIME type. In this case
460 // just show a disabled "Open With..." entry.
461 QAction
* action
= popup
->addAction(i18n("Open With..."));
462 action
->setEnabled(false);
465 return openWithActions
;
468 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
469 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
471 // Parts of the following code have been taken
472 // from the class KonqOperations located in
473 // libqonq/konq_operations.h of Konqueror.
474 // (Copyright (C) 2000 David Faure <faure@kde.org>)
476 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
478 QList
<QAction
*> serviceActions
;
480 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
483 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
486 filters
<< "*.desktop";
487 dir
.setNameFilters(filters
);
488 QStringList entries
= dir
.entryList(QDir::Files
);
490 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
491 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
492 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
493 //const QStringList types = cfg.readListEntry("ServiceTypes");
495 types
= cfg
.readEntry("ServiceTypes", types
);
496 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
497 // check whether the mime type is equal or whether the
498 // mimegroup (e. g. image/*) is supported
501 if ((*it
) == "all/allfiles") {
502 // The service type is valid for all files, but not for directories.
503 // Check whether the selected items only consist of files...
504 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
506 while (insert
&& mimeIt
.hasNext()) {
507 KFileItem
* item
= mimeIt
.next();
508 insert
= !item
->isDir();
513 // Check whether the MIME types of all selected files match
514 // to the mimetype of the service action. As soon as one MIME
515 // type does not match, no service menu is shown at all.
516 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
518 while (insert
&& mimeIt
.hasNext()) {
519 KFileItem
* item
= mimeIt
.next();
520 const QString
mimeType(item
->mimetype());
521 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
523 insert
= (*it
== mimeType
) ||
524 ((*it
).right(1) == "*") &&
525 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
532 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
533 if (!submenuName
.isEmpty()) {
534 menu
= new KMenu(submenuName
);
535 actionsMenu
->addMenu(menu
);
538 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
539 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
541 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
542 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
543 KDEDesktopMimeType::Service service
= (*serviceIt
);
544 if (!service
.m_strIcon
.isEmpty()) {
545 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
547 serviceActions
<< action
;
550 QAction
*action
= menu
->addAction(service
.m_strName
);
551 serviceActions
<< action
;
553 actionsVector
.append(service
);
561 const int itemsCount
= actionsMenu
->actions().count();
562 if (itemsCount
== 0) {
563 // no actions are available at all, hence show the "Actions"
565 actionsMenu
->setEnabled(false);
568 if (itemsCount
== 1) {
569 // Exactly one item is available. Instead of showing a sub menu with
570 // only one item, show the item directly in the root menu.
571 if (menu
== actionsMenu
) {
572 // The item is an action, hence show the action in the root menu.
573 const QList
<QAction
*> actions
= actionsMenu
->actions();
574 Q_ASSERT(actions
.count() == 1);
576 const QString text
= actions
[0]->text();
577 const QIcon icon
= actions
[0]->icon();
579 QAction
* action
= popup
->addAction(text
);
580 serviceActions
.clear();
581 serviceActions
<< action
;
584 QAction
* action
= popup
->addAction(icon
, text
);
585 serviceActions
.clear();
586 serviceActions
<< action
;
590 // The item is a sub menu, hence show the sub menu in the root menu.
591 popup
->addMenu(menu
);
593 actionsMenu
->deleteLater();
597 popup
->addMenu(actionsMenu
);
600 return serviceActions
;
603 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
604 const QString
& entryName
) const
608 const QList
<QAction
*> list
= menu
->actions();
609 const uint count
= list
.count();
610 for (uint i
= 0; i
< count
; ++i
) {
611 const QAction
* action
= list
.at(i
);
612 if (action
->text() == entryName
) {
620 #include "dolphincontextmenu.moc"