- /***************************************************************************
- * 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., *
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <QApplication>
#include <QClipboard>
#include <QKeyEvent>
-#include <QMenu>
#include <QMenuBar>
#include <QMimeDatabase>
if (exec(m_pos) == restoreAction) {
QList<QUrl> selectedUrls;
selectedUrls.reserve(m_selectedItems.count());
- foreach (const KFileItem &item, m_selectedItems) {
+ for (const KFileItem &item : qAsConst(m_selectedItems)) {
selectedUrls.append(item.url());
}
{
const DolphinView* view = m_mainWindow->activeViewContainer()->view();
- // Insert 'Open With' entries
- KFileItem baseItem = view->rootItem();
- if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
- baseItem = baseFileItem();
- }
-
- const KFileItemListProperties baseUrlProperties(KFileItemList() << baseItem);
+ const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
KFileItemActions fileItemActions;
fileItemActions.setParentWidget(m_mainWindow);
fileItemActions.setItemListProperties(baseUrlProperties);
newFileMenu->setPopupFiles(QList<QUrl>() << m_baseUrl);
addMenu(newFileMenu->menu());
- // Don't show "Open With" menu items if the current dir is empty, because there's
- // generally no app that can do anything interesting with an empty directory
- if (view->itemsCount() != 0) {
- addOpenWithActions(fileItemActions);
- }
+ // 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(fileItemActions);
QAction* pasteAction = createPasteAction();
- addAction(pasteAction);
+ if (pasteAction) {
+ addAction(pasteAction);
+ }
// Insert 'Add to Places' entry if it's not already in the places panel
if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
{
const KActionCollection* collection = m_mainWindow->actionCollection();
- // Insert 'Cut', 'Copy', 'Copy location' and 'Paste'
+ // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
QAction* copyPathAction = collection->action(QString("copy_location"));
copyPathAction->setEnabled(m_selectedItems.size() == 1);
addAction(copyPathAction);
- addAction(createPasteAction());
+ QAction* pasteAction = createPasteAction();
+ if (pasteAction) {
+ addAction(pasteAction);
+ }
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
addSeparator();
QAction* DolphinContextMenu::createPasteAction()
{
QAction* action = nullptr;
- const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
- if (isDir && (m_selectedItems.count() == 1)) {
- const QMimeData *mimeData = QApplication::clipboard()->mimeData();
- bool canPaste;
- const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
- action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
- action->setEnabled(canPaste);
- connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
+ KFileItem destItem;
+ if (!m_fileInfo.isNull()) {
+ destItem = m_fileInfo;
} else {
- action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
+ destItem = baseFileItem();
+ }
+
+ if (!destItem.isNull() && destItem.isDir()) {
+ if (m_selectedItems.count() <= 1) {
+ const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+ bool canPaste;
+ const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
+ action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
+ if (canPaste) {
+ connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
+ } else {
+ // don't add the unavailable action
+ action = nullptr;
+ }
+ }
}
return action;
KFileItem DolphinContextMenu::baseFileItem()
{
if (!m_baseFileItem) {
- m_baseFileItem = new KFileItem(m_baseUrl);
+ 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;
}