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
) :
66 // The context menu either accesses the URLs of the selected items
67 // or the items itself. To increase the performance both lists are cached.
68 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
69 m_selectedUrls
= view
->selectedUrls();
70 m_selectedItems
= view
->selectedItems();
73 DolphinContextMenu::~DolphinContextMenu()
75 delete m_capabilities
;
79 void DolphinContextMenu::open()
81 // get the context information
82 if (m_baseUrl
.protocol() == "trash") {
83 m_context
|= TrashContext
;
86 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
87 m_context
|= ItemContext
;
88 // TODO: handle other use cases like devices + desktop files
91 // open the corresponding popup for the context
92 if (m_context
& TrashContext
) {
93 if (m_context
& ItemContext
) {
94 openTrashItemContextMenu();
96 openTrashContextMenu();
98 } else if (m_context
& ItemContext
) {
99 openItemContextMenu();
101 Q_ASSERT(m_context
== NoContext
);
102 openViewportContextMenu();
106 void DolphinContextMenu::openTrashContextMenu()
108 Q_ASSERT(m_context
& TrashContext
);
110 KMenu
* popup
= new KMenu(m_mainWindow
);
112 addShowMenubarAction(popup
);
114 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
115 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
116 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
117 popup
->addAction(emptyTrashAction
);
119 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
120 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
122 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
123 popup
->addAction(propertiesAction
);
125 QAction
*action
= popup
->exec(QCursor::pos());
126 if (action
== emptyTrashAction
) {
127 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
128 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
131 KGuiItem(i18nc("@action:button", "Empty Trash"),
133 ) == KMessageBox::Continue
;
135 KonqOperations::emptyTrash(m_mainWindow
);
137 } else if (action
== addToPlacesAction
) {
138 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
140 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
144 popup
->deleteLater();
147 void DolphinContextMenu::openTrashItemContextMenu()
149 Q_ASSERT(m_context
& TrashContext
);
150 Q_ASSERT(m_context
& ItemContext
);
152 KMenu
* popup
= new KMenu(m_mainWindow
);
154 addShowMenubarAction(popup
);
156 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
157 popup
->addAction(restoreAction
);
159 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
160 popup
->addAction(deleteAction
);
162 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
163 popup
->addAction(propertiesAction
);
165 if (popup
->exec(QCursor::pos()) == restoreAction
) {
166 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
169 popup
->deleteLater();
172 void DolphinContextMenu::openItemContextMenu()
174 Q_ASSERT(!m_fileInfo
.isNull());
176 KMenu
* popup
= new KMenu(m_mainWindow
);
177 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
178 // setup 'Create New' menu
179 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
180 newMenu
->slotCheckUpToDate();
181 newMenu
->setPopupFiles(m_fileInfo
.url());
182 newMenu
->setEnabled(capabilities().supportsWriting());
184 KMenu
* menu
= newMenu
->menu();
185 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
186 menu
->setIcon(KIcon("document-new"));
187 popup
->addMenu(newMenu
->menu());
188 popup
->addSeparator();
190 // insert 'Open in new window' and 'Open in new tab' entries
191 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
192 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
193 popup
->addSeparator();
195 addShowMenubarAction(popup
);
196 insertDefaultItemActions(popup
);
198 popup
->addSeparator();
200 // insert 'Bookmark This Folder' entry if exactly one item is selected
201 QAction
* addToPlacesAction
= 0;
202 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
203 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
204 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
207 KonqPopupMenuInformation popupInfo
;
208 popupInfo
.setItems(m_selectedItems
);
209 popupInfo
.setParentWidget(m_mainWindow
);
210 KonqMenuActions menuActions
;
211 menuActions
.setPopupMenuInfo(popupInfo
);
213 // Insert 'Open With...' action or sub menu
214 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
216 // Insert 'Actions' sub menu
217 if (menuActions
.addActionsTo(popup
)) {
218 popup
->addSeparator();
221 // Insert 'Copy To' and 'Move To' sub menus
222 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
223 m_copyToMenu
.setItems(m_selectedItems
);
224 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
225 m_copyToMenu
.addActionsTo(popup
);
226 popup
->addSeparator();
229 // insert 'Properties...' entry
230 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
231 popup
->addAction(propertiesAction
);
233 QAction
* activatedAction
= popup
->exec(QCursor::pos());
235 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
236 const KUrl
selectedUrl(m_fileInfo
.url());
237 if (selectedUrl
.isValid()) {
238 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
243 popup
->deleteLater();
246 void DolphinContextMenu::openViewportContextMenu()
248 KMenu
* popup
= new KMenu(m_mainWindow
);
250 addShowMenubarAction(popup
);
252 // setup 'Create New' menu
253 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
254 newMenu
->slotCheckUpToDate();
255 newMenu
->setPopupFiles(m_baseUrl
);
256 popup
->addMenu(newMenu
->menu());
257 popup
->addSeparator();
259 QAction
* pasteAction
= createPasteAction();
260 popup
->addAction(pasteAction
);
262 // setup 'View Mode' menu
263 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
265 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
266 viewModeMenu
->addAction(iconsMode
);
268 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
269 viewModeMenu
->addAction(detailsMode
);
271 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
272 viewModeMenu
->addAction(columnsMode
);
274 popup
->addMenu(viewModeMenu
);
276 popup
->addSeparator();
278 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
279 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
280 popup
->addSeparator();
282 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
284 QAction
* action
= popup
->exec(QCursor::pos());
285 if (action
== propertiesAction
) {
286 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
288 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
289 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
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"