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 "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());
181 newMenu
->setEnabled(capabilities().supportsWriting());
183 KMenu
* menu
= newMenu
->menu();
184 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
185 menu
->setIcon(KIcon("document-new"));
186 popup
->addMenu(newMenu
->menu());
187 popup
->addSeparator();
189 // insert 'Open in new window' and 'Open in new tab' entries
190 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
191 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
192 popup
->addSeparator();
194 addShowMenubarAction(popup
);
195 insertDefaultItemActions(popup
);
197 popup
->addSeparator();
199 // insert 'Bookmark This Folder' entry if exactly one item is selected
200 QAction
* addToPlacesAction
= 0;
201 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
202 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
203 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
206 KonqPopupMenuInformation popupInfo
;
207 popupInfo
.setItems(m_selectedItems
);
208 popupInfo
.setParentWidget(m_mainWindow
);
209 KonqMenuActions menuActions
;
210 menuActions
.setPopupMenuInfo(popupInfo
);
212 // Insert 'Open With...' action or sub menu
213 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
215 // Insert 'Actions' sub menu
216 if (menuActions
.addActionsTo(popup
)) {
217 popup
->addSeparator();
220 // Insert 'Copy To' and 'Move To' sub menus
221 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
222 m_copyToMenu
.setItems(m_selectedItems
);
223 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
224 m_copyToMenu
.addActionsTo(popup
);
225 popup
->addSeparator();
228 // insert 'Properties...' entry
229 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
230 popup
->addAction(propertiesAction
);
232 QAction
* activatedAction
= popup
->exec(QCursor::pos());
234 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
235 const KUrl
selectedUrl(m_fileInfo
.url());
236 if (selectedUrl
.isValid()) {
237 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
242 popup
->deleteLater();
245 void DolphinContextMenu::openViewportContextMenu()
247 KMenu
* popup
= new KMenu(m_mainWindow
);
249 addShowMenubarAction(popup
);
251 // setup 'Create New' menu
252 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
253 newMenu
->slotCheckUpToDate();
254 newMenu
->setPopupFiles(m_baseUrl
);
255 popup
->addMenu(newMenu
->menu());
256 popup
->addSeparator();
258 QAction
* pasteAction
= createPasteAction();
259 popup
->addAction(pasteAction
);
261 // setup 'View Mode' menu
262 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
264 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
265 viewModeMenu
->addAction(iconsMode
);
267 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
268 viewModeMenu
->addAction(detailsMode
);
270 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
271 viewModeMenu
->addAction(columnsMode
);
273 popup
->addMenu(viewModeMenu
);
275 popup
->addSeparator();
277 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
278 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
279 popup
->addSeparator();
281 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
283 QAction
* action
= popup
->exec(QCursor::pos());
284 if (action
== propertiesAction
) {
285 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
287 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
288 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
290 } else if (action
== addToPlacesAction
) {
291 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
293 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
297 popup
->deleteLater();
300 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
302 Q_ASSERT(popup
!= 0);
303 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
305 // insert 'Cut', 'Copy' and 'Paste'
306 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
307 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
308 QAction
* pasteAction
= createPasteAction();
310 popup
->addAction(cutAction
);
311 popup
->addAction(copyAction
);
312 popup
->addAction(pasteAction
);
313 popup
->addSeparator();
316 QAction
* renameAction
= collection
->action("rename");
317 popup
->addAction(renameAction
);
319 // insert 'Move to Trash' and (optionally) 'Delete'
320 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
321 KConfigGroup
configGroup(globalConfig
, "KDE");
322 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
324 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
325 if (url
.isLocalFile()) {
326 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
327 popup
->addAction(moveToTrashAction
);
329 showDeleteCommand
= true;
332 if (showDeleteCommand
) {
333 QAction
* deleteAction
= collection
->action("delete");
334 popup
->addAction(deleteAction
);
338 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
340 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
341 if (!m_mainWindow
->menuBar()->isVisible()) {
342 menu
->addAction(showMenuBar
);
343 menu
->addSeparator();
347 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
349 QString name
= url
.fileName();
350 if (name
.isEmpty()) {
356 QAction
* DolphinContextMenu::createPasteAction()
359 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
360 if (isDir
&& (m_selectedItems
.count() == 1)) {
361 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
362 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
363 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
364 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
365 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
367 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
373 KonqFileItemCapabilities
& DolphinContextMenu::capabilities()
375 if (m_capabilities
== 0) {
376 m_capabilities
= new KonqFileItemCapabilities(m_selectedItems
);
378 return *m_capabilities
;
381 #include "dolphincontextmenu.moc"