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