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