]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
When renaming items in Dolphin, only the name should be selected, not the extensions...
[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
27 #include <kactioncollection.h>
28 #include <kfileplacesmodel.h>
29 #include <kdesktopfile.h>
30 #include <kglobal.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
33 #include <kmenu.h>
34 #include <kmessagebox.h>
35 #include <kmimetypetrader.h>
36 #include <knewmenu.h>
37 #include <konqmimedata.h>
38 #include <konq_operations.h>
39 #include <klocale.h>
40 #include <kpropertiesdialog.h>
41 #include <krun.h>
42 #include <kstandardaction.h>
43 #include <kstandarddirs.h>
44
45 #include <QtGui/QApplication>
46 #include <QtGui/QClipboard>
47 #include <QtCore/QDir>
48 #include <Qt3Support/Q3ValueList>
49
50 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
51 KFileItem* fileInfo,
52 const KUrl& baseUrl) :
53 m_mainWindow(parent),
54 m_fileInfo(fileInfo),
55 m_baseUrl(baseUrl),
56 m_context(NoContext)
57 {
58 // The context menu either accesses the URLs of the selected items
59 // or the items itself. To increase the performance both lists are cached.
60 DolphinView* view = m_mainWindow->activeView();
61 m_selectedUrls = view->selectedUrls();
62 m_selectedItems = view->selectedItems();
63 }
64
65 DolphinContextMenu::~DolphinContextMenu()
66 {}
67
68 void DolphinContextMenu::open()
69 {
70 // get the context information
71 if (m_baseUrl.protocol() == "trash") {
72 m_context |= TrashContext;
73 }
74
75 if (m_fileInfo != 0) {
76 m_context |= ItemContext;
77 // TODO: handle other use cases like devices + desktop files
78 }
79
80 // open the corresponding popup for the context
81 if (m_context & TrashContext) {
82 if (m_context & ItemContext) {
83 openTrashItemContextMenu();
84 } else {
85 openTrashContextMenu();
86 }
87 } else if (m_context & ItemContext) {
88 openItemContextMenu();
89 } else {
90 Q_ASSERT(m_context == NoContext);
91 openViewportContextMenu();
92 }
93 }
94
95
96 void DolphinContextMenu::openTrashContextMenu()
97 {
98 Q_ASSERT(m_context & TrashContext);
99
100 KMenu* popup = new KMenu(m_mainWindow);
101
102 QAction* emptyTrashAction = new QAction(KIcon("emptytrash"), i18n("Empty Trash"), popup);
103 KConfig trashConfig("trashrc", KConfig::OnlyLocal);
104 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
105 popup->addAction(emptyTrashAction);
106
107 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
108 popup->addAction(propertiesAction);
109
110 if (popup->exec(QCursor::pos()) == emptyTrashAction) {
111 const QString text(i18n("Do you really want to empty the Trash? All items will get deleted."));
112 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
113 text,
114 QString(),
115 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
116 ) == KMessageBox::Continue;
117 if (del) {
118 KonqOperations::emptyTrash(m_mainWindow);
119 }
120 }
121
122 popup->deleteLater();
123 }
124
125 void DolphinContextMenu::openTrashItemContextMenu()
126 {
127 Q_ASSERT(m_context & TrashContext);
128 Q_ASSERT(m_context & ItemContext);
129
130 KMenu* popup = new KMenu(m_mainWindow);
131
132 QAction* restoreAction = new QAction(i18n("Restore"), m_mainWindow);
133 popup->addAction(restoreAction);
134
135 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
136 popup->addAction(deleteAction);
137
138 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
139 popup->addAction(propertiesAction);
140
141 if (popup->exec(QCursor::pos()) == restoreAction) {
142 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
143 }
144
145 popup->deleteLater();
146 }
147
148 void DolphinContextMenu::openItemContextMenu()
149 {
150 Q_ASSERT(m_fileInfo != 0);
151
152 KMenu* popup = new KMenu(m_mainWindow);
153 insertDefaultItemActions(popup);
154
155 popup->addSeparator();
156
157 // insert 'Bookmark This Folder' entry if exactly one item is selected
158 QAction* bookmarkAction = 0;
159 if (m_fileInfo->isDir() && (m_selectedUrls.count() == 1)) {
160 bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
161 }
162
163 // Insert 'Open With...' sub menu
164 QVector<KService::Ptr> openWithVector;
165 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
166
167 // Insert 'Actions' sub menu
168 QVector<KDesktopFileActions::Service> actionsVector;
169 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
170 popup->addSeparator();
171
172 // insert 'Properties...' entry
173 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
174 popup->addAction(propertiesAction);
175
176 QAction* activatedAction = popup->exec(QCursor::pos());
177
178 if ((bookmarkAction != 0) && (activatedAction == bookmarkAction)) {
179 const KUrl selectedUrl(m_fileInfo->url());
180 if (selectedUrl.isValid()) {
181 DolphinSettings::instance().placesModel()->addPlace(selectedUrl.fileName(),
182 selectedUrl);
183 }
184 } else if (serviceActions.contains(activatedAction)) {
185 // one of the 'Actions' items has been selected
186 int id = serviceActions.indexOf(activatedAction);
187 KDesktopFileActions::executeService(m_selectedUrls, actionsVector[id]);
188 } else if (openWithActions.contains(activatedAction)) {
189 // one of the 'Open With' items has been selected
190 if (openWithActions.last() == activatedAction) {
191 // the item 'Other...' has been selected
192 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
193 } else {
194 int id = openWithActions.indexOf(activatedAction);
195 KService::Ptr servicePtr = openWithVector[id];
196 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
197 }
198 }
199
200 openWithVector.clear();
201 actionsVector.clear();
202 popup->deleteLater();
203 }
204
205 void DolphinContextMenu::openViewportContextMenu()
206 {
207 Q_ASSERT(m_fileInfo == 0);
208 KMenu* popup = new KMenu(m_mainWindow);
209
210 // setup 'Create New' menu
211 KNewMenu* newMenu = m_mainWindow->newMenu();
212 newMenu->slotCheckUpToDate();
213 newMenu->setPopupFiles(m_baseUrl);
214 popup->addMenu(newMenu->menu());
215 popup->addSeparator();
216
217 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
218 popup->addAction(pasteAction);
219
220 // setup 'View Mode' menu
221 KMenu* viewModeMenu = new KMenu(i18n("View Mode"));
222
223 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
224 viewModeMenu->addAction(iconsMode);
225
226 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
227 viewModeMenu->addAction(detailsMode);
228
229 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
230 viewModeMenu->addAction(columnsMode);
231
232 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
233 viewModeMenu->addAction(previewsMode);
234
235 popup->addMenu(viewModeMenu);
236 popup->addSeparator();
237
238 QAction* bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
239 popup->addSeparator();
240
241 QAction* propertiesAction = popup->addAction(i18n("Properties"));
242
243 QAction* activatedAction = popup->exec(QCursor::pos());
244 if (activatedAction == propertiesAction) {
245 new KPropertiesDialog(m_mainWindow->activeView()->url());
246 } else if (activatedAction == bookmarkAction) {
247 const KUrl& url = m_mainWindow->activeView()->url();
248 if (url.isValid()) {
249 DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url);
250 }
251 }
252
253 popup->deleteLater();
254 }
255
256 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
257 {
258 Q_ASSERT(popup != 0);
259 const KActionCollection* collection = m_mainWindow->actionCollection();
260
261 // insert 'Cut', 'Copy' and 'Paste'
262 QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut));
263 QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy));
264 QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste));
265
266 popup->addAction(cutAction);
267 popup->addAction(copyAction);
268 popup->addAction(pasteAction);
269 popup->addSeparator();
270
271 // insert 'Rename'
272 QAction* renameAction = collection->action("rename");
273 popup->addAction(renameAction);
274
275 // insert 'Move to Trash' and (optionally) 'Delete'
276 const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals);
277 const KConfigGroup kdeConfig(globalConfig, "KDE");
278 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
279 const KUrl& url = m_mainWindow->activeView()->url();
280 if (url.isLocalFile()) {
281 QAction* moveToTrashAction = collection->action("move_to_trash");
282 popup->addAction(moveToTrashAction);
283 } else {
284 showDeleteCommand = true;
285 }
286
287 if (showDeleteCommand) {
288 QAction* deleteAction = collection->action("delete");
289 popup->addAction(deleteAction);
290 }
291 }
292
293 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
294 QVector<KService::Ptr>& openWithVector)
295 {
296 // Parts of the following code have been taken
297 // from the class KonqOperations located in
298 // libqonq/konq_operations.h of Konqueror.
299 // (Copyright (C) 2000 David Faure <faure@kde.org>)
300
301 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
302 // are listed which are registered to open the item. As last entry "Other..." will be
303 // attached which allows to select a custom application. If no applications are registered
304 // no sub menu is created at all, only "Open With..." will be offered.
305 bool insertOpenWithItems = true;
306 const QString contextMimeType(m_fileInfo->mimetype());
307
308 QListIterator<KFileItem*> mimeIt(m_selectedItems);
309 while (insertOpenWithItems && mimeIt.hasNext()) {
310 KFileItem* item = mimeIt.next();
311 insertOpenWithItems = (contextMimeType == item->mimetype());
312 }
313
314 QList<QAction*> openWithActions;
315 if (insertOpenWithItems) {
316 // fill the 'Open with' sub menu with application types
317 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
318 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
319 "Application",
320 "Type == 'Application'");
321 if (offers.count() > 0) {
322 KService::List::Iterator it;
323 KMenu* openWithMenu = new KMenu(i18n("Open With"));
324 for (it = offers.begin(); it != offers.end(); ++it) {
325 // The offer list from the KTrader returns duplicate
326 // application entries. Although this seems to be a configuration
327 // problem outside the scope of Dolphin, duplicated entries just
328 // will be skipped here.
329 const QString appName((*it)->name());
330 if (!containsEntry(openWithMenu, appName)) {
331 const KIcon icon((*it)->icon());
332 QAction* action = openWithMenu->addAction(icon, appName);
333 openWithVector.append(*it);
334 openWithActions << action;
335 }
336 }
337
338 openWithMenu->addSeparator();
339 QAction* action = openWithMenu->addAction(i18n("&Other..."));
340
341 openWithActions << action;
342 popup->addMenu(openWithMenu);
343 } else {
344 // No applications are registered, hence just offer
345 // a "Open With..." item instead of a sub menu containing
346 // only one entry.
347 QAction* action = popup->addAction(i18n("Open With..."));
348 openWithActions << action;
349 }
350 } else {
351 // At least one of the selected items has a different MIME type. In this case
352 // just show a disabled "Open With..." entry.
353 QAction* action = popup->addAction(i18n("Open With..."));
354 action->setEnabled(false);
355 }
356
357 return openWithActions;
358 }
359
360 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
361 QVector<KDesktopFileActions::Service>& actionsVector)
362 {
363 // Parts of the following code have been taken
364 // from the class KonqOperations located in
365 // libqonq/konq_operations.h of Konqueror.
366 // (Copyright (C) 2000 David Faure <faure@kde.org>)
367
368 KMenu* actionsMenu = new KMenu(i18n("Actions"));
369
370 QList<QAction*> serviceActions;
371
372 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
373
374 KMenu* menu = 0;
375 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
376 QDir dir(*dirIt);
377 QStringList filters;
378 filters << "*.desktop";
379 dir.setNameFilters(filters);
380 QStringList entries = dir.entryList(QDir::Files);
381
382 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
383 KConfigGroup cfg(KSharedConfig::openConfig(*dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry");
384 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
385 //const QStringList types = cfg.readListEntry("ServiceTypes");
386 QStringList types;
387 types = cfg.readEntry("ServiceTypes", types);
388 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
389 // check whether the mime type is equal or whether the
390 // mimegroup (e. g. image/*) is supported
391
392 bool insert = false;
393 if ((*it) == "all/allfiles") {
394 // The service type is valid for all files, but not for directories.
395 // Check whether the selected items only consist of files...
396 QListIterator<KFileItem*> mimeIt(m_selectedItems);
397 insert = true;
398 while (insert && mimeIt.hasNext()) {
399 KFileItem* item = mimeIt.next();
400 insert = !item->isDir();
401 }
402 }
403
404 if (!insert) {
405 // Check whether the MIME types of all selected files match
406 // to the mimetype of the service action. As soon as one MIME
407 // type does not match, no service menu is shown at all.
408 QListIterator<KFileItem*> mimeIt(m_selectedItems);
409 insert = true;
410 while (insert && mimeIt.hasNext()) {
411 KFileItem* item = mimeIt.next();
412 const QString mimeType(item->mimetype());
413 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
414
415 insert = (*it == mimeType) ||
416 ((*it).right(1) == "*") &&
417 ((*it).left((*it).indexOf('/')) == mimeGroup);
418 }
419 }
420
421 if (insert) {
422 menu = actionsMenu;
423
424 const QString submenuName = cfg.readEntry("X-KDE-Submenu");
425 if (!submenuName.isEmpty()) {
426 menu = new KMenu(submenuName);
427 actionsMenu->addMenu(menu);
428 }
429
430 Q3ValueList<KDesktopFileActions::Service> userServices =
431 KDesktopFileActions::userDefinedServices(*dirIt + *entryIt, true);
432
433 Q3ValueList<KDesktopFileActions::Service>::Iterator serviceIt;
434 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
435 KDesktopFileActions::Service service = (*serviceIt);
436 if (!service.m_strIcon.isEmpty()) {
437 QAction* action = menu->addAction(KIcon(service.m_strIcon),
438 service.m_strName);
439 serviceActions << action;
440 } else {
441 QAction *action = menu->addAction(service.m_strName);
442 serviceActions << action;
443 }
444 actionsVector.append(service);
445 }
446 }
447 }
448 }
449 }
450 }
451
452 const int itemsCount = actionsMenu->actions().count();
453 if (itemsCount == 0) {
454 // no actions are available at all, hence show the "Actions"
455 // submenu disabled
456 actionsMenu->setEnabled(false);
457 }
458
459 if (itemsCount == 1) {
460 // Exactly one item is available. Instead of showing a sub menu with
461 // only one item, show the item directly in the root menu.
462 if (menu == actionsMenu) {
463 // The item is an action, hence show the action in the root menu.
464 const QList<QAction*> actions = actionsMenu->actions();
465 Q_ASSERT(actions.count() == 1);
466
467 const QString text = actions[0]->text();
468 const QIcon icon = actions[0]->icon();
469 if (icon.isNull()) {
470 QAction* action = popup->addAction(text);
471 serviceActions.clear();
472 serviceActions << action;
473 } else {
474 QAction* action = popup->addAction(icon, text);
475 serviceActions.clear();
476 serviceActions << action;
477 }
478 } else {
479 // The item is a sub menu, hence show the sub menu in the root menu.
480 popup->addMenu(menu);
481 }
482 actionsMenu->deleteLater();
483 actionsMenu = 0;
484 } else {
485 popup->addMenu(actionsMenu);
486 }
487
488 return serviceActions;
489 }
490
491 bool DolphinContextMenu::containsEntry(const KMenu* menu,
492 const QString& entryName) const
493 {
494 Q_ASSERT(menu != 0);
495
496 const QList<QAction*> list = menu->actions();
497 const uint count = list.count();
498 for (uint i = 0; i < count; ++i) {
499 const QAction* action = list.at(i);
500 if (action->text() == entryName) {
501 return true;
502 }
503 }
504
505 return false;
506 }
507
508 #include "dolphincontextmenu.moc"