@@ -255,13 +255,14 @@ def editorEvent(self, event, model, option, index):
255255class FolderDialog (QDialog ):
256256 """现代化文件夹选择对话框"""
257257
258- def __init__ (self , parent = None , start_dir : str = "" , multi_select : bool = True ):
258+ def __init__ (self , parent = None , start_dir : str = "" , multi_select : bool = True , config_service = None ):
259259 super ().__init__ (parent )
260260 self .multi_select = multi_select
261261 self .selected_folders : List [str ] = []
262262 self .history : List [str ] = [] # 导航历史
263263 self .history_index = - 1 # 当前历史位置
264264 self .favorite_folders : List [str ] = [] # 收藏的文件夹
265+ self .config_service = config_service
265266
266267 self .setWindowTitle ("选择文件夹" + (" (可多选)" if multi_select else "" ))
267268 self .setMinimumSize (1000 , 650 )
@@ -1162,48 +1163,71 @@ def _get_config_path(self) -> str:
11621163 config_path = os .path .join (project_root , "examples" , "config.json" )
11631164
11641165 return config_path
1166+
1167+ def _get_favorites_config_path (self ) -> str :
1168+ """获取收藏文件夹配置文件路径(用户目录)"""
1169+ # 使用用户目录存储收藏,避免污染模板文件
1170+ user_config_dir = Path .home () / ".manga-translator-ui"
1171+ user_config_dir .mkdir (exist_ok = True )
1172+ return str (user_config_dir / "favorites.json" )
11651173
11661174 def _load_favorite_folders (self ):
11671175 """从配置文件加载收藏文件夹"""
11681176 try :
1169- config_path = self ._get_config_path ()
1170- if os .path .exists (config_path ):
1171- with open (config_path , 'r' , encoding = 'utf-8' ) as f :
1172- config = json .load (f )
1173- self .favorite_folders = config .get ('app' , {}).get ('favorite_folders' , [])
1177+ if self .config_service :
1178+ # 使用config_service加载
1179+ config = self .config_service .get_config ()
1180+ self .favorite_folders = config .app .favorite_folders or []
1181+ else :
1182+ # 降级方案:直接读取文件
1183+ config_path = self ._get_config_path ()
1184+ if os .path .exists (config_path ):
1185+ with open (config_path , 'r' , encoding = 'utf-8' ) as f :
1186+ config_dict = json .load (f )
1187+ self .favorite_folders = config_dict .get ('app' , {}).get ('favorite_folders' , [])
1188+ else :
1189+ self .favorite_folders = []
11741190 except Exception as e :
11751191 print (f"加载收藏文件夹失败: { e } " )
11761192 self .favorite_folders = []
11771193
11781194 def _save_favorite_folders (self ):
11791195 """保存收藏文件夹到配置文件"""
11801196 try :
1181- config_path = self ._get_config_path ()
1182-
1183- # 读取现有配置
1184- config = {}
1185- if os .path .exists (config_path ):
1186- try :
1187- with open (config_path , 'r' , encoding = 'utf-8' ) as f :
1188- config = json .load (f )
1189- except :
1190- config = {}
1191-
1192- # 确保 app 键存在
1193- if 'app' not in config :
1194- config ['app' ] = {}
1195-
1196- # 确保 app 是字典类型
1197- if not isinstance (config ['app' ], dict ):
1198- config ['app' ] = {}
1199-
1200- # 更新收藏文件夹
1201- config ['app' ]['favorite_folders' ] = self .favorite_folders
1202-
1203- # 保存配置
1204- os .makedirs (os .path .dirname (config_path ), exist_ok = True )
1205- with open (config_path , 'w' , encoding = 'utf-8' ) as f :
1206- json .dump (config , f , indent = 2 , ensure_ascii = False )
1197+ if self .config_service :
1198+ # 使用config_service保存
1199+ config = self .config_service .get_config ()
1200+ config .app .favorite_folders = self .favorite_folders
1201+ self .config_service .set_config (config )
1202+ self .config_service .save_config_file ()
1203+ else :
1204+ # 降级方案:直接写入文件
1205+ config_path = self ._get_config_path ()
1206+
1207+ # 读取现有配置
1208+ config_dict = {}
1209+ if os .path .exists (config_path ):
1210+ try :
1211+ with open (config_path , 'r' , encoding = 'utf-8' ) as f :
1212+ config_dict = json .load (f )
1213+ except :
1214+ config_dict = {}
1215+
1216+ # 确保 app 键存在
1217+ if 'app' not in config_dict :
1218+ config_dict ['app' ] = {}
1219+
1220+ # 确保 app 是字典类型
1221+ if not isinstance (config_dict ['app' ], dict ):
1222+ config_dict ['app' ] = {}
1223+
1224+ # 更新收藏文件夹
1225+ config_dict ['app' ]['favorite_folders' ] = self .favorite_folders
1226+
1227+ # 保存配置
1228+ os .makedirs (os .path .dirname (config_path ), exist_ok = True )
1229+ with open (config_path , 'w' , encoding = 'utf-8' ) as f :
1230+ json .dump (config_dict , f , indent = 2 , ensure_ascii = False )
12071231
12081232 except Exception as e :
12091233 print (f"保存收藏文件夹失败: { e } " )
@@ -1250,19 +1274,20 @@ def _refresh_shortcuts_tree(self):
12501274 self .folder_tree .viewport ().update ()
12511275
12521276
1253- def select_folders (parent = None , start_dir : str = "" , multi_select : bool = True ) -> Optional [List [str ]]:
1277+ def select_folders (parent = None , start_dir : str = "" , multi_select : bool = True , config_service = None ) -> Optional [List [str ]]:
12541278 """
12551279 显示文件夹选择对话框
12561280
12571281 Args:
12581282 parent: 父窗口
12591283 start_dir: 起始目录
12601284 multi_select: 是否支持多选
1285+ config_service: 配置服务实例
12611286
12621287 Returns:
12631288 选中的文件夹路径列表,如果取消则返回 None
12641289 """
1265- dialog = FolderDialog (parent , start_dir , multi_select )
1290+ dialog = FolderDialog (parent , start_dir , multi_select , config_service )
12661291 if dialog .exec () == QDialog .DialogCode .Accepted :
12671292 return dialog .get_selected_folders ()
12681293 return None
0 commit comments