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 "dolphinnewmenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
29 #include <kactioncollection.h>
30 #include <kdesktopfile.h>
31 #include <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kfileitemactions.h>
45 #include <kpropertiesdialog.h>
46 #include <kstandardaction.h>
47 #include <kstandarddirs.h>
49 #include <QtGui/QApplication>
50 #include <QtGui/QClipboard>
51 #include <QtCore/QDir>
53 #include "views/dolphinview.h"
54 #include "views/viewmodecontroller.h"
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
57 const KFileItem
& fileInfo
,
58 const KUrl
& baseUrl
) :
67 // The context menu either accesses the URLs of the selected items
68 // or the items itself. To increase the performance both lists are cached.
69 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
70 m_selectedUrls
= view
->selectedUrls();
71 m_selectedItems
= view
->selectedItems();
74 DolphinContextMenu::~DolphinContextMenu()
76 delete m_capabilities
;
80 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
82 m_customActions
= actions
;
85 void DolphinContextMenu::open()
87 // get the context information
88 if (m_baseUrl
.protocol() == "trash") {
89 m_context
|= TrashContext
;
92 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
93 m_context
|= ItemContext
;
94 // TODO: handle other use cases like devices + desktop files
97 // open the corresponding popup for the context
98 if (m_context
& TrashContext
) {
99 if (m_context
& ItemContext
) {
100 openTrashItemContextMenu();
102 openTrashContextMenu();
104 } else if (m_context
& ItemContext
) {
105 openItemContextMenu();
107 Q_ASSERT(m_context
== NoContext
);
108 openViewportContextMenu();
112 void DolphinContextMenu::openTrashContextMenu()
114 Q_ASSERT(m_context
& TrashContext
);
116 KMenu
* popup
= new KMenu(m_mainWindow
);
118 addShowMenubarAction(popup
);
120 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
121 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
122 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
123 popup
->addAction(emptyTrashAction
);
125 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
126 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
128 // Don't show if url is already in places
129 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
130 addToPlacesAction
->setVisible(false);
133 addCustomActions(popup
);
135 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
136 popup
->addAction(propertiesAction
);
138 QAction
*action
= popup
->exec(QCursor::pos());
139 if (action
== emptyTrashAction
) {
140 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
141 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
144 KGuiItem(i18nc("@action:button", "Empty Trash"),
146 ) == KMessageBox::Continue
;
148 KonqOperations::emptyTrash(m_mainWindow
);
150 } else if (action
== addToPlacesAction
) {
151 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
153 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
157 popup
->deleteLater();
160 void DolphinContextMenu::openTrashItemContextMenu()
162 Q_ASSERT(m_context
& TrashContext
);
163 Q_ASSERT(m_context
& ItemContext
);
165 KMenu
* popup
= new KMenu(m_mainWindow
);
167 addShowMenubarAction(popup
);
169 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
170 popup
->addAction(restoreAction
);
172 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
173 popup
->addAction(deleteAction
);
175 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
176 popup
->addAction(propertiesAction
);
178 if (popup
->exec(QCursor::pos()) == restoreAction
) {
179 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
182 popup
->deleteLater();
185 void DolphinContextMenu::openItemContextMenu()
187 Q_ASSERT(!m_fileInfo
.isNull());
189 KMenu
* popup
= new KMenu(m_mainWindow
);
190 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
191 // setup 'Create New' menu
192 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
193 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
194 newMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
195 newMenu
->checkUpToDate();
196 newMenu
->setPopupFiles(m_fileInfo
.url());
197 newMenu
->setEnabled(capabilities().supportsWriting());
199 KMenu
* menu
= newMenu
->menu();
200 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
201 menu
->setIcon(KIcon("document-new"));
202 popup
->addMenu(newMenu
->menu());
203 popup
->addSeparator();
205 // insert 'Open in new window' and 'Open in new tab' entries
206 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
207 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
208 popup
->addSeparator();
210 addShowMenubarAction(popup
);
211 insertDefaultItemActions(popup
);
213 popup
->addSeparator();
215 // insert 'Bookmark This Folder' entry if exactly one item is selected
216 QAction
* addToPlacesAction
= 0;
217 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
218 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
219 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
220 // Don't show if url is already in places
221 if (placeExists(m_fileInfo
.url())) {
222 addToPlacesAction
->setVisible(false);
226 KFileItemActions menuActions
;
227 menuActions
.setParentWidget(m_mainWindow
);
228 menuActions
.setItemListProperties(m_selectedItems
);
230 // insert 'Open With...' action or sub menu
231 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
233 // insert 'Actions' sub menu
234 if (menuActions
.addServiceActionsTo(popup
)) {
235 popup
->addSeparator();
238 // insert version control actions
239 addVersionControlActions(popup
);
241 // insert 'Copy To' and 'Move To' sub menus
242 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
243 m_copyToMenu
.setItems(m_selectedItems
);
244 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
245 m_copyToMenu
.addActionsTo(popup
);
246 popup
->addSeparator();
249 // insert 'Properties...' entry
250 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
251 popup
->addAction(propertiesAction
);
253 QAction
* activatedAction
= popup
->exec(QCursor::pos());
255 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
256 const KUrl
selectedUrl(m_fileInfo
.url());
257 if (selectedUrl
.isValid()) {
258 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
263 popup
->deleteLater();
266 void DolphinContextMenu::openViewportContextMenu()
268 KMenu
* popup
= new KMenu(m_mainWindow
);
270 addShowMenubarAction(popup
);
272 // setup 'Create New' menu
273 KNewFileMenu
* newMenu
= m_mainWindow
->newMenu();
274 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
275 newMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
276 newMenu
->checkUpToDate();
277 newMenu
->setPopupFiles(m_baseUrl
);
278 popup
->addMenu(newMenu
->menu());
279 popup
->addSeparator();
281 QAction
* pasteAction
= createPasteAction();
282 popup
->addAction(pasteAction
);
284 // setup 'View Mode' menu
285 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"), popup
);
287 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
288 viewModeMenu
->addAction(iconsMode
);
290 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
291 viewModeMenu
->addAction(detailsMode
);
293 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
294 viewModeMenu
->addAction(columnsMode
);
296 popup
->addMenu(viewModeMenu
);
298 popup
->addSeparator();
300 addVersionControlActions(popup
);
302 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
303 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
305 // Don't show if url is already in places
306 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
307 addToPlacesAction
->setVisible(false);
310 addCustomActions(popup
);
312 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
313 propertiesAction
->setIcon(KIcon("document-properties"));
314 QAction
* action
= popup
->exec(QCursor::pos());
315 if (action
== propertiesAction
) {
316 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
318 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
319 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
321 } else if (action
== addToPlacesAction
) {
322 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
324 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
328 popup
->deleteLater();
331 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
333 Q_ASSERT(popup
!= 0);
334 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
336 // insert 'Cut', 'Copy' and 'Paste'
337 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
338 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
339 QAction
* pasteAction
= createPasteAction();
341 popup
->addAction(cutAction
);
342 popup
->addAction(copyAction
);
343 popup
->addAction(pasteAction
);
344 popup
->addSeparator();
347 QAction
* renameAction
= collection
->action("rename");
348 popup
->addAction(renameAction
);
350 // insert 'Move to Trash' and (optionally) 'Delete'
351 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
352 KConfigGroup
configGroup(globalConfig
, "KDE");
353 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
355 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
356 if (url
.isLocalFile()) {
357 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
358 popup
->addAction(moveToTrashAction
);
360 showDeleteCommand
= true;
363 if (showDeleteCommand
) {
364 QAction
* deleteAction
= collection
->action("delete");
365 popup
->addAction(deleteAction
);
369 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
371 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
372 if (!m_mainWindow
->menuBar()->isVisible()) {
373 menu
->addAction(showMenuBar
);
374 menu
->addSeparator();
378 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
380 QString name
= url
.fileName();
381 if (name
.isEmpty()) {
387 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
389 const KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
390 const int count
= placesModel
->rowCount();
392 for (int i
= 0; i
< count
; ++i
) {
393 const QModelIndex index
= placesModel
->index(i
, 0);
395 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
402 QAction
* DolphinContextMenu::createPasteAction()
405 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
406 if (isDir
&& (m_selectedItems
.count() == 1)) {
407 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
408 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
409 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
410 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
411 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
413 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
419 KFileItemListProperties
& DolphinContextMenu::capabilities()
421 if (m_capabilities
== 0) {
422 m_capabilities
= new KFileItemListProperties(m_selectedItems
);
424 return *m_capabilities
;
427 void DolphinContextMenu::addVersionControlActions(KMenu
* menu
)
429 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
430 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
431 if (!versionControlActions
.isEmpty()) {
432 foreach (QAction
* action
, versionControlActions
) {
433 menu
->addAction(action
);
435 menu
->addSeparator();
439 void DolphinContextMenu::addCustomActions(KMenu
* menu
)
441 foreach (QAction
* action
, m_customActions
) {
442 menu
->addAction(action
);
446 #include "dolphincontextmenu.moc"