profile.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use crate::{
  2. config::{ProfileItem, ProfilesConfig},
  3. events::state::{ClashInfoState, ProfilesState},
  4. utils::{clash, fetch},
  5. };
  6. use tauri::State;
  7. /// get all profiles from `profiles.yaml`
  8. /// do not acquire the lock of ProfileLock
  9. #[tauri::command]
  10. pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result<ProfilesConfig, String> {
  11. match profiles.0.lock() {
  12. Ok(profiles) => Ok(profiles.clone()),
  13. Err(_) => Err("can not get profiles lock".into()),
  14. }
  15. }
  16. /// synchronize data irregularly
  17. #[tauri::command]
  18. pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> {
  19. match profiles.0.lock() {
  20. Ok(mut profiles) => profiles.sync_file(),
  21. Err(_) => Err("can not get profiles lock".into()),
  22. }
  23. }
  24. /// Import the profile from url
  25. /// and save to `profiles.yaml`
  26. #[tauri::command]
  27. pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) -> Result<(), String> {
  28. let result = match fetch::fetch_profile(&url).await {
  29. Some(r) => r,
  30. None => return Err(format!("failed to fetch profile from `{}`", url)),
  31. };
  32. match profiles.0.lock() {
  33. Ok(mut profiles) => profiles.import_from_url(url, result),
  34. Err(_) => Err("can not get profiles lock".into()),
  35. }
  36. }
  37. /// Update the profile
  38. /// and save to `profiles.yaml`
  39. /// http request firstly
  40. /// then acquire the lock of `profiles.yaml`
  41. #[tauri::command]
  42. pub async fn update_profile(
  43. index: usize,
  44. profiles: State<'_, ProfilesState>,
  45. ) -> Result<(), String> {
  46. // maybe we can get the url from the web app directly
  47. let url = {
  48. match profiles.0.lock() {
  49. Ok(mut profile) => {
  50. let items = profile.items.take().unwrap_or(vec![]);
  51. if index >= items.len() {
  52. return Err("the index out of bound".into());
  53. }
  54. let url = match &items[index].url {
  55. Some(u) => u.clone(),
  56. None => return Err("failed to update profile for `invalid url`".into()),
  57. };
  58. profile.items = Some(items);
  59. url
  60. }
  61. Err(_) => return Err("can not get profiles lock".into()),
  62. }
  63. };
  64. let result = match fetch::fetch_profile(&url).await {
  65. Some(r) => r,
  66. None => return Err(format!("failed to fetch profile from `{}`", url)),
  67. };
  68. match profiles.0.lock() {
  69. Ok(mut profiles) => profiles.update_item(index, result),
  70. Err(_) => Err("can not get profiles lock".into()),
  71. }
  72. }
  73. /// change the current profile
  74. #[tauri::command]
  75. pub async fn select_profile(
  76. index: usize,
  77. profiles: State<'_, ProfilesState>,
  78. clash_info: State<'_, ClashInfoState>,
  79. ) -> Result<(), String> {
  80. match profiles.0.lock() {
  81. Ok(mut profiles) => profiles.put_current(index)?,
  82. Err(_) => return Err("can not get profiles lock".into()),
  83. };
  84. let arc = match clash_info.0.lock() {
  85. Ok(arc) => arc.clone(),
  86. _ => return Err("can not get clash info lock".into()),
  87. };
  88. clash::put_clash_profile(&arc).await
  89. }
  90. /// delete profile item
  91. #[tauri::command]
  92. pub fn delete_profile(index: usize, profiles: State<'_, ProfilesState>) -> Result<(), String> {
  93. match profiles.0.lock() {
  94. Ok(mut profiles) => profiles.delete_item(index),
  95. Err(_) => Err("can not get profiles lock".into()),
  96. }
  97. }
  98. /// patch the profile config
  99. #[tauri::command]
  100. pub fn patch_profile(
  101. index: usize,
  102. profile: ProfileItem,
  103. profiles: State<'_, ProfilesState>,
  104. ) -> Result<(), String> {
  105. match profiles.0.lock() {
  106. Ok(mut profiles) => profiles.patch_item(index, profile),
  107. Err(_) => Err("can not get profiles lock".into()),
  108. }
  109. }