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