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 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
);
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 void PlacesPanel::slotConfigureTrash()
132 const QUrl url
= currentIndex().data(KFilePlacesModel::UrlRole
).toUrl();
134 DolphinSettingsDialog
* settingsDialog
= new DolphinSettingsDialog(url
, this);
135 settingsDialog
->setCurrentPage(settingsDialog
->trashSettings
);
136 settingsDialog
->setAttribute(Qt::WA_DeleteOnClose
);
137 settingsDialog
->show();
140 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
142 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
144 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) Q_EMIT
errorMessage(job
->errorString()); });
148 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
152 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
153 const QUrl url
= placesModel
->url(index
);
154 const Solid::Device device
= placesModel
->deviceForIndex(index
);
156 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
158 // show customContextMenuActions only on the view's context menu
159 if (!url
.isValid() && !device
.isValid()) {
160 addActions(m_customContextMenuActions
);
162 const auto actions
= this->actions();
163 for (QAction
*action
: actions
) {
164 if (m_customContextMenuActions
.contains(action
)) {
165 removeAction(action
);
171 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
173 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
175 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
176 if (!storageAccess
) {
180 m_deviceToTearDown
= storageAccess
;
182 // disconnect the Solid::StorageAccess::teardownRequested
183 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
184 // after we have emitted PlacesPanel::storageTearDownRequested
185 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
186 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
189 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
192 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
194 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
197 void PlacesPanel::slotTearDownDone(Solid::ErrorType error
, const QVariant
& errorData
)
199 if (error
&& errorData
.isValid()) {
200 if (error
== Solid::ErrorType::DeviceBusy
) {
201 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
202 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
203 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
205 if (blockingProcesses
.isEmpty()) {
206 errorString
= i18n("One or more files on this device are open within an application.");
208 QStringList blockingApps
;
209 for (const auto& process
: blockingProcesses
) {
210 blockingApps
<< process
.name();
212 blockingApps
.removeDuplicates();
213 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
214 "One or more files on this device are opened in following applications: <application>%2</application>.",
215 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
217 Q_EMIT
errorMessage(errorString
);
219 listOpenFilesJob
->start();
221 Q_EMIT
errorMessage(errorData
.toString());
224 // No error; it must have been unmounted successfully
225 Q_EMIT
storageTearDownSuccessful();
227 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
228 this, &PlacesPanel::slotTearDownDone
);
229 m_deviceToTearDown
= nullptr;
232 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
234 for (int i
= first
; i
<= last
; ++i
) {
235 connectDeviceSignals(model()->index(first
, 0, parent
));
239 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
241 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
243 for (int i
= first
; i
<= last
; ++i
) {
244 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
246 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
247 if (!storageAccess
) {
251 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
255 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
257 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
259 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
260 if (!storageAccess
) {
264 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);