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