]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
Merge branch 'frameworks'
[dolphin.git] / src / panels / folders / treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "treeviewcontextmenu.h"
22
23 #include <KFileItem>
24 #include <KIO/CopyJob>
25 #include <KIO/DeleteJob>
26 #include <KIO/JobUiDelegate>
27 #include <QMenu>
28 #include <QIcon>
29 #include <KJobWidgets>
30 #include <KSharedConfig>
31 #include <KConfigGroup>
32 #include <KUrlMimeData>
33 #include <KFileItemListProperties>
34 #include <KLocalizedString>
35 #include <KIO/PasteJob>
36 #include <KIO/Paste>
37 #include <KIO/FileUndoManager>
38 #include <KPropertiesDialog>
39
40 #include "folderspanel.h"
41
42 #include <QApplication>
43 #include <QClipboard>
44 #include <QMimeData>
45 #include <QPointer>
46
47 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
48 const KFileItem& fileInfo) :
49 QObject(parent),
50 m_parent(parent),
51 m_fileItem(fileInfo)
52 {
53 }
54
55 TreeViewContextMenu::~TreeViewContextMenu()
56 {
57 }
58
59 void TreeViewContextMenu::open()
60 {
61 QMenu* popup = new QMenu(m_parent);
62
63 if (!m_fileItem.isNull()) {
64 KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
65
66 // insert 'Cut', 'Copy' and 'Paste'
67 QAction* cutAction = new QAction(QIcon::fromTheme("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
68 cutAction->setEnabled(capabilities.supportsMoving());
69 connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut);
70
71 QAction* copyAction = new QAction(QIcon::fromTheme("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
72 connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy);
73
74 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
75 bool canPaste;
76 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileItem);
77 QAction* pasteAction = new QAction(QIcon::fromTheme("edit-paste"), text, this);
78 connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste);
79 pasteAction->setEnabled(canPaste);
80
81 popup->addAction(cutAction);
82 popup->addAction(copyAction);
83 popup->addAction(pasteAction);
84 popup->addSeparator();
85
86 // insert 'Rename'
87 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
88 renameAction->setEnabled(capabilities.supportsMoving());
89 renameAction->setIcon(QIcon::fromTheme("edit-rename"));
90 connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename);
91 popup->addAction(renameAction);
92
93 // insert 'Move to Trash' and (optionally) 'Delete'
94 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
95 KConfigGroup configGroup(globalConfig, "KDE");
96 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
97
98 const QUrl url = m_fileItem.url();
99 if (url.isLocalFile()) {
100 QAction* moveToTrashAction = new QAction(QIcon::fromTheme("user-trash"),
101 i18nc("@action:inmenu", "Move to Trash"), this);
102 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
103 moveToTrashAction->setEnabled(enableMoveToTrash);
104 connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash);
105 popup->addAction(moveToTrashAction);
106 } else {
107 showDeleteCommand = true;
108 }
109
110 if (showDeleteCommand) {
111 QAction* deleteAction = new QAction(QIcon::fromTheme("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
112 deleteAction->setEnabled(capabilities.supportsDeleting());
113 connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem);
114 popup->addAction(deleteAction);
115 }
116
117 popup->addSeparator();
118 }
119
120 // insert 'Show Hidden Files'
121 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
122 showHiddenFilesAction->setCheckable(true);
123 showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
124 popup->addAction(showHiddenFilesAction);
125 connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles);
126
127 // insert 'Automatic Scrolling'
128 QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
129 autoScrollingAction->setCheckable(true);
130 autoScrollingAction->setChecked(m_parent->autoScrolling());
131 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
132 // in KItemViews or manually as part of the FoldersPanel
133 //popup->addAction(autoScrollingAction);
134 connect(autoScrollingAction, &QAction::toggled, this, &TreeViewContextMenu::setAutoScrolling);
135
136 if (!m_fileItem.isNull()) {
137 // insert 'Properties' entry
138 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
139 propertiesAction->setIcon(QIcon::fromTheme("document-properties"));
140 connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties);
141 popup->addAction(propertiesAction);
142 }
143
144 QList<QAction*> customActions = m_parent->customContextMenuActions();
145 if (!customActions.isEmpty()) {
146 popup->addSeparator();
147 foreach (QAction* action, customActions) {
148 popup->addAction(action);
149 }
150 }
151
152 QPointer<QMenu> popupPtr = popup;
153 popup->exec(QCursor::pos());
154 if (popupPtr.data()) {
155 popupPtr.data()->deleteLater();
156 }
157 }
158
159 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
160 {
161 QList<QUrl> kdeUrls;
162 kdeUrls.append(m_fileItem.url());
163 QList<QUrl> mostLocalUrls;
164 bool dummy;
165 mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
166 KIO::setClipboardDataCut(mimeData, cut);
167 KUrlMimeData::setUrls(kdeUrls, mostLocalUrls, mimeData);
168 }
169
170 void TreeViewContextMenu::cut()
171 {
172 QMimeData* mimeData = new QMimeData();
173 populateMimeData(mimeData, true);
174 QApplication::clipboard()->setMimeData(mimeData);
175 }
176
177 void TreeViewContextMenu::copy()
178 {
179 QMimeData* mimeData = new QMimeData();
180 populateMimeData(mimeData, false);
181 QApplication::clipboard()->setMimeData(mimeData);
182 }
183
184 void TreeViewContextMenu::paste()
185 {
186 KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), m_fileItem.url());
187 KJobWidgets::setWindow(job, m_parent);
188 }
189
190 void TreeViewContextMenu::rename()
191 {
192 m_parent->rename(m_fileItem);
193 }
194
195 void TreeViewContextMenu::moveToTrash()
196 {
197 const QList<QUrl> list{m_fileItem.url()};
198 KIO::JobUiDelegate uiDelegate;
199 uiDelegate.setWindow(m_parent);
200 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) {
201 KIO::Job* job = KIO::trash(list);
202 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, QUrl("trash:/"), job);
203 KJobWidgets::setWindow(job, m_parent);
204 job->ui()->setAutoErrorHandlingEnabled(true);
205 }
206 }
207
208 void TreeViewContextMenu::deleteItem()
209 {
210 const QList<QUrl> list{m_fileItem.url()};
211 KIO::JobUiDelegate uiDelegate;
212 uiDelegate.setWindow(m_parent);
213 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) {
214 KIO::Job* job = KIO::del(list);
215 KJobWidgets::setWindow(job, m_parent);
216 job->ui()->setAutoErrorHandlingEnabled(true);
217 }
218 }
219
220 void TreeViewContextMenu::showProperties()
221 {
222 KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
223 dialog->setAttribute(Qt::WA_DeleteOnClose);
224 dialog->show();
225 }
226
227 void TreeViewContextMenu::setShowHiddenFiles(bool show)
228 {
229 m_parent->setShowHiddenFiles(show);
230 }
231
232 void TreeViewContextMenu::setAutoScrolling(bool enable)
233 {
234 m_parent->setAutoScrolling(enable);
235 }
236