]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Warn when the unsuspecting user is about to create a directory that starts with a...
[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 <kdesktopfile.h>
32 #include <kfileitemlistproperties.h>
33 #include <kfileplacesmodel.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 <konq_operations.h>
44 #include <konq_menuactions.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 m_copyToMenu(parent),
64 m_customActions()
65 {
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();
71 }
72
73 DolphinContextMenu::~DolphinContextMenu()
74 {
75 delete m_capabilities;
76 m_capabilities = 0;
77 }
78
79 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
80 {
81 m_customActions = actions;
82 }
83
84 void DolphinContextMenu::open()
85 {
86 // get the context information
87 if (m_baseUrl.protocol() == "trash") {
88 m_context |= TrashContext;
89 }
90
91 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
92 m_context |= ItemContext;
93 // TODO: handle other use cases like devices + desktop files
94 }
95
96 // open the corresponding popup for the context
97 if (m_context & TrashContext) {
98 if (m_context & ItemContext) {
99 openTrashItemContextMenu();
100 } else {
101 openTrashContextMenu();
102 }
103 } else if (m_context & ItemContext) {
104 openItemContextMenu();
105 } else {
106 Q_ASSERT(m_context == NoContext);
107 openViewportContextMenu();
108 }
109 }
110
111 void DolphinContextMenu::openTrashContextMenu()
112 {
113 Q_ASSERT(m_context & TrashContext);
114
115 KMenu* popup = new KMenu(m_mainWindow);
116
117 addShowMenubarAction(popup);
118
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);
123
124 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
125 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
126
127 // Don't show if url is already in places
128 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
129 addToPlacesAction->setVisible(false);
130 }
131
132 addCustomActions(popup);
133
134 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
135 popup->addAction(propertiesAction);
136
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,
141 text,
142 QString(),
143 KGuiItem(i18nc("@action:button", "Empty Trash"),
144 KIcon("user-trash"))
145 ) == KMessageBox::Continue;
146 if (del) {
147 KonqOperations::emptyTrash(m_mainWindow);
148 }
149 } else if (action == addToPlacesAction) {
150 const KUrl& url = m_mainWindow->activeViewContainer()->url();
151 if (url.isValid()) {
152 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
153 }
154 }
155
156 popup->deleteLater();
157 }
158
159 void DolphinContextMenu::openTrashItemContextMenu()
160 {
161 Q_ASSERT(m_context & TrashContext);
162 Q_ASSERT(m_context & ItemContext);
163
164 KMenu* popup = new KMenu(m_mainWindow);
165
166 addShowMenubarAction(popup);
167
168 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
169 popup->addAction(restoreAction);
170
171 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
172 popup->addAction(deleteAction);
173
174 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
175 popup->addAction(propertiesAction);
176
177 if (popup->exec(QCursor::pos()) == restoreAction) {
178 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
179 }
180
181 popup->deleteLater();
182 }
183
184 void DolphinContextMenu::openItemContextMenu()
185 {
186 Q_ASSERT(!m_fileInfo.isNull());
187
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 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
193 newMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
194 newMenu->slotCheckUpToDate();
195 newMenu->setPopupFiles(m_fileInfo.url());
196 newMenu->setEnabled(capabilities().supportsWriting());
197
198 KMenu* menu = newMenu->menu();
199 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
200 menu->setIcon(KIcon("document-new"));
201 popup->addMenu(newMenu->menu());
202 popup->addSeparator();
203
204 // insert 'Open in new window' and 'Open in new tab' entries
205 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
206 popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
207 popup->addSeparator();
208 }
209 addShowMenubarAction(popup);
210 insertDefaultItemActions(popup);
211
212 popup->addSeparator();
213
214 // insert 'Bookmark This Folder' entry if exactly one item is selected
215 QAction* addToPlacesAction = 0;
216 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
217 addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
218 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
219 // Don't show if url is already in places
220 if (placeExists(m_fileInfo.url())) {
221 addToPlacesAction->setVisible(false);
222 }
223 }
224
225 KonqMenuActions menuActions;
226 menuActions.setParentWidget(m_mainWindow);
227 menuActions.setItemListProperties(m_selectedItems);
228
229 // insert 'Open With...' action or sub menu
230 menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'");
231
232 // insert 'Actions' sub menu
233 if (menuActions.addActionsTo(popup)) {
234 popup->addSeparator();
235 }
236
237 // insert version control actions
238 addRevisionControlActions(popup);
239
240 // insert 'Copy To' and 'Move To' sub menus
241 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
242 m_copyToMenu.setItems(m_selectedItems);
243 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
244 m_copyToMenu.addActionsTo(popup);
245 popup->addSeparator();
246 }
247
248 // insert 'Properties...' entry
249 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
250 popup->addAction(propertiesAction);
251
252 QAction* activatedAction = popup->exec(QCursor::pos());
253
254 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
255 const KUrl selectedUrl(m_fileInfo.url());
256 if (selectedUrl.isValid()) {
257 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
258 selectedUrl);
259 }
260 }
261
262 popup->deleteLater();
263 }
264
265 void DolphinContextMenu::openViewportContextMenu()
266 {
267 KMenu* popup = new KMenu(m_mainWindow);
268
269 addShowMenubarAction(popup);
270
271 // setup 'Create New' menu
272 KNewMenu* newMenu = m_mainWindow->newMenu();
273 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
274 newMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
275 newMenu->slotCheckUpToDate();
276 newMenu->setPopupFiles(m_baseUrl);
277 popup->addMenu(newMenu->menu());
278 popup->addSeparator();
279
280 QAction* pasteAction = createPasteAction();
281 popup->addAction(pasteAction);
282
283 // setup 'View Mode' menu
284 KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"), popup);
285
286 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
287 viewModeMenu->addAction(iconsMode);
288
289 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
290 viewModeMenu->addAction(detailsMode);
291
292 QAction* columnsMode = m_mainWindow->actionCollection()->action("columns");
293 viewModeMenu->addAction(columnsMode);
294
295 popup->addMenu(viewModeMenu);
296
297 popup->addSeparator();
298
299 addRevisionControlActions(popup);
300
301 QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"),
302 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
303
304 // Don't show if url is already in places
305 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
306 addToPlacesAction->setVisible(false);
307 }
308
309 addCustomActions(popup);
310
311 QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties"));
312 propertiesAction->setIcon(KIcon("document-properties"));
313 QAction* action = popup->exec(QCursor::pos());
314 if (action == propertiesAction) {
315 const KUrl& url = m_mainWindow->activeViewContainer()->url();
316
317 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
318 dialog->setAttribute(Qt::WA_DeleteOnClose);
319 dialog->show();
320 } else if (action == addToPlacesAction) {
321 const KUrl& url = m_mainWindow->activeViewContainer()->url();
322 if (url.isValid()) {
323 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
324 }
325 }
326
327 popup->deleteLater();
328 }
329
330 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
331 {
332 Q_ASSERT(popup != 0);
333 const KActionCollection* collection = m_mainWindow->actionCollection();
334
335 // insert 'Cut', 'Copy' and 'Paste'
336 QAction* cutAction = collection->action(KStandardAction::name(KStandardAction::Cut));
337 QAction* copyAction = collection->action(KStandardAction::name(KStandardAction::Copy));
338 QAction* pasteAction = createPasteAction();
339
340 popup->addAction(cutAction);
341 popup->addAction(copyAction);
342 popup->addAction(pasteAction);
343 popup->addSeparator();
344
345 // insert 'Rename'
346 QAction* renameAction = collection->action("rename");
347 popup->addAction(renameAction);
348
349 // insert 'Move to Trash' and (optionally) 'Delete'
350 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
351 KConfigGroup configGroup(globalConfig, "KDE");
352 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
353
354 const KUrl& url = m_mainWindow->activeViewContainer()->url();
355 if (url.isLocalFile()) {
356 QAction* moveToTrashAction = collection->action("move_to_trash");
357 popup->addAction(moveToTrashAction);
358 } else {
359 showDeleteCommand = true;
360 }
361
362 if (showDeleteCommand) {
363 QAction* deleteAction = collection->action("delete");
364 popup->addAction(deleteAction);
365 }
366 }
367
368 void DolphinContextMenu::addShowMenubarAction(KMenu* menu)
369 {
370 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
371 if (!m_mainWindow->menuBar()->isVisible()) {
372 menu->addAction(showMenuBar);
373 menu->addSeparator();
374 }
375 }
376
377 QString DolphinContextMenu::placesName(const KUrl& url) const
378 {
379 QString name = url.fileName();
380 if (name.isEmpty()) {
381 name = url.host();
382 }
383 return name;
384 }
385
386 bool DolphinContextMenu::placeExists(const KUrl& url) const
387 {
388 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
389 const int count = placesModel->rowCount();
390
391 for (int i = 0; i < count; ++i) {
392 const QModelIndex index = placesModel->index(i, 0);
393
394 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
395 return true;
396 }
397 }
398 return false;
399 }
400
401 QAction* DolphinContextMenu::createPasteAction()
402 {
403 QAction* action = 0;
404 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
405 if (isDir && (m_selectedItems.count() == 1)) {
406 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
407 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
408 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
409 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
410 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
411 } else {
412 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
413 }
414
415 return action;
416 }
417
418 KFileItemListProperties& DolphinContextMenu::capabilities()
419 {
420 if (m_capabilities == 0) {
421 m_capabilities = new KFileItemListProperties(m_selectedItems);
422 }
423 return *m_capabilities;
424 }
425
426 void DolphinContextMenu::addRevisionControlActions(KMenu* menu)
427 {
428 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
429 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
430 if (!versionControlActions.isEmpty()) {
431 foreach (QAction* action, versionControlActions) {
432 menu->addAction(action);
433 }
434 menu->addSeparator();
435 }
436 }
437
438 void DolphinContextMenu::addCustomActions(KMenu* menu)
439 {
440 foreach (QAction* action, m_customActions) {
441 menu->addAction(action);
442 }
443 }
444
445 #include "dolphincontextmenu.moc"