]>
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/CopyJob>
26 #include <KIO/DeleteJob>
27 #include <KIO/JobUiDelegate>
30 #include <KJobWidgets>
31 #include <KSharedConfig>
32 #include <KConfigGroup>
33 #include <kurlmimedata.h>
34 #include <KFileItemListProperties>
35 #include <konq_operations.h>
38 #include <KIO/FileUndoManager>
39 #include <KPropertiesDialog>
41 #include "folderspanel.h"
43 #include <QApplication>
47 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel
* parent
,
48 const KFileItem
& fileInfo
) :
55 TreeViewContextMenu::~TreeViewContextMenu()
59 void TreeViewContextMenu::open()
61 KMenu
* popup
= new KMenu(m_parent
);
63 if (!m_fileItem
.isNull()) {
64 KFileItemListProperties
capabilities(KFileItemList() << m_fileItem
);
66 // insert 'Cut', 'Copy' and 'Paste'
67 QAction
* cutAction
= new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
68 cutAction
->setEnabled(capabilities
.supportsMoving());
69 connect(cutAction
, &QAction::triggered
, this, &TreeViewContextMenu::cut
);
71 QAction
* copyAction
= new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
72 connect(copyAction
, &QAction::triggered
, this, &TreeViewContextMenu::copy
);
74 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileItem
.url());
75 QAction
* pasteAction
= new QAction(KIcon("edit-paste"), pasteInfo
.second
, this);
76 connect(pasteAction
, &QAction::triggered
, this, &TreeViewContextMenu::paste
);
77 pasteAction
->setEnabled(pasteInfo
.first
);
79 popup
->addAction(cutAction
);
80 popup
->addAction(copyAction
);
81 popup
->addAction(pasteAction
);
82 popup
->addSeparator();
85 QAction
* renameAction
= new QAction(i18nc("@action:inmenu", "Rename..."), this);
86 renameAction
->setEnabled(capabilities
.supportsMoving());
87 renameAction
->setIcon(KIcon("edit-rename"));
88 connect(renameAction
, &QAction::triggered
, this, &TreeViewContextMenu::rename
);
89 popup
->addAction(renameAction
);
91 // insert 'Move to Trash' and (optionally) 'Delete'
92 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
93 KConfigGroup
configGroup(globalConfig
, "KDE");
94 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
96 const KUrl url
= m_fileItem
.url();
97 if (url
.isLocalFile()) {
98 QAction
* moveToTrashAction
= new QAction(KIcon("user-trash"),
99 i18nc("@action:inmenu", "Move to Trash"), this);
100 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
101 moveToTrashAction
->setEnabled(enableMoveToTrash
);
102 connect(moveToTrashAction
, &QAction::triggered
, this, &TreeViewContextMenu::moveToTrash
);
103 popup
->addAction(moveToTrashAction
);
105 showDeleteCommand
= true;
108 if (showDeleteCommand
) {
109 QAction
* deleteAction
= new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
110 deleteAction
->setEnabled(capabilities
.supportsDeleting());
111 connect(deleteAction
, &QAction::triggered
, this, &TreeViewContextMenu::deleteItem
);
112 popup
->addAction(deleteAction
);
115 popup
->addSeparator();
118 // insert 'Show Hidden Files'
119 QAction
* showHiddenFilesAction
= new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
120 showHiddenFilesAction
->setCheckable(true);
121 showHiddenFilesAction
->setChecked(m_parent
->showHiddenFiles());
122 popup
->addAction(showHiddenFilesAction
);
123 connect(showHiddenFilesAction
, &QAction::toggled
, this, &TreeViewContextMenu::setShowHiddenFiles
);
125 // insert 'Automatic Scrolling'
126 QAction
* autoScrollingAction
= new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
127 autoScrollingAction
->setCheckable(true);
128 autoScrollingAction
->setChecked(m_parent
->autoScrolling());
129 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
130 // in KItemViews or manually as part of the FoldersPanel
131 //popup->addAction(autoScrollingAction);
132 connect(autoScrollingAction
, &QAction::toggled
, this, &TreeViewContextMenu::setAutoScrolling
);
134 if (!m_fileItem
.isNull()) {
135 // insert 'Properties' entry
136 QAction
* propertiesAction
= new QAction(i18nc("@action:inmenu", "Properties"), this);
137 propertiesAction
->setIcon(KIcon("document-properties"));
138 connect(propertiesAction
, &QAction::triggered
, this, &TreeViewContextMenu::showProperties
);
139 popup
->addAction(propertiesAction
);
142 QList
<QAction
*> customActions
= m_parent
->customContextMenuActions();
143 if (!customActions
.isEmpty()) {
144 popup
->addSeparator();
145 foreach (QAction
* action
, customActions
) {
146 popup
->addAction(action
);
150 QWeakPointer
<KMenu
> popupPtr
= popup
;
151 popup
->exec(QCursor::pos());
152 if (popupPtr
.data()) {
153 popupPtr
.data()->deleteLater();
157 void TreeViewContextMenu::populateMimeData(QMimeData
* mimeData
, bool cut
)
160 kdeUrls
.append(m_fileItem
.url());
161 KUrl::List mostLocalUrls
;
163 mostLocalUrls
.append(m_fileItem
.mostLocalUrl(dummy
));
164 KIO::setClipboardDataCut(mimeData
, cut
);
165 KUrlMimeData::setUrls(kdeUrls
, mostLocalUrls
, mimeData
);
168 void TreeViewContextMenu::cut()
170 QMimeData
* mimeData
= new QMimeData();
171 populateMimeData(mimeData
, true);
172 QApplication::clipboard()->setMimeData(mimeData
);
175 void TreeViewContextMenu::copy()
177 QMimeData
* mimeData
= new QMimeData();
178 populateMimeData(mimeData
, false);
179 QApplication::clipboard()->setMimeData(mimeData
);
182 void TreeViewContextMenu::paste()
184 KonqOperations::doPaste(m_parent
, m_fileItem
.url());
187 void TreeViewContextMenu::rename()
189 m_parent
->rename(m_fileItem
);
192 void TreeViewContextMenu::moveToTrash()
194 KUrl::List list
= KUrl::List() << m_fileItem
.url();
195 KIO::JobUiDelegate uiDelegate
;
196 uiDelegate
.setWindow(m_parent
);
197 if (uiDelegate
.askDeleteConfirmation(list
, KIO::JobUiDelegate::Trash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
198 KIO::Job
* job
= KIO::trash(list
);
199 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash
, list
, KUrl("trash:/"), job
);
200 KJobWidgets::setWindow(job
, m_parent
);
201 job
->ui()->setAutoErrorHandlingEnabled(true);
205 void TreeViewContextMenu::deleteItem()
207 KUrl::List list
= KUrl::List() << m_fileItem
.url();
208 KIO::JobUiDelegate uiDelegate
;
209 uiDelegate
.setWindow(m_parent
);
210 if (uiDelegate
.askDeleteConfirmation(list
, KIO::JobUiDelegate::Delete
, KIO::JobUiDelegate::DefaultConfirmation
)) {
211 KIO::Job
* job
= KIO::del(list
);
212 KJobWidgets::setWindow(job
, m_parent
);
213 job
->ui()->setAutoErrorHandlingEnabled(true);
217 void TreeViewContextMenu::showProperties()
219 KPropertiesDialog
* dialog
= new KPropertiesDialog(m_fileItem
.url(), m_parent
);
220 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
224 void TreeViewContextMenu::setShowHiddenFiles(bool show
)
226 m_parent
->setShowHiddenFiles(show
);
229 void TreeViewContextMenu::setAutoScrolling(bool enable
)
231 m_parent
->setAutoScrolling(enable
);