refactor: 重构项目为 MVC 架构并移除全局变量

- 将原有扁平目录结构重构为 MVC 分层架构:
  * src/core/: 核心业务逻辑(Model),完全独立于 UI 框架
  * src/ui/: 界面组件构建(View),负责纯视觉展示
  * src/controller/: 用户交互处理(Controller),连接数据与界面
  * src/utils/: 底层工具函数,专注于系统调用和字符串处理
- 引入 AppContext 结构体统一管理应用状态,替代原有的全局变量模式
- 重命名并重新组织头文件,按功能模块划分到对应子目录
- 更新 CMakeLists.txt 以适配新的目录结构
- 同步更新 README.md 文档,说明新的架构设计
This commit is contained in:
2026-03-19 20:58:41 +08:00
parent 6509ef98e4
commit a769a6b9b3
30 changed files with 1351 additions and 1243 deletions
+36
View File
@@ -0,0 +1,36 @@
#include "core/app_context.h"
#include <stdlib.h>
// 创建应用上下文
AppContext *create_app_context(void)
{
AppContext *ctx = (AppContext *)malloc(sizeof(AppContext));
if (ctx)
{
init_string_list(&ctx->sys_paths);
init_string_list(&ctx->user_paths);
}
return ctx;
}
// 销毁应用上下文
void destroy_app_context(AppContext *ctx)
{
if (ctx)
{
clear_string_list(&ctx->sys_paths);
clear_string_list(&ctx->user_paths);
free(ctx);
}
}
// 获取应用上下文
AppContext *get_app_context(Ihandle *ih)
{
if (!ih)
return NULL;
Ihandle *dlg = IupGetDialog(ih);
if (!dlg)
return NULL;
return (AppContext *)IupGetAttribute(dlg, "APP_CONTEXT");
}
+82
View File
@@ -0,0 +1,82 @@
#include "core/path_manager.h"
#include "utils/os_env.h"
#include <stdlib.h>
#include <string.h>
// 删除指定索引的路径项
void path_manager_remove_at(StringList *list, int index)
{
if (!list || index < 0 || index >= list->count)
return;
free(list->items[index]);
for (int i = index; i < list->count - 1; i++)
{
list->items[i] = list->items[i + 1];
}
list->count--;
}
// 向上移动路径项
void path_manager_move_up(StringList *list, int index)
{
if (!list || index <= 0 || index >= list->count)
return;
char *temp = list->items[index];
list->items[index] = list->items[index - 1];
list->items[index - 1] = temp;
}
// 向下移动路径项
void path_manager_move_down(StringList *list, int index)
{
if (!list || index < 0 || index >= list->count - 1)
return;
char *temp = list->items[index];
list->items[index] = list->items[index + 1];
list->items[index + 1] = temp;
}
// 清理无效路径项
int path_manager_clean(StringList *list)
{
if (!list) return 0;
int removed_count = 0;
// 从后往前遍历,方便删除
for (int i = list->count - 1; i >= 0; i--)
{
char *item = list->items[i];
if (!item) continue;
int should_remove = 0;
// 1. 检查有效性
if (!is_path_valid(item))
{
should_remove = 1;
}
else
{
// 2. 检查重复 (检查当前项之前是否出现过)
for (int j = 0; j < i; j++)
{
char *prev_item = list->items[j];
if (prev_item && _stricmp(item, prev_item) == 0)
{
should_remove = 1;
break;
}
}
}
if (should_remove)
{
path_manager_remove_at(list, i);
removed_count++;
}
}
return removed_count;
}
+144
View File
@@ -0,0 +1,144 @@
#include "core/registry_service.h"
#include "utils/string_ext.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define REG_PATH_SYS L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
#define REG_PATH_USER L"Environment"
#define REG_VALUE L"Path"
// 内部辅助函数:加载单个列表
static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list)
{
clear_string_list(list);
HKEY hKey;
LONG res = RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_READ, &hKey);
if (res != ERROR_SUCCESS)
{
return 0; // 失败
}
DWORD type, size;
res = RegQueryValueExW(hKey, REG_VALUE, NULL, &type, NULL, &size);
if (res == ERROR_SUCCESS)
{
wchar_t *buffer = (wchar_t *)malloc(size + 2);
if (buffer)
{
memset(buffer, 0, size + 2);
if (RegQueryValueExW(hKey, REG_VALUE, NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS)
{
wchar_t *current = buffer;
wchar_t *next_semicolon = NULL;
while (*current)
{
next_semicolon = wcschr(current, L';');
if (next_semicolon)
*next_semicolon = L'\0';
if (wcslen(current) > 0)
{
char *utf8_str = wide_to_utf8(current);
if (utf8_str)
{
add_string_list(list, utf8_str);
free(utf8_str);
}
}
if (next_semicolon)
current = next_semicolon + 1;
else
break;
}
}
free(buffer);
}
}
RegCloseKey(hKey);
return 1; // 成功
}
// 加载系统环境变量路径
int load_system_paths(StringList *list)
{
return load_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
}
// 加载用户环境变量路径
int load_user_paths(StringList *list)
{
return load_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
}
// 内部辅助函数:保存单个列表
static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringList *list)
{
if (!list) return 0;
// 计算大小
size_t total_len = 0;
for (int i = 0; i < list->count; i++)
{
if (list->items[i])
{
wchar_t *witem = utf8_to_wide(list->items[i]);
if (witem)
{
total_len += wcslen(witem) + 1;
free(witem);
}
}
}
total_len += 1;
wchar_t *buffer = (wchar_t *)malloc(total_len * sizeof(wchar_t));
if (!buffer)
return 0;
buffer[0] = L'\0';
for (int i = 0; i < list->count; i++)
{
if (list->items[i])
{
wchar_t *witem = utf8_to_wide(list->items[i]);
if (witem)
{
wcscat(buffer, witem);
if (i < list->count - 1)
wcscat(buffer, L";");
free(witem);
}
}
}
HKEY hKey;
int success = 0;
if (RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS)
{
DWORD size = (DWORD)((wcslen(buffer) + 1) * sizeof(wchar_t));
if (RegSetValueExW(hKey, REG_VALUE, 0, REG_EXPAND_SZ, (LPBYTE)buffer, size) == ERROR_SUCCESS)
{
success = 1;
}
RegCloseKey(hKey);
}
free(buffer);
return success;
}
// 保存系统环境变量路径
int save_system_paths(const StringList *list)
{
return save_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
}
// 保存用户环境变量路径
int save_user_paths(const StringList *list)
{
return save_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
}