Пример #1
0
        /// <summary>Show a message in a dialog box</summary>
        /// <param name="message">The message.</param>
        /// <param name="title">Title of the dialog.</param>
        /// <param name="msgType">Message type (info, warning, error, ...).</param>
        /// <param name="buttonType">Type of buttons to be shown in the dialog.</param>
        /// <param name="errorLevel">The error level.</param>
        /// <param name="masterWindow">The main window.</param>
        public int ShowMsgDialog(string message, string title, Gtk.MessageType msgType, Gtk.ButtonsType buttonType, Window masterWindow)
        {
            MessageDialog md = new Gtk.MessageDialog(masterWindow, Gtk.DialogFlags.Modal,
                                                     msgType, buttonType, message);

            md.Title          = title;
            md.WindowPosition = WindowPosition.Center;
            int result = md.Run();

            md.Cleanup();
            return(result);
        }
Пример #2
0
        /// <summary>
        /// User has requested an upgrade.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnUpgrade(object sender, EventArgs e)
        {
            try
            {
                int selIndex = GetSelIndex();
                if (selIndex >= 0)
                {
                    if (!checkbutton1.Active)
                    {
                        throw new Exception("You must agree to the license terms before upgrading.");
                    }

                    AssertInputsAreValid();

                    Upgrade[] upgradeList = oldVersions.Active ? allUpgrades : upgrades;
                    Upgrade   upgrade     = upgradeList[selIndex];
                    versionNumber = upgrade.ReleaseDate.ToString("yyyy.MM.dd.") + upgrade.IssueNumber;

                    if ((Gtk.ResponseType)ViewBase.MasterView.ShowMsgDialog("Are you sure you want to upgrade to version " + versionNumber + "?",
                                                                            "Are you sure?", MessageType.Question, ButtonsType.YesNo, window1) == Gtk.ResponseType.Yes)
                    {
                        // Write to the registration database.
                        AssertInputsAreValid();
                        try
                        {
                            WriteUpgradeRegistration(versionNumber);
                        }
                        catch (Exception err)
                        {
                            throw new Exception("Encountered an error while updating registration information. Please try again later.", err);
                        }

                        window1.GetGdkWindow().Cursor = new Gdk.Cursor(Gdk.CursorType.Watch);

                        WebClient web = new WebClient();

                        tempSetupFileName = Path.Combine(Path.GetTempPath(), "APSIMSetup.exe");

                        string sourceURL;
#if NETCOREAPP
                        upgrade.ReleaseURL = upgrade.ReleaseURL.Replace("ApsimSetup", "apsim-");
#endif
                        if (ProcessUtilities.CurrentOS.IsMac)
                        {
                            sourceURL         = Path.ChangeExtension(upgrade.ReleaseURL, "dmg");
                            tempSetupFileName = Path.ChangeExtension(tempSetupFileName, "dmg");
                        }
                        else if (ProcessUtilities.CurrentOS.IsUnix)
                        {
                            sourceURL         = System.IO.Path.ChangeExtension(upgrade.ReleaseURL, "deb");
                            tempSetupFileName = System.IO.Path.ChangeExtension(tempSetupFileName, "deb");
                        }
                        else
                        {
                            sourceURL = upgrade.ReleaseURL;
                        }

                        if (File.Exists(tempSetupFileName))
                        {
                            File.Delete(tempSetupFileName);
                        }

                        try
                        {
                            waitDlg = new Gtk.MessageDialog(window1, Gtk.DialogFlags.Modal,
                                                            Gtk.MessageType.Info, Gtk.ButtonsType.Cancel, "Downloading file. Please wait...");
                            waitDlg.Title                = "APSIM Upgrade";
                            web.DownloadFileCompleted   += Web_DownloadFileCompleted;
                            web.DownloadProgressChanged += OnDownloadProgressChanged;
                            web.DownloadFileAsync(new Uri(sourceURL), tempSetupFileName);
                            if (waitDlg.Run() == (int)ResponseType.Cancel)
                            {
                                web.CancelAsync();
                            }
                        }
                        catch (Exception err)
                        {
                            ViewBase.MasterView.ShowMsgDialog("Cannot download this release. Error message is: \r\n" + err.Message, "Error", MessageType.Error, ButtonsType.Ok, window1);
                        }
                        finally
                        {
                            if (waitDlg != null)
                            {
                                web.DownloadProgressChanged -= OnDownloadProgressChanged;
                                waitDlg.Cleanup();
                                waitDlg = null;
                            }
                            if (window1 != null && window1.GetGdkWindow() != null)
                            {
                                window1.GetGdkWindow().Cursor = null;
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }
Пример #3
0
        private void Web_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            try
            {
                Application.Invoke((_, __) =>
                {
                    if (waitDlg != null)
                    {
                        waitDlg.Cleanup();
                        waitDlg = null;
                    }
                });
                if (!e.Cancelled && !string.IsNullOrEmpty(tempSetupFileName) && versionNumber != null)
                {
                    try
                    {
                        if (e.Error != null) // On Linux, we get to this point even when errors have occurred
                        {
                            throw e.Error;
                        }

                        if (File.Exists(tempSetupFileName))
                        {
#if NETFRAMEWORK
                            // Copy the separate upgrader executable to the temp directory.
                            string sourceUpgraderFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Updater.exe");
                            string upgraderFileName       = Path.Combine(Path.GetTempPath(), "Updater.exe");

                            // Check to see if upgrader is already running for whatever reason.
                            // Kill them if found.
                            foreach (Process process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(upgraderFileName)))
                            {
                                process.Kill();
                            }

                            // Delete the old upgrader.
                            if (File.Exists(upgraderFileName))
                            {
                                File.Delete(upgraderFileName);
                            }
                            // Copy in the new upgrader.
                            File.Copy(sourceUpgraderFileName, upgraderFileName, true);

                            // Run the upgrader.
                            string ourDirectory = PathUtilities.GetApsimXDirectory();
                            string newDirectory = Path.GetFullPath(Path.Combine(ourDirectory, "..", "APSIM" + versionNumber));
                            string arguments    = StringUtilities.DQuote(ourDirectory) + " " +
                                                  StringUtilities.DQuote(newDirectory);

                            ProcessStartInfo info = new ProcessStartInfo();
                            if (ProcessUtilities.CurrentOS.IsMac)
                            {
                                info.FileName  = "mono";
                                info.Arguments = upgraderFileName + " " + arguments;
                            }
                            else
                            {
                                info.FileName  = upgraderFileName;
                                info.Arguments = arguments;
                            }
                            info.WorkingDirectory = Path.GetTempPath();
                            Process.Start(info);
#else
                            if (ProcessUtilities.CurrentOS.IsWindows)
                            {
                                // The InnoSetup installer can be run with the /upgradefrom:xxx parameter
                                // and will handle the removal of the previous version.
                                string oldVersion = new Models.Core.Simulations().ApsimVersion;
                                Process.Start(tempSetupFileName, $"/upgradefrom={oldVersion}");
                            }
                            else if (ProcessUtilities.CurrentOS.IsMac)
                            {
                                string script = Path.Combine(Path.GetTempPath(), $"apsim-upgrade-mac-{Guid.NewGuid()}.sh");
                                ReflectionUtilities.WriteResourceToFile(GetType().Assembly, "ApsimNG.Resources.Scripts.upgrade-mac.sh", script);
                                string apsimxDir = PathUtilities.GetAbsolutePath("%root%", null);
                                Process.Start("/bin/sh", $"{script} {tempSetupFileName} {apsimxDir}");
                            }
                            else
                            {
                                // Assume (Debian) Linux and hope for the best.
                                string script = Path.Combine(Path.GetTempPath(), $"apsim-upgrade-debian-{Guid.NewGuid()}.sh");
                                ReflectionUtilities.WriteResourceToFile(GetType().Assembly, "ApsimNG.Resources.Scripts.upgrade-debian.sh", script);
                                Process.Start("/bin/sh", $"{script} {tempSetupFileName}");
                            }
#endif
                            Application.Invoke((_, __) =>
                            {
                                window1.GetGdkWindow().Cursor = null;

                                // Shutdown the user interface
                                window1.Cleanup();
                                tabbedExplorerView.Close();
                            });
                        }
                    }
                    catch (Exception err)
                    {
                        Application.Invoke(delegate
                        {
                            window1.GetGdkWindow().Cursor = null;
                            ViewBase.MasterView.ShowMsgDialog(err.Message, "Installation Error", MessageType.Error, ButtonsType.Ok, window1);
                        });
                    }
                }
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }
Пример #4
0
        private void Web_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            try
            {
                if (waitDlg != null)
                {
                    waitDlg.Cleanup();
                    waitDlg = null;
                }
                if (!e.Cancelled && !string.IsNullOrEmpty(tempSetupFileName) && versionNumber != null)
                {
                    try
                    {
                        if (e.Error != null) // On Linux, we get to this point even when errors have occurred
                        {
                            throw e.Error;
                        }

                        if (File.Exists(tempSetupFileName))
                        {
                            // Copy the separate upgrader executable to the temp directory.
                            string sourceUpgraderFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Updater.exe");
                            string upgraderFileName       = Path.Combine(Path.GetTempPath(), "Updater.exe");

                            // Check to see if upgrader is already running for whatever reason.
                            // Kill them if found.
                            foreach (Process process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(upgraderFileName)))
                            {
                                process.Kill();
                            }

                            // Delete the old upgrader.
                            if (File.Exists(upgraderFileName))
                            {
                                File.Delete(upgraderFileName);
                            }
                            // Copy in the new upgrader.
                            File.Copy(sourceUpgraderFileName, upgraderFileName, true);

                            // Run the upgrader.
                            string binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            string ourDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));
                            string newDirectory = Path.GetFullPath(Path.Combine(ourDirectory, "..", "APSIM" + versionNumber));
                            string arguments    = StringUtilities.DQuote(ourDirectory) + " " +
                                                  StringUtilities.DQuote(newDirectory);

                            ProcessStartInfo info = new ProcessStartInfo();
                            if (ProcessUtilities.CurrentOS.IsMac)
                            {
                                info.FileName  = "mono";
                                info.Arguments = upgraderFileName + " " + arguments;
                            }
                            else
                            {
                                info.FileName  = upgraderFileName;
                                info.Arguments = arguments;
                            }
                            info.WorkingDirectory = Path.GetTempPath();
                            Process.Start(info);
                            window1.GetGdkWindow().Cursor = null;

                            // Shutdown the user interface
                            window1.Cleanup();
                            tabbedExplorerView.Close();
                        }
                    }
                    catch (Exception err)
                    {
                        window1.GetGdkWindow().Cursor = null;
                        Application.Invoke(delegate
                        {
                            ViewBase.MasterView.ShowMsgDialog(err.Message, "Installation Error", MessageType.Error, ButtonsType.Ok, window1);
                        });
                    }
                }
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }