]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
As requested by Peter: upgrade version to 1.0
[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 <kfileitem.h>
24 #include <kiconloader.h>
25 #include <kio/deletejob.h>
26 #include <kmenu.h>
27 #include <konqmimedata.h>
28 #include <konq_operations.h>
29 #include <klocale.h>
30 #include <kpropertiesdialog.h>
31
32 #include "renamedialog.h"
33
34 #include <QtGui/QApplication>
35 #include <QtGui/QClipboard>
36
37 TreeViewContextMenu::TreeViewContextMenu(QWidget* parent,
38 const KFileItem& fileInfo) :
39 m_parent(parent),
40 m_fileInfo(fileInfo)
41 {
42 }
43
44 TreeViewContextMenu::~TreeViewContextMenu()
45 {
46 }
47
48 void TreeViewContextMenu::open()
49 {
50 Q_ASSERT(!m_fileInfo.isNull());
51
52 KMenu* popup = new KMenu(m_parent);
53
54 // insert 'Cut', 'Copy' and 'Paste'
55 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
56 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
57
58 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
59 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
60
61 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
62 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
63 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
64 pasteAction->setEnabled(!pasteData.isEmpty());
65 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
66
67 popup->addAction(cutAction);
68 popup->addAction(copyAction);
69 popup->addAction(pasteAction);
70 popup->addSeparator();
71
72 // insert 'Rename'
73 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
74 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
75 popup->addAction(renameAction);
76
77 // insert 'Move to Trash' and (optionally) 'Delete'
78 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
79 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
80 const KUrl& url = m_fileInfo.url();
81 if (url.isLocalFile()) {
82 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
83 i18nc("@action:inmenu", "Move To Trash"), this);
84 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
85 popup->addAction(moveToTrashAction);
86 } else {
87 showDeleteCommand = true;
88 }
89
90 if (showDeleteCommand) {
91 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
92 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
93 popup->addAction(deleteAction);
94 }
95
96 popup->addSeparator();
97
98 // insert 'Properties' entry
99 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
100 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
101 popup->addAction(propertiesAction);
102
103 popup->exec(QCursor::pos());
104 popup->deleteLater();
105 }
106
107 void TreeViewContextMenu::cut()
108 {
109 QMimeData* mimeData = new QMimeData();
110 KUrl::List kdeUrls;
111 kdeUrls.append(m_fileInfo.url());
112 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), true);
113 QApplication::clipboard()->setMimeData(mimeData);
114 }
115
116 void TreeViewContextMenu::copy()
117 {
118 QMimeData* mimeData = new QMimeData();
119 KUrl::List kdeUrls;
120 kdeUrls.append(m_fileInfo.url());
121 KonqMimeData::populateMimeData(mimeData, kdeUrls, KUrl::List(), false);
122 QApplication::clipboard()->setMimeData(mimeData);
123 }
124
125 void TreeViewContextMenu::paste()
126 {
127 QClipboard* clipboard = QApplication::clipboard();
128 const QMimeData* mimeData = clipboard->mimeData();
129
130 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
131 const KUrl& dest = m_fileInfo.url();
132 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
133 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
134 clipboard->clear();
135 } else {
136 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
137 }
138 }
139
140 void TreeViewContextMenu::rename()
141 {
142 KFileItemList item;
143 item.append(m_fileInfo);
144 RenameDialog dialog(m_parent, item);
145 if (dialog.exec() == QDialog::Accepted) {
146 const QString& newName = dialog.newName();
147 if (!newName.isEmpty()) {
148 KUrl newUrl = m_fileInfo.url();
149 newUrl.setFileName(newName);
150 KonqOperations::rename(m_parent, m_fileInfo.url(), newUrl);
151 }
152 }
153 }
154
155 void TreeViewContextMenu::moveToTrash()
156 {
157 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo.url());
158 }
159
160 void TreeViewContextMenu::deleteItem()
161 {
162 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileInfo.url());
163 }
164
165 void TreeViewContextMenu::showProperties()
166 {
167 KPropertiesDialog dialog(m_fileInfo.url(), m_parent);
168 dialog.exec();
169 }
170
171 #include "treeviewcontextmenu.moc"