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 <kdesktopfile.h>
32 #include <kfileitemlistproperties.h>
33 #include <kfileplacesmodel.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
39 #include <kmessagebox.h>
40 #include <kmimetypetrader.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <konq_menuactions.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
) :
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 const 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::setCustomActions(const QList
<QAction
*>& actions
)
81 m_customActions
= actions
;
84 void DolphinContextMenu::open()
86 // get the context information
87 if (m_baseUrl
.protocol() == "trash") {
88 m_context
|= TrashContext
;
91 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
92 m_context
|= ItemContext
;
93 // TODO: handle other use cases like devices + desktop files
96 // open the corresponding popup for the context
97 if (m_context
& TrashContext
) {
98 if (m_context
& ItemContext
) {
99 openTrashItemContextMenu();
101 openTrashContextMenu();
103 } else if (m_context
& ItemContext
) {
104 openItemContextMenu();
106 Q_ASSERT(m_context
== NoContext
);
107 openViewportContextMenu();
111 void DolphinContextMenu::openTrashContextMenu()
113 Q_ASSERT(m_context
& TrashContext
);
115 KMenu
* popup
= new KMenu(m_mainWindow
);
117 addShowMenubarAction(popup
);
119 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
120 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
121 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
122 popup
->addAction(emptyTrashAction
);
124 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
125 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
127 // Don't show if url is already in places
128 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
129 addToPlacesAction
->setVisible(false);
132 addCustomActions(popup
);
134 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
135 popup
->addAction(propertiesAction
);
137 QAction
*action
= popup
->exec(QCursor::pos());
138 if (action
== emptyTrashAction
) {
139 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
140 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
143 KGuiItem(i18nc("@action:button", "Empty Trash"),
145 ) == KMessageBox::Continue
;
147 KonqOperations::emptyTrash(m_mainWindow
);
149 } else if (action
== addToPlacesAction
) {
150 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
152 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
156 popup
->deleteLater();
159 void DolphinContextMenu::openTrashItemContextMenu()
161 Q_ASSERT(m_context
& TrashContext
);
162 Q_ASSERT(m_context
& ItemContext
);
164 KMenu
* popup
= new KMenu(m_mainWindow
);
166 addShowMenubarAction(popup
);
168 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
169 popup
->addAction(restoreAction
);
171 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
172 popup
->addAction(deleteAction
);
174 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
175 popup
->addAction(propertiesAction
);
177 if (popup
->exec(QCursor::pos()) == restoreAction
) {
178 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
181 popup
->deleteLater();
184 void DolphinContextMenu::openItemContextMenu()
186 Q_ASSERT(!m_fileInfo
.isNull());
188 KMenu
* popup
= new KMenu(m_mainWindow
);
189 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
190 // setup 'Create New' menu
191 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
192 newMenu
->slotCheckUpToDate();
193 newMenu
->setPopupFiles(m_fileInfo
.url());
194 newMenu
->setEnabled(capabilities().supportsWriting());
196 KMenu
* menu
= newMenu
->menu();
197 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
198 menu
->setIcon(KIcon("document-new"));
199 popup
->addMenu(newMenu
->menu());
200 popup
->addSeparator();
202 // insert 'Open in new window' and 'Open in new tab' entries
203 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
204 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
205 popup
->addSeparator();
207 addShowMenubarAction(popup
);
208 insertDefaultItemActions(popup
);
210 popup
->addSeparator();
212 // insert 'Bookmark This Folder' entry if exactly one item is selected
213 QAction
* addToPlacesAction
= 0;
214 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
215 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
216 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
217 // Don't show if url is already in places
218 if (placeExists(m_fileInfo
.url())) {
219 addToPlacesAction
->setVisible(false);
223 KonqMenuActions menuActions
;
224 menuActions
.setParentWidget(m_mainWindow
);
225 menuActions
.setItemListProperties(m_selectedItems
);
227 // insert 'Open With...' action or sub menu
228 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
230 // insert 'Actions' sub menu
231 if (menuActions
.addActionsTo(popup
)) {
232 popup
->addSeparator();
235 // insert version control actions
236 addRevisionControlActions(popup
);
238 // insert 'Copy To' and 'Move To' sub menus
239 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
240 m_copyToMenu
.setItems(m_selectedItems
);
241 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
242 m_copyToMenu
.addActionsTo(popup
);
243 popup
->addSeparator();
246 // insert 'Properties...' entry
247 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
248 popup
->addAction(propertiesAction
);
250 QAction
* activatedAction
= popup
->exec(QCursor::pos());
252 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
253 const KUrl
selectedUrl(m_fileInfo
.url());
254 if (selectedUrl
.isValid()) {
255 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
260 popup
->deleteLater();
263 void DolphinContextMenu::openViewportContextMenu()
265 KMenu
* popup
= new KMenu(m_mainWindow
);
267 addShowMenubarAction(popup
);
269 // setup 'Create New' menu
270 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
271 newMenu
->slotCheckUpToDate();
272 newMenu
->setPopupFiles(m_baseUrl
);
273 popup
->addMenu(newMenu
->menu());
274 popup
->addSeparator();
276 QAction
* pasteAction
= createPasteAction();
277 popup
->addAction(pasteAction
);
279 // setup 'View Mode' menu
280 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"), popup
);
282 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
283 viewModeMenu
->addAction(iconsMode
);
285 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
286 viewModeMenu
->addAction(detailsMode
);
288 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
289 viewModeMenu
->addAction(columnsMode
);
291 popup
->addMenu(viewModeMenu
);
293 popup
->addSeparator();
295 addRevisionControlActions(popup
);
297 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
298 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
300 // Don't show if url is already in places
301 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
302 addToPlacesAction
->setVisible(false);
305 addCustomActions(popup
);
307 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
308 propertiesAction
->setIcon(KIcon("document-properties"));
309 QAction
* action
= popup
->exec(QCursor::pos());
310 if (action
== propertiesAction
) {
311 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
313 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
314 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
316 } else if (action
== addToPlacesAction
) {
317 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
319 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
323 popup
->deleteLater();
326 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
328 Q_ASSERT(popup
!= 0);
329 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
331 // insert 'Cut', 'Copy' and 'Paste'
332 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
333 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
334 QAction
* pasteAction
= createPasteAction();
336 popup
->addAction(cutAction
);
337 popup
->addAction(copyAction
);
338 popup
->addAction(pasteAction
);
339 popup
->addSeparator();
342 QAction
* renameAction
= collection
->action("rename");
343 popup
->addAction(renameAction
);
345 // insert 'Move to Trash' and (optionally) 'Delete'
346 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
347 KConfigGroup
configGroup(globalConfig
, "KDE");
348 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
350 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
351 if (url
.isLocalFile()) {
352 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
353 popup
->addAction(moveToTrashAction
);
355 showDeleteCommand
= true;
358 if (showDeleteCommand
) {
359 QAction
* deleteAction
= collection
->action("delete");
360 popup
->addAction(deleteAction
);
364 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
366 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
367 if (!m_mainWindow
->menuBar()->isVisible()) {
368 menu
->addAction(showMenuBar
);
369 menu
->addSeparator();
373 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
375 QString name
= url
.fileName();
376 if (name
.isEmpty()) {
382 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
384 const KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
385 const int count
= placesModel
->rowCount();
387 for (int i
= 0; i
< count
; ++i
) {
388 const QModelIndex index
= placesModel
->index(i
, 0);
390 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
397 QAction
* DolphinContextMenu::createPasteAction()
400 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
401 if (isDir
&& (m_selectedItems
.count() == 1)) {
402 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
403 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
404 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
405 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
406 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
408 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
414 KFileItemListProperties
& DolphinContextMenu::capabilities()
416 if (m_capabilities
== 0) {
417 m_capabilities
= new KFileItemListProperties(m_selectedItems
);
419 return *m_capabilities
;
422 void DolphinContextMenu::addRevisionControlActions(KMenu
* menu
)
424 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
425 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
426 if (!versionControlActions
.isEmpty()) {
427 foreach (QAction
* action
, versionControlActions
) {
428 menu
->addAction(action
);
430 menu
->addSeparator();
434 void DolphinContextMenu::addCustomActions(KMenu
* menu
)
436 foreach (QAction
* action
, m_customActions
) {
437 menu
->addAction(action
);
441 #include "dolphincontextmenu.moc"