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