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"
18 #include "settings/dolphinsettingsdialog.h"
19 #include "views/draganddrophelper.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
, this, &PlacesPanel::slotUrlsDropped
);
40 setAutoResizeItemsEnabled(false);
42 setTeardownFunction([this](const QModelIndex
&index
) {
43 slotTearDownRequested(index
);
46 m_configureTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
47 m_configureTrashAction
->setPriority(QAction::HighPriority
);
48 connect(m_configureTrashAction
, &QAction::triggered
, this, &PlacesPanel::slotConfigureTrash
);
49 addAction(m_configureTrashAction
);
51 connect(this, &PlacesPanel::contextMenuAboutToShow
, this, &PlacesPanel::slotContextMenuAboutToShow
);
53 connect(this, &PlacesPanel::iconSizeChanged
, this, [this](const QSize
&newSize
) {
54 int iconSize
= qMin(newSize
.width(), newSize
.height());
56 // Don't store 0 size, let's keep -1 for default/small/automatic
59 PlacesPanelSettings
*settings
= PlacesPanelSettings::self();
60 settings
->setIconSize(iconSize
);
65 PlacesPanel::~PlacesPanel() = default;
67 void PlacesPanel::setUrl(const QUrl
&url
)
69 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
70 // Remember the URL and set it in showEvent
72 KFilePlacesView::setUrl(url
);
75 QList
<QAction
*> PlacesPanel::customContextMenuActions() const
77 return m_customContextMenuActions
;
80 void PlacesPanel::setCustomContextMenuActions(const QList
<QAction
*> &actions
)
82 m_customContextMenuActions
= actions
;
85 void PlacesPanel::proceedWithTearDown()
87 if (m_indexToTearDown
.isValid()) {
88 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
89 placesModel
->requestTeardown(m_indexToTearDown
);
91 qWarning() << "Places entry to tear down is no longer valid";
95 void PlacesPanel::readSettings()
97 if (GeneralSettings::autoExpandFolders()) {
98 setDragAutoActivationDelay(750);
100 setDragAutoActivationDelay(0);
103 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
104 setIconSize(QSize(iconSize
, iconSize
));
107 void PlacesPanel::showEvent(QShowEvent
*event
)
109 if (!event
->spontaneous() && !model()) {
112 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
113 setModel(placesModel
);
115 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
116 connect(placesModel
, &KFilePlacesModel::teardownDone
, this, &PlacesPanel::slotTearDownDone
);
118 connect(placesModel
, &QAbstractItemModel::rowsInserted
, this, &PlacesPanel::slotRowsInserted
);
119 connect(placesModel
, &QAbstractItemModel::rowsAboutToBeRemoved
, this, &PlacesPanel::slotRowsAboutToBeRemoved
);
121 for (int i
= 0; i
< model()->rowCount(); ++i
) {
122 connectDeviceSignals(model()->index(i
, 0, QModelIndex()));
128 KFilePlacesView::showEvent(event
);
131 static bool isInternalDrag(const QMimeData
*mimeData
)
133 const auto formats
= mimeData
->formats();
134 for (const auto &format
: formats
) {
135 // from KFilePlacesModel::_k_internalMimetype
136 if (format
.startsWith(QLatin1String("application/x-kfileplacesmodel-"))) {
143 void PlacesPanel::dragMoveEvent(QDragMoveEvent
*event
)
145 const QModelIndex index
= indexAt(event
->position().toPoint());
146 if (index
.isValid()) {
147 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
149 // Reject drag ontop of a non-writable protocol
150 // We don't know whether we're dropping inbetween or ontop of a place
151 // so still allow internal drag events so that re-arranging still works.
152 const QUrl url
= placesModel
->url(index
);
153 if (url
.isValid() && !isInternalDrag(event
->mimeData()) && !KProtocolManager::supportsWriting(url
)) {
154 event
->setDropAction(Qt::IgnoreAction
);
158 KFilePlacesView::dragMoveEvent(event
);
161 void PlacesPanel::slotConfigureTrash()
163 const QUrl url
= currentIndex().data(KFilePlacesModel::UrlRole
).toUrl();
165 DolphinSettingsDialog
*settingsDialog
= new DolphinSettingsDialog(url
, this);
166 settingsDialog
->setCurrentPage(settingsDialog
->trashSettings
);
167 settingsDialog
->setAttribute(Qt::WA_DeleteOnClose
);
168 settingsDialog
->show();
171 void PlacesPanel::slotUrlsDropped(const QUrl
&dest
, QDropEvent
*event
, QWidget
*parent
)
173 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
175 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) {
176 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
177 Q_EMIT
errorMessage(job
->errorString());
183 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
187 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
188 const QUrl url
= placesModel
->url(index
);
189 const Solid::Device device
= placesModel
->deviceForIndex(index
);
191 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
193 // show customContextMenuActions only on the view's context menu
194 if (!url
.isValid() && !device
.isValid()) {
195 addActions(m_customContextMenuActions
);
197 const auto actions
= this->actions();
198 for (QAction
*action
: actions
) {
199 if (m_customContextMenuActions
.contains(action
)) {
200 removeAction(action
);
206 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
208 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
210 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
211 if (!storageAccess
) {
215 m_indexToTearDown
= QPersistentModelIndex(index
);
217 // disconnect the Solid::StorageAccess::teardownRequested
218 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
219 // after we have emitted PlacesPanel::storageTearDownRequested
220 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
221 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
224 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
227 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
229 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
232 void PlacesPanel::slotTearDownDone(const QModelIndex
&index
, Solid::ErrorType error
, const QVariant
&errorData
)
234 Q_UNUSED(errorData
); // All error handling is currently done in frameworks.
236 if (index
== m_indexToTearDown
) {
237 if (error
== Solid::ErrorType::NoError
) {
238 // No error; it must have been unmounted successfully
239 Q_EMIT
storageTearDownSuccessful();
241 m_indexToTearDown
= QPersistentModelIndex();
245 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
247 for (int i
= first
; i
<= last
; ++i
) {
248 connectDeviceSignals(model()->index(first
, 0, parent
));
252 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
254 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
256 for (int i
= first
; i
<= last
; ++i
) {
257 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
259 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
260 if (!storageAccess
) {
264 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
268 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
270 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
272 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
273 if (!storageAccess
) {
277 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);