2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
9 using System.Threading;
12 using System.Diagnostics;
13 using System.Reflection;
16 namespace AppLimit.NetSparkle
18 public partial class NetSparkleDownloadProgress : Form
20 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
22 private String _tempName;
23 private NetSparkleAppCastItem _item;
24 private String _referencedAssembly;
25 private Sparkle _sparkle;
26 private Boolean _unattend;
28 public NetSparkleDownloadProgress(Sparkle sparkle, NetSparkleAppCastItem item, String referencedAssembly, Image appIcon, Icon windowIcon, Boolean Unattend)
30 InitializeComponent();
33 imgAppIcon.Image = appIcon;
35 if (windowIcon != null)
41 _referencedAssembly = referencedAssembly;
45 btnInstallAndReLaunch.Visible = false;
46 lblHeader.Text = lblHeader.Text.Replace("APP", item.AppName + " " + item.Version);
47 progressDownload.Maximum = 100;
48 progressDownload.Minimum = 0;
49 progressDownload.Step = 1;
52 Size = new Size(Size.Width, 107);
53 lblSecurityHint.Visible = false;
55 // get the filename of the download lin
56 String[] segments = item.DownloadLink.Split('/');
57 String fileName = segments[segments.Length - 1];
60 _tempName = Environment.ExpandEnvironmentVariables("%temp%\\" + fileName);
62 // start async download
63 WebClient Client = new WebClient();
64 Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
65 Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
67 Uri url = new Uri(item.DownloadLink);
69 Client.DownloadFileAsync(url, _tempName);
72 private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
74 progressDownload.Visible = false;
75 btnInstallAndReLaunch.Visible = true;
79 Log.Error("Update download failed ",e.Error);
80 Size = new Size(Size.Width, 137);
81 lblSecurityHint.Text = "An error occured while downloading the update. Please try again later.";
82 lblSecurityHint.Visible = true;
83 BackColor = Color.Tomato;
84 _sparkle.ReportDiagnosticMessage("Failed downloading file to: " + _tempName);
85 btnInstallAndReLaunch.Click-=btnInstallAndReLaunch_Click;
86 btnInstallAndReLaunch.Text = "Close";
87 btnInstallAndReLaunch.Click+=(s,args)=> Close();
88 btnInstallAndReLaunch.Visible = true;
93 _sparkle.ReportDiagnosticMessage("Finished downloading file to: " + _tempName);
95 // check if we have a dsa signature in appcast
96 if (_item.DSASignature == null || _item.DSASignature.Length == 0)
98 _sparkle.ReportDiagnosticMessage("No DSA check needed");
102 Boolean bDSAOk = false;
105 _sparkle.ReportDiagnosticMessage("Performing DSA check");
108 if (File.Exists(_tempName))
110 // check if the file was downloaded successfully
111 String absolutePath = Path.GetFullPath(_tempName);
112 if (!File.Exists(absolutePath))
113 throw new FileNotFoundException();
115 // get the assembly reference from which we start the update progress
116 // only from this trusted assembly the public key can be used
117 Assembly refassembly = System.Reflection.Assembly.GetEntryAssembly();
118 if (refassembly != null)
120 // Check if we found the public key in our entry assembly
121 if (NetSparkleDSAVerificator.ExistsPublicKey("NetSparkle_DSA.pub"))
123 // check the DSA Code and modify the back color
124 NetSparkleDSAVerificator dsaVerifier = new NetSparkleDSAVerificator("NetSparkle_DSA.pub");
125 bDSAOk = dsaVerifier.VerifyDSASignature(_item.DSASignature, _tempName);
132 Size = new Size(Size.Width, 137);
133 lblSecurityHint.Visible = true;
134 BackColor = Color.Tomato;
138 // Check the unattended mode
140 btnInstallAndReLaunch_Click(null, null);
143 private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
145 progressDownload.Value = e.ProgressPercentage;
148 private void btnInstallAndReLaunch_Click(object sender, EventArgs e)
153 // get the commandline
154 String cmdLine = Environment.CommandLine;
155 String workingDir = Environment.CurrentDirectory;
157 // generate the batch file path
159 String cmd = Environment.ExpandEnvironmentVariables("%temp%\\" + Guid.NewGuid() + ".cmd");
163 var extension = Path.GetExtension(_tempName).ToLower();
167 installerCMD = _tempName;
170 installerCMD = String.Format("msiexec /i \"{0}\"", _tempName);
173 MessageBox.Show("Updater not supported, please execute " + _tempName + " manually", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
174 Environment.Exit(-1);
178 // generate the batch file
179 _sparkle.ReportDiagnosticMessage("Generating MSI batch in " + Path.GetFullPath(cmd));
181 using (var write = new StreamWriter(cmd))
183 write.WriteLine(installerCMD);
184 write.WriteLine("cd " + workingDir);
185 write.WriteLine(cmdLine);
190 _sparkle.ReportDiagnosticMessage("Going to execute batch: " + cmd);
192 // start the installer helper
193 var process = new Process();
194 process.StartInfo.FileName = cmd;
195 process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
201 catch (Exception exc)
204 Log.Error("Error while launching the update", exc);
205 this.DialogResult = DialogResult.Cancel;
206 MessageBox.Show("An error occured while executing the update.", "Update failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);