]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/folders/treeviewcontextmenu.cpp
Adapt to KConfigGroup name officially being a QString type
[dolphin.git] / src / panels / folders / treeviewcontextmenu.cpp
index 4afe9e8e39360b235413a5e9cdee07393faaa344..ec1ccb4d637089a7733a5027c7a3dac55b886c29 100644 (file)
@@ -1,53 +1,37 @@
-/***************************************************************************
- *   Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com>        *
- *   Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com>       *
- *                                                                         *
- *   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-2010 Peter Penz <peter.penz19@gmail.com>
+ * SPDX-FileCopyrightText: 2006 Cvetoslav Ludmiloff <ludmiloff@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "treeviewcontextmenu.h"
 
-#include <KFileItem>
-#include <KIO/CopyJob>
-#include <KIO/DeleteJob>
-#include <KIO/JobUiDelegate>
-#include <QMenu>
-#include <QIcon>
-#include <KJobWidgets>
-#include <KSharedConfig>
+#include "folderspanel.h"
+#include "global.h"
+
 #include <KConfigGroup>
-#include <kurlmimedata.h>
 #include <KFileItemListProperties>
-#include <konq_operations.h>
-#include <KLocalizedString>
+#include <KIO/CopyJob>
+#include <KIO/DeleteOrTrashJob>
 #include <KIO/Paste>
-#include <KIO/FileUndoManager>
+#include <KIO/PasteJob>
+#include <KJobWidgets>
+#include <KLocalizedString>
 #include <KPropertiesDialog>
-
-#include "folderspanel.h"
+#include <KSharedConfig>
+#include <KUrlMimeData>
 
 #include <QApplication>
 #include <QClipboard>
+#include <QMenu>
 #include <QMimeData>
+#include <QPointer>
 
-TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
-                                         const KFileItem& fileInfo) :
-    QObject(parent),
-    m_parent(parent),
-    m_fileItem(fileInfo)
+TreeViewContextMenu::TreeViewContextMenu(FoldersPanel *parent, const KFileItem &fileInfo)
+    : QObject(parent)
+    , m_parent(parent)
+    , m_fileItem(fileInfo)
 {
 }
 
@@ -55,25 +39,27 @@ TreeViewContextMenu::~TreeViewContextMenu()
 {
 }
 
-void TreeViewContextMenu::open()
+void TreeViewContextMenu::open(const QPoint &pos)
 {
-    QMenupopup = new QMenu(m_parent);
+    QMenu *popup = new QMenu(m_parent);
 
     if (!m_fileItem.isNull()) {
         KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
 
         // insert 'Cut', 'Copy' and 'Paste'
-        QAction* cutAction = new QAction(QIcon::fromTheme("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
+        QAction *cutAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-cut")), i18nc("@action:inmenu", "Cut"), this);
         cutAction->setEnabled(capabilities.supportsMoving());
         connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut);
 
-        QAction* copyAction = new QAction(QIcon::fromTheme("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
+        QAction *copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18nc("@action:inmenu", "Copy"), this);
         connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy);
 
-        const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileItem.url());
-        QAction* pasteAction = new QAction(QIcon::fromTheme("edit-paste"), pasteInfo.second, this);
+        const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+        bool canPaste;
+        const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileItem);
+        QAction *pasteAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
         connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste);
-        pasteAction->setEnabled(pasteInfo.first);
+        pasteAction->setEnabled(canPaste);
 
         popup->addAction(cutAction);
         popup->addAction(copyAction);
@@ -81,21 +67,20 @@ void TreeViewContextMenu::open()
         popup->addSeparator();
 
         // insert 'Rename'
-        QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
+        QAction *renameAction = new QAction(i18nc("@action:inmenu", "Rename…"), this);
         renameAction->setEnabled(capabilities.supportsMoving());
-        renameAction->setIcon(QIcon::fromTheme("edit-rename"));
+        renameAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
         connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename);
         popup->addAction(renameAction);
 
         // insert 'Move to Trash' and (optionally) 'Delete'
-        KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
-        KConfigGroup configGroup(globalConfig, "KDE");
+        KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
+        KConfigGroup configGroup(globalConfig, QStringLiteral("KDE"));
         bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
 
         const QUrl url = m_fileItem.url();
         if (url.isLocalFile()) {
-            QAction* moveToTrashAction = new QAction(QIcon::fromTheme("user-trash"),
-                                                    i18nc("@action:inmenu", "Move to Trash"), this);
+            QAction *moveToTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("user-trash")), i18nc("@action:inmenu", "Move to Trash"), this);
             const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
             moveToTrashAction->setEnabled(enableMoveToTrash);
             connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash);
@@ -105,7 +90,7 @@ void TreeViewContextMenu::open()
         }
 
         if (showDeleteCommand) {
-            QAction* deleteAction = new QAction(QIcon::fromTheme("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
+            QAction *deleteAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Delete"), this);
             deleteAction->setEnabled(capabilities.supportsDeleting());
             connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem);
             popup->addAction(deleteAction);
@@ -115,14 +100,26 @@ void TreeViewContextMenu::open()
     }
 
     // insert 'Show Hidden Files'
-    QActionshowHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
+    QAction *showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
     showHiddenFilesAction->setCheckable(true);
     showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
     popup->addAction(showHiddenFilesAction);
     connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles);
 
+    if (!m_fileItem.isNull()) {
+        // insert 'Limit to Home Directory'
+        const QUrl url = m_fileItem.url();
+        const bool enableLimitToHomeDirectory = url.isLocalFile();
+        QAction *limitFoldersPanelToHomeAction = new QAction(i18nc("@action:inmenu", "Limit to Home Directory"), this);
+        limitFoldersPanelToHomeAction->setCheckable(true);
+        limitFoldersPanelToHomeAction->setEnabled(enableLimitToHomeDirectory);
+        limitFoldersPanelToHomeAction->setChecked(m_parent->limitFoldersPanelToHome());
+        popup->addAction(limitFoldersPanelToHomeAction);
+        connect(limitFoldersPanelToHomeAction, &QAction::toggled, this, &TreeViewContextMenu::setLimitFoldersPanelToHome);
+    }
+
     // insert 'Automatic Scrolling'
-    QActionautoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
+    QAction *autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
     autoScrollingAction->setCheckable(true);
     autoScrollingAction->setChecked(m_parent->autoScrolling());
     // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
@@ -132,55 +129,57 @@ void TreeViewContextMenu::open()
 
     if (!m_fileItem.isNull()) {
         // insert 'Properties' entry
-        QActionpropertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
-        propertiesAction->setIcon(QIcon::fromTheme("document-properties"));
+        QAction *propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
+        propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
         connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties);
         popup->addAction(propertiesAction);
     }
 
-    QList<QAction*> customActions = m_parent->customContextMenuActions();
+    const QList<QAction *> customActions = m_parent->customContextMenuActions();
     if (!customActions.isEmpty()) {
         popup->addSeparator();
-        foreach (QAction* action, customActions) {
+        for (QAction *action : customActions) {
             popup->addAction(action);
         }
     }
 
-    QWeakPointer<QMenu> popupPtr = popup;
-    popup->exec(QCursor::pos());
+    QPointer<QMenu> popupPtr = popup;
+    popup->exec(pos);
     if (popupPtr.data()) {
         popupPtr.data()->deleteLater();
     }
 }
 
-void TreeViewContextMenu::populateMimeData(QMimeDatamimeData, bool cut)
+void TreeViewContextMenu::populateMimeData(QMimeData *mimeData, bool cut)
 {
     QList<QUrl> kdeUrls;
     kdeUrls.append(m_fileItem.url());
     QList<QUrl> mostLocalUrls;
     bool dummy;
-    mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
+    mostLocalUrls.append(m_fileItem.mostLocalUrl(&dummy));
     KIO::setClipboardDataCut(mimeData, cut);
+    KUrlMimeData::exportUrlsToPortal(mimeData);
     KUrlMimeData::setUrls(kdeUrls, mostLocalUrls, mimeData);
 }
 
 void TreeViewContextMenu::cut()
 {
-    QMimeDatamimeData = new QMimeData();
+    QMimeData *mimeData = new QMimeData();
     populateMimeData(mimeData, true);
     QApplication::clipboard()->setMimeData(mimeData);
 }
 
 void TreeViewContextMenu::copy()
 {
-    QMimeDatamimeData = new QMimeData();
+    QMimeData *mimeData = new QMimeData();
     populateMimeData(mimeData, false);
     QApplication::clipboard()->setMimeData(mimeData);
 }
 
 void TreeViewContextMenu::paste()
 {
-    KonqOperations::doPaste(m_parent, m_fileItem.url());
+    KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), m_fileItem.url());
+    KJobWidgets::setWindow(job, m_parent);
 }
 
 void TreeViewContextMenu::rename()
@@ -190,32 +189,21 @@ void TreeViewContextMenu::rename()
 
 void TreeViewContextMenu::moveToTrash()
 {
-    const QList<QUrl> list {QList<QUrl>() << m_fileItem.url()};
-    KIO::JobUiDelegate uiDelegate;
-    uiDelegate.setWindow(m_parent);
-    if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) {
-        KIO::Job* job = KIO::trash(list);
-        KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, QUrl("trash:/"), job);
-        KJobWidgets::setWindow(job, m_parent);
-        job->ui()->setAutoErrorHandlingEnabled(true);
-    }
+    using Iface = KIO::AskUserActionInterface;
+    auto *deleteJob = new KIO::DeleteOrTrashJob(QList{m_fileItem.url()}, Iface::Trash, Iface::DefaultConfirmation, m_parent);
+    deleteJob->start();
 }
 
 void TreeViewContextMenu::deleteItem()
 {
-    const QList<QUrl> list {QList<QUrl>() << m_fileItem.url()};
-    KIO::JobUiDelegate uiDelegate;
-    uiDelegate.setWindow(m_parent);
-    if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) {
-        KIO::Job* job = KIO::del(list);
-        KJobWidgets::setWindow(job, m_parent);
-        job->ui()->setAutoErrorHandlingEnabled(true);
-    }
+    using Iface = KIO::AskUserActionInterface;
+    auto *deleteJob = new KIO::DeleteOrTrashJob(QList{m_fileItem.url()}, Iface::Delete, Iface::DefaultConfirmation, m_parent);
+    deleteJob->start();
 }
 
 void TreeViewContextMenu::showProperties()
 {
-    KPropertiesDialogdialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
+    KPropertiesDialog *dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
     dialog->setAttribute(Qt::WA_DeleteOnClose);
     dialog->show();
 }
@@ -225,8 +213,14 @@ void TreeViewContextMenu::setShowHiddenFiles(bool show)
     m_parent->setShowHiddenFiles(show);
 }
 
+void TreeViewContextMenu::setLimitFoldersPanelToHome(bool enable)
+{
+    m_parent->setLimitFoldersPanelToHome(enable);
+}
+
 void TreeViewContextMenu::setAutoScrolling(bool enable)
 {
     m_parent->setAutoScrolling(enable);
 }
 
+#include "moc_treeviewcontextmenu.cpp"