summaryrefslogtreecommitdiffstats
path: root/win/C#/frmDownload.cs
blob: 73020ff1b00863fdfaf771244b03857ccc1166db (plain)
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
146
147
148
/*  frmDownload.cs $
 	
 	   This file is part of the HandBrake source code.
 	   Homepage: <http://handbrake.m0k.org/>.
 	   It may be used under the terms of the GNU General Public License. */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace Handbrake
{
    public partial class frmDownload : Form
    {
        private Thread downloadThread;
        private Stream responceStream;
        private Stream loacalStream;
        private HttpWebRequest webRequest;
        private HttpWebResponse webResponse;
        private static int progress;
        private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
        private delegate void DownloadCompleteCallback();
        private delegate void DownloadFailedCallback();


        public frmDownload()
        {
            InitializeComponent();

            try
            {
                downloadThread = new Thread(Download);
                downloadThread.Start();
            }
            catch (Exception exc)
            {
                MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
          
        }

        private void Download()
        {
            Functions.RssReader rssRead = new Functions.RssReader();

            string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");

            if (File.Exists(tempPath))
                File.Delete(tempPath);

            string hbUpdate = rssRead.downloadFile();

            WebClient wcDownload = new WebClient();
                try
                {
                    webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    Int64 fileSize = webResponse.ContentLength;

                    responceStream = wcDownload.OpenRead(hbUpdate);
                    loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);

                    int bytesSize = 0;
                    byte[] downBuffer = new byte[2048];

                    long flength = 0;
                    while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
                    {
                        loacalStream.Write(downBuffer, 0, bytesSize);
                        flength = loacalStream.Length;
                        this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });
                    }

                    responceStream.Close();
                    loacalStream.Close();

                    if (flength != fileSize)
                        this.Invoke(new DownloadFailedCallback(this.downloadFailed));
                    else
                        this.Invoke(new DownloadCompleteCallback(this.downloadComplete));
                }
                catch (Exception)
                {
                    // Do Nothing 
                }
        }

        private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
        {
            try
            {
                long p = (BytesRead * 100) / TotalBytes;
                progress = int.Parse(p.ToString());
                progress_download.Value = progress;
                lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
            }
            catch (Exception exc)
            {
                MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void downloadComplete()
        {
            lblProgress.Text = "Download Complete";
            btn_cancel.Text = "Close";

            string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");

            Process startInstall = Process.Start(tempPath);
            this.Close();
            Application.Exit();
        }

        private void downloadFailed()
        {
            lblProgress.Text = "Download Failed";
            btn_cancel.Text = "Close";
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            try
            {
                webResponse.Close();
                responceStream.Close();
                loacalStream.Close();
                downloadThread.Abort();
                progress_download.Value = 0;
                lblProgress.Text = "Download Stopped";
                this.Close();
            }
            catch (Exception)
            {
                // Do nothing
            }
        }
    }
}