|
|
|
|
@ -4,21 +4,174 @@
|
|
|
|
|
#include "cgooglefonts.h"
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
|
|
|
|
|
cGoogleFonts::cGoogleFonts()
|
|
|
|
|
{
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
#include <QUrlQuery>
|
|
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QStringList arrayToString(const QJsonArray& array) {
|
|
|
|
|
QStringList list;
|
|
|
|
|
|
|
|
|
|
foreach (const QJsonValue& item, array) {
|
|
|
|
|
list.append(item.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMap<QString, QString> objectToMap(const QJsonObject& object) {
|
|
|
|
|
QMap<QString, QString> list;
|
|
|
|
|
|
|
|
|
|
for (const QString &key : object.keys()) {
|
|
|
|
|
QJsonValue value = object.value(key);
|
|
|
|
|
list.insert(key, value.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cGoogleFonts::~cGoogleFonts()
|
|
|
|
|
{
|
|
|
|
|
cGoogleFonts::cGoogleFonts() {
|
|
|
|
|
m_key = tr("AIzaSyCqPgQFvG-k4lbPF7L0oqJLKJxoC1O6R90");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString cGoogleFonts::name()
|
|
|
|
|
{
|
|
|
|
|
cGoogleFonts::~cGoogleFonts() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString cGoogleFonts::pluginAPIVersion() {
|
|
|
|
|
return tr("1.0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString cGoogleFonts::name() {
|
|
|
|
|
return tr("Google Fonts");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString cGoogleFonts::version()
|
|
|
|
|
{
|
|
|
|
|
QString cGoogleFonts::version() {
|
|
|
|
|
return tr("0.1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<cFont> cGoogleFonts::search(const QString& family, const QString& subset, const Sort& sort) {
|
|
|
|
|
QNetworkAccessManager networkManager;
|
|
|
|
|
QString urlString = "https://www.googleapis.com/webfonts/v1/webfonts";
|
|
|
|
|
QUrlQuery query;
|
|
|
|
|
|
|
|
|
|
query.addQueryItem("key", m_key);
|
|
|
|
|
|
|
|
|
|
if(!family.isEmpty())
|
|
|
|
|
query.addQueryItem("family", family);
|
|
|
|
|
|
|
|
|
|
if(!subset.isEmpty())
|
|
|
|
|
query.addQueryItem("subset", subset);
|
|
|
|
|
|
|
|
|
|
switch(sort)
|
|
|
|
|
{
|
|
|
|
|
case Sort::alpha:
|
|
|
|
|
query.addQueryItem("sort", "alpha");
|
|
|
|
|
break;
|
|
|
|
|
case Sort::date:
|
|
|
|
|
query.addQueryItem("sort", "date");
|
|
|
|
|
break;
|
|
|
|
|
case Sort::popularity:
|
|
|
|
|
query.addQueryItem("sort", "popularity");
|
|
|
|
|
break;
|
|
|
|
|
case Sort::style:
|
|
|
|
|
query.addQueryItem("sort", "style");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
query.addQueryItem("sort", "alpha");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QUrl url(urlString);
|
|
|
|
|
url.setQuery(query);
|
|
|
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
|
|
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
|
|
QNetworkReply* reply = networkManager.get(request);
|
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
|
delete reply;
|
|
|
|
|
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data);
|
|
|
|
|
if(doc.isNull()) {
|
|
|
|
|
qDebug() << "Error: JSON return is empty";
|
|
|
|
|
return QList<cFont>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!doc.isObject()) {
|
|
|
|
|
qDebug() << "Error: invalid JSON";
|
|
|
|
|
return QList<cFont>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonObject root = doc.object();
|
|
|
|
|
|
|
|
|
|
if(!root.contains("items")) {
|
|
|
|
|
qDebug() << "Error: no fonts found";
|
|
|
|
|
return QList<cFont>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonValue itemsV = root.value("items");
|
|
|
|
|
if(!itemsV.isArray()) {
|
|
|
|
|
qDebug() << "Error: no fonts found";
|
|
|
|
|
return QList<cFont>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray items = itemsV.toArray();
|
|
|
|
|
|
|
|
|
|
if(!items.count())
|
|
|
|
|
return QList<cFont>();
|
|
|
|
|
|
|
|
|
|
QList<cFont> fontList;
|
|
|
|
|
|
|
|
|
|
foreach (const QJsonValue& item, items) {
|
|
|
|
|
if (item.isObject()) {
|
|
|
|
|
cFont font;
|
|
|
|
|
QJsonObject obj = item.toObject();
|
|
|
|
|
|
|
|
|
|
if(obj.contains("family"))
|
|
|
|
|
font.setFamily(obj.value("family").toString());
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if(obj.contains("variants"))
|
|
|
|
|
font.setVariants(arrayToString(obj.value("variants").toArray()));
|
|
|
|
|
|
|
|
|
|
if(obj.contains("subsets"))
|
|
|
|
|
font.setSubsets(arrayToString(obj.value("subsets").toArray()));
|
|
|
|
|
|
|
|
|
|
if(obj.contains("version"))
|
|
|
|
|
font.setVersion(obj.value("version").toString());
|
|
|
|
|
|
|
|
|
|
if(obj.contains("lastModified"))
|
|
|
|
|
font.setLastModified(QDate::fromString(obj.value("lastModified").toString()));
|
|
|
|
|
|
|
|
|
|
if(obj.contains("files")) {
|
|
|
|
|
QJsonObject filesObj = obj.value("files").toObject();
|
|
|
|
|
|
|
|
|
|
font.setFiles(objectToMap(filesObj));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(obj.contains("category"))
|
|
|
|
|
font.setCategory(obj.value("category").toString());
|
|
|
|
|
|
|
|
|
|
if(obj.contains("kind"))
|
|
|
|
|
font.setKind(obj.value("kind").toString());
|
|
|
|
|
|
|
|
|
|
fontList.append(font);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fontList;
|
|
|
|
|
}
|
|
|
|
|
|