]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Assure that automatically expanding of a folder also works when the new folder has...
[dolphin.git] / src / dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "dolphincontextmenu.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kfileplacesmodel.h>
31 #include <kdesktopfile.h>
32 #include <kglobal.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmenu.h>
36 #include <kmenubar.h>
37 #include <kmessagebox.h>
38 #include <kmimetypetrader.h>
39 #include <knewmenu.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>
45 #include <klocale.h>
46 #include <kpropertiesdialog.h>
47 #include <krun.h>
48 #include <kstandardaction.h>
49 #include <kstandarddirs.h>
50
51 #include <QtGui/QApplication>
52 #include <QtGui/QClipboard>
53 #include <QtCore/QDir>
54
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
56 const KFileItem& fileInfo,
57 const KUrl& baseUrl) :
58 m_mainWindow(parent),
59 m_capabilities(0),
60 m_fileInfo(fileInfo),
61 m_baseUrl(baseUrl),
62 m_context(NoContext)
63 {
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();
69 }
70
71 DolphinContextMenu::~DolphinContextMenu()
72 {
73 delete m_capabilities;
74 m_capabilities = 0;
75 }
76
77 void DolphinContextMenu::open()
78 {
79 // get the context information
80 if (m_baseUrl.protocol() == "trash") {
81 m_context |= TrashContext;
82 }
83
84 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
85 m_context |= ItemContext;
86 // TODO: handle other use cases like devices + desktop files
87 }
88
89 // open the corresponding popup for the context
90 if (m_context & TrashContext) {
91 if (m_context & ItemContext) {
92 openTrashItemContextMenu();
93 } else {
94 openTrashContextMenu();
95 }
96 } else if (m_context & ItemContext) {
97 openItemContextMenu();
98 } else {
99 Q_ASSERT(m_context == NoContext);
100 openViewportContextMenu();
101 }
102 }
103
104 void DolphinContextMenu::openTrashContextMenu()
105 {
106 Q_ASSERT(m_context & TrashContext);
107
108 KMenu* popup = new KMenu(m_mainWindow);
109
110 addShowMenubarAction(popup);
111
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);
116
117 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
118 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
119
120 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
121 popup->addAction(propertiesAction);
122
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,
127 text,
128 QString(),
129 KGuiItem(i18nc("@action:button", "Empty Trash"),
130 KIcon("user-trash"))
131 ) == KMessageBox::Continue;
132 if (del) {
133 KonqOperations::emptyTrash(m_mainWindow);
134 }
135 } else if (action == addToPlacesAction) {
136 const KUrl& url = m_mainWindow->activeViewContainer()->url();
137 if (url.isValid()) {
138 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
139 }
140 }
141
142 popup->deleteLater();
143 }
144
145 void DolphinContextMenu::openTrashItemContextMenu()
146 {
147 Q_ASSERT(m_context & TrashContext);
148 Q_ASSERT(m_context & ItemContext);
149
150 KMenu* popup = new KMenu(m_mainWindow);
151
152 addShowMenubarAction(popup);
153
154 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
155 popup->addAction(restoreAction);
156
157 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
158 popup->addAction(deleteAction);
159
160 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
161 popup->addAction(propertiesAction);
162
163 if (popup->exec(QCursor::pos()) == restoreAction) {
164 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
165 }
166
167 popup->deleteLater();
168 }
169
170 void DolphinContextMenu::openItemContextMenu()
171 {
172 Q_ASSERT(!m_fileInfo.isNull());
173
174 KMenu* popup = new KMenu(m_mainWindow);
175 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
176 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
177 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
178 popup->addSeparator();
179 }
180 addShowMenubarAction(popup);
181 insertDefaultItemActions(popup);
182
183 popup->addSeparator();
184
185 // insert 'Bookmark This Folder' entry if exactly one item is selected
186 QAction* addToPlacesAction = 0;
187 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
188 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
189 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
190 }
191
192 KonqPopupMenuInformation popupInfo;
193 popupInfo.setItems(m_selectedItems);
194 popupInfo.setParentWidget(m_mainWindow);
195 KonqMenuActions menuActions;
196 menuActions.setPopupMenuInfo(popupInfo);
197
198 // Insert 'Open With...' action or sub menu
199 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
200
201 // Insert 'Actions' sub menu
202 if (menuActions.addActionsTo(popup)) {
203 popup->addSeparator();
204 }
205
206 // Insert 'Copy To' and 'Move To' sub menus
207 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
208 m_copyToMenu.setItems(m_selectedItems);
209 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
210 m_copyToMenu.addActionsTo(popup);
211 popup->addSeparator();
212 }
213
214 // insert 'Properties...' entry
215 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
216 popup->addAction(propertiesAction);
217
218 QAction* activatedAction = popup->exec(QCursor::pos());
219
220 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
221 const KUrl selectedUrl(m_fileInfo.url());
222 if (selectedUrl.isValid()) {
223 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
224 selectedUrl);
225 }
226 }
227
228 popup->deleteLater();
229 }
230
231 void DolphinContextMenu::openViewportContextMenu()
232 {
233 KMenu* popup = new KMenu(m_mainWindow);
234
235 addShowMenubarAction(popup);
236
237 // setup 'Create New' menu
238 KNewMenu* newMenu = m_mainWindow->newMenu();
239 newMenu->slotCheckUpToDate();
240 newMenu->setPopupFiles(m_baseUrl);
241 popup->addMenu(newMenu->menu());
242 popup->addSeparator();
243
244 QAction* pasteAction = createPasteAction();
245 popup->addAction(pasteAction);
246
247 // setup 'View Mode' menu
248 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
249
250 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
251 viewModeMenu->addAction(iconsMode);
252
253 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
254 viewModeMenu->addAction(detailsMode);
255
256 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
257 viewModeMenu->addAction(columnsMode);
258
259 popup->addMenu(viewModeMenu);
260
261 popup->addSeparator();
262
263 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
264 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
265 popup->addSeparator();
266
267 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
268
269 QAction* action = popup->exec(QCursor::pos());
270 if (action == propertiesAction) {
271 const KUrl& url = m_mainWindow->activeViewContainer()->url();
272
273 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
274 dialog->setAttribute(Qt::WA_DeleteOnClose);
275 dialog->show();
276 dialog->raise();
277 dialog->activateWindow();
278 } else if (action == addToPlacesAction) {
279 const KUrl& url = m_mainWindow->activeViewContainer()->url();
280 if (url.isValid()) {
281 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
282 }
283 }
284
285 popup->deleteLater();
286 }
287
288 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
289 {
290 Q_ASSERT(popup != 0);
291 const KActionCollection* collection = m_mainWindow->actionCollection();
292
293 // insert 'Cut', 'Copy' and 'Paste'
294 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
295 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
296 QAction* pasteAction = createPasteAction();
297
298 popup->addAction(cutAction);
299 popup->addAction(copyAction);
300 popup->addAction(pasteAction);
301 popup->addSeparator();
302
303 // insert 'Rename'
304 QAction* renameAction = collection->action("rename");
305 popup->addAction(renameAction);
306
307 // insert 'Move to Trash' and (optionally) 'Delete'
308 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
309 KConfigGroup configGroup(globalConfig, "KDE");
310 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
311
312 const KUrl& url = m_mainWindow->activeViewContainer()->url();
313 if (url.isLocalFile()) {
314 QAction* moveToTrashAction = collection->action("move_to_trash");
315 popup->addAction(moveToTrashAction);
316 } else {
317 showDeleteCommand = true;
318 }
319
320 if (showDeleteCommand) {
321 QAction* deleteAction = collection->action("delete");
322 popup->addAction(deleteAction);
323 }
324 }
325
326 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
327 {
328 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
329 if (!m_mainWindow->menuBar()->isVisible()) {
330 menu->addAction(showMenuBar);
331 menu->addSeparator();
332 }
333 }
334
335 QString DolphinContextMenu::placesName(const KUrl& url) const
336 {
337 QString name = url.fileName();
338 if (name.isEmpty()) {
339 name = url.host();
340 }
341 return name;
342 }
343
344 QAction* DolphinContextMenu::createPasteAction()
345 {
346 QAction* action = 0;
347 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
348 if (isDir && (m_selectedItems.count() == 1)) {
349 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
350 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
351 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
352 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
353 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
354 } else {
355 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
356 }
357
358 return action;
359 }
360
361 KonqFileItemCapabilities& DolphinContextMenu::capabilities()
362 {
363 if (m_capabilities == 0) {
364 m_capabilities = new KonqFileItemCapabilities(m_selectedItems);
365 }
366 return *m_capabilities;
367 }
368
369 #include "dolphincontextmenu.moc"