From d5ab22629ba39ac9f0fae6768b527e82670f96fc Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 2 Feb 2017 17:11:14 -0500 Subject: [PATCH] Implement the Downloader --- ShiftOS.Server/Program.cs | 23 +++ .../Applications/Downloader.Designer.cs | 61 +++++++ ShiftOS.WinForms/Applications/Downloader.cs | 172 ++++++++++++++++++ ShiftOS.WinForms/Applications/Downloader.resx | 120 ++++++++++++ ShiftOS.WinForms/Applications/Shiftnet.cs | 53 +++++- ShiftOS.WinForms/ShiftOS.WinForms.csproj | 9 + 6 files changed, 431 insertions(+), 7 deletions(-) create mode 100644 ShiftOS.WinForms/Applications/Downloader.Designer.cs create mode 100644 ShiftOS.WinForms/Applications/Downloader.cs create mode 100644 ShiftOS.WinForms/Applications/Downloader.resx diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 138764f..c0c27ed 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -655,6 +655,29 @@ Contents: } break; + case "download_start": + if (File.Exists(msg.Contents)) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("download", new ServerMessage + { + Name = "download_meta", + GUID = "server", + Contents = JsonConvert.SerializeObject(File.ReadAllBytes(msg.Contents)) + })); + } + else + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + +The page you requested at was not found on this multi-user domain." + })); + + } + break; case "shiftnet_get": string surl = args["url"] as string; while (surl.EndsWith("/")) diff --git a/ShiftOS.WinForms/Applications/Downloader.Designer.cs b/ShiftOS.WinForms/Applications/Downloader.Designer.cs new file mode 100644 index 0000000..620e00e --- /dev/null +++ b/ShiftOS.WinForms/Applications/Downloader.Designer.cs @@ -0,0 +1,61 @@ +namespace ShiftOS.WinForms.Applications +{ + partial class Downloader + { + /// + /// 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 Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.fllist = new System.Windows.Forms.FlowLayoutPanel(); + this.SuspendLayout(); + // + // fllist + // + this.fllist.AutoScroll = true; + this.fllist.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.fllist.Dock = System.Windows.Forms.DockStyle.Fill; + this.fllist.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; + this.fllist.Location = new System.Drawing.Point(0, 0); + this.fllist.Name = "fllist"; + this.fllist.Size = new System.Drawing.Size(557, 149); + this.fllist.TabIndex = 0; + this.fllist.WrapContents = false; + // + // Downloader + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.fllist); + this.Name = "Downloader"; + this.Size = new System.Drawing.Size(557, 149); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.FlowLayoutPanel fllist; + } +} diff --git a/ShiftOS.WinForms/Applications/Downloader.cs b/ShiftOS.WinForms/Applications/Downloader.cs new file mode 100644 index 0000000..021af3e --- /dev/null +++ b/ShiftOS.WinForms/Applications/Downloader.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Threading; +using ShiftOS.Engine; +using Newtonsoft.Json; +using ShiftOS.WinForms.Controls; +using ShiftOS.WinForms.Tools; + +namespace ShiftOS.WinForms.Applications +{ + [Launcher("Downloader", false, null, "Networking")] + public partial class Downloader : UserControl, IShiftOSWindow + { + public Downloader() + { + InitializeComponent(); + } + + Action pupdate = null; + Action completed = null; + Action started = null; + + public void OnLoad() + { + SetupUI(); + pupdate = (i, o) => + { + this.Invoke(new Action(() => + { + SetupUI(); + })); + }; + started = (i) => + { + this.Invoke(new Action(() => + { + SetupUI(); + })); + }; completed = (i) => + { + this.Invoke(new Action(() => + { + SetupUI(); + })); + }; + DownloadManager.ProgressUpdate += pupdate; + DownloadManager.DownloadStarted += started; + DownloadManager.DownloadCompleted += completed; + } + + public void OnSkinLoad() + { + SetupUI(); + } + + public bool OnUnload() + { + DownloadManager.ProgressUpdate -= pupdate; + DownloadManager.DownloadStarted -= started; + DownloadManager.DownloadCompleted -= completed; + return true; + } + + public void OnUpgrade() + { + } + + public void SetupUI() + { + fllist.Controls.Clear(); + + int heightMultiplier = 0; + + foreach(var download in DownloadManager.Downloads) + { + var pnl = new Panel(); + pnl.Width = fllist.Width; + pnl.Height = 50; + var picpreview = new PictureBox(); + picpreview.Size = new Size(42, 42); + picpreview.Image = FileSkimmerBackend.GetImage(download.Destination); + picpreview.Location = new Point(4, 4); + if (heightMultiplier < 5) + heightMultiplier++; + pnl.Controls.Add(picpreview); + picpreview.Show(); + var prg = new ShiftedProgressBar(); + prg.Maximum = 100; + prg.Value = download.Progress; + prg.Width = pnl.Width - 8; + prg.Left = 4; + prg.Top = picpreview.Height + 8; + prg.Height = 20; + var lbtitle = new Label(); + lbtitle.Tag = "header1"; + lbtitle.Text = download.ShiftnetUrl; + lbtitle.Top = 4; + lbtitle.Left = 8 + picpreview.Height; + pnl.Controls.Add(lbtitle); + lbtitle.Show(); + lbtitle.AutoSize = true; + pnl.Controls.Add(prg); + prg.Show(); + + fllist.Controls.Add(pnl); + pnl.Show(); + ControlManager.SetupControls(pnl); + } + + if (heightMultiplier == 0) + heightMultiplier = 1; + + this.Parent.Height = 50 * heightMultiplier; + } + } + + public static class DownloadManager + { + public static Download[] Downloads + { + get + { + return _downloads.ToArray(); + } + } + + private static List _downloads = new List(); + + public static event Action ProgressUpdate; + + public static event Action DownloadCompleted; + + public static event Action DownloadStarted; + + public static void StartDownload(Download down) + { + var t = new Thread(() => + { + int byteWrite = 256; + _downloads.Add(down); + DownloadStarted?.Invoke(down); + for (int i = 0; i < down.Bytes.Length; i += byteWrite) + { + Thread.Sleep(1000); + _downloads[_downloads.IndexOf(down)].Progress = i / down.Bytes.Length; + ProgressUpdate?.Invoke(_downloads.IndexOf(down), i / down.Bytes.Length); + } + ShiftOS.Objects.ShiftFS.Utils.WriteAllBytes(down.Destination, down.Bytes); + _downloads.Remove(down); + DownloadCompleted?.Invoke(down.Destination); + }); + t.IsBackground = true; + t.Start(); + } + } + + public class Download + { + + public string ShiftnetUrl { get; set; } + public string Destination { get; set; } + public byte[] Bytes { get; set; } + public int Progress { get; set; } + } +} diff --git a/ShiftOS.WinForms/Applications/Downloader.resx b/ShiftOS.WinForms/Applications/Downloader.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ShiftOS.WinForms/Applications/Downloader.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ShiftOS.WinForms/Applications/Shiftnet.cs b/ShiftOS.WinForms/Applications/Shiftnet.cs index eebebd9..6f99d25 100644 --- a/ShiftOS.WinForms/Applications/Shiftnet.cs +++ b/ShiftOS.WinForms/Applications/Shiftnet.cs @@ -102,15 +102,54 @@ namespace ShiftOS.WinForms.Applications public void ShiftnetNavigate(string Url, bool pushHistory = true) { - if (!string.IsNullOrEmpty(CurrentUrl) && pushHistory) - History.Push(CurrentUrl); - CurrentUrl = Url; + - ServerManager.SendMessage("shiftnet_get", JsonConvert.SerializeObject(new + if (Url.EndsWith(".rnp") || !Url.Contains(".")) { - url = Url - })); - txturl.Text = Url; + if (!string.IsNullOrEmpty(CurrentUrl) && pushHistory) + History.Push(CurrentUrl); + CurrentUrl = Url; + ServerManager.SendMessage("shiftnet_get", JsonConvert.SerializeObject(new + { + url = Url + })); + txturl.Text = Url; + + } + else + { + ServerMessageReceived smr = null; + smr = (msg) => + { + if(msg.Name == "download_meta") + { + var bytes = JsonConvert.DeserializeObject(msg.Contents); + string destPath = null; + string ext = Url.Split('.')[Url.Split('.').Length - 1]; + + FileSkimmerBackend.GetFile(new[] { ext }, FileOpenerStyle.Save, new Action((file) => + { + destPath = file; + })); + while (string.IsNullOrEmpty(destPath)) + { + + } + var d = new Download + { + ShiftnetUrl = Url, + Destination = destPath, + Bytes = bytes, + Progress = 0, + }; + DownloadManager.StartDownload(d); + AppearanceManager.SetupWindow(new Downloader()); + ServerManager.MessageReceived -= smr; + } + }; + ServerManager.MessageReceived += smr; + ServerManager.SendMessage("download_start", Url); + } } public void OnLoad() diff --git a/ShiftOS.WinForms/ShiftOS.WinForms.csproj b/ShiftOS.WinForms/ShiftOS.WinForms.csproj index 3012488..d8b1517 100644 --- a/ShiftOS.WinForms/ShiftOS.WinForms.csproj +++ b/ShiftOS.WinForms/ShiftOS.WinForms.csproj @@ -87,6 +87,12 @@ Dialog.cs + + UserControl + + + Downloader.cs + UserControl @@ -247,6 +253,9 @@ Dialog.cs + + Downloader.cs + FileDialog.cs