1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "treeviewcontextmenu.h"
23 #include "folderspanel.h"
26 #include <KConfigGroup>
27 #include <KFileItemListProperties>
28 #include <KIO/CopyJob>
29 #include <KIO/DeleteJob>
30 #include <KIO/FileUndoManager>
31 #include <KIO/JobUiDelegate>
33 #include <KIO/PasteJob>
34 #include <KJobWidgets>
35 #include <KLocalizedString>
36 #include <KPropertiesDialog>
37 #include <KSharedConfig>
38 #include <KUrlMimeData>
40 #include <QApplication>
46 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel
* parent
,
47 const KFileItem
& fileInfo
) :
54 TreeViewContextMenu::~TreeViewContextMenu()
58 void TreeViewContextMenu::open(const QPoint
& pos
)
60 QMenu
* popup
= new QMenu(m_parent
);
62 if (!m_fileItem
.isNull()) {
63 KFileItemListProperties
capabilities(KFileItemList() << m_fileItem
);
65 // insert 'Cut', 'Copy' and 'Paste'
66 QAction
* cutAction
= new QAction(QIcon::fromTheme(QStringLiteral("edit-cut")), i18nc("@action:inmenu", "Cut"), this);
67 cutAction
->setEnabled(capabilities
.supportsMoving());
68 connect(cutAction
, &QAction::triggered
, this, &TreeViewContextMenu::cut
);
70 QAction
* copyAction
= new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18nc("@action:inmenu", "Copy"), this);
71 connect(copyAction
, &QAction::triggered
, this, &TreeViewContextMenu::copy
);
73 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
75 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileItem
);
76 QAction
* pasteAction
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
77 connect(pasteAction
, &QAction::triggered
, this, &TreeViewContextMenu::paste
);
78 pasteAction
->setEnabled(canPaste
);
80 popup
->addAction(cutAction
);
81 popup
->addAction(copyAction
);
82 popup
->addAction(pasteAction
);
83 popup
->addSeparator();
86 QAction
* renameAction
= new QAction(i18nc("@action:inmenu", "Rename..."), this);
87 renameAction
->setEnabled(capabilities
.supportsMoving());
88 renameAction
->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
89 connect(renameAction
, &QAction::triggered
, this, &TreeViewContextMenu::rename
);
90 popup
->addAction(renameAction
);
92 // insert 'Move to Trash' and (optionally) 'Delete'
93 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
94 KConfigGroup
configGroup(globalConfig
, "KDE");
95 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
97 const QUrl url
= m_fileItem
.url();
98 if (url
.isLocalFile()) {
99 QAction
* moveToTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("user-trash")),
100 i18nc("@action:inmenu", "Move to Trash"), this);
101 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
102 moveToTrashAction
->setEnabled(enableMoveToTrash
);
103 connect(moveToTrashAction
, &QAction::triggered
, this, &TreeViewContextMenu::moveToTrash
);
104 popup
->addAction(moveToTrashAction
);
106 showDeleteCommand
= true;
109 if (showDeleteCommand
) {
110 QAction
* deleteAction
= new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Delete"), this);
111 deleteAction
->setEnabled(capabilities
.supportsDeleting());
112 connect(deleteAction
, &QAction::triggered
, this, &TreeViewContextMenu::deleteItem
);
113 popup
->addAction(deleteAction
);
116 popup
->addSeparator();
119 // insert 'Show Hidden Files'
120 QAction
* showHiddenFilesAction
= new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
121 showHiddenFilesAction
->setCheckable(true);
122 showHiddenFilesAction
->setChecked(m_parent
->showHiddenFiles());
123 popup
->addAction(showHiddenFilesAction
);
124 connect(showHiddenFilesAction
, &QAction::toggled
, this, &TreeViewContextMenu::setShowHiddenFiles
);
126 if (!m_fileItem
.isNull()) {
127 // insert 'Limit to Home Directory'
128 const QUrl url
= m_fileItem
.url();
129 const bool enableLimitToHomeDirectory
= url
.isLocalFile();
130 QAction
* limitFoldersPanelToHomeAction
= new QAction(i18nc("@action:inmenu", "Limit to Home Directory"), this);
131 limitFoldersPanelToHomeAction
->setCheckable(true);
132 limitFoldersPanelToHomeAction
->setEnabled(enableLimitToHomeDirectory
);
133 limitFoldersPanelToHomeAction
->setChecked(m_parent
->limitFoldersPanelToHome());
134 popup
->addAction(limitFoldersPanelToHomeAction
);
135 connect(limitFoldersPanelToHomeAction
, &QAction::toggled
, this, &TreeViewContextMenu::setLimitFoldersPanelToHome
);
138 // insert 'Automatic Scrolling'
139 QAction
* autoScrollingAction
= new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
140 autoScrollingAction
->setCheckable(true);
141 autoScrollingAction
->setChecked(m_parent
->autoScrolling());
142 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
143 // in KItemViews or manually as part of the FoldersPanel
144 //popup->addAction(autoScrollingAction);
145 connect(autoScrollingAction
, &QAction::toggled
, this, &TreeViewContextMenu::setAutoScrolling
);
147 if (!m_fileItem
.isNull()) {
148 // insert 'Properties' entry
149 QAction
* propertiesAction
= new QAction(i18nc("@action:inmenu", "Properties"), this);
150 propertiesAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
151 connect(propertiesAction
, &QAction::triggered
, this, &TreeViewContextMenu::showProperties
);
152 popup
->addAction(propertiesAction
);
155 QList
<QAction
*> customActions
= m_parent
->customContextMenuActions();
156 if (!customActions
.isEmpty()) {
157 popup
->addSeparator();
158 foreach (QAction
* action
, customActions
) {
159 popup
->addAction(action
);
163 QPointer
<QMenu
> popupPtr
= popup
;
165 if (popupPtr
.data()) {
166 popupPtr
.data()->deleteLater();
170 void TreeViewContextMenu::populateMimeData(QMimeData
* mimeData
, bool cut
)
173 kdeUrls
.append(m_fileItem
.url());
174 QList
<QUrl
> mostLocalUrls
;
176 mostLocalUrls
.append(m_fileItem
.mostLocalUrl(&dummy
));
177 KIO::setClipboardDataCut(mimeData
, cut
);
178 KUrlMimeData::setUrls(kdeUrls
, mostLocalUrls
, mimeData
);
181 void TreeViewContextMenu::cut()
183 QMimeData
* mimeData
= new QMimeData();
184 populateMimeData(mimeData
, true);
185 QApplication::clipboard()->setMimeData(mimeData
);
188 void TreeViewContextMenu::copy()
190 QMimeData
* mimeData
= new QMimeData();
191 populateMimeData(mimeData
, false);
192 QApplication::clipboard()->setMimeData(mimeData
);
195 void TreeViewContextMenu::paste()
197 KIO::PasteJob
*job
= KIO::paste(QApplication::clipboard()->mimeData(), m_fileItem
.url());
198 KJobWidgets::setWindow(job
, m_parent
);
201 void TreeViewContextMenu::rename()
203 m_parent
->rename(m_fileItem
);
206 void TreeViewContextMenu::moveToTrash()
208 const QList
<QUrl
> list
{m_fileItem
.url()};
209 KIO::JobUiDelegate uiDelegate
;
210 uiDelegate
.setWindow(m_parent
);
211 if (uiDelegate
.askDeleteConfirmation(list
, KIO::JobUiDelegate::Trash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
212 KIO::Job
* job
= KIO::trash(list
);
213 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash
, list
, QUrl(QStringLiteral("trash:/")), job
);
214 KJobWidgets::setWindow(job
, m_parent
);
215 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
219 void TreeViewContextMenu::deleteItem()
221 const QList
<QUrl
> list
{m_fileItem
.url()};
222 KIO::JobUiDelegate uiDelegate
;
223 uiDelegate
.setWindow(m_parent
);
224 if (uiDelegate
.askDeleteConfirmation(list
, KIO::JobUiDelegate::Delete
, KIO::JobUiDelegate::DefaultConfirmation
)) {
225 KIO::Job
* job
= KIO::del(list
);
226 KJobWidgets::setWindow(job
, m_parent
);
227 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
231 void TreeViewContextMenu::showProperties()
233 KPropertiesDialog
* dialog
= new KPropertiesDialog(m_fileItem
.url(), m_parent
);
234 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
238 void TreeViewContextMenu::setShowHiddenFiles(bool show
)
240 m_parent
->setShowHiddenFiles(show
);
243 void TreeViewContextMenu::setLimitFoldersPanelToHome(bool enable
)
245 m_parent
->setLimitFoldersPanelToHome(enable
);
248 void TreeViewContextMenu::setAutoScrolling(bool enable
)
250 m_parent
->setAutoScrolling(enable
);