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