]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Remove unused includes
[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 #include <KProtocolManager>
27
28 #include <QIcon>
29 #include <QMenu>
30 #include <QMimeData>
31 #include <QShowEvent>
32
33 #include <Solid/StorageAccess>
34
35 PlacesPanel::PlacesPanel(QWidget* parent)
36 : KFilePlacesView(parent)
37 {
38 setDropOnPlaceEnabled(true);
39 connect(this, &PlacesPanel::urlsDropped,
40 this, &PlacesPanel::slotUrlsDropped);
41
42 setAutoResizeItemsEnabled(false);
43
44 setTeardownFunction([this](const QModelIndex &index) {
45 slotTearDownRequested(index);
46 });
47
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);
52
53 connect(this, &PlacesPanel::contextMenuAboutToShow, this, &PlacesPanel::slotContextMenuAboutToShow);
54
55 connect(this, &PlacesPanel::iconSizeChanged, this, [this](const QSize &newSize) {
56 int iconSize = qMin(newSize.width(), newSize.height());
57 if (iconSize == 0) {
58 // Don't store 0 size, let's keep -1 for default/small/automatic
59 iconSize = -1;
60 }
61 PlacesPanelSettings* settings = PlacesPanelSettings::self();
62 settings->setIconSize(iconSize);
63 settings->save();
64 });
65 }
66
67 PlacesPanel::~PlacesPanel() = default;
68
69 void PlacesPanel::setUrl(const QUrl &url)
70 {
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
73 m_url = url;
74 KFilePlacesView::setUrl(url);
75 }
76
77 QList<QAction*> PlacesPanel::customContextMenuActions() const
78 {
79 return m_customContextMenuActions;
80 }
81
82 void PlacesPanel::setCustomContextMenuActions(const QList<QAction *> &actions)
83 {
84 m_customContextMenuActions = actions;
85 }
86
87 void PlacesPanel::proceedWithTearDown()
88 {
89 Q_ASSERT(m_deviceToTearDown);
90
91 connect(m_deviceToTearDown, &Solid::StorageAccess::teardownDone,
92 this, &PlacesPanel::slotTearDownDone);
93 m_deviceToTearDown->teardown();
94 }
95
96 void PlacesPanel::readSettings()
97 {
98 if (GeneralSettings::autoExpandFolders()) {
99 setDragAutoActivationDelay(750);
100 } else {
101 setDragAutoActivationDelay(0);
102 }
103
104 const int iconSize = qMax(0, PlacesPanelSettings::iconSize());
105 setIconSize(QSize(iconSize, iconSize));
106 }
107
108 void PlacesPanel::showEvent(QShowEvent* event)
109 {
110 if (!event->spontaneous() && !model()) {
111 readSettings();
112
113 auto *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
114 setModel(placesModel);
115
116 connect(placesModel, &KFilePlacesModel::errorMessage, this, &PlacesPanel::errorMessage);
117
118 connect(placesModel, &QAbstractItemModel::rowsInserted, this, &PlacesPanel::slotRowsInserted);
119 connect(placesModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &PlacesPanel::slotRowsAboutToBeRemoved);
120
121 for (int i = 0; i < model()->rowCount(); ++i) {
122 connectDeviceSignals(model()->index(i, 0, QModelIndex()));
123 }
124
125 setUrl(m_url);
126 }
127
128 KFilePlacesView::showEvent(event);
129 }
130
131 static bool isInternalDrag(const QMimeData *mimeData)
132 {
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-"))) {
137 return true;
138 }
139 }
140 return false;
141 }
142
143 void PlacesPanel::dragMoveEvent(QDragMoveEvent *event)
144 {
145 const QModelIndex index = indexAt(event->pos());
146 if (index.isValid()) {
147 auto *placesModel = static_cast<KFilePlacesModel *>(model());
148
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);
155 }
156 }
157
158 KFilePlacesView::dragMoveEvent(event);
159 }
160
161 void PlacesPanel::slotConfigureTrash()
162 {
163 const QUrl url = currentIndex().data(KFilePlacesModel::UrlRole).toUrl();
164
165 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
166 settingsDialog->setCurrentPage(settingsDialog->trashSettings);
167 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
168 settingsDialog->show();
169 }
170
171 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
172 {
173 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
174 if (job) {
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());
178 }
179 });
180 }
181 }
182
183 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex &index, QMenu *menu)
184 {
185 Q_UNUSED(menu);
186
187 auto *placesModel = static_cast<KFilePlacesModel *>(model());
188 const QUrl url = placesModel->url(index);
189 const Solid::Device device = placesModel->deviceForIndex(index);
190
191 m_configureTrashAction->setVisible(url.scheme() == QLatin1String("trash"));
192
193 // show customContextMenuActions only on the view's context menu
194 if (!url.isValid() && !device.isValid()) {
195 addActions(m_customContextMenuActions);
196 } else {
197 const auto actions = this->actions();
198 for (QAction *action : actions) {
199 if (m_customContextMenuActions.contains(action)) {
200 removeAction(action);
201 }
202 }
203 }
204 }
205
206 void PlacesPanel::slotTearDownRequested(const QModelIndex &index)
207 {
208 auto *placesModel = static_cast<KFilePlacesModel *>(model());
209
210 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
211 if (!storageAccess) {
212 return;
213 }
214
215 m_deviceToTearDown = storageAccess;
216
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());
222 }
223
224 void PlacesPanel::slotTearDownRequestedExternally(const QString &udi)
225 {
226 Q_UNUSED(udi);
227 auto *storageAccess = static_cast<Solid::StorageAccess*>(sender());
228
229 Q_EMIT storageTearDownExternallyRequested(storageAccess->filePath());
230 }
231
232 void PlacesPanel::slotTearDownDone(Solid::ErrorType error, const QVariant& errorData)
233 {
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();
241 QString errorString;
242 if (blockingProcesses.isEmpty()) {
243 errorString = i18n("One or more files on this device are open within an application.");
244 } else {
245 QStringList blockingApps;
246 for (const auto& process : blockingProcesses) {
247 blockingApps << process.name();
248 }
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", ", ")));
253 }
254 Q_EMIT errorMessage(errorString);
255 });
256 listOpenFilesJob->start();
257 } else {
258 Q_EMIT errorMessage(errorData.toString());
259 }
260 } else {
261 // No error; it must have been unmounted successfully
262 Q_EMIT storageTearDownSuccessful();
263 }
264 disconnect(m_deviceToTearDown, &Solid::StorageAccess::teardownDone,
265 this, &PlacesPanel::slotTearDownDone);
266 m_deviceToTearDown = nullptr;
267 }
268
269 void PlacesPanel::slotRowsInserted(const QModelIndex &parent, int first, int last)
270 {
271 for (int i = first; i <= last; ++i) {
272 connectDeviceSignals(model()->index(first, 0, parent));
273 }
274 }
275
276 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
277 {
278 auto *placesModel = static_cast<KFilePlacesModel *>(model());
279
280 for (int i = first; i <= last; ++i) {
281 const QModelIndex index = placesModel->index(i, 0, parent);
282
283 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
284 if (!storageAccess) {
285 continue;
286 }
287
288 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, nullptr);
289 }
290 }
291
292 void PlacesPanel::connectDeviceSignals(const QModelIndex &index)
293 {
294 auto *placesModel = static_cast<KFilePlacesModel *>(model());
295
296 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
297 if (!storageAccess) {
298 return;
299 }
300
301 connect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
302 }