mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 19:13:43 +08:00
- Renamed `cmds` module to `cmd` for better naming consistency - Reorganized command modules into separate files under src/cmd/ - Updated all imports and references to use the new module name - Fixed missing dependency in webdav.rs to reference core::backup - Updated tray module to use new cmd namespace - Improved uwp.rs module structure using platform-specific implementations - Removed unnecessary imports from various command files
85 lines
2.9 KiB
Rust
85 lines
2.9 KiB
Rust
use crate::cmd;
|
||
use crate::config::{Config, PrfItem, PrfOption};
|
||
use crate::core::handle;
|
||
use crate::core::CoreManager;
|
||
use crate::core::*;
|
||
use anyhow::{bail, Result};
|
||
|
||
/// Toggle proxy profile
|
||
pub fn toggle_proxy_profile(profile_index: String) {
|
||
tauri::async_runtime::spawn(async move {
|
||
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||
match cmd::patch_profiles_config_by_profile_index(app_handle, profile_index).await {
|
||
Ok(_) => {
|
||
let _ = tray::Tray::global().update_menu();
|
||
}
|
||
Err(err) => {
|
||
log::error!(target: "app", "{err}");
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/// Update a profile
|
||
/// If updating current profile, activate it
|
||
pub async fn update_profile(uid: String, option: Option<PrfOption>) -> Result<()> {
|
||
println!("[订阅更新] 开始更新订阅 {}", uid);
|
||
|
||
let url_opt = {
|
||
let profiles = Config::profiles();
|
||
let profiles = profiles.latest();
|
||
let item = profiles.get_item(&uid)?;
|
||
let is_remote = item.itype.as_ref().map_or(false, |s| s == "remote");
|
||
|
||
if !is_remote {
|
||
println!("[订阅更新] {} 不是远程订阅,跳过更新", uid);
|
||
None // 非远程订阅直接更新
|
||
} else if item.url.is_none() {
|
||
println!("[订阅更新] {} 缺少URL,无法更新", uid);
|
||
bail!("failed to get the profile item url");
|
||
} else {
|
||
println!(
|
||
"[订阅更新] {} 是远程订阅,URL: {}",
|
||
uid,
|
||
item.url.clone().unwrap()
|
||
);
|
||
Some((item.url.clone().unwrap(), item.option.clone()))
|
||
}
|
||
};
|
||
|
||
let should_update = match url_opt {
|
||
Some((url, opt)) => {
|
||
println!("[订阅更新] 开始下载新的订阅内容");
|
||
let merged_opt = PrfOption::merge(opt, option);
|
||
let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
|
||
|
||
println!("[订阅更新] 更新订阅配置");
|
||
let profiles = Config::profiles();
|
||
let mut profiles = profiles.latest();
|
||
profiles.update_item(uid.clone(), item)?;
|
||
|
||
let is_current = Some(uid.clone()) == profiles.get_current();
|
||
println!("[订阅更新] 是否为当前使用的订阅: {}", is_current);
|
||
is_current
|
||
}
|
||
None => true,
|
||
};
|
||
|
||
if should_update {
|
||
println!("[订阅更新] 更新内核配置");
|
||
match CoreManager::global().update_config().await {
|
||
Ok(_) => {
|
||
println!("[订阅更新] 更新成功");
|
||
handle::Handle::refresh_clash();
|
||
}
|
||
Err(err) => {
|
||
println!("[订阅更新] 更新失败: {}", err);
|
||
handle::Handle::notice_message("set_config::error", format!("{err}"));
|
||
log::error!(target: "app", "{err}");
|
||
}
|
||
}
|
||
}
|
||
|
||
Ok(())
|
||
}
|