]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Merge branch 'master' into kf6
[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 "settings/dolphinsettingsdialog.h"
18 #include "views/draganddrophelper.h"
19
20 #include <KFilePlacesModel>
21 #include <KIO/DropJob>
22 #include <KIO/Job>
23 #include <KLocalizedString>
24 #include <KProtocolManager>
25
26 #include <QIcon>
27 #include <QMenu>
28 #include <QMimeData>
29 #include <QShowEvent>
30
31 #include <Solid/StorageAccess>
32
33 PlacesPanel::PlacesPanel(QWidget *parent)
34 : KFilePlacesView(parent)
35 {
36 setDropOnPlaceEnabled(true);
37 connect(this, &PlacesPanel::urlsDropped, this, &PlacesPanel::slotUrlsDropped);
38
39 setAutoResizeItemsEnabled(false);
40
41 setTeardownFunction([this](const QModelIndex &index) {
42 slotTearDownRequested(index);
43 });
44
45 m_configureTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("configure")), i18nc("@action:inmenu", "Configure Trash…"));
46 m_configureTrashAction->setPriority(QAction::HighPriority);
47 connect(m_configureTrashAction, &QAction::triggered, this, &PlacesPanel::slotConfigureTrash);
48 addAction(m_configureTrashAction);
49
50 connect(this, &PlacesPanel::contextMenuAboutToShow, this, &PlacesPanel::slotContextMenuAboutToShow);
51
52 connect(this, &PlacesPanel::iconSizeChanged, this, [](const QSize &newSize) {
53 int iconSize = qMin(newSize.width(), newSize.height());
54 if (iconSize == 0) {
55 // Don't store 0 size, let's keep -1 for default/small/automatic
56 iconSize = -1;
57 }
58 PlacesPanelSettings *settings = PlacesPanelSettings::self();
59 settings->setIconSize(iconSize);
60 settings->save();
61 });
62 }
63
64 PlacesPanel::~PlacesPanel() = default;
65
66 void PlacesPanel::setUrl(const QUrl &url)
67 {
68 // KFilePlacesView::setUrl no-ops when no model is set but we only set it in showEvent()
69 // Remember the URL and set it in showEvent
70 m_url = url;
71 KFilePlacesView::setUrl(url);
72 }
73
74 QList<QAction *> PlacesPanel::customContextMenuActions() const
75 {
76 return m_customContextMenuActions;
77 }
78
79 void PlacesPanel::setCustomContextMenuActions(const QList<QAction *> &actions)
80 {
81 m_customContextMenuActions = actions;
82 }
83
84 void PlacesPanel::proceedWithTearDown()
85 {
86 if (m_indexToTearDown.isValid()) {
87 auto *placesModel = static_cast<KFilePlacesModel *>(model());
88 placesModel->requestTeardown(m_indexToTearDown);
89 } else {
90 qWarning() << "Places entry to tear down is no longer valid";
91 }
92 }
93
94 void PlacesPanel::readSettings()
95 {
96 if (GeneralSettings::autoExpandFolders()) {
97 setDragAutoActivationDelay(750);
98 } else {
99 setDragAutoActivationDelay(0);
100 }
101
102 const int iconSize = qMax(0, PlacesPanelSettings::iconSize());
103 setIconSize(QSize(iconSize, iconSize));
104 }
105
106 void PlacesPanel::showEvent(QShowEvent *event)
107 {
108 if (!event->spontaneous() && !model()) {
109 readSettings();
110
111 auto *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
112 setModel(placesModel);
113
114 connect(placesModel, &KFilePlacesModel::errorMessage, this, &PlacesPanel::errorMessage);
115 connect(placesModel, &KFilePlacesModel::teardownDone, this, &PlacesPanel::slotTearDownDone);
116
117 connect(placesModel, &QAbstractItemModel::rowsInserted, this, &PlacesPanel::slotRowsInserted);
118 connect(placesModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &PlacesPanel::slotRowsAboutToBeRemoved);
119
120 for (int i = 0; i < model()->rowCount(); ++i) {
121 connectDeviceSignals(model()->index(i, 0, QModelIndex()));
122 }
123
124 setUrl(m_url);
125 }
126
127 KFilePlacesView::showEvent(event);
128 }
129
130 static bool isInternalDrag(const QMimeData *mimeData)
131 {
132 const auto formats = mimeData->formats();
133 for (const auto &format : formats) {
134 // from KFilePlacesModel::_k_internalMimetype
135 if (format.startsWith(QLatin1String("application/x-kfileplacesmodel-"))) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 void PlacesPanel::dragMoveEvent(QDragMoveEvent *event)
143 {
144 const QModelIndex index = indexAt(event->position().toPoint());
145 if (index.isValid()) {
146 auto *placesModel = static_cast<KFilePlacesModel *>(model());
147
148 // Reject drag ontop of a non-writable protocol
149 // We don't know whether we're dropping inbetween or ontop of a place
150 // so still allow internal drag events so that re-arranging still works.
151 const QUrl url = placesModel->url(index);
152 if (url.isValid() && !isInternalDrag(event->mimeData()) && !KProtocolManager::supportsWriting(url)) {
153 event->setDropAction(Qt::IgnoreAction);
154 }
155 }
156
157 KFilePlacesView::dragMoveEvent(event);
158 }
159
160 void PlacesPanel::slotConfigureTrash()
161 {
162 const QUrl url = currentIndex().data(KFilePlacesModel::UrlRole).toUrl();
163
164 DolphinSettingsDialog *settingsDialog = new DolphinSettingsDialog(url, this);
165 settingsDialog->setCurrentPage(settingsDialog->trashSettings);
166 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
167 settingsDialog->show();
168 }
169
170 void PlacesPanel::slotUrlsDropped(const QUrl &dest, QDropEvent *event, QWidget *parent)
171 {
172 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
173 if (job) {
174 connect(job, &KIO::DropJob::result, this, [this](KJob *job) {
175 if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
176 Q_EMIT errorMessage(job->errorString());
177 }
178 });
179 }
180 }
181
182 void PlacesPanel::slotContextMenuAboutToShow(const QModelIndex &index, QMenu *menu)
183 {
184 Q_UNUSED(menu);
185
186 auto *placesModel = static_cast<KFilePlacesModel *>(model());
187 const QUrl url = placesModel->url(index);
188 const Solid::Device device = placesModel->deviceForIndex(index);
189
190 m_configureTrashAction->setVisible(url.scheme() == QLatin1String("trash"));
191
192 // show customContextMenuActions only on the view's context menu
193 if (!url.isValid() && !device.isValid()) {
194 addActions(m_customContextMenuActions);
195 } else {
196 const auto actions = this->actions();
197 for (QAction *action : actions) {
198 if (m_customContextMenuActions.contains(action)) {
199 removeAction(action);
200 }
201 }
202 }
203 }
204
205 void PlacesPanel::slotTearDownRequested(const QModelIndex &index)
206 {
207 auto *placesModel = static_cast<KFilePlacesModel *>(model());
208
209 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
210 if (!storageAccess) {
211 return;
212 }
213
214 m_indexToTearDown = QPersistentModelIndex(index);
215
216 // disconnect the Solid::StorageAccess::teardownRequested
217 // to prevent emitting PlacesPanel::storageTearDownExternallyRequested
218 // after we have emitted PlacesPanel::storageTearDownRequested
219 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
220 Q_EMIT storageTearDownRequested(storageAccess->filePath());
221 }
222
223 void PlacesPanel::slotTearDownRequestedExternally(const QString &udi)
224 {
225 Q_UNUSED(udi);
226 auto *storageAccess = static_cast<Solid::StorageAccess *>(sender());
227
228 Q_EMIT storageTearDownExternallyRequested(storageAccess->filePath());
229 }
230
231 void PlacesPanel::slotTearDownDone(const QModelIndex &index, Solid::ErrorType error, const QVariant &errorData)
232 {
233 Q_UNUSED(errorData); // All error handling is currently done in frameworks.
234
235 if (index == m_indexToTearDown) {
236 if (error == Solid::ErrorType::NoError) {
237 // No error; it must have been unmounted successfully
238 Q_EMIT storageTearDownSuccessful();
239 }
240 m_indexToTearDown = QPersistentModelIndex();
241 }
242 }
243
244 void PlacesPanel::slotRowsInserted(const QModelIndex &parent, int first, int last)
245 {
246 for (int i = first; i <= last; ++i) {
247 connectDeviceSignals(model()->index(first, 0, parent));
248 }
249 }
250
251 void PlacesPanel::slotRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
252 {
253 auto *placesModel = static_cast<KFilePlacesModel *>(model());
254
255 for (int i = first; i <= last; ++i) {
256 const QModelIndex index = placesModel->index(i, 0, parent);
257
258 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
259 if (!storageAccess) {
260 continue;
261 }
262
263 disconnect(storageAccess, &Solid::StorageAccess::teardownRequested, this, nullptr);
264 }
265 }
266
267 void PlacesPanel::connectDeviceSignals(const QModelIndex &index)
268 {
269 auto *placesModel = static_cast<KFilePlacesModel *>(model());
270
271 Solid::StorageAccess *storageAccess = placesModel->deviceForIndex(index).as<Solid::StorageAccess>();
272 if (!storageAccess) {
273 return;
274 }
275
276 connect(storageAccess, &Solid::StorageAccess::teardownRequested, this, &PlacesPanel::slotTearDownRequestedExternally);
277 }