]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Disable additional properties per default as discussed with Sebastian TrĂ¼g.
[dolphin.git] / src / dolphincontextmenu.cpp
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 ***************************************************************************/
20
21 #include "dolphincontextmenu.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinnewmenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinview.h"
27 #include "dolphinviewcontainer.h"
28 #include "dolphin_generalsettings.h"
29
30 #include <kactioncollection.h>
31 #include <kdesktopfile.h>
32 #include <kfileitemlistproperties.h>
33 #include <kfileplacesmodel.h>
34 #include <kglobal.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
37 #include <kmenu.h>
38 #include <kmenubar.h>
39 #include <kmessagebox.h>
40 #include <kmimetypetrader.h>
41 #include <knewmenu.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <konq_menuactions.h>
45 #include <klocale.h>
46 #include <kpropertiesdialog.h>
47 #include <krun.h>
48 #include <kstandardaction.h>
49 #include <kstandarddirs.h>
50
51 #include <QtGui/QApplication>
52 #include <QtGui/QClipboard>
53 #include <QtCore/QDir>
54
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
56 const KFileItem& fileInfo,
57 const KUrl& baseUrl) :
58 m_mainWindow(parent),
59 m_capabilities(0),
60 m_fileInfo(fileInfo),
61 m_baseUrl(baseUrl),
62 m_context(NoContext),
63 m_copyToMenu(parent),
64 m_customActions()
65 {
66 // The context menu either accesses the URLs of the selected items
67 // or the items itself. To increase the performance both lists are cached.
68 DolphinView* view = m_mainWindow->activeViewContainer()->view();
69 m_selectedUrls = view->selectedUrls();
70 m_selectedItems = view->selectedItems();
71 }
72
73 DolphinContextMenu::~DolphinContextMenu()
74 {
75 delete m_capabilities;
76 m_capabilities = 0;
77 }
78
79 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
80 {
81 m_customActions = actions;
82 }
83
84 void DolphinContextMenu::open()
85 {
86 // get the context information
87 if (m_baseUrl.protocol() == "trash") {
88 m_context |= TrashContext;
89 }
90
91 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
92 m_context |= ItemContext;
93 // TODO: handle other use cases like devices + desktop files
94 }
95
96 // open the corresponding popup for the context
97 if (m_context & TrashContext) {
98 if (m_context & ItemContext) {
99 openTrashItemContextMenu();
100 } else {
101 openTrashContextMenu();
102 }
103 } else if (m_context & ItemContext) {
104 openItemContextMenu();
105 } else {
106 Q_ASSERT(m_context == NoContext);
107 openViewportContextMenu();
108 }
109 }
110
111 void DolphinContextMenu::openTrashContextMenu()
112 {
113 Q_ASSERT(m_context & TrashContext);
114
115 KMenu* popup = new KMenu(m_mainWindow);
116
117 addShowMenubarAction(popup);
118
119 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup);
120 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
121 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
122 popup->addAction(emptyTrashAction);
123
124 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
125 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
126
127 addCustomActions(popup);
128
129 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
130 popup->addAction(propertiesAction);
131
132 QAction *action = popup->exec(QCursor::pos());
133 if (action == emptyTrashAction) {
134 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
135 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
136 text,
137 QString(),
138 KGuiItem(i18nc("@action:button", "Empty Trash"),
139 KIcon("user-trash"))
140 ) == KMessageBox::Continue;
141 if (del) {
142 KonqOperations::emptyTrash(m_mainWindow);
143 }
144 } else if (action == addToPlacesAction) {
145 const KUrl& url = m_mainWindow->activeViewContainer()->url();
146 if (url.isValid()) {
147 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
148 }
149 }
150
151 popup->deleteLater();
152 }
153
154 void DolphinContextMenu::openTrashItemContextMenu()
155 {
156 Q_ASSERT(m_context & TrashContext);
157 Q_ASSERT(m_context & ItemContext);
158
159 KMenu* popup = new KMenu(m_mainWindow);
160
161 addShowMenubarAction(popup);
162
163 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
164 popup->addAction(restoreAction);
165
166 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
167 popup->addAction(deleteAction);
168
169 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
170 popup->addAction(propertiesAction);
171
172 if (popup->exec(QCursor::pos()) == restoreAction) {
173 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
174 }
175
176 popup->deleteLater();
177 }
178
179 void DolphinContextMenu::openItemContextMenu()
180 {
181 Q_ASSERT(!m_fileInfo.isNull());
182
183 KMenu* popup = new KMenu(m_mainWindow);
184 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
185 // setup 'Create New' menu
186 DolphinNewMenu* newMenu = new DolphinNewMenu(popup, m_mainWindow);
187 newMenu->slotCheckUpToDate();
188 newMenu->setPopupFiles(m_fileInfo.url());
189 newMenu->setEnabled(capabilities().supportsWriting());
190
191 KMenu* menu = newMenu->menu();
192 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
193 menu->setIcon(KIcon("document-new"));
194 popup->addMenu(newMenu->menu());
195 popup->addSeparator();
196
197 // insert 'Open in new window' and 'Open in new tab' entries
198 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
199 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
200 popup->addSeparator();
201 }
202 addShowMenubarAction(popup);
203 insertDefaultItemActions(popup);
204
205 popup->addSeparator();
206
207 // insert 'Bookmark This Folder' entry if exactly one item is selected
208 QAction* addToPlacesAction = 0;
209 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
210 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
211 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
212 }
213
214 KonqMenuActions menuActions;
215 menuActions.setParentWidget(m_mainWindow);
216 menuActions.setItemListProperties(m_selectedItems);
217
218 // insert 'Open With...' action or sub menu
219 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
220
221 // insert 'Actions' sub menu
222 if (menuActions.addActionsTo(popup)) {
223 popup->addSeparator();
224 }
225
226 // insert 'Copy To' and 'Move To' sub menus
227 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
228 m_copyToMenu.setItems(m_selectedItems);
229 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
230 m_copyToMenu.addActionsTo(popup);
231 popup->addSeparator();
232 }
233
234 // insert 'Properties...' entry
235 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
236 popup->addAction(propertiesAction);
237
238 QAction* activatedAction = popup->exec(QCursor::pos());
239
240 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
241 const KUrl selectedUrl(m_fileInfo.url());
242 if (selectedUrl.isValid()) {
243 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
244 selectedUrl);
245 }
246 }
247
248 popup->deleteLater();
249 }
250
251 void DolphinContextMenu::openViewportContextMenu()
252 {
253 KMenu* popup = new KMenu(m_mainWindow);
254
255 addShowMenubarAction(popup);
256
257 // setup 'Create New' menu
258 KNewMenu* newMenu = m_mainWindow->newMenu();
259 newMenu->slotCheckUpToDate();
260 newMenu->setPopupFiles(m_baseUrl);
261 popup->addMenu(newMenu->menu());
262 popup->addSeparator();
263
264 QAction* pasteAction = createPasteAction();
265 popup->addAction(pasteAction);
266
267 // setup 'View Mode' menu
268 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
269
270 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
271 viewModeMenu->addAction(iconsMode);
272
273 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
274 viewModeMenu->addAction(detailsMode);
275
276 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
277 viewModeMenu->addAction(columnsMode);
278
279 popup->addMenu(viewModeMenu);
280
281 popup->addSeparator();
282
283 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
284 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
285
286 addCustomActions(popup);
287
288 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
289 propertiesAction->setIcon(KIcon("document-properties"));
290 QAction* action = popup->exec(QCursor::pos());
291 if (action == propertiesAction) {
292 const KUrl& url = m_mainWindow->activeViewContainer()->url();
293
294 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
295 dialog->setAttribute(Qt::WA_DeleteOnClose);
296 dialog->show();
297 } else if (action == addToPlacesAction) {
298 const KUrl& url = m_mainWindow->activeViewContainer()->url();
299 if (url.isValid()) {
300 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
301 }
302 }
303
304 popup->deleteLater();
305 }
306
307 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
308 {
309 Q_ASSERT(popup != 0);
310 const KActionCollection* collection = m_mainWindow->actionCollection();
311
312 // insert 'Cut', 'Copy' and 'Paste'
313 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
314 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
315 QAction* pasteAction = createPasteAction();
316
317 popup->addAction(cutAction);
318 popup->addAction(copyAction);
319 popup->addAction(pasteAction);
320 popup->addSeparator();
321
322 // insert 'Rename'
323 QAction* renameAction = collection->action("rename");
324 popup->addAction(renameAction);
325
326 // insert 'Move to Trash' and (optionally) 'Delete'
327 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
328 KConfigGroup configGroup(globalConfig, "KDE");
329 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
330
331 const KUrl& url = m_mainWindow->activeViewContainer()->url();
332 if (url.isLocalFile()) {
333 QAction* moveToTrashAction = collection->action("move_to_trash");
334 popup->addAction(moveToTrashAction);
335 } else {
336 showDeleteCommand = true;
337 }
338
339 if (showDeleteCommand) {
340 QAction* deleteAction = collection->action("delete");
341 popup->addAction(deleteAction);
342 }
343 }
344
345 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
346 {
347 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
348 if (!m_mainWindow->menuBar()->isVisible()) {
349 menu->addAction(showMenuBar);
350 menu->addSeparator();
351 }
352 }
353
354 QString DolphinContextMenu::placesName(const KUrl& url) const
355 {
356 QString name = url.fileName();
357 if (name.isEmpty()) {
358 name = url.host();
359 }
360 return name;
361 }
362
363 QAction* DolphinContextMenu::createPasteAction()
364 {
365 QAction* action = 0;
366 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
367 if (isDir && (m_selectedItems.count() == 1)) {
368 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
369 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
370 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
371 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
372 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
373 } else {
374 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
375 }
376
377 return action;
378 }
379
380 KFileItemListProperties& DolphinContextMenu::capabilities()
381 {
382 if (m_capabilities == 0) {
383 m_capabilities = new KFileItemListProperties(m_selectedItems);
384 }
385 return *m_capabilities;
386 }
387
388 void DolphinContextMenu::addCustomActions(KMenu* menu)
389 {
390 foreach (QAction* action, m_customActions) {
391 menu->addAction(action);
392 }
393 }
394
395 #include "dolphincontextmenu.moc"