From 7b26c6dd9c44e552ce518fc49cc179fe76582f1c Mon Sep 17 00:00:00 2001 From: momen Date: Tue, 30 Sep 2025 22:10:05 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E6=B7=BB=E5=8A=A0=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 +- src/ignoreconfig.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ src/ignoreconfig.h | 39 +++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/ignoreconfig.cpp create mode 100644 src/ignoreconfig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c624b4..ab9cd9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) src/appdelegate.h src/appdelegate.cpp src/applistmodel.h src/applistmodel.cpp src/downloadmanager.h src/downloadmanager.cpp + src/ignoreconfig.h src/ignoreconfig.cpp ) # Define target properties for Android with Qt 6 as: # set_property(TARGET spark-update-tool APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR @@ -72,4 +73,4 @@ install(TARGETS spark-update-tool if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(spark-update-tool) -endif() +endif() \ No newline at end of file diff --git a/src/ignoreconfig.cpp b/src/ignoreconfig.cpp new file mode 100644 index 0000000..4bebe18 --- /dev/null +++ b/src/ignoreconfig.cpp @@ -0,0 +1,102 @@ +#include "ignoreconfig.h" +#include +#include +#include +#include +#include + +IgnoreConfig::IgnoreConfig(QObject *parent) + : QObject(parent) +{ + // 设置配置文件路径 + QString configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); + QDir dir(configDir); + if (!dir.exists()) { + dir.mkpath("."); + } + m_configFilePath = dir.filePath("spark-update-tool/ignored_apps.conf"); + + // 确保目录存在 + QFileInfo fileInfo(m_configFilePath); + QDir configDirPath = fileInfo.dir(); + if (!configDirPath.exists()) { + configDirPath.mkpath("."); + } + + // 加载现有配置 + loadConfig(); +} + +void IgnoreConfig::addIgnoredApp(const QString &packageName, const QString &version) +{ + m_ignoredApps.insert(qMakePair(packageName, version)); + saveConfig(); +} + +void IgnoreConfig::removeIgnoredApp(const QString &packageName) +{ + // 移除所有该包名的版本 + auto it = m_ignoredApps.begin(); + while (it != m_ignoredApps.end()) { + if (it->first == packageName) { + it = m_ignoredApps.erase(it); + } else { + ++it; + } + } + saveConfig(); +} + +bool IgnoreConfig::isAppIgnored(const QString &packageName, const QString &version) const +{ + return m_ignoredApps.contains(qMakePair(packageName, version)); +} + +QSet> IgnoreConfig::getIgnoredApps() const +{ + return m_ignoredApps; +} + +bool IgnoreConfig::saveConfig() +{ + QFile file(m_configFilePath); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "无法打开配置文件进行写入:" << m_configFilePath; + return false; + } + + QTextStream out(&file); + for (const auto &app : m_ignoredApps) { + out << app.first << "|" << app.second << "\n"; + } + file.close(); + return true; +} + +bool IgnoreConfig::loadConfig() +{ + QFile file(m_configFilePath); + if (!file.exists()) { + // 配置文件不存在,这是正常的,返回true表示没有错误 + return true; + } + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qDebug() << "无法打开配置文件进行读取:" << m_configFilePath; + return false; + } + + m_ignoredApps.clear(); + QTextStream in(&file); + while (!in.atEnd()) { + QString line = in.readLine().trimmed(); + if (!line.isEmpty()) { + QStringList parts = line.split('|'); + if (parts.size() == 2) { + m_ignoredApps.insert(qMakePair(parts[0], parts[1])); + } + } + } + file.close(); + return true; +} \ No newline at end of file diff --git a/src/ignoreconfig.h b/src/ignoreconfig.h new file mode 100644 index 0000000..610fff5 --- /dev/null +++ b/src/ignoreconfig.h @@ -0,0 +1,39 @@ +#ifndef IGNORECONFIG_H +#define IGNORECONFIG_H + +#include +#include +#include +#include + +class IgnoreConfig : public QObject +{ + Q_OBJECT + +public: + explicit IgnoreConfig(QObject *parent = nullptr); + + // 添加忽略的应用(包名和版本号) + void addIgnoredApp(const QString &packageName, const QString &version); + + // 移除忽略的应用 + void removeIgnoredApp(const QString &packageName); + + // 检查应用是否被忽略 + bool isAppIgnored(const QString &packageName, const QString &version) const; + + // 获取所有被忽略的应用 + QSet> getIgnoredApps() const; + + // 保存配置到文件 + bool saveConfig(); + + // 从文件加载配置 + bool loadConfig(); + +private: + QSet> m_ignoredApps; + QString m_configFilePath; +}; + +#endif // IGNORECONFIG_H \ No newline at end of file