]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Port to Qt6
[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 "dolphin_generalsettings.h"
15 #include "dolphin_placespanelsettings.h"
16 #include "dolphinplacesmodelsingleton.h"
17 #include "global.h"
18 #include "settings/dolphinsettingsdialog.h"
19 #include "views/draganddrophelper.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, this, &PlacesPanel::slotUrlsDropped);
39
40 setAutoResizeItemsEnabled(false);
41
42 setTeardownFunction([this](const QModelIndex &index) {
43 slotTearDownRequested(index);
44 });
45
46 m_configureTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
47 m_configureTrashAction->setPriority(QAction::HighPriority);
48 connect(m_configureTrashAction, &QAction::triggered, this, &PlacesPanel::slotConfigureTrash);
49 addAction(m_configureTrashAction);
50
51 connect(this, &PlacesPanel::contextMenuAboutToShow, this, &PlacesPanel::slotContextMenuAboutToShow);
52
53 connect(this, &PlacesPanel::iconSizeChanged, this, [this](const QSize &newSize) {
54 int iconSize = qMin(newSize.width(), newSize.height());
55 if (iconSize == 0) {
56 // Don't store 0 size, let's keep -1 for default/small/automatic
57 iconSize = -1;
58 }
59 PlacesPanelSettings *settings = PlacesPanelSettings::self();
60 settings->setIconSize(iconSize);
61 settings->save();
62 });
63 }
64
65 PlacesPanel::~PlacesPanel() = default;
66
67 void PlacesPanel::setUrl(const QUrl &url)
68 {
69 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
70 // Remember the URL and set it in showEvent
71 m_url = url;
72 KFilePlacesView::setUrl(url);
73 }
74
75 QList<QAction *> PlacesPanel::customContextMenuActions() const
76 {
77 return m_customContextMenuActions;
78 }
79
80 void PlacesPanel::setCustomContextMenuActions(const QList<QAction *> &actions)
81 {
82 m_customContextMenuActions = actions;
83 }
84
85 void PlacesPanel::proceedWithTearDown()
86 {
87 if (m_indexToTearDown.isValid()) {
88 auto *placesModel = static_cast<KFilePlacesModel *>(model());
89 placesModel->requestTeardown(m_indexToTearDown);
90 } else {
91 qWarning() << "Places entry to tear down is no longer valid";
92 }
93 }
94
95 void PlacesPanel::readSettings()
96 {
97 if (GeneralSettings::autoExpandFolders()) {
98 setDragAutoActivationDelay(750);
99 } else {
100 setDragAutoActivationDelay(0);
101 }
102
103 const int iconSize = qMax(0, PlacesPanelSettings::iconSize());
104 setIconSize(QSize(iconSize, iconSize));
105 }
106
107 void PlacesPanel::showEvent(QShowEvent *event)
108 {
109 if (!event->spontaneous() && !model()) {
110 readSettings();
111
112 auto *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
113 setModel(placesModel);
114
115 connect(placesModel, &KFilePlacesModel::errorMessage, this, &PlacesPanel::errorMessage);
116 connect(placesModel, &KFilePlacesModel::teardownDone, this, &PlacesPanel::slotTearDownDone);
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->position().toPoint());
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_indexToTearDown = QPersistentModelIndex(index);
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(const QModelIndex &index, Solid::ErrorType error, const QVariant &errorData)
233 {
234 Q_UNUSED(errorData); // All error handling is currently done in frameworks.
235
236 if (index == m_indexToTearDown) {
237 if (error == Solid::ErrorType::NoError) {
238 // No error; it must have been unmounted successfully
239 Q_EMIT storageTearDownSuccessful();
240 }
241 m_indexToTearDown = QPersistentModelIndex();
242 }
243 }
244
245 void PlacesPanel::slotRowsInserted(const QModelIndex &parent, int first, int last)
246 {
247 for (int i = first; i <= last; ++i) {
248 connectDeviceSignals(model()->index(first, 0, parent));
249 }
250 }
251
252 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
253 {
254 auto *placesModel = static_cast<KFilePlacesModel *>(model());
255
256 for (int i = first; i <= last; ++i) {
257 const QModelIndex index = placesModel->index(i, 0, parent);
258
259 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
260 if (!storageAccess) {
261 continue;
262 }
263
264 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, nullptr);
265 }
266 }
267
268 void PlacesPanel::connectDeviceSignals(const QModelIndex &index)
269 {
270 auto *placesModel = static_cast<KFilePlacesModel *>(model());
271
272 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
273 if (!storageAccess) {
274 return;
275 }
276
277 connect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
278 }