]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Cleanup Places Panel context menus
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesView from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "placespanel.h"
25
26 #include "dolphin_generalsettings.h"
27
28 #include <KFileItem>
29 #include "dolphindebug.h"
30 #include <KDirNotify>
31 #include <QIcon>
32 #include <KIO/Job>
33 #include <KIO/DropJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
36 #include <KJobWidgets>
37 #include <KLocalizedString>
38 #include <KIconLoader>
39 #include <kitemviews/kitemlistcontainer.h>
40 #include <kitemviews/kitemlistcontroller.h>
41 #include <kitemviews/kitemlistselectionmanager.h>
42 #include <kitemviews/kstandarditem.h>
43 #include <QMenu>
44 #include <KMessageBox>
45 #include <KNotification>
46 #include "placesitem.h"
47 #include "placesitemeditdialog.h"
48 #include "placesitemlistgroupheader.h"
49 #include "placesitemlistwidget.h"
50 #include "placesitemmodel.h"
51 #include "placesview.h"
52 #include <views/draganddrophelper.h>
53 #include <QGraphicsSceneDragDropEvent>
54 #include <QVBoxLayout>
55 #include <QShowEvent>
56 #include <QMimeData>
57
58 PlacesPanel::PlacesPanel(QWidget* parent) :
59 Panel(parent),
60 m_controller(0),
61 m_model(0),
62 m_storageSetupFailedUrl(),
63 m_triggerStorageSetupButton(),
64 m_itemDropEventIndex(-1),
65 m_itemDropEventMimeData(0),
66 m_itemDropEvent(0)
67 {
68 }
69
70 PlacesPanel::~PlacesPanel()
71 {
72 }
73
74 bool PlacesPanel::urlChanged()
75 {
76 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
77 // Skip results shown by a search, as possible identical
78 // directory names are useless without parent-path information.
79 return false;
80 }
81
82 if (m_controller) {
83 selectClosestItem();
84 }
85
86 return true;
87 }
88
89 void PlacesPanel::readSettings()
90 {
91 if (m_controller) {
92 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
93 m_controller->setAutoActivationDelay(delay);
94 }
95 }
96
97 void PlacesPanel::showEvent(QShowEvent* event)
98 {
99 if (event->spontaneous()) {
100 Panel::showEvent(event);
101 return;
102 }
103
104 if (!m_controller) {
105 // Postpone the creating of the controller to the first show event.
106 // This assures that no performance and memory overhead is given when the folders panel is not
107 // used at all and stays invisible.
108 m_model = new PlacesItemModel(this);
109 m_model->setGroupedSorting(true);
110 connect(m_model, &PlacesItemModel::errorMessage,
111 this, &PlacesPanel::errorMessage);
112
113 m_view = new PlacesView();
114 m_view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
115 m_view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
116
117 m_controller = new KItemListController(m_model, m_view, this);
118 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
119 m_controller->setSingleClickActivationEnforced(true);
120
121 readSettings();
122
123 connect(m_controller, &KItemListController::itemActivated, this, &PlacesPanel::slotItemActivated);
124 connect(m_controller, &KItemListController::itemMiddleClicked, this, &PlacesPanel::slotItemMiddleClicked);
125 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &PlacesPanel::slotItemContextMenuRequested);
126 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &PlacesPanel::slotViewContextMenuRequested);
127 connect(m_controller, &KItemListController::itemDropEvent, this, &PlacesPanel::slotItemDropEvent);
128 connect(m_controller, &KItemListController::aboveItemDropEvent, this, &PlacesPanel::slotAboveItemDropEvent);
129
130 KItemListContainer* container = new KItemListContainer(m_controller, this);
131 container->setEnabledFrame(false);
132
133 QVBoxLayout* layout = new QVBoxLayout(this);
134 layout->setMargin(0);
135 layout->addWidget(container);
136
137 selectClosestItem();
138 }
139
140 Panel::showEvent(event);
141 }
142
143 void PlacesPanel::slotItemActivated(int index)
144 {
145 triggerItem(index, Qt::LeftButton);
146 }
147
148 void PlacesPanel::slotItemMiddleClicked(int index)
149 {
150 triggerItem(index, Qt::MiddleButton);
151 }
152
153 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
154 {
155 PlacesItem* item = m_model->placesItem(index);
156 if (!item) {
157 return;
158 }
159
160 QMenu menu(this);
161
162 QAction* emptyTrashAction = 0;
163 QAction* editAction = 0;
164 QAction* teardownAction = 0;
165 QAction* ejectAction = 0;
166
167 const QString label = item->text();
168
169 const bool isDevice = !item->udi().isEmpty();
170 const bool isTrash = (item->url().scheme() == QLatin1String("trash"));
171 if (isDevice) {
172 ejectAction = m_model->ejectAction(index);
173 if (ejectAction) {
174 ejectAction->setParent(&menu);
175 menu.addAction(ejectAction);
176 }
177
178 teardownAction = m_model->teardownAction(index);
179 if (teardownAction) {
180 teardownAction->setParent(&menu);
181 menu.addAction(teardownAction);
182 }
183
184 if (teardownAction || ejectAction) {
185 menu.addSeparator();
186 }
187 } else {
188 if (isTrash) {
189 emptyTrashAction = menu.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
190 emptyTrashAction->setEnabled(item->icon() == QLatin1String("user-trash-full"));
191 menu.addSeparator();
192 }
193 }
194
195 QAction* openInNewTabAction = menu.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
196 if (!isDevice && !isTrash) {
197 menu.addSeparator();
198 }
199
200 editAction = menu.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
201
202 QAction* removeAction = 0;
203 if (!isDevice && !item->isSystemItem()) {
204 removeAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
205 }
206
207 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide"));
208 hideAction->setCheckable(true);
209 hideAction->setChecked(item->isHidden());
210
211 QAction* action = menu.exec(pos.toPoint());
212 if (action) {
213 if (action == emptyTrashAction) {
214 emptyTrash();
215 } else {
216 // The index might have changed if devices were added/removed while
217 // the context menu was open.
218 index = m_model->index(item);
219 if (index < 0) {
220 // The item is not in the model any more, probably because it was an
221 // external device that has been removed while the context menu was open.
222 return;
223 }
224
225 if (action == editAction) {
226 editEntry(index);
227 } else if (action == removeAction) {
228 m_model->removeItem(index);
229 m_model->saveBookmarks();
230 } else if (action == hideAction) {
231 item->setHidden(hideAction->isChecked());
232 m_model->saveBookmarks();
233 } else if (action == openInNewTabAction) {
234 // TriggerItem does set up the storage first and then it will
235 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
236 triggerItem(index, Qt::MiddleButton);
237 } else if (action == teardownAction) {
238 m_model->requestTeardown(index);
239 } else if (action == ejectAction) {
240 m_model->requestEject(index);
241 }
242 }
243 }
244
245 selectClosestItem();
246 }
247
248 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
249 {
250 QMenu menu(this);
251
252 QAction* addAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
253
254 QAction* showAllAction = 0;
255 if (m_model->hiddenCount() > 0) {
256 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
257 showAllAction->setCheckable(true);
258 showAllAction->setChecked(m_model->hiddenItemsShown());
259 }
260
261 QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu);
262
263 struct IconSizeInfo
264 {
265 int size;
266 const char* context;
267 const char* text;
268 };
269
270 const int iconSizeCount = 4;
271 static const IconSizeInfo iconSizes[iconSizeCount] = {
272 {KIconLoader::SizeSmall, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
273 {KIconLoader::SizeSmallMedium, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
274 {KIconLoader::SizeMedium, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
275 {KIconLoader::SizeLarge, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
276 };
277
278 QMap<QAction*, int> iconSizeActionMap;
279 QActionGroup* iconSizeGroup = new QActionGroup(iconSizeSubMenu);
280
281 for (int i = 0; i < iconSizeCount; ++i) {
282 const int size = iconSizes[i].size;
283 const QString text = i18nc(iconSizes[i].context, iconSizes[i].text,
284 size, size);
285
286 QAction* action = iconSizeSubMenu->addAction(text);
287 iconSizeActionMap.insert(action, size);
288 action->setActionGroup(iconSizeGroup);
289 action->setCheckable(true);
290 action->setChecked(m_view->iconSize() == size);
291 }
292
293 menu.addMenu(iconSizeSubMenu);
294
295 menu.addSeparator();
296 foreach (QAction* action, customContextMenuActions()) {
297 menu.addAction(action);
298 }
299
300 QAction* action = menu.exec(pos.toPoint());
301 if (action) {
302 if (action == addAction) {
303 addEntry();
304 } else if (action == showAllAction) {
305 m_model->setHiddenItemsShown(showAllAction->isChecked());
306 } else if (iconSizeActionMap.contains(action)) {
307 m_view->setIconSize(iconSizeActionMap.value(action));
308 }
309 }
310
311 selectClosestItem();
312 }
313
314 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
315 {
316 if (index < 0) {
317 return;
318 }
319
320 const PlacesItem* destItem = m_model->placesItem(index);
321 const PlacesItem::GroupType group = destItem->groupType();
322 if (group == PlacesItem::SearchForType || group == PlacesItem::RecentlySavedType) {
323 return;
324 }
325
326 if (m_model->storageSetupNeeded(index)) {
327 connect(m_model, &PlacesItemModel::storageSetupDone,
328 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
329
330 m_itemDropEventIndex = index;
331
332 // Make a full copy of the Mime-Data
333 m_itemDropEventMimeData = new QMimeData;
334 m_itemDropEventMimeData->setText(event->mimeData()->text());
335 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
336 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
337 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
338 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
339
340 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
341 event->possibleActions(),
342 m_itemDropEventMimeData,
343 event->buttons(),
344 event->modifiers());
345
346 m_model->requestStorageSetup(index);
347 return;
348 }
349
350 QUrl destUrl = destItem->url();
351 QDropEvent dropEvent(event->pos().toPoint(),
352 event->possibleActions(),
353 event->mimeData(),
354 event->buttons(),
355 event->modifiers());
356
357 slotUrlsDropped(destUrl, &dropEvent, this);
358 }
359
360 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
361 {
362 disconnect(m_model, &PlacesItemModel::storageSetupDone,
363 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
364
365 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
366 if (success) {
367 QUrl destUrl = m_model->placesItem(index)->url();
368 slotUrlsDropped(destUrl, m_itemDropEvent, this);
369 }
370
371 delete m_itemDropEventMimeData;
372 delete m_itemDropEvent;
373
374 m_itemDropEventIndex = -1;
375 m_itemDropEventMimeData = 0;
376 m_itemDropEvent = 0;
377 }
378 }
379
380 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
381 {
382 m_model->dropMimeDataBefore(index, event->mimeData());
383 m_model->saveBookmarks();
384 }
385
386 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
387 {
388 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
389 if (job) {
390 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
391 }
392 }
393
394 void PlacesPanel::slotTrashUpdated(KJob* job)
395 {
396 if (job->error()) {
397 emit errorMessage(job->errorString());
398 }
399 // as long as KIO doesn't do this, do it ourselves
400 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), 0, KNotification::DefaultEvent);
401 }
402
403 void PlacesPanel::slotStorageSetupDone(int index, bool success)
404 {
405 disconnect(m_model, &PlacesItemModel::storageSetupDone,
406 this, &PlacesPanel::slotStorageSetupDone);
407
408 if (m_triggerStorageSetupButton == Qt::NoButton) {
409 return;
410 }
411
412 if (success) {
413 Q_ASSERT(!m_model->storageSetupNeeded(index));
414 triggerItem(index, m_triggerStorageSetupButton);
415 m_triggerStorageSetupButton = Qt::NoButton;
416 } else {
417 setUrl(m_storageSetupFailedUrl);
418 m_storageSetupFailedUrl = QUrl();
419 }
420 }
421
422 void PlacesPanel::emptyTrash()
423 {
424 KIO::JobUiDelegate uiDelegate;
425 uiDelegate.setWindow(window());
426 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
427 KIO::Job* job = KIO::emptyTrash();
428 KJobWidgets::setWindow(job, window());
429 connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
430 }
431 }
432
433 void PlacesPanel::addEntry()
434 {
435 const int index = m_controller->selectionManager()->currentItem();
436 const QUrl url = m_model->data(index).value("url").toUrl();
437
438 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
439 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
440 dialog->setAllowGlobal(true);
441 dialog->setUrl(url);
442 if (dialog->exec() == QDialog::Accepted) {
443 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
444 m_model->appendItemToGroup(item);
445 m_model->saveBookmarks();
446 }
447
448 delete dialog;
449 }
450
451 void PlacesPanel::editEntry(int index)
452 {
453 QHash<QByteArray, QVariant> data = m_model->data(index);
454
455 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
456 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
457 dialog->setIcon(data.value("iconName").toString());
458 dialog->setText(data.value("text").toString());
459 dialog->setUrl(data.value("url").toUrl());
460 dialog->setAllowGlobal(true);
461 if (dialog->exec() == QDialog::Accepted) {
462 PlacesItem* oldItem = m_model->placesItem(index);
463 if (oldItem) {
464 oldItem->setText(dialog->text());
465 oldItem->setUrl(dialog->url());
466 oldItem->setIcon(dialog->icon());
467 m_model->saveBookmarks();
468 }
469 }
470
471 delete dialog;
472 }
473
474 void PlacesPanel::selectClosestItem()
475 {
476 const int index = m_model->closestItem(url());
477 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
478 selectionManager->setCurrentItem(index);
479 selectionManager->clearSelection();
480 selectionManager->setSelected(index);
481 }
482
483 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
484 {
485 const PlacesItem* item = m_model->placesItem(index);
486 if (!item) {
487 return;
488 }
489
490 if (m_model->storageSetupNeeded(index)) {
491 m_triggerStorageSetupButton = button;
492 m_storageSetupFailedUrl = url();
493
494 connect(m_model, &PlacesItemModel::storageSetupDone,
495 this, &PlacesPanel::slotStorageSetupDone);
496
497 m_model->requestStorageSetup(index);
498 } else {
499 m_triggerStorageSetupButton = Qt::NoButton;
500
501 const QUrl url = m_model->data(index).value("url").toUrl();
502 if (!url.isEmpty()) {
503 if (button == Qt::MiddleButton) {
504 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
505 } else {
506 emit placeActivated(PlacesItemModel::convertedUrl(url));
507 }
508 }
509 }
510 }