]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
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"
24 #include <KIconLoader>
25 #include <KIO/DeleteJob>
27 #include <konqmimedata.h>
28 #include <KFileItemListProperties>
29 #include <konq_operations.h>
31 #include <KPropertiesDialog>
33 #include "folderspanel.h"
35 #include <QApplication>
38 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel
* parent
,
39 const KFileItem
& fileInfo
) :
46 TreeViewContextMenu::~TreeViewContextMenu()
50 void TreeViewContextMenu::open()
52 KMenu
* popup
= new KMenu(m_parent
);
54 if (!m_fileItem
.isNull()) {
55 KFileItemListProperties
capabilities(KFileItemList() << m_fileItem
);
57 // insert 'Cut', 'Copy' and 'Paste'
58 QAction
* cutAction
= new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
59 cutAction
->setEnabled(capabilities
.supportsMoving());
60 connect(cutAction
, SIGNAL(triggered()), this, SLOT(cut()));
62 QAction
* copyAction
= new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
63 connect(copyAction
, SIGNAL(triggered()), this, SLOT(copy()));
65 QAction
* pasteAction
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
66 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
67 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
68 connect(pasteAction
, SIGNAL(triggered()), this, SLOT(paste()));
69 pasteAction
->setEnabled(!pasteData
.isEmpty() && capabilities
.supportsWriting());
71 popup
->addAction(cutAction
);
72 popup
->addAction(copyAction
);
73 popup
->addAction(pasteAction
);
74 popup
->addSeparator();
77 QAction
* renameAction
= new QAction(i18nc("@action:inmenu", "Rename..."), this);
78 renameAction
->setEnabled(capabilities
.supportsMoving());
79 renameAction
->setIcon(KIcon("edit-rename"));
80 connect(renameAction
, SIGNAL(triggered()), this, SLOT(rename()));
81 popup
->addAction(renameAction
);
83 // insert 'Move to Trash' and (optionally) 'Delete'
84 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
85 KConfigGroup
configGroup(globalConfig
, "KDE");
86 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
88 const KUrl url
= m_fileItem
.url();
89 if (url
.isLocalFile()) {
90 QAction
* moveToTrashAction
= new QAction(KIcon("user-trash"),
91 i18nc("@action:inmenu", "Move to Trash"), this);
92 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
93 moveToTrashAction
->setEnabled(enableMoveToTrash
);
94 connect(moveToTrashAction
, SIGNAL(triggered()), this, SLOT(moveToTrash()));
95 popup
->addAction(moveToTrashAction
);
97 showDeleteCommand
= true;
100 if (showDeleteCommand
) {
101 QAction
* deleteAction
= new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
102 deleteAction
->setEnabled(capabilities
.supportsDeleting());
103 connect(deleteAction
, SIGNAL(triggered()), this, SLOT(deleteItem()));
104 popup
->addAction(deleteAction
);
107 popup
->addSeparator();
110 // insert 'Show Hidden Files'
111 QAction
* showHiddenFilesAction
= new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
112 showHiddenFilesAction
->setCheckable(true);
113 showHiddenFilesAction
->setChecked(m_parent
->showHiddenFiles());
114 popup
->addAction(showHiddenFilesAction
);
115 connect(showHiddenFilesAction
, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
117 // insert 'Automatic Scrolling'
118 QAction
* autoScrollingAction
= new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
119 autoScrollingAction
->setCheckable(true);
120 autoScrollingAction
->setChecked(m_parent
->autoScrolling());
121 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
122 // in KItemViews or manually as part of the FoldersPanel
123 //popup->addAction(autoScrollingAction);
124 connect(autoScrollingAction
, SIGNAL(toggled(bool)), this, SLOT(setAutoScrolling(bool)));
126 if (!m_fileItem
.isNull()) {
127 // insert 'Properties' entry
128 QAction
* propertiesAction
= new QAction(i18nc("@action:inmenu", "Properties"), this);
129 propertiesAction
->setIcon(KIcon("document-properties"));
130 connect(propertiesAction
, SIGNAL(triggered()), this, SLOT(showProperties()));
131 popup
->addAction(propertiesAction
);
134 QList
<QAction
*> customActions
= m_parent
->customContextMenuActions();
135 if (!customActions
.isEmpty()) {
136 popup
->addSeparator();
137 foreach (QAction
* action
, customActions
) {
138 popup
->addAction(action
);
142 QWeakPointer
<KMenu
> popupPtr
= popup
;
143 popup
->exec(QCursor::pos());
144 if (popupPtr
.data()) {
145 popupPtr
.data()->deleteLater();
149 void TreeViewContextMenu::populateMimeData(QMimeData
* mimeData
, bool cut
)
152 kdeUrls
.append(m_fileItem
.url());
153 KUrl::List mostLocalUrls
;
155 mostLocalUrls
.append(m_fileItem
.mostLocalUrl(dummy
));
156 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, cut
);
159 void TreeViewContextMenu::cut()
161 QMimeData
* mimeData
= new QMimeData();
162 populateMimeData(mimeData
, true);
163 QApplication::clipboard()->setMimeData(mimeData
);
166 void TreeViewContextMenu::copy()
168 QMimeData
* mimeData
= new QMimeData();
169 populateMimeData(mimeData
, false);
170 QApplication::clipboard()->setMimeData(mimeData
);
173 void TreeViewContextMenu::paste()
175 QClipboard
* clipboard
= QApplication::clipboard();
176 const QMimeData
* mimeData
= clipboard
->mimeData();
178 const KUrl::List source
= KUrl::List::fromMimeData(mimeData
);
179 const KUrl
& dest
= m_fileItem
.url();
180 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
181 KonqOperations::copy(m_parent
, KonqOperations::MOVE
, source
, dest
);
184 KonqOperations::copy(m_parent
, KonqOperations::COPY
, source
, dest
);
188 void TreeViewContextMenu::rename()
190 m_parent
->rename(m_fileItem
);
193 void TreeViewContextMenu::moveToTrash()
195 KonqOperations::del(m_parent
, KonqOperations::TRASH
, m_fileItem
.url());
198 void TreeViewContextMenu::deleteItem()
200 KonqOperations::del(m_parent
, KonqOperations::DEL
, m_fileItem
.url());
203 void TreeViewContextMenu::showProperties()
205 KPropertiesDialog
* dialog
= new KPropertiesDialog(m_fileItem
.url(), m_parent
);
206 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
210 void TreeViewContextMenu::setShowHiddenFiles(bool show
)
212 m_parent
->setShowHiddenFiles(show
);
215 void TreeViewContextMenu::setAutoScrolling(bool enable
)
217 m_parent
->setAutoScrolling(enable
);
220 #include "treeviewcontextmenu.moc"