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