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