private void listView1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { ListViewItem li = listView1.GetItemAt(e.X, e.Y); if (li != null) { GXAddIn addIn = li.Tag as GXAddIn; contextMenuStrip1.Enabled = true; if ((addIn.State & AddInStates.Disabled) != 0) { EnableMnu.Text = Resources.Enable; } else { EnableMnu.Text = Resources.Disable; } DownloadMnu.Visible = addIn.State == AddInStates.Update || addIn.State == AddInStates.Available || addIn.State == AddInStates.Disabled; } else { contextMenuStrip1.Enabled = false; } } }
private void EnableMnu_Click(object sender, EventArgs e) { foreach (ListViewItem it in listView1.SelectedItems) { GXAddIn addIn = it.Tag as GXAddIn; addIn.State ^= AddInStates.Disabled; UpdateState(it, addIn.State); } }
private void DownloadMnu_Click(object sender, EventArgs e) { foreach (ListViewItem it in listView1.SelectedItems) { GXAddIn addIn = it.Tag as GXAddIn; if (addIn.State == AddInStates.Available || addIn.State == AddInStates.Update || addIn.State == AddInStates.Disabled) { addIn.State = AddInStates.Download; UpdateState(it, addIn.State); } } }
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { GXAddIn w = (GXAddIn)e.Item.Tag; if (e.ColumnIndex == 1 && w.ProgressPercentage != 0)//"ProgressBar" { e.DrawBackground(); Rectangle progressBarRect = e.Bounds; double progress = w.ProgressPercentage * progressBarRect.Width; progress /= 100; progressBarRect.Width = Convert.ToInt32(progress); e.Graphics.FillRectangle(System.Drawing.SystemBrushes.ActiveCaption, progressBarRect); ControlPaint.DrawBorder3D(e.Graphics, e.Bounds); } else { e.DrawDefault = true; } }
void updater_OnProgress(GXAddIn sender) { ListViewItem target = null; foreach (ListViewItem it in listView1.Items) { GXAddIn addIn = it.Tag as GXAddIn; if (addIn.Name == sender.Name) { addIn.ProgressPercentage = sender.ProgressPercentage; target = it; break; } } if (target != null) { listView1.RedrawItems(target.Index, target.Index, false); } }
private void listView1_DoubleClick(object sender, EventArgs e) { foreach (ListViewItem it in listView1.SelectedItems) { GXAddIn addIn = it.Tag as GXAddIn; if ((addIn.State & AddInStates.Disabled) != 0) { addIn.State ^= AddInStates.Disabled; } else if (addIn.State == AddInStates.Available) { addIn.State = AddInStates.Download; } else if (addIn.State == AddInStates.Download) { addIn.State = AddInStates.Available; } else if (addIn.State == AddInStates.Installed) { addIn.State |= AddInStates.Disabled; } UpdateState(it, addIn.State); } }
/// <summary> /// Check if there are any updates available in Gurux www server. /// </summary> /// <returns>Returns true if there are any updates available.</returns> internal static GXAddInList GetUpdatesOnline(bool applicationsOnly) { lock (m_sync) { try { //Do not check updates while debugging. string path = Path.Combine(GXCommon.ProtocolAddInsPath, "updates.xml"); DataContractSerializer x = new DataContractSerializer(typeof(GXAddInList)); System.Net.WebClient client = new System.Net.WebClient(); GXAddInList onlineAddIns, localAddins; // Put the byte array into a stream and rewind it to the beginning using (MemoryStream ms = new MemoryStream(client.DownloadData("http://www.gurux.org/updates/updates.xml"))) { ms.Flush(); ms.Position = 0; onlineAddIns = (GXAddInList)x.ReadObject(ms); } GXAddInList newItems = new GXAddInList(); if (System.IO.File.Exists(path)) { using (FileStream reader = new FileStream(path, FileMode.Open)) { try { localAddins = (GXAddInList)x.ReadObject(reader); } catch { localAddins = new GXAddInList(); } } foreach (GXAddIn it in onlineAddIns) { //Check only applications updates. if (applicationsOnly && it.Type != GXAddInType.Application) { continue; } GXAddIn localAddin = localAddins.FindByName(it.Name); if (localAddin == null) { if (string.Compare(Path.GetFileNameWithoutExtension(Application.ExecutablePath), it.Name, true) != 0) { newItems.Add(it); } else { //Get version info System.Reflection.Assembly ass = System.Reflection.Assembly.GetEntryAssembly(); System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(ass.Location); it.InstalledVersion = info.FileVersion; if (it.Version == info.FileVersion) { it.State = AddInStates.Installed; } else { newItems.Add(it); } } localAddins.Add(it); } else //Compare versions. { bool newVersion = IsNewVersion(it.Version, localAddin.InstalledVersion); if ((localAddin.State & AddInStates.Disabled) == 0 && newVersion) { if (it.Type == GXAddInType.Application && string.Compare(Path.GetFileNameWithoutExtension(Application.ExecutablePath), it.Name, true) != 0) { continue; } localAddin.Version = it.Version; localAddin.State = AddInStates.Available; if (newVersion) { localAddin.State = AddInStates.Update; } newItems.Add(localAddin); } } if (localAddin != null && string.IsNullOrEmpty(it.InstalledVersion) && it.Type == GXAddInType.Application && string.Compare(Path.GetFileNameWithoutExtension(Application.ExecutablePath), it.Name, true) == 0) { //Get version info System.Reflection.Assembly ass = System.Reflection.Assembly.GetEntryAssembly(); System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(ass.Location); localAddin.InstalledVersion = info.FileVersion; if (localAddin.Version == info.FileVersion) { localAddin.State = AddInStates.Installed; } bool newVersion = IsNewVersion(it.Version, localAddin.InstalledVersion); if (newVersion) { localAddin.State = AddInStates.Update; } else { newItems.Remove(localAddin); } } } } else { newItems = localAddins = onlineAddIns; //Update product version. foreach (GXAddIn it in onlineAddIns) { if (string.Compare(Path.GetFileNameWithoutExtension(Application.ExecutablePath), it.Name, true) == 0) { //Get version info System.Reflection.Assembly ass = System.Reflection.Assembly.GetEntryAssembly(); System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(ass.Location); it.InstalledVersion = info.FileVersion; if (it.Version == info.FileVersion) { it.State = AddInStates.Installed; } break; } } } if (newItems.Count != 0) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; settings.CloseOutput = true; settings.CheckCharacters = false; string tmp = Path.GetDirectoryName(path); if (!System.IO.Directory.Exists(tmp)) { Directory.CreateDirectory(tmp); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(tmp); } using (XmlWriter writer = XmlWriter.Create(path, settings)) { x.WriteObject(writer, localAddins); writer.Close(); } Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(path); } return(newItems); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return(new GXAddInList()); } } }
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); } }
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); } }
void OnCheckUpdatesDone(GXAddInList list) { //If done first time. if (!listView1.Enabled) { listView1.Items.Clear(); OKBtn.Enabled = listView1.Enabled = true; foreach (GXAddIn it in list) { //Show only new AddIns. if (OnlyNewItems && (it.State != AddInStates.Available && it.State != AddInStates.Update)) { continue; } if (!ShowAddins && it.Type == GXAddInType.AddIn) { continue; } //If installed protocols are shown. if (!OnlyNewItems && it.Type == GXAddInType.Application) { continue; } if (it.Type == GXAddInType.Application && string.Compare(it.Name, System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath), true) != 0) { continue; } if (!GXUpdateChecker.IsNewVersion(it.Version, it.InstalledVersion)) { continue; } ListViewItem li = listView1.Items.Add(it.Name); li.SubItems.Add(it.State.ToString()); li.SubItems.Add(it.InstalledVersion); li.SubItems.Add(it.Version); li.Tag = it; UpdateState(li, it.State); } } else //Update exist items... { foreach (GXAddIn it in list) { //Show only new AddIns. if (OnlyNewItems && it.State != AddInStates.Available) { continue; } if (!ShowAddins && it.Type == GXAddInType.AddIn) { continue; } //If installed protocols are shown. if (!OnlyNewItems && it.Type == GXAddInType.Application) { continue; } if (it.Type == GXAddInType.Application && string.Compare(it.Name, System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath), true) != 0) { continue; } if (!GXUpdateChecker.IsNewVersion(it.Version, it.InstalledVersion)) { continue; } ListViewItem item = null; foreach (ListViewItem li in listView1.Items) { if (string.Compare(li.Text, it.Name, true) == 0) { item = li; break; } } if (item == null) { item = listView1.Items.Add(it.Name); item.SubItems.Add(it.InstalledVersion); item.SubItems.Add(it.Version); item.Tag = it; } else { GXAddIn t = item.Tag as GXAddIn; t.State = it.State; } UpdateState(item, it.State); } } }