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>
26 #include <KProtocolManager>
34 #include <Solid/StorageAccess>
36 PlacesPanel::PlacesPanel(QWidget
* parent
)
37 : KFilePlacesView(parent
)
39 setDropOnPlaceEnabled(true);
40 connect(this, &PlacesPanel::urlsDropped
,
41 this, &PlacesPanel::slotUrlsDropped
);
43 setAutoResizeItemsEnabled(false);
45 setTeardownFunction([this](const QModelIndex
&index
) {
46 slotTearDownRequested(index
);
49 m_configureTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
50 m_configureTrashAction
->setPriority(QAction::HighPriority
);
51 connect(m_configureTrashAction
, &QAction::triggered
, this, &PlacesPanel::slotConfigureTrash
);
52 addAction(m_configureTrashAction
);
54 connect(this, &PlacesPanel::contextMenuAboutToShow
, this, &PlacesPanel::slotContextMenuAboutToShow
);
56 connect(this, &PlacesPanel::iconSizeChanged
, this, [this](const QSize
&newSize
) {
57 int iconSize
= qMin(newSize
.width(), newSize
.height());
59 // Don't store 0 size, let's keep -1 for default/small/automatic
62 PlacesPanelSettings
* settings
= PlacesPanelSettings::self();
63 settings
->setIconSize(iconSize
);
68 PlacesPanel::~PlacesPanel() = default;
70 void PlacesPanel::setUrl(const QUrl
&url
)
72 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
73 // Remember the URL and set it in showEvent
75 KFilePlacesView::setUrl(url
);
78 QList
<QAction
*> PlacesPanel::customContextMenuActions() const
80 return m_customContextMenuActions
;
83 void PlacesPanel::setCustomContextMenuActions(const QList
<QAction
*> &actions
)
85 m_customContextMenuActions
= actions
;
88 void PlacesPanel::proceedWithTearDown()
90 Q_ASSERT(m_deviceToTearDown
);
92 connect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
93 this, &PlacesPanel::slotTearDownDone
);
94 m_deviceToTearDown
->teardown();
97 void PlacesPanel::readSettings()
99 if (GeneralSettings::autoExpandFolders()) {
100 setDragAutoActivationDelay(750);
102 setDragAutoActivationDelay(0);
105 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
106 setIconSize(QSize(iconSize
, iconSize
));
109 void PlacesPanel::showEvent(QShowEvent
* event
)
111 if (!event
->spontaneous() && !model()) {
114 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
115 setModel(placesModel
);
117 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
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
) { if (job
->error()) Q_EMIT
errorMessage(job
->errorString()); });
180 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex
&index
, QMenu
*menu
)
184 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
185 const QUrl url
= placesModel
->url(index
);
186 const Solid::Device device
= placesModel
->deviceForIndex(index
);
188 m_configureTrashAction
->setVisible(url
.scheme() == QLatin1String("trash"));
190 // show customContextMenuActions only on the view's context menu
191 if (!url
.isValid() && !device
.isValid()) {
192 addActions(m_customContextMenuActions
);
194 const auto actions
= this->actions();
195 for (QAction
*action
: actions
) {
196 if (m_customContextMenuActions
.contains(action
)) {
197 removeAction(action
);
203 void PlacesPanel::slotTearDownRequested(const QModelIndex
&index
)
205 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
207 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
208 if (!storageAccess
) {
212 m_deviceToTearDown
= storageAccess
;
214 // disconnect the Solid::StorageAccess::teardownRequested
215 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
216 // after we have emitted PlacesPanel::storageTearDownRequested
217 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);
218 Q_EMIT
storageTearDownRequested(storageAccess
->filePath());
221 void PlacesPanel::slotTearDownRequestedExternally(const QString
&udi
)
224 auto *storageAccess
= static_cast<Solid::StorageAccess
*>(sender());
226 Q_EMIT
storageTearDownExternallyRequested(storageAccess
->filePath());
229 void PlacesPanel::slotTearDownDone(Solid::ErrorType error
, const QVariant
& errorData
)
231 if (error
&& errorData
.isValid()) {
232 if (error
== Solid::ErrorType::DeviceBusy
) {
233 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
234 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
235 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
237 if (blockingProcesses
.isEmpty()) {
238 errorString
= i18n("One or more files on this device are open within an application.");
240 QStringList blockingApps
;
241 for (const auto& process
: blockingProcesses
) {
242 blockingApps
<< process
.name();
244 blockingApps
.removeDuplicates();
245 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
246 "One or more files on this device are opened in following applications: <application>%2</application>.",
247 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
249 Q_EMIT
errorMessage(errorString
);
251 listOpenFilesJob
->start();
253 Q_EMIT
errorMessage(errorData
.toString());
256 // No error; it must have been unmounted successfully
257 Q_EMIT
storageTearDownSuccessful();
259 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
260 this, &PlacesPanel::slotTearDownDone
);
261 m_deviceToTearDown
= nullptr;
264 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
266 for (int i
= first
; i
<= last
; ++i
) {
267 connectDeviceSignals(model()->index(first
, 0, parent
));
271 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
273 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
275 for (int i
= first
; i
<= last
; ++i
) {
276 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
278 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
279 if (!storageAccess
) {
283 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
287 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
289 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
291 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
292 if (!storageAccess
) {
296 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);