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#endif
31
32#include <KLocalizedString>
33
34#ifdef KIO_AVAILABLE
35int mapPathType(const FMStatic::PATHTYPE_KEY& value)
36{
37 switch(value)
38 {
39 case FMStatic::PLACES_PATH: return KFilePlacesModel::GroupType::PlacesType;
40 case FMStatic::REMOTE_PATH: return KFilePlacesModel::GroupType::RemoteType;
41 case FMStatic::DRIVES_PATH: return KFilePlacesModel::GroupType::DevicesType;
42 case FMStatic::REMOVABLE_PATH: return KFilePlacesModel::GroupType::RemovableDevicesType;
43 case FMStatic::TAGS_PATH: return KFilePlacesModel::GroupType::TagsType;
44 case FMStatic::UNKNOWN_TYPE: return KFilePlacesModel::GroupType::UnknownType;
45 default: return value;
46 }
47}
48#endif
49
50
51#ifdef KIO_AVAILABLE
52PlacesList::PlacesList(QObject *parent)
53: MauiList(parent)
54, model(new KFilePlacesModel(this))
55#else
57: MauiList(parent)
58, model(nullptr)
59#endif
60{
61 #ifdef KIO_AVAILABLE
62 connect(this->model, &KFilePlacesModel::reloaded, this, &PlacesList::setList);
63
64 connect(this->model, &KFilePlacesModel::setupDone, this, &PlacesList::setList);
65
66 connect(this->model, &KFilePlacesModel::rowsInserted, [this](const QModelIndex, int, int)
67 {
68 this->setList();
69 Q_EMIT this->bookmarksChanged();
70
71 /*Q_EMIT this->preListChanged();
72 *
73 * for (int i = first; i <= last; i++)
74 * {
75 * const QModelIndex index = model->index(i, 0);
76 *
77 * if(this->groups.contains(model->groupType(index)))
78 * {
79 * this->list << getGroup(*this->model, static_cast<FMH::PATHTYPE_KEY>(model->groupType(index)));
80 }
81 }
82 Q_EMIT this->postListChanged(); */
83 }); // TODO improve the usage of the model
84 #endif
85}
86
87void PlacesList::componentComplete()
88{
89 connect(this, &PlacesList::groupsChanged, this, &PlacesList::setList);
90 this->setList();
91}
92
93const FMH::MODEL_LIST &PlacesList::items() const
94{
95 return this->list;
96}
97
98FMH::MODEL_LIST PlacesList::getGroup(const KFilePlacesModel &model, const FMStatic::PATHTYPE_KEY &type)
99{
100 FMH::MODEL_LIST res;
101
103 {
104 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")}};
105
106 // #ifdef KIO_AVAILABLE
107 // 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"}};
108 // #endif
109
110 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")}};
111
112 return res;
113 }
114
116 {
118 return res;
119 }
120
121 #ifdef KIO_AVAILABLE
122 auto mappedType = mapPathType(type);
123
124 const auto group = model.groupIndexes(static_cast<KFilePlacesModel::GroupType>(type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH ? mapPathType(FMStatic::PATHTYPE_KEY::PLACES_PATH) : mappedType));
125
126 res << std::accumulate(group.constBegin(), group.constEnd(), FMH::MODEL_LIST(), [&model, &type, this](FMH::MODEL_LIST &list, const QModelIndex &index) -> FMH::MODEL_LIST
127 {
128 const QUrl url = model.url(index);
129 if (type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH && FMStatic::defaultPaths.contains(url.toString()))
130 return list;
131
132 auto data = FMH::MODEL {{FMH::MODEL_KEY::PATH, url.toString()},
133 {FMH::MODEL_KEY::URL, url.toString()},
134 {FMH::MODEL_KEY::ICON, model.icon(index).name()},
135 {FMH::MODEL_KEY::LABEL, model.text(index)},
136 {FMH::MODEL_KEY::NAME, model.text(index)},
137 {FMH::MODEL_KEY::TYPE, type == FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH ? FMStatic::PathTypeLabel(FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH) : FMStatic::PathTypeLabel(type)}};
138
139 if(model.isDevice(index))
140 {
141 const auto udi = model.deviceForIndex(index).udi();
142 qDebug() << "DEVICE" << udi;
143
144 data.insert(FMH::MODEL_KEY::UDI, udi);
145 m_devices.insert(udi, index);
146 }
147
148 list << data;
149 return list;
150 });
151
152 #else
153 Q_UNUSED(model)
154 switch (type) {
156 {
157 QSettings settings(QStringLiteral("org.mauikit.filebrowsing")) ;
158 settings.beginGroup(QStringLiteral("PREFERENCES"));
159 auto bookmarks = settings.value(QStringLiteral("BOOKMARKS"), {}).toStringList();
160 settings.endGroup();
161
163
164 break;
165 }
167 res = FMStatic::getDevices();
168 break;
169 default:
170 break;
171 }
172
173 #endif
174
175 return res;
176}
177
178void PlacesList::setList()
179{
180 this->list.clear();
181
182 qDebug() << "Setting PlacesList model" << groups;
183 Q_EMIT this->preListChanged();
184
185 if (!this->groups.isEmpty())
186 {
187 for (const auto &group : qAsConst(this->groups))
188 {
189 switch (group.toInt()) {
191 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::PLACES_PATH);
192 break;
193
195 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::BOOKMARKS_PATH);
196 break;
197
199 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::QUICK_PATH);
200 break;
201
203 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::DRIVES_PATH);
204 break;
205
207 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::REMOTE_PATH);
208 break;
209
211 this->list << getGroup(*this->model, FMStatic::PATHTYPE_KEY::REMOVABLE_PATH);
212 break;
213
215 this->list << Tagging::getInstance()->getTags();
216 break;
217 }
218 }
219 }
220
221 Q_EMIT this->postListChanged();
222 Q_EMIT this->countChanged();
223}
224
225QVariantList PlacesList::getGroups() const
226{
227 return this->groups;
228}
229
230void PlacesList::setGroups(const QVariantList &value)
231{
232 if (this->groups == value)
233 return;
234
235 this->groups = value;
236 Q_EMIT this->groupsChanged();
237}
238
239void PlacesList::removePlace(const int &index)
240{
241 if (index >= this->list.size() || index < 0)
242 return;
243
244 #ifdef KIO_AVAILABLE
245 Q_EMIT this->preItemRemoved(index);
246 this->model->removePlace(this->model->closestItem(QUrl(this->list.at(index)[FMH::MODEL_KEY::PATH])));
247 this->list.removeAt(index);
248 Q_EMIT this->postItemRemoved();
249 #else
250 QSettings settings (QStringLiteral("org.mauikit.filebrowsing"));
251 settings.beginGroup(QStringLiteral("PREFERENCES"));
252 auto bookmarks = settings.value(QStringLiteral("BOOKMARKS"), {}).toStringList();
253 bookmarks.removeOne(this->list.at(index)[FMH::MODEL_KEY::PATH]);
254 settings.setValue(QStringLiteral("BOOKMARKS"), bookmarks);
255 settings.endGroup();
256 #endif
258}
259
260bool PlacesList::contains(const QUrl &path)
261{
262 return this->exists(FMH::MODEL_KEY::PATH, path.toString());
263}
264
265bool PlacesList::isDevice(const int &index)
266{
267 if (index >= this->list.size() || index < 0)
268 return false;
269
270 #ifdef KIO_AVAILABLE
271 const auto item = this->list.at(index);
272 return m_devices.contains(item[FMH::MODEL_KEY::UDI]);
273 #endif
274
275 return false;
276}
277
278bool PlacesList::setupNeeded(const int &index)
279{
280 if (index >= this->list.size() || index < 0)
281 return false;
282
283 #ifdef KIO_AVAILABLE
284 const auto item = this->list.at(index);
285 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
286 {
287 return this->model->setupNeeded(m_devices.value(item[FMH::MODEL_KEY::UDI]));
288 }
289 #endif
290
291 return false;
292}
293
294void PlacesList::requestEject(const int &index)
295{
296 if (index >= this->list.size() || index < 0)
297 return;
298
299 #ifdef KIO_AVAILABLE
300 const auto item = this->list.at(index);
301 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
302 {
303 this->model->requestEject(m_devices.value(item[FMH::MODEL_KEY::UDI]));
304 }
305 #endif
306}
307
308void PlacesList::requestSetup(const int &index)
309{
310 if (index >= this->list.size() || index < 0)
311 return;
312
313 #ifdef KIO_AVAILABLE
314 const auto item = this->list.at(index);
315 if(m_devices.contains(item[FMH::MODEL_KEY::UDI]))
316 {
317 this->model->requestSetup(m_devices.value(item[FMH::MODEL_KEY::UDI]));
318 }
319 #endif
320}
321
323{
324 #ifdef KIO_AVAILABLE
325 KFilePlacesModel model;
326 model.addPlace(QDir(url.toLocalFile()).dirName(), url, FMStatic::getIconName(url));
327 #else
328 // do android stuff until cmake works with android
330 return;
331
332 QSettings settings(QStringLiteral("org.mauikit.filebrowsing")) ;
333 settings.beginGroup(QStringLiteral("PREFERENCES"));
334 auto bookmarks = settings.value(QStringLiteral("BOOKMARKS"), {}).toStringList();
335 bookmarks << url.toString();
336 settings.setValue(QStringLiteral("BOOKMARKS"), bookmarks);
337 settings.endGroup();
338 #endif
339}
340
341int PlacesList::indexOfPath(const QUrl &url) const
342{
343 return this->indexOf(FMH::MODEL_KEY::PATH, url.toString());
344}
345
346void PlacesList::toggleSection(const int& section)
347{
348 if(this->groups.contains(section))
349 {
350 this->groups.removeAll(section);
351 }else
352 {
353 this->groups.append(section);
354 }
355
356 Q_EMIT this->groupsChanged();
357}
358
359bool PlacesList::containsGroup(const int& group)
360{
361 return this->groups.contains(group);
362}
363
static const QString getIconName(const QUrl &path)
Returns the icon name for certain file.
Definition fmstatic.cpp:617
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:375
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:258
@ REMOTE_PATH
Remote locations, such as servers accessed via SSH or FTP.
Definition fmstatic.h:267
@ UNKNOWN_TYPE
Unknown location type.
Definition fmstatic.h:287
@ TAGS_PATH
A tag location.
Definition fmstatic.h:282
@ DRIVES_PATH
Hard drives locations.
Definition fmstatic.h:272
@ BOOKMARKS_PATH
A bookmarked location.
Definition fmstatic.h:327
@ REMOVABLE_PATH
Removable places, such as optic CDs, USB pen drives, etc.
Definition fmstatic.h:277
@ QUICK_PATH
The common standard paths.
Definition fmstatic.h:322
@ PLACES_PATH
Local paths, such as the Downloads, Pictures, etc.
Definition fmstatic.h:262
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
Solid::Device deviceForIndex(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:44
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:57
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.
QString udi() const
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
iterator insert(const Key &key, const T &value)
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)
void beginGroup(QAnyStringView prefix)
void endGroup()
void setValue(QAnyStringView key, const QVariant &value)
QVariant value(QAnyStringView key) const const
QString & insert(qsizetype position, QChar ch)
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 May 17 2024 11:51:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.