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 <KListOpenFilesJob>
25 #include <KLocalizedString>
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 Q_ASSERT(m_deviceToTearDown
);
90 connect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
91 this, &PlacesPanel::slotTearDownDone
);
92 m_deviceToTearDown
->teardown();
95 void PlacesPanel::readSettings()
97 if (GeneralSettings::autoExpandFolders()) {
98 if (!m_dragActivationTimer
) {
99 m_dragActivationTimer
= new QTimer(this);
100 m_dragActivationTimer
->setInterval(750);
101 m_dragActivationTimer
->setSingleShot(true);
102 connect(m_dragActivationTimer
, &QTimer::timeout
,
103 this, &PlacesPanel::slotDragActivationTimeout
);
106 delete m_dragActivationTimer
;
107 m_dragActivationTimer
= nullptr;
108 m_pendingDragActivation
= QPersistentModelIndex();
111 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
112 setIconSize(QSize(iconSize
, iconSize
));
115 void PlacesPanel::showEvent(QShowEvent
* event
)
117 if (!event
->spontaneous() && !model()) {
120 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
121 setModel(placesModel
);
123 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
125 connect(placesModel
, &QAbstractItemModel::rowsInserted
, this, &PlacesPanel::slotRowsInserted
);
126 connect(placesModel
, &QAbstractItemModel::rowsAboutToBeRemoved
, this, &PlacesPanel::slotRowsAboutToBeRemoved
);
128 for (int i
= 0; i
< model()->rowCount(); ++i
) {
129 connectDeviceSignals(model()->index(i
, 0, QModelIndex()));
135 KFilePlacesView::showEvent(event
);
138 void PlacesPanel::dragMoveEvent(QDragMoveEvent
*event
)
140 KFilePlacesView::dragMoveEvent(event
);
142 if (!m_dragActivationTimer
) {
146 const QModelIndex index
= indexAt(event
->pos());
147 if (!index
.isValid()) {
151 QPersistentModelIndex
persistentIndex(index
);
152 if (!m_pendingDragActivation
.isValid() || m_pendingDragActivation
!= persistentIndex
) {
153 m_pendingDragActivation
= persistentIndex
;
154 m_dragActivationTimer
->start();
158 void PlacesPanel::dragLeaveEvent(QDragLeaveEvent
*event
)
160 KFilePlacesView::dragLeaveEvent(event
);
162 if (m_dragActivationTimer
) {
163 m_dragActivationTimer
->stop();
164 m_pendingDragActivation
= QPersistentModelIndex();
168 void PlacesPanel::slotConfigureTrash()
170 const QUrl url
= currentIndex().data(KFilePlacesModel::UrlRole
).toUrl();
172 DolphinSettingsDialog
* settingsDialog
= new DolphinSettingsDialog(url
, this);
173 settingsDialog
->setCurrentPage(settingsDialog
->trashSettings
);
174 settingsDialog
->setAttribute(Qt::WA_DeleteOnClose
);
175 settingsDialog
->show();
178 void PlacesPanel::slotDragActivationTimeout()
180 if (!m_pendingDragActivation
.isValid()) {
184 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
185 Q_EMIT
placeActivated(KFilePlacesModel::convertedUrl(placesModel
->url(m_pendingDragActivation
)));
188 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
190 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
192 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) Q_EMIT
errorMessage(job
->errorString()); });
196 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
200 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
201 const QUrl url
= placesModel
->url(index
);
202 const Solid::Device device
= placesModel
->deviceForIndex(index
);
204 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
206 // show customContextMenuActions only on the view's context menu
207 if (!url
.isValid() && !device
.isValid()) {
208 addActions(m_customContextMenuActions
);
210 const auto actions
= this->actions();
211 for (QAction
*action
: actions
) {
212 if (m_customContextMenuActions
.contains(action
)) {
213 removeAction(action
);
219 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
221 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
223 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
224 if (!storageAccess
) {
228 m_deviceToTearDown
= storageAccess
;
230 // disconnect the Solid::StorageAccess::teardownRequested
231 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
232 // after we have emitted PlacesPanel::storageTearDownRequested
233 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
234 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
237 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
240 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
242 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
245 void PlacesPanel::slotTearDownDone(Solid::ErrorType error
, const QVariant
& errorData
)
247 if (error
&& errorData
.isValid()) {
248 if (error
== Solid::ErrorType::DeviceBusy
) {
249 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
250 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
251 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
253 if (blockingProcesses
.isEmpty()) {
254 errorString
= i18n("One or more files on this device are open within an application.");
256 QStringList blockingApps
;
257 for (const auto& process
: blockingProcesses
) {
258 blockingApps
<< process
.name();
260 blockingApps
.removeDuplicates();
261 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
262 "One or more files on this device are opened in following applications: <application>%2</application>.",
263 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
265 Q_EMIT
errorMessage(errorString
);
267 listOpenFilesJob
->start();
269 Q_EMIT
errorMessage(errorData
.toString());
272 // No error; it must have been unmounted successfully
273 Q_EMIT
storageTearDownSuccessful();
275 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
276 this, &PlacesPanel::slotTearDownDone
);
277 m_deviceToTearDown
= nullptr;
280 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
282 for (int i
= first
; i
<= last
; ++i
) {
283 connectDeviceSignals(model()->index(first
, 0, parent
));
287 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
289 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
291 for (int i
= first
; i
<= last
; ++i
) {
292 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
294 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
295 if (!storageAccess
) {
299 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
303 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
305 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
307 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
308 if (!storageAccess
) {
312 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);