MauiKit File Browsing

placeslist.cpp
1/*
2 * <one line to give the program's name and a brief idea of what it does.>
3 * Copyright (C) 2018 camilo <email>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "placeslist.h"
20#include "tagging.h"
21
22#include <QDir>
23#include <QDebug>
24#include <QIcon>
25#include <QSettings>
26
27#ifdef KIO_AVAILABLE
28#include <KFilePlacesModel>
29#include <Solid/Device>
30#else
31#include <MauiKit4/Core/appsettings.h>
32#endif
33
34#include <KLocalizedString>
35
36#ifdef KIO_AVAILABLE
37int mapPathType(const FMStatic::PATHTYPE_KEY& value)
38{
39 switch(value)
40 {
41 case FMStatic::PLACES_PATH: return KFilePlacesModel::GroupType::PlacesType;
42 case FMStatic::REMOTE_PATH: return KFilePlacesModel::GroupType::RemoteType;
43 case FMStatic::DRIVES_PATH: return KFilePlacesModel::GroupType::DevicesType;
44 case FMStatic::REMOVABLE_PATH: return KFilePlacesModel::GroupType::RemovableDevicesType;
45 case FMStatic::TAGS_PATH: return KFilePlacesModel::GroupType::TagsType;
46 case FMStatic::UNKNOWN_TYPE: return KFilePlacesModel::GroupType::UnknownType;
47 default: return value;
48 }
49}
50#endif
51
52
53#ifdef KIO_AVAILABLE
54PlacesList::PlacesList(QObject *parent)
55 : MauiList(parent)
56 , model(new KFilePlacesModel(this))
57#else
59 : MauiList(parent)
60 , model(nullptr)
61#endif
62{
63#ifdef KIO_AVAILABLE
64 connect(this->model, &KFilePlacesModel::reloaded, this, &PlacesList::setList);
65
66 connect(this->model, &KFilePlacesModel::setupDone, this, &PlacesList::setList);
67
68 connect(this->model, &KFilePlacesModel::rowsInserted, [this](const QModelIndex, int, int)
69 {
70 this->setList();
71 Q_EMIT this->bookmarksChanged();
72
73 /*Q_EMIT this->preListChanged();
74 *
75 * for (int i = first; i <= last; i++)
76 * {
77 * const QModelIndex index = model->index(i, 0);
78 *
79 * if(this->groups.contains(model->groupType(index)))
80 * {
81 * this->list << getGroup(*this->model, static_cast<FMH::PATHTYPE_KEY>(model->groupType(index)));
82 }
83 }
84 Q_EMIT this->postListChanged(); */
85 }); // TODO improve the usage of the model
86#else
87 connect(&AppSettings::global(), &AppSettings::settingChanged, [this](const QUrl, const QString &key, const QVariant, const QString &group) {
88 if (key == QStringLiteral("BOOKMARKS") && group == QStringLiteral("PREFERENCES")) {
89 this->setList();
90 Q_EMIT this->bookmarksChanged();
91 }
92 });
93#endif
94}
95
96void PlacesList::componentComplete()
97{
98 connect(this, &PlacesList::groupsChanged, this, &PlacesList::setList);
99 this->setList();
100}
101
102const FMH::MODEL_LIST &PlacesList::items() const
103{
104 return this->list;
105}
106
107FMH::MODEL_LIST PlacesList::getGroup(const KFilePlacesModel &model, const FMStatic::PATHTYPE_KEY &type)
108{
109 FMH::MODEL_LIST res;
110
112 {
113 res << FMH::MODEL {{FMH::MODEL_KEY::PATH, FMStatic::PATHTYPE_URI[FMStatic::PATHTYPE_KEY::TAGS_PATH] + QStringLiteral("fav")}, {FMH::MODEL_KEY::ICON, QStringLiteral("love")}, {FMH::MODEL_KEY::LABEL, i18n("Favorite")}, {FMH::MODEL_KEY::TYPE, QStringLiteral("Quick")}};
114
115 // #ifdef KIO_AVAILABLE
116 // res << FMH::MODEL {{FMH::MODEL_KEY::PATH, "recentdocuments:///"}, {FMH::MODEL_KEY::ICON, "view-media-recent"}, {FMH::MODEL_KEY::LABEL, "Recent"}, {FMH::MODEL_KEY::TYPE, "Quick"}};
117 // #endif
118
119 res << FMH::MODEL {{FMH::MODEL_KEY::PATH, QStringLiteral("tags:///")}, {FMH::MODEL_KEY::ICON, QStringLiteral("tag")}, {FMH::MODEL_KEY::LABEL, i18n("Tags")}, {FMH::MODEL_KEY::TYPE, QStringLiteral("Quick")}};
120
121 return res;
122 }
123
125 {
127 return res;
128 }
129
130#ifdef KIO_AVAILABLE
131 auto mappedType = mapPathType(type);
132
133 const auto group = model.groupIndexes(static_cast<KFilePlacesModel::GroupType>(type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH ? mapPathType(FMStatic::PATHTYPE_KEY::PLACES_PATH) : mappedType));
134
135 res << std::accumulate(group.constBegin(), group.constEnd(), FMH::MODEL_LIST(), [&model, &type, this](FMH::MODEL_LIST &list, const QModelIndex &index) -> FMH::MODEL_LIST
136 {
137 const QUrl url = model.url(index);
138 if (type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH && FMStatic::defaultPaths.contains(url.toString()))
139 return list;
140
141 auto data = FMH::MODEL {{FMH::MODEL_KEY::PATH, url.toString()},
142 {FMH::MODEL_KEY::URL, url.toString()},
143 {FMH::MODEL_KEY::ICON, model.icon(index).name()},
144 {FMH::MODEL_KEY::LABEL, model.text(index)},
145 {FMH::MODEL_KEY::NAME, model.text(index)},
146 {FMH::MODEL_KEY::TYPE, type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH ? FMStatic::PathTypeLabel(FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH) : FMStatic::PathTypeLabel(type)}};
147
148 if(model.isDevice(index))
149 {
150 const auto udi = model.deviceForIndex(index).udi();
151 qDebug() << "DEVICE" << udi;
152
153 data.insert(FMH::MODEL_KEY::UDI, udi);
154 m_devices.insert(udi, index);
155 }
156
157 list << data;
158 return list;
159 });
160
161#else
162 Q_UNUSED(model)
163 switch (type) {
165 {
166 auto bookmarks = AppSettings::global().load(QStringLiteral("BOOKMARKS"), QStringLiteral("PREFERENCES"), {}).toStringList();
167
169
170 break;
171 }
173 res = FMStatic::getDevices();
174 break;
175 default:
176 break;
177 }
178
179#endif
180
181 return res;
182}
183
184void PlacesList::setList()
185{
186 this->list.clear();
187
188 qDebug() << "Setting PlacesList model" << groups;
189 Q_EMIT this->preListChanged();
190
191 if (!this->groups.isEmpty())
192 {
193 for (const auto &group : std::as_const(this->groups))
194 {
195 switch (group.toInt()) {
197 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::PLACES_PATH);
198 break;
199
201 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH);
202 break;
203
205 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::QUICK_PATH);
206 break;
207
209 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::DRIVES_PATH);
210 break;
211
213 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::REMOTE_PATH);
214 break;
215
217 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::REMOVABLE_PATH);
218 break;
219
221 this->list << Tagging::getInstance()->getTags();
222 break;
223 }
224 }
225 }
226
227 Q_EMIT this->postListChanged();
228 Q_EMIT this->countChanged();
229}
230
231QVariantList PlacesList::getGroups() const
232{
233 return this->groups;
234}
235
236void PlacesList::setGroups(const QVariantList &value)
237{
238 if (this->groups == value)
239 return;
240
241 this->groups = value;
242 Q_EMIT this->groupsChanged();
243}
244
245void PlacesList::removePlace(const int &index)
246{
247 if (index >= this->list.size() || index < 0)
248 return;
249
250#ifdef KIO_AVAILABLE
251 Q_EMIT this->preItemRemoved(index);
252 this->model->removePlace(this->model->closestItem(QUrl(this->list.at(index)[FMH::MODEL_KEY::PATH])));
253 this->list.removeAt(index);
254 Q_EMIT this->postItemRemoved();
255#else
256 auto bookmarks = AppSettings::global().load(QStringLiteral("BOOKMARKS"), QStringLiteral("PREFERENCES"), {}).toStringList();
257 bookmarks.removeOne(this->list.at(index)[FMH::MODEL_KEY::PATH]);
258 AppSettings::global().save(QStringLiteral("BOOKMARKS"), bookmarks, QStringLiteral("PREFERENCES"));
259#endif
261}
262
263bool PlacesList::contains(const QUrl &path)
264{
265 return this->exists(FMH::MODEL_KEY::PATH, path.toString());
266}
267
268bool PlacesList::isDevice(const int &index)
269{
270 if (index >= this->list.size() || index < 0)
271 return false;
272
273#ifdef KIO_AVAILABLE
274 const auto item = this->list.at(index);
275 return m_devices.contains(item[FMH::MODEL_KEY::UDI]);
276#endif
277
278 return false;
279}
280
281bool PlacesList::setupNeeded(const int &index)
282{
283 if (index >= this->list.size() || index < 0)
284 return false;
285
286#ifdef KIO_AVAILABLE
287 const auto item = this->list.at(index);
288 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
289 {
290 return this->model->setupNeeded(m_devices.value(item[FMH::MODEL_KEY::UDI]));
291 }
292#endif
293
294 return false;
295}
296
297void PlacesList::requestEject(const int &index)
298{
299 if (index >= this->list.size() || index < 0)
300 return;
301
302#ifdef KIO_AVAILABLE
303 const auto item = this->list.at(index);
304 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
305 {
306 this->model->requestEject(m_devices.value(item[FMH::MODEL_KEY::UDI]));
307 }
308#endif
309}
310
311void PlacesList::requestSetup(const int &index)
312{
313 if (index >= this->list.size() || index < 0)
314 return;
315
316#ifdef KIO_AVAILABLE
317 const auto item = this->list.at(index);
318 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
319 {
320 this->model->requestSetup(m_devices.value(item[FMH::MODEL_KEY::UDI]));
321 }
322#endif
323}
324
326{
327#ifdef KIO_AVAILABLE
328 KFilePlacesModel model;
329 model.addPlace(QDir(url.toLocalFile()).dirName(), url, FMStatic::getIconName(url));
330#else
331 // do android stuff until cmake works with android
333 return;
334
335 auto bookmarks = AppSettings::global().load(QStringLiteral("BOOKMARKS"), QStringLiteral("PREFERENCES"), {}).toStringList();
336 if(bookmarks.contains(url.toString()))
337 return;
338
339 bookmarks << url.toString();
340 AppSettings::global().save(QStringLiteral("BOOKMARKS"), bookmarks, QStringLiteral("PREFERENCES"));
341#endif
342}
343
344int PlacesList::indexOfPath(const QUrl &url) const
345{
346 return this->indexOf(FMH::MODEL_KEY::PATH, url.toString());
347}
348
349void PlacesList::toggleSection(const int& section)
350{
351 if(this->groups.contains(section))
352 {
353 this->groups.removeAll(section);
354 }else
355 {
356 this->groups.append(section);
357 }
358
359 Q_EMIT this->groupsChanged();
360}
361
362bool PlacesList::containsGroup(const int& group)
363{
364 return this->groups.contains(group);
365}
366
QVariant load(const QString &key, const QString &group, const QVariant &defaultValue) const
static AppSettings & global()
void save(const QString &key, const QVariant &value, const QString &group)
void settingChanged(QUrl url, QString key, QVariant value, QString group)
static const QString getIconName(const QUrl &path)
Returns the icon name for certain file.
Definition fmstatic.cpp:618
static FMH::MODEL_LIST packItems(const QStringList &items, const QString &type)
Given a list of path URLs pack all the info of such files as a FMH::MODEL_LIST.
Definition fmstatic.cpp:57
static FMH::MODEL_LIST getDevices()
Devices mounted in the file system.
Definition fmstatic.cpp:110
static const QHash< PATHTYPE_KEY, QString > PATHTYPE_URI
Similar to PATHTYPE_SCHEME, but mapped with the complete scheme.
Definition fmstatic.h:369
static QString PathTypeLabel(const FMStatic::PATHTYPE_KEY &key)
Given a PATHTYPE_KEY return a user friendly string.
Definition fmstatic.cpp:31
static FMH::MODEL_LIST getDefaultPaths()
A model list of the default paths in most systems, such as Home, Pictures, Video, Downloads,...
Definition fmstatic.cpp:74
static bool isDefaultPath(const QString &path)
Checks if a given path URL is a default path as found in the defaultPaths method.
Definition fmstatic.cpp:122
PATHTYPE_KEY
The different location types supported. Most of them need of KDE KIO framework to be fully operationa...
Definition fmstatic.h:252
@ REMOTE_PATH
Remote locations, such as servers accessed via SSH or FTP.
Definition fmstatic.h:261
@ UNKNOWN_TYPE
Unknown location type.
Definition fmstatic.h:281
@ TAGS_PATH
A tag location.
Definition fmstatic.h:276
@ DRIVES_PATH
Hard drives locations.
Definition fmstatic.h:266
@ BOOKMARKS_PATH
A bookmarked location.
Definition fmstatic.h:321
@ REMOVABLE_PATH
Removable places, such as optic CDs, USB pen drives, etc.
Definition fmstatic.h:271
@ QUICK_PATH
The common standard paths.
Definition fmstatic.h:316
@ PLACES_PATH
Local paths, such as the Downloads, Pictures, etc.
Definition fmstatic.h:256
Q_INVOKABLE bool isDevice(const QModelIndex &index) const
Q_INVOKABLE void requestSetup(const QModelIndex &index)
Q_INVOKABLE bool setupNeeded(const QModelIndex &index) const
Q_INVOKABLE void removePlace(const QModelIndex &index) const
Q_INVOKABLE void addPlace(const QString &text, const QUrl &url, const QString &iconName, const QString &appName, const QModelIndex &after)
void setupDone(const QModelIndex &index, bool success)
QModelIndex closestItem(const QUrl &url) const
Q_INVOKABLE void requestEject(const QModelIndex &index)
Q_INVOKABLE QModelIndexList groupIndexes(const GroupType type) const
void countChanged()
void preItemRemoved(int index)
void preListChanged()
void postListChanged()
void postItemRemoved()
int indexOf(const FMH::MODEL_KEY &key, const QString &value) const
bool exists(const FMH::MODEL_KEY &key, const QString &value) const
The list model of the system locations, such as bookmarks, standard places, networks and devices.
Definition placeslist.h:40
static void addBookmark(const QUrl &url)
Add a location to the bookmarks sections.
bool setupNeeded(const int &index)
Check if a device type entry needs to be setup, as in mounted.
bool isDevice(const int &index)
Check if a entry at the given index is a device.
void requestEject(const int &index)
Request to eject a removable device type at the given index.
bool contains(const QUrl &path)
Checks of a file URL exists in the places model.
void toggleSection(const int &section)
Hide/show a section.
int indexOfPath(const QUrl &url) const
Given an URL path, if it exists in the places list return its index position.
void removePlace(const int &index)
Removes a place from the model and if the data at the given index is a file URL bookmark then it gets...
QML_ELEMENTQVariantList groups
The groups to be listed.
Definition placeslist.h:53
void requestSetup(const int &index)
Request to setup or mount the device type entry at the given index.
bool containsGroup(const int &group)
Whether the current listing contains a group type.
void bookmarksChanged()
Emitted when a new bookmark entry has been added.
FMH::MODEL_LIST getTags(const int &limit=5)
Get all the tags available with detailed information packaged as a FMH::MODEL_LIST.
Definition tagging.cpp:374
static Tagging * getInstance()
Returns an instance to the tagging object.
Definition tagging.cpp:70
QString i18n(const char *text, const TYPE &arg...)
void rowsInserted(const QModelIndex &parent, int first, int last)
QString dirName() const const
bool contains(const Key &key) const const
T value(const Key &key) const const
const_reference at(qsizetype i) const const
void clear()
void removeAt(qsizetype i)
qsizetype size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
const_iterator constBegin() const const
const_iterator constEnd() const const
int toInt(bool *ok, int base) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.