-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourcemanager.cpp
More file actions
130 lines (110 loc) · 4 KB
/
Copy pathsourcemanager.cpp
File metadata and controls
130 lines (110 loc) · 4 KB
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
#include "sourcemanager.h"
#include "database.h"
#include <QNetworkReply>
#include <QDebug>
SourceManager::SourceManager(QObject *parent) : QObject(parent)
{
manager = new QNetworkAccessManager(this);
}
/**
* @brief SourceManager::getSourceData
* @param QString `url` the url of the source. It must be in JSON format.
*/
void SourceManager::getSourceData(const QString &url) {
QNetworkRequest request((QUrl(url)));
QNetworkReply *reply = manager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
QString responseData = reply->readAll();
getSourcesInfo(responseData);
// STEP 1: extract sources info
database db;
int newId = db.insertSourceInfos(source_name, source_url, version);
// STEP 2: add source infos to database
if (newId != -1) {
// STEP 3: uniquely if the step 2 is a success, we parse the musics array
m_musicData = parseJson(responseData);
// STEP 4: we add the musics
addToDatabase(m_musicData, newId);
emit ready();
} else {
qDebug() << "Error: unable to add the musics";
}
}
reply->deleteLater();
});
}
/**
* Extract the ` musics ` JSON array from the response.
* @brief SourceManager::parseJson
* @param ` QString data ` the JSON downloaded previuosly by getSourceData void function.
* @return QJsonArray
*/
QJsonArray SourceManager::parseJson(const QString &data) {
// 1. Convert String to JSON doc
QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
// 2. Verify if JSON is valid
if (doc.isNull()) {
qDebug() << "Le JSON est invalide !";
return QJsonArray();
}
qDebug() << "JSON valide";
// 3. Access to root object
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj.contains("musics")) {
if (obj["musics"].isArray()) {
QJsonArray musics = obj["musics"].toArray();
return musics;
} else {
return QJsonArray();
}
} else {
return QJsonArray();
}
}
return QJsonArray();
}
void SourceManager::getSourcesInfo(const QString &json) {
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj["name"].isUndefined() || obj["version"].isUndefined()) {
return;
}
source_name = obj["name"].toString();
source_url = obj["url"].toString();
version = obj["version"].toInt();
}
}
bool SourceManager::addToDatabase(const QJsonArray &data, const int fk_source) {
database db;
for (int i = 0; i < data.size(); ++i) {
QJsonObject music = data[i].toObject();
QString name = music["name"].toString();
qDebug() << "Name: "<< name;
QString artist = music["artist"].toString();
QString album = music["album"].toString();
int year = music["year"].toInt(); // Assure que la clé "year" existe dans `music`
QString url = music["url"].toString();
QString url_cover = music["url_cover"].toString(); // Remplace par la clé correcte si différente
QString lyrics = music["lyrics"].toString(); // Remplace par la clé correcte si différente
QString tags = music["tags"].toString(); // Remplace par la clé correcte si différente
db.addSourceToDatabase(fk_source, name, url, tags, artist, album, year, url_cover, lyrics);
}
return true;
}
/**
* Call every necessary functions to add a source
* @brief SourceManager::addSource
* @param QString url
* @return
* | | |
* |-------|-------|
* | True | Success|
* |False | Failure |
*/
bool SourceManager::addSource(const QString &url) {
getSourceData(url);
return true; // On retourne true pour dire que le processus est lancé
}