Statistics
| Branch: | Revision:

root / trunk / NetSparkle / NetSparkleDownloadProgress.cs @ 049333d2

History | View | Annotate | Download (6.6 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9
using System.Threading;
10
using System.Net;
11
using System.IO;
12
using System.Diagnostics;
13
using System.Reflection;
14

    
15
namespace AppLimit.NetSparkle
16
{
17
    public partial class NetSparkleDownloadProgress : Form
18
    {
19
        private String _tempName;
20
        private NetSparkleAppCastItem _item;
21
        private String _referencedAssembly;
22
        private Sparkle _sparkle;
23
        private Boolean _unattend;
24

    
25
        public NetSparkleDownloadProgress(Sparkle sparkle, NetSparkleAppCastItem item, String referencedAssembly, Image appIcon, Icon windowIcon, Boolean Unattend)
26
        {
27
            InitializeComponent();
28

    
29
            if (appIcon != null)
30
                imgAppIcon.Image = appIcon;
31

    
32
            if (windowIcon != null)
33
                Icon = windowIcon;
34

    
35
            // store the item
36
            _sparkle = sparkle;
37
            _item = item;
38
            _referencedAssembly = referencedAssembly;
39
            _unattend = Unattend;
40

    
41
            // init ui
42
            btnInstallAndReLaunch.Visible = false;
43
            lblHeader.Text = lblHeader.Text.Replace("APP", item.AppName + " " + item.Version);
44
            progressDownload.Maximum = 100;
45
            progressDownload.Minimum = 0;
46
            progressDownload.Step = 1;
47

    
48
            // show the right 
49
            Size = new Size(Size.Width, 107);
50
            lblSecurityHint.Visible = false;                
51
            
52
            // get the filename of the download lin
53
            String[] segments = item.DownloadLink.Split('/');
54
            String fileName = segments[segments.Length - 1];
55

    
56
            // get temp path
57
            _tempName = Environment.ExpandEnvironmentVariables("%temp%\\" + fileName);
58

    
59
            // start async download
60
            WebClient Client = new WebClient();
61
            Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
62
            Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
63

    
64
            Uri url = new Uri(item.DownloadLink);
65

    
66
            Client.DownloadFileAsync(url, _tempName);
67
        }
68

    
69
        private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
70
        {
71
            progressDownload.Visible = false;
72
            btnInstallAndReLaunch.Visible = true;            
73

    
74
            // report message            
75
            _sparkle.ReportDiagnosticMessage("Finished downloading file to: " + _tempName);
76

    
77
            // check if we have a dsa signature in appcast            
78
            if (_item.DSASignature == null || _item.DSASignature.Length == 0)
79
            {
80
                _sparkle.ReportDiagnosticMessage("No DSA check needed");
81
            }
82
            else
83
            {
84
                Boolean bDSAOk = false;
85

    
86
            // report
87
            _sparkle.ReportDiagnosticMessage("Performing DSA check");
88

    
89
            // get the assembly
90
            if (File.Exists(_tempName))
91
            {
92
                // check if the file was downloaded successfully
93
                String absolutePath = Path.GetFullPath(_tempName);
94
                if (!File.Exists(absolutePath))
95
                    throw new FileNotFoundException();
96

    
97
                // get the assembly reference from which we start the update progress
98
                // only from this trusted assembly the public key can be used
99
                Assembly refassembly = System.Reflection.Assembly.GetEntryAssembly();
100
                if (refassembly != null)
101
                {
102
                    // Check if we found the public key in our entry assembly
103
                    if (NetSparkleDSAVerificator.ExistsPublicKey("NetSparkle_DSA.pub"))
104
                    {
105
                        // check the DSA Code and modify the back color            
106
                        NetSparkleDSAVerificator dsaVerifier = new NetSparkleDSAVerificator("NetSparkle_DSA.pub");
107
                        bDSAOk = dsaVerifier.VerifyDSASignature(_item.DSASignature, _tempName);
108
                    }
109
                }
110
            }
111

    
112
                if (!bDSAOk)
113
            {
114
                Size = new Size(Size.Width, 137);
115
                lblSecurityHint.Visible = true;
116
                BackColor = Color.Tomato;
117
        }
118
            }
119
               
120
            // Check the unattended mode
121
            if (_unattend)
122
                btnInstallAndReLaunch_Click(null, null);
123
        }
124
               
125
        private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
126
        {
127
            progressDownload.Value = e.ProgressPercentage;            
128
        }
129

    
130
        private void btnInstallAndReLaunch_Click(object sender, EventArgs e)
131
        {
132
            // get the commandline 
133
            String cmdLine = Environment.CommandLine;
134
            String workingDir = Environment.CurrentDirectory;
135

    
136
            // generate the batch file path
137
            String cmd = Environment.ExpandEnvironmentVariables("%temp%\\" + Guid.NewGuid() + ".cmd");
138
            String installerCMD;
139

    
140
            // get the file type
141
            if (Path.GetExtension(_tempName).ToLower().Equals(".exe"))
142
            {
143
                // build the command line 
144
                installerCMD = _tempName;
145
            }
146
            else if (Path.GetExtension(_tempName).ToLower().Equals(".msi"))
147
            {                
148
                // buid the command line
149
                installerCMD = "msiexec /i \"" + _tempName + "\"";                
150
            }
151
            else
152
            {
153
                MessageBox.Show("Updater not supported, please execute " + _tempName + " manually", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
154
                Environment.Exit(-1);
155
                return;
156
            }
157

    
158
            // generate the batch file                
159
            _sparkle.ReportDiagnosticMessage("Generating MSI batch in " + Path.GetFullPath(cmd));
160

    
161
            StreamWriter write = new StreamWriter(cmd);
162
            write.WriteLine(installerCMD);
163
            write.WriteLine("cd " + workingDir);
164
            write.WriteLine(cmdLine);
165
            write.Close();
166

    
167
            // report
168
            _sparkle.ReportDiagnosticMessage("Going to execute batch: " + cmd);
169

    
170
            // start the installer helper
171
            Process process = new Process();
172
            process.StartInfo.FileName = cmd;
173
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
174
            process.Start();
175
            
176

    
177
            // quit the app
178
            Environment.Exit(0);
179
        }
180
    }
181
}