11use crate :: plugins:: PluginConfig ;
2+ // 全局配置管理器
3+ use crate :: PluginManagerState ;
24use log:: { info, warn} ;
35use serde:: { Deserialize , Serialize } ;
46use std:: fs;
57use std:: path:: PathBuf ;
6- // 全局配置管理器
78use std:: sync:: Mutex ;
8- use tauri:: command;
9+ use tauri:: { AppHandle , Manager , command} ;
10+
911static CONFIG_MANAGER : Mutex < Option < ConfigManager > > = Mutex :: new ( None ) ;
1012
1113#[ derive( Debug , Clone , Serialize , Deserialize ) ]
@@ -35,9 +37,9 @@ pub struct ConfigManager {
3537}
3638
3739impl ConfigManager {
38- pub fn new ( ) -> Result < Self , String > {
40+ pub fn new ( app_handle : Option < & AppHandle > ) -> Result < Self , String > {
3941 let config_path = Self :: get_config_path ( ) ?;
40- let config = Self :: load_config ( & config_path) ?;
42+ let config = Self :: load_config ( & config_path, app_handle ) ?;
4143
4244 Ok ( Self {
4345 config_path,
@@ -59,27 +61,65 @@ impl ConfigManager {
5961 Ok ( config_file)
6062 }
6163
62- fn load_config ( config_path : & PathBuf ) -> Result < AppConfig , String > {
64+ fn load_config (
65+ config_path : & PathBuf ,
66+ app_handle : Option < & AppHandle > ,
67+ ) -> Result < AppConfig , String > {
68+ println ! ( "读取配置 -> 正在读取配置文件 {:?}" , config_path) ;
6369 if config_path. exists ( ) {
6470 match fs:: read_to_string ( config_path) {
6571 Ok ( content) => match serde_json:: from_str :: < AppConfig > ( & content) {
66- Ok ( config) => {
67- info ! ( "读取配置 -> 成功加载配置文件: {:?}" , config_path) ;
72+ Ok ( mut config) => {
73+ println ! ( "读取配置 -> 成功加载配置文件: {:?}" , config_path) ;
74+
75+ // 检查 plugins 是否为 null,如果是则加载默认配置
76+ if config. plugins . is_none ( ) {
77+ println ! ( "读取配置 -> plugins 为 null,加载默认插件配置" ) ;
78+ config. plugins = Self :: get_default_plugins_config ( app_handle) ;
79+ }
80+
6881 Ok ( config)
6982 }
7083 Err ( e) => {
7184 warn ! ( "读取配置 -> 配置文件格式错误,使用默认配置: {}" , e) ;
72- Ok ( AppConfig :: default ( ) )
85+ Ok ( Self :: create_default_config ( app_handle ) )
7386 }
7487 } ,
7588 Err ( e) => {
7689 warn ! ( "读取配置 -> 读取配置文件失败,使用默认配置: {}" , e) ;
77- Ok ( AppConfig :: default ( ) )
90+ Ok ( Self :: create_default_config ( app_handle ) )
7891 }
7992 }
8093 } else {
81- info ! ( "读取配置 -> 配置文件不存在,使用默认配置" ) ;
82- Ok ( AppConfig :: default ( ) )
94+ println ! ( "读取配置 -> 配置文件不存在,使用默认配置" ) ;
95+ Ok ( Self :: create_default_config ( app_handle) )
96+ }
97+ }
98+
99+ fn get_default_plugins_config ( app_handle : Option < & AppHandle > ) -> Option < Vec < PluginConfig > > {
100+ if let Some ( handle) = app_handle {
101+ // 从 Tauri 状态中获取 PluginManager
102+ if let Some ( plugin_manager_state) = handle. try_state :: < PluginManagerState > ( ) {
103+ // 同步访问插件管理器
104+ if let Ok ( manager) = plugin_manager_state. try_lock ( ) {
105+ return Some ( manager. get_all_plugin_default_config ( ) ) ;
106+ } else {
107+ println ! ( "读取配置 -> 无法获取插件管理器锁,使用空配置" ) ;
108+ }
109+ } else {
110+ println ! ( "读取配置 -> 无法获取插件管理器状态,使用空配置" ) ;
111+ }
112+ }
113+ Some ( vec ! [ ] )
114+ }
115+
116+ fn create_default_config ( app_handle : Option < & AppHandle > ) -> AppConfig {
117+ AppConfig {
118+ log_directory : None ,
119+ auto_clear_logs : Some ( true ) ,
120+ keep_log_days : Some ( 30 ) ,
121+ theme : Some ( "system" . to_string ( ) ) ,
122+ plugins : Self :: get_default_plugins_config ( app_handle) ,
83123 }
84124 }
85125
@@ -89,7 +129,7 @@ impl ConfigManager {
89129
90130 fs:: write ( & self . config_path , content) . map_err ( |e| format ! ( "写入配置文件失败: {}" , e) ) ?;
91131
92- info ! ( "保存配置 -> 配置文件已保存: {:? }" , self . config_path) ;
132+ info ! ( "保存配置 -> 配置文件已保存 { }" , self . config_path. display ( ) ) ;
93133 Ok ( ( ) )
94134 }
95135
@@ -108,15 +148,15 @@ impl ConfigManager {
108148}
109149
110150// 初始化配置
111- pub fn init_config ( ) -> Result < ( ) , String > {
112- let config_manager = ConfigManager :: new ( ) ?;
151+ pub fn init_config ( app_handle : Option < & AppHandle > ) -> Result < ( ) , String > {
152+ let config_manager = ConfigManager :: new ( app_handle ) ?;
113153
114154 // 如果配置中有自定义日志目录,设置到日志系统
115155 if let Some ( log_dir) = config_manager. get_log_directory ( ) {
116- info ! ( "初始化 -> 从配置文件加载日志目录: {}" , log_dir) ;
156+ println ! ( "读取配置 -> 从配置文件加载日志目录: {}" , log_dir) ;
117157 // 使用内部函数设置,避免循环保存
118158 if let Err ( e) = crate :: logger:: set_log_directory_internal ( log_dir. to_string ( ) ) {
119- warn ! ( "初始化 -> 应用配置中的日志目录失败: {}" , e) ;
159+ warn ! ( "读取配置 -> 应用配置中的日志目录失败: {}" , e) ;
120160 }
121161 }
122162
0 commit comments