Debian Codegraph @

src/dlicenseinfo.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dlicenseinfo.h"
6
7 #include <DObjectPrivate>
8
9 #include <QFile>
10 #include <QJsonParseError>
11 #include <QJsonObject>
12 #include <QJsonArray>
13 #include <QDebug>
14
15 DCORE_BEGIN_NAMESPACE
16
17 class DLicenseInfo::DComponentInfoPrivate : public DObjectPrivate
18 {
19 public:
20 QString name;
21 QString version;
22 QString copyRight;
23 QString licenseName;
24
25 protected:
26 explicit DComponentInfoPrivate(DLicenseInfo::DComponentInfo *qq)
27 : DObjectPrivate(qq)
28 {
29 }
30
31 private:
32 Q_DECLARE_PUBLIC(DLicenseInfo::DComponentInfo)
33 friend class DLicenseInfoPrivate;
34 };
35
36 DLicenseInfo::DComponentInfo::DComponentInfo(DObject * parent)
37 : DObject(*new DLicenseInfo::DComponentInfoPrivate(this), parent)
38 {
39
40 }
41
42 DLicenseInfo::DComponentInfo::~DComponentInfo()
43 {
44 }
45
46 QString DLicenseInfo::DComponentInfo::name() const
47 {
48 return d_func()->name;
49 }
50
51 QString DLicenseInfo::DComponentInfo::version() const
52 {
53 return d_func()->version;
54 }
55
56 QString DLicenseInfo::DComponentInfo::copyRight() const
57 {
58 return d_func()->copyRight;
59 }
60
61 QString DLicenseInfo::DComponentInfo::licenseName() const
62 {
63 return d_func()->licenseName;
64 }
65
66 class Q_DECL_HIDDEN DLicenseInfoPrivate : public DObjectPrivate
67 {
68 public:
69 explicit DLicenseInfoPrivate(DLicenseInfo *qq);
70 ~DLicenseInfoPrivate() override;
71
72 bool loadFile(const QString &file);
73 bool loadContent(const QByteArray &content);
74 QByteArray licenseContent(const QString &licenseName);
75 void clear();
76
77 QString licenseSearchPath;
78 DLicenseInfo::DComponentInfos componentInfos;
79 };
80
81 DLicenseInfoPrivate::DLicenseInfoPrivate(DLicenseInfo *qq)
82 : DObjectPrivate(qq)
83 {
84 }
85
86 DLicenseInfoPrivate::~DLicenseInfoPrivate()
87 {
88 clear();
89 }
90
91 bool DLicenseInfoPrivate::loadFile(const QString &file)
92 {
93 QFile jsonFile(file);
94 if (!jsonFile.open(QIODevice::ReadOnly)) {
95 qWarning() << QString("Failed on open file: \"%1\", error message: \"%2\"").arg(
96 qPrintable(jsonFile.fileName()), qPrintable(jsonFile.errorString()));
97 return false;
98 }
99 return loadContent(jsonFile.readAll());
100 }
101
102 bool DLicenseInfoPrivate::loadContent(const QByteArray &content)
103 {
104 QJsonParseError error;
105 QJsonDocument jsonDoc = QJsonDocument::fromJson(content, &error);
106 if (error.error != QJsonParseError::NoError) {
107 qWarning() << "When loading the license, parseJson failed:" << qPrintable(error.errorString());
108 return false;
109 }
110 if (!jsonDoc.isArray()) {
111 qWarning() << "When loading the license, parseJson failed: it is not a JSON array";
112 return false;
113 }
114
115 clear();
116 QJsonArray array = jsonDoc.array();
117 for (const QJsonValue &value : array) {
118 if (!value.isObject()) {
119 qWarning() << "When loading the license, parseJson failed: it is not a JSON object!";
120 return false;
121 }
122 DLicenseInfo::DComponentInfo *componentInfo = new DLicenseInfo::DComponentInfo;
123 QJsonObject obj = value.toObject();
124 QJsonValue name = obj.value("name");
125 QJsonValue version = obj.value("version");
126 QJsonValue copyright = obj.value("copyright");
127 QJsonValue license = obj.value("license");
128 if (!name.isString() || !version.isString()
129 || !copyright.isString() || !license.isString()) {
130 qWarning() << "When loading the license, parseJson failed: it is not a string!";
131 return false;
132 }
133 componentInfo->d_func()->name = name.toString();
134 componentInfo->d_func()->version = version.toString();
135 componentInfo->d_func()->copyRight = copyright.toString();
136 componentInfo->d_func()->licenseName = license.toString();
137 componentInfos.append(componentInfo);
138 }
139 return true;
140 }
141
142 QByteArray DLicenseInfoPrivate::licenseContent(const QString &licenseName)
143 {
144 QByteArray content;
145 QStringList dirs{"/usr/share/spdx-license"};
146 if (!licenseSearchPath.isEmpty())
147 dirs.prepend(licenseSearchPath);
148 for (const QString &dir : dirs) {
149 QFile file(QString("%1/%2.txt").arg(dir).arg(licenseName));
150 if (!file.exists())
151 continue;
152 if (file.open(QIODevice::ReadOnly)) {
153 content = file.readAll();
154 file.close();
155 break;
156 }
157 }
158 if (content.isEmpty()) {
159 qWarning() << QString("License content is empty when getting license content!");
160 }
161 return content;
162 }
163
164 void DLicenseInfoPrivate::clear()
165 {
166 qDeleteAll(componentInfos);
167 componentInfos.clear();
168 }
169
170 DLicenseInfo::DLicenseInfo(DObject *parent)
171 : DObject(*new DLicenseInfoPrivate(this), parent)
172 {
173 }
174
175 bool DLicenseInfo::loadContent(const QByteArray &content)
176 {
177 D_D(DLicenseInfo);
178 return d->loadContent(content);
179 }
180
181 bool DLicenseInfo::loadFile(const QString &file)
182 {
183 D_D(DLicenseInfo);
184 return d->loadFile(file);
185 }
186
187 void DLicenseInfo::setLicenseSearchPath(const QString &path)
188 {
189 D_D(DLicenseInfo);
190 d->licenseSearchPath = path;
191 }
192
193 QByteArray DLicenseInfo::licenseContent(const QString &licenseName)
194 {
195 D_D(DLicenseInfo);
196 return d->licenseContent(licenseName);
197 }
198
199 DLicenseInfo::DComponentInfos DLicenseInfo::componentInfos() const
200 {
201 D_DC(DLicenseInfo);
202 return d->componentInfos;
203 }
204
205 DCORE_END_NAMESPACE