162 lines
4.8 KiB
C++
162 lines
4.8 KiB
C++
#ifndef PATHMANAGER_H
|
||
#define PATHMANAGER_H
|
||
|
||
#include <QString>
|
||
#include <mutex>
|
||
|
||
/**
|
||
* @brief 路径管理器类(单例模式),统一管理应用程序的配置文件路径
|
||
*
|
||
* 该类负责根据不同操作系统提供合适的配置文件存储路径:
|
||
* - Windows: 程序目录
|
||
* - Linux: 用户配置目录 (~/.config/AppName/)
|
||
*
|
||
* 特点:
|
||
* - 懒加载模式:首次调用GetInstance()时自动初始化
|
||
* - 自动检测应用名称:从应用程序路径中提取
|
||
* - 线程安全:使用互斥锁保证多线程环境下的安全访问
|
||
*
|
||
* 用法:
|
||
* @code
|
||
* // 直接获取实例,无需手动初始化
|
||
* PathManager& pm = PathManager::GetInstance();
|
||
* QString configPath = pm.GetConfigFilePath();
|
||
* QString calibPath = pm.GetCalibrationFilePath();
|
||
* @endcode
|
||
*/
|
||
class PathManager
|
||
{
|
||
public:
|
||
/**
|
||
* @brief 获取单例实例
|
||
* 首次调用时自动初始化,从应用程序路径获取应用名称
|
||
* @return 单例引用
|
||
*/
|
||
static PathManager& GetInstance();
|
||
|
||
/**
|
||
* @brief 获取配置文件(config.xml)的完整路径
|
||
* @return 配置文件的完整路径
|
||
*/
|
||
QString GetConfigFilePath() const;
|
||
|
||
/**
|
||
* @brief 获取手眼标定文件(EyeHandCalibMatrixInfo.ini)的完整路径
|
||
* @return 手眼标定文件的完整路径
|
||
*/
|
||
QString GetCalibrationFilePath() const;
|
||
|
||
/**
|
||
* @brief 获取应用程序配置目录路径
|
||
* @return 配置目录的完整路径
|
||
*/
|
||
QString GetAppConfigDirectory() const;
|
||
|
||
/**
|
||
* @brief 获取应用程序名称
|
||
* @return 应用程序名称
|
||
*/
|
||
QString GetAppName() const { return m_appName; }
|
||
|
||
/**
|
||
* @brief 设置配置加密密码
|
||
* @param password 加密密码
|
||
*/
|
||
void SetEncryptionPassword(const QString& password);
|
||
|
||
/**
|
||
* @brief 启用配置目录加密保护
|
||
* @param enable 是否启用加密保护
|
||
* @return 成功返回 true,失败返回 false
|
||
*/
|
||
bool EnableEncryptionProtection(bool enable);
|
||
|
||
/**
|
||
* @brief 读取加密的配置文件
|
||
* @param filePath 文件路径
|
||
* @return 解密后的文件内容,失败返回空
|
||
*/
|
||
QByteArray ReadEncryptedConfig(const QString& filePath) const;
|
||
|
||
/**
|
||
* @brief 写入加密的配置文件
|
||
* @param filePath 文件路径
|
||
* @param data 要写入的数据
|
||
* @return 成功返回 true,失败返回 false
|
||
*/
|
||
bool WriteEncryptedConfig(const QString& filePath, const QByteArray& data);
|
||
|
||
/**
|
||
* @brief 检查配置文件是否已加密
|
||
* @param filePath 文件路径
|
||
* @return 已加密返回 true,否则返回 false
|
||
*/
|
||
bool IsConfigEncrypted(const QString& filePath) const;
|
||
|
||
/**
|
||
* @brief 设置配置目录访问权限(仅管理员可访问)
|
||
* @return 成功返回 true,失败返回 false
|
||
*/
|
||
bool SetDirectoryProtection();
|
||
|
||
/**
|
||
* @brief 获取加密密钥(自动生成:AppName + "VisionRobot")
|
||
* @return 加密密钥
|
||
*/
|
||
QString GetEncryptionKey() const;
|
||
|
||
// 禁止拷贝和赋值
|
||
PathManager(const PathManager&) = delete;
|
||
PathManager& operator=(const PathManager&) = delete;
|
||
|
||
private:
|
||
/**
|
||
* @brief 私有构造函数(单例模式)
|
||
* @param appName 应用程序名称
|
||
*/
|
||
explicit PathManager(const QString& appName);
|
||
|
||
/**
|
||
* @brief 确保配置目录存在,如果不存在则创建
|
||
* @return 成功创建或目录已存在返回true,失败返回false
|
||
*/
|
||
bool EnsureConfigDirectoryExists();
|
||
|
||
/**
|
||
* @brief 获取程序目录路径
|
||
* @return 程序目录的完整路径
|
||
*/
|
||
QString GetProgramDirectory() const;
|
||
|
||
/**
|
||
* @brief 获取用户配置目录路径(仅Linux系统)
|
||
* @return 用户配置目录的完整路径
|
||
*/
|
||
QString GetUserConfigDirectory() const;
|
||
|
||
/**
|
||
* @brief 获取配置目录路径(内部方法)
|
||
* @return 配置目录的完整路径
|
||
*/
|
||
QString GetConfigDirectory() const;
|
||
|
||
/**
|
||
* @brief 迁移旧的明文配置文件到加密配置
|
||
* 如果 config.xml 存在但 config.encrypt 不存在,则加密并删除 config.xml
|
||
*/
|
||
void MigrateToEncryptedConfig();
|
||
|
||
private:
|
||
QString m_appName; // 应用程序名称
|
||
QString m_configFilePath; // 缓存的配置文件路径
|
||
QString m_calibrationFilePath; // 缓存的标定文件路径
|
||
QString m_configDirectory; // 缓存的配置目录路径
|
||
QString m_encryptionPassword; // 配置加密密码
|
||
bool m_encryptionEnabled; // 是否启用加密
|
||
|
||
static PathManager* s_instance; // 单例实例指针
|
||
static std::mutex s_mutex; // 线程安全的互斥锁
|
||
};
|
||
|
||
#endif // PATHMANAGER_H
|