-/***************************************************************************
- * 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., *
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Cvetoslav Ludmiloff
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
#include "dolphincontextmenu.h"
-#include <kactioncollection.h>
-#include <kbookmarkmanager.h>
-#include <kbookmark.h>
-#include <kmimetypetrader.h>
-#include <klocale.h>
-#include <krun.h>
-#include <qdir.h>
-//Added by qt3to4:
-#include <Q3ValueList>
-#include <kglobal.h>
-#include <kstandarddirs.h>
-#include <kiconloader.h>
-#include <kaction.h>
-#include <kpropertiesdialog.h>
-#include <kdesktopfile.h>
-#include <assert.h>
-#include <kio/netaccess.h>
-#include <kmenu.h>
-#include <kstdaction.h>
-
+#include "dolphin_contextmenusettings.h"
#include "dolphinmainwindow.h"
-#include "dolphinview.h"
-#include "editbookmarkdialog.h"
-#include "dolphinsettings.h"
+#include "dolphinnewfilemenu.h"
+#include "dolphinplacesmodelsingleton.h"
+#include "dolphinremoveaction.h"
+#include "dolphinviewcontainer.h"
+#include "global.h"
+#include "trash/dolphintrash.h"
+#include "views/dolphinview.h"
+
+#include <KActionCollection>
+#include <KFileItemListProperties>
+#include <KHamburgerMenu>
+#include <KIO/EmptyTrashJob>
+#include <KIO/JobUiDelegate>
+#include <KIO/Paste>
+#include <KIO/RestoreJob>
+#include <KJobWidgets>
+#include <KLocalizedString>
+#include <KNewFileMenu>
+#include <KStandardAction>
+
+#include <QApplication>
+#include <QClipboard>
+#include <QKeyEvent>
+
+DolphinContextMenu::DolphinContextMenu(DolphinMainWindow *parent,
+ const KFileItem &fileInfo,
+ const KFileItemList &selectedItems,
+ const QUrl &baseUrl,
+ KFileItemActions *fileItemActions)
+ : QMenu(parent)
+ , m_mainWindow(parent)
+ , m_fileInfo(fileInfo)
+ , m_baseUrl(baseUrl)
+ , m_baseFileItem(nullptr)
+ , m_selectedItems(selectedItems)
+ , m_selectedItemsProperties(nullptr)
+ , m_context(NoContext)
+ , m_copyToMenu(parent)
+ , m_removeAction(nullptr)
+ , m_fileItemActions(fileItemActions)
+{
+ QApplication::instance()->installEventFilter(this);
+ addAllActions();
+}
-DolphinContextMenu::DolphinContextMenu(DolphinView* parent,
- KFileItem* fileInfo,
- const QPoint& pos) :
- m_dolphinView(parent),
- m_fileInfo(fileInfo),
- m_pos(pos)
+DolphinContextMenu::~DolphinContextMenu()
{
+ delete m_baseFileItem;
+ m_baseFileItem = nullptr;
+ delete m_selectedItemsProperties;
+ m_selectedItemsProperties = nullptr;
}
-void DolphinContextMenu::open()
+void DolphinContextMenu::addAllActions()
{
- if (m_fileInfo == 0) {
- openViewportContextMenu();
+ static_cast<KHamburgerMenu *>(m_mainWindow->actionCollection()->action(QStringLiteral("hamburger_menu")))->addToMenu(this);
+
+ // get the context information
+ const auto scheme = m_baseUrl.scheme();
+ if (scheme == QLatin1String("trash")) {
+ m_context |= TrashContext;
+ } else if (scheme.contains(QLatin1String("search"))) {
+ m_context |= SearchContext;
+ } else if (scheme.contains(QLatin1String("timeline"))) {
+ m_context |= TimelineContext;
+ } else if (scheme == QStringLiteral("recentlyused")) {
+ m_context |= RecentlyUsedContext;
}
- else {
- openItemContextMenu();
+
+ if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
+ m_context |= ItemContext;
+ // TODO: handle other use cases like devices + desktop files
+ }
+
+ // open the corresponding popup for the context
+ if (m_context & TrashContext) {
+ if (m_context & ItemContext) {
+ addTrashItemContextMenu();
+ } else {
+ addTrashContextMenu();
+ }
+ } else if (m_context & ItemContext) {
+ addItemContextMenu();
+ } else {
+ addViewportContextMenu();
}
}
-DolphinContextMenu::~DolphinContextMenu()
+bool DolphinContextMenu::eventFilter(QObject *object, QEvent *event)
{
+ Q_UNUSED(object)
+
+ if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
+ QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
+
+ if (m_removeAction && keyEvent->key() == Qt::Key_Shift) {
+ if (event->type() == QEvent::KeyPress) {
+ m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
+ } else {
+ m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
+ }
+ }
+ }
+
+ return false;
}
-void DolphinContextMenu::openViewportContextMenu()
+void DolphinContextMenu::addTrashContextMenu()
{
- // 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>)
+ Q_ASSERT(m_context & TrashContext);
- assert(m_fileInfo == 0);
+ QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this]() {
+ Trash::empty(m_mainWindow);
+ });
+ emptyTrashAction->setEnabled(!Trash::isEmpty());
- KMenu* popup = new KMenu(m_dolphinView);
- DolphinMainWindow *dolphin = m_dolphinView->mainWindow();
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
+}
- // setup 'Create New' menu
- KMenu* createNewMenu = new KMenu();
+void DolphinContextMenu::addTrashItemContextMenu()
+{
+ Q_ASSERT(m_context & TrashContext);
+ Q_ASSERT(m_context & ItemContext);
+
+ addAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), [this]() {
+ QList<QUrl> selectedUrls;
+ selectedUrls.reserve(m_selectedItems.count());
+ for (const KFileItem &item : std::as_const(m_selectedItems)) {
+ selectedUrls.append(item.url());
+ }
- KAction* createFolderAction = dolphin->actionCollection()->action("create_folder");
- if (createFolderAction != 0) {
- createFolderAction->plug(createNewMenu);
- }
+ KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
+ KJobWidgets::setWindow(job, m_mainWindow);
+ job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+ });
- createNewMenu->insertSeparator();
+ QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
+ addAction(deleteAction);
- KAction* action = 0;
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
+}
- Q3PtrListIterator<KAction> fileGrouptIt(dolphin->fileGroupActions());
- while ((action = fileGrouptIt.current()) != 0) {
- action->plug(createNewMenu);
- ++fileGrouptIt;
+void DolphinContextMenu::addDirectoryItemContextMenu()
+{
+ // insert 'Open in new window' and 'Open in new tab' entries
+ const KFileItemListProperties &selectedItemsProps = selectedItemsProperties();
+ if (ContextMenuSettings::showOpenInNewTab()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
}
-
- // TODO: not used yet. See documentation of Dolphin::linkGroupActions()
- // and Dolphin::linkToDeviceActions() in the header file for details.
- //
- //createNewMenu->insertSeparator();
- //
- //QPtrListIterator<KAction> linkGroupIt(dolphin->linkGroupActions());
- //while ((action = linkGroupIt.current()) != 0) {
- // action->plug(createNewMenu);
- // ++linkGroupIt;
- //}
- //
- //KMenu* linkToDeviceMenu = new KMenu();
- //QPtrListIterator<KAction> linkToDeviceIt(dolphin->linkToDeviceActions());
- //while ((action = linkToDeviceIt.current()) != 0) {
- // action->plug(linkToDeviceMenu);
- // ++linkToDeviceIt;
- //}
- //
- //createNewMenu->insertItem(i18n("Link to Device"), linkToDeviceMenu);
-
- popup->insertItem(SmallIcon("filenew"), i18n("Create New"), createNewMenu);
- popup->insertSeparator();
-
- KAction* pasteAction = dolphin->actionCollection()->action(KStdAction::stdName(KStdAction::Paste));
- pasteAction->plug(popup);
-
- // setup 'View Mode' menu
- KMenu* viewModeMenu = new KMenu();
-
- KAction* iconsMode = dolphin->actionCollection()->action("icons");
- iconsMode->plug(viewModeMenu);
-
- KAction* detailsMode = dolphin->actionCollection()->action("details");
- detailsMode->plug(viewModeMenu);
-
- KAction* previewsMode = dolphin->actionCollection()->action("previews");
- previewsMode->plug(viewModeMenu);
-
- popup->insertItem(i18n("View Mode"), viewModeMenu);
- popup->insertSeparator();
-
- QAction *bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
- popup->insertSeparator();
-
- QAction *propertiesAction = popup->addAction(i18n("Properties..."));
-
- QAction *activatedAction = popup->exec(m_pos);
- if (activatedAction == propertiesAction) {
- new KPropertiesDialog(dolphin->activeView()->url());
+ if (ContextMenuSettings::showOpenInNewWindow()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
}
- else if (activatedAction == bookmarkAction) {
- const KUrl& url = dolphin->activeView()->url();
- KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
- url.fileName(),
- url,
- "bookmark");
- if (!bookmark.isNull()) {
- KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
- KBookmarkGroup root = manager->root();
- root.addBookmark(manager, bookmark);
- manager->emitChanged(root);
- }
+
+ if (ContextMenuSettings::showOpenInSplitView()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_split_view")));
}
- popup->deleteLater();
+ // Insert 'Open With' entries
+ addOpenWithActions();
+
+ // set up 'Create New' menu
+ DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection()->action(QStringLiteral("create_dir")), m_mainWindow);
+ newFileMenu->checkUpToDate();
+ newFileMenu->setWorkingDirectory(m_fileInfo.url());
+ newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
+ connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
+ connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
+
+ QMenu *menu = newFileMenu->menu();
+ menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
+ menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
+ addMenu(menu);
+
+ addSeparator();
}
-void DolphinContextMenu::openItemContextMenu()
+void DolphinContextMenu::addOpenParentFolderActions()
{
- // 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>)
-
- assert(m_fileInfo != 0);
-
- KMenu* popup = new KMenu(m_dolphinView);
- DolphinMainWindow* dolphin = m_dolphinView->mainWindow();
- const KUrl::List urls = m_dolphinView->selectedUrls();
-
- // insert 'Cut', 'Copy' and 'Paste'
- const KStdAction::StdAction actionNames[] = { KStdAction::Cut, KStdAction::Copy, KStdAction::Paste };
- const int count = sizeof(actionNames) / sizeof(KStdAction::StdAction);
- for (int i = 0; i < count; ++i) {
- KAction* action = dolphin->actionCollection()->action(KStdAction::stdName(actionNames[i]));
- if (action != 0) {
- action->plug(popup);
- }
- }
- popup->insertSeparator();
+ addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), [this]() {
+ const QUrl url = m_fileInfo.targetUrl();
+ const QUrl parentUrl = KIO::upUrl(url);
+ m_mainWindow->changeUrl(parentUrl);
+ m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({url});
+ m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(url);
+ });
+
+ addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), [this]() {
+ m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.targetUrl()));
+ });
+
+ addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), [this]() {
+ Dolphin::openNewWindow({m_fileInfo.targetUrl()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select);
+ });
+}
- // insert 'Rename'
- KAction* renameAction = dolphin->actionCollection()->action("rename");
- renameAction->plug(popup);
+void DolphinContextMenu::addItemContextMenu()
+{
+ Q_ASSERT(!m_fileInfo.isNull());
- // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
- const KUrl& url = dolphin->activeView()->url();
- if (url.isLocalFile()) {
- KAction* moveToTrashAction = dolphin->actionCollection()->action("move_to_trash");
- moveToTrashAction->plug(popup);
- }
- else {
- KAction* deleteAction = dolphin->actionCollection()->action("delete");
- deleteAction->plug(popup);
- }
+ const KFileItemListProperties &selectedItemsProps = selectedItemsProperties();
- // insert 'Bookmark this folder...' entry
- // urls is a list of selected items, so insert boolmark menu if
- // urls contains only one item, i.e. no multiple selection made
- QAction *bookmarkAction = 0;
- if (m_fileInfo->isDir() && (urls.count() == 1)) {
- bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
- }
+ m_fileItemActions->setItemListProperties(selectedItemsProps);
- popup->insertSeparator();
+ if (m_selectedItems.count() == 1) {
+ // single files
+ if (m_fileInfo.isDir()) {
+ addDirectoryItemContextMenu();
+ } else if (m_context & TimelineContext || m_context & SearchContext || m_context & RecentlyUsedContext) {
+ addOpenWithActions();
- // Insert 'Open With...' sub menu
- Q3ValueVector<KService::Ptr> openWithVector;
- const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
+ addOpenParentFolderActions();
- // Insert 'Actions' sub menu
- Q3ValueVector<KDEDesktopMimeType::Service> actionsVector;
- const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
+ addSeparator();
+ } else {
+ // Insert 'Open With" entries
+ addOpenWithActions();
+ }
+ if (m_fileInfo.isLink()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
+ addSeparator();
+ }
+ } else {
+ // multiple files
+ bool selectionHasOnlyDirs = true;
+ for (const auto &item : std::as_const(m_selectedItems)) {
+ const QUrl &url = DolphinView::openItemAsFolderUrl(item);
+ if (url.isEmpty()) {
+ selectionHasOnlyDirs = false;
+ break;
+ }
+ }
- // insert 'Properties...' entry
- popup->insertSeparator();
- KAction* propertiesAction = dolphin->actionCollection()->action("properties");
- propertiesAction->plug(popup);
-
- QAction *activatedAction = popup->exec(m_pos);
-
- if (bookmarkAction!=0 && activatedAction == bookmarkAction) {
- const KUrl selectedUrl(m_fileInfo->url());
- KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
- selectedUrl.fileName(),
- selectedUrl,
- "bookmark");
- if (!bookmark.isNull()) {
- KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
- KBookmarkGroup root = manager->root();
- root.addBookmark(manager, bookmark);
- manager->emitChanged(root);
+ if (selectionHasOnlyDirs && ContextMenuSettings::showOpenInNewTab()) {
+ // insert 'Open in new tab' entry
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
}
+ // Insert 'Open With" entries
+ addOpenWithActions();
}
- else if (serviceActions.contains(activatedAction)) {
- // one of the 'Actions' items has been selected
- int id = serviceActions.indexOf(activatedAction);
- KDEDesktopMimeType::executeService(urls, actionsVector[id]);
+
+ insertDefaultItemActions(selectedItemsProps);
+
+ addAdditionalActions(selectedItemsProps);
+
+ // insert 'Copy To' and 'Move To' sub menus
+ if (ContextMenuSettings::showCopyMoveMenu()) {
+ m_copyToMenu.setUrls(m_selectedItems.urlList());
+ m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
+ m_copyToMenu.setAutoErrorHandlingEnabled(true);
+ m_copyToMenu.addActionsTo(this);
}
- else if (openWithActions.contains(activatedAction)) {
- // one of the 'Open With' items has been selected
- if (openWithActions.last()==activatedAction) {
- // the item 'Other...' has been selected
- KRun::displayOpenWithDialog(urls, m_dolphinView);
+
+ if (m_mainWindow->isSplitViewEnabledInCurrentTab()) {
+ if (ContextMenuSettings::showCopyToOtherSplitView()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view")));
}
- else {
- int id = openWithActions.indexOf(activatedAction);
- KService::Ptr servicePtr = openWithVector[id];
- KRun::run(*servicePtr, urls, m_dolphinView);
+
+ if (ContextMenuSettings::showMoveToOtherSplitView()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("move_to_inactive_split_view")));
}
}
- openWithVector.clear();
- actionsVector.clear();
- popup->deleteLater();
+ // insert 'Properties...' entry
+ addSeparator();
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
}
-QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
- Q3ValueVector<KService::Ptr>& openWithVector)
+void DolphinContextMenu::addViewportContextMenu()
{
- // 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.
- const KFileItemList list = m_dolphinView->selectedItems();
-
- bool insertOpenWithItems = true;
- const QString contextMimeType(m_fileInfo->mimetype());
- QListIterator<KFileItem*> mimeIt(list);
- while (insertOpenWithItems && mimeIt.hasNext()) {
- KFileItem* item = mimeIt.next();
- insertOpenWithItems = (contextMimeType == item->mimetype());
+ const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
+ m_fileItemActions->setItemListProperties(baseUrlProperties);
+
+ // Set up and insert 'Create New' menu
+ KNewFileMenu *newFileMenu = m_mainWindow->newFileMenu();
+ newFileMenu->checkUpToDate();
+ newFileMenu->setWorkingDirectory(m_baseUrl);
+ addMenu(newFileMenu->menu());
+
+ // Show "open with" menu items even if the dir is empty, because there are legitimate
+ // use cases for this, such as opening an empty dir in Kate or VSCode or something
+ addOpenWithActions();
+
+ QAction *pasteAction = createPasteAction();
+ if (pasteAction) {
+ addAction(pasteAction);
}
- QList<QAction*> openWithActions;
-
- if (insertOpenWithItems) {
- // fill the 'Open with' sub menu with application types
- const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
- KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
- "Application",
- "Type == 'Application'");
- if (offers.count() > 0) {
- KService::List::Iterator it;
- KMenu* openWithMenu = new KMenu();
- for(it = offers.begin(); it != offers.end(); ++it) {
- // 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)) {
- QAction *action = openWithMenu->addAction((*it)->pixmap(K3Icon::Small),
- appName);
- openWithVector.append(*it);
- openWithActions << action;
- }
- }
+ // Insert 'Add to Places' entry if it's not already in the places panel
+ if (ContextMenuSettings::showAddToPlaces() && !placeExists(m_mainWindow->activeViewContainer()->url())) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
+ }
+ addSeparator();
- openWithMenu->insertSeparator();
- QAction *action = openWithMenu->addAction(i18n("&Other..."));
- openWithActions << action;
- popup->insertItem(i18n("Open With"), openWithMenu);
- }
- else {
- // No applications are registered, hence just offer
- // a "Open With..." item instead of a sub menu containing
- // only one entry.
- QAction *action = popup->addAction(i18n("Open With..."));
- openWithActions << action;
- }
+ // Insert 'Sort By' and 'View Mode'
+ if (ContextMenuSettings::showSortBy()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
}
- else {
- // At least one of the selected items has a different MIME type. In this case
- // just show a disabled "Open With..." entry.
- QAction *action = popup->addAction(i18n("Open With..."));
- action->setEnabled(false);
+ if (ContextMenuSettings::showViewMode()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
}
+ if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) {
+ addSeparator();
+ }
+
+ addAdditionalActions(baseUrlProperties);
+
+ addSeparator();
- return openWithActions;
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
}
-QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
- Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
+void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties &properties)
{
- KMenu* actionsMenu = new KMenu();
-
- QList<QAction*> serviceActions;
-
- QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
-
- KMenu* menu = 0;
- for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
- QDir dir(*dirIt);
- QStringList entries = dir.entryList("*.desktop", QDir::Files);
-
- for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
- KSimpleConfig cfg(*dirIt + *entryIt, true);
- cfg.setDesktopGroup();
- if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
- const QStringList types = cfg.readListEntry("ServiceTypes");
- 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...
- const KFileItemList list = m_dolphinView->selectedItems();
-
- QListIterator<KFileItem*> mimeIt(list);
- insert = true;
- while (insert && mimeIt.hasNext()) {
- KFileItem* item = mimeIt.next();
- 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.
- const KFileItemList list = m_dolphinView->selectedItems();
-
- QListIterator<KFileItem*> mimeIt(list);
- insert = true;
- while (insert && mimeIt.hasNext()) {
- KFileItem* item = mimeIt.next();
- const QString mimeType(item->mimetype());
- const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
-
- insert = (*it == mimeType) ||
- ((*it).right(1) == "*") &&
- ((*it).left((*it).indexOf('/')) == mimeGroup);
- }
- }
-
- if (insert) {
- menu = actionsMenu;
-
- const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
- if (!submenuName.isEmpty()) {
- menu = new KMenu();
- actionsMenu->insertItem(submenuName, menu, submenuID);
- }
-
- Q3ValueList<KDEDesktopMimeType::Service> userServices =
- KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
-
- Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
- for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
- KDEDesktopMimeType::Service service = (*serviceIt);
- if (!service.m_strIcon.isEmpty()) {
- QAction *action = menu->addAction(SmallIcon(service.m_strIcon),
- service.m_strName);
- serviceActions << action;
- }
- else {
- QAction *action = menu->addAction(service.m_strName);
- serviceActions << action;
- }
- actionsVector.append(service);
- }
- }
- }
- }
+ const KActionCollection *collection = m_mainWindow->actionCollection();
+
+ // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
+ addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
+ addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
+ if (ContextMenuSettings::showCopyLocation()) {
+ QAction *copyPathAction = collection->action(QString("copy_location"));
+ copyPathAction->setEnabled(m_selectedItems.size() == 1);
+ addAction(copyPathAction);
+ }
+ QAction *pasteAction = createPasteAction();
+ if (pasteAction) {
+ addAction(pasteAction);
+ }
+
+ // Insert 'Duplicate Here'
+ if (ContextMenuSettings::showDuplicateHere()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
+ }
+
+ // Insert 'Rename'
+ addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
+
+ // Insert 'Add to Places' entry if appropriate
+ if (ContextMenuSettings::showAddToPlaces() && m_selectedItems.count() == 1 && m_fileInfo.isDir() && !placeExists(m_fileInfo.url())) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
+ }
+
+ addSeparator();
+
+ // Insert 'Move to Trash' and/or 'Delete'
+ const bool showDeleteAction = (KSharedConfig::openConfig()->group(QStringLiteral("KDE")).readEntry("ShowDeleteCommand", false) || !properties.isLocal());
+ const bool showMoveToTrashAction = (properties.isLocal() && properties.supportsMoving());
+
+ if (showDeleteAction && showMoveToTrashAction) {
+ delete m_removeAction;
+ m_removeAction = nullptr;
+ addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
+ addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
+ } else if (showDeleteAction && !showMoveToTrashAction) {
+ addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
+ } else {
+ if (!m_removeAction) {
+ m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
}
+ addAction(m_removeAction);
+ m_removeAction->update();
}
+}
+
+bool DolphinContextMenu::placeExists(const QUrl &url) const
+{
+ const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
+
+ const auto &matchedPlaces = placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
- const int itemsCount = actionsMenu->count();
- if (itemsCount == 0) {
- // no actions are available at all, hence show the "Actions"
- // submenu disabled
- actionsMenu->setEnabled(false);
+ return !matchedPlaces.isEmpty();
+}
+
+QAction *DolphinContextMenu::createPasteAction()
+{
+ QAction *action = nullptr;
+ KFileItem destItem;
+ if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
+ destItem = m_fileInfo;
+ } else {
+ destItem = baseFileItem();
}
- 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.
- const int id = actionsMenu->idAt(0);
- const QString text(actionsMenu->text(id));
- QIcon iconSet = actionsMenu->iconSet(id);
- if (iconSet.isNull()) {
- QAction *action = popup->addAction(text);
- serviceActions.clear();
- serviceActions << action;
+ if (!destItem.isNull() && destItem.isDir()) {
+ const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+ bool canPaste;
+ const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
+ if (canPaste) {
+ if (destItem == m_fileInfo) {
+ // if paste destination is a selected folder
+ action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
+ connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
+ } else {
+ action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
}
- else {
- QAction *action = popup->addAction(iconSet, text);
- serviceActions.clear();
- serviceActions << action;
- }
- }
- else {
- // The item is a sub menu, hence show the sub menu in the root menu.
- popup->insertItem(actionsMenu->text(submenuID), menu);
}
- actionsMenu->deleteLater();
- actionsMenu = 0;
- }
- else {
- popup->insertItem(i18n("Actions"), actionsMenu);
}
- return serviceActions;
+ return action;
}
-bool DolphinContextMenu::containsEntry(const KMenu* menu,
- const QString& entryName) const
+KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const
{
- assert(menu != 0);
+ if (!m_selectedItemsProperties) {
+ m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
+ }
+ return *m_selectedItemsProperties;
+}
- const uint count = menu->count();
- for (uint i = 0; i < count; ++i) {
- const int id = menu->idAt(i);
- if (menu->text(id) == entryName) {
- return true;
+KFileItem DolphinContextMenu::baseFileItem()
+{
+ if (!m_baseFileItem) {
+ const DolphinView *view = m_mainWindow->activeViewContainer()->view();
+ KFileItem baseItem = view->rootItem();
+ if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
+ m_baseFileItem = new KFileItem(m_baseUrl);
+ } else {
+ m_baseFileItem = new KFileItem(baseItem);
}
}
+ return *m_baseFileItem;
+}
- return false;
+void DolphinContextMenu::addOpenWithActions()
+{
+ // insert 'Open With...' action or sub menu
+ m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()});
+
+ // For a single file, hint in "Open with" menu that middle-clicking would open it in the secondary app.
+ if (m_selectedItems.count() == 1 && !m_fileInfo.isDir()) {
+ if (QAction *openWithSubMenu = findChild<QAction *>(QStringLiteral("openWith_submenu"))) {
+ Q_ASSERT(openWithSubMenu->menu());
+ Q_ASSERT(!openWithSubMenu->menu()->isEmpty());
+
+ auto *secondaryApp = openWithSubMenu->menu()->actions().first();
+ // Add it like a keyboard shortcut, Qt uses \t as a separator.
+ if (!secondaryApp->text().contains(QLatin1Char('\t'))) {
+ secondaryApp->setText(secondaryApp->text() + QLatin1Char('\t')
+ + i18nc("@action:inmenu Shortcut, middle click to trigger menu item, keep short", "Middle Click"));
+ }
+ }
+ }
}
+
+void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props)
+{
+ addSeparator();
+
+ QList<QAction *> additionalActions;
+ if (props.isLocal() && ContextMenuSettings::showOpenTerminal()) {
+ additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal_here"));
+ }
+ m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
+
+ const DolphinView *view = m_mainWindow->activeViewContainer()->view();
+ const QList<QAction *> versionControlActions = view->versionControlActions(m_selectedItems);
+ if (!versionControlActions.isEmpty()) {
+ addSeparator();
+ addActions(versionControlActions);
+ addSeparator();
+ }
+}
+
+#include "moc_dolphincontextmenu.cpp"