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