]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
disable rating, comments and tags if no meta data is available
[dolphin.git] / src / panels / folders / 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 "folderspanel.h"
36
37 #include <QtGui/QApplication>
38 #include <QtGui/QClipboard>
39
40 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* 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 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 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
113 popup->addAction(propertiesAction);
114
115 popup->addSeparator();
116 }
117
118 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
119 showHiddenFilesAction->setCheckable(true);
120 showHiddenFilesAction->setChecked(FoldersPanelSettings::showHiddenFiles());
121 popup->addAction(showHiddenFilesAction);
122
123 connect(showHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
124
125 popup->exec(QCursor::pos());
126 popup->deleteLater();
127 }
128
129 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
130 {
131 KUrl::List kdeUrls;
132 kdeUrls.append(m_fileInfo.url());
133 KUrl::List mostLocalUrls;
134 bool dummy;
135 mostLocalUrls.append(m_fileInfo.mostLocalUrl(dummy));
136 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, cut);
137 }
138
139 void TreeViewContextMenu::cut()
140 {
141 QMimeData* mimeData = new QMimeData();
142 populateMimeData(mimeData, true);
143 QApplication::clipboard()->setMimeData(mimeData);
144 }
145
146 void TreeViewContextMenu::copy()
147 {
148 QMimeData* mimeData = new QMimeData();
149 populateMimeData(mimeData, false);
150 QApplication::clipboard()->setMimeData(mimeData);
151 }
152
153 void TreeViewContextMenu::paste()
154 {
155 QClipboard* clipboard = QApplication::clipboard();
156 const QMimeData* mimeData = clipboard->mimeData();
157
158 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
159 const KUrl& dest = m_fileInfo.url();
160 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
161 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
162 clipboard->clear();
163 } else {
164 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
165 }
166 }
167
168 void TreeViewContextMenu::rename()
169 {
170 m_parent->rename(m_fileInfo);
171 }
172
173 void TreeViewContextMenu::moveToTrash()
174 {
175 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo.url());
176 }
177
178 void TreeViewContextMenu::deleteItem()
179 {
180 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileInfo.url());
181 }
182
183 void TreeViewContextMenu::showProperties()
184 {
185 KPropertiesDialog dialog(m_fileInfo.url(), m_parent);
186 dialog.exec();
187 }
188
189 void TreeViewContextMenu::setShowHiddenFiles(bool show)
190 {
191 m_parent->setShowHiddenFiles(show);
192 }
193
194 #include "treeviewcontextmenu.moc"