diff --git a/Source/LibationWinForms/BookLiberation/DownloadForm.Designer.cs b/Source/LibationWinForms/BookLiberation/DownloadForm.Designer.cs deleted file mode 100644 index b6746fe5..00000000 --- a/Source/LibationWinForms/BookLiberation/DownloadForm.Designer.cs +++ /dev/null @@ -1,109 +0,0 @@ -using DataLayer; -using System; - -namespace LibationWinForms.BookLiberation -{ - partial class DownloadForm - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.filenameLbl = new System.Windows.Forms.Label(); - this.progressBar1 = new System.Windows.Forms.ProgressBar(); - this.progressLbl = new System.Windows.Forms.Label(); - this.lastUpdateLbl = new System.Windows.Forms.Label(); - this.SuspendLayout(); - // - // filenameLbl - // - this.filenameLbl.AutoSize = true; - this.filenameLbl.Location = new System.Drawing.Point(12, 9); - this.filenameLbl.Name = "filenameLbl"; - this.filenameLbl.Size = new System.Drawing.Size(52, 13); - this.filenameLbl.TabIndex = 0; - this.filenameLbl.Text = "[filename]"; - // - // progressBar1 - // - this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.progressBar1.Location = new System.Drawing.Point(15, 67); - this.progressBar1.Name = "progressBar1"; - this.progressBar1.Size = new System.Drawing.Size(877, 23); - this.progressBar1.TabIndex = 4; - // - // progressLbl - // - this.progressLbl.Location = new System.Drawing.Point(12, 36); - this.progressLbl.Name = "progressLbl"; - this.progressLbl.Size = new System.Drawing.Size(173, 13); - this.progressLbl.TabIndex = 5; - this.progressLbl.Text = "[2,999,999,999] of [2,999,999,999]"; - this.progressLbl.TextAlign = System.Drawing.ContentAlignment.TopRight; - // - // lastUpdateLbl - // - this.lastUpdateLbl.AutoSize = true; - this.lastUpdateLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.lastUpdateLbl.ForeColor = System.Drawing.Color.DarkRed; - this.lastUpdateLbl.Location = new System.Drawing.Point(361, 36); - this.lastUpdateLbl.Name = "lastUpdateLbl"; - this.lastUpdateLbl.Size = new System.Drawing.Size(81, 13); - this.lastUpdateLbl.TabIndex = 6; - this.lastUpdateLbl.Text = "Last updated"; - this.lastUpdateLbl.Visible = false; - // - // DownloadForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(904, 102); - this.Controls.Add(this.lastUpdateLbl); - this.Controls.Add(this.progressLbl); - this.Controls.Add(this.progressBar1); - this.Controls.Add(this.filenameLbl); - this.MaximizeBox = false; - this.MinimizeBox = false; - this.Name = "DownloadForm"; - this.ShowIcon = false; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; - this.Text = "Downloading"; - this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DownloadForm_FormClosing); - this.Load += new System.EventHandler(this.DownloadForm_Load); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - - #endregion - - private System.Windows.Forms.Label filenameLbl; - private System.Windows.Forms.ProgressBar progressBar1; - private System.Windows.Forms.Label progressLbl; - private System.Windows.Forms.Label lastUpdateLbl; - } -} \ No newline at end of file diff --git a/Source/LibationWinForms/BookLiberation/DownloadForm.cs b/Source/LibationWinForms/BookLiberation/DownloadForm.cs deleted file mode 100644 index 77d6058f..00000000 --- a/Source/LibationWinForms/BookLiberation/DownloadForm.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Windows.Forms; -using Dinah.Core.Net.Http; -using Dinah.Core.Threading; -using FileLiberator; - -namespace LibationWinForms.BookLiberation -{ - public partial class DownloadForm : Form - { - protected Streamable Streamable { get; private set; } - protected LogMe LogMe { get; private set; } - private SynchronizeInvoker Invoker { get; init; } - - public DownloadForm() - { - //SynchronizationContext.Current will be null until the process contains a Form. - //If this is the first form created, it will not exist until after execution - //reaches inside the constructor (after base class has been initialized). - Invoker = new SynchronizeInvoker(); - InitializeComponent(); - - this.SetLibationIcon(); - progressLbl.Text = ""; - filenameLbl.Text = ""; - } - - public void RegisterFileLiberator(Streamable streamable, LogMe logMe = null) - { - if (streamable is null) return; - streamable.StreamingBegin += Streamable_StreamingBegin; - streamable.StreamingProgressChanged += Streamable_StreamingProgressChanged; - streamable.StreamingCompleted += (_, _) => this.UIThreadAsync(Close); - Streamable = streamable; - LogMe = logMe; - } - - - #region Streamable event handler overrides - public void Streamable_StreamingBegin(object sender, string beginString) - { - Invoker.UIThreadAsync(Show); - filenameLbl.UIThreadAsync(() => filenameLbl.Text = beginString); - } - public void Streamable_StreamingProgressChanged(object sender, DownloadProgress downloadProgress) - { - // this won't happen with download file. it will happen with download string - if (!downloadProgress.TotalBytesToReceive.HasValue || downloadProgress.TotalBytesToReceive.Value <= 0) - return; - - progressLbl.UIThreadAsync(() => progressLbl.Text = $"{downloadProgress.BytesReceived:#,##0} of {downloadProgress.TotalBytesToReceive.Value:#,##0}"); - - var d = double.Parse(downloadProgress.BytesReceived.ToString()) / double.Parse(downloadProgress.TotalBytesToReceive.Value.ToString()) * 100.0; - var i = int.Parse(Math.Truncate(d).ToString()); - progressBar1.UIThreadAsync(() => progressBar1.Value = i); - - lastDownloadProgress = DateTime.Now; - } - #endregion - - #region timer - private Timer timer { get; } = new Timer { Interval = 1000 }; - private void DownloadForm_Load(object sender, EventArgs e) - { - timer.Tick += new EventHandler(timer_Tick); - timer.Start(); - } - private DateTime lastDownloadProgress = DateTime.Now; - private void timer_Tick(object sender, EventArgs e) - { - // if no update in the last 30 seconds, display frozen label - lastUpdateLbl.UIThreadAsync(() => lastUpdateLbl.Visible = lastDownloadProgress.AddSeconds(30) < DateTime.Now); - if (lastUpdateLbl.Visible) - { - var diff = DateTime.Now - lastDownloadProgress; - var min = (int)diff.TotalMinutes; - var minText = min > 0 ? $"{min}min " : ""; - - lastUpdateLbl.UIThreadAsync(() => lastUpdateLbl.Text = $"Frozen? Last download activity: {minText}{diff.Seconds}sec ago"); - } - } - private void DownloadForm_FormClosing(object sender, FormClosingEventArgs e) => timer.Stop(); - #endregion - } -} diff --git a/Source/LibationWinForms/BookLiberation/DownloadForm.resx b/Source/LibationWinForms/BookLiberation/DownloadForm.resx deleted file mode 100644 index e8ae276d..00000000 --- a/Source/LibationWinForms/BookLiberation/DownloadForm.resx +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/Source/LibationWinForms/BookLiberation/ILogForm.cs b/Source/LibationWinForms/ProcessQueue/ILogForm.cs similarity index 67% rename from Source/LibationWinForms/BookLiberation/ILogForm.cs rename to Source/LibationWinForms/ProcessQueue/ILogForm.cs index ae3b27e6..10b341b5 100644 --- a/Source/LibationWinForms/BookLiberation/ILogForm.cs +++ b/Source/LibationWinForms/ProcessQueue/ILogForm.cs @@ -1,6 +1,6 @@ using System; -namespace LibationWinForms.BookLiberation +namespace LibationWinForms.ProcessQueue { public interface ILogForm { diff --git a/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/Source/LibationWinForms/ProcessQueue/LogMe.cs similarity index 55% rename from Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs rename to Source/LibationWinForms/ProcessQueue/LogMe.cs index 87917db0..b6d61f69 100644 --- a/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/Source/LibationWinForms/ProcessQueue/LogMe.cs @@ -1,11 +1,10 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; -using System.Windows.Forms; -using FileLiberator; -namespace LibationWinForms.BookLiberation +namespace LibationWinForms.ProcessQueue { // decouple serilog and form. include convenience factory method public class LogMe @@ -45,28 +44,4 @@ namespace LibationWinForms.BookLiberation public void Error(string text) => LogErrorString?.Invoke(this, text); public void Error(Exception ex, string text = null) => LogError?.Invoke(this, (ex, text)); } - - public static class ProcessorAutomationController - { - public static void DownloadFile(string url, string destination, bool showDownloadCompletedDialog = false) - { - Serilog.Log.Logger.Information($"Begin {nameof(DownloadFile)} for {url}"); - - void onDownloadFileStreamingCompleted(object sender, string savedFile) - { - Serilog.Log.Logger.Information($"Completed {nameof(DownloadFile)} for {url}. Saved to {savedFile}"); - - if (showDownloadCompletedDialog) - MessageBox.Show($"File downloaded to:{Environment.NewLine}{Environment.NewLine}{savedFile}"); - } - - var downloadFile = new DownloadFile(); - var downloadForm = new DownloadForm(); - downloadForm.RegisterFileLiberator(downloadFile); - downloadFile.StreamingCompleted += onDownloadFileStreamingCompleted; - - async void runDownload() => await downloadFile.PerformDownloadFileAsync(url, destination); - new Task(runDownload).Start(); - } - } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBook.cs b/Source/LibationWinForms/ProcessQueue/ProcessBook.cs index 75cb21e9..f84db515 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBook.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessBook.cs @@ -2,7 +2,6 @@ using Dinah.Core; using FileLiberator; using LibationFileManager; -using LibationWinForms.BookLiberation; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs index 98b7c01a..88c309a3 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs @@ -1,5 +1,4 @@ -using LibationWinForms.BookLiberation; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; diff --git a/Source/LibationWinForms/Program.cs b/Source/LibationWinForms/Program.cs index f02df0ba..5e186a6f 100644 --- a/Source/LibationWinForms/Program.cs +++ b/Source/LibationWinForms/Program.cs @@ -188,18 +188,25 @@ namespace LibationWinForms return; } - var result = MessageBox.Show($"New version available @ {htmlUrl}\r\nDownload the zip file?", "New version available", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + var result = MessageBox.Show($"New version available @ {htmlUrl}\r\n\r\nWould you like to upgrade?", "New version available", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result != DialogResult.Yes) return; try { - using var fileSelector = new SaveFileDialog { FileName = zipName, Filter = "Zip Files (*.zip)|*.zip|All files (*.*)|*.*" }; - if (fileSelector.ShowDialog() != DialogResult.OK) - return; - var selectedPath = fileSelector.FileName; + //Download the upgrader app from github + string fileName = "ht" + "tps://github.com/Mbucari/Upgrader/raw/master/Upgrader/win-x86/Upgrader.exe"; + var cli = new System.Net.Http.HttpClient(); + var upgrader = cli.GetByteArrayAsync(fileName).GetAwaiter().GetResult(); - BookLiberation.ProcessorAutomationController.DownloadFile(zipUrl, selectedPath, true); + + string upgraderPath = Path.Combine(Path.GetTempPath(), "Upgrader.exe"); + File.WriteAllBytes(upgraderPath, upgrader); + var thisExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; + + //usage is Upgrader.exe [zip url] [extract directory] [exe to launch] + System.Diagnostics.Process.Start(upgraderPath, new string[] { zipUrl, Path.GetDirectoryName(thisExe), "Libation.exe" }); + Environment.Exit(0); } catch (Exception ex) {