Exemplo n.º 1
0
        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)
                                {
                                    throw new ArgumentException("Zip is not supported");
                                }
                                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;
            }
        }
Exemplo n.º 2
0
 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);
     }
 }