]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Merge branch 'Applications/17.12'
[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 buildGroupContextMenu(&menu, index);
227
228 QAction* action = menu.exec(pos.toPoint());
229 if (action) {
230 if (action == emptyTrashAction) {
231 emptyTrash();
232 } else {
233 // The index might have changed if devices were added/removed while
234 // the context menu was open.
235 index = m_model->index(item);
236 if (index < 0) {
237 // The item is not in the model any more, probably because it was an
238 // external device that has been removed while the context menu was open.
239 return;
240 }
241
242 if (action == editAction) {
243 editEntry(index);
244 } else if (action == removeAction) {
245 m_model->deleteItem(index);
246 } else if (action == hideAction) {
247 item->setHidden(hideAction->isChecked());
248 } else if (action == openInNewWindowAction) {
249 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model->data(index).value("url").toUrl())}, this);
250 } else if (action == openInNewTabAction) {
251 // TriggerItem does set up the storage first and then it will
252 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
253 triggerItem(index, Qt::MiddleButton);
254 } else if (action == teardownAction) {
255 m_model->requestTearDown(index);
256 } else if (action == ejectAction) {
257 m_model->requestEject(index);
258 }
259 }
260 }
261
262 selectClosestItem();
263 }
264
265 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
266 {
267 QMenu menu(this);
268
269 QAction* addAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
270
271 QAction* showAllAction = nullptr;
272 if (m_model->hiddenCount() > 0) {
273 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
274 showAllAction->setCheckable(true);
275 showAllAction->setChecked(m_model->hiddenItemsShown());
276 }
277
278 buildGroupContextMenu(&menu, m_controller->indexCloseToMousePressedPosition());
279
280 QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu);
281
282 struct IconSizeInfo
283 {
284 int size;
285 const char* context;
286 const char* text;
287 };
288
289 const int iconSizeCount = 4;
290 static const IconSizeInfo iconSizes[iconSizeCount] = {
291 {KIconLoader::SizeSmall, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
292 {KIconLoader::SizeSmallMedium, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
293 {KIconLoader::SizeMedium, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
294 {KIconLoader::SizeLarge, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
295 };
296
297 QMap<QAction*, int> iconSizeActionMap;
298 QActionGroup* iconSizeGroup = new QActionGroup(iconSizeSubMenu);
299
300 for (int i = 0; i < iconSizeCount; ++i) {
301 const int size = iconSizes[i].size;
302 const QString text = i18nc(iconSizes[i].context, iconSizes[i].text,
303 size, size);
304
305 QAction* action = iconSizeSubMenu->addAction(text);
306 iconSizeActionMap.insert(action, size);
307 action->setActionGroup(iconSizeGroup);
308 action->setCheckable(true);
309 action->setChecked(m_view->iconSize() == size);
310 }
311
312 menu.addMenu(iconSizeSubMenu);
313
314 menu.addSeparator();
315 foreach (QAction* action, customContextMenuActions()) {
316 menu.addAction(action);
317 }
318
319 QAction* action = menu.exec(pos.toPoint());
320 if (action) {
321 if (action == addAction) {
322 addEntry();
323 } else if (action == showAllAction) {
324 m_model->setHiddenItemsShown(showAllAction->isChecked());
325 } else if (iconSizeActionMap.contains(action)) {
326 m_view->setIconSize(iconSizeActionMap.value(action));
327 }
328 }
329
330 selectClosestItem();
331 }
332
333 QAction *PlacesPanel::buildGroupContextMenu(QMenu *menu, int index)
334 {
335 if (index == -1) {
336 return nullptr;
337 }
338
339 KFilePlacesModel::GroupType groupType = m_model->groupType(index);
340 QAction *hideGroupAction = menu->addAction(i18nc("@item:inmenu", "Hide Section '%1'", m_model->item(index)->group()));
341 hideGroupAction->setCheckable(true);
342 hideGroupAction->setChecked(m_model->isGroupHidden(groupType));
343
344 connect(hideGroupAction, &QAction::triggered, this, [this, groupType, hideGroupAction]{
345 m_model->setGroupHidden(groupType, hideGroupAction->isChecked());
346 });
347
348 return hideGroupAction;
349 }
350
351 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
352 {
353 if (index < 0) {
354 return;
355 }
356
357 const PlacesItem* destItem = m_model->placesItem(index);
358
359 if (destItem->isSearchOrTimelineUrl()) {
360 return;
361 }
362
363 if (m_model->storageSetupNeeded(index)) {
364 connect(m_model, &PlacesItemModel::storageSetupDone,
365 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
366
367 m_itemDropEventIndex = index;
368
369 // Make a full copy of the Mime-Data
370 m_itemDropEventMimeData = new QMimeData;
371 m_itemDropEventMimeData->setText(event->mimeData()->text());
372 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
373 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
374 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
375 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
376
377 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
378 event->possibleActions(),
379 m_itemDropEventMimeData,
380 event->buttons(),
381 event->modifiers());
382
383 m_model->requestStorageSetup(index);
384 return;
385 }
386
387 QUrl destUrl = destItem->url();
388 QDropEvent dropEvent(event->pos().toPoint(),
389 event->possibleActions(),
390 event->mimeData(),
391 event->buttons(),
392 event->modifiers());
393
394 slotUrlsDropped(destUrl, &dropEvent, this);
395 }
396
397 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
398 {
399 disconnect(m_model, &PlacesItemModel::storageSetupDone,
400 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
401
402 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
403 if (success) {
404 QUrl destUrl = m_model->placesItem(index)->url();
405 slotUrlsDropped(destUrl, m_itemDropEvent, this);
406 }
407
408 delete m_itemDropEventMimeData;
409 delete m_itemDropEvent;
410
411 m_itemDropEventIndex = -1;
412 m_itemDropEventMimeData = nullptr;
413 m_itemDropEvent = nullptr;
414 }
415 }
416
417 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
418 {
419 m_model->dropMimeDataBefore(index, event->mimeData());
420 }
421
422 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
423 {
424 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
425 if (job) {
426 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
427 }
428 }
429
430 void PlacesPanel::slotTrashUpdated(KJob* job)
431 {
432 if (job->error()) {
433 emit errorMessage(job->errorString());
434 }
435 // as long as KIO doesn't do this, do it ourselves
436 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent);
437 }
438
439 void PlacesPanel::slotStorageSetupDone(int index, bool success)
440 {
441 disconnect(m_model, &PlacesItemModel::storageSetupDone,
442 this, &PlacesPanel::slotStorageSetupDone);
443
444 if (m_triggerStorageSetupButton == Qt::NoButton) {
445 return;
446 }
447
448 if (success) {
449 Q_ASSERT(!m_model->storageSetupNeeded(index));
450 triggerItem(index, m_triggerStorageSetupButton);
451 m_triggerStorageSetupButton = Qt::NoButton;
452 } else {
453 setUrl(m_storageSetupFailedUrl);
454 m_storageSetupFailedUrl = QUrl();
455 }
456 }
457
458 void PlacesPanel::emptyTrash()
459 {
460 KIO::JobUiDelegate uiDelegate;
461 uiDelegate.setWindow(window());
462 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
463 KIO::Job* job = KIO::emptyTrash();
464 KJobWidgets::setWindow(job, window());
465 connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
466 }
467 }
468
469 void PlacesPanel::addEntry()
470 {
471 const int index = m_controller->selectionManager()->currentItem();
472 const QUrl url = m_model->data(index).value("url").toUrl();
473
474 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
475 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
476 dialog->setAllowGlobal(true);
477 dialog->setUrl(url);
478 if (dialog->exec() == QDialog::Accepted) {
479 m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
480 }
481
482 delete dialog;
483 }
484
485 void PlacesPanel::editEntry(int index)
486 {
487 QHash<QByteArray, QVariant> data = m_model->data(index);
488
489 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
490 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
491 dialog->setIcon(data.value("iconName").toString());
492 dialog->setText(data.value("text").toString());
493 dialog->setUrl(data.value("url").toUrl());
494 dialog->setAllowGlobal(true);
495 if (dialog->exec() == QDialog::Accepted) {
496 PlacesItem* oldItem = m_model->placesItem(index);
497 if (oldItem) {
498 oldItem->setText(dialog->text());
499 oldItem->setUrl(dialog->url());
500 oldItem->setIcon(dialog->icon());
501 m_model->refresh();
502 }
503 }
504
505 delete dialog;
506 }
507
508 void PlacesPanel::selectClosestItem()
509 {
510 const int index = m_model->closestItem(url());
511 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
512 selectionManager->setCurrentItem(index);
513 selectionManager->clearSelection();
514 selectionManager->setSelected(index);
515 }
516
517 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
518 {
519 const PlacesItem* item = m_model->placesItem(index);
520 if (!item) {
521 return;
522 }
523
524 if (m_model->storageSetupNeeded(index)) {
525 m_triggerStorageSetupButton = button;
526 m_storageSetupFailedUrl = url();
527
528 connect(m_model, &PlacesItemModel::storageSetupDone,
529 this, &PlacesPanel::slotStorageSetupDone);
530
531 m_model->requestStorageSetup(index);
532 } else {
533 m_triggerStorageSetupButton = Qt::NoButton;
534
535 const QUrl url = m_model->data(index).value("url").toUrl();
536 if (!url.isEmpty()) {
537 if (button == Qt::MiddleButton) {
538 emit placeMiddleClicked(KFilePlacesModel::convertedUrl(url));
539 } else {
540 emit placeActivated(KFilePlacesModel::convertedUrl(url));
541 }
542 }
543 }
544 }