]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
Implemented (QTabBar based) DolphinTabBar class to encapsulate the tab bar handling...
[dolphin.git] / 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> *
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 <KFileItem>
24 #include <KIconLoader>
25 #include <KIO/DeleteJob>
26 #include <KMenu>
27 #include <konqmimedata.h>
28 #include <KFileItemListProperties>
29 #include <konq_operations.h>
30 #include <KLocale>
31 #include <KPropertiesDialog>
32
33 #include "folderspanel.h"
34
35 #include <QApplication>
36 #include <QClipboard>
37
38 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
39 const KFileItem& fileInfo) :
40 QObject(parent),
41 m_parent(parent),
42 m_fileItem(fileInfo)
43 {
44 }
45
46 TreeViewContextMenu::~TreeViewContextMenu()
47 {
48 }
49
50 void TreeViewContextMenu::open()
51 {
52 KMenu* popup = new KMenu(m_parent);
53
54 if (!m_fileItem.isNull()) {
55 KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
56
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()));
61
62 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
63 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
64
65 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileItem.url());
66 QAction* pasteAction = new QAction(KIcon("edit-paste"), pasteInfo.second, this);
67 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
68 pasteAction->setEnabled(pasteInfo.first);
69
70 popup->addAction(cutAction);
71 popup->addAction(copyAction);
72 popup->addAction(pasteAction);
73 popup->addSeparator();
74
75 // insert 'Rename'
76 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
77 renameAction->setEnabled(capabilities.supportsMoving());
78 renameAction->setIcon(KIcon("edit-rename"));
79 connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
80 popup->addAction(renameAction);
81
82 // insert 'Move to Trash' and (optionally) 'Delete'
83 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
84 KConfigGroup configGroup(globalConfig, "KDE");
85 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
86
87 const KUrl url = m_fileItem.url();
88 if (url.isLocalFile()) {
89 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
90 i18nc("@action:inmenu", "Move to Trash"), this);
91 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
92 moveToTrashAction->setEnabled(enableMoveToTrash);
93 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
94 popup->addAction(moveToTrashAction);
95 } else {
96 showDeleteCommand = true;
97 }
98
99 if (showDeleteCommand) {
100 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
101 deleteAction->setEnabled(capabilities.supportsDeleting());
102 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
103 popup->addAction(deleteAction);
104 }
105
106 popup->addSeparator();
107 }
108
109 // insert 'Show Hidden Files'
110 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
111 showHiddenFilesAction->setCheckable(true);
112 showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
113 popup->addAction(showHiddenFilesAction);
114 connect(showHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
115
116 // insert 'Automatic Scrolling'
117 QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
118 autoScrollingAction->setCheckable(true);
119 autoScrollingAction->setChecked(m_parent->autoScrolling());
120 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
121 // in KItemViews or manually as part of the FoldersPanel
122 //popup->addAction(autoScrollingAction);
123 connect(autoScrollingAction, SIGNAL(toggled(bool)), this, SLOT(setAutoScrolling(bool)));
124
125 if (!m_fileItem.isNull()) {
126 // insert 'Properties' entry
127 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
128 propertiesAction->setIcon(KIcon("document-properties"));
129 connect(propertiesAction, SIGNAL(triggered()), this, SLOT(showProperties()));
130 popup->addAction(propertiesAction);
131 }
132
133 QList<QAction*> customActions = m_parent->customContextMenuActions();
134 if (!customActions.isEmpty()) {
135 popup->addSeparator();
136 foreach (QAction* action, customActions) {
137 popup->addAction(action);
138 }
139 }
140
141 QWeakPointer<KMenu> popupPtr = popup;
142 popup->exec(QCursor::pos());
143 if (popupPtr.data()) {
144 popupPtr.data()->deleteLater();
145 }
146 }
147
148 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
149 {
150 KUrl::List kdeUrls;
151 kdeUrls.append(m_fileItem.url());
152 KUrl::List mostLocalUrls;
153 bool dummy;
154 mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
155 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, cut);
156 }
157
158 void TreeViewContextMenu::cut()
159 {
160 QMimeData* mimeData = new QMimeData();
161 populateMimeData(mimeData, true);
162 QApplication::clipboard()->setMimeData(mimeData);
163 }
164
165 void TreeViewContextMenu::copy()
166 {
167 QMimeData* mimeData = new QMimeData();
168 populateMimeData(mimeData, false);
169 QApplication::clipboard()->setMimeData(mimeData);
170 }
171
172 void TreeViewContextMenu::paste()
173 {
174 KonqOperations::doPaste(m_parent, m_fileItem.url());
175 }
176
177 void TreeViewContextMenu::rename()
178 {
179 m_parent->rename(m_fileItem);
180 }
181
182 void TreeViewContextMenu::moveToTrash()
183 {
184 KonqOperations::del(m_parent, KonqOperations::TRASH, m_fileItem.url());
185 }
186
187 void TreeViewContextMenu::deleteItem()
188 {
189 KonqOperations::del(m_parent, KonqOperations::DEL, m_fileItem.url());
190 }
191
192 void TreeViewContextMenu::showProperties()
193 {
194 KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
195 dialog->setAttribute(Qt::WA_DeleteOnClose);
196 dialog->show();
197 }
198
199 void TreeViewContextMenu::setShowHiddenFiles(bool show)
200 {
201 m_parent->setShowHiddenFiles(show);
202 }
203
204 void TreeViewContextMenu::setAutoScrolling(bool enable)
205 {
206 m_parent->setAutoScrolling(enable);
207 }
208
209 #include "treeviewcontextmenu.moc"