2 * SPDX-FileCopyrightText: 2008-2012 Peter Penz <peter.penz19@gmail.com>
4 * Based on KFilePlacesView from kdelibs:
5 * SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
6 * SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "placespanel.h"
13 #include "dolphin_generalsettings.h"
15 #include "kitemviews/kitemlistcontainer.h"
16 #include "kitemviews/kitemlistcontroller.h"
17 #include "kitemviews/kitemlistselectionmanager.h"
18 #include "kitemviews/kstandarditem.h"
19 #include "placesitem.h"
20 #include "placesitemlistgroupheader.h"
21 #include "placesitemlistwidget.h"
22 #include "placesitemmodel.h"
23 #include "placesview.h"
24 #include "trash/dolphintrash.h"
25 #include "views/draganddrophelper.h"
27 #include <KFilePlaceEditDialog>
28 #include <KFilePlacesModel>
29 #include <KIO/DropJob>
30 #include <KIO/EmptyTrashJob>
32 #include <KIconLoader>
33 #include <KLocalizedString>
34 #include <KMountPoint>
35 #include <KPropertiesDialog>
37 #include <QActionGroup>
38 #include <QGraphicsSceneDragDropEvent>
42 #include <QVBoxLayout>
46 PlacesPanel::PlacesPanel(QWidget
* parent
) :
48 m_controller(nullptr),
51 m_storageSetupFailedUrl(),
52 m_triggerStorageSetupButton(),
53 m_itemDropEventIndex(-1),
54 m_itemDropEventMimeData(nullptr),
55 m_itemDropEvent(nullptr),
58 m_tooltipTimer
.setInterval(500);
59 m_tooltipTimer
.setSingleShot(true);
60 connect(&m_tooltipTimer
, &QTimer::timeout
, this, &PlacesPanel::slotShowTooltip
);
63 PlacesPanel::~PlacesPanel()
67 void PlacesPanel::proceedWithTearDown()
69 m_model
->proceedWithTearDown();
72 bool PlacesPanel::urlChanged()
74 if (!url().isValid() || url().scheme().contains(QLatin1String("search"))) {
75 // Skip results shown by a search, as possible identical
76 // directory names are useless without parent-path information.
87 void PlacesPanel::readSettings()
90 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
91 m_controller
->setAutoActivationDelay(delay
);
95 void PlacesPanel::showEvent(QShowEvent
* event
)
97 if (event
->spontaneous()) {
98 Panel::showEvent(event
);
103 // Postpone the creating of the controller to the first show event.
104 // This assures that no performance and memory overhead is given when the folders panel is not
105 // used at all and stays invisible.
106 m_model
= new PlacesItemModel(this);
107 m_model
->setGroupedSorting(true);
108 connect(m_model
, &PlacesItemModel::errorMessage
,
109 this, &PlacesPanel::errorMessage
);
110 connect(m_model
, &PlacesItemModel::storageTearDownRequested
,
111 this, &PlacesPanel::storageTearDownRequested
);
112 connect(m_model
, &PlacesItemModel::storageTearDownExternallyRequested
,
113 this, &PlacesPanel::storageTearDownExternallyRequested
);
114 connect(m_model
, &PlacesItemModel::storageTearDownSuccessful
,
115 this, &PlacesPanel::storageTearDownSuccessful
);
117 m_view
= new PlacesView();
118 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
119 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
121 installEventFilter(this);
123 m_controller
= new KItemListController(m_model
, m_view
, this);
124 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
125 m_controller
->setSingleClickActivationEnforced(true);
129 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
130 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
131 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
132 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
133 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
134 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
136 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
137 container
->setEnabledFrame(false);
139 QVBoxLayout
* layout
= new QVBoxLayout(this);
140 layout
->setContentsMargins(0, 0, 0, 0);
141 layout
->addWidget(container
);
146 Panel::showEvent(event
);
149 bool PlacesPanel::eventFilter(QObject
* /* obj */, QEvent
*event
)
151 if (event
->type() == QEvent::ToolTip
) {
153 QHelpEvent
*hoverEvent
= reinterpret_cast<QHelpEvent
*>(event
);
155 m_hoveredIndex
= m_view
->itemAt(hoverEvent
->pos());
156 m_hoverPos
= mapToGlobal(hoverEvent
->pos());
158 m_tooltipTimer
.start();
164 void PlacesPanel::slotItemActivated(int index
)
166 triggerItem(index
, Qt::LeftButton
);
169 void PlacesPanel::slotItemMiddleClicked(int index
)
171 triggerItem(index
, Qt::MiddleButton
);
174 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
176 PlacesItem
* item
= m_model
->placesItem(index
);
183 QAction
* emptyTrashAction
= nullptr;
184 QAction
* editAction
= nullptr;
185 QAction
* teardownAction
= nullptr;
186 QAction
* ejectAction
= nullptr;
187 QAction
* mountAction
= nullptr;
189 const bool isDevice
= !item
->udi().isEmpty();
190 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
192 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
193 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
197 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@item:inmenu", "Open in New Tab"));
198 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@item:inmenu", "Open in New Window"));
199 QAction
* propertiesAction
= nullptr;
200 if (item
->url().isLocalFile()) {
201 propertiesAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-properties")), i18nc("@action:inmenu", "Properties"));
203 if (!isDevice
&& !isTrash
) {
208 ejectAction
= m_model
->ejectAction(index
);
210 ejectAction
->setParent(&menu
);
211 menu
.addAction(ejectAction
);
214 teardownAction
= m_model
->teardownAction(index
);
215 if (teardownAction
) {
216 // Disable teardown option for root and home partitions
217 bool teardownEnabled
= item
->url() != QUrl::fromLocalFile(QDir::rootPath());
218 if (teardownEnabled
) {
219 KMountPoint::Ptr mountPoint
= KMountPoint::currentMountPoints().findByPath(QDir::homePath());
220 if (mountPoint
&& item
->url() == QUrl::fromLocalFile(mountPoint
->mountPoint())) {
221 teardownEnabled
= false;
224 teardownAction
->setEnabled(teardownEnabled
);
226 teardownAction
->setParent(&menu
);
227 menu
.addAction(teardownAction
);
230 if (item
->storageSetupNeeded()) {
231 mountAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("media-mount")), i18nc("@action:inmenu", "Mount"));
234 if (teardownAction
|| ejectAction
|| mountAction
) {
240 editAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-entry")), i18nc("@item:inmenu", "Edit..."));
243 QAction
* removeAction
= nullptr;
244 if (!isDevice
&& !item
->isSystemItem()) {
245 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
248 QAction
* hideAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("view-hidden")), i18nc("@item:inmenu", "Hide"));
249 hideAction
->setCheckable(true);
250 hideAction
->setChecked(item
->isHidden());
252 buildGroupContextMenu(&menu
, index
);
254 QAction
* action
= menu
.exec(pos
.toPoint());
256 if (action
== emptyTrashAction
) {
259 // The index might have changed if devices were added/removed while
260 // the context menu was open.
261 index
= m_model
->index(item
);
263 // The item is not in the model any more, probably because it was an
264 // external device that has been removed while the context menu was open.
268 if (action
== editAction
) {
270 } else if (action
== removeAction
) {
271 m_model
->deleteItem(index
);
272 } else if (action
== hideAction
) {
273 item
->setHidden(hideAction
->isChecked());
274 if (!m_model
->hiddenCount()) {
275 showHiddenEntries(false);
277 } else if (action
== openInNewWindowAction
) {
278 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
279 } else if (action
== openInNewTabAction
) {
280 // TriggerItem does set up the storage first and then it will
281 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
282 triggerItem(index
, Qt::MiddleButton
);
283 } else if (action
== mountAction
) {
284 m_model
->requestStorageSetup(index
);
285 } else if (action
== teardownAction
) {
286 m_model
->requestTearDown(index
);
287 } else if (action
== ejectAction
) {
288 m_model
->requestEject(index
);
289 } else if (action
== propertiesAction
) {
290 KPropertiesDialog
* dialog
= new KPropertiesDialog(item
->url(), this);
291 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
300 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
304 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
306 QAction
* showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show Hidden Places"));
307 showAllAction
->setCheckable(true);
308 showAllAction
->setChecked(m_model
->hiddenItemsShown());
309 showAllAction
->setIcon(QIcon::fromTheme(m_model
->hiddenItemsShown() ? QStringLiteral("view-visible") : QStringLiteral("view-hidden")));
310 showAllAction
->setEnabled(m_model
->hiddenCount());
312 buildGroupContextMenu(&menu
, m_controller
->indexCloseToMousePressedPosition());
314 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
323 const int iconSizeCount
= 4;
324 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
325 {KIconLoader::SizeSmall
, I18NC_NOOP("Small icon size", "Small (%1x%2)")},
326 {KIconLoader::SizeSmallMedium
, I18NC_NOOP("Medium icon size", "Medium (%1x%2)")},
327 {KIconLoader::SizeMedium
, I18NC_NOOP("Large icon size", "Large (%1x%2)")},
328 {KIconLoader::SizeLarge
, I18NC_NOOP("Huge icon size", "Huge (%1x%2)")}
331 QHash
<QAction
*, int> iconSizeActionMap
;
332 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
334 for (int i
= 0; i
< iconSizeCount
; ++i
) {
335 const int size
= iconSizes
[i
].size
;
336 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
339 QAction
* action
= iconSizeSubMenu
->addAction(text
);
340 iconSizeActionMap
.insert(action
, size
);
341 action
->setActionGroup(iconSizeGroup
);
342 action
->setCheckable(true);
343 action
->setChecked(m_view
->iconSize() == size
);
346 menu
.addMenu(iconSizeSubMenu
);
349 const auto actions
= customContextMenuActions();
350 for (QAction
* action
: actions
) {
351 menu
.addAction(action
);
354 QAction
* action
= menu
.exec(pos
.toPoint());
356 if (action
== addAction
) {
358 } else if (action
== showAllAction
) {
359 showHiddenEntries(showAllAction
->isChecked());
360 } else if (iconSizeActionMap
.contains(action
)) {
361 m_view
->setIconSize(iconSizeActionMap
.value(action
));
368 QAction
*PlacesPanel::buildGroupContextMenu(QMenu
*menu
, int index
)
374 KFilePlacesModel::GroupType groupType
= m_model
->groupType(index
);
375 QAction
*hideGroupAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("view-hidden")), i18nc("@item:inmenu", "Hide Section '%1'", m_model
->item(index
)->group()));
376 hideGroupAction
->setCheckable(true);
377 hideGroupAction
->setChecked(m_model
->isGroupHidden(groupType
));
379 connect(hideGroupAction
, &QAction::triggered
, this, [this, groupType
, hideGroupAction
]{
380 m_model
->setGroupHidden(groupType
, hideGroupAction
->isChecked());
381 if (!m_model
->hiddenCount()) {
382 showHiddenEntries(false);
386 return hideGroupAction
;
389 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
395 const PlacesItem
* destItem
= m_model
->placesItem(index
);
397 if (destItem
->isSearchOrTimelineUrl()) {
401 if (m_model
->storageSetupNeeded(index
)) {
402 connect(m_model
, &PlacesItemModel::storageSetupDone
,
403 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
405 m_itemDropEventIndex
= index
;
407 // Make a full copy of the Mime-Data
408 m_itemDropEventMimeData
= new QMimeData
;
409 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
410 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
411 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
412 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
413 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
415 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
416 event
->possibleActions(),
417 m_itemDropEventMimeData
,
421 m_model
->requestStorageSetup(index
);
425 QUrl destUrl
= destItem
->url();
426 QDropEvent
dropEvent(event
->pos().toPoint(),
427 event
->possibleActions(),
432 slotUrlsDropped(destUrl
, &dropEvent
, this);
435 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
437 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
438 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
440 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
442 QUrl destUrl
= m_model
->placesItem(index
)->url();
443 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
446 delete m_itemDropEventMimeData
;
447 delete m_itemDropEvent
;
449 m_itemDropEventIndex
= -1;
450 m_itemDropEventMimeData
= nullptr;
451 m_itemDropEvent
= nullptr;
455 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
457 m_model
->dropMimeDataBefore(index
, event
->mimeData());
460 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
462 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
464 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) Q_EMIT
errorMessage(job
->errorString()); });
468 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
470 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
471 this, &PlacesPanel::slotStorageSetupDone
);
473 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
478 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
479 triggerItem(index
, m_triggerStorageSetupButton
);
480 m_triggerStorageSetupButton
= Qt::NoButton
;
482 setUrl(m_storageSetupFailedUrl
);
483 m_storageSetupFailedUrl
= QUrl();
487 void PlacesPanel::slotShowTooltip()
489 const QUrl url
= m_model
->data(m_hoveredIndex
).value("url").value
<QUrl
>();
490 const QString text
= url
.toDisplayString(QUrl::PreferLocalFile
);
491 QToolTip::showText(m_hoverPos
, text
);
494 void PlacesPanel::addEntry()
496 const int index
= m_controller
->selectionManager()->currentItem();
497 const QUrl url
= m_model
->data(index
).value("url").toUrl();
498 const QString text
= url
.fileName().isEmpty() ? url
.toDisplayString(QUrl::PreferLocalFile
) : url
.fileName();
500 QPointer
<KFilePlaceEditDialog
> dialog
= new KFilePlaceEditDialog(true, url
, text
, QString(), true, false, KIconLoader::SizeMedium
, this);
501 if (dialog
->exec() == QDialog::Accepted
) {
502 const QString appName
= dialog
->applicationLocal() ? QCoreApplication::applicationName() : QString();
503 m_model
->createPlacesItem(dialog
->label(), dialog
->url(), dialog
->icon(), appName
);
509 void PlacesPanel::editEntry(int index
)
511 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
512 const QUrl url
= data
.value("url").toUrl();
513 const QString text
= data
.value("text").toString();
514 const QString iconName
= data
.value("iconName").toString();
515 const bool applicationLocal
= !data
.value("applicationName").toString().isEmpty();
517 QPointer
<KFilePlaceEditDialog
> dialog
= new KFilePlaceEditDialog(true, url
, text
, iconName
, true, applicationLocal
, KIconLoader::SizeMedium
, this);
518 if (dialog
->exec() == QDialog::Accepted
) {
519 PlacesItem
* oldItem
= m_model
->placesItem(index
);
521 const QString appName
= dialog
->applicationLocal() ? QCoreApplication::applicationName() : QString();
522 oldItem
->setApplicationName(appName
);
523 oldItem
->setText(dialog
->label());
524 oldItem
->setUrl(dialog
->url());
525 oldItem
->setIcon(dialog
->icon());
533 void PlacesPanel::selectItem()
535 const int index
= m_model
->closestItem(url());
536 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
537 selectionManager
->setCurrentItem(index
);
538 selectionManager
->clearSelection();
540 const QUrl closestUrl
= m_model
->url(index
);
541 if (!closestUrl
.path().isEmpty() && url() == closestUrl
) {
542 selectionManager
->setSelected(index
);
546 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
548 const PlacesItem
* item
= m_model
->placesItem(index
);
553 if (m_model
->storageSetupNeeded(index
)) {
554 m_triggerStorageSetupButton
= button
;
555 m_storageSetupFailedUrl
= url();
557 connect(m_model
, &PlacesItemModel::storageSetupDone
,
558 this, &PlacesPanel::slotStorageSetupDone
);
560 m_model
->requestStorageSetup(index
);
562 m_triggerStorageSetupButton
= Qt::NoButton
;
564 const QUrl url
= m_model
->data(index
).value("url").toUrl();
565 if (!url
.isEmpty()) {
566 if (button
== Qt::MiddleButton
) {
567 Q_EMIT
placeMiddleClicked(KFilePlacesModel::convertedUrl(url
));
569 Q_EMIT
placeActivated(KFilePlacesModel::convertedUrl(url
));
575 void PlacesPanel::showHiddenEntries(bool shown
)
577 m_model
->setHiddenItemsShown(shown
);
578 Q_EMIT
showHiddenEntriesChanged(shown
);
581 int PlacesPanel::hiddenListCount()
586 return m_model
->hiddenCount();