2006-11-21 06:02:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
|
|
|
|
* Cvetoslav Ludmiloff *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
2006-12-08 21:24:08 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
|
2006-11-21 06:02:05 +00:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "dolphincontextmenu.h"
|
|
|
|
|
2007-01-21 21:20:37 +00:00
|
|
|
#include "dolphinmainwindow.h"
|
|
|
|
#include "dolphinsettings.h"
|
|
|
|
#include "dolphinview.h"
|
2007-06-07 21:10:48 +00:00
|
|
|
#include "dolphinviewcontainer.h"
|
2007-01-21 21:20:37 +00:00
|
|
|
|
2006-11-21 06:02:05 +00:00
|
|
|
#include <kactioncollection.h>
|
2007-04-09 09:16:39 +00:00
|
|
|
#include <kfileplacesmodel.h>
|
2007-01-21 21:20:37 +00:00
|
|
|
#include <kdesktopfile.h>
|
2006-11-21 06:02:05 +00:00
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kiconloader.h>
|
|
|
|
#include <kio/netaccess.h>
|
2006-11-22 08:22:45 +00:00
|
|
|
#include <kmenu.h>
|
2007-03-15 21:24:41 +00:00
|
|
|
#include <kmessagebox.h>
|
2007-01-21 21:20:37 +00:00
|
|
|
#include <kmimetypetrader.h>
|
2007-01-25 16:56:52 +00:00
|
|
|
#include <knewmenu.h>
|
2007-03-17 21:10:50 +00:00
|
|
|
#include <konqmimedata.h>
|
2007-03-15 21:24:41 +00:00
|
|
|
#include <konq_operations.h>
|
2007-01-21 21:20:37 +00:00
|
|
|
#include <klocale.h>
|
|
|
|
#include <kpropertiesdialog.h>
|
|
|
|
#include <krun.h>
|
2006-12-19 02:26:06 +00:00
|
|
|
#include <kstandardaction.h>
|
2007-01-21 21:20:37 +00:00
|
|
|
#include <kstandarddirs.h>
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-05-09 22:23:52 +00:00
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
#include <Qt3Support/Q3ValueList>
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
|
|
|
|
KFileItem* fileInfo,
|
2007-03-25 06:44:28 +00:00
|
|
|
const KUrl& baseUrl) :
|
2007-05-06 17:41:49 +00:00
|
|
|
m_mainWindow(parent),
|
|
|
|
m_fileInfo(fileInfo),
|
|
|
|
m_baseUrl(baseUrl),
|
|
|
|
m_context(NoContext)
|
2006-11-21 06:02:05 +00:00
|
|
|
{
|
2007-03-25 06:44:28 +00:00
|
|
|
// The context menu either accesses the URLs of the selected items
|
|
|
|
// or the items itself. To increase the performance both lists are cached.
|
2007-06-07 21:10:48 +00:00
|
|
|
DolphinView* view = m_mainWindow->activeViewContainer()->view();
|
2007-03-25 06:44:28 +00:00
|
|
|
m_selectedUrls = view->selectedUrls();
|
|
|
|
m_selectedItems = view->selectedItems();
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 20:42:26 +00:00
|
|
|
DolphinContextMenu::~DolphinContextMenu()
|
2007-04-09 19:12:54 +00:00
|
|
|
{}
|
2007-03-17 20:42:26 +00:00
|
|
|
|
2006-11-21 06:02:05 +00:00
|
|
|
void DolphinContextMenu::open()
|
|
|
|
{
|
2007-03-15 21:24:41 +00:00
|
|
|
// get the context information
|
2007-03-15 22:42:21 +00:00
|
|
|
if (m_baseUrl.protocol() == "trash") {
|
2007-03-15 21:24:41 +00:00
|
|
|
m_context |= TrashContext;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
2007-03-15 21:24:41 +00:00
|
|
|
|
|
|
|
if (m_fileInfo != 0) {
|
|
|
|
m_context |= ItemContext;
|
|
|
|
// TODO: handle other use cases like devices + desktop files
|
2007-04-09 19:12:54 +00:00
|
|
|
}
|
2007-03-15 21:24:41 +00:00
|
|
|
|
|
|
|
// open the corresponding popup for the context
|
|
|
|
if (m_context & TrashContext) {
|
|
|
|
if (m_context & ItemContext) {
|
|
|
|
openTrashItemContextMenu();
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-03-15 21:24:41 +00:00
|
|
|
openTrashContextMenu();
|
|
|
|
}
|
2007-04-09 19:12:54 +00:00
|
|
|
} else if (m_context & ItemContext) {
|
2006-11-21 06:02:05 +00:00
|
|
|
openItemContextMenu();
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-03-15 21:24:41 +00:00
|
|
|
Q_ASSERT(m_context == NoContext);
|
|
|
|
openViewportContextMenu();
|
|
|
|
}
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-15 21:24:41 +00:00
|
|
|
void DolphinContextMenu::openTrashContextMenu()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_context & TrashContext);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
KMenu* popup = new KMenu(m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
|
2007-04-12 05:56:03 +00:00
|
|
|
QAction* emptyTrashAction = new QAction(KIcon("emptytrash"), i18n("Empty Trash"), popup);
|
2007-03-15 21:24:41 +00:00
|
|
|
KConfig trashConfig("trashrc", KConfig::OnlyLocal);
|
|
|
|
emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
|
|
|
|
popup->addAction(emptyTrashAction);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
|
2007-03-15 21:24:41 +00:00
|
|
|
popup->addAction(propertiesAction);
|
|
|
|
|
|
|
|
if (popup->exec(QCursor::pos()) == emptyTrashAction) {
|
|
|
|
const QString text(i18n("Do you really want to empty the Trash? All items will get deleted."));
|
2007-03-15 22:42:21 +00:00
|
|
|
const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
|
2007-04-09 19:12:54 +00:00
|
|
|
text,
|
|
|
|
QString(),
|
|
|
|
KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
|
|
|
|
) == KMessageBox::Continue;
|
2007-03-15 21:24:41 +00:00
|
|
|
if (del) {
|
2007-03-15 22:42:21 +00:00
|
|
|
KonqOperations::emptyTrash(m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
popup->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinContextMenu::openTrashItemContextMenu()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_context & TrashContext);
|
|
|
|
Q_ASSERT(m_context & ItemContext);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
KMenu* popup = new KMenu(m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* restoreAction = new QAction(i18n("Restore"), m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
popup->addAction(restoreAction);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
|
2007-03-15 21:24:41 +00:00
|
|
|
popup->addAction(deleteAction);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
|
2007-03-15 21:24:41 +00:00
|
|
|
popup->addAction(propertiesAction);
|
|
|
|
|
|
|
|
if (popup->exec(QCursor::pos()) == restoreAction) {
|
2007-03-15 22:42:21 +00:00
|
|
|
KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
popup->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinContextMenu::openItemContextMenu()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_fileInfo != 0);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
KMenu* popup = new KMenu(m_mainWindow);
|
2007-03-17 20:42:26 +00:00
|
|
|
insertDefaultItemActions(popup);
|
2007-03-16 00:24:56 +00:00
|
|
|
|
|
|
|
popup->addSeparator();
|
2007-03-15 21:24:41 +00:00
|
|
|
|
2007-04-04 21:11:21 +00:00
|
|
|
// insert 'Bookmark This Folder' entry if exactly one item is selected
|
2007-03-15 21:24:41 +00:00
|
|
|
QAction* bookmarkAction = 0;
|
2007-03-15 22:42:21 +00:00
|
|
|
if (m_fileInfo->isDir() && (m_selectedUrls.count() == 1)) {
|
2007-04-04 21:11:21 +00:00
|
|
|
bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
|
2007-03-15 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert 'Open With...' sub menu
|
|
|
|
QVector<KService::Ptr> openWithVector;
|
|
|
|
const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
|
|
|
|
|
|
|
|
// Insert 'Actions' sub menu
|
2007-03-26 08:24:06 +00:00
|
|
|
QVector<KDesktopFileActions::Service> actionsVector;
|
2007-03-15 21:24:41 +00:00
|
|
|
const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
|
|
|
|
popup->addSeparator();
|
|
|
|
|
2007-03-17 20:42:26 +00:00
|
|
|
// insert 'Properties...' entry
|
2007-03-25 06:44:28 +00:00
|
|
|
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
|
2007-03-17 20:42:26 +00:00
|
|
|
popup->addAction(propertiesAction);
|
2007-03-15 21:24:41 +00:00
|
|
|
|
|
|
|
QAction* activatedAction = popup->exec(QCursor::pos());
|
|
|
|
|
2007-04-09 19:12:54 +00:00
|
|
|
if ((bookmarkAction != 0) && (activatedAction == bookmarkAction)) {
|
2007-03-15 21:24:41 +00:00
|
|
|
const KUrl selectedUrl(m_fileInfo->url());
|
2007-04-09 09:16:39 +00:00
|
|
|
if (selectedUrl.isValid()) {
|
|
|
|
DolphinSettings::instance().placesModel()->addPlace(selectedUrl.fileName(),
|
2007-04-09 19:12:54 +00:00
|
|
|
selectedUrl);
|
2007-03-15 21:24:41 +00:00
|
|
|
}
|
2007-04-09 19:12:54 +00:00
|
|
|
} else if (serviceActions.contains(activatedAction)) {
|
2007-03-15 21:24:41 +00:00
|
|
|
// one of the 'Actions' items has been selected
|
|
|
|
int id = serviceActions.indexOf(activatedAction);
|
2007-03-26 08:24:06 +00:00
|
|
|
KDesktopFileActions::executeService(m_selectedUrls, actionsVector[id]);
|
2007-04-09 19:12:54 +00:00
|
|
|
} else if (openWithActions.contains(activatedAction)) {
|
2007-03-15 21:24:41 +00:00
|
|
|
// one of the 'Open With' items has been selected
|
|
|
|
if (openWithActions.last() == activatedAction) {
|
|
|
|
// the item 'Other...' has been selected
|
2007-03-15 22:42:21 +00:00
|
|
|
KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-03-15 21:24:41 +00:00
|
|
|
int id = openWithActions.indexOf(activatedAction);
|
|
|
|
KService::Ptr servicePtr = openWithVector[id];
|
2007-03-15 22:42:21 +00:00
|
|
|
KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
|
2007-03-15 21:24:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openWithVector.clear();
|
|
|
|
actionsVector.clear();
|
|
|
|
popup->deleteLater();
|
|
|
|
}
|
|
|
|
|
2006-11-21 06:02:05 +00:00
|
|
|
void DolphinContextMenu::openViewportContextMenu()
|
|
|
|
{
|
2007-03-15 21:24:41 +00:00
|
|
|
Q_ASSERT(m_fileInfo == 0);
|
2007-03-15 22:42:21 +00:00
|
|
|
KMenu* popup = new KMenu(m_mainWindow);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
// setup 'Create New' menu
|
2007-03-15 22:42:21 +00:00
|
|
|
KNewMenu* newMenu = m_mainWindow->newMenu();
|
2007-01-25 16:56:52 +00:00
|
|
|
newMenu->slotCheckUpToDate();
|
2007-03-15 22:42:21 +00:00
|
|
|
newMenu->setPopupFiles(m_baseUrl);
|
2007-01-25 16:56:52 +00:00
|
|
|
popup->addMenu(newMenu->menu());
|
2007-01-21 22:12:49 +00:00
|
|
|
popup->addSeparator();
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
|
2006-12-04 21:51:17 +00:00
|
|
|
popup->addAction(pasteAction);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
// setup 'View Mode' menu
|
2007-01-21 21:20:37 +00:00
|
|
|
KMenu* viewModeMenu = new KMenu(i18n("View Mode"));
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
|
2006-12-04 21:51:17 +00:00
|
|
|
viewModeMenu->addAction(iconsMode);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
|
2006-12-04 21:51:17 +00:00
|
|
|
viewModeMenu->addAction(detailsMode);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-04-16 20:01:19 +00:00
|
|
|
QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
|
|
|
|
viewModeMenu->addAction(columnsMode);
|
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
|
2006-12-04 21:51:17 +00:00
|
|
|
viewModeMenu->addAction(previewsMode);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-01-21 21:20:37 +00:00
|
|
|
popup->addMenu(viewModeMenu);
|
2007-06-12 21:45:22 +00:00
|
|
|
|
|
|
|
QAction* toggleViewsAction = 0;
|
|
|
|
if (m_mainWindow->isSplit()) {
|
|
|
|
toggleViewsAction = popup->addAction(i18n("Toggle Views"));
|
|
|
|
}
|
|
|
|
|
2007-01-21 22:12:49 +00:00
|
|
|
popup->addSeparator();
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-04-04 21:11:21 +00:00
|
|
|
QAction* bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
|
2007-01-21 22:12:49 +00:00
|
|
|
popup->addSeparator();
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-04-04 21:11:21 +00:00
|
|
|
QAction* propertiesAction = popup->addAction(i18n("Properties"));
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-06-12 21:45:22 +00:00
|
|
|
QAction* action = popup->exec(QCursor::pos());
|
|
|
|
if (action == propertiesAction) {
|
2007-06-07 21:10:48 +00:00
|
|
|
const KUrl& url = m_mainWindow->activeViewContainer()->url();
|
2007-05-28 04:32:57 +00:00
|
|
|
KPropertiesDialog dialog(url);
|
|
|
|
dialog.exec();
|
2007-06-12 21:45:22 +00:00
|
|
|
} else if (action == bookmarkAction) {
|
2007-06-07 21:10:48 +00:00
|
|
|
const KUrl& url = m_mainWindow->activeViewContainer()->url();
|
2007-04-09 09:16:39 +00:00
|
|
|
if (url.isValid()) {
|
|
|
|
DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
2007-06-12 21:45:22 +00:00
|
|
|
} else if ((toggleViewsAction != 0) && (action == toggleViewsAction)) {
|
|
|
|
m_mainWindow->toggleViews();
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
popup->deleteLater();
|
|
|
|
}
|
|
|
|
|
2007-03-15 21:24:41 +00:00
|
|
|
void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
|
2006-11-21 06:02:05 +00:00
|
|
|
{
|
2007-03-15 21:24:41 +00:00
|
|
|
Q_ASSERT(popup != 0);
|
2007-03-17 20:42:26 +00:00
|
|
|
const KActionCollection* collection = m_mainWindow->actionCollection();
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
// insert 'Cut', 'Copy' and 'Paste'
|
2007-03-25 06:44:28 +00:00
|
|
|
QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut));
|
|
|
|
QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy));
|
|
|
|
QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste));
|
2007-03-17 20:42:26 +00:00
|
|
|
|
2007-03-16 00:24:56 +00:00
|
|
|
popup->addAction(cutAction);
|
|
|
|
popup->addAction(copyAction);
|
|
|
|
popup->addAction(pasteAction);
|
2007-01-21 22:12:49 +00:00
|
|
|
popup->addSeparator();
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
// insert 'Rename'
|
2007-03-25 06:44:28 +00:00
|
|
|
QAction* renameAction = collection->action("rename");
|
2006-12-04 21:51:17 +00:00
|
|
|
popup->addAction(renameAction);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-02-25 20:51:05 +00:00
|
|
|
// insert 'Move to Trash' and (optionally) 'Delete'
|
2007-02-25 22:42:47 +00:00
|
|
|
const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals);
|
|
|
|
const KConfigGroup kdeConfig(globalConfig, "KDE");
|
|
|
|
bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
|
2007-06-07 21:10:48 +00:00
|
|
|
const KUrl& url = m_mainWindow->activeViewContainer()->url();
|
2006-11-21 06:02:05 +00:00
|
|
|
if (url.isLocalFile()) {
|
2007-03-25 06:44:28 +00:00
|
|
|
QAction* moveToTrashAction = collection->action("move_to_trash");
|
2006-12-04 21:51:17 +00:00
|
|
|
popup->addAction(moveToTrashAction);
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-02-25 20:51:05 +00:00
|
|
|
showDeleteCommand = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showDeleteCommand) {
|
2007-03-25 06:44:28 +00:00
|
|
|
QAction* deleteAction = collection->action("delete");
|
2006-12-04 21:51:17 +00:00
|
|
|
popup->addAction(deleteAction);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
|
2007-04-09 19:12:54 +00:00
|
|
|
QVector<KService::Ptr>& openWithVector)
|
2006-11-21 06:02:05 +00:00
|
|
|
{
|
2007-01-25 16:56:52 +00:00
|
|
|
// Parts of the following code have been taken
|
|
|
|
// from the class KonqOperations located in
|
|
|
|
// libqonq/konq_operations.h of Konqueror.
|
|
|
|
// (Copyright (C) 2000 David Faure <faure@kde.org>)
|
|
|
|
|
2006-11-21 06:02:05 +00:00
|
|
|
// Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
|
|
|
|
// are listed which are registered to open the item. As last entry "Other..." will be
|
|
|
|
// attached which allows to select a custom application. If no applications are registered
|
|
|
|
// no sub menu is created at all, only "Open With..." will be offered.
|
|
|
|
bool insertOpenWithItems = true;
|
2007-01-21 22:12:49 +00:00
|
|
|
const QString contextMimeType(m_fileInfo->mimetype());
|
2007-01-21 21:20:37 +00:00
|
|
|
|
2007-03-15 22:42:21 +00:00
|
|
|
QListIterator<KFileItem*> mimeIt(m_selectedItems);
|
2006-11-22 11:34:19 +00:00
|
|
|
while (insertOpenWithItems && mimeIt.hasNext()) {
|
|
|
|
KFileItem* item = mimeIt.next();
|
2006-11-21 06:02:05 +00:00
|
|
|
insertOpenWithItems = (contextMimeType == item->mimetype());
|
|
|
|
}
|
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
QList<QAction*> openWithActions;
|
2006-11-21 06:02:05 +00:00
|
|
|
if (insertOpenWithItems) {
|
|
|
|
// fill the 'Open with' sub menu with application types
|
2006-11-22 11:34:19 +00:00
|
|
|
const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
|
|
|
|
KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
|
2007-04-09 19:12:54 +00:00
|
|
|
"Application",
|
|
|
|
"Type == 'Application'");
|
2006-11-21 06:02:05 +00:00
|
|
|
if (offers.count() > 0) {
|
2006-11-22 11:34:19 +00:00
|
|
|
KService::List::Iterator it;
|
2007-01-21 21:20:37 +00:00
|
|
|
KMenu* openWithMenu = new KMenu(i18n("Open With"));
|
2007-04-09 19:12:54 +00:00
|
|
|
for (it = offers.begin(); it != offers.end(); ++it) {
|
2006-11-21 06:02:05 +00:00
|
|
|
// The offer list from the KTrader returns duplicate
|
|
|
|
// application entries. Although this seems to be a configuration
|
|
|
|
// problem outside the scope of Dolphin, duplicated entries just
|
|
|
|
// will be skipped here.
|
|
|
|
const QString appName((*it)->name());
|
|
|
|
if (!containsEntry(openWithMenu, appName)) {
|
2007-01-03 20:24:55 +00:00
|
|
|
const KIcon icon((*it)->icon());
|
2007-03-16 00:24:56 +00:00
|
|
|
QAction* action = openWithMenu->addAction(icon, appName);
|
2006-11-21 06:02:05 +00:00
|
|
|
openWithVector.append(*it);
|
2006-11-22 11:34:19 +00:00
|
|
|
openWithActions << action;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 22:12:49 +00:00
|
|
|
openWithMenu->addSeparator();
|
2007-01-21 21:20:37 +00:00
|
|
|
QAction* action = openWithMenu->addAction(i18n("&Other..."));
|
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
openWithActions << action;
|
2007-01-21 21:20:37 +00:00
|
|
|
popup->addMenu(openWithMenu);
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2006-11-21 06:02:05 +00:00
|
|
|
// No applications are registered, hence just offer
|
|
|
|
// a "Open With..." item instead of a sub menu containing
|
|
|
|
// only one entry.
|
2007-01-21 21:20:37 +00:00
|
|
|
QAction* action = popup->addAction(i18n("Open With..."));
|
2006-11-22 11:34:19 +00:00
|
|
|
openWithActions << action;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2006-11-21 06:02:05 +00:00
|
|
|
// At least one of the selected items has a different MIME type. In this case
|
|
|
|
// just show a disabled "Open With..." entry.
|
2007-01-21 21:20:37 +00:00
|
|
|
QAction* action = popup->addAction(i18n("Open With..."));
|
2006-11-22 11:34:19 +00:00
|
|
|
action->setEnabled(false);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
return openWithActions;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
|
2007-04-09 19:12:54 +00:00
|
|
|
QVector<KDesktopFileActions::Service>& actionsVector)
|
2006-11-21 06:02:05 +00:00
|
|
|
{
|
2007-01-25 16:56:52 +00:00
|
|
|
// Parts of the following code have been taken
|
|
|
|
// from the class KonqOperations located in
|
|
|
|
// libqonq/konq_operations.h of Konqueror.
|
|
|
|
// (Copyright (C) 2000 David Faure <faure@kde.org>)
|
|
|
|
|
2007-01-21 21:20:37 +00:00
|
|
|
KMenu* actionsMenu = new KMenu(i18n("Actions"));
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2006-11-22 11:34:19 +00:00
|
|
|
QList<QAction*> serviceActions;
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
|
|
|
|
|
2006-11-22 08:12:45 +00:00
|
|
|
KMenu* menu = 0;
|
2006-11-21 06:02:05 +00:00
|
|
|
for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
|
|
|
|
QDir dir(*dirIt);
|
2007-01-21 21:20:37 +00:00
|
|
|
QStringList filters;
|
|
|
|
filters << "*.desktop";
|
|
|
|
dir.setNameFilters(filters);
|
|
|
|
QStringList entries = dir.entryList(QDir::Files);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
|
2007-04-09 19:12:54 +00:00
|
|
|
KConfigGroup cfg(KSharedConfig::openConfig(*dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry");
|
2006-11-21 06:02:05 +00:00
|
|
|
if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
|
2007-01-21 21:20:37 +00:00
|
|
|
//const QStringList types = cfg.readListEntry("ServiceTypes");
|
|
|
|
QStringList types;
|
|
|
|
types = cfg.readEntry("ServiceTypes", types);
|
2006-11-21 06:02:05 +00:00
|
|
|
for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
|
|
|
|
// check whether the mime type is equal or whether the
|
|
|
|
// mimegroup (e. g. image/*) is supported
|
|
|
|
|
|
|
|
bool insert = false;
|
|
|
|
if ((*it) == "all/allfiles") {
|
|
|
|
// The service type is valid for all files, but not for directories.
|
|
|
|
// Check whether the selected items only consist of files...
|
2007-03-15 22:42:21 +00:00
|
|
|
QListIterator<KFileItem*> mimeIt(m_selectedItems);
|
2006-11-21 06:02:05 +00:00
|
|
|
insert = true;
|
2006-11-22 11:34:19 +00:00
|
|
|
while (insert && mimeIt.hasNext()) {
|
|
|
|
KFileItem* item = mimeIt.next();
|
2006-11-21 06:02:05 +00:00
|
|
|
insert = !item->isDir();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!insert) {
|
|
|
|
// Check whether the MIME types of all selected files match
|
|
|
|
// to the mimetype of the service action. As soon as one MIME
|
|
|
|
// type does not match, no service menu is shown at all.
|
2007-03-15 22:42:21 +00:00
|
|
|
QListIterator<KFileItem*> mimeIt(m_selectedItems);
|
2006-11-21 06:02:05 +00:00
|
|
|
insert = true;
|
2006-11-22 11:34:19 +00:00
|
|
|
while (insert && mimeIt.hasNext()) {
|
|
|
|
KFileItem* item = mimeIt.next();
|
|
|
|
const QString mimeType(item->mimetype());
|
2006-12-04 19:51:53 +00:00
|
|
|
const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
|
2006-11-21 06:02:05 +00:00
|
|
|
|
|
|
|
insert = (*it == mimeType) ||
|
|
|
|
((*it).right(1) == "*") &&
|
2006-12-04 19:51:53 +00:00
|
|
|
((*it).left((*it).indexOf('/')) == mimeGroup);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insert) {
|
|
|
|
menu = actionsMenu;
|
|
|
|
|
2007-04-09 19:12:54 +00:00
|
|
|
const QString submenuName = cfg.readEntry("X-KDE-Submenu");
|
2006-11-21 06:02:05 +00:00
|
|
|
if (!submenuName.isEmpty()) {
|
2007-01-21 21:20:37 +00:00
|
|
|
menu = new KMenu(submenuName);
|
|
|
|
actionsMenu->addMenu(menu);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-26 08:24:06 +00:00
|
|
|
Q3ValueList<KDesktopFileActions::Service> userServices =
|
|
|
|
KDesktopFileActions::userDefinedServices(*dirIt + *entryIt, true);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-03-26 08:24:06 +00:00
|
|
|
Q3ValueList<KDesktopFileActions::Service>::Iterator serviceIt;
|
2006-11-21 06:02:05 +00:00
|
|
|
for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
|
2007-03-26 08:24:06 +00:00
|
|
|
KDesktopFileActions::Service service = (*serviceIt);
|
2006-11-21 06:02:05 +00:00
|
|
|
if (!service.m_strIcon.isEmpty()) {
|
2007-03-27 19:44:43 +00:00
|
|
|
QAction* action = menu->addAction(KIcon(service.m_strIcon),
|
2006-11-22 11:34:19 +00:00
|
|
|
service.m_strName);
|
|
|
|
serviceActions << action;
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2006-11-22 11:34:19 +00:00
|
|
|
QAction *action = menu->addAction(service.m_strName);
|
|
|
|
serviceActions << action;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
actionsVector.append(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 21:20:37 +00:00
|
|
|
const int itemsCount = actionsMenu->actions().count();
|
2006-11-21 06:02:05 +00:00
|
|
|
if (itemsCount == 0) {
|
|
|
|
// no actions are available at all, hence show the "Actions"
|
|
|
|
// submenu disabled
|
|
|
|
actionsMenu->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemsCount == 1) {
|
|
|
|
// Exactly one item is available. Instead of showing a sub menu with
|
|
|
|
// only one item, show the item directly in the root menu.
|
|
|
|
if (menu == actionsMenu) {
|
|
|
|
// The item is an action, hence show the action in the root menu.
|
2007-01-21 21:20:37 +00:00
|
|
|
const QList<QAction*> actions = actionsMenu->actions();
|
2007-03-15 21:24:41 +00:00
|
|
|
Q_ASSERT(actions.count() == 1);
|
2007-01-21 21:20:37 +00:00
|
|
|
|
|
|
|
const QString text = actions[0]->text();
|
|
|
|
const QIcon icon = actions[0]->icon();
|
|
|
|
if (icon.isNull()) {
|
|
|
|
QAction* action = popup->addAction(text);
|
2006-11-22 11:34:19 +00:00
|
|
|
serviceActions.clear();
|
|
|
|
serviceActions << action;
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-01-21 21:20:37 +00:00
|
|
|
QAction* action = popup->addAction(icon, text);
|
2006-11-22 11:34:19 +00:00
|
|
|
serviceActions.clear();
|
|
|
|
serviceActions << action;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2006-11-21 06:02:05 +00:00
|
|
|
// The item is a sub menu, hence show the sub menu in the root menu.
|
2007-01-21 21:20:37 +00:00
|
|
|
popup->addMenu(menu);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
actionsMenu->deleteLater();
|
|
|
|
actionsMenu = 0;
|
2007-04-09 19:12:54 +00:00
|
|
|
} else {
|
2007-01-21 21:20:37 +00:00
|
|
|
popup->addMenu(actionsMenu);
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
2006-11-22 11:34:19 +00:00
|
|
|
|
|
|
|
return serviceActions;
|
2006-11-21 06:02:05 +00:00
|
|
|
}
|
|
|
|
|
2006-11-22 08:12:45 +00:00
|
|
|
bool DolphinContextMenu::containsEntry(const KMenu* menu,
|
2006-11-21 06:02:05 +00:00
|
|
|
const QString& entryName) const
|
|
|
|
{
|
2007-03-15 21:24:41 +00:00
|
|
|
Q_ASSERT(menu != 0);
|
2006-11-21 06:02:05 +00:00
|
|
|
|
2007-01-21 21:20:37 +00:00
|
|
|
const QList<QAction*> list = menu->actions();
|
|
|
|
const uint count = list.count();
|
2006-11-21 06:02:05 +00:00
|
|
|
for (uint i = 0; i < count; ++i) {
|
2007-01-21 21:20:37 +00:00
|
|
|
const QAction* action = list.at(i);
|
|
|
|
if (action->text() == entryName) {
|
2006-11-21 06:02:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-17 20:42:26 +00:00
|
|
|
|
|
|
|
#include "dolphincontextmenu.moc"
|