]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Update the title of the tab when closing the second view in the split-mode. Thanks to
[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 "dolphinnewfilemenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kdesktopfile.h>
31 #include <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobal.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
36 #include <kmenu.h>
37 #include <kmenubar.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
40 #include <kmodifierkeyinfo.h>
41 #include <knewfilemenu.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <kfileitemactions.h>
45 #include <klocale.h>
46 #include <kpropertiesdialog.h>
47 #include <kstandardaction.h>
48 #include <kstandarddirs.h>
49
50 #include <QtGui/QApplication>
51 #include <QtGui/QClipboard>
52 #include <QtCore/QDir>
53
54 #include "views/dolphinview.h"
55 #include "views/viewmodecontroller.h"
56
57 K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
58
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
60 const KFileItem& fileInfo,
61 const KUrl& baseUrl) :
62 m_mainWindow(parent),
63 m_capabilities(0),
64 m_fileInfo(fileInfo),
65 m_baseUrl(baseUrl),
66 m_context(NoContext),
67 m_copyToMenu(parent),
68 m_customActions(),
69 m_popup(new KMenu(m_mainWindow)),
70 m_shiftPressed(false),
71 m_removeAction(0)
72 {
73 // The context menu either accesses the URLs of the selected items
74 // or the items itself. To increase the performance both lists are cached.
75 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
76 m_selectedUrls = view->selectedUrls();
77 m_selectedItems = view->selectedItems();
78
79 if (m_keyInfo != 0) {
80 if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
81 m_shiftPressed = true;
82 }
83 connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)),
84 this, SLOT(slotKeyModifierPressed(Qt::Key, bool)));
85 }
86
87 m_removeAction = new QAction(this);
88 connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
89 }
90
91 DolphinContextMenu::~DolphinContextMenu()
92 {
93 delete m_capabilities;
94 m_capabilities = 0;
95 }
96
97 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
98 {
99 m_customActions = actions;
100 }
101
102 void DolphinContextMenu::open()
103 {
104 // get the context information
105 if (m_baseUrl.protocol() == "trash") {
106 m_context |= TrashContext;
107 }
108
109 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
110 m_context |= ItemContext;
111 // TODO: handle other use cases like devices + desktop files
112 }
113
114 // open the corresponding popup for the context
115 if (m_context & TrashContext) {
116 if (m_context & ItemContext) {
117 openTrashItemContextMenu();
118 } else {
119 openTrashContextMenu();
120 }
121 } else if (m_context & ItemContext) {
122 openItemContextMenu();
123 } else {
124 Q_ASSERT(m_context == NoContext);
125 openViewportContextMenu();
126 }
127 }
128
129 void DolphinContextMenu::initializeModifierKeyInfo()
130 {
131 // Access m_keyInfo, so that it gets instantiated by
132 // K_GLOBAL_STATIC
133 KModifierKeyInfo* keyInfo = m_keyInfo;
134 Q_UNUSED(keyInfo);
135 }
136
137 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
138 {
139 m_shiftPressed = (key == Qt::Key_Shift) && pressed;
140 updateRemoveAction();
141 }
142
143 void DolphinContextMenu::slotRemoveActionTriggered()
144 {
145 const KActionCollection* collection = m_mainWindow->actionCollection();
146 if (m_shiftPressed) {
147 collection->action("delete")->trigger();
148 } else {
149 collection->action("move_to_trash")->trigger();
150 }
151 }
152
153 void DolphinContextMenu::openTrashContextMenu()
154 {
155 Q_ASSERT(m_context & TrashContext);
156
157 addShowMenubarAction();
158
159 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup.data());
160 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
161 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
162 m_popup->addAction(emptyTrashAction);
163
164 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
165 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
166
167 // Don't show if url is already in places
168 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
169 addToPlacesAction->setVisible(false);
170 }
171
172 addCustomActions();
173
174 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
175 m_popup->addAction(propertiesAction);
176
177 QAction *action = m_popup->exec(QCursor::pos());
178 if (action == emptyTrashAction) {
179 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
180 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
181 text,
182 QString(),
183 KGuiItem(i18nc("@action:button", "Empty Trash"),
184 KIcon("user-trash"))
185 ) == KMessageBox::Continue;
186 if (del) {
187 KonqOperations::emptyTrash(m_mainWindow);
188 }
189 } else if (action == addToPlacesAction) {
190 const KUrl& url = m_mainWindow->activeViewContainer()->url();
191 if (url.isValid()) {
192 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
193 }
194 }
195 }
196
197 void DolphinContextMenu::openTrashItemContextMenu()
198 {
199 Q_ASSERT(m_context & TrashContext);
200 Q_ASSERT(m_context & ItemContext);
201
202 addShowMenubarAction();
203
204 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
205 m_popup->addAction(restoreAction);
206
207 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
208 m_popup->addAction(deleteAction);
209
210 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
211 m_popup->addAction(propertiesAction);
212
213 if (m_popup->exec(QCursor::pos()) == restoreAction) {
214 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
215 }
216 }
217
218 void DolphinContextMenu::openItemContextMenu()
219 {
220 Q_ASSERT(!m_fileInfo.isNull());
221
222 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
223 // setup 'Create New' menu
224 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
225 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
226 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
227 newFileMenu->checkUpToDate();
228 newFileMenu->setPopupFiles(m_fileInfo.url());
229 newFileMenu->setEnabled(capabilities().supportsWriting());
230
231 KMenu* menu = newFileMenu->menu();
232 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
233 menu->setIcon(KIcon("document-new"));
234 m_popup->addMenu(menu);
235 m_popup->addSeparator();
236
237 // insert 'Open in new window' and 'Open in new tab' entries
238 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
239 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
240 m_popup->addSeparator();
241 }
242 addShowMenubarAction();
243 insertDefaultItemActions();
244
245 m_popup->addSeparator();
246
247 KFileItemActions fileItemActions;
248 fileItemActions.setItemListProperties(capabilities());
249 addServiceActions(fileItemActions);
250
251 addVersionControlActions();
252
253 // insert 'Copy To' and 'Move To' sub menus
254 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
255 m_copyToMenu.setItems(m_selectedItems);
256 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
257 m_copyToMenu.addActionsTo(m_popup.data());
258 m_popup->addSeparator();
259 }
260
261 // insert 'Add to Places' entry if exactly one item is selected
262 QAction* addToPlacesAction = 0;
263 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1) && !placeExists(m_fileInfo.url())) {
264 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
265 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
266 }
267
268 // insert 'Properties...' entry
269 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
270 m_popup->addAction(propertiesAction);
271
272 QAction* activatedAction = m_popup->exec(QCursor::pos());
273
274 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
275 const KUrl selectedUrl(m_fileInfo.url());
276 if (selectedUrl.isValid()) {
277 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
278 selectedUrl);
279 }
280 }
281 }
282
283 void DolphinContextMenu::openViewportContextMenu()
284 {
285 addShowMenubarAction();
286
287 // setup 'Create New' menu
288 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
289 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
290 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
291 newFileMenu->checkUpToDate();
292 newFileMenu->setPopupFiles(m_baseUrl);
293 m_popup->addMenu(newFileMenu->menu());
294 m_popup->addSeparator();
295
296 // insert 'Open in new window' and 'Open in new tab' entries
297 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
298 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
299 m_popup->addSeparator();
300
301 QAction* pasteAction = createPasteAction();
302 m_popup->addAction(pasteAction);
303 m_popup->addSeparator();
304
305 // insert service actions
306 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
307 const KFileItemListProperties baseUrlProperties(KFileItemList() << item);
308 KFileItemActions fileItemActions;
309 fileItemActions.setItemListProperties(baseUrlProperties);
310 addServiceActions(fileItemActions);
311
312 addVersionControlActions();
313
314 // insert 'Add to Places' entry if exactly one item is selected
315 QAction* addToPlacesAction = 0;
316 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
317 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
318 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
319 }
320
321 addCustomActions();
322
323 QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
324 propertiesAction->setIcon(KIcon("document-properties"));
325 QAction* action = m_popup->exec(QCursor::pos());
326 if (action == propertiesAction) {
327 const KUrl& url = m_mainWindow->activeViewContainer()->url();
328
329 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
330 dialog->setAttribute(Qt::WA_DeleteOnClose);
331 dialog->show();
332 } else if ((addToPlacesAction != 0) && (action == addToPlacesAction)) {
333 const KUrl& url = m_mainWindow->activeViewContainer()->url();
334 if (url.isValid()) {
335 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
336 }
337 }
338 }
339
340 void DolphinContextMenu::insertDefaultItemActions()
341 {
342 const KActionCollection* collection = m_mainWindow->actionCollection();
343
344 // Insert 'Cut', 'Copy' and 'Paste'
345 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
346 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
347 m_popup->addAction(createPasteAction());
348
349 m_popup->addSeparator();
350
351 // Insert 'Rename'
352 QAction* renameAction = collection->action("rename");
353 m_popup->addAction(renameAction);
354
355 // Insert 'Move to Trash' and/or 'Delete'
356 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
357 m_popup->addAction(collection->action("move_to_trash"));
358 m_popup->addAction(collection->action("delete"));
359 } else {
360 m_popup->addAction(m_removeAction);
361 updateRemoveAction();
362 }
363 }
364
365 void DolphinContextMenu::addShowMenubarAction()
366 {
367 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
368 if (!m_mainWindow->menuBar()->isVisible()) {
369 m_popup->addAction(showMenuBar);
370 m_popup->addSeparator();
371 }
372 }
373
374 QString DolphinContextMenu::placesName(const KUrl& url) const
375 {
376 QString name = url.fileName();
377 if (name.isEmpty()) {
378 name = url.host();
379 }
380 return name;
381 }
382
383 bool DolphinContextMenu::placeExists(const KUrl& url) const
384 {
385 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
386 const int count = placesModel->rowCount();
387
388 for (int i = 0; i < count; ++i) {
389 const QModelIndex index = placesModel->index(i, 0);
390
391 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
392 return true;
393 }
394 }
395 return false;
396 }
397
398 QAction* DolphinContextMenu::createPasteAction()
399 {
400 QAction* action = 0;
401 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
402 if (isDir && (m_selectedItems.count() == 1)) {
403 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
404 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
405 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
406 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
407 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
408 } else {
409 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
410 }
411
412 return action;
413 }
414
415 KFileItemListProperties& DolphinContextMenu::capabilities()
416 {
417 if (m_capabilities == 0) {
418 m_capabilities = new KFileItemListProperties(m_selectedItems);
419 }
420 return *m_capabilities;
421 }
422
423 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
424 {
425 fileItemActions.setParentWidget(m_mainWindow);
426
427 // insert 'Open With...' action or sub menu
428 fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'");
429
430 // insert 'Actions' sub menu
431 if (fileItemActions.addServiceActionsTo(m_popup.data())) {
432 m_popup->addSeparator();
433 }
434 }
435
436 void DolphinContextMenu::addVersionControlActions()
437 {
438 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
439 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
440 if (!versionControlActions.isEmpty()) {
441 foreach (QAction* action, versionControlActions) {
442 m_popup->addAction(action);
443 }
444 m_popup->addSeparator();
445 }
446 }
447
448 void DolphinContextMenu::addCustomActions()
449 {
450 foreach (QAction* action, m_customActions) {
451 m_popup->addAction(action);
452 }
453 }
454
455 void DolphinContextMenu::updateRemoveAction()
456 {
457 const KActionCollection* collection = m_mainWindow->actionCollection();
458 const bool moveToTrash = capabilities().isLocal() && !m_shiftPressed;
459 const QAction* action = moveToTrash ? collection->action("move_to_trash") : collection->action("delete");
460 m_removeAction->setText(action->text());
461 m_removeAction->setIcon(action->icon());
462 m_removeAction->setShortcuts(action->shortcuts());
463 }
464
465 #include "dolphincontextmenu.moc"