]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Typo fixes
[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 "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kfileplacesmodel.h>
31 #include <kdesktopfile.h>
32 #include <kglobal.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmenu.h>
36 #include <kmenubar.h>
37 #include <kmessagebox.h>
38 #include <kmimetypetrader.h>
39 #include <knewmenu.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <konq_menuactions.h>
43 #include <klocale.h>
44 #include <kpropertiesdialog.h>
45 #include <krun.h>
46 #include <kstandardaction.h>
47 #include <kstandarddirs.h>
48
49 #include <QtGui/QApplication>
50 #include <QtGui/QClipboard>
51 #include <QtCore/QDir>
52
53 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
54 const KFileItem& fileInfo,
55 const KUrl& baseUrl) :
56 m_mainWindow(parent),
57 m_fileInfo(fileInfo),
58 m_baseUrl(baseUrl),
59 m_context(NoContext)
60 {
61 // The context menu either accesses the URLs of the selected items
62 // or the items itself. To increase the performance both lists are cached.
63 DolphinView* view = m_mainWindow->activeViewContainer()->view();
64 m_selectedUrls = view->selectedUrls();
65 m_selectedItems = view->selectedItems();
66 }
67
68 DolphinContextMenu::~DolphinContextMenu()
69 {
70 }
71
72 void DolphinContextMenu::open()
73 {
74 // get the context information
75 if (m_baseUrl.protocol() == "trash") {
76 m_context |= TrashContext;
77 }
78
79 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
80 m_context |= ItemContext;
81 // TODO: handle other use cases like devices + desktop files
82 }
83
84 // open the corresponding popup for the context
85 if (m_context & TrashContext) {
86 if (m_context & ItemContext) {
87 openTrashItemContextMenu();
88 } else {
89 openTrashContextMenu();
90 }
91 } else if (m_context & ItemContext) {
92 openItemContextMenu();
93 } else {
94 Q_ASSERT(m_context == NoContext);
95 openViewportContextMenu();
96 }
97 }
98
99 void DolphinContextMenu::openTrashContextMenu()
100 {
101 Q_ASSERT(m_context & TrashContext);
102
103 KMenu* popup = new KMenu(m_mainWindow);
104
105 addShowMenubarAction(popup);
106
107 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup);
108 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
109 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
110 popup->addAction(emptyTrashAction);
111
112 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
113 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
114
115 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
116 popup->addAction(propertiesAction);
117
118 QAction *action = popup->exec(QCursor::pos());
119 if (action == emptyTrashAction) {
120 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
121 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
122 text,
123 QString(),
124 KGuiItem(i18nc("@action:button", "Empty Trash"),
125 KIcon("user-trash"))
126 ) == KMessageBox::Continue;
127 if (del) {
128 KonqOperations::emptyTrash(m_mainWindow);
129 }
130 } else if (action == addToPlacesAction) {
131 const KUrl& url = m_mainWindow->activeViewContainer()->url();
132 if (url.isValid()) {
133 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
134 }
135 }
136
137 popup->deleteLater();
138 }
139
140 void DolphinContextMenu::openTrashItemContextMenu()
141 {
142 Q_ASSERT(m_context & TrashContext);
143 Q_ASSERT(m_context & ItemContext);
144
145 KMenu* popup = new KMenu(m_mainWindow);
146
147 addShowMenubarAction(popup);
148
149 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
150 popup->addAction(restoreAction);
151
152 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
153 popup->addAction(deleteAction);
154
155 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
156 popup->addAction(propertiesAction);
157
158 if (popup->exec(QCursor::pos()) == restoreAction) {
159 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
160 }
161
162 popup->deleteLater();
163 }
164
165 void DolphinContextMenu::openItemContextMenu()
166 {
167 Q_ASSERT(!m_fileInfo.isNull());
168
169 KMenu* popup = new KMenu(m_mainWindow);
170 addShowMenubarAction(popup);
171 insertDefaultItemActions(popup);
172
173 popup->addSeparator();
174
175 // insert 'Bookmark This Folder' entry if exactly one item is selected
176 QAction* addToPlacesAction = 0;
177 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
178 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
179 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
180 }
181
182 // Insert 'Open With...' sub menu
183 QVector<KService::Ptr> openWithVector;
184 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
185
186 // Insert 'Actions' sub menu
187 KonqMenuActions menuActions;
188 menuActions.setItems(m_selectedItems);
189 if (menuActions.addActionsTo(popup)) {
190 popup->addSeparator();
191 }
192
193 // Insert 'Copy To' and 'Move To' sub menus
194 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
195 m_copyToMenu.setItems(m_selectedItems);
196 m_copyToMenu.addActionsTo(popup);
197 popup->addSeparator();
198 }
199
200 // insert 'Properties...' entry
201 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
202 popup->addAction(propertiesAction);
203
204 QAction* activatedAction = popup->exec(QCursor::pos());
205
206 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
207 const KUrl selectedUrl(m_fileInfo.url());
208 if (selectedUrl.isValid()) {
209 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
210 selectedUrl);
211 }
212 } else if (openWithActions.contains(activatedAction)) {
213 // one of the 'Open With' items has been selected
214 if (openWithActions.last() == activatedAction) {
215 // the item 'Other...' has been selected
216 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
217 } else {
218 int id = openWithActions.indexOf(activatedAction);
219 KService::Ptr servicePtr = openWithVector[id];
220 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
221 }
222 }
223
224 openWithVector.clear();
225 popup->deleteLater();
226 }
227
228 void DolphinContextMenu::openViewportContextMenu()
229 {
230 KMenu* popup = new KMenu(m_mainWindow);
231
232 addShowMenubarAction(popup);
233
234 // setup 'Create New' menu
235 KNewMenu* newMenu = m_mainWindow->newMenu();
236 newMenu->slotCheckUpToDate();
237 newMenu->setPopupFiles(m_baseUrl);
238 popup->addMenu(newMenu->menu());
239 popup->addSeparator();
240
241 QAction* pasteAction = createPasteAction();
242 popup->addAction(pasteAction);
243
244 // setup 'View Mode' menu
245 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
246
247 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
248 viewModeMenu->addAction(iconsMode);
249
250 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
251 viewModeMenu->addAction(detailsMode);
252
253 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
254 viewModeMenu->addAction(columnsMode);
255
256 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
257 viewModeMenu->addAction(previewsMode);
258
259 popup->addMenu(viewModeMenu);
260
261 popup->addSeparator();
262
263 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
264 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
265 popup->addSeparator();
266
267 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
268
269 QAction* action = popup->exec(QCursor::pos());
270 if (action == propertiesAction) {
271 const KUrl& url = m_mainWindow->activeViewContainer()->url();
272 KPropertiesDialog dialog(url, m_mainWindow);
273 dialog.exec();
274 } else if (action == addToPlacesAction) {
275 const KUrl& url = m_mainWindow->activeViewContainer()->url();
276 if (url.isValid()) {
277 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
278 }
279 }
280
281 popup->deleteLater();
282 }
283
284 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
285 {
286 Q_ASSERT(popup != 0);
287 const KActionCollection* collection = m_mainWindow->actionCollection();
288
289 // insert 'Cut', 'Copy' and 'Paste'
290 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
291 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
292 QAction* pasteAction = createPasteAction();
293
294 popup->addAction(cutAction);
295 popup->addAction(copyAction);
296 popup->addAction(pasteAction);
297 popup->addSeparator();
298
299 // insert 'Rename'
300 QAction* renameAction = collection->action("rename");
301 popup->addAction(renameAction);
302
303 // insert 'Move to Trash' and (optionally) 'Delete'
304 KConfigGroup kdeConfig(KGlobal::config(), "KDE");
305 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
306 const KUrl& url = m_mainWindow->activeViewContainer()->url();
307 if (url.isLocalFile()) {
308 QAction* moveToTrashAction = collection->action("move_to_trash");
309 popup->addAction(moveToTrashAction);
310 } else {
311 showDeleteCommand = true;
312 }
313
314 if (showDeleteCommand) {
315 QAction* deleteAction = collection->action("delete");
316 popup->addAction(deleteAction);
317 }
318 }
319
320 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
321 QVector<KService::Ptr>& openWithVector)
322 {
323 // Parts of the following code have been taken
324 // from the class KonqOperations located in
325 // libqonq/konq_operations.h of Konqueror.
326 // (Copyright (C) 2000 David Faure <faure@kde.org>)
327
328 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
329 // are listed which are registered to open the item. As last entry "Other..." will be
330 // attached which allows to select a custom application. If no applications are registered
331 // no sub menu is created at all, only "Open With..." will be offered.
332 bool insertOpenWithItems = true;
333 const QString contextMimeType(m_fileInfo.mimetype());
334
335 QListIterator<KFileItem> mimeIt(m_selectedItems);
336 while (insertOpenWithItems && mimeIt.hasNext()) {
337 KFileItem item = mimeIt.next();
338 insertOpenWithItems = (contextMimeType == item.mimetype());
339 }
340
341 QList<QAction*> openWithActions;
342 if (insertOpenWithItems) {
343 // fill the 'Open with' sub menu with application types
344 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url());
345 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
346 "Application",
347 "Type == 'Application'");
348 if (offers.count() > 0) {
349 KService::List::Iterator it;
350 KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With"));
351 for (it = offers.begin(); it != offers.end(); ++it) {
352 // The offer list from the KTrader returns duplicate
353 // application entries. Although this seems to be a configuration
354 // problem outside the scope of Dolphin, duplicated entries just
355 // will be skipped here.
356 const QString appName((*it)->name());
357 if (!containsEntry(openWithMenu, appName)) {
358 const KIcon icon((*it)->icon());
359 QAction* action = openWithMenu->addAction(icon, appName);
360 openWithVector.append(*it);
361 openWithActions << action;
362 }
363 }
364
365 openWithMenu->addSeparator();
366 QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other..."));
367
368 openWithActions << action;
369 popup->addMenu(openWithMenu);
370 } else {
371 // No applications are registered, hence just offer
372 // a "Open With..." item instead of a sub menu containing
373 // only one entry.
374 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
375 openWithActions << action;
376 }
377 } else {
378 // At least one of the selected items has a different MIME type. In this case
379 // just show a disabled "Open With..." entry.
380 QAction* action = popup->addAction(i18nc("@title:menu", "Open With..."));
381 action->setEnabled(false);
382 }
383
384 return openWithActions;
385 }
386
387 bool DolphinContextMenu::containsEntry(const KMenu* menu,
388 const QString& entryName) const
389 {
390 Q_ASSERT(menu != 0);
391
392 const QList<QAction*> list = menu->actions();
393 const uint count = list.count();
394 for (uint i = 0; i < count; ++i) {
395 const QAction* action = list.at(i);
396 if (action->text() == entryName) {
397 return true;
398 }
399 }
400
401 return false;
402 }
403
404 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
405 {
406 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
407 if (!m_mainWindow->menuBar()->isVisible()) {
408 // TODO: it should not be necessary to uncheck the menu
409 // bar action, but currently the action states don't get
410 // updated if the menu is disabled
411 showMenuBar->setChecked(false);
412 menu->addAction(showMenuBar);
413 menu->addSeparator();
414 }
415 }
416
417 QString DolphinContextMenu::placesName(const KUrl& url) const
418 {
419 QString name = url.fileName();
420 if (name.isEmpty()) {
421 name = url.host();
422 }
423 return name;
424 }
425
426 QAction* DolphinContextMenu::createPasteAction()
427 {
428 QAction* action = 0;
429 if ((m_selectedItems.count() == 1) && m_fileInfo.isDir()) {
430 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
431 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
432 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
433 action->setEnabled(!pasteData.isEmpty());
434 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
435 } else {
436 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
437 }
438
439 return action;
440 }
441
442 #include "dolphincontextmenu.moc"