From b40786c02cd3d8b08415ccde436031910274e932 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Sat, 2 Aug 2025 00:40:03 -0500 Subject: [PATCH] Updated App Since we can't rely on Process.GetProcessByName() to work from within a sandboxed environment such as Flatpak, we instead rely on the SingletonCookie/SingletonLock/SingletonSocket to determine if the FreeTube Process is still running. --- FreeTubeSyncer/App.axaml.cs | 39 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/FreeTubeSyncer/App.axaml.cs b/FreeTubeSyncer/App.axaml.cs index bbfd8ac..68ad15a 100644 --- a/FreeTubeSyncer/App.axaml.cs +++ b/FreeTubeSyncer/App.axaml.cs @@ -41,6 +41,7 @@ public partial class App : Application private static readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); private TimeSpan _checkInterval; private DateTime _lastUpdated; + private string _freeTubeDataPath; private WindowIcon? _normalTrayIcon; private WindowIcon? _waitingTrayIcon; @@ -66,18 +67,18 @@ public partial class App : Application SetupLogger(); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - var path = ""; + _freeTubeDataPath = ""; if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) - path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FreeTube"); + _freeTubeDataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FreeTube"); else if (OperatingSystem.IsLinux()) { - path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", + _freeTubeDataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", "FreeTube"); - if (!Path.Exists(path)) + if (!Path.Exists(_freeTubeDataPath)) { - path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".var", "app", "io.freetubeapp.FreeTube", "config", + _freeTubeDataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".var", "app", "io.freetubeapp.FreeTube", "config", "FreeTube"); - if (!Path.Exists(path)) + if (!Path.Exists(_freeTubeDataPath)) Log.Error("Failed to find Path for FreeTube!"); } } @@ -85,16 +86,16 @@ public partial class App : Application LoadSettings(); _checkInterval = TimeSpan.FromSeconds(_settings!.CheckInterval); - _watcher = new DBSyncWatcher(path); - _historySyncer = new Syncer(_watcher, Path.Join(path, "history.db"), "history.db", _settings.RestBaseUrl, "/history"); + _watcher = new DBSyncWatcher(_freeTubeDataPath); + _historySyncer = new Syncer(_watcher, Path.Join(_freeTubeDataPath, "history.db"), "history.db", _settings.RestBaseUrl, "/history"); _historySyncer.SetEnabled(_settings.SyncHistory); - _playlistSyncer = new Syncer(_watcher, Path.Join(path, "playlists.db"), "playlists.db", _settings.RestBaseUrl, "/playlist"); + _playlistSyncer = new Syncer(_watcher, Path.Join(_freeTubeDataPath, "playlists.db"), "playlists.db", _settings.RestBaseUrl, "/playlist"); _playlistSyncer.SetEnabled(_settings.SyncPlaylist); - _profileSyncer = new Syncer(_watcher, Path.Join(path, "profiles.db"), "profiles.db", _settings.RestBaseUrl, "/profile"); + _profileSyncer = new Syncer(_watcher, Path.Join(_freeTubeDataPath, "profiles.db"), "profiles.db", _settings.RestBaseUrl, "/profile"); _profileSyncer.SetEnabled(_settings.SyncProfile); - _searchHistorySyncer = new Syncer(_watcher, Path.Join(path, "search-history.db"), "search-history.db", _settings.RestBaseUrl, "/searchHistory"); + _searchHistorySyncer = new Syncer(_watcher, Path.Join(_freeTubeDataPath, "search-history.db"), "search-history.db", _settings.RestBaseUrl, "/searchHistory"); _searchHistorySyncer.SetEnabled(_settings.SyncSearchHistory); - _settingSyncer = new Syncer(_watcher, Path.Join(path, "settings.db"), "settings.db", _settings.RestBaseUrl, "/settings"); + _settingSyncer = new Syncer(_watcher, Path.Join(_freeTubeDataPath, "settings.db"), "settings.db", _settings.RestBaseUrl, "/settings"); _settingSyncer.SetEnabled(_settings.SyncSettings); _syncers = [ @@ -244,11 +245,13 @@ public partial class App : Application } if (!_syncers.Any(x => x.IsDirty())) continue; + + if (File.Exists(Path.Join(_freeTubeDataPath, "SingletonCookie")) || + File.Exists(Path.Join(_freeTubeDataPath, "SingletonLock")) || + File.Exists(Path.Join(_freeTubeDataPath, "SingletonSocket"))) continue; - var procs = Process.GetProcessesByName("FreeTube"); - if (procs.Length > 0) continue; Log.Information("FreeTube instance closed, and we have writes to make..."); - await Task.Delay(1500); + await Task.Delay(2000); await UpdateSTIconHint(_waitingTrayIcon, "FreeTube Syncer - Syncing data..."); await _semaphoreSlim.WaitAsync(); @@ -339,13 +342,13 @@ public partial class App : Application desktop.MainWindow!.Show(); } - private void Quit_OnClick(object? sender, EventArgs e) + private async void Quit_OnClick(object? sender, EventArgs e) { if (App.Current!.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return; - _semaphoreSlim.Wait(); + await _semaphoreSlim.WaitAsync(); _isRunning = false; _semaphoreSlim.Release(); - _watcherTask?.Wait(); + await _watcherTask!; desktop.Shutdown(); }