]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
Merge branch 'master' into 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 <KIconLoader>
25 #include <KIO/DeleteJob>
26 #include <KMenu>
27 #include <KIcon>
28 #include <KSharedConfig>
29 #include <KConfigGroup>
30 #include <konqmimedata.h>
31 #include <KFileItemListProperties>
32 #include <konq_operations.h>
33 #include <KLocale>
34 #include <KPropertiesDialog>
35
36 #include "folderspanel.h"
37
38 #include <QApplication>
39 #include <QClipboard>
40 #include <QMimeData>
41
42 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
43 const KFileItem& fileInfo) :
44 QObject(parent),
45 m_parent(parent),
46 m_fileItem(fileInfo)
47 {
48 }
49
50 TreeViewContextMenu::~TreeViewContextMenu()
51 {
52 }
53
54 void TreeViewContextMenu::open()
55 {
56 KMenu* popup = new KMenu(m_parent);
57
58 if (!m_fileItem.isNull()) {
59 KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
60
61 // insert 'Cut', 'Copy' and 'Paste'
62 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
63 cutAction->setEnabled(capabilities.supportsMoving());
64 connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut);
65
66 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
67 connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy);
68
69 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileItem.url());
70 QAction* pasteAction = new QAction(KIcon("edit-paste"), pasteInfo.second, this);
71 connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste);
72 pasteAction->setEnabled(pasteInfo.first);
73
74 popup->addAction(cutAction);
75 popup->addAction(copyAction);
76 popup->addAction(pasteAction);
77 popup->addSeparator();
78
79 // insert 'Rename'
80 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
81 renameAction->setEnabled(capabilities.supportsMoving());
82 renameAction->setIcon(KIcon("edit-rename"));
83 connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename);
84 popup->addAction(renameAction);
85
86 // insert 'Move to Trash' and (optionally) 'Delete'
87 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
88 KConfigGroup configGroup(globalConfig, "KDE");
89 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
90
91 const KUrl url = m_fileItem.url();
92 if (url.isLocalFile()) {
93 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
94 i18nc("@action:inmenu", "Move to Trash"), this);
95 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
96 moveToTrashAction->setEnabled(enableMoveToTrash);
97 connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash);
98 popup->addAction(moveToTrashAction);
99 } else {
100 showDeleteCommand = true;
101 }
102
103 if (showDeleteCommand) {
104 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
105 deleteAction->setEnabled(capabilities.supportsDeleting());
106 connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem);
107 popup->addAction(deleteAction);
108 }
109
110 popup->addSeparator();
111 }
112
113 // insert 'Show Hidden Files'
114 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
115 showHiddenFilesAction->setCheckable(true);
116 showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
117 popup->addAction(showHiddenFilesAction);
118 connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles);
119
120 // insert 'Automatic Scrolling'
121 QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
122 autoScrollingAction->setCheckable(true);
123 autoScrollingAction->setChecked(m_parent->autoScrolling());
124 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
125 // in KItemViews or manually as part of the FoldersPanel
126 //popup->addAction(autoScrollingAction);
127 connect(autoScrollingAction, &QAction::toggled, this, &TreeViewContextMenu::setAutoScrolling);
128
129 if (!m_fileItem.isNull()) {
130 // insert 'Properties' entry
131 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
132 propertiesAction->setIcon(KIcon("document-properties"));
133 connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties);
134 popup->addAction(propertiesAction);
135 }
136
137 QList<QAction*> customActions = m_parent->customContextMenuActions();
138 if (!customActions.isEmpty()) {
139 popup->addSeparator();
140 foreach (QAction* action, customActions) {
141 popup->addAction(action);
142 }
143 }
144
145 QWeakPointer<KMenu> popupPtr = popup;
146 popup->exec(QCursor::pos());
147 if (popupPtr.data()) {
148 popupPtr.data()->deleteLater();
149 }
150 }
151
152 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
153 {
154 KUrl::List kdeUrls;
155 kdeUrls.append(m_fileItem.url());
156 KUrl::List mostLocalUrls;
157 bool dummy;
158 mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
159 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, cut);
160 }
161
162 void TreeViewContextMenu::cut()
163 {
164 QMimeData* mimeData = new QMimeData();
165 populateMimeData(mimeData, true);
166 QApplication::clipboard()->setMimeData(mimeData);
167 }
168
169 void TreeViewContextMenu::copy()
170 {
171 QMimeData* mimeData = new QMimeData();
172 populateMimeData(mimeData, false);
173 QApplication::clipboard()->setMimeData(mimeData);
174 }
175
176 void TreeViewContextMenu::paste()
177 {
178 KonqOperations::doPaste(m_parent, m_fileItem.url());
179 }
180
181 void TreeViewContextMenu::rename()
182 {
183 m_parent->rename(m_fileItem);
184 }
185
186 void TreeViewContextMenu::moveToTrash()
187 {
188 KonqOperations::del(m_parent, KonqOperations::TRASH, KUrl::List() << m_fileItem.url());
189 }
190
191 void TreeViewContextMenu::deleteItem()
192 {
193 KonqOperations::del(m_parent, KonqOperations::DEL, KUrl::List() << m_fileItem.url());
194 }
195
196 void TreeViewContextMenu::showProperties()
197 {
198 KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
199 dialog->setAttribute(Qt::WA_DeleteOnClose);
200 dialog->show();
201 }
202
203 void TreeViewContextMenu::setShowHiddenFiles(bool show)
204 {
205 m_parent->setShowHiddenFiles(show);
206 }
207
208 void TreeViewContextMenu::setAutoScrolling(bool enable)
209 {
210 m_parent->setAutoScrolling(enable);
211 }
212