]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
respect the inline-renaming setting also for the Folder Panel (= treeview)
[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_operations.h>
31 #include <klocale.h>
32 #include <kpropertiesdialog.h>
33
34 #include "treeviewsidebarpage.h"
35
36 #include <QtGui/QApplication>
37 #include <QtGui/QClipboard>
38
39 TreeViewContextMenu::TreeViewContextMenu(TreeViewSidebarPage* 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 // insert 'Cut', 'Copy' and 'Paste'
57 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
58 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
59
60 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
61 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
62
63 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
64 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
65 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
66 pasteAction->setEnabled(!pasteData.isEmpty());
67 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
68
69 popup->addAction(cutAction);
70 popup->addAction(copyAction);
71 popup->addAction(pasteAction);
72 popup->addSeparator();
73
74 // insert 'Rename'
75 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
76 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
77 popup->addAction(renameAction);
78
79 // insert 'Move to Trash' and (optionally) 'Delete'
80 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
81 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
82 const KUrl& url = m_fileInfo.url();
83 if (url.isLocalFile()) {
84 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
85 i18nc("@action:inmenu", "Move To Trash"), this);
86 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
87 popup->addAction(moveToTrashAction);
88 } else {
89 showDeleteCommand = true;
90 }
91
92 if (showDeleteCommand) {
93 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
94 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
95 popup->addAction(deleteAction);
96 }
97
98 popup->addSeparator();
99
100 // insert 'Properties' entry
101 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
102 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
103 popup->addAction(propertiesAction);
104
105 popup->addSeparator();
106 }
107
108 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
109 showHiddenFilesAction->setCheckable(true);
110 showHiddenFilesAction->setChecked(FoldersPanelSettings::showHiddenFiles());
111 popup->addAction(showHiddenFilesAction);
112
113 connect(showHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
114
115 popup->exec(QCursor::pos());
116 popup->deleteLater();
117 }
118
119 void TreeViewContextMenu::cut()
120 {
121 QMimeData* mimeData = new QMimeData();
122 KUrl::List kdeUrls;
123 kdeUrls.append(m_fileInfo.url());
124 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), true);
125 QApplication::clipboard()->setMimeData(mimeData);
126 }
127
128 void TreeViewContextMenu::copy()
129 {
130 QMimeData* mimeData = new QMimeData();
131 KUrl::List kdeUrls;
132 kdeUrls.append(m_fileInfo.url());
133 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), false);
134 QApplication::clipboard()->setMimeData(mimeData);
135 }
136
137 void TreeViewContextMenu::paste()
138 {
139 QClipboard* clipboard = QApplication::clipboard();
140 const QMimeData* mimeData = clipboard->mimeData();
141
142 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
143 const KUrl& dest = m_fileInfo.url();
144 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
145 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
146 clipboard->clear();
147 } else {
148 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
149 }
150 }
151
152 void TreeViewContextMenu::rename()
153 {
154 m_parent->rename(m_fileInfo);
155 }
156
157 void TreeViewContextMenu::moveToTrash()
158 {
159 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo.url());
160 }
161
162 void TreeViewContextMenu::deleteItem()
163 {
164 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileInfo.url());
165 }
166
167 void TreeViewContextMenu::showProperties()
168 {
169 KPropertiesDialog dialog(m_fileInfo.url(), m_parent);
170 dialog.exec();
171 }
172
173 void TreeViewContextMenu::setShowHiddenFiles(bool show)
174 {
175 m_parent->setShowHiddenFiles(show);
176 }
177
178 #include "treeviewcontextmenu.moc"