Skip to content

Commit a134058

Browse files
mhduiydeepin-bot[bot]
authored andcommitted
chore: update license info loading and search paths
1. Extended copyright year range from 2023 to 2023-2026 2. Added support for automatic license file discovery in standard data locations 3. When no file path is provided, now searches for application-specific JSON config in deepin/credits directory 4. Enhanced license content search to check both custom-licenses and spdx-licenses directories in all standard data locations 5. Improved code organization with constants for directory names 6. Simplified license content loading logic with early returns chore: 更新许可证信息加载和搜索路径 1. 将版权年份范围从2023年扩展到2023-2026年 2. 添加对标准数据位置中自动发现许可证文件的支持 3. 当未提供文件路径时,现在会在deepin/credits目录中搜索应用程序特定的 JSON配置 4. 增强许可证内容搜索,在所有标准数据位置中检查custom-licenses和spdx- licenses目录 5. 使用常量优化目录名称的代码组织 6. 通过提前返回简化许可证内容加载逻辑 PMS: TASK-387863
1 parent 4a1dabc commit a134058

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

src/dlicenseinfo.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -11,9 +11,16 @@
1111
#include <QJsonObject>
1212
#include <QJsonArray>
1313
#include <QDebug>
14+
#include <QCoreApplication>
15+
#include <QStandardPaths>
1416

1517
DCORE_BEGIN_NAMESPACE
1618

19+
// /usr/share/spdx-licenses
20+
constexpr auto SystemLicenseDir = "spdx-licenses";
21+
constexpr auto CustomLicenseDir = "custom-licenses";
22+
constexpr auto SystemConfigDir = "deepin/credits";
23+
1724
class DLicenseInfo::DComponentInfoPrivate : public DObjectPrivate
1825
{
1926
public:
@@ -90,6 +97,16 @@ DLicenseInfoPrivate::~DLicenseInfoPrivate()
9097

9198
bool DLicenseInfoPrivate::loadFile(const QString &file)
9299
{
100+
clear();
101+
if (file.isEmpty()) {
102+
const QString relPath = QString("%1/%2.json").arg(SystemConfigDir, qApp->applicationName());
103+
for (const QString &dataDir : QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation)) {
104+
const QString file = dataDir + '/' + relPath;
105+
if (QFile::exists(file))
106+
return loadFile(file);
107+
}
108+
return false;
109+
}
93110
QFile jsonFile(file);
94111
if (!jsonFile.open(QIODevice::ReadOnly)) {
95112
qWarning() << QString("Failed on open file: \"%1\", error message: \"%2\"").arg(
@@ -141,24 +158,20 @@ bool DLicenseInfoPrivate::loadContent(const QByteArray &content)
141158

142159
QByteArray DLicenseInfoPrivate::licenseContent(const QString &licenseName)
143160
{
144-
QByteArray content;
145-
QStringList dirs{"/usr/share/spdx-license"};
161+
const QStringList dataDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
162+
QStringList searchDirs;
146163
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!");
164+
searchDirs << licenseSearchPath;
165+
for (const QString &dataDir : dataDirs)
166+
for (const auto *dir : {CustomLicenseDir, SystemLicenseDir})
167+
searchDirs << (dataDir + '/' + dir);
168+
169+
for (const QString &dir : searchDirs) {
170+
QFile file(dir + '/' + licenseName + ".txt");
171+
if (file.open(QIODevice::ReadOnly))
172+
return file.readAll();
160173
}
161-
return content;
174+
return {};
162175
}
163176

164177
void DLicenseInfoPrivate::clear()

0 commit comments

Comments
 (0)