1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphincontextmenu.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "dolphinviewcontainer.h"
28 #include <kactioncollection.h>
29 #include <kfileplacesmodel.h>
30 #include <kdesktopfile.h>
32 #include <kiconloader.h>
33 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
41 #include <konq_menuactions.h>
43 #include <kpropertiesdialog.h>
45 #include <kstandardaction.h>
46 #include <kstandarddirs.h>
48 #include <QtGui/QApplication>
49 #include <QtGui/QClipboard>
50 #include <QtCore/QDir>
52 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
53 const KFileItem
& fileInfo
,
54 const KUrl
& baseUrl
) :
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();
67 DolphinContextMenu::~DolphinContextMenu()
71 void DolphinContextMenu::open()
73 // get the context information
74 if (m_baseUrl
.protocol() == "trash") {
75 m_context
|= TrashContext
;
78 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
79 m_context
|= ItemContext
;
80 // TODO: handle other use cases like devices + desktop files
83 // open the corresponding popup for the context
84 if (m_context
& TrashContext
) {
85 if (m_context
& ItemContext
) {
86 openTrashItemContextMenu();
88 openTrashContextMenu();
90 } else if (m_context
& ItemContext
) {
91 openItemContextMenu();
93 Q_ASSERT(m_context
== NoContext
);
94 openViewportContextMenu();
98 void DolphinContextMenu::pasteIntoFolder()
100 // TODO: this method should go into DolphinView (see DolphinContextMenu::createPasteAction())
101 Q_ASSERT(m_selectedItems
.count() == 1);
102 Q_ASSERT(m_fileInfo
.isDir());
104 QClipboard
* clipboard
= QApplication::clipboard();
105 const QMimeData
* mimeData
= clipboard
->mimeData();
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
);
113 KonqOperations::copy(m_mainWindow
, KonqOperations::COPY
, source
, dest
);
117 void DolphinContextMenu::openTrashContextMenu()
119 Q_ASSERT(m_context
& TrashContext
);
121 KMenu
* popup
= new KMenu(m_mainWindow
);
123 addShowMenubarAction(popup
);
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
);
130 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
131 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
133 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
134 popup
->addAction(propertiesAction
);
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
,
142 KGuiItem(i18nc("@action:button", "Empty Trash"),
144 ) == KMessageBox::Continue
;
146 KonqOperations::emptyTrash(m_mainWindow
);
148 } else if (action
== addToPlacesAction
) {
149 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
151 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
155 popup
->deleteLater();
158 void DolphinContextMenu::openTrashItemContextMenu()
160 Q_ASSERT(m_context
& TrashContext
);
161 Q_ASSERT(m_context
& ItemContext
);
163 KMenu
* popup
= new KMenu(m_mainWindow
);
165 addShowMenubarAction(popup
);
167 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
168 popup
->addAction(restoreAction
);
170 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
171 popup
->addAction(deleteAction
);
173 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
174 popup
->addAction(propertiesAction
);
176 if (popup
->exec(QCursor::pos()) == restoreAction
) {
177 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
180 popup
->deleteLater();
183 void DolphinContextMenu::openItemContextMenu()
185 Q_ASSERT(!m_fileInfo
.isNull());
187 KMenu
* popup
= new KMenu(m_mainWindow
);
188 addShowMenubarAction(popup
);
189 insertDefaultItemActions(popup
);
191 popup
->addSeparator();
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"));
200 // Insert 'Open With...' sub menu
201 QVector
<KService::Ptr
> openWithVector
;
202 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
204 // Insert 'Actions' sub menu
205 KonqMenuActions menuActions
;
206 menuActions
.setItems(m_selectedItems
);
207 if (menuActions
.addActionsTo(popup
))
208 popup
->addSeparator();
210 // insert 'Properties...' entry
211 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
212 popup
->addAction(propertiesAction
);
214 QAction
* activatedAction
= popup
->exec(QCursor::pos());
216 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
217 const KUrl
selectedUrl(m_fileInfo
.url());
218 if (selectedUrl
.isValid()) {
219 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
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
);
228 int id
= openWithActions
.indexOf(activatedAction
);
229 KService::Ptr servicePtr
= openWithVector
[id
];
230 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
234 openWithVector
.clear();
235 popup
->deleteLater();
238 void DolphinContextMenu::openViewportContextMenu()
240 KMenu
* popup
= new KMenu(m_mainWindow
);
242 addShowMenubarAction(popup
);
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();
251 QAction
* pasteAction
= createPasteAction();
252 popup
->addAction(pasteAction
);
254 // setup 'View Mode' menu
255 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
257 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
258 viewModeMenu
->addAction(iconsMode
);
260 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
261 viewModeMenu
->addAction(detailsMode
);
263 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
264 viewModeMenu
->addAction(columnsMode
);
266 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
267 viewModeMenu
->addAction(previewsMode
);
269 popup
->addMenu(viewModeMenu
);
271 popup
->addSeparator();
273 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
274 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
275 popup
->addSeparator();
277 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
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
);
284 } else if (action
== addToPlacesAction
) {
285 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
287 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
291 popup
->deleteLater();
294 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
296 Q_ASSERT(popup
!= 0);
297 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
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();
304 popup
->addAction(cutAction
);
305 popup
->addAction(copyAction
);
306 popup
->addAction(pasteAction
);
307 popup
->addSeparator();
310 QAction
* renameAction
= collection
->action("rename");
311 popup
->addAction(renameAction
);
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
);
321 showDeleteCommand
= true;
324 if (showDeleteCommand
) {
325 QAction
* deleteAction
= collection
->action("delete");
326 popup
->addAction(deleteAction
);
330 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
331 QVector
<KService::Ptr
>& openWithVector
)
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>)
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());
345 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
346 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
347 KFileItem item
= mimeIt
.next();
348 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
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(),
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
;
375 openWithMenu
->addSeparator();
376 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
378 openWithActions
<< action
;
379 popup
->addMenu(openWithMenu
);
381 // No applications are registered, hence just offer
382 // a "Open With..." item instead of a sub menu containing
384 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
385 openWithActions
<< action
;
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);
394 return openWithActions
;
397 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
398 const QString
& entryName
) const
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
) {
414 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
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();
427 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
429 QString name
= url
.fileName();
430 if (name
.isEmpty()) {
436 QAction
* DolphinContextMenu::createPasteAction()
438 // TODO: move this method as QAction* action pasteAction() into DolphinMainWindow
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()));
447 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
453 #include "dolphincontextmenu.moc"