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