-/***************************************************************************
- * 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 <ktrader.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 <ksortablevaluelist.h>
-#include <kio/netaccess.h>
-
-#include "dolphin.h"
-#include "dolphinview.h"
-#include "editbookmarkdialog.h"
-#include "dolphinsettings.h"
-
-
-DolphinContextMenu::DolphinContextMenu(DolphinView* parent,
- KFileItem* fileInfo,
- const QPoint& pos) :
- m_dolphinView(parent),
- m_fileInfo(fileInfo),
- m_pos(pos)
+#include "dolphin_contextmenusettings.h"
+#include "dolphin_generalsettings.h"
+#include "dolphinmainwindow.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>
+#include <QAction>
+
+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);
-void DolphinContextMenu::open()
-{
- if (m_fileInfo == 0) {
- openViewportContextMenu();
- }
- else {
- openItemContextMenu();
- }
+ addAllActions();
}
DolphinContextMenu::~DolphinContextMenu()
{
+ delete m_baseFileItem;
+ m_baseFileItem = nullptr;
+ delete m_selectedItemsProperties;
+ m_selectedItemsProperties = nullptr;
}
-void DolphinContextMenu::openViewportContextMenu()
+void DolphinContextMenu::addAllActions()
{
- // 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>)
+ 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;
+ }
+
+ if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
+ m_context |= ItemContext;
+ // TODO: handle other use cases like devices + desktop files
+ }
- assert(m_fileInfo == 0);
- const int propertiesID = 100;
- const int bookmarkID = 101;
+ // 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();
+ }
+}
- KMenu* popup = new KMenu(m_dolphinView);
- Dolphin& dolphin = Dolphin::mainWin();
+bool DolphinContextMenu::eventFilter(QObject *object, QEvent *event)
+{
+ Q_UNUSED(object)
- // setup 'Create New' menu
- KMenu* createNewMenu = new KMenu();
+ if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
+ QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
- KAction* createFolderAction = dolphin.actionCollection()->action("create_folder");
- if (createFolderAction != 0) {
- createFolderAction->plug(createNewMenu);
+ 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);
+ }
+ }
}
- createNewMenu->insertSeparator();
+ return false;
+}
- KAction* action = 0;
+void DolphinContextMenu::addTrashContextMenu()
+{
+ Q_ASSERT(m_context & TrashContext);
- Q3PtrListIterator<KAction> fileGrouptIt(dolphin.fileGroupActions());
- while ((action = fileGrouptIt.current()) != 0) {
- action->plug(createNewMenu);
- ++fileGrouptIt;
- }
+ QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Empty Trash"), this, [this]() {
+ Trash::empty(m_mainWindow);
+ });
+ emptyTrashAction->setEnabled(!Trash::isEmpty());
- // 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;
- //}
- //
- //KPopupMenu* linkToDeviceMenu = new KPopupMenu();
- //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();
-
- popup->insertItem(i18n("Bookmark this folder"), bookmarkID);
- popup->insertSeparator();
-
- popup->insertItem(i18n("Properties..."), propertiesID);
-
- int id = popup->exec(m_pos);
- if (id == propertiesID) {
- new KPropertiesDialog(dolphin.activeView()->url());
+ // Insert 'Sort By' and 'View Mode'
+ if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) {
+ addSeparator();
}
- else if (id == bookmarkID) {
- 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::showSortBy()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
+ }
+ if (ContextMenuSettings::showViewMode()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
}
- popup->deleteLater();
+ addSeparator();
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
}
-void DolphinContextMenu::openItemContextMenu()
+void DolphinContextMenu::addTrashItemContextMenu()
{
- // 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);
- Dolphin& dolphin = Dolphin::mainWin();
- 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();
+ Q_ASSERT(m_context & TrashContext);
+ Q_ASSERT(m_context & ItemContext);
+
+ addAction(QIcon::fromTheme(QStringLiteral("edit-reset")),
+ i18ncp("@action:inmenu Restore the selected files that are in the trash to the place they lived at the moment they were trashed. Minimize the "
+ "length of this string if possible.",
+ "Restore to Former Location",
+ "Restore to Former Locations",
+ m_selectedItems.count()),
+ this,
+ [this]() {
+ QList<QUrl> selectedUrls;
+ selectedUrls.reserve(m_selectedItems.count());
+ for (const KFileItem &item : std::as_const(m_selectedItems)) {
+ selectedUrls.append(item.url());
+ }
+
+ KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
+ KJobWidgets::setWindow(job, m_mainWindow);
+ job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+ });
- // insert 'Rename'
- KAction* renameAction = dolphin.actionCollection()->action("rename");
- renameAction->plug(popup);
+ addSeparator();
- // 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);
+ addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Cut)));
+ addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Copy)));
+
+ addSeparator();
+
+ QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
+ addAction(deleteAction);
+
+ addSeparator();
+
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("properties")));
+}
+
+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")));
}
- else {
- KAction* deleteAction = dolphin.actionCollection()->action("delete");
- deleteAction->plug(popup);
+ if (ContextMenuSettings::showOpenInNewWindow()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
}
- // 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
- if (m_fileInfo->isDir() && (urls.count() == 1)) {
- popup->insertItem(i18n("Bookmark this folder"), bookmarkID);
+ if (ContextMenuSettings::showOpenInSplitView()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_split_view")));
}
- popup->insertSeparator();
+ // Insert 'Open With' entries
+ addOpenWithActions();
+
+ // set up 'Create New' menu
+ QAction *newDirAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_dir"));
+ QAction *newFileAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_file"));
+ DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(newDirAction, newFileAction, this);
+ 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::addOpenParentFolderActions()
+{
+ 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]() {
+ const QUrl url = m_fileInfo.targetUrl();
+ const QUrl parentUrl = KIO::upUrl(url);
+ DolphinTabPage *tabPage = m_mainWindow->openNewTab(parentUrl);
+ tabPage->activeViewContainer()->view()->markUrlsAsSelected({url});
+ tabPage->activeViewContainer()->view()->markUrlAsCurrent(url);
+ });
+
+ 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);
+ });
+}
+
+void DolphinContextMenu::addItemContextMenu()
+{
+ Q_ASSERT(!m_fileInfo.isNull());
- // Insert 'Open With...' sub menu
- Q3ValueVector<KService::Ptr> openWithVector;
- const int openWithID = insertOpenWithItems(popup, openWithVector);
+ const KFileItemListProperties &selectedItemsProps = selectedItemsProperties();
- // Insert 'Actions' sub menu
- Q3ValueVector<KDEDesktopMimeType::Service> actionsVector;
- insertActionItems(popup, actionsVector);
+ m_fileItemActions->setItemListProperties(selectedItemsProps);
- // insert 'Properties...' entry
- popup->insertSeparator();
- KAction* propertiesAction = dolphin.actionCollection()->action("properties");
- propertiesAction->plug(popup);
-
- int id = popup->exec(m_pos);
-
- if (id == bookmarkID) {
- 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 (m_selectedItems.count() == 1) {
+ // single files
+ if (m_fileInfo.isDir()) {
+ addDirectoryItemContextMenu();
+ } else if (m_context & TimelineContext || m_context & SearchContext || m_context & RecentlyUsedContext) {
+ addOpenWithActions();
+
+ addOpenParentFolderActions();
+
+ 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;
+ }
+ }
+
+ 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 (id >= actionsIDStart) {
- // one of the 'Actions' items has been selected
- KDEDesktopMimeType::executeService(urls, actionsVector[id - actionsIDStart]);
+
+ 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 (id >= openWithIDStart) {
- // one of the 'Open With' items has been selected
- if (id == openWithID) {
- // the item 'Other...' has been selected
- KRun::displayOpenWithDialog(urls);
+
+ if (m_mainWindow->isSplitViewEnabledInCurrentTab()) {
+ if (ContextMenuSettings::showCopyToOtherSplitView()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view")));
}
- else {
- KService::Ptr servicePtr = openWithVector[id - openWithIDStart];
- KRun::run(*servicePtr, urls);
+
+ 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);
}
-int 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();
- assert(list != 0);
-
- bool insertOpenWithItems = true;
- const QString contextMimeType(m_fileInfo->mimetype());
- KFileItemListIterator mimeIt(*list);
- KFileItem* item = 0;
- while (insertOpenWithItems && ((item = mimeIt.current()) != 0)) {
- insertOpenWithItems = (contextMimeType == item->mimetype());
- ++mimeIt;
+ 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);
}
- int openWithID = -1;
-
- if (insertOpenWithItems) {
- // fill the 'Open with' sub menu with application types
- const KMimeType::Ptr mimePtr = KMimeType::findByURL(m_fileInfo->url());
- KTrader::OfferList offers = KTrader::self()->query(mimePtr->name(),
- "Type == 'Application'");
- int index = openWithIDStart;
- if (offers.count() > 0) {
- KTrader::OfferList::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)) {
- openWithMenu->insertItem((*it)->pixmap(KIcon::Small),
- appName, index);
- openWithVector.append(*it);
- ++index;
- }
- }
+ // 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();
- openWithMenu->insertItem(i18n("&Other..."), index);
- 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.
- popup->insertItem(i18n("Open With..."), openWithIDStart);
- }
- openWithID = index;
+ // Insert 'Sort By', 'Group 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.
- popup->insertItem(i18n("Open With..."), openWithIDStart);
- popup->setItemEnabled(openWithIDStart, false);
+ if (ContextMenuSettings::showGroupBy()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("group")));
}
+ if (ContextMenuSettings::showViewMode()) {
+ addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
+ }
+ if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) {
+ addSeparator();
+ }
+
+ addAdditionalActions(baseUrlProperties);
- popup->setItemEnabled(openWithID, insertOpenWithItems);
+ addSeparator();
- return openWithID;
+ QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+ addAction(propertiesAction);
}
-void DolphinContextMenu::insertActionItems(KMenu* popup,
- Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
+void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties &properties)
{
- KMenu* actionsMenu = new KMenu();
-
- int actionsIndex = 0;
-
- 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();
- assert(list != 0);
-
- KFileItemListIterator mimeIt(*list);
- KFileItem* item = 0;
- insert = true;
- while (insert && ((item = mimeIt.current()) != 0)) {
- insert = !item->isDir();
- ++mimeIt;
- }
- }
-
- 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();
- assert(list != 0);
-
- KFileItemListIterator mimeIt(*list);
- KFileItem* item = 0;
- insert = true;
- while (insert && ((item = mimeIt.current()) != 0)) {
- const QString mimeType((*mimeIt)->mimetype());
- const QString mimeGroup(mimeType.left(mimeType.find('/')));
-
- insert = (*it == mimeType) ||
- ((*it).right(1) == "*") &&
- ((*it).left((*it).find('/')) == mimeGroup);
- ++mimeIt;
- }
- }
-
- 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()) {
- menu->insertItem(SmallIcon(service.m_strIcon),
- service.m_strName,
- actionsIDStart + actionsIndex);
- }
- else {
- menu->insertItem(service.m_strName,
- actionsIDStart + actionsIndex);
- }
- actionsVector.append(service);
- ++actionsIndex;
- }
- }
- }
- }
+ 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(QStringLiteral("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();
}
+}
- const int itemsCount = actionsMenu->count();
- if (itemsCount == 0) {
- // no actions are available at all, hence show the "Actions"
- // submenu disabled
- actionsMenu->setEnabled(false);
+bool DolphinContextMenu::placeExists(const QUrl &url) const
+{
+ const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
+
+ QModelIndex url_index = placesModel->closestItem(url);
+ return url_index.isValid() && placesModel->url(url_index).matches(url, QUrl::StripTrailingSlash);
+}
+
+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));
- const QIcon* iconSet = actionsMenu->iconSet(id);
- if (iconSet == 0) {
- popup->insertItem(text, id);
- }
- else {
- popup->insertItem(*iconSet, text, id);
+ 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 {
- // 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 action;
+}
+
+KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const
+{
+ if (!m_selectedItemsProperties) {
+ m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
}
+ return *m_selectedItemsProperties;
}
-bool DolphinContextMenu::containsEntry(const KMenu* menu,
- const QString& entryName) const
+KFileItem DolphinContextMenu::baseFileItem()
{
- assert(menu != 0);
+ 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;
+}
- 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;
+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.
+ // (Unless middle-clicking would open it as a folder in a new tab (e.g. archives).)
+ const QUrl &url = DolphinView::openItemAsFolderUrl(m_fileInfo, GeneralSettings::browseThroughArchives());
+ if (m_selectedItems.count() == 1 && url.isEmpty()) {
+ 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"));
+ }
}
}
+}
- return false;
+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"