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