add more spacing
[personal-kdebase.git] / apps / dolphin / src / panels / folders / treeviewcontextmenu.cpp
blobc63772a389e4603fab7d0183a59bdfd3ae816e2d
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 ***************************************************************************/
21 #include "treeviewcontextmenu.h"
23 #include "dolphin_folderspanelsettings.h"
25 #include <kfileitem.h>
26 #include <kiconloader.h>
27 #include <kio/deletejob.h>
28 #include <kmenu.h>
29 #include <konqmimedata.h>
30 #include <konq_fileitemcapabilities.h>
31 #include <konq_operations.h>
32 #include <klocale.h>
33 #include <kpropertiesdialog.h>
35 #include "folderspanel.h"
37 #include <QtGui/QApplication>
38 #include <QtGui/QClipboard>
40 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
41 const KFileItem& fileInfo) :
42 QObject(parent),
43 m_parent(parent),
44 m_fileInfo(fileInfo)
48 TreeViewContextMenu::~TreeViewContextMenu()
52 void TreeViewContextMenu::open()
54 KMenu* popup = new KMenu(m_parent);
56 if (!m_fileInfo.isNull()) {
57 KonqFileItemCapabilities capabilities(KFileItemList() << m_fileInfo);
59 // insert 'Cut', 'Copy' and 'Paste'
60 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
61 cutAction->setEnabled(capabilities.supportsMoving());
62 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
64 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
65 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
67 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
68 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
69 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
70 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
71 pasteAction->setEnabled(!pasteData.isEmpty() && capabilities.supportsWriting());
73 popup->addAction(cutAction);
74 popup->addAction(copyAction);
75 popup->addAction(pasteAction);
76 popup->addSeparator();
78 // insert 'Rename'
79 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
80 renameAction->setEnabled(capabilities.supportsMoving());
81 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
82 popup->addAction(renameAction);
84 // insert 'Move to Trash' and (optionally) 'Delete'
85 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
86 KConfigGroup configGroup(globalConfig, "KDE");
87 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
89 const KUrl& url = m_fileInfo.url();
90 if (url.isLocalFile()) {
91 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
92 i18nc("@action:inmenu", "Move To Trash"), this);
93 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
94 moveToTrashAction->setEnabled(enableMoveToTrash);
95 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
96 popup->addAction(moveToTrashAction);
97 } else {
98 showDeleteCommand = true;
101 if (showDeleteCommand) {
102 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
103 deleteAction->setEnabled(capabilities.supportsDeleting());
104 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
105 popup->addAction(deleteAction);
108 popup->addSeparator();
110 // insert 'Properties' entry
111 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
112 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
113 popup->addAction(propertiesAction);
115 popup->addSeparator();
118 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
119 showHiddenFilesAction->setCheckable(true);
120 showHiddenFilesAction->setChecked(FoldersPanelSettings::showHiddenFiles());
121 popup->addAction(showHiddenFilesAction);
123 connect(showHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
125 popup->exec(QCursor::pos());
126 popup->deleteLater();
129 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
131 KUrl::List kdeUrls;
132 kdeUrls.append(m_fileInfo.url());
133 KUrl::List mostLocalUrls;
134 bool dummy;
135 mostLocalUrls.append(m_fileInfo.mostLocalUrl(dummy));
136 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, cut);
139 void TreeViewContextMenu::cut()
141 QMimeData* mimeData = new QMimeData();
142 populateMimeData(mimeData, true);
143 QApplication::clipboard()->setMimeData(mimeData);
146 void TreeViewContextMenu::copy()
148 QMimeData* mimeData = new QMimeData();
149 populateMimeData(mimeData, false);
150 QApplication::clipboard()->setMimeData(mimeData);
153 void TreeViewContextMenu::paste()
155 QClipboard* clipboard = QApplication::clipboard();
156 const QMimeData* mimeData = clipboard->mimeData();
158 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
159 const KUrl& dest = m_fileInfo.url();
160 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
161 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
162 clipboard->clear();
163 } else {
164 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
168 void TreeViewContextMenu::rename()
170 m_parent->rename(m_fileInfo);
173 void TreeViewContextMenu::moveToTrash()
175 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileInfo.url());
178 void TreeViewContextMenu::deleteItem()
180 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileInfo.url());
183 void TreeViewContextMenu::showProperties()
185 KPropertiesDialog dialog(m_fileInfo.url(), m_parent);
186 dialog.exec();
189 void TreeViewContextMenu::setShowHiddenFiles(bool show)
191 m_parent->setShowHiddenFiles(show);
194 #include "treeviewcontextmenu.moc"