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