2 * SPDX-FileCopyrightText: 2008-2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2021 Kai Uwe Broulik <kde@broulik.de>
5 * Based on KFilePlacesView from kdelibs:
6 * SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
7 * SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "placespanel.h"
14 #include "dolphinplacesmodelsingleton.h"
15 #include "dolphin_generalsettings.h"
16 #include "dolphin_placespanelsettings.h"
18 #include "views/draganddrophelper.h"
19 #include "settings/dolphinsettingsdialog.h"
21 #include <KFilePlacesModel>
22 #include <KIO/DropJob>
24 #include <KLocalizedString>
25 #include <KProtocolManager>
32 #include <Solid/StorageAccess>
34 PlacesPanel::PlacesPanel(QWidget
* parent
)
35 : KFilePlacesView(parent
)
37 setDropOnPlaceEnabled(true);
38 connect(this, &PlacesPanel::urlsDropped
,
39 this, &PlacesPanel::slotUrlsDropped
);
41 setAutoResizeItemsEnabled(false);
43 setTeardownFunction([this](const QModelIndex
&index
) {
44 slotTearDownRequested(index
);
47 m_configureTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
48 m_configureTrashAction
->setPriority(QAction::HighPriority
);
49 connect(m_configureTrashAction
, &QAction::triggered
, this, &PlacesPanel::slotConfigureTrash
);
50 addAction(m_configureTrashAction
);
52 connect(this, &PlacesPanel::contextMenuAboutToShow
, this, &PlacesPanel::slotContextMenuAboutToShow
);
54 connect(this, &PlacesPanel::iconSizeChanged
, this, [this](const QSize
&newSize
) {
55 int iconSize
= qMin(newSize
.width(), newSize
.height());
57 // Don't store 0 size, let's keep -1 for default/small/automatic
60 PlacesPanelSettings
* settings
= PlacesPanelSettings::self();
61 settings
->setIconSize(iconSize
);
66 PlacesPanel::~PlacesPanel() = default;
68 void PlacesPanel::setUrl(const QUrl
&url
)
70 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
71 // Remember the URL and set it in showEvent
73 KFilePlacesView::setUrl(url
);
76 QList
<QAction
*> PlacesPanel::customContextMenuActions() const
78 return m_customContextMenuActions
;
81 void PlacesPanel::setCustomContextMenuActions(const QList
<QAction
*> &actions
)
83 m_customContextMenuActions
= actions
;
86 void PlacesPanel::proceedWithTearDown()
88 if (m_indexToTearDown
.isValid()) {
89 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
90 placesModel
->requestTeardown(m_indexToTearDown
);
92 qWarning() << "Places entry to tear down is no longer valid";
96 void PlacesPanel::readSettings()
98 if (GeneralSettings::autoExpandFolders()) {
99 setDragAutoActivationDelay(750);
101 setDragAutoActivationDelay(0);
104 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
105 setIconSize(QSize(iconSize
, iconSize
));
108 void PlacesPanel::showEvent(QShowEvent
* event
)
110 if (!event
->spontaneous() && !model()) {
113 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
114 setModel(placesModel
);
116 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
117 connect(placesModel
, &KFilePlacesModel::teardownDone
, this, &PlacesPanel::slotTearDownDone
);
119 connect(placesModel
, &QAbstractItemModel::rowsInserted
, this, &PlacesPanel::slotRowsInserted
);
120 connect(placesModel
, &QAbstractItemModel::rowsAboutToBeRemoved
, this, &PlacesPanel::slotRowsAboutToBeRemoved
);
122 for (int i
= 0; i
< model()->rowCount(); ++i
) {
123 connectDeviceSignals(model()->index(i
, 0, QModelIndex()));
129 KFilePlacesView::showEvent(event
);
132 static bool isInternalDrag(const QMimeData
*mimeData
)
134 const auto formats
= mimeData
->formats();
135 for (const auto &format
: formats
) {
136 // from KFilePlacesModel::_k_internalMimetype
137 if (format
.startsWith(QLatin1String("application/x-kfileplacesmodel-"))) {
144 void PlacesPanel::dragMoveEvent(QDragMoveEvent
*event
)
146 const QModelIndex index
= indexAt(event
->pos());
147 if (index
.isValid()) {
148 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
150 // Reject drag ontop of a non-writable protocol
151 // We don't know whether we're dropping inbetween or ontop of a place
152 // so still allow internal drag events so that re-arranging still works.
153 const QUrl url
= placesModel
->url(index
);
154 if (url
.isValid() && !isInternalDrag(event
->mimeData()) && !KProtocolManager::supportsWriting(url
)) {
155 event
->setDropAction(Qt::IgnoreAction
);
159 KFilePlacesView::dragMoveEvent(event
);
162 void PlacesPanel::slotConfigureTrash()
164 const QUrl url
= currentIndex().data(KFilePlacesModel::UrlRole
).toUrl();
166 DolphinSettingsDialog
* settingsDialog
= new DolphinSettingsDialog(url
, this);
167 settingsDialog
->setCurrentPage(settingsDialog
->trashSettings
);
168 settingsDialog
->setAttribute(Qt::WA_DeleteOnClose
);
169 settingsDialog
->show();
172 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
174 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
176 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) {
177 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
178 Q_EMIT
errorMessage(job
->errorString());
184 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
188 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
189 const QUrl url
= placesModel
->url(index
);
190 const Solid::Device device
= placesModel
->deviceForIndex(index
);
192 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
194 // show customContextMenuActions only on the view's context menu
195 if (!url
.isValid() && !device
.isValid()) {
196 addActions(m_customContextMenuActions
);
198 const auto actions
= this->actions();
199 for (QAction
*action
: actions
) {
200 if (m_customContextMenuActions
.contains(action
)) {
201 removeAction(action
);
207 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
209 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
211 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
212 if (!storageAccess
) {
216 m_indexToTearDown
= QPersistentModelIndex(index
);
218 // disconnect the Solid::StorageAccess::teardownRequested
219 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
220 // after we have emitted PlacesPanel::storageTearDownRequested
221 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
222 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
225 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
228 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
230 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
233 void PlacesPanel::slotTearDownDone(const QModelIndex
&index
, Solid::ErrorType error
, const QVariant
&errorData
)
235 Q_UNUSED(errorData
); // All error handling is currently done in frameworks.
237 if (index
== m_indexToTearDown
) {
238 if (error
== Solid::ErrorType::NoError
) {
239 // No error; it must have been unmounted successfully
240 Q_EMIT
storageTearDownSuccessful();
242 m_indexToTearDown
= QPersistentModelIndex();
246 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
248 for (int i
= first
; i
<= last
; ++i
) {
249 connectDeviceSignals(model()->index(first
, 0, parent
));
253 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
255 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
257 for (int i
= first
; i
<= last
; ++i
) {
258 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
260 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
261 if (!storageAccess
) {
265 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
269 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
271 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
273 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
274 if (!storageAccess
) {
278 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);