aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/Downloader.cs
blob: 196866098aa03f0d86fba19d33a6c8ac02aeb3c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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<int, int> pupdate = null;
        Action<string> completed = null;
        Action<Download> 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()
        {
        }

        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<Download> _downloads = new List<Download>();

        public static event Action<int, int> ProgressUpdate;

        public static event Action<string> DownloadCompleted;

        public static event Action<Download> 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; }
    }
}