/// <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();
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the AddIns form.
        /// </summary>
        public AddInsForm(GXAddInList addIns, bool showAddins, bool onlyNew)
        {
            InitializeComponent();
            //Update resources.
            this.CancelBtn.Text = Resources.CancelTxt;
            this.NameCH.Text = Resources.NameTxt;
            this.StateCH.Text = Resources.State;
            this.InstalledCH.Text = Resources.Installed;
            this.AvailableCH.Text = Resources.Available;
            this.DownloadMnu.Text = Resources.Download;
            this.EnableMnu.Text = Resources.Enable;
            this.Text = Resources.AvailableAddIns;

            ShowAddins = showAddins;
            AddIns = addIns;
            OnlyNewItems = onlyNew;
            if (onlyNew)
            {
                this.Text = Resources.AvailableUpdatesTxt;
            }
            else
            {
                this.Text = Resources.ProtocolsTxt;
            }
            if (addIns.Count == 0)
            {
                listView1.Items.Add(Resources.FindingProtocols);
                OKBtn.Enabled = listView1.Enabled = false;
            }                                   
            foreach (GXAddIn it in addIns)
            {
                //Show only new AddIns.                
                if (onlyNew && (it.State != AddInStates.Available && it.State != AddInStates.Update))
                {
                    continue;
                }
                if (!ShowAddins && it.Type == GXAddInType.AddIn)
                {
                    continue;
                }
                //If installed protocols are shown.
                if (!onlyNew && it.Type == GXAddInType.Application)
                {
                    continue;
                }
                if (it.Type == GXAddInType.Application && string.Compare(it.Name, System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath), true) != 0)
                {
                    continue;
                }
                if (onlyNew && !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);
            }
            ThreadPool.QueueUserWorkItem(CheckUpdates, this);
        }        
 /// <summary>
 /// Show updates.
 /// </summary>
 /// <param name="owner"></param>
 /// <param name="showAddins">Are addIns show or only application updates.</param>
 /// <param name="onlyNew"></param>
 /// <param name="DisabledItems">collection of disabled addins.</param>
 /// <returns></returns>
 public static ProtocolUpdateStatus ShowUpdates(System.Windows.Forms.IWin32Window owner, bool showAddins, bool onlyNew, out string[] DisabledItems)
 {
     GXAddInList localAddins;
     string path = Path.Combine(GXCommon.ProtocolAddInsPath, "updates.xml");
     DataContractSerializer x = new DataContractSerializer(typeof(GXAddInList));
     lock (m_sync)
     {
         if (!System.IO.File.Exists(path))
         {
             localAddins = new GXAddInList();
         }
         else
         {
             try
             {
                 using (FileStream reader = new FileStream(path, FileMode.Open))
                 {
                     localAddins = (GXAddInList)x.ReadObject(reader);
                 }
             }
             catch (Exception)
             {
                 try
                 {
                     File.Delete(path);
                 }
                 catch
                 {
                     //It's OK if this fails.
                 }
                 localAddins = new GXAddInList();
             }
         }
     }
     AddInsForm dlg = new AddInsForm(localAddins, showAddins, onlyNew);
     if (dlg.ShowDialog(owner) == System.Windows.Forms.DialogResult.OK)
     {
         List<string> items = new List<string>();
         foreach (GXAddIn it in localAddins)
         {
             if (it.Type == GXAddInType.AddIn && (it.State & AddInStates.Disabled) != 0)
             {
                 items.Add(it.Name);
             }
         }
         DisabledItems = items.ToArray();
         return dlg.Status;
     }
     DisabledItems = new string[0];
     return ProtocolUpdateStatus.None;
 }
 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);
         }
     }
 }