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