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"
27 #include "dolphin_generalsettings.h"
29 #include <kactioncollection.h>
30 #include <kfileplacesmodel.h>
31 #include <kdesktopfile.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
37 #include <kmessagebox.h>
38 #include <kmimetypetrader.h>
40 #include <konqmimedata.h>
41 #include <konq_fileitemcapabilities.h>
42 #include <konq_operations.h>
43 #include <konq_menuactions.h>
44 #include <konq_popupmenuinformation.h>
46 #include <kpropertiesdialog.h>
48 #include <kstandardaction.h>
49 #include <kstandarddirs.h>
51 #include <QtGui/QApplication>
52 #include <QtGui/QClipboard>
53 #include <QtCore/QDir>
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
56 const KFileItem
& fileInfo
,
57 const KUrl
& baseUrl
) :
64 // The context menu either accesses the URLs of the selected items
65 // or the items itself. To increase the performance both lists are cached.
66 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
67 m_selectedUrls
= view
->selectedUrls();
68 m_selectedItems
= view
->selectedItems();
71 DolphinContextMenu::~DolphinContextMenu()
73 delete m_capabilities
;
77 void DolphinContextMenu::open()
79 // get the context information
80 if (m_baseUrl
.protocol() == "trash") {
81 m_context
|= TrashContext
;
84 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
85 m_context
|= ItemContext
;
86 // TODO: handle other use cases like devices + desktop files
89 // open the corresponding popup for the context
90 if (m_context
& TrashContext
) {
91 if (m_context
& ItemContext
) {
92 openTrashItemContextMenu();
94 openTrashContextMenu();
96 } else if (m_context
& ItemContext
) {
97 openItemContextMenu();
99 Q_ASSERT(m_context
== NoContext
);
100 openViewportContextMenu();
104 void DolphinContextMenu::openTrashContextMenu()
106 Q_ASSERT(m_context
& TrashContext
);
108 KMenu
* popup
= new KMenu(m_mainWindow
);
110 addShowMenubarAction(popup
);
112 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
113 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
114 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
115 popup
->addAction(emptyTrashAction
);
117 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
118 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
120 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
121 popup
->addAction(propertiesAction
);
123 QAction
*action
= popup
->exec(QCursor::pos());
124 if (action
== emptyTrashAction
) {
125 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
126 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
129 KGuiItem(i18nc("@action:button", "Empty Trash"),
131 ) == KMessageBox::Continue
;
133 KonqOperations::emptyTrash(m_mainWindow
);
135 } else if (action
== addToPlacesAction
) {
136 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
138 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
142 popup
->deleteLater();
145 void DolphinContextMenu::openTrashItemContextMenu()
147 Q_ASSERT(m_context
& TrashContext
);
148 Q_ASSERT(m_context
& ItemContext
);
150 KMenu
* popup
= new KMenu(m_mainWindow
);
152 addShowMenubarAction(popup
);
154 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
155 popup
->addAction(restoreAction
);
157 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
158 popup
->addAction(deleteAction
);
160 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
161 popup
->addAction(propertiesAction
);
163 if (popup
->exec(QCursor::pos()) == restoreAction
) {
164 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
167 popup
->deleteLater();
170 void DolphinContextMenu::openItemContextMenu()
172 Q_ASSERT(!m_fileInfo
.isNull());
174 KMenu
* popup
= new KMenu(m_mainWindow
);
175 addShowMenubarAction(popup
);
176 insertDefaultItemActions(popup
);
178 popup
->addSeparator();
180 // insert 'Bookmark This Folder' entry if exactly one item is selected
181 QAction
* addToPlacesAction
= 0;
182 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
183 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
184 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
187 KonqPopupMenuInformation popupInfo
;
188 popupInfo
.setItems(m_selectedItems
);
189 popupInfo
.setParentWidget(m_mainWindow
);
190 KonqMenuActions menuActions
;
191 menuActions
.setPopupMenuInfo(popupInfo
);
193 // Insert 'Open With...' action or sub menu
194 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
196 // Insert 'Actions' sub menu
197 if (menuActions
.addActionsTo(popup
)) {
198 popup
->addSeparator();
201 // Insert 'Copy To' and 'Move To' sub menus
202 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
203 m_copyToMenu
.setItems(m_selectedItems
);
204 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
205 m_copyToMenu
.addActionsTo(popup
);
206 popup
->addSeparator();
209 // insert 'Properties...' entry
210 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
211 popup
->addAction(propertiesAction
);
213 QAction
* activatedAction
= popup
->exec(QCursor::pos());
215 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
216 const KUrl
selectedUrl(m_fileInfo
.url());
217 if (selectedUrl
.isValid()) {
218 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
223 popup
->deleteLater();
226 void DolphinContextMenu::openViewportContextMenu()
228 KMenu
* popup
= new KMenu(m_mainWindow
);
230 addShowMenubarAction(popup
);
232 // setup 'Create New' menu
233 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
234 newMenu
->slotCheckUpToDate();
235 newMenu
->setPopupFiles(m_baseUrl
);
236 popup
->addMenu(newMenu
->menu());
237 popup
->addSeparator();
239 QAction
* pasteAction
= createPasteAction();
240 popup
->addAction(pasteAction
);
242 // setup 'View Mode' menu
243 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
245 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
246 viewModeMenu
->addAction(iconsMode
);
248 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
249 viewModeMenu
->addAction(detailsMode
);
251 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
252 viewModeMenu
->addAction(columnsMode
);
254 popup
->addMenu(viewModeMenu
);
256 popup
->addSeparator();
258 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
259 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
260 popup
->addSeparator();
262 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
264 QAction
* action
= popup
->exec(QCursor::pos());
265 if (action
== propertiesAction
) {
266 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
267 KPropertiesDialog
dialog(url
, m_mainWindow
);
269 } else if (action
== addToPlacesAction
) {
270 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
272 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
276 popup
->deleteLater();
279 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
281 Q_ASSERT(popup
!= 0);
282 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
284 // insert 'Cut', 'Copy' and 'Paste'
285 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
286 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
287 QAction
* pasteAction
= createPasteAction();
289 popup
->addAction(cutAction
);
290 popup
->addAction(copyAction
);
291 popup
->addAction(pasteAction
);
292 popup
->addSeparator();
295 QAction
* renameAction
= collection
->action("rename");
296 popup
->addAction(renameAction
);
298 // insert 'Move to Trash' and (optionally) 'Delete'
299 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
300 KConfigGroup
configGroup(globalConfig
, "KDE");
301 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
303 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
304 if (url
.isLocalFile()) {
305 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
306 popup
->addAction(moveToTrashAction
);
308 showDeleteCommand
= true;
311 if (showDeleteCommand
) {
312 QAction
* deleteAction
= collection
->action("delete");
313 popup
->addAction(deleteAction
);
317 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
319 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
320 if (!m_mainWindow
->menuBar()->isVisible()) {
321 // TODO: it should not be necessary to uncheck the menu
322 // bar action, but currently the action states don't get
323 // updated if the menu is disabled
324 showMenuBar
->setChecked(false);
325 menu
->addAction(showMenuBar
);
326 menu
->addSeparator();
330 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
332 QString name
= url
.fileName();
333 if (name
.isEmpty()) {
339 QAction
* DolphinContextMenu::createPasteAction()
342 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
343 if (isDir
&& (m_selectedItems
.count() == 1)) {
344 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
345 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
346 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
347 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
348 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
350 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
356 KonqFileItemCapabilities
& DolphinContextMenu::capabilities()
358 if (m_capabilities
== 0) {
359 m_capabilities
= new KonqFileItemCapabilities(m_selectedItems
);
361 return *m_capabilities
;
364 #include "dolphincontextmenu.moc"