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