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 "dolphinsettings.h"
26 #include "dolphinview.h"
27 #include "dolphinviewcontainer.h"
28 #include "dolphin_generalsettings.h"
30 #include <kactioncollection.h>
31 #include <kfileplacesmodel.h>
32 #include <kdesktopfile.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_fileitemcapabilities.h>
43 #include <konq_operations.h>
44 #include <konq_menuactions.h>
45 #include <konq_popupmenuinformation.h>
47 #include <kpropertiesdialog.h>
49 #include <kstandardaction.h>
50 #include <kstandarddirs.h>
52 #include <QtGui/QApplication>
53 #include <QtGui/QClipboard>
54 #include <QtCore/QDir>
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
57 const KFileItem
& fileInfo
,
58 const KUrl
& baseUrl
) :
65 // The context menu either accesses the URLs of the selected items
66 // or the items itself. To increase the performance both lists are cached.
67 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
68 m_selectedUrls
= view
->selectedUrls();
69 m_selectedItems
= view
->selectedItems();
72 DolphinContextMenu::~DolphinContextMenu()
74 delete m_capabilities
;
78 void DolphinContextMenu::open()
80 // get the context information
81 if (m_baseUrl
.protocol() == "trash") {
82 m_context
|= TrashContext
;
85 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
86 m_context
|= ItemContext
;
87 // TODO: handle other use cases like devices + desktop files
90 // open the corresponding popup for the context
91 if (m_context
& TrashContext
) {
92 if (m_context
& ItemContext
) {
93 openTrashItemContextMenu();
95 openTrashContextMenu();
97 } else if (m_context
& ItemContext
) {
98 openItemContextMenu();
100 Q_ASSERT(m_context
== NoContext
);
101 openViewportContextMenu();
105 void DolphinContextMenu::openTrashContextMenu()
107 Q_ASSERT(m_context
& TrashContext
);
109 KMenu
* popup
= new KMenu(m_mainWindow
);
111 addShowMenubarAction(popup
);
113 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
114 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
115 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
116 popup
->addAction(emptyTrashAction
);
118 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
119 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
121 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
122 popup
->addAction(propertiesAction
);
124 QAction
*action
= popup
->exec(QCursor::pos());
125 if (action
== emptyTrashAction
) {
126 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
127 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
130 KGuiItem(i18nc("@action:button", "Empty Trash"),
132 ) == KMessageBox::Continue
;
134 KonqOperations::emptyTrash(m_mainWindow
);
136 } else if (action
== addToPlacesAction
) {
137 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
139 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
143 popup
->deleteLater();
146 void DolphinContextMenu::openTrashItemContextMenu()
148 Q_ASSERT(m_context
& TrashContext
);
149 Q_ASSERT(m_context
& ItemContext
);
151 KMenu
* popup
= new KMenu(m_mainWindow
);
153 addShowMenubarAction(popup
);
155 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
156 popup
->addAction(restoreAction
);
158 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
159 popup
->addAction(deleteAction
);
161 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
162 popup
->addAction(propertiesAction
);
164 if (popup
->exec(QCursor::pos()) == restoreAction
) {
165 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
168 popup
->deleteLater();
171 void DolphinContextMenu::openItemContextMenu()
173 Q_ASSERT(!m_fileInfo
.isNull());
175 KMenu
* popup
= new KMenu(m_mainWindow
);
176 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
177 // setup 'Create New' menu
178 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
179 newMenu
->slotCheckUpToDate();
180 newMenu
->setPopupFiles(m_fileInfo
.url());
182 KMenu
* menu
= newMenu
->menu();
183 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
184 menu
->setIcon(KIcon("document-new"));
185 popup
->addMenu(newMenu
->menu());
186 popup
->addSeparator();
188 // insert 'Open in new window' and 'Open in new tab' entries
189 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
190 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
191 popup
->addSeparator();
193 addShowMenubarAction(popup
);
194 insertDefaultItemActions(popup
);
196 popup
->addSeparator();
198 // insert 'Bookmark This Folder' entry if exactly one item is selected
199 QAction
* addToPlacesAction
= 0;
200 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
201 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
202 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
205 KonqPopupMenuInformation popupInfo
;
206 popupInfo
.setItems(m_selectedItems
);
207 popupInfo
.setParentWidget(m_mainWindow
);
208 KonqMenuActions menuActions
;
209 menuActions
.setPopupMenuInfo(popupInfo
);
211 // Insert 'Open With...' action or sub menu
212 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
214 // Insert 'Actions' sub menu
215 if (menuActions
.addActionsTo(popup
)) {
216 popup
->addSeparator();
219 // Insert 'Copy To' and 'Move To' sub menus
220 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
221 m_copyToMenu
.setItems(m_selectedItems
);
222 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
223 m_copyToMenu
.addActionsTo(popup
);
224 popup
->addSeparator();
227 // insert 'Properties...' entry
228 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
229 popup
->addAction(propertiesAction
);
231 QAction
* activatedAction
= popup
->exec(QCursor::pos());
233 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
234 const KUrl
selectedUrl(m_fileInfo
.url());
235 if (selectedUrl
.isValid()) {
236 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
241 popup
->deleteLater();
244 void DolphinContextMenu::openViewportContextMenu()
246 KMenu
* popup
= new KMenu(m_mainWindow
);
248 addShowMenubarAction(popup
);
250 // setup 'Create New' menu
251 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
252 newMenu
->slotCheckUpToDate();
253 newMenu
->setPopupFiles(m_baseUrl
);
254 popup
->addMenu(newMenu
->menu());
255 popup
->addSeparator();
257 QAction
* pasteAction
= createPasteAction();
258 popup
->addAction(pasteAction
);
260 // setup 'View Mode' menu
261 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
263 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
264 viewModeMenu
->addAction(iconsMode
);
266 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
267 viewModeMenu
->addAction(detailsMode
);
269 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
270 viewModeMenu
->addAction(columnsMode
);
272 popup
->addMenu(viewModeMenu
);
274 popup
->addSeparator();
276 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
277 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
278 popup
->addSeparator();
280 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
282 QAction
* action
= popup
->exec(QCursor::pos());
283 if (action
== propertiesAction
) {
284 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
286 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
287 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
290 dialog
->activateWindow();
291 } else if (action
== addToPlacesAction
) {
292 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
294 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
298 popup
->deleteLater();
301 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
303 Q_ASSERT(popup
!= 0);
304 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
306 // insert 'Cut', 'Copy' and 'Paste'
307 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
308 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
309 QAction
* pasteAction
= createPasteAction();
311 popup
->addAction(cutAction
);
312 popup
->addAction(copyAction
);
313 popup
->addAction(pasteAction
);
314 popup
->addSeparator();
317 QAction
* renameAction
= collection
->action("rename");
318 popup
->addAction(renameAction
);
320 // insert 'Move to Trash' and (optionally) 'Delete'
321 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
322 KConfigGroup
configGroup(globalConfig
, "KDE");
323 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
325 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
326 if (url
.isLocalFile()) {
327 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
328 popup
->addAction(moveToTrashAction
);
330 showDeleteCommand
= true;
333 if (showDeleteCommand
) {
334 QAction
* deleteAction
= collection
->action("delete");
335 popup
->addAction(deleteAction
);
339 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
341 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
342 if (!m_mainWindow
->menuBar()->isVisible()) {
343 menu
->addAction(showMenuBar
);
344 menu
->addSeparator();
348 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
350 QString name
= url
.fileName();
351 if (name
.isEmpty()) {
357 QAction
* DolphinContextMenu::createPasteAction()
360 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
361 if (isDir
&& (m_selectedItems
.count() == 1)) {
362 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
363 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
364 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
365 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
366 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
368 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
374 KonqFileItemCapabilities
& DolphinContextMenu::capabilities()
376 if (m_capabilities
== 0) {
377 m_capabilities
= new KonqFileItemCapabilities(m_selectedItems
);
379 return *m_capabilities
;
382 #include "dolphincontextmenu.moc"