diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index f933a7f..b1dc204 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -24,6 +24,9 @@ namespace Elwig { protected static App CurrentApp; public static int NumWindows => CurrentApp.Windows.Count; + public static bool ForceShutdown { get; private set; } = false; + + private readonly DispatcherTimer _autoUpdateTimer = new() { Interval = TimeSpan.FromHours(1) }; public static readonly string DataPath = @"C:\ProgramData\Elwig\"; public static readonly string ExePath = @"C:\Program Files\Elwig\"; @@ -111,10 +114,26 @@ namespace Elwig { BranchNum = branches.Count; } + Utils.RunBackground("Temp File Cleanup", () => { + Utils.CleanupTempFiles(); + return Task.CompletedTask; + }); + Utils.RunBackground("HTML Initialization", () => Html.Init(PrintingReadyChanged)); Utils.RunBackground("PDF Initialization", () => Pdf.Init(PrintingReadyChanged)); Utils.RunBackground("JSON Schema Initialization", BillingData.Init); + if (Config.UpdateAuto && Config.UpdateUrl != null) { + if (Utils.HasInternetConnectivity()) { + Utils.RunBackground("Auto Updater", async () => { + await Task.Delay(500); + await CheckForUpdates(); + }); + } + _autoUpdateTimer.Tick += new EventHandler(OnAutoUpdateTimer); + _autoUpdateTimer.Start(); + } + var list = new List(); foreach (var s in Config.Scales) { try { @@ -184,6 +203,29 @@ namespace Elwig { } } + private void OnAutoUpdateTimer(object? sender, EventArgs? evt) { + foreach (Window w in CurrentApp.Windows) { + if (w is UpdateDialog) return; + } + if (Utils.HasInternetConnectivity()) { + Utils.RunBackground("Auto Updater", CheckForUpdates); + } + } + + public static async Task CheckForUpdates() { + if (Config.UpdateUrl == null) return; + var latest = await Utils.GetLatestInstallerUrl(Config.UpdateUrl); + if (latest != null && latest.Value.Version != Version) { + await MainDispatcher.BeginInvoke(() => { + var d = new UpdateDialog(latest.Value.Version, latest.Value.Url, latest.Value.Size); + if (d.ShowDialog() == true) { + ForceShutdown = true; + Current.Shutdown(); + } + }); + } + } + private static T FocusWindow(Func constructor, Predicate? selector = null) where T : Window { foreach (Window w in CurrentApp.Windows) { if (w is T t && (selector == null || selector(t))) { diff --git a/Elwig/Dialogs/UpdateDialog.xaml b/Elwig/Dialogs/UpdateDialog.xaml new file mode 100644 index 0000000..e6153ba --- /dev/null +++ b/Elwig/Dialogs/UpdateDialog.xaml @@ -0,0 +1,33 @@ + + + + Version 0.0.0 von Elwig ist verfügbar! + Soll das Update heruntergeladen und + installiert werden? (ca. 100 MB) + Achtung: Elwig wird dabei geschlossen! + + + + +