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>
47 #include <kstandardaction.h>
48 #include <kstandarddirs.h>
50 #include <QtGui/QApplication>
51 #include <QtGui/QClipboard>
52 #include <QtCore/QDir>
54 #include "views/dolphinview.h"
55 #include "views/viewmodecontroller.h"
57 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
58 const KFileItem
& fileInfo
,
59 const KUrl
& baseUrl
) :
68 // The context menu either accesses the URLs of the selected items
69 // or the items itself. To increase the performance both lists are cached.
70 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
71 m_selectedUrls
= view
->selectedUrls();
72 m_selectedItems
= view
->selectedItems();
75 DolphinContextMenu::~DolphinContextMenu()
77 delete m_capabilities
;
81 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
83 m_customActions
= actions
;
86 void DolphinContextMenu::open()
88 // get the context information
89 if (m_baseUrl
.protocol() == "trash") {
90 m_context
|= TrashContext
;
93 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
94 m_context
|= ItemContext
;
95 // TODO: handle other use cases like devices + desktop files
98 // open the corresponding popup for the context
99 if (m_context
& TrashContext
) {
100 if (m_context
& ItemContext
) {
101 openTrashItemContextMenu();
103 openTrashContextMenu();
105 } else if (m_context
& ItemContext
) {
106 openItemContextMenu();
108 Q_ASSERT(m_context
== NoContext
);
109 openViewportContextMenu();
113 void DolphinContextMenu::openTrashContextMenu()
115 Q_ASSERT(m_context
& TrashContext
);
117 KMenu
* popup
= new KMenu(m_mainWindow
);
119 addShowMenubarAction(popup
);
121 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
122 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
123 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
124 popup
->addAction(emptyTrashAction
);
126 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
127 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
129 // Don't show if url is already in places
130 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
131 addToPlacesAction
->setVisible(false);
134 addCustomActions(popup
);
136 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
137 popup
->addAction(propertiesAction
);
139 QAction
*action
= popup
->exec(QCursor::pos());
140 if (action
== emptyTrashAction
) {
141 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
142 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
145 KGuiItem(i18nc("@action:button", "Empty Trash"),
147 ) == KMessageBox::Continue
;
149 KonqOperations::emptyTrash(m_mainWindow
);
151 } else if (action
== addToPlacesAction
) {
152 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
154 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
158 popup
->deleteLater();
161 void DolphinContextMenu::openTrashItemContextMenu()
163 Q_ASSERT(m_context
& TrashContext
);
164 Q_ASSERT(m_context
& ItemContext
);
166 KMenu
* popup
= new KMenu(m_mainWindow
);
168 addShowMenubarAction(popup
);
170 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
171 popup
->addAction(restoreAction
);
173 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
174 popup
->addAction(deleteAction
);
176 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
177 popup
->addAction(propertiesAction
);
179 if (popup
->exec(QCursor::pos()) == restoreAction
) {
180 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
183 popup
->deleteLater();
186 void DolphinContextMenu::openItemContextMenu()
188 Q_ASSERT(!m_fileInfo
.isNull());
190 KMenu
* popup
= new KMenu(m_mainWindow
);
191 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
192 // setup 'Create New' menu
193 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
194 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
195 newMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
196 newMenu
->checkUpToDate();
197 newMenu
->setPopupFiles(m_fileInfo
.url());
198 newMenu
->setEnabled(capabilities().supportsWriting());
200 KMenu
* menu
= newMenu
->menu();
201 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
202 menu
->setIcon(KIcon("document-new"));
203 popup
->addMenu(newMenu
->menu());
204 popup
->addSeparator();
206 // insert 'Open in new window' and 'Open in new tab' entries
207 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
208 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
209 popup
->addSeparator();
211 addShowMenubarAction(popup
);
212 insertDefaultItemActions(popup
);
214 popup
->addSeparator();
216 // insert 'Bookmark This Folder' entry if exactly one item is selected
217 QAction
* addToPlacesAction
= 0;
218 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
219 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
220 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
221 // Don't show if url is already in places
222 if (placeExists(m_fileInfo
.url())) {
223 addToPlacesAction
->setVisible(false);
227 KFileItemActions menuActions
;
228 menuActions
.setParentWidget(m_mainWindow
);
229 menuActions
.setItemListProperties(m_selectedItems
);
231 // insert 'Open With...' action or sub menu
232 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
234 // insert 'Actions' sub menu
235 if (menuActions
.addServiceActionsTo(popup
)) {
236 popup
->addSeparator();
239 // insert version control actions
240 addVersionControlActions(popup
);
242 // insert 'Copy To' and 'Move To' sub menus
243 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
244 m_copyToMenu
.setItems(m_selectedItems
);
245 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
246 m_copyToMenu
.addActionsTo(popup
);
247 popup
->addSeparator();
250 // insert 'Properties...' entry
251 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
252 popup
->addAction(propertiesAction
);
254 QAction
* activatedAction
= popup
->exec(QCursor::pos());
256 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
257 const KUrl
selectedUrl(m_fileInfo
.url());
258 if (selectedUrl
.isValid()) {
259 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
264 popup
->deleteLater();
267 void DolphinContextMenu::openViewportContextMenu()
269 KMenu
* popup
= new KMenu(m_mainWindow
);
271 addShowMenubarAction(popup
);
273 // setup 'Create New' menu
274 KNewFileMenu
* newMenu
= m_mainWindow
->newMenu();
275 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
276 newMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
277 newMenu
->checkUpToDate();
278 newMenu
->setPopupFiles(m_baseUrl
);
279 popup
->addMenu(newMenu
->menu());
280 popup
->addSeparator();
282 QAction
* pasteAction
= createPasteAction();
283 popup
->addAction(pasteAction
);
285 // setup 'View Mode' menu
286 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"), popup
);
288 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
289 viewModeMenu
->addAction(iconsMode
);
291 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
292 viewModeMenu
->addAction(detailsMode
);
294 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
295 viewModeMenu
->addAction(columnsMode
);
297 popup
->addMenu(viewModeMenu
);
299 popup
->addSeparator();
301 addVersionControlActions(popup
);
303 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
304 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
306 // Don't show if url is already in places
307 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
308 addToPlacesAction
->setVisible(false);
311 addCustomActions(popup
);
313 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
314 propertiesAction
->setIcon(KIcon("document-properties"));
315 QAction
* action
= popup
->exec(QCursor::pos());
316 if (action
== propertiesAction
) {
317 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
319 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
320 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
322 } else if (action
== addToPlacesAction
) {
323 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
325 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
329 popup
->deleteLater();
332 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
334 Q_ASSERT(popup
!= 0);
335 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
337 // insert 'Cut', 'Copy' and 'Paste'
338 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
339 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
340 QAction
* pasteAction
= createPasteAction();
342 popup
->addAction(cutAction
);
343 popup
->addAction(copyAction
);
344 popup
->addAction(pasteAction
);
345 popup
->addSeparator();
348 QAction
* renameAction
= collection
->action("rename");
349 popup
->addAction(renameAction
);
351 // insert 'Move to Trash' and (optionally) 'Delete'
352 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
353 KConfigGroup
configGroup(globalConfig
, "KDE");
354 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
356 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
357 if (url
.isLocalFile()) {
358 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
359 popup
->addAction(moveToTrashAction
);
361 showDeleteCommand
= true;
364 if (showDeleteCommand
) {
365 QAction
* deleteAction
= collection
->action("delete");
366 popup
->addAction(deleteAction
);
370 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
372 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
373 if (!m_mainWindow
->menuBar()->isVisible()) {
374 menu
->addAction(showMenuBar
);
375 menu
->addSeparator();
379 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
381 QString name
= url
.fileName();
382 if (name
.isEmpty()) {
388 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
390 const KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
391 const int count
= placesModel
->rowCount();
393 for (int i
= 0; i
< count
; ++i
) {
394 const QModelIndex index
= placesModel
->index(i
, 0);
396 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
403 QAction
* DolphinContextMenu::createPasteAction()
406 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
407 if (isDir
&& (m_selectedItems
.count() == 1)) {
408 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
409 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
410 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
411 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
412 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
414 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
420 KFileItemListProperties
& DolphinContextMenu::capabilities()
422 if (m_capabilities
== 0) {
423 m_capabilities
= new KFileItemListProperties(m_selectedItems
);
425 return *m_capabilities
;
428 void DolphinContextMenu::addVersionControlActions(KMenu
* menu
)
430 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
431 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
432 if (!versionControlActions
.isEmpty()) {
433 foreach (QAction
* action
, versionControlActions
) {
434 menu
->addAction(action
);
436 menu
->addSeparator();
440 void DolphinContextMenu::addCustomActions(KMenu
* menu
)
442 foreach (QAction
* action
, m_customActions
) {
443 menu
->addAction(action
);
447 #include "dolphincontextmenu.moc"