Пример #1
0
 public override void BeginTask(string name, int totalWork)
 {
     if (dialog != null)
     {
         dialog.BeginTask(name);
     }
     base.BeginTask(name, totalWork);
 }
 protected override void OnBeginTask(string name, int totalWork, int stepWork)
 {
     if (dialog != null)
     {
         dialog.BeginTask(name);
     }
 }
        public void InstallAddin(string url)
        {
            ProgressDialog d = new ProgressDialog(IdeApp.Workbench.RootWindow, false, true);

            d.Show();
            d.Title   = "Installing MFractor - The Essential Producitivity Tool for Xamarin Studio";
            d.Message = "Installing MFractor";
            Task.Run(() => {
                Runtime.RunInMainThread(() => { d.BeginTask("Downloading MFractor For Xamarin Studio..."); });

                string downloadUrl    = url + @"/root.mrep";
                string downloadFolder = Path.Combine(DirectoryForAssembly(Assembly.GetExecutingAssembly()), ".temp");
                string mrepFilePath   = Path.Combine(downloadFolder, "root.mrep");

                if (Directory.Exists(downloadFolder))
                {
                    Directory.Delete(downloadFolder, true);
                }

                Directory.CreateDirectory(downloadFolder);

                Runtime.RunInMainThread(() => { d.WriteText("Locating MFractor addin package...\n"); });

                var webClient = new WebClient();

                webClient.DownloadFile(downloadUrl, mrepFilePath);

                string addinDownloadUrl = "";
                string addinFilePath    = "";

                try {
                    var xdoc         = XDocument.Load(mrepFilePath);
                    string addinFile = xdoc.Root.Element("Addin").Element("Url").Value;

                    addinDownloadUrl = url + "/" + addinFile;
                    addinFilePath    = Path.Combine(downloadFolder, addinFile);

                    var fi = new FileInfo(addinFilePath);
                    if (!Directory.Exists(fi.DirectoryName))
                    {
                        Directory.CreateDirectory(fi.DirectoryName);
                    }
                } catch {
                }

                if (String.IsNullOrEmpty(addinDownloadUrl))
                {
                    Runtime.RunInMainThread(() => { d.EndTask(); });
                    Runtime.RunInMainThread(() => { d.Message = "Installation failed. Please try again through the addin manager\n"; });
                    return;
                }

                Runtime.RunInMainThread(() => { d.WriteText("Downloading " + addinDownloadUrl + "...\n"); });

                webClient = new WebClient();
                webClient.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => {
                    Runtime.RunInMainThread(() => {
                        d.Progress = (double)e.ProgressPercentage / 100.0;;
                    });
                };

                webClient.DownloadFileCompleted += (object sender, System.ComponentModel.AsyncCompletedEventArgs e) => {
                    if (e.Cancelled)
                    {
                        Runtime.RunInMainThread(() => {
                            d.Message = "The download was cancelled.";
                        });

                        return;
                    }

                    if (e.Error != null)
                    {
                        Runtime.RunInMainThread(() => {
                            d.Message = "An error occurred while trying to download. Please file a bug report at https://github.com/matthewrdev/mfractor-installer";
                            d.WriteText("Error:\n" + e.Error.ToString());
                        });

                        return;
                    }

                    Runtime.RunInMainThread(() => { d.EndTask(); });

                    Runtime.RunInMainThread(() => {
                        d.BeginTask("Installing MFractor For Xamarin Studio");

                        var monitor = new InstallMonitor(d);

                        string dataPath = "";
                        if (Platform.IsWindows)
                        {
                            dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "mfractor");
                        }
                        else
                        {
                            dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "mfractor");
                        }

                        bool requiresDialog = File.Exists(Path.Combine(dataPath, ".first_run"));

                        Task.Run(() => {
                            Runtime.AddinSetupService.Install(monitor, addinFilePath);

                            Runtime.RunInMainThread(() => {
                                d.Hide();
                            });
                        });
                    });
                };

                webClient.DownloadFileAsync(new Uri(addinDownloadUrl), addinFilePath);
            });
        }