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>
33 #include <Solid/StorageAccess>
35 PlacesPanel::PlacesPanel(QWidget
* parent
)
36 : KFilePlacesView(parent
)
38 setDropOnPlaceEnabled(true);
39 connect(this, &PlacesPanel::urlsDropped
,
40 this, &PlacesPanel::slotUrlsDropped
);
42 setAutoResizeItemsEnabled(false);
44 setTeardownFunction([this](const QModelIndex
&index
) {
45 slotTearDownRequested(index
);
48 m_configureTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
49 m_configureTrashAction
->setPriority(QAction::HighPriority
);
50 connect(m_configureTrashAction
, &QAction::triggered
, this, &PlacesPanel::slotConfigureTrash
);
51 addAction(m_configureTrashAction
);
53 connect(this, &PlacesPanel::contextMenuAboutToShow
, this, &PlacesPanel::slotContextMenuAboutToShow
);
55 connect(this, &PlacesPanel::iconSizeChanged
, this, [this](const QSize
&newSize
) {
56 int iconSize
= qMin(newSize
.width(), newSize
.height());
58 // Don't store 0 size, let's keep -1 for default/small/automatic
61 PlacesPanelSettings
* settings
= PlacesPanelSettings::self();
62 settings
->setIconSize(iconSize
);
67 PlacesPanel::~PlacesPanel() = default;
69 void PlacesPanel::setUrl(const QUrl
&url
)
71 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
72 // Remember the URL and set it in showEvent
74 KFilePlacesView::setUrl(url
);
77 QList
<QAction
*> PlacesPanel::customContextMenuActions() const
79 return m_customContextMenuActions
;
82 void PlacesPanel::setCustomContextMenuActions(const QList
<QAction
*> &actions
)
84 m_customContextMenuActions
= actions
;
87 void PlacesPanel::proceedWithTearDown()
89 Q_ASSERT(m_deviceToTearDown
);
91 connect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
92 this, &PlacesPanel::slotTearDownDone
);
93 m_deviceToTearDown
->teardown();
96 void PlacesPanel::readSettings()
98 if (GeneralSettings::autoExpandFolders()) {
99 setDragAutoActivationDelay(750);
101 setDragAutoActivationDelay(0);
104 const int iconSize
= qMax(0, PlacesPanelSettings::iconSize());
105 setIconSize(QSize(iconSize
, iconSize
));
108 void PlacesPanel::showEvent(QShowEvent
* event
)
110 if (!event
->spontaneous() && !model()) {
113 auto *placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
114 setModel(placesModel
);
116 connect(placesModel
, &KFilePlacesModel::errorMessage
, this, &PlacesPanel::errorMessage
);
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
->pos());
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_deviceToTearDown
= storageAccess
;
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(Solid::ErrorType error
, const QVariant
& errorData
)
234 if (error
&& errorData
.isValid()) {
235 if (error
== Solid::ErrorType::UserCanceled
) {
236 // No need to tell the user what they just did.
237 } else if (error
== Solid::ErrorType::DeviceBusy
) {
238 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
239 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
240 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
242 if (blockingProcesses
.isEmpty()) {
243 errorString
= i18n("One or more files on this device are open within an application.");
245 QStringList blockingApps
;
246 for (const auto& process
: blockingProcesses
) {
247 blockingApps
<< process
.name();
249 blockingApps
.removeDuplicates();
250 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
251 "One or more files on this device are opened in following applications: <application>%2</application>.",
252 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
254 Q_EMIT
errorMessage(errorString
);
256 listOpenFilesJob
->start();
258 Q_EMIT
errorMessage(errorData
.toString());
261 // No error; it must have been unmounted successfully
262 Q_EMIT
storageTearDownSuccessful();
264 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
265 this, &PlacesPanel::slotTearDownDone
);
266 m_deviceToTearDown
= nullptr;
269 void PlacesPanel::slotRowsInserted(const QModelIndex
&parent
, int first
, int last
)
271 for (int i
= first
; i
<= last
; ++i
) {
272 connectDeviceSignals(model()->index(first
, 0, parent
));
276 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
278 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
280 for (int i
= first
; i
<= last
; ++i
) {
281 const QModelIndex index
= placesModel
->index(i
, 0, parent
);
283 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
284 if (!storageAccess
) {
288 disconnect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, nullptr);
292 void PlacesPanel::connectDeviceSignals(const QModelIndex
&index
)
294 auto *placesModel
= static_cast<KFilePlacesModel
*>(model());
296 Solid::StorageAccess
*storageAccess
= placesModel
->deviceForIndex(index
).as
<Solid::StorageAccess
>();
297 if (!storageAccess
) {
301 connect(storageAccess
, &Solid::StorageAccess::teardownRequested
, this, &PlacesPanel::slotTearDownRequestedExternally
);