CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
340 lines
11 KiB
C#
340 lines
11 KiB
C#
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using Chronos.Core.Diagnostics;
|
|
using Chronos.Core.Localization;
|
|
using Chronos.Ui.Interop;
|
|
using Chronos.Ui.Mvvm;
|
|
using Chronos.Ui.Theming;
|
|
using Chronos.Uploader.Infrastructure;
|
|
using Chronos.Uploader.Server;
|
|
using Chronos.Uploader.Shell;
|
|
using Chronos.Uploader.Uploads;
|
|
using Chronos.Uploader.Watching;
|
|
|
|
namespace Chronos.Uploader;
|
|
|
|
/// <summary>
|
|
/// Chronos Uploader (BRIEF §15): one instance per session, logging, language and theme, the persisted queue, the
|
|
/// server session, watch folders and the tray icon. <c>--capture <dir></c> renders screenshots and exits.
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
private const int TrayShow = 1;
|
|
private const int TrayPause = 2;
|
|
private const int TrayExit = 3;
|
|
|
|
private readonly CoalescingWriter _writer = new();
|
|
private FileLogSink? _log;
|
|
private SingleInstance? _instance;
|
|
private UploaderEnvironment? _environment;
|
|
private UploaderSettings? _settings;
|
|
private UploadQueue? _queue;
|
|
private ProcessedFileMemory? _memory;
|
|
private ServerSession? _session;
|
|
private UploadEngine? _engine;
|
|
private WatchFolderService? _watch;
|
|
private DiscoveryListener? _discovery;
|
|
private MainViewModel? _vm;
|
|
private Shell.MainWindow? _window;
|
|
private TrayIcon? _tray;
|
|
private DispatcherTimer? _persist;
|
|
private bool _queueDirty;
|
|
private bool _exiting;
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
var command = CommandLine.Parse(e.Args);
|
|
_environment = UploaderEnvironment.Detect();
|
|
_log = new FileLogSink(_environment.LogDirectory, "uploader");
|
|
Log.SetSink(_log);
|
|
Log.Info("app", $"Chronos Uploader {typeof(App).Assembly.GetName().Version} starting; data at {_environment.DataDirectory}");
|
|
HookUnhandledExceptions();
|
|
|
|
if (command.CaptureDirectory is { } captureDirectory)
|
|
{
|
|
RunCapture(captureDirectory);
|
|
return;
|
|
}
|
|
|
|
_instance = SingleInstance.TryAcquire();
|
|
if (_instance is null)
|
|
{
|
|
SingleInstance.HandOver([.. command.Paths.Select(Absolute).OfType<string>()]);
|
|
Log.Info("app", "Another uploader runs in this session; arguments handed over");
|
|
Shutdown();
|
|
return;
|
|
}
|
|
|
|
_settings = UploaderSettings.Load(_environment.SettingsPath);
|
|
ApplyLanguage(_settings.Language);
|
|
ThemeManager.Initialize(this, _settings.Theme);
|
|
BuildServices(_environment, _settings);
|
|
|
|
_instance.Listen(args => Dispatcher.BeginInvoke(() => OnHandOver(args)));
|
|
_session!.Start();
|
|
_engine!.Start();
|
|
_vm!.Start();
|
|
_vm.StartWatching();
|
|
UpdateTray();
|
|
UpdateDiscovery();
|
|
if (command.Paths.Count > 0) _ = _vm.EnqueueAsync(command.Paths);
|
|
|
|
if (command.StartInTray && _settings.MinimizeToTray) return;
|
|
if (command.StartInTray) _window!.WindowState = WindowState.Minimized;
|
|
_window!.Show();
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
_persist?.Stop();
|
|
_vm?.Dispose();
|
|
// Cancels transfers; interrupted items are written as queued and resume from the server's chunks.
|
|
_engine?.Dispose();
|
|
if (_queue is not null && _environment is not null)
|
|
{
|
|
Persist();
|
|
_writer.Flush(TimeSpan.FromSeconds(10));
|
|
}
|
|
if (_window is not null && _settings is not null) _window.SavePlacement();
|
|
SaveSettings();
|
|
_watch?.Dispose();
|
|
_discovery?.Dispose();
|
|
_session?.Dispose();
|
|
_tray?.Dispose();
|
|
_instance?.Dispose();
|
|
Log.Info("app", "Chronos Uploader exiting");
|
|
_log?.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
|
|
private void BuildServices(UploaderEnvironment environment, UploaderSettings settings)
|
|
{
|
|
_queue = new UploadQueue();
|
|
QueueStore.Load(environment.QueuePath, _queue);
|
|
_memory = ProcessedFileMemory.Load(environment.WatchMemoryPath);
|
|
_session = new ServerSession(settings, SaveSettings);
|
|
_engine = new UploadEngine(_queue, _session, () => settings.DeleteAfterUpload);
|
|
_watch = new WatchFolderService(_memory, _queue.ContainsPath);
|
|
var startup = new RunKeyStartupRegistration();
|
|
startup.Refresh();
|
|
var dialogs = new UploaderDialogs();
|
|
_vm = new MainViewModel(new UploaderServices(settings, SaveSettings, _session, _queue, _engine, startup, dialogs, _watch, _memory, environment.LogDirectory));
|
|
_window = new Shell.MainWindow(_vm, settings);
|
|
dialogs.Owner = _window;
|
|
MainWindow = _window;
|
|
|
|
AsyncRelayCommand.Failed += _vm.ReportError;
|
|
_engine.ItemConfirmed += (_, item) =>
|
|
{
|
|
if (item.WatchRoot is { } root) _memory.Add(root, item.Path, item.Size, item.ModifiedUtc);
|
|
};
|
|
_watch.FilesReady += _vm.OnWatchFilesReady;
|
|
_watch.AvailabilityChanged += _vm.OnWatchAvailabilityChanged;
|
|
_queue.Changed += (_, _) => _queueDirty = true;
|
|
_vm.UploadsCompleted += OnUploadsCompleted;
|
|
_vm.PropertyChanged += OnViewModelChanged;
|
|
_window.Closing += OnWindowClosing;
|
|
_window.Closed += (_, _) => ExitApplication();
|
|
_window.StateChanged += OnWindowStateChanged;
|
|
_persist = new DispatcherTimer(DispatcherPriority.Background) { Interval = TimeSpan.FromSeconds(2) };
|
|
_persist.Tick += (_, _) => Persist();
|
|
_persist.Start();
|
|
}
|
|
|
|
/// <summary>Switches the UI language (BRIEF §16); an unknown code falls back to the OS language.</summary>
|
|
private static void ApplyLanguage(string? code)
|
|
{
|
|
CultureInfo requested;
|
|
try
|
|
{
|
|
requested = code is null ? CultureInfo.CurrentUICulture : CultureInfo.GetCultureInfo(code);
|
|
}
|
|
catch (CultureNotFoundException)
|
|
{
|
|
requested = CultureInfo.CurrentUICulture;
|
|
}
|
|
var culture = Languages.BestMatch(requested);
|
|
CultureInfo.DefaultThreadCurrentUICulture = culture;
|
|
CultureInfo.CurrentUICulture = culture;
|
|
Loc.SetCulture(culture);
|
|
}
|
|
|
|
/// <summary>Writes the queue and the watch memory when they changed (every 2 s and at exit).</summary>
|
|
private void Persist()
|
|
{
|
|
if (_queue is null || _memory is null || _environment is null) return;
|
|
if (_queueDirty)
|
|
{
|
|
_queueDirty = false;
|
|
_writer.Write(_environment.QueuePath, QueueStore.Serialize(_queue));
|
|
}
|
|
if (_memory.IsDirty) _writer.Write(_environment.WatchMemoryPath, _memory.Serialize());
|
|
}
|
|
|
|
private void SaveSettings()
|
|
{
|
|
if (_settings is null || _environment is null) return;
|
|
try
|
|
{
|
|
_settings.Save(_environment.SettingsPath);
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
Log.Warning("app", "Settings not saved", ex);
|
|
}
|
|
}
|
|
|
|
private void OnHandOver(IReadOnlyList<string> paths)
|
|
{
|
|
ShowWindow();
|
|
if (paths.Count > 0) _ = _vm!.EnqueueAsync(paths);
|
|
}
|
|
|
|
private void OnViewModelChanged(object? sender, PropertyChangedEventArgs e)
|
|
{
|
|
switch (e.PropertyName)
|
|
{
|
|
case nameof(MainViewModel.MinimizeToTray):
|
|
UpdateTray();
|
|
break;
|
|
case nameof(MainViewModel.IsConnectVisible):
|
|
UpdateDiscovery();
|
|
break;
|
|
case nameof(MainViewModel.TrayText):
|
|
_tray?.SetToolTip(_vm!.TrayText);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>The tray icon exists while "Minimise to tray" is on.</summary>
|
|
private void UpdateTray()
|
|
{
|
|
if (_settings!.MinimizeToTray && _tray is null)
|
|
{
|
|
_tray = new TrayIcon(_vm!.TrayText, TrayMenu, OnTrayCommand, ShowWindow);
|
|
}
|
|
else if (!_settings.MinimizeToTray && _tray is not null)
|
|
{
|
|
_tray.Dispose();
|
|
_tray = null;
|
|
if (_window is { IsVisible: false }) ShowWindow();
|
|
}
|
|
}
|
|
|
|
private IReadOnlyList<TrayMenuItem> TrayMenu() =>
|
|
[
|
|
new(TrayShow, Loc.L("Uploader.Tray.Show")),
|
|
new(TrayPause, _vm!.PauseLabel),
|
|
new(0, null),
|
|
new(TrayExit, Loc.L("Uploader.Tray.Exit")),
|
|
];
|
|
|
|
private void OnTrayCommand(int id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case TrayShow:
|
|
ShowWindow();
|
|
break;
|
|
case TrayPause:
|
|
_vm!.TogglePause();
|
|
break;
|
|
case TrayExit:
|
|
ExitApplication();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>Listens for server beacons only while the sign-in form is shown.</summary>
|
|
private void UpdateDiscovery()
|
|
{
|
|
if (_vm!.IsConnectVisible)
|
|
{
|
|
if (_discovery is not null) return;
|
|
_discovery = new DiscoveryListener();
|
|
_discovery.Found += _vm.Connect.AddDiscovered;
|
|
_discovery.Start();
|
|
}
|
|
else if (_discovery is not null)
|
|
{
|
|
_discovery.Dispose();
|
|
_discovery = null;
|
|
}
|
|
}
|
|
|
|
private void OnUploadsCompleted(int count)
|
|
{
|
|
if (_window is { IsVisible: true, WindowState: not WindowState.Minimized }) return;
|
|
_tray?.ShowBalloon(Loc.L("Uploader.Tray.Done.Title"), Loc.Plural("Uploader.Tray.Done.Message", count));
|
|
}
|
|
|
|
/// <summary>Closing keeps the uploader running in the tray when "Minimise to tray" is on.</summary>
|
|
private void OnWindowClosing(object? sender, CancelEventArgs e)
|
|
{
|
|
if (_exiting || !_settings!.MinimizeToTray) return;
|
|
e.Cancel = true;
|
|
_window!.SavePlacement();
|
|
_window.Hide();
|
|
}
|
|
|
|
private void OnWindowStateChanged(object? sender, EventArgs e)
|
|
{
|
|
if (_window is { WindowState: WindowState.Minimized } && _settings!.MinimizeToTray) _window.Hide();
|
|
}
|
|
|
|
private void ShowWindow()
|
|
{
|
|
if (_window is null) return;
|
|
_window.Show();
|
|
if (_window.WindowState == WindowState.Minimized) _window.WindowState = WindowState.Normal;
|
|
_window.Activate();
|
|
}
|
|
|
|
private void ExitApplication()
|
|
{
|
|
if (_exiting) return;
|
|
_exiting = true;
|
|
Shutdown();
|
|
}
|
|
|
|
private void RunCapture(string directory)
|
|
{
|
|
ThemeManager.Initialize(this, AppTheme.Dark);
|
|
ScreenCapture.Run(directory, ApplyLanguage);
|
|
}
|
|
|
|
private void HookUnhandledExceptions()
|
|
{
|
|
DispatcherUnhandledException += (_, e) =>
|
|
{
|
|
Log.Error("app", "Unhandled UI exception", e.Exception);
|
|
if (_vm is null) return;
|
|
_vm.ReportError(e.Exception);
|
|
e.Handled = true;
|
|
};
|
|
AppDomain.CurrentDomain.UnhandledException += (_, e) => Log.Error("app", "Unhandled exception", e.ExceptionObject as Exception);
|
|
TaskScheduler.UnobservedTaskException += (_, e) =>
|
|
{
|
|
Log.Error("app", "Unobserved task exception", e.Exception);
|
|
e.SetObserved();
|
|
};
|
|
}
|
|
|
|
/// <summary>Paths handed to the running instance must not depend on this process's working directory.</summary>
|
|
private static string? Absolute(string path)
|
|
{
|
|
try
|
|
{
|
|
return Path.GetFullPath(path);
|
|
}
|
|
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|