]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Places Panel: Delegate open file error to KIO
[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 <KLocalizedString>
25 #include <KProtocolManager>
26
27 #include <QIcon>
28 #include <QMenu>
29 #include <QMimeData>
30 #include <QShowEvent>
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 if (m_indexToTearDown.isValid()) {
89 auto *placesModel = static_cast<KFilePlacesModel *>(model());
90 placesModel->requestTeardown(m_indexToTearDown);
91 } else {
92 qWarning() << "Places entry to tear down is no longer valid";
93 }
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 connect(placesModel, &KFilePlacesModel::teardownDone, this, &PlacesPanel::slotTearDownDone);
118
119 connect(placesModel, &QAbstractItemModel::rowsInserted, this, &PlacesPanel::slotRowsInserted);
120 connect(placesModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &PlacesPanel::slotRowsAboutToBeRemoved);
121
122 for (int i = 0; i < model()->rowCount(); ++i) {
123 connectDeviceSignals(model()->index(i, 0, QModelIndex()));
124 }
125
126 setUrl(m_url);
127 }
128
129 KFilePlacesView::showEvent(event);
130 }
131
132 static bool isInternalDrag(const QMimeData *mimeData)
133 {
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-"))) {
138 return true;
139 }
140 }
141 return false;
142 }
143
144 void PlacesPanel::dragMoveEvent(QDragMoveEvent *event)
145 {
146 const QModelIndex index = indexAt(event->pos());
147 if (index.isValid()) {
148 auto *placesModel = static_cast<KFilePlacesModel *>(model());
149
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);
156 }
157 }
158
159 KFilePlacesView::dragMoveEvent(event);
160 }
161
162 void PlacesPanel::slotConfigureTrash()
163 {
164 const QUrl url = currentIndex().data(KFilePlacesModel::UrlRole).toUrl();
165
166 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
167 settingsDialog->setCurrentPage(settingsDialog->trashSettings);
168 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
169 settingsDialog->show();
170 }
171
172 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
173 {
174 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
175 if (job) {
176 connect(job, &KIO::DropJob::result, this, [this](KJob *job) {
177 if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
178 Q_EMIT errorMessage(job->errorString());
179 }
180 });
181 }
182 }
183
184 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex &index, QMenu *menu)
185 {
186 Q_UNUSED(menu);
187
188 auto *placesModel = static_cast<KFilePlacesModel *>(model());
189 const QUrl url = placesModel->url(index);
190 const Solid::Device device = placesModel->deviceForIndex(index);
191
192 m_configureTrashAction->setVisible(url.scheme() == QLatin1String("trash"));
193
194 // show customContextMenuActions only on the view's context menu
195 if (!url.isValid() && !device.isValid()) {
196 addActions(m_customContextMenuActions);
197 } else {
198 const auto actions = this->actions();
199 for (QAction *action : actions) {
200 if (m_customContextMenuActions.contains(action)) {
201 removeAction(action);
202 }
203 }
204 }
205 }
206
207 void PlacesPanel::slotTearDownRequested(const QModelIndex &index)
208 {
209 auto *placesModel = static_cast<KFilePlacesModel *>(model());
210
211 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
212 if (!storageAccess) {
213 return;
214 }
215
216 m_indexToTearDown = QPersistentModelIndex(index);
217
218 // disconnect the Solid::StorageAccess::teardownRequested
219 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
220 // after we have emitted PlacesPanel::storageTearDownRequested
221 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
222 Q_EMIT storageTearDownRequested(storageAccess->filePath());
223 }
224
225 void PlacesPanel::slotTearDownRequestedExternally(const QString &udi)
226 {
227 Q_UNUSED(udi);
228 auto *storageAccess = static_cast<Solid::StorageAccess*>(sender());
229
230 Q_EMIT storageTearDownExternallyRequested(storageAccess->filePath());
231 }
232
233 void PlacesPanel::slotTearDownDone(const QModelIndex &index, Solid::ErrorType error, const QVariant &errorData)
234 {
235 Q_UNUSED(errorData); // All error handling is currently done in frameworks.
236
237 if (index == m_indexToTearDown) {
238 if (error == Solid::ErrorType::NoError) {
239 // No error; it must have been unmounted successfully
240 Q_EMIT storageTearDownSuccessful();
241 }
242 m_indexToTearDown = QPersistentModelIndex();
243 }
244 }
245
246 void PlacesPanel::slotRowsInserted(const QModelIndex &parent, int first, int last)
247 {
248 for (int i = first; i <= last; ++i) {
249 connectDeviceSignals(model()->index(first, 0, parent));
250 }
251 }
252
253 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
254 {
255 auto *placesModel = static_cast<KFilePlacesModel *>(model());
256
257 for (int i = first; i <= last; ++i) {
258 const QModelIndex index = placesModel->index(i, 0, parent);
259
260 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
261 if (!storageAccess) {
262 continue;
263 }
264
265 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, nullptr);
266 }
267 }
268
269 void PlacesPanel::connectDeviceSignals(const QModelIndex &index)
270 {
271 auto *placesModel = static_cast<KFilePlacesModel *>(model());
272
273 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
274 if (!storageAccess) {
275 return;
276 }
277
278 connect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
279 }