Kgapi

app.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Andrius da Costa Ribas <andriusmao@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "app.h"
8#include "utils_p.h"
9
10#include <QJsonDocument>
11#include <QVariantMap>
12
13using namespace KGAPI2;
14using namespace KGAPI2::Drive;
15
16class Q_DECL_HIDDEN App::Icon::Private
17{
18public:
19 Private();
20 Private(const Private &other);
21
22 Category category;
23 int size = -1;
24 QUrl iconUrl;
25
26 static Category categoryFromName(const QString &categoryName);
27};
28
29App::Icon::Private::Private()
30{
31}
32
33App::Icon::Private::Private(const Private &other)
34 : category(other.category)
35 , size(other.size)
36 , iconUrl(other.iconUrl)
37{
38}
39
40App::Icon::Category App::Icon::Private::categoryFromName(const QString &categoryName)
41{
42 if (categoryName == QLatin1StringView("application")) {
43 return App::Icon::ApplicationCategory;
44 } else if (categoryName == QLatin1StringView("document")) {
45 return App::Icon::DocumentCategory;
46 } else if (categoryName == QLatin1StringView("documentShared")) {
47 return App::Icon::DocumentSharedCategory;
48 } else {
49 return App::Icon::UndefinedCategory;
50 }
51}
52
53App::Icon::Icon()
54 : d(new Private)
55{
56}
57
58App::Icon::Icon(const App::Icon &other)
59 : d(new Private(*(other.d)))
60{
61}
62
63App::Icon::~Icon()
64{
65 delete d;
66}
67
68bool App::Icon::operator==(const Icon &other) const
69{
70 GAPI_COMPARE(category)
71 GAPI_COMPARE(size)
72 GAPI_COMPARE(iconUrl)
73 return true;
74}
75
76App::Icon::Category App::Icon::category() const
77{
78 return d->category;
79}
80
81int App::Icon::size() const
82{
83 return d->size;
84}
85
86QUrl App::Icon::iconUrl() const
87{
88 return d->iconUrl;
89}
90
91///// DriveApp
92
93class Q_DECL_HIDDEN App::Private
94{
95public:
96 Private();
97 Private(const Private &other);
98
99 QString id;
101 QString objectType;
102 bool supportsCreate;
103 bool supportsImport;
104 bool installed;
105 bool authorized;
106 bool useByDefault;
107 QUrl productUrl;
108 QStringList primaryMimeTypes;
109 QStringList secondaryMimeTypes;
110 QStringList primaryFileExtensions;
111 QStringList secondaryFileExtensions;
112 IconsList icons;
113
114 static AppPtr fromJSON(const QVariantMap &map);
115};
116
117App::Private::Private()
118 : supportsCreate(false)
119 , supportsImport(false)
120 , installed(false)
121 , authorized(false)
122 , useByDefault(false)
123{
124}
125
126App::Private::Private(const App::Private &other)
127 : id(other.id)
128 , name(other.name)
129 , objectType(other.objectType)
130 , supportsCreate(other.supportsCreate)
131 , supportsImport(other.supportsImport)
132 , installed(other.installed)
133 , authorized(other.authorized)
134 , useByDefault(other.useByDefault)
135 , productUrl(other.productUrl)
136 , primaryMimeTypes(other.primaryMimeTypes)
137 , secondaryMimeTypes(other.secondaryMimeTypes)
138 , primaryFileExtensions(other.primaryFileExtensions)
139 , secondaryFileExtensions(other.secondaryFileExtensions)
140 , icons(other.icons)
141{
142}
143
144AppPtr App::Private::fromJSON(const QVariantMap &map)
145{
146 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#app")) {
147 return AppPtr();
148 }
149
150 AppPtr app(new App);
151 app->setEtag(map[QStringLiteral("etag")].toString());
152 app->d->id = map[QStringLiteral("id")].toString();
153 app->d->name = map[QStringLiteral("map")].toString();
154 app->d->objectType = map[QStringLiteral("objectType")].toString();
155 app->d->supportsCreate = map[QStringLiteral("supportsCreate")].toBool();
156 app->d->supportsImport = map[QStringLiteral("supportsImport")].toBool();
157 app->d->installed = map[QStringLiteral("installed")].toBool();
158 app->d->authorized = map[QStringLiteral("authorized")].toBool();
159 app->d->useByDefault = map[QStringLiteral("useByDefault")].toBool();
160 app->d->productUrl = map[QStringLiteral("productUrl")].toUrl();
161 app->d->primaryMimeTypes = map[QStringLiteral("primaryMimeTypes")].toStringList();
162 app->d->secondaryMimeTypes = map[QStringLiteral("secondaryMimeTypes")].toStringList();
163 app->d->primaryFileExtensions = map[QStringLiteral("primaryFileExtensions")].toStringList();
164 app->d->secondaryFileExtensions = map[QStringLiteral("secondaryFileExtensions")].toStringList();
165
166 const QVariantList icons = map[QStringLiteral("icons")].toList();
167 for (const QVariant &i : icons) {
168 const QVariantMap &iconData = i.toMap();
169
170 IconPtr icon(new Icon());
171 icon->d->category = Icon::Private::categoryFromName(iconData[QStringLiteral("category")].toString());
172 icon->d->size = iconData[QStringLiteral("size")].toInt();
173 icon->d->iconUrl = iconData[QStringLiteral("iconUrl")].toUrl();
174
175 app->d->icons << icon;
176 }
177
178 return app;
179}
180
181App::App()
182 : KGAPI2::Object()
183 , d(new Private)
184{
185}
186
187App::App(const App &other)
188 : KGAPI2::Object(other)
189 , d(new Private(*(other.d)))
190{
191}
192
193App::~App()
194{
195 delete d;
196}
197
198bool App::operator==(const App &other) const
199{
200 if (!Object::operator==(other)) {
201 return false;
202 }
203
204 GAPI_COMPARE(id)
205 GAPI_COMPARE(name)
206 GAPI_COMPARE(objectType)
207 GAPI_COMPARE(supportsCreate)
208 GAPI_COMPARE(supportsImport)
209 GAPI_COMPARE(installed)
210 GAPI_COMPARE(authorized)
211 GAPI_COMPARE(useByDefault)
212 GAPI_COMPARE(productUrl)
213 GAPI_COMPARE(primaryMimeTypes)
214 GAPI_COMPARE(secondaryMimeTypes)
215 GAPI_COMPARE(primaryFileExtensions)
216 GAPI_COMPARE(secondaryFileExtensions)
217 GAPI_COMPARE_CONTAINERS(icons)
218 return true;
219}
220
221QString App::id() const
222{
223 return d->id;
224}
225
226QString App::name() const
227{
228 return d->name;
229}
230
231QString App::objectType() const
232{
233 return d->objectType;
234}
235
236bool App::supportsCreate() const
237{
238 return d->supportsCreate;
239}
240
241bool App::supportsImport() const
242{
243 return d->supportsImport;
244}
245
246bool App::installed() const
247{
248 return d->installed;
249}
250
251bool App::authorized() const
252{
253 return d->authorized;
254}
255
256bool App::useByDefault() const
257{
258 return d->useByDefault;
259}
260
261QUrl App::productUrl() const
262{
263 return d->productUrl;
264}
265
266QStringList App::primaryMimeTypes() const
267{
268 return d->primaryMimeTypes;
269}
270
271QStringList App::secondaryMimeTypes() const
272{
273 return d->secondaryMimeTypes;
274}
275
276QStringList App::primaryFileExtensions() const
277{
278 return d->primaryFileExtensions;
279}
280
281QStringList App::secondaryFileExtensions() const
282{
283 return d->secondaryFileExtensions;
284}
285
286App::IconsList App::icons() const
287{
288 return d->icons;
289}
290
291AppPtr App::fromJSON(const QByteArray &jsonData)
292{
293 QJsonDocument document = QJsonDocument::fromJson(jsonData);
294 if (document.isNull()) {
295 return AppPtr();
296 }
297 const QVariant data = document.toVariant();
298 return Private::fromJSON(data.toMap());
299}
300
301AppsList App::fromJSONFeed(const QByteArray &jsonData)
302{
303 QJsonDocument document = QJsonDocument::fromJson(jsonData);
304 if (document.isNull()) {
305 return AppsList();
306 }
307 const QVariant data = document.toVariant();
308 const QVariantMap map = data.toMap();
309 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#appList")) {
310 return AppsList();
311 }
312
314 const QVariantList items = map[QStringLiteral("items")].toList();
315 for (const QVariant &item : items) {
316 const AppPtr app = Private::fromJSON(item.toMap());
317
318 if (!app.isNull()) {
319 list << app;
320 }
321 }
322
323 return list;
324}
Base class for all objects.
Definition object.h:31
char * toString(const EngineQuery &query)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
Category category(StandardShortcut id)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
bool isNull() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:59:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.