tauri-plugin-config-manager 2.2.4

A Tauri plugin for managing configuration for Vasak applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
use serde::de::DeserializeOwned;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tauri::{plugin::PluginApi, AppHandle, Emitter, Runtime};
use tokio::io::AsyncWriteExt;
use tokio::sync::{Mutex as AsyncMutex, RwLock};

#[cfg(feature = "system-theme-sync")]
use std::process::Command;

use crate::models::*;

pub fn init<R: Runtime, C: DeserializeOwned>(
    app: &AppHandle<R>,
    _api: PluginApi<R, C>,
) -> crate::Result<ConfigManager<R>> {
    Ok(ConfigManager::new(app.clone()))
}

/// Access to the config-manager APIs with an internal TTL cache.
#[derive(Clone)]
pub struct ConfigManager<R: Runtime> {
    app: AppHandle<R>,
    cache: Arc<RwLock<Option<CacheEntry>>>,
    write_lock: Arc<AsyncMutex<()>>,
    ttl: Duration,
}

#[derive(Debug, Clone)]
struct CacheEntry {
    content: String,
    timestamp: Instant,
}

impl<R: Runtime> ConfigManager<R> {
    fn config_path_from_env() -> Option<std::path::PathBuf> {
        std::env::var_os("VASAK_CONFIG_PATH").and_then(|value| {
            let path = std::path::PathBuf::from(value);
            if path.as_os_str().is_empty() {
                None
            } else {
                Some(path)
            }
        })
    }

    fn default_scheme_paths() -> crate::Result<Vec<std::path::PathBuf>> {
        Ok(vec![
            Self::home_dir()?.join(".config/vasak/schemes"),
            std::path::PathBuf::from("/usr/share/schemes"),
        ])
    }

    fn scheme_paths_from_env() -> Option<Vec<std::path::PathBuf>> {
        let raw = std::env::var_os("VASAK_SCHEMES_PATHS")?;
        let paths: Vec<std::path::PathBuf> = std::env::split_paths(&raw)
            .filter(|path| !path.as_os_str().is_empty())
            .collect();

        if paths.is_empty() {
            None
        } else {
            Some(paths)
        }
    }

    fn effective_scheme_paths() -> crate::Result<Vec<std::path::PathBuf>> {
        if let Some(paths) = Self::scheme_paths_from_env() {
            return Ok(paths);
        }

        Self::default_scheme_paths()
    }

    async fn write_file_atomically(path: &std::path::Path, content: &str) -> crate::Result<()> {
        use std::time::{SystemTime, UNIX_EPOCH};

        let parent = path.parent().ok_or_else(|| {
            crate::Error::Other(format!(
                "Config path has no parent directory: {}",
                path.display()
            ))
        })?;

        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| crate::Error::Other(format!("System time error: {}", e)))?
            .as_nanos();
        let tmp_path = parent.join(format!(".vasak.conf.tmp-{}-{}", std::process::id(), nonce));

