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 "dolphin_generalsettings.h"
15 #include "dolphin_placespanelsettings.h"
16 #include "dolphinplacesmodelsingleton.h"
17 #include "settings/dolphinsettingsdialog.h"
18 #include "views/draganddrophelper.h"
20 #include <KFilePlacesModel>
21 #include <KIO/DropJob>
23 #include <KLocalizedString>
24 #include <KProtocolManager>
31 #include <Solid/StorageAccess>
33 PlacesPanel::PlacesPanel(QWidget
*parent
)
34 : KFilePlacesView(parent
)
36 setDropOnPlaceEnabled(true);
37 connect(this, &PlacesPanel::urlsDropped
, this, &PlacesPanel::slotUrlsDropped
);
39 setAutoResizeItemsEnabled(false);
41 setTeardownFunction([this](const QModelIndex
&index
) {
42 slotTearDownRequested(index
);
45 m_configureTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
46 m_configureTrashAction
->setPriority(QAction::HighPriority
);
47 connect(m_configureTrashAction
, &QAction::triggered
, this, &PlacesPanel::slotConfigureTrash
);
48 addAction(m_configureTrashAction
);
50 connect(this, &PlacesPanel::contextMenuAboutToShow
, this, &PlacesPanel::slotContextMenuAboutToShow
);
52 connect(this, &PlacesPanel::iconSizeChanged
, this, [](const QSize
&newSize
) {
53 int iconSize
= qMin(newSize
.width(), newSize
.height());
55 // Don't store 0 size, let's keep -1 for default/small/automatic
58 PlacesPanelSettings
*settings
= PlacesPanelSettings::self();
59 settings
->setIconSize(iconSize
);
64 PlacesPanel::~PlacesPanel() = default;
66 void PlacesPanel::setUrl(const QUrl
&url
)
68 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
69 // Remember the URL and set it in showEvent
71 KFilePlacesView::setUrl(url
);
74 QList
<QAction
*> PlacesPanel::customContextMenuActions() const
76 return m_customContextMenuActions
;
79 void PlacesPanel::setCustomContextMenuActions(const QList
<QAction
*> &actions
)
81 m_customContextMenuActions
= actions
;
84 void PlacesPanel::proceedWithTearDown()
86 if (m_indexToTearDown
.isValid()) {
87 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
88 placesModel
->requestTeardown(m_indexToTearDown
);
90 qWarning() << "Places entry to tear down is no longer valid";
94 void PlacesPanel::readSettings()
96 if (GeneralSettings::autoExpandFolders()) {
97 setDragAutoActivationDelay(750);
99 setDragAutoActivationDelay(0);
102 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
103 setIconSize(QSize(iconSize
, iconSize
));
106 void PlacesPanel::showEvent(QShowEvent
*event
)
108 if (!event
->spontaneous() && !model()) {
111 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
112 setModel(placesModel
);
114 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
115 connect(placesModel
, &KFilePlacesModel::teardownDone
, this, &PlacesPanel::slotTearDownDone
);
117 connect(placesModel
, &QAbstractItemModel::rowsInserted
, this, &PlacesPanel::slotRowsInserted
);
118 connect(placesModel
, &QAbstractItemModel::rowsAboutToBeRemoved
, this, &PlacesPanel::slotRowsAboutToBeRemoved
);
120 for (int i
= 0; i
< model()->rowCount(); ++i
) {
121 connectDeviceSignals(model()->index(i
, 0, QModelIndex()));
127 KFilePlacesView::showEvent(event
);
130 static bool isInternalDrag(const QMimeData
*mimeData
)
132 const auto formats
= mimeData
->formats();
133 for (const auto &format
: formats
) {
134 // from KFilePlacesModel::_k_internalMimetype
135 if (format
.startsWith(QLatin1String("application/x-kfileplacesmodel-"))) {
142 void PlacesPanel::dragMoveEvent(QDragMoveEvent
*event
)
144 const QModelIndex index
= indexAt(event
->position().toPoint());
145 if (index
.isValid()) {
146 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
148 // Reject drag ontop of a non-writable protocol
149 // We don't know whether we're dropping inbetween or ontop of a place
150 // so still allow internal drag events so that re-arranging still works.
151 const QUrl url
= placesModel
->url(index
);
152 if (url
.isValid() && !isInternalDrag(event
->mimeData()) && !KProtocolManager::supportsWriting(url
)) {
153 event
->setDropAction(Qt::IgnoreAction
);
157 KFilePlacesView::dragMoveEvent(event
);
160 void PlacesPanel::slotConfigureTrash()
162 const QUrl url
= currentIndex().data(KFilePlacesModel::UrlRole
).toUrl();
164 DolphinSettingsDialog
*settingsDialog
= new DolphinSettingsDialog(url
, this);
165 settingsDialog
->setCurrentPage(settingsDialog
->trashSettings
);
166 settingsDialog
->setAttribute(Qt::WA_DeleteOnClose
);
167 settingsDialog
->show();
170 void PlacesPanel::slotUrlsDropped(const QUrl
&dest
, QDropEvent
*event
, QWidget
*parent
)
172 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
174 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) {
175 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
176 Q_EMIT
errorMessage(job
->errorString());
182 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
186 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
187 const QUrl url
= placesModel
->url(index
);
188 const Solid::Device device
= placesModel
->deviceForIndex(index
);
190 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
192 // show customContextMenuActions only on the view's context menu
193 if (!url
.isValid() && !device
.isValid()) {
194 addActions(m_customContextMenuActions
);
196 const auto actions
= this->actions();
197 for (QAction
*action
: actions
) {
198 if (m_customContextMenuActions
.contains(action
)) {
199 removeAction(action
);
205 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
207 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
209 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
210 if (!storageAccess
) {
214 m_indexToTearDown
= QPersistentModelIndex(index
);
216 // disconnect the Solid::StorageAccess::teardownRequested
217 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
218 // after we have emitted PlacesPanel::storageTearDownRequested
219 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
220 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
223 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
226 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
228 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
231 void PlacesPanel::slotTearDownDone(const QModelIndex
&index
, Solid::ErrorType error
, const QVariant
&errorData
)
233 Q_UNUSED(errorData
); // All error handling is currently done in frameworks.
235 if (index
== m_indexToTearDown
) {
236 if (error
== Solid::ErrorType::NoError
) {
237 // No error; it must have been unmounted successfully
238 Q_EMIT
storageTearDownSuccessful();
240 m_indexToTearDown
= QPersistentModelIndex();
244 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
246 for (int i
= first
; i
<= last
; ++i
) {
247 connectDeviceSignals(model()->index(first
, 0, parent
));
251 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
253 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
255 for (int i
= first
; i
<= last
; ++i
) {
256 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
258 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
259 if (!storageAccess
) {
263 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
267 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
269 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
271 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
272 if (!storageAccess
) {
276 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);