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