private void OKBtn_Click(object sender, EventArgs e) { try { bool updatesAvailable = false; foreach (ListViewItem it in listView1.Items) { GXAddIn addIn = it.Tag as GXAddIn; if (addIn.State == AddInStates.Available || addIn.State == AddInStates.Update) { updatesAvailable = true; } //If user has select items to download do not notify. else if (addIn.State == AddInStates.Download) { updatesAvailable = false; break; } } DialogResult res = DialogResult.Yes; if (updatesAvailable) { res = MessageBox.Show(this, Resources.NewProtocolsDownloadTxt, Resources.NewProtocolsAvailableTxt, MessageBoxButtons.YesNoCancel); if (res != DialogResult.Yes) { this.DialogResult = DialogResult.OK; this.Close(); return; } } //Add downloadable items to the list or they are not shown anymore. foreach (ListViewItem it in listView1.Items) { GXAddIn addIn = it.Tag as GXAddIn; if (addIn.State == AddInStates.Available || addIn.State == AddInStates.Download) { if (addIn.State == AddInStates.Available) { if (res == DialogResult.Yes) { addIn.State = AddInStates.Download; } else if (res == DialogResult.No) { addIn.State = AddInStates.Disabled; } } //Add new Addins. bool exists = false; foreach (var a in AddIns) { if (a.Name == addIn.Name) { exists = true; break; } } if (!exists) { AddIns.Add(addIn); } } } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; settings.CloseOutput = true; settings.CheckCharacters = false; string path = System.IO.Path.Combine(GXCommon.ProtocolAddInsPath, "updates.xml"); DataContractSerializer x = new DataContractSerializer(typeof(GXAddInList)); using (XmlWriter writer = XmlWriter.Create(path, settings)) { x.WriteObject(writer, AddIns); writer.Close(); } Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(path); GXUpdateChecker updater = new GXUpdateChecker(); updater.OnProgress += new GXUpdateChecker.ProgressEventHandler(updater_OnProgress); Status = updater.UpdateProtocols(); updater.UpdateApplications(); this.DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { GXCommon.ShowError(this, ex); } }
ProtocolUpdateStatus Update(bool protocols) { lock (m_sync) { string backupPath = Path.Combine(GXCommon.ProtocolAddInsPath, "backup"); if (!System.IO.Directory.Exists(backupPath)) { System.IO.Directory.CreateDirectory(backupPath); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(backupPath); } DataContractSerializer x = new DataContractSerializer(typeof(GXAddInList)); GXAddInList localAddins; string path = Path.Combine(GXCommon.ProtocolAddInsPath, "updates.xml"); ProtocolUpdateStatus status = ProtocolUpdateStatus.None; if (!System.IO.File.Exists(path)) { return(status); } using (FileStream reader = new FileStream(path, FileMode.Open)) { localAddins = (GXAddInList)x.ReadObject(reader); } System.Net.WebClient client = new System.Net.WebClient(); client.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadDataCompleted += new System.Net.DownloadDataCompletedEventHandler(client_DownloadDataCompleted); foreach (GXAddIn it in localAddins) { if ((protocols && it.Type != GXAddInType.AddIn) || (!protocols && it.Type != GXAddInType.Application)) { continue; } if (it.State == AddInStates.Download || it.State == AddInStates.Update) { string AddInPath = Path.Combine(GXCommon.ProtocolAddInsPath, it.File); if (it.Type == GXAddInType.AddIn || it.Type == GXAddInType.None) { AddInPath = GXCommon.ProtocolAddInsPath; } else if (it.Type == GXAddInType.Application) { AddInPath = Path.GetDirectoryName(typeof(GXUpdateChecker).Assembly.Location); } else { throw new Exception(Resources.UnknownType + it.Type.ToString()); } try { string ext = Path.GetExtension(it.File); if (string.Compare(ext, ".zip", true) == 0 || string.Compare(ext, ".msi", true) == 0) { Target = it; string tmpPath = Path.Combine(System.IO.Path.GetTempPath(), it.File); Downloaded.Reset(); if (string.Compare(ext, ".zip", true) == 0) { client.DownloadDataAsync(new Uri("http://www.gurux.org/updates/" + it.File), tmpPath); } else //If .msi { client.DownloadDataAsync(new Uri("http://www.gurux.org/Downloads/" + it.File), tmpPath); } while (!Downloaded.WaitOne(100)) { Application.DoEvents(); } if (string.Compare(ext, ".zip", true) == 0) { ZipInputStream s = new ZipInputStream(File.OpenRead(tmpPath)); ZipEntry theEntry; byte[] data = new byte[2000]; //2MB buffer while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsFile) { string FileName = Path.Combine(AddInPath, Path.GetFileName(theEntry.Name)); int size; if (File.Exists(FileName)) { status |= ProtocolUpdateStatus.Restart; FileName = Path.Combine(Path.GetDirectoryName(FileName), "cached"); if (!Directory.Exists(FileName)) { Directory.CreateDirectory(FileName); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(FileName); } FileName = Path.Combine(FileName, Path.GetFileName(theEntry.Name)); } if (File.Exists(FileName)) { File.Delete(FileName); } using (BinaryWriter b = new BinaryWriter(File.Create(FileName))) { do { size = s.Read(data, 0, 2000); if (size > 0) { b.Write(data, 0, size); } else { b.Close(); } }while (size > 0); ext = Path.GetExtension(FileName); if (string.Compare(ext, ".dll", true) == 0 || string.Compare(ext, ".exe", true) == 0) { System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(FileName); System.Diagnostics.FileVersionInfo newVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(asm.Location); it.Version = it.InstalledVersion = newVersion.FileVersion; } } Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(FileName); } } } else //If .msi { Process msi = new Process(); msi.StartInfo.FileName = "msiexec.exe"; msi.StartInfo.Arguments = "/i \"" + tmpPath + "\" /qr"; msi.Start(); } } else { AddInPath = Path.Combine(AddInPath, it.File); System.IO.File.WriteAllBytes(AddInPath, client.DownloadData("http://www.gurux.org/updates/" + it.File)); Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(AddInPath); System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(AddInPath); System.Diagnostics.FileVersionInfo newVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(asm.Location); it.Version = it.InstalledVersion = newVersion.FileVersion; } } //If file is in use. catch (System.IO.IOException) { string cachedPath = Path.Combine(GXCommon.ProtocolAddInsPath, "cached"); if (!Directory.Exists(cachedPath)) { Directory.CreateDirectory(cachedPath); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(cachedPath); } cachedPath = Path.Combine(cachedPath, it.File); System.IO.File.WriteAllBytes(cachedPath, client.DownloadData("http://www.gurux.org/updates/" + it.File)); Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(cachedPath); AppDomain domain = AppDomain.CreateDomain("import", null, AppDomain.CurrentDomain.SetupInformation); //Get version number and unload assmbly. System.Reflection.Assembly asm = domain.Load(System.Reflection.AssemblyName.GetAssemblyName(cachedPath)); System.Diagnostics.FileVersionInfo newVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(asm.Location); it.Version = it.InstalledVersion = newVersion.FileVersion; AppDomain.Unload(domain); status |= ProtocolUpdateStatus.Restart; } status |= ProtocolUpdateStatus.Changed; it.State = AddInStates.Installed; } } if ((status & ProtocolUpdateStatus.Changed) != 0) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; settings.CloseOutput = true; settings.CheckCharacters = false; using (XmlWriter writer = XmlWriter.Create(path, settings)) { x.WriteObject(writer, localAddins); writer.Close(); } Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(path); //TODO: GXDeviceList.UpdateProtocols(); } return(status); } }