]>
cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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. *
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. *
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 ***************************************************************************/
21 #include "treeviewcontextmenu.h"
23 #include <kfileitem.h>
24 #include <kiconloader.h>
25 #include <kio/deletejob.h>
27 #include <konqmimedata.h>
28 #include <konq_operations.h>
30 #include <kpropertiesdialog.h>
32 #include "renamedialog.h"
34 #include <QtGui/QApplication>
35 #include <QtGui/QClipboard>
37 TreeViewContextMenu::TreeViewContextMenu(QWidget
* parent
,
38 const KFileItem
& fileInfo
) :
44 TreeViewContextMenu::~TreeViewContextMenu()
48 void TreeViewContextMenu::open()
50 Q_ASSERT(!m_fileInfo
.isNull());
52 KMenu
* popup
= new KMenu(m_parent
);
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()));
58 QAction
* copyAction
= new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
59 connect(copyAction
, SIGNAL(triggered()), this, SLOT(copy()));
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()));
67 popup
->addAction(cutAction
);
68 popup
->addAction(copyAction
);
69 popup
->addAction(pasteAction
);
70 popup
->addSeparator();
73 QAction
* renameAction
= new QAction(i18nc("@action:inmenu", "Rename..."), this);
74 connect(renameAction
, SIGNAL(triggered()), this, SLOT(rename()));
75 popup
->addAction(renameAction
);
77 // insert 'Move to Trash' and (optionally) 'Delete'
78 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
79 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
80 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
81 const KUrl
& url
= m_fileInfo
.url();
82 if (url
.isLocalFile()) {
83 QAction
* moveToTrashAction
= new QAction(KIcon("edit-trash"),
84 i18nc("@action:inmenu", "Move To Trash"), this);
85 connect(moveToTrashAction
, SIGNAL(triggered()), this, SLOT(moveToTrash()));
86 popup
->addAction(moveToTrashAction
);
88 showDeleteCommand
= true;
91 if (showDeleteCommand
) {
92 QAction
* deleteAction
= new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
93 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(deleteItem()));
94 popup
->addAction(deleteAction
);
97 popup
->addSeparator();
99 // insert 'Properties' entry
100 QAction
* propertiesAction
= new QAction(i18nc("@action:inmenu", "Properties"), this);
101 connect(propertiesAction
, SIGNAL(triggered()), this, SLOT(showProperties()));
102 popup
->addAction(propertiesAction
);
104 popup
->exec(QCursor::pos());
105 popup
->deleteLater();
108 void TreeViewContextMenu::cut()
110 QMimeData
* mimeData
= new QMimeData();
112 kdeUrls
.append(m_fileInfo
.url());
113 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, KUrl::List(), true);
114 QApplication::clipboard()->setMimeData(mimeData
);
117 void TreeViewContextMenu::copy()
119 QMimeData
* mimeData
= new QMimeData();
121 kdeUrls
.append(m_fileInfo
.url());
122 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, KUrl::List(), false);
123 QApplication::clipboard()->setMimeData(mimeData
);
126 void TreeViewContextMenu::paste()
128 QClipboard
* clipboard
= QApplication::clipboard();
129 const QMimeData
* mimeData
= clipboard
->mimeData();
131 const KUrl::List source
= KUrl::List::fromMimeData(mimeData
);
132 const KUrl
& dest
= m_fileInfo
.url();
133 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
134 KonqOperations::copy(m_parent
, KonqOperations::MOVE
, source
, dest
);
137 KonqOperations::copy(m_parent
, KonqOperations::COPY
, source
, dest
);
141 void TreeViewContextMenu::rename()
144 item
.append(m_fileInfo
);
145 RenameDialog
dialog(item
);
146 if (dialog
.exec() == QDialog::Accepted
) {
147 const QString
& newName
= dialog
.newName();
148 if (!newName
.isEmpty()) {
149 KUrl newUrl
= m_fileInfo
.url();
150 newUrl
.setFileName(newName
);
151 KonqOperations::rename(m_parent
, m_fileInfo
.url(), newUrl
);
156 void TreeViewContextMenu::moveToTrash()
158 KonqOperations::del(m_parent
, KonqOperations::TRASH
, m_fileInfo
.url());
161 void TreeViewContextMenu::deleteItem()
163 KonqOperations::del(m_parent
, KonqOperations::DEL
, m_fileInfo
.url());
166 void TreeViewContextMenu::showProperties()
168 KPropertiesDialog
dialog(m_fileInfo
.url());
172 #include "treeviewcontextmenu.moc"