Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* A library for Qt app
*
* LICENSE: The GNU Lesser General Public License, version 3.0
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0
* @link https://github.com/akiraohgaki/qtlibs
*/
#include "ocsapi.h"
#include <QXmlStreamReader>
#include "json.h"
#include "networkresource.h"
namespace qtlibs {
// OCS-API Specification
// https://www.freedesktop.org/wiki/Specifications/open-collaboration-services/
OcsApi::OcsApi(const QString &id, const QUrl &baseUrl, const QString &userName, const QString &password, QObject *parent)
: QObject(parent), id_(id), baseUrl_(baseUrl), userName_(userName), password_(password)
{}
OcsApi::OcsApi(const OcsApi &other, QObject *parent)
: QObject(parent)
{
setId(other.id());
setBaseUrl(other.baseUrl());
setUserName(other.userName());
setPassword(other.password());
}
OcsApi &OcsApi::operator =(const OcsApi &other)
{
setId(other.id());
setBaseUrl(other.baseUrl());
setUserName(other.userName());
setPassword(other.password());
return *this;
}
QString OcsApi::id() const
{
return id_;
}
void OcsApi::setId(const QString &id)
{
id_ = id;
}
QUrl OcsApi::baseUrl() const
{
return baseUrl_;
}
void OcsApi::setBaseUrl(const QUrl &baseUrl)
{
baseUrl_ = baseUrl;
}
QString OcsApi::userName() const
{
return userName_;
}
void OcsApi::setUserName(const QString &userName)
{
userName_ = userName;
}
QString OcsApi::password() const
{
return password_;
}
void OcsApi::setPassword(const QString &password)
{
password_ = password;
}
QJsonObject OcsApi::getConfig()
{
QUrl url = baseUrl().resolved(QUrl("config"));
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::checkPerson()
{
QUrl url = baseUrl().resolved(QUrl("person/check"));
QUrlQuery formData;
formData.addQueryItem("login", userName());
formData.addQueryItem("password", password());
formData.addQueryItem("format", "json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.post(formData)->readData()).toObject();
}
QJsonObject OcsApi::getPersonDataSet(const QUrlQuery &query)
{
QUrl url = baseUrl().resolved(QUrl("person/data"));
url.setUserName(userName());
url.setPassword(password());
QUrlQuery newQuery(query);
newQuery.removeQueryItem("format");
newQuery.addQueryItem("format", "json");
url.setQuery(newQuery);
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getPersonData(const QString &personId)
{
QUrl url = baseUrl().resolved(QUrl("person/data/" + personId));
url.setUserName(userName());
url.setPassword(password());
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getPersonSelf()
{
QUrl url = baseUrl().resolved(QUrl("person/self"));
url.setUserName(userName());
url.setPassword(password());
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentCategories()
{
QUrl url = baseUrl().resolved(QUrl("content/categories"));
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentDataSet(const QUrlQuery &query)
{
QUrl url = baseUrl().resolved(QUrl("content/data"));
QUrlQuery newQuery(query);
newQuery.removeQueryItem("format");
newQuery.addQueryItem("format", "json");
url.setQuery(newQuery);
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentData(const QString &contentId)
{
QUrl url = baseUrl().resolved(QUrl("content/data/" + contentId));
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentDownload(const QString &contentId, const QString &itemId)
{
QUrl url = baseUrl().resolved(QUrl("content/download/" + contentId + "/" + itemId));
url.setQuery("format=json");
qtlibs::NetworkResource resource(url.toString(), url, false);
return qtlibs::Json(resource.get()->readData()).toObject();
}
QJsonArray OcsApi::getProviderFile(const QUrl &url)
{
QJsonArray providers;
qtlibs::NetworkResource resource(url.toString(), url, false);
QXmlStreamReader reader(resource.get()->readData());
QStringList whitelist;
whitelist << "id" << "location" << "name" << "icon" << "termsofuse" << "register";
while (!reader.atEnd() && !reader.hasError()) {
reader.readNext();
if (reader.isStartElement() && reader.name() == "provider") {
QJsonObject provider;
provider["_providerfile"] = url.toString();
providers.append(provider);
continue;
}
QString elementName = reader.name().toString();
if (!providers.isEmpty() && whitelist.contains(elementName)) {
int i(providers.size() - 1);
QJsonObject provider = providers[i].toObject();
provider[elementName] = reader.readElementText();
providers[i] = provider;
}
}
return providers;
}
} // namespace qtlibs