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 <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
33 #include <kdesktopfile.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
39 #include <kmessagebox.h>
40 #include <kmimetypetrader.h>
42 #include <konqmimedata.h>
43 #include <kfileitemlistproperties.h>
44 #include <konq_operations.h>
45 #include <konq_menuactions.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
) :
67 // The context menu either accesses the URLs of the selected items
68 // or the items itself. To increase the performance both lists are cached.
69 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
70 m_selectedUrls
= view
->selectedUrls();
71 m_selectedItems
= view
->selectedItems();
74 DolphinContextMenu::~DolphinContextMenu()
76 delete m_capabilities
;
80 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
82 m_customActions
= actions
;
85 void DolphinContextMenu::open()
87 // get the context information
88 if (m_baseUrl
.protocol() == "trash") {
89 m_context
|= TrashContext
;
92 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
93 m_context
|= ItemContext
;
94 // TODO: handle other use cases like devices + desktop files
97 // open the corresponding popup for the context
98 if (m_context
& TrashContext
) {
99 if (m_context
& ItemContext
) {
100 openTrashItemContextMenu();
102 openTrashContextMenu();
104 } else if (m_context
& ItemContext
) {
105 openItemContextMenu();
107 Q_ASSERT(m_context
== NoContext
);
108 openViewportContextMenu();
112 void DolphinContextMenu::openTrashContextMenu()
114 Q_ASSERT(m_context
& TrashContext
);
116 KMenu
* popup
= new KMenu(m_mainWindow
);
118 addShowMenubarAction(popup
);
120 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
121 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
122 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
123 popup
->addAction(emptyTrashAction
);
125 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
126 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
128 addCustomActions(popup
);
130 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
131 popup
->addAction(propertiesAction
);
133 QAction
*action
= popup
->exec(QCursor::pos());
134 if (action
== emptyTrashAction
) {
135 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
136 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
139 KGuiItem(i18nc("@action:button", "Empty Trash"),
141 ) == KMessageBox::Continue
;
143 KonqOperations::emptyTrash(m_mainWindow
);
145 } else if (action
== addToPlacesAction
) {
146 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
148 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
152 popup
->deleteLater();
155 void DolphinContextMenu::openTrashItemContextMenu()
157 Q_ASSERT(m_context
& TrashContext
);
158 Q_ASSERT(m_context
& ItemContext
);
160 KMenu
* popup
= new KMenu(m_mainWindow
);
162 addShowMenubarAction(popup
);
164 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
165 popup
->addAction(restoreAction
);
167 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
168 popup
->addAction(deleteAction
);
170 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
171 popup
->addAction(propertiesAction
);
173 if (popup
->exec(QCursor::pos()) == restoreAction
) {
174 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
177 popup
->deleteLater();
180 void DolphinContextMenu::openItemContextMenu()
182 Q_ASSERT(!m_fileInfo
.isNull());
184 KMenu
* popup
= new KMenu(m_mainWindow
);
185 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
186 // setup 'Create New' menu
187 DolphinNewMenu
* newMenu
= new DolphinNewMenu(popup
, m_mainWindow
);
188 newMenu
->slotCheckUpToDate();
189 newMenu
->setPopupFiles(m_fileInfo
.url());
190 newMenu
->setEnabled(capabilities().supportsWriting());
192 KMenu
* menu
= newMenu
->menu();
193 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
194 menu
->setIcon(KIcon("document-new"));
195 popup
->addMenu(newMenu
->menu());
196 popup
->addSeparator();
198 // insert 'Open in new window' and 'Open in new tab' entries
199 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
200 popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
201 popup
->addSeparator();
203 addShowMenubarAction(popup
);
204 insertDefaultItemActions(popup
);
206 popup
->addSeparator();
208 // insert 'Bookmark This Folder' entry if exactly one item is selected
209 QAction
* addToPlacesAction
= 0;
210 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
211 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
212 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
215 KonqMenuActions menuActions
;
216 menuActions
.setParentWidget(m_mainWindow
);
217 menuActions
.setItemListProperties(m_selectedItems
);
219 // insert 'Open With...' action or sub menu
220 menuActions
.addOpenWithActionsTo(popup
, "DesktopEntryName != 'dolphin'");
222 // insert 'Actions' sub menu
223 if (menuActions
.addActionsTo(popup
)) {
224 popup
->addSeparator();
227 // insert 'Copy To' and 'Move To' sub menus
228 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
229 m_copyToMenu
.setItems(m_selectedItems
);
230 m_copyToMenu
.setReadOnly(!capabilities().supportsWriting());
231 m_copyToMenu
.addActionsTo(popup
);
232 popup
->addSeparator();
235 // insert 'Properties...' entry
236 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
237 popup
->addAction(propertiesAction
);
239 QAction
* activatedAction
= popup
->exec(QCursor::pos());
241 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
242 const KUrl
selectedUrl(m_fileInfo
.url());
243 if (selectedUrl
.isValid()) {
244 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
249 popup
->deleteLater();
252 void DolphinContextMenu::openViewportContextMenu()
254 KMenu
* popup
= new KMenu(m_mainWindow
);
256 addShowMenubarAction(popup
);
258 // setup 'Create New' menu
259 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
260 newMenu
->slotCheckUpToDate();
261 newMenu
->setPopupFiles(m_baseUrl
);
262 popup
->addMenu(newMenu
->menu());
263 popup
->addSeparator();
265 QAction
* pasteAction
= createPasteAction();
266 popup
->addAction(pasteAction
);
268 // setup 'View Mode' menu
269 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
271 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
272 viewModeMenu
->addAction(iconsMode
);
274 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
275 viewModeMenu
->addAction(detailsMode
);
277 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
278 viewModeMenu
->addAction(columnsMode
);
280 popup
->addMenu(viewModeMenu
);
282 popup
->addSeparator();
284 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
285 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
287 addCustomActions(popup
);
289 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
290 propertiesAction
->setIcon(KIcon("document-properties"));
291 QAction
* action
= popup
->exec(QCursor::pos());
292 if (action
== propertiesAction
) {
293 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
295 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
296 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
298 } else if (action
== addToPlacesAction
) {
299 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
301 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
305 popup
->deleteLater();
308 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
310 Q_ASSERT(popup
!= 0);
311 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
313 // insert 'Cut', 'Copy' and 'Paste'
314 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
315 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
316 QAction
* pasteAction
= createPasteAction();
318 popup
->addAction(cutAction
);
319 popup
->addAction(copyAction
);
320 popup
->addAction(pasteAction
);
321 popup
->addSeparator();
324 QAction
* renameAction
= collection
->action("rename");
325 popup
->addAction(renameAction
);
327 // insert 'Move to Trash' and (optionally) 'Delete'
328 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
329 KConfigGroup
configGroup(globalConfig
, "KDE");
330 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
332 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
333 if (url
.isLocalFile()) {
334 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
335 popup
->addAction(moveToTrashAction
);
337 showDeleteCommand
= true;
340 if (showDeleteCommand
) {
341 QAction
* deleteAction
= collection
->action("delete");
342 popup
->addAction(deleteAction
);
346 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
348 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
349 if (!m_mainWindow
->menuBar()->isVisible()) {
350 menu
->addAction(showMenuBar
);
351 menu
->addSeparator();
355 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
357 QString name
= url
.fileName();
358 if (name
.isEmpty()) {
364 QAction
* DolphinContextMenu::createPasteAction()
367 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
368 if (isDir
&& (m_selectedItems
.count() == 1)) {
369 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
370 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
371 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
372 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
373 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
375 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
381 KFileItemListProperties
& DolphinContextMenu::capabilities()
383 if (m_capabilities
== 0) {
384 m_capabilities
= new KFileItemListProperties(m_selectedItems
);
386 return *m_capabilities
;
389 void DolphinContextMenu::addCustomActions(KMenu
* menu
)
391 foreach (QAction
* action
, m_customActions
) {
392 menu
->addAction(action
);
396 #include "dolphincontextmenu.moc"