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