コード例 #1
0
 /// <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());
         }
     }
 }
コード例 #2
0
ファイル: AddInsForm.cs プロジェクト: ivan45654/Gurux.Common
 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);
     }
 }