Ejemplo n.º 1
0
        /// <summary>
        /// Checks for updates.
        /// </summary>
        /// <param name="manual"><see langword="True"/> if the user requested the check, <see langword="false"/> otherwise.</param>
        private async void CheckForUpdates(bool manual = false)
        {
            IReadOnlyList <Release> releases;

            // Fetching all releases
            try
            {
                releases = await githubClient.Repository.Release.GetAll("maximmax42", "Discord-CustomRP");
            }
            catch
            {
                // If there's no internet or Github is down, do nothing, unless it's a user requested update check
                if (manual)
                {
                    MessageBox.Show(this, Strings.errorNoInternet, Strings.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }

            latestRelease = releases[0];

            string latestStr = latestRelease.TagName;

            if (latestStr == settings.ignoreVersion && !manual)
            {
                return; // The user ignored this version; this gets ignored if the user requested the update check manually, maybe they changed their mind?
            }
            Version current = VersionHelper.GetVersion(Application.ProductVersion);
            Version latest  = VersionHelper.GetVersion(latestStr);

            if (current.CompareTo(latest) < 0)                          // If update is available...
            {
                var changelogBuilder = new System.Text.StringBuilder(); // ...build the changelog...

                foreach (var release in releases)
                {
                    Version releaseVer = VersionHelper.GetVersion(release.TagName);

                    if (releaseVer.Equals(current))
                    {
                        break;
                    }

                    var releaseBodyArr = release.Body.Split("\r\n".ToCharArray());
                    var releaseBody    = "";

                    // Removing 1st ("Changes:") and 2 last lines (description to exe and zip files) from the changelog
                    for (int i = 1; i < releaseBodyArr.Length - 3; i++)
                    {
                        releaseBody += releaseBodyArr[i] + "\r\n";
                    }

                    changelogBuilder
                    .Append("<h3>" + release.Name + "</h3>")
                    .Append(CommonMarkConverter.Convert(releaseBody.Trim()));
                }

                string changelog = changelogBuilder.ToString();

                downloadUpdateToolStripMenuItem.Visible = true;                             // ...activate the "Download update" button...
                MaximizeFromTray();                                                         // ...make sure the app window is shown if it was minimized...

                var messageBox = new UpdatePrompt(current, latest, changelog).ShowDialog(); // ...and show a dialog box telling there's an update

                if (messageBox == DialogResult.Yes)
                {
                    DownloadAndInstallUpdate();
                    downloadUpdateToolStripMenuItem.Enabled = false;
                }
                else if (messageBox == DialogResult.Ignore)
                {
                    settings.ignoreVersion = latestStr;
                    Analytics.TrackEvent("Ignored an update", new Dictionary <string, string> {
                        { "Version", latestStr }
                    });
                }

                checkUpdatesToolStripMenuItem.Checked = settings.checkUpdates;

                if (!settings.checkUpdates || messageBox == DialogResult.Ignore)
                {
                    downloadUpdateToolStripMenuItem.Visible = false; // If user doesn't want update notifications, let's not bother them
                }
            }
            else if (manual) // If there's no update available and it was a user initiated update check, notify them about it
            {
                MessageBox.Show(this, Strings.noUpdatesFound, Strings.information, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }
Ejemplo n.º 2
0
        // Checking updates
        private async void CheckForUpdates()
        {
            // Fetching latest release and getting its version
            try
            {
                latestRelease = await githubClient.Repository.Release.GetLatest("maximmax42", "Discord-CustomRP");
            } catch
            {
                return; // If there's no internet or Github is down, do nothing
            }

            string latestVersion = latestRelease.TagName;

            if (latestVersion == settings.ignoreVersion)
            {
                return;                                                                                                    // The user ignored this version
            }
            Version current = new Version(Application.ProductVersion.Substring(0, Application.ProductVersion.Length - 2)); // To not deal with revision number
            Version latest  = new Version(latestVersion);

            if (current.CompareTo(latest) < 0)                                                                 // If update is available...
            {
                var changelogBuilder = new System.Text.StringBuilder();                                        // ...build the changelog...

                var releases = await githubClient.Repository.Release.GetAll("maximmax42", "Discord-CustomRP"); // Get all Releases of the app

                foreach (var release in releases)
                {
                    Version releaseVer = new Version(release.TagName);
                    if (releaseVer.Build == -1)
                    {
                        releaseVer = new Version(releaseVer.Major, releaseVer.Minor, 0);                         // Because 1.3 != 1.3.0
                    }
                    if (releaseVer.Equals(current))
                    {
                        break;
                    }

                    var releaseBody = release.Body.Trim().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    changelogBuilder.Append("<h3>" + release.Name + "</h3><ul>");

                    for (int i = 1; i < releaseBody.Length - 2; i++)
                    {
                        changelogBuilder.Append("<li>" + releaseBody[i].Substring(2) + "</li>");
                    }

                    changelogBuilder.Append("</ul>");
                }

                string changelog = changelogBuilder.ToString();

                downloadUpdateToolStripMenuItem.Visible = true;                             // ...activate the "Download update" button...
                Show();                                                                     // ...make sure the app window is shown if it was minimized...

                var messageBox = new UpdatePrompt(current, latest, changelog).ShowDialog(); // ...and show a dialog box telling there's an update

                if (messageBox == DialogResult.Yes)
                {
                    DownloadAndInstallUpdate();
                }
                else if (messageBox == DialogResult.Ignore)
                {
                    settings.ignoreVersion = latestVersion;
                }

                checkUpdatesToolStripMenuItem.Checked = settings.checkUpdates;

                if (!settings.checkUpdates || messageBox == DialogResult.Ignore)
                {
                    downloadUpdateToolStripMenuItem.Visible = false; // If user doesn't want update notifications, let's not bother them
                }
            }
        }