        let mut tmp_file = tokio::fs::File::create(&tmp_path).await.map_err(|e| {
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to create temporary config file {}: {}",
                    tmp_path.display(),
                    e
                ),
            ))
        })?;

        if let Err(e) = tmp_file.write_all(content.as_bytes()).await {
            let _ = tokio::fs::remove_file(&tmp_path).await;
            return Err(crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to write temporary config file {}: {}",
                    tmp_path.display(),
                    e
                ),
            )));
        }

        if let Err(e) = tmp_file.sync_all().await {
            let _ = tokio::fs::remove_file(&tmp_path).await;
            return Err(crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to sync temporary config file {}: {}",
                    tmp_path.display(),
                    e
                ),
            )));
        }

        drop(tmp_file);

        tokio::fs::rename(&tmp_path, path).await.map_err(|e| {
            let _ = std::fs::remove_file(&tmp_path);
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to atomically replace config file {}: {}",
                    path.display(),
                    e
                ),
            ))
        })
    }

    pub fn new(app: AppHandle<R>) -> Self {
        // Default TTL de 30 minutos para evitar lecturas de disco frecuentes.
        Self {
            app,
            cache: Arc::new(RwLock::new(None)),
            write_lock: Arc::new(AsyncMutex::new(())),
            ttl: Duration::from_secs(30 * 60),
        }
    }

    fn home_dir() -> crate::Result<std::path::PathBuf> {
        dirs_next::home_dir().ok_or_else(|| {
            crate::Error::Other("No se pudo obtener el directorio home del usuario".to_string())
        })
    }

    /// Returns true if the cache is present and not expired.
    async fn is_cache_valid(&self) -> bool {
        let guard = self.cache.read().await;
        if let Some(entry) = guard.as_ref() {
            entry.timestamp.elapsed() < self.ttl
        } else {
            false
        }
    }

    /// Read configuration using cache-first strategy.
    pub async fn read_config(&self) -> crate::Result<String> {
        if self.is_cache_valid().await {
            let guard = self.cache.read().await;
            if let Some(entry) = guard.as_ref() {
                return Ok(entry.content.clone());
            }
        }

        // Cache inválido o inexistente: leer de disco y actualizar cache.
        let config_path = self.config_path()?;

        // Si el archivo no existe, crearlo con una configuración por defecto
        if !config_path.exists() {
            let _write_guard = self.write_lock.lock().await;
            if !config_path.exists() {
                self.create_default_config().await?;
            }
        }

        let config_content = tokio::fs::read_to_string(&config_path).await.map_err(|e| {
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to read config file {}: {}",
                    config_path.display(),
                    e
                ),
            ))
        })?;

        {
            let mut guard = self.cache.write().await;
            *guard = Some(CacheEntry {
                content: config_content.clone(),
                timestamp: Instant::now(),
            });
        }

        Ok(config_content)
    }

    pub async fn write_config(&self, config: &str) -> crate::Result<()> {
        let config_path = self.config_path()?;

        // Validar semánticamente el payload antes de persistir.
        let parsed_config: VSKConfig =
            serde_json::from_str(config).map_err(crate::Error::Json)?;

        let _write_guard = self.write_lock.lock().await;

        // Aplicar icon pack en runtime según el modo actual guardado.
        Self::try_apply_icon_pack(&parsed_config.icons, parsed_config.style.darkmode);

        // Crear el directorio padre si no existe
        if let Some(parent) = config_path.parent() {
            tokio::fs::create_dir_all(parent).await.map_err(|e| {
                crate::Error::Io(std::io::Error::new(
                    e.kind(),
                    format!(
                        "Failed to create config directory {}: {}",
                        parent.display(),
                        e
                    ),
                ))
            })?;
        }

        Self::write_file_atomically(config_path.as_path(), config).await?;
        // Actualizar cache inmediatamente con el contenido provisto
        {
            let mut guard = self.cache.write().await;
            *guard = Some(CacheEntry {
                content: config.to_string(),
                timestamp: Instant::now(),
            });
        }
        // Emitir evento para que frontends reaccionen
        let _ = self.app.emit(crate::CONFIG_CHANGED_EVENT, ());
        Ok(())
    }

    pub fn config_path(&self) -> crate::Result<std::path::PathBuf> {
        if let Some(path) = Self::config_path_from_env() {
            return Ok(path);
        }

        Ok(Self::home_dir()?.join(".config/vasak/vasak.conf"))
    }

    #[cfg(feature = "system-theme-sync")]
    fn run_gsettings(args: &[&str]) -> crate::Result<String> {
        let output = Command::new("gsettings").args(args).output().map_err(|e| {
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!("Failed to run gsettings {}: {}", args.join(" "), e),
            ))
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
            let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
            let detail = if stderr.is_empty() { stdout } else { stderr };
            return Err(crate::Error::Io(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("gsettings {} failed: {}", args.join(" "), detail),
            )));
        }

        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }

    #[cfg(feature = "system-theme-sync")]
    fn has_gsettings_binary() -> bool {
        Command::new("gsettings").arg("help").output().is_ok()
    }

    #[cfg(feature = "system-theme-sync")]
    fn try_sync_system_darkmode(darkmode: bool) {
        if !Self::has_gsettings_binary() {
            eprintln!(
                "[ConfigManager::set_darkmode] gsettings not found; skipping system theme sync"
            );
            return;
        }

        let current_scheme_raw = match Self::run_gsettings(&[
            "get",
            "org.gnome.desktop.interface",
            "color-scheme",
        ]) {
            Ok(value) => value,
            Err(e) => {
                eprintln!(
                    "[ConfigManager::set_darkmode] Could not read system color-scheme via gsettings: {}",
                    e
                );
                return;
            }
        };

        let current_scheme = current_scheme_raw
            .trim_matches('"')
            .trim_matches('\'')
            .to_string();

        if darkmode && current_scheme != "prefer-dark" {
            if let Err(e) = Self::run_gsettings(&[
                "set",
                "org.gnome.desktop.interface",
                "color-scheme",
                "prefer-dark",
            ]) {
                eprintln!(
                    "[ConfigManager::set_darkmode] Could not set GNOME color-scheme to prefer-dark: {}",
                    e
                );
                return;
            }

            if let Err(e) = Self::run_gsettings(&[
                "set",
                "org.gnome.desktop.interface",
                "gtk-theme",
                "Adwaita-dark",
            ]) {
                eprintln!(
                    "[ConfigManager::set_darkmode] Could not set GNOME gtk-theme to Adwaita-dark: {}",
                    e
                );
            }
        } else if !darkmode && current_scheme != "prefer-light" {
            if let Err(e) = Self::run_gsettings(&[
                "set",
                "org.gnome.desktop.interface",
                "color-scheme",
                "prefer-light",
            ]) {
                eprintln!(
                    "[ConfigManager::set_darkmode] Could not set GNOME color-scheme to prefer-light: {}",
                    e
                );
                return;
            }

            if let Err(e) =
                Self::run_gsettings(&["set", "org.gnome.desktop.interface", "gtk-theme", "Adwaita"])
            {
                eprintln!(
                    "[ConfigManager::set_darkmode] Could not set GNOME gtk-theme to Adwaita: {}",
                    e
                );
            }
        }
    }

    #[cfg(not(feature = "system-theme-sync"))]
    fn try_sync_system_darkmode(_darkmode: bool) {}

    #[cfg(feature = "system-theme-sync")]
    fn try_apply_icon_pack(icons: &Icons, darkmode: bool) {
        if !Self::has_gsettings_binary() {
            return;
        }

        let selected_pack = if darkmode {
            icons.dark.trim()
        } else {
            icons.light.trim()
        };

        if selected_pack.is_empty() {
            return;
        }

        if let Err(e) = Self::run_gsettings(&[
            "set",
            "org.gnome.desktop.interface",
            "icon-theme",
            selected_pack,
        ]) {
            eprintln!(
                "[ConfigManager] Could not set icon theme to '{}': {}",
                selected_pack,
                e
            );
        }
    }

    #[cfg(not(feature = "system-theme-sync"))]
    fn try_apply_icon_pack(_icons: &Icons, _darkmode: bool) {}

    pub async fn set_darkmode(&self, darkmode: bool) -> crate::Result<()> {
        let _write_guard = self.write_lock.lock().await;

        // Intentamos sincronizar con GNOME si está disponible, pero sin bloquear
        // la persistencia de configuración cuando no existe gsettings o falla.
        Self::try_sync_system_darkmode(darkmode);

        let config_path = self.config_path()?;

        // Si el archivo no existe, crearlo con una configuración por defecto
        if !config_path.exists() {
            self.create_default_config().await?;
        }

        let config_content = tokio::fs::read_to_string(&config_path).await.map_err(|e| {
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to read config file {}: {}",
                    config_path.display(),
                    e
                ),
            ))
        })?;

        let mut config: VSKConfig =
            serde_json::from_str(&config_content).map_err(crate::Error::Json)?;

        config.style.darkmode = darkmode;

        // Aplicar icon pack asociado al modo actual (dark/light).
        Self::try_apply_icon_pack(&config.icons, darkmode);

        let new_content = serde_json::to_string_pretty(&config).map_err(crate::Error::Json)?;

        Self::write_file_atomically(config_path.as_path(), &new_content).await?;
        // Actualizar cache con el nuevo contenido
        {
            let mut guard = self.cache.write().await;
            *guard = Some(CacheEntry {
                content: serde_json::to_string_pretty(&config).map_err(crate::Error::Json)?,
                timestamp: Instant::now(),
            });
        }
        Ok(())
    }

    /// Limpia el cache manualmente.
    pub async fn clear_cache(&self) {
        let mut guard = self.cache.write().await;
        *guard = None;
    }

    /// Fuerza refrescar el cache leyendo desde disco.
    pub async fn refresh_cache_from_file(&self) -> crate::Result<()> {
        let config_path = self.config_path()?;

        // Si el archivo no existe, crearlo con una configuración por defecto
        if !config_path.exists() {
            let _write_guard = self.write_lock.lock().await;
            if !config_path.exists() {
                self.create_default_config().await?;
            }
        }

        let content = tokio::fs::read_to_string(&config_path).await.map_err(|e| {
            crate::Error::Io(std::io::Error::new(
                e.kind(),
                format!(
                    "Failed to read config file {}: {}",
                    config_path.display(),
                    e
                ),
            ))
        })?;
        let mut guard = self.cache.write().await;
        *guard = Some(CacheEntry {
            content,
            timestamp: Instant::now(),
        });
        Ok(())
    }

    /// Crea el archivo de configuración con valores por defecto.
    async fn create_default_config(&self) -> crate::Result<()> {
        let config_path = self.config_path()?;

        // Crear el directorio padre si no existe
        if let Some(parent) = config_path.parent() {
            tokio::fs::create_dir_all(parent).await.map_err(|e| {
                crate::Error::Io(std::io::Error::new(
                    e.kind(),
                    format!(
                        "Failed to create config directory {}: {}",
                        parent.display(),
                        e
                    ),
                ))
            })?;
        }

        // Configuración por defecto
        let default_config = VSKConfig {
            style: Style {
                darkmode: false,
                color_scheme: "vasak-default".to_string(),
                radius: 8,
            },
            desktop: Some(Desktop {
                wallpaper: vec![],
                iconsize: 48,
                showfiles: true,
                showhiddenfiles: false,
            }),
            fonts: Fonts {
                terminal: String::new(),
                title: String::new(),
                apps: String::new(),
            },
            icons: Icons {
                dark: String::new(),
                light: String::new(),
            },
        };

        let config_content =
            serde_json::to_string_pretty(&default_config).map_err(|e| crate::Error::Json(e))?;

        Self::write_file_atomically(config_path.as_path(), &config_content).await?;

        Ok(())
    }

    /// Busca y carga todos los esquemas JSON desde /usr/share/vasak-schemes y ~/.config/vasak/schemes
    pub async fn load_schemes(&self) -> crate::Result<Vec<Scheme>> {
        let mut schemes = Vec::new();
        let paths = Self::effective_scheme_paths()?;

        // Crear directorios si no existen
        for path in &paths {
            if let Err(e) = tokio::fs::create_dir_all(path).await {
                eprintln!(
                    "[ConfigManager::load_schemes] Could not ensure schemes directory {}: {}",
                    path.display(),
                    e
                );
            }
        }

        // Buscar esquemas en las rutas efectivas.
        for path in &paths {
            if let Ok(mut entries) = tokio::fs::read_dir(path).await {
                while let Ok(Some(entry)) = entries.next_entry().await {
                    if let Ok(metadata) = entry.metadata().await {
                        if metadata.is_file() {
                            if let Some(filename) = entry.file_name().to_str() {
                                if filename.ends_with(".json") {
                                    let file_path = entry.path();
                                    if let Ok(content) = tokio::fs::read_to_string(&file_path).await
                                    {
                                        match serde_json::from_str::<SchemeData>(&content) {
                                            Ok(scheme_data) => {
                                                schemes.push(Scheme {
                                                    path: file_path.to_string_lossy().to_string(),
                                                    scheme: scheme_data,
                                                });
                                            }
                                            Err(e) => {
                                                eprintln!(
                                                    "[ConfigManager::load_schemes] Invalid scheme JSON in {}: {}",
                                                    file_path.display(),
                                                    e
                                                );
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                eprintln!(
                    "[ConfigManager::load_schemes] Could not read schemes directory {}",
                    path.display()
                );
            }
        }

        Ok(schemes)
    }

    /// Obtiene un esquema específico por su ID.
    /// Prioridad:
    /// 1) orden de VASAK_SCHEMES_PATHS (si existe)
    /// 2) orden por defecto: ~/.config/vasak/schemes y luego /usr/share/vasak-schemes
    pub async fn get_scheme_by_id(&self, scheme_id: &str) -> crate::Result<Option<Scheme>> {
        let schemes = self.load_schemes().await?;
        let preferred_paths = Self::effective_scheme_paths()?;

        // Buscar esquemas que coincidan con el ID
        let matching_schemes: Vec<Scheme> = schemes
            .into_iter()
            .filter(|s| s.scheme.id == scheme_id)
            .collect();

        if matching_schemes.is_empty() {
            return Ok(None);
        }

        for preferred in preferred_paths {
            let preferred_prefix = preferred.to_string_lossy().to_string();
            for scheme in &matching_schemes {
                if scheme.path.starts_with(&preferred_prefix) {
                    return Ok(Some(scheme.clone()));
                }
            }
        }

        // Fallback por seguridad.
        Ok(matching_schemes.into_iter().next())
    }
}