]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Port back to KFilePlacesView
[dolphin.git] / src / panels / places / placespanel.cpp
1 /*
2 * SPDX-FileCopyrightText: 2008-2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2021 Kai Uwe Broulik <kde@broulik.de>
4 *
5 * Based on KFilePlacesView from kdelibs:
6 * SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
7 * SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 #include "placespanel.h"
13
14 #include "dolphinplacesmodelsingleton.h"
15 #include "dolphin_generalsettings.h"
16 #include "dolphin_placespanelsettings.h"
17 #include "global.h"
18 #include "views/draganddrophelper.h"
19 #include "settings/dolphinsettingsdialog.h"
20
21 #include <KFilePlacesModel>
22 #include <KIO/DropJob>
23 #include <KIO/Job>
24 #include <KListOpenFilesJob>
25 #include <KLocalizedString>
26
27 #include <QIcon>
28 #include <QMenu>
29 #include <QShowEvent>
30 #include <QTimer>
31
32 #include <Solid/StorageAccess>
33
34 PlacesPanel::PlacesPanel(QWidget* parent)
35 : KFilePlacesView(parent)
36 {
37 setDropOnPlaceEnabled(true);
38 connect(this, &PlacesPanel::urlsDropped,
39 this, &PlacesPanel::slotUrlsDropped);
40
41 setAutoResizeItemsEnabled(false);
42
43 setTeardownFunction([this](const QModelIndex &index) {
44 slotTearDownRequested(index);
45 });
46
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);
51
52 connect(this, &PlacesPanel::contextMenuAboutToShow, this, &PlacesPanel::slotContextMenuAboutToShow);
53
54 connect(this, &PlacesPanel::iconSizeChanged, this, [this](const QSize &newSize) {
55 int iconSize = qMin(newSize.width(), newSize.height());
56 if (iconSize == 0) {
57 // Don't store 0 size, let's keep -1 for default/small/automatic
58 iconSize = -1;
59 }
60 PlacesPanelSettings* settings = PlacesPanelSettings::self();
61 settings->setIconSize(iconSize);
62 settings->save();
63 });
64 }
65
66 PlacesPanel::~PlacesPanel() = default;
67
68 void PlacesPanel::setUrl(const QUrl &url)
69 {
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
72 m_url = url;
73 KFilePlacesView::setUrl(url);
74 }
75
76 QList<QAction*> PlacesPanel::customContextMenuActions() const
77 {
78 return m_customContextMenuActions;
79 }
80
81 void PlacesPanel::setCustomContextMenuActions(const QList<QAction *> &actions)
82 {
83 m_customContextMenuActions = actions;
84 }
85
86 void PlacesPanel::proceedWithTearDown()
87 {
88 Q_ASSERT(m_deviceToTearDown);
89
90 connect(m_deviceToTearDown, &Solid::StorageAccess::teardownDone,
91 this, &PlacesPanel::slotTearDownDone);
92 m_deviceToTearDown->teardown();
93 }
94
95 void PlacesPanel::readSettings()
96 {
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);
104 }
105 } else {
106 delete m_dragActivationTimer;
107 m_dragActivationTimer = nullptr;
108 m_pendingDragActivation = QPersistentModelIndex();
109 }
110
111 const int iconSize = qMax(0, PlacesPanelSettings::iconSize());
112 setIconSize(QSize(iconSize, iconSize));
113 }
114
115 void PlacesPanel::showEvent(QShowEvent* event)
116 {
117 if (!event->spontaneous() && !model()) {
118 readSettings();
119
120 auto *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
121 setModel(placesModel);
122
123 connect(placesModel, &KFilePlacesModel::errorMessage, this, &PlacesPanel::errorMessage);
124
125 connect(placesModel, &QAbstractItemModel::rowsInserted, this, &PlacesPanel::slotRowsInserted);
126 connect(placesModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &PlacesPanel::slotRowsAboutToBeRemoved);
127
128 for (int i = 0; i < model()->rowCount(); ++i) {
129 connectDeviceSignals(model()->index(i, 0, QModelIndex()));
130 }
131
132 setUrl(m_url);
133 }
134
135 KFilePlacesView::showEvent(event);
136 }
137
138 void PlacesPanel::dragMoveEvent(QDragMoveEvent *event)
139 {
140 KFilePlacesView::dragMoveEvent(event);
141
142 if (!m_dragActivationTimer) {
143 return;
144 }
145
146 const QModelIndex index = indexAt(event->pos());
147 if (!index.isValid()) {
148 return;
149 }
150
151 QPersistentModelIndex persistentIndex(index);
152 if (!m_pendingDragActivation.isValid() || m_pendingDragActivation != persistentIndex) {
153 m_pendingDragActivation = persistentIndex;
154 m_dragActivationTimer->start();
155 }
156 }
157
158 void PlacesPanel::dragLeaveEvent(QDragLeaveEvent *event)
159 {
160 KFilePlacesView::dragLeaveEvent(event);
161
162 if (m_dragActivationTimer) {
163 m_dragActivationTimer->stop();
164 m_pendingDragActivation = QPersistentModelIndex();
165 }
166 }
167
168 void PlacesPanel::slotConfigureTrash()
169 {
170 const QUrl url = currentIndex().data(KFilePlacesModel::UrlRole).toUrl();
171
172 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
173 settingsDialog->setCurrentPage(settingsDialog->trashSettings);
174 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
175 settingsDialog->show();
176 }
177
178 void PlacesPanel::slotDragActivationTimeout()
179 {
180 if (!m_pendingDragActivation.isValid()) {
181 return;
182 }
183
184 auto *placesModel = static_cast<KFilePlacesModel *>(model());
185 Q_EMIT placeActivated(KFilePlacesModel::convertedUrl(placesModel->url(m_pendingDragActivation)));
186 }
187
188 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
189 {
190 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
191 if (job) {
192 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) Q_EMIT errorMessage(job->errorString()); });
193 }
194 }
195
196 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex &index, QMenu *menu)
197 {
198 Q_UNUSED(menu);
199
200 auto *placesModel = static_cast<KFilePlacesModel *>(model());
201 const QUrl url = placesModel->url(index);
202 const Solid::Device device = placesModel->deviceForIndex(index);
203
204 m_configureTrashAction->setVisible(url.scheme() == QLatin1String("trash"));
205
206 // show customContextMenuActions only on the view's context menu
207 if (!url.isValid() && !device.isValid()) {
208 addActions(m_customContextMenuActions);
209 } else {
210 const auto actions = this->actions();
211 for (QAction *action : actions) {
212 if (m_customContextMenuActions.contains(action)) {
213 removeAction(action);
214 }
215 }
216 }
217 }
218
219 void PlacesPanel::slotTearDownRequested(const QModelIndex &index)
220 {
221 auto *placesModel = static_cast<KFilePlacesModel *>(model());
222
223 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
224 if (!storageAccess) {
225 return;
226 }
227
228 m_deviceToTearDown = storageAccess;
229
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());
235 }
236
237 void PlacesPanel::slotTearDownRequestedExternally(const QString &udi)
238 {
239 Q_UNUSED(udi);
240 auto *storageAccess = static_cast<Solid::StorageAccess*>(sender());
241
242 Q_EMIT storageTearDownExternallyRequested(storageAccess->filePath());
243 }
244
245 void PlacesPanel::slotTearDownDone(Solid::ErrorType error, const QVariant& errorData)
246 {
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();
252 QString errorString;
253 if (blockingProcesses.isEmpty()) {
254 errorString = i18n("One or more files on this device are open within an application.");
255 } else {
256 QStringList blockingApps;
257 for (const auto& process : blockingProcesses) {
258 blockingApps << process.name();
259 }
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", ", ")));
264 }
265 Q_EMIT errorMessage(errorString);
266 });
267 listOpenFilesJob->start();
268 } else {
269 Q_EMIT errorMessage(errorData.toString());
270 }
271 } else {
272 // No error; it must have been unmounted successfully
273 Q_EMIT storageTearDownSuccessful();
274 }
275 disconnect(m_deviceToTearDown, &Solid::StorageAccess::teardownDone,
276 this, &PlacesPanel::slotTearDownDone);
277 m_deviceToTearDown = nullptr;
278 }
279
280 void PlacesPanel::slotRowsInserted(const QModelIndex &parent, int first, int last)
281 {
282 for (int i = first; i <= last; ++i) {
283 connectDeviceSignals(model()->index(first, 0, parent));
284 }
285 }
286
287 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
288 {
289 auto *placesModel = static_cast<KFilePlacesModel *>(model());
290
291 for (int i = first; i <= last; ++i) {
292 const QModelIndex index = placesModel->index(i, 0, parent);
293
294 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
295 if (!storageAccess) {
296 continue;
297 }
298
299 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, nullptr);
300 }
301 }
302
303 void PlacesPanel::connectDeviceSignals(const QModelIndex &index)
304 {
305 auto *placesModel = static_cast<KFilePlacesModel *>(model());
306
307 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
308 if (!storageAccess) {
309 return;
310 }
311
312 connect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
313 }