]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
Assure that the file capabilities are respected when opening the context menu for...
[dolphin.git] / src / treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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 "dolphin_folderspanelsettings.h"
24
25 #include <kfileitem.h>
26 #include <kiconloader.h>
27 #include <kio/deletejob.h>
28 #include <kmenu.h>
29 #include <konqmimedata.h>
30 #include <konq_fileitemcapabilities.h>
31 #include <konq_operations.h>
32 #include <klocale.h>
33 #include <kpropertiesdialog.h>
34
35 #include "treeviewsidebarpage.h"
36
37 #include <QtGui/QApplication>
38 #include <QtGui/QClipboard>
39
40 TreeViewContextMenu::TreeViewContextMenu(TreeViewSidebarPage* parent,
41 const KFileItem& fileInfo) :
42 QObject(parent),
43 m_parent(parent),
44 m_fileInfo(fileInfo)
45 {
46 }
47
48 TreeViewContextMenu::~TreeViewContextMenu()
49 {
50 }
51
52 void TreeViewContextMenu::open()
53 {
54 KMenu* popup = new KMenu(m_parent);
55
56 if (!m_fileInfo.isNull()) {
57 KonqFileItemCapabilities capabilities(KFileItemList() << m_fileInfo);
58
59 // insert 'Cut', 'Copy' and 'Paste'
60 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
61 cutAction->setEnabled(capabilities.supportsMoving());
62 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
63
64 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
65 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
66
67 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
68 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
69 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
70 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
71 pasteAction->setEnabled(!pasteData.isEmpty() && capabilities.supportsWriting());
72
73 popup->addAction(cutAction);
74 popup->addAction(copyAction);
75 popup->addAction(pasteAction);
76 popup->addSeparator();
77
78 // insert 'Rename'
79 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
80 renameAction->setEnabled(capabilities.supportsMoving());
81 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
82 popup->addAction(renameAction);
83
84 // insert 'Move to Trash' and (optionally) 'Delete'
85 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
86 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
87 const KUrl& url = m_fileInfo.url();
88 if (url.isLocalFile()) {
89 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
90 i18nc("@action:inmenu", "Move To Trash"), this);
91 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
92 moveToTrashAction->setEnabled(enableMoveToTrash);
93 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
94 popup->addAction(moveToTrashAction);
95 } else {
96 showDeleteCommand = true;
97 }
98
99 if (showDeleteCommand) {
100 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
101 deleteAction->setEnabled(capabilities.supportsDeleting());
102 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
103 popup->addAction(deleteAction);
104 }
105
106 popup->addSeparator();
107
108 // insert 'Properties' entry
109 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
110 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
111 popup->addAction(propertiesAction);
112
113 popup->addSeparator();
114 }
115
116 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
117 showHiddenFilesAction->setCheckable(true);
118 showHiddenFilesAction->setChecked(FoldersPanelSettings::showHiddenFiles());
119 popup->addAction(showHiddenFilesAction);
120
121 connect(showHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
122
123 popup->exec(QCursor::pos());
124 popup->deleteLater();
125 }
126
127 void TreeViewContextMenu::cut()
128 {
129 QMimeData* mimeData = new QMimeData();
130 KUrl::List kdeUrls;
131 kdeUrls.append(m_fileInfo.url());
132 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), true);
133 QApplication::clipboard()->setMimeData(mimeData);
134 }
135
136 void TreeViewContextMenu::copy()
137 {
138 QMimeData* mimeData = new QMimeData();
139 KUrl::List kdeUrls;
140 kdeUrls.append(m_fileInfo.url());
141 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), false);
142 QApplication::clipboard()->setMimeData(mimeData);
143 }
144
145 void TreeViewContextMenu::paste()
146 {
147 QClipboard* clipboard = QApplication::clipboard();
148 const QMimeData* mimeData = clipboard->mimeData();
149
150 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
151 const KUrl& dest = m_fileInfo.url();
152 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
153 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
154 clipboard->clear();
155 } else {
156 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
157 }
158 }
159
160 void TreeViewContextMenu::rename()
161 {
162 m_parent->rename(m_fileInfo);
163 }
164
165 void TreeViewContextMenu::moveToTrash()
166 {
167 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo.url());
168 }
169
170 void TreeViewContextMenu::deleteItem()
171 {
172 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileInfo.url());
173 }
174
175 void TreeViewContextMenu::showProperties()
176 {
177 KPropertiesDialog dialog(m_fileInfo.url(), m_parent);
178 dialog.exec();
179 }
180
181 void TreeViewContextMenu::setShowHiddenFiles(bool show)
182 {
183 m_parent->setShowHiddenFiles(show);
184 }
185
186 #include "treeviewcontextmenu.moc"