]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
SVN_SILENT made messages (.desktop file)
[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 "dolphinnewmenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinview.h"
27 #include "dolphinviewcontainer.h"
28 #include "dolphin_generalsettings.h"
29
30 #include <kactioncollection.h>
31 #include <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
33 #include <kdesktopfile.h>
34 #include <kglobal.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
37 #include <kmenu.h>
38 #include <kmenubar.h>
39 #include <kmessagebox.h>
40 #include <kmimetypetrader.h>
41 #include <knewmenu.h>
42 #include <konqmimedata.h>
43 #include <kfileitemlistproperties.h>
44 #include <konq_operations.h>
45 #include <konq_menuactions.h>
46 #include <klocale.h>
47 #include <kpropertiesdialog.h>
48 #include <krun.h>
49 #include <kstandardaction.h>
50 #include <kstandarddirs.h>
51
52 #include <QtGui/QApplication>
53 #include <QtGui/QClipboard>
54 #include <QtCore/QDir>
55
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
57 const KFileItem& fileInfo,
58 const KUrl& baseUrl) :
59 m_mainWindow(parent),
60 m_capabilities(0),
61 m_fileInfo(fileInfo),
62 m_baseUrl(baseUrl),
63 m_context(NoContext),
64 m_copyToMenu(parent),
65 m_customActions()
66 {
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();
72 }
73
74 DolphinContextMenu::~DolphinContextMenu()
75 {
76 delete m_capabilities;
77 m_capabilities = 0;
78 }
79
80 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
81 {
82 m_customActions = actions;
83 }
84
85 void DolphinContextMenu::open()
86 {
87 // get the context information
88 if (m_baseUrl.protocol() == "trash") {
89 m_context |= TrashContext;
90 }
91
92 if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) {
93 m_context |= ItemContext;
94 // TODO: handle other use cases like devices + desktop files
95 }
96
97 // open the corresponding popup for the context
98 if (m_context & TrashContext) {
99 if (m_context & ItemContext) {
100 openTrashItemContextMenu();
101 } else {
102 openTrashContextMenu();
103 }
104 } else if (m_context & ItemContext) {
105 openItemContextMenu();
106 } else {
107 Q_ASSERT(m_context == NoContext);
108 openViewportContextMenu();
109 }
110 }
111
112 void DolphinContextMenu::openTrashContextMenu()
113 {
114 Q_ASSERT(m_context & TrashContext);
115
116 KMenu* popup = new KMenu(m_mainWindow);
117
118 addShowMenubarAction(popup);
119
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);
124
125 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
126 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
127
128 addCustomActions(popup);
129
130 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
131 popup->addAction(propertiesAction);
132
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,
137 text,
138 QString(),
139 KGuiItem(i18nc("@action:button", "Empty Trash"),
140 KIcon("user-trash"))
141 ) == KMessageBox::Continue;
142 if (del) {
143 KonqOperations::emptyTrash(m_mainWindow);
144 }
145 } else if (action == addToPlacesAction) {
146 const KUrl& url = m_mainWindow->activeViewContainer()->url();
147 if (url.isValid()) {
148 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
149 }
150 }
151
152 popup->deleteLater();
153 }
154
155 void DolphinContextMenu::openTrashItemContextMenu()
156 {
157 Q_ASSERT(m_context & TrashContext);
158 Q_ASSERT(m_context & ItemContext);
159
160 KMenu* popup = new KMenu(m_mainWindow);
161
162 addShowMenubarAction(popup);
163
164 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
165 popup->addAction(restoreAction);
166
167 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
168 popup->addAction(deleteAction);
169
170 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
171 popup->addAction(propertiesAction);
172
173 if (popup->exec(QCursor::pos()) == restoreAction) {
174 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
175 }
176
177 popup->deleteLater();
178 }
179
180 void DolphinContextMenu::openItemContextMenu()
181 {
182 Q_ASSERT(!m_fileInfo.isNull());
183
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());
191
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();
197
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();
202 }
203 addShowMenubarAction(popup);
204 insertDefaultItemActions(popup);
205
206 popup->addSeparator();
207
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"));
213 }
214
215 KonqMenuActions menuActions;
216 menuActions.setParentWidget(m_mainWindow);
217 menuActions.setItemListProperties(m_selectedItems);
218
219 // insert 'Open With...' action or sub menu
220 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
221
222 // insert 'Actions' sub menu
223 if (menuActions.addActionsTo(popup)) {
224 popup->addSeparator();
225 }
226
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();
233 }
234
235 // insert 'Properties...' entry
236 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
237 popup->addAction(propertiesAction);
238
239 QAction* activatedAction = popup->exec(QCursor::pos());
240
241 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
242 const KUrl selectedUrl(m_fileInfo.url());
243 if (selectedUrl.isValid()) {
244 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
245 selectedUrl);
246 }
247 }
248
249 popup->deleteLater();
250 }
251
252 void DolphinContextMenu::openViewportContextMenu()
253 {
254 KMenu* popup = new KMenu(m_mainWindow);
255
256 addShowMenubarAction(popup);
257
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();
264
265 QAction* pasteAction = createPasteAction();
266 popup->addAction(pasteAction);
267
268 // setup 'View Mode' menu
269 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"));
270
271 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
272 viewModeMenu->addAction(iconsMode);
273
274 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
275 viewModeMenu->addAction(detailsMode);
276
277 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
278 viewModeMenu->addAction(columnsMode);
279
280 popup->addMenu(viewModeMenu);
281
282 popup->addSeparator();
283
284 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
285 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
286
287 addCustomActions(popup);
288
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();
294
295 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
296 dialog->setAttribute(Qt::WA_DeleteOnClose);
297 dialog->show();
298 } else if (action == addToPlacesAction) {
299 const KUrl& url = m_mainWindow->activeViewContainer()->url();
300 if (url.isValid()) {
301 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
302 }
303 }
304
305 popup->deleteLater();
306 }
307
308 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
309 {
310 Q_ASSERT(popup != 0);
311 const KActionCollection* collection = m_mainWindow->actionCollection();
312
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();
317
318 popup->addAction(cutAction);
319 popup->addAction(copyAction);
320 popup->addAction(pasteAction);
321 popup->addSeparator();
322
323 // insert 'Rename'
324 QAction* renameAction = collection->action("rename");
325 popup->addAction(renameAction);
326
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);
331
332 const KUrl& url = m_mainWindow->activeViewContainer()->url();
333 if (url.isLocalFile()) {
334 QAction* moveToTrashAction = collection->action("move_to_trash");
335 popup->addAction(moveToTrashAction);
336 } else {
337 showDeleteCommand = true;
338 }
339
340 if (showDeleteCommand) {
341 QAction* deleteAction = collection->action("delete");
342 popup->addAction(deleteAction);
343 }
344 }
345
346 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
347 {
348 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
349 if (!m_mainWindow->menuBar()->isVisible()) {
350 menu->addAction(showMenuBar);
351 menu->addSeparator();
352 }
353 }
354
355 QString DolphinContextMenu::placesName(const KUrl& url) const
356 {
357 QString name = url.fileName();
358 if (name.isEmpty()) {
359 name = url.host();
360 }
361 return name;
362 }
363
364 QAction* DolphinContextMenu::createPasteAction()
365 {
366 QAction* action = 0;
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()));
374 } else {
375 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
376 }
377
378 return action;
379 }
380
381 KFileItemListProperties& DolphinContextMenu::capabilities()
382 {
383 if (m_capabilities == 0) {
384 m_capabilities = new KFileItemListProperties(m_selectedItems);
385 }
386 return *m_capabilities;
387 }
388
389 void DolphinContextMenu::addCustomActions(KMenu* menu)
390 {
391 foreach (QAction* action, m_customActions) {
392 menu->addAction(action);
393 }
394 }
395
396 #include "dolphincontextmenu.moc"