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.DownloadStarted += started; DownloadManager.DownloadCompleted += completed; } public void OnSkinLoad() { SetupUI(); } public bool OnUnload() { DownloadManager.DownloadStarted -= started; DownloadManager.DownloadCompleted -= completed; return true; } public void OnUpgrade() { } public void SetupUI() { fllist.Controls.Clear(); int heightMultiplier = 0; for(int i = 0; i < DownloadManager.Downloads.Length; i++) { var dctrl = new DownloadControl(i); if(heightMultiplier < 10) { heightMultiplier++; } fllist.Controls.Add(dctrl); dctrl.Show(); } if (heightMultiplier == 0) heightMultiplier = 1; this.ParentForm.Height = 150 * 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 = (int)((float)(i / down.Bytes.Length) * 100); ProgressUpdate?.Invoke(_downloads.IndexOf(down), (int)((float)(i / down.Bytes.Length) * 100)); } 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; } } }