ukui-quick/ 0000775 0001750 0001750 00000000000 15167362530 011577 5 ustar feng feng ukui-quick/framework/ 0000775 0001750 0001750 00000000000 15153756415 013600 5 ustar feng feng ukui-quick/framework/ukui-quick-framework.pc.in 0000664 0001750 0001750 00000000477 15153755732 020624 0 ustar feng feng prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/@CMAKE_LIBRARY_ARCHITECTURE@
includedir=${prefix}/include/ukui-quick/framework
Name: ukui-quick-framework
Description: ukui-quick-framework header files
URL: https://www.ukui.org/
Version: @VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -lukui-quick-framework ukui-quick/framework/config/ 0000775 0001750 0001750 00000000000 15153755732 015046 5 ustar feng feng ukui-quick/framework/config/config.h 0000664 0001750 0001750 00000011022 15153755732 016460 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#ifndef UKUI_QUICK_CONFIG_H
#define UKUI_QUICK_CONFIG_H
#include
#include
#include
#include
#include
namespace UkuiQuick {
class ConfigIFace : public QObject
{
Q_OBJECT
public:
explicit ConfigIFace(QObject *parent = nullptr);
Q_INVOKABLE virtual QStringList keys() const = 0;
Q_INVOKABLE virtual QVariant getValue(const QString &key) const = 0;
Q_INVOKABLE virtual void setValue(const QString &key, const QVariant &value) = 0;
/**
* 从数据源同步数据或写入数据
*/
Q_INVOKABLE virtual void forceSync() = 0;
Q_SIGNALS:
void configChanged(const QString &key);
};
class Config;
/**
* 一个组必须是一个数组,数组中的对象顺序不限
* 第一个String是组的名称
* 第二个String是组内对象的主键名称,唯一标识一个对象
*/
typedef QHash ConfigGroupInfo;
typedef QList ConfigList;
/**
* 一个配置文件节点。
* 如果从文件或者一个Map生成,那么就是一个根节点。
* 如果由父配置构造,那么就是一个子节点,并且有一个指向父节点的指针。
*
* 构造方式:
* 1.从文件加载
* 2.从VariantMap加载
* 3.从父节点获取
*
*/
class ConfigPrivate;
class Config : public ConfigIFace
{
Q_OBJECT
public:
/**
* 从文件加载数据
* @param file 一个json文件
* @param groupInfo 分组信息
* @param parent 父对象
*/
explicit Config(const QString &file, QObject *parent = nullptr);
/**
* 从VariantMap加载数据
* @param data 数据
* @param groupInfo 分组信息
* @param parent 父节点
*/
explicit Config(const QVariantMap &data, QObject *parent = nullptr);
~Config() override;
// Apis
const QVariantMap &data() const;
bool contains(const QString &key) const;
QStringList keys() const override;
QVariant getValue(const QString &key) const override;
void setValue(const QString &key, const QVariant &value) override;
void forceSync() override;
bool isRoot() const;
bool isNull() const;
const QVariant &id() const;
const QString &group() const;
const ConfigGroupInfo &groupInfo() const;
// void setGroupInfo(const ConfigGroupInfo &groupInfo);
void addGroupInfo(const QString &group, const QString &key);
// TODO: remove group
// void removeGroupInfo(const QString &group);
ConfigList children(const QString &group) const;
int numberOfChildren(const QString &group) const;
Config *child(const QString &group, const QVariant &id) const;
/**
* 添加子节点
* @param group 组名
* @param childData 数据
*/
void addChild(const QString &group, const QVariantMap &childData);
/**
* 删除一个子节点
* @param group 组名
* @param id 主键值
*/
void removeChild(const QString &group, const QVariant &id);
/**
* 删除一个key
* @param key 要删除的键
*/
void removeKey(const QString &key);
Q_SIGNALS:
void childAdded(const QString &group, const QVariant &id);
void childRemoved(const QString &group, const QVariant &id);
public:
bool event(QEvent *event) override;
private:
/**
* 从父节点继承数据
* 作为私有构造函数,由内部函数调用
* @param data 数据
* @param groupInfo 分组信息
* @param parent 父节点
*/
explicit Config(const QVariantMap &data, Config *parent = nullptr);
void extractChildren();
void extractGroup(const QString &group, const QString &key);
void setId(const QVariant &id);
void setGroup(const QString &group);
void requestUpdate();
private:
ConfigPrivate *d {nullptr};
friend class ConfigPrivate;
};
} // UkuiQuick
#endif //UKUI_QUICK_CONFIG_H
ukui-quick/framework/config/config-loader.h 0000664 0001750 0001750 00000004035 15153755732 017732 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#ifndef UKUI_QUICK_CONFIG_LOADER_H
#define UKUI_QUICK_CONFIG_LOADER_H
#include
#include "config.h"
namespace UkuiQuick {
/**
* 获取从文件系统加载配置文件
* 此处负责处理文件的加载路径
* 根据Domain进行映射:domain {
* Local: ~/.config/org.ukui/appid/id.json
* Global: ~/.config/org.ukui/_ukui-config-global/id.json
* }
*/
class ConfigLoader
{
public:
enum Domain {
Local = 0,
Global
};
enum Type {
Json = 0,
Ini
};
/**
* 加载配置文件
* @param id 配置文件ID,对应id.json
* @param app 应用名称
* @param domain {
* Local: ~/.config/org.ukui/appid/id.json
* Global: ~/.config/org.ukui/_ukui-config-global/id.json
* }
* @return config
*/
static Config *getConfig(const QString &id, Domain domain = Global, const QString &appid = QString());
static QString getFullFileName(const QString &id, Type type = Json, Domain domain = Global, const QString &appid = QString());
static QString globalConfigPath();
static QString localConfigPath();
static QString getConfigFileName(const QString &id, Type type = Json);
private:
static QMap> globalCache;
};
} // UkuiQuick
#endif //UKUI_QUICK_CONFIG_LOADER_H
ukui-quick/framework/config/config.cpp 0000664 0001750 0001750 00000031771 15153755732 017030 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#include "config.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#include
namespace UkuiQuick {
ConfigIFace::ConfigIFace(QObject *parent) : QObject(parent)
{
}
// ====== ConfigPrivate ====== //
class ConfigPrivate
{
public:
explicit ConfigPrivate(QString file);
explicit ConfigPrivate(QVariantMap d, Config *p);
void loadConfigFile();
void save();
static QFileInfo getFileInfo(const QString &filename);
QJsonObject toJsonObject();
static Config *findConfig(const ConfigList &configList, const QString &key, const QVariant &value);
// 配置文件全路径
QString configFile;
QString configLockFile;
bool requestUpdate {false};
// 所属组
QString group;
// 当前配置的ID值
QVariant id;
// 当前节点的分组信息
ConfigGroupInfo groupInfo;
// 配置数据核心
QVariantMap data;
// 全部子节点
QHash children;
// 父节点,如果是来自于父配置的话
Config *parentConfig {nullptr};
private:
friend class Config;
};
ConfigPrivate::ConfigPrivate(QString file)
: configFile(std::move(file)), configLockFile(file + QStringLiteral(".lock")), id(QStringLiteral("_root"))
{
loadConfigFile();
}
ConfigPrivate::ConfigPrivate(QVariantMap d, Config *p)
: data(std::move(d)), parentConfig(p)
{
}
QFileInfo ConfigPrivate::getFileInfo(const QString &filename)
{
QFileInfo fileInfo(filename);
QDir dir = fileInfo.dir();
if (!dir.exists()) {
bool mpb = dir.mkpath(fileInfo.path());
qDebug() << "Config: mkPath:" << fileInfo.path() << mpb;
}
return fileInfo;
}
void ConfigPrivate::loadConfigFile()
{
QByteArray jsonData("{}");
QFileInfo fileInfo = ConfigPrivate::getFileInfo(configFile);
if (!fileInfo.exists()) {
QFile file(fileInfo.absoluteFilePath());
if (file.open(QFile::ReadWrite | QFile::Text)) {
file.write(jsonData);
file.flush();
file.close();
}
qDebug() << "Config: File does not exist, initializing to default values:" << jsonData << fileInfo.fileName();
return;
}
if (!fileInfo.isReadable()) {
qWarning() << "Config: Permission denied to read file:" << configFile;
return;
}
QFile file(fileInfo.absoluteFilePath());
if (file.open(QFile::ReadOnly)) {
jsonData = file.readAll();
file.close();
}
QJsonParseError parseError {};
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData, &parseError);
if (jsonDocument.isNull()) {
qWarning() << "Config: Failed to parse JSON:" << parseError.errorString() << configFile;
return;
}
if (!jsonDocument.isObject()) {
qWarning() << "Config: Configuration file format error." << configFile;
return;
}
data = jsonDocument.object().toVariantMap();
}
void ConfigPrivate::save()
{
if (configFile.isEmpty()) {
return;
}
// 1.读取文件信息,是否只读
QFileInfo fileInfo = getFileInfo(configFile);
if (!fileInfo.isWritable()) {
qWarning() << "Config: No permission to write to the file:" << fileInfo.fileName();
return;
}
// 2.获取lockfile信息
QLockFile lockFile(configLockFile);
if (!lockFile.lock()) {
return;
}
QJsonDocument jsonDocument(toJsonObject());
if (jsonDocument.isObject()) {
QFile file(fileInfo.absoluteFilePath());
if (file.open(QFile::ReadWrite | QFile::Truncate | QFile::Text)) {
qint64 writeLength = file.write(jsonDocument.toJson());
if (writeLength == -1) {
qWarning() << "Config::save: File write failure," << file.errorString() << configFile;
} else {
qDebug() << "Config::save: File write succeed," << writeLength;
file.flush();
}
file.close();
} else {
qWarning() << "Config::save: File open failed," << file.errorString() << configFile;
}
} else {
qWarning() << "Config::save: Failed to parse JSON";
}
lockFile.unlock();
}
QJsonObject ConfigPrivate::toJsonObject()
{
if (children.isEmpty()) {
return QJsonObject::fromVariantMap(data);
}
QJsonObject tempObj = QJsonObject::fromVariantMap(data);
QHashIterator iterator(children);
while (iterator.hasNext()) {
iterator.next();
QJsonArray jsonArray;
for (const auto &config : iterator.value()) {
jsonArray.append(config->d->toJsonObject());
}
tempObj.insert(iterator.key(), jsonArray);
}
return tempObj;
}
Config *ConfigPrivate::findConfig(const ConfigList& configList, const QString &key, const QVariant &value)
{
auto it = std::find_if(configList.constBegin(), configList.constEnd(), [&key, &value] (const Config *config) {
return config->getValue(key) == value;
});
if (it == configList.constEnd()) {
return nullptr;
}
return *it;
}
/*!
\class UkuiQuick::Config
\inheaderfile config.h
\inmodule UkuiQuickFramework
\brief 配置类,该类用于加载配置文件内容.
一个配置文件节点,如果从文件或者一个Map生成,那么就是一个根节点。
如果由父配置构造,那么就是一个子节点,并且有一个指向父节点的指针。
构造方式:
\list
\li 1.从文件加载
\li 2.从VariantMap加载
\li 3.从父节点获取
\endlist
*/
/*!
\typedef ConfigGroupInfo
\relates QString
同 QHash
*/
/*!
\typedef ConfigList
\relates Config
同 QList
*/
// ====== Config ====== //
/*!
\brief 从文件加载配置.
\a file 配置文件路径
*/
Config::Config(const QString &file, QObject *parent)
: ConfigIFace(parent), d(new ConfigPrivate(file))
{
}
/*!
\brief 从VariantMap加载配置.
\a data 配置数据
*/
Config::Config(const QVariantMap &data, QObject *parent)
: ConfigIFace(parent), d(new ConfigPrivate(data, nullptr))
{
}
Config::Config(const QVariantMap &data, Config *parent)
: ConfigIFace(parent), d(new ConfigPrivate(data, parent))
{
}
Config::~Config()
{
if (d) {
delete d;
d = nullptr;
}
}
/*!
\brief 提取所有子节点配置
根据当前配置的groupInfo信息,遍历所有分组并提取对应的子节点配置。
每个分组会调用extractGroup方法处理具体的子节点提取逻辑。
\note 如果groupInfo为空则直接返回,不做任何处理
*/
void Config::extractChildren()
{
if (d->groupInfo.isEmpty()) {
return;
}
QHashIterator iterator(d->groupInfo);
while (iterator.hasNext()) {
iterator.next();
extractGroup(iterator.key(), iterator.value());
}
}
/*!
\brief 提取指定分组配置
*/
void Config::extractGroup(const QString &group, const QString &key)
{
if (d->data.contains(group)) {
auto &value = d->data[group];
QJsonArray jsonArray = value.toJsonArray();
for (const auto &jsonObjectRef : jsonArray) {
if (!jsonObjectRef.isObject()) {
continue;
}
QVariantMap data = jsonObjectRef.toObject().toVariantMap();
addChild(group, data);
}
// 清空生成子Config的节点
value = QVariantList();
}
}
/*!
\brief 返回当前配置ID值
*/
const QVariant &Config::id() const
{
return d->id;
}
/*!
\brief 返回当前配置分组
*/
const QString &Config::group() const
{
return d->group;
}
void Config::setId(const QVariant &id)
{
if (d->id == id) {
return;
}
d->id = id;
}
void Config::setGroup(const QString &group)
{
if (d->group == group) {
return;
}
d->group = group;
}
/*!
\brief 返回配置分组信息
*/
const ConfigGroupInfo &Config::groupInfo() const
{
return d->groupInfo;
}
//void Config::setGroupInfo(const ConfigGroupInfo &groupInfo)
//{
// if (d->groupInfo.isEmpty() && d->children.isEmpty()) {
// d->groupInfo = groupInfo;
// // 将子组存放到children中
// extractChildren();
// }
//}
/*!
\brief 添加分组信息
*/
void Config::addGroupInfo(const QString &group, const QString &key)
{
if (key.isEmpty() || d->groupInfo.contains(group)) {
return;
}
d->groupInfo.insert(group, key);
extractGroup(group, key);
}
/*!
\brief 返回指定分组的子节点列表
*/
ConfigList Config::children(const QString &group) const
{
if (d->children.contains(group)) {
return d->children[group];
}
return {};
}
/*!
\brief 返回指定分组的子节点数量
*/
int Config::numberOfChildren(const QString &group) const
{
if (d->children.contains(group)) {
return static_cast(d->children[group].count());
}
return 0;
}
Config *Config::child(const QString &group, const QVariant &id) const
{
if (!d->children.contains(group)) {
return nullptr;
}
const auto key = d->groupInfo.value(group);
const ConfigList &children = d->children[group];
return ConfigPrivate::findConfig(children, key, id);
}
/*!
\brief 返回配置是否为根节点
*/
bool Config::isRoot() const
{
return d->parentConfig == nullptr;
}
/*!
\brief 返回配置是否为空
*/
bool Config::isNull() const
{
return d->data.isEmpty();
}
/*!
\brief 强制同步配置数据
*/
void Config::forceSync()
{
requestUpdate();
}
/*!
\brief 返回所有键值列表
*/
QStringList Config::keys() const
{
return d->data.keys();
}
/*!
\brief 返回配置数据
*/
const QVariantMap &Config::data() const
{
return d->data;
}
/*!
\brief 根据指定键值返回配置值
*/
QVariant Config::getValue(const QString &key) const
{
return d->data.value(key);
}
/*!
\brief 设置指定键的值
*/
void Config::setValue(const QString &key, const QVariant &value)
{
if (d->groupInfo.contains(key)) {
return;
}
d->data.insert(key, value);
Q_EMIT configChanged(key);
requestUpdate();
}
/*!
\brief 添加子节点配置.
\a group 分组名称,\a childData 子节点数据
*/
void Config::addChild(const QString &group, const QVariantMap &childData)
{
const QString key = d->groupInfo.value(group);
if (key.isEmpty() || !childData.contains(key)) {
return;
}
const auto id = childData.value(key);
if (id.isNull()) {
return;
}
auto &children = d->children[group];
auto config = ConfigPrivate::findConfig(children, key, id);
if (config) {
return;
}
// create childData
config = new Config(childData, this);
config->setGroup(group);
config->setId(id);
children.append(config);
requestUpdate();
Q_EMIT childAdded(group, id);
}
/*!
\brief 删除指定分组的子节点配置
\a group 分组名称,\a id 子节点ID
*/
void Config::removeChild(const QString &group, const QVariant &id)
{
if (!d->children.contains(group)) {
return;
}
auto &children = d->children[group];
const auto config = ConfigPrivate::findConfig(children, d->groupInfo[group], id);
if (!config) {
return;
}
children.removeOne(config);
requestUpdate();
Q_EMIT childRemoved(group, id);
}
/*!
\brief 删除指定键值的配置
*/
void Config::removeKey(const QString& key)
{
d->data.remove(key);
requestUpdate();
}
bool Config::event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest) {
d->save();
d->requestUpdate = false;
}
return QObject::event(event);
}
void Config::requestUpdate()
{
if (d->parentConfig) {
d->parentConfig->requestUpdate();
return;
}
// do sync
if (!d->requestUpdate) {
d->requestUpdate = true;
auto ev = new QEvent(QEvent::UpdateRequest);
QCoreApplication::postEvent(this, ev);
}
}
/*!
\brief 配置是否包含指定键值
*/
bool Config::contains(const QString &key) const
{
return d->data.contains(key);
}
} // UkuiQuick
ukui-quick/framework/config/config-property-map.cpp 0000664 0001750 0001750 00000004225 15153755732 021457 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: baijunjie
*
*/
#include "config-property-map.h"
#include
#include
using namespace UkuiQuick;
class UkuiQuick::ConfigPropertyMapPrivate
{
public:
explicit ConfigPropertyMapPrivate(ConfigPropertyMap *map) : q(map) {};
void loadConfig() const;
ConfigPropertyMap *q;
QPointer config;
};
void ConfigPropertyMapPrivate::loadConfig() const
{
if (!config) {
return;
}
for (const auto& key : config->keys()) {
q->insert(key, config->getValue(key));
}
}
/*!
\class UkuiQuick::ConfigPropertyMap
\inmodule UkuiQuickFramework
\inheaderfile config-property-map.h
\brief 提供配置属性映射功能,用于将配置数据与QML属性进行绑定.
*/
ConfigPropertyMap::ConfigPropertyMap(Config *config, QObject *parent) : QQmlPropertyMap(this, parent), d(new ConfigPropertyMapPrivate(this))
{
Q_ASSERT(config);
d->config = config;
d->loadConfig();
connect(d->config, &Config::configChanged, this, [this](const QString& key) {
insert(key, d->config->getValue(key));
});
connect(this, &ConfigPropertyMap::valueChanged, this, [this] (const QString &key, const QVariant &value) {
d->config->setValue(key, value);
});
}
QVariant ConfigPropertyMap::updateValue(const QString &key, const QVariant &input)
{
d->config->setValue(key, input);
d->config->forceSync();
return input;
}
#include "moc_config-property-map.cpp"
ukui-quick/framework/config/config-property-map.h 0000664 0001750 0001750 00000002425 15153755732 021124 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: baijunjie
*
*/
#ifndef UKUI_QUICK_CONFIGPROPERTYMAP_H
#define UKUI_QUICK_CONFIGPROPERTYMAP_H
#include
#include
#include "config.h"
namespace UkuiQuick {
class ConfigPropertyMapPrivate;
class ConfigPropertyMap : public QQmlPropertyMap
{
Q_OBJECT
public:
explicit ConfigPropertyMap(Config* config, QObject *parent = nullptr);
protected:
QVariant updateValue(const QString &key, const QVariant &input) override;
private:
std::unique_ptr const d;
};
}
#endif //UKUI_QUICK_CONFIGPROPERTYMAP_H
ukui-quick/framework/config/ini-config.h 0000664 0001750 0001750 00000002526 15153755732 017246 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#ifndef UKUI_QUICK_INI_CONFIG_H
#define UKUI_QUICK_INI_CONFIG_H
#include "config.h"
class QSettings;
namespace UkuiQuick {
class IniConfigPrivate;
class IniConfig : public ConfigIFace
{
Q_OBJECT
public:
explicit IniConfig(const QString &filename, QObject *parent = nullptr);
QStringList keys() const override;
QVariant getValue(const QString &key) const override;
void setValue(const QString &key, const QVariant &value) override;
void forceSync() override;
QSettings *settings() const;
private:
IniConfigPrivate *d {nullptr};
};
} // UkuiQuick
#endif //UKUI_QUICK_INI_CONFIG_H
ukui-quick/framework/config/ini-config.cpp 0000664 0001750 0001750 00000004451 15153755732 017600 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#include "ini-config.h"
#include
#include
namespace UkuiQuick {
class IniConfigPrivate
{
public:
explicit IniConfigPrivate(const QString &file, IniConfig *q);
QSettings *settings {nullptr};
};
IniConfigPrivate::IniConfigPrivate(const QString &file, IniConfig *q)
{
settings = new QSettings(file, QSettings::IniFormat, q);
}
// ===== IniConfig ===== //
/*!
\class UkuiQuick::IniConfig
\inmodule UkuiQuickFramework
\inheaderfile ini-config.h
\brief 基于 INI 格式文件的配置实现.
*/
IniConfig::IniConfig(const QString &filename, QObject *parent) : ConfigIFace(parent), d(new IniConfigPrivate(filename, this))
{
}
/*!
\brief 根据 \a key 获取指定键的值.
*/
QVariant IniConfig::getValue(const QString &key) const
{
if (d->settings) {
return d->settings->value(key);
}
return {};
}
/*!
\brief 设置指定键 \a key 的值 \a value.
*/
void IniConfig::setValue(const QString &key, const QVariant &value)
{
if (d->settings) {
d->settings->setValue(key, value);
Q_EMIT configChanged(key);
}
}
/*!
\brief 调用该函数会立即将内存中的配置同步到文件中.
*/
void IniConfig::forceSync()
{
if (d->settings) {
d->settings->sync();
}
}
/*!
\brief 获取内部的 \c QSettings 对象.
*/
QSettings *IniConfig::settings() const
{
return d->settings;
}
/*!
\brief 获取所有配置键名.
*/
QStringList IniConfig::keys() const
{
if (d->settings) {
return d->settings->allKeys();
}
return {};
}
} // UkuiQuick
ukui-quick/framework/config/config-loader.cpp 0000664 0001750 0001750 00000007101 15153755732 020262 0 ustar feng feng /*
* Copyright (C) 2024, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authors: hxf
*
*/
#include "config-loader.h"
#include
#include
#include
#include
#include
#include
/*!
\class UkuiQuick::ConfigLoader
\inheaderfile config-loader.h
\inmodule UkuiQuickFramework
\brief 配置加载器,该类用于加载配置文件.
*/
/*!
\enum ConfigLoader::Domain
\brief 配置文件域.
\value Local 本地配置文件域
\value Global 全局配置文件域
*/
/*!
\enum ConfigLoader::Type
\brief 配置文件格式.
\value Json JSON格式
\value Ini INI格式
*/
namespace UkuiQuick {
QMap> ConfigLoader::globalCache = QMap>();
/*!
\fn Config *ConfigLoader::getConfig(const QString &id, ConfigLoader::Domain domain, const QString &appid)
\brief 获取配置文件.
\a id 配置文件 \ cid ,\a domain 配置文件域 , \a appid 应用 \c id
*/
Config *ConfigLoader::getConfig(const QString &id, ConfigLoader::Domain domain, const QString &appid)
{
QString filename = getFullFileName(id, Json, domain, appid);
if (filename.isEmpty()) {
return nullptr;
}
const QString cacheKey = (domain == Local) ? (appid + QStringLiteral("_") + id) : id;
auto &cache = ConfigLoader::globalCache[domain];
if (cache.contains(cacheKey)) {
return cache.value(cacheKey);
}
auto config = new Config(filename);
cache.insert(cacheKey, config);
return config;
}
/*!
\brief 获取配置文件的完整路径.
\a id 配置文件 \c id, \a type 配置文件格式, \a domain 配置文件域, \a appid 应用 \c id
*/
QString ConfigLoader::getFullFileName(const QString &id, Type type, Domain domain, const QString &appid)
{
const QString filename = ConfigLoader::getConfigFileName(id, type);
if (filename.isEmpty()) {
return {};
}
if (domain == Global) {
return ConfigLoader::globalConfigPath() + filename;
}
if (appid.isEmpty()) {
return {};
}
return ConfigLoader::localConfigPath() + appid + QStringLiteral("/") + filename;
}
/*!
\brief 获取全局配置文件的完整路径.
*/
QString ConfigLoader::globalConfigPath()
{
return QDir::homePath() + QStringLiteral("/.config/org.ukui/_ukui-config-global/");
}
/*!
\brief 获取本地配置文件的完整路径.
*/
QString ConfigLoader::localConfigPath()
{
return QDir::homePath() + QStringLiteral("/.config/org.ukui/");
}
/*!
\brief 获取配置文件的文件名.
\a id 配置文件 \c id, \a type 配置文件格式
*/
QString ConfigLoader::getConfigFileName(const QString &id, ConfigLoader::Type type)
{
if (id.isEmpty()) {
return {};
}
if (type == Json) {
return id + QStringLiteral(".json");
}
return id + QStringLiteral(".conf");
}
} // UkuiQuick
ukui-quick/framework/doc/ 0000775 0001750 0001750 00000000000 15153755732 014346 5 ustar feng feng ukui-quick/framework/doc/images/ 0000775 0001750 0001750 00000000000 15153755732 015613 5 ustar feng feng ukui-quick/framework/doc/images/model_view.png 0000664 0001750 0001750 00000202662 15153755732 020463 0 ustar feng feng PNG
IHDR ; cHRM z&