2022-07-06 07:53:38 +12:00
|
|
|
|
// <copyright file="GitHubUpdate.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Helper.Updater
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
2022-11-13 09:07:15 +13:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using SystemTrayMenu.UserInterface;
|
2022-07-06 07:53:38 +12:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
|
|
|
|
public class GitHubUpdate
|
|
|
|
|
{
|
|
|
|
|
private static List<Dictionary<string, object>> releases;
|
2022-11-13 09:07:15 +13:00
|
|
|
|
private static UpdateWindow newVersionForm;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the latest release version name .
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string LatestVersionName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string result = "Unknown";
|
|
|
|
|
|
|
|
|
|
if (releases == null)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = releases[0]["tag_name"].ToString() !.Replace("v", string.Empty); // 0 = latest
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"{nameof(LatestVersionName)} failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens the website on GitHub of the latest release version .
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void WebOpenLatestRelease() => Log.ProcessStart("https://github.com/Hofknecht/SystemTrayMenu/releases");
|
2022-07-06 07:53:38 +12:00
|
|
|
|
|
|
|
|
|
public static void ActivateNewVersionFormOrCheckForUpdates(bool showWhenUpToDate)
|
|
|
|
|
{
|
|
|
|
|
if (newVersionForm != null)
|
2022-11-13 09:07:15 +13:00
|
|
|
|
{
|
2022-12-03 14:14:15 +13:00
|
|
|
|
newVersionForm!.HandleInvoke(() => newVersionForm?.Activate());
|
2022-07-06 07:53:38 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CheckForUpdates(showWhenUpToDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CheckForUpdates(bool showWhenUpToDate)
|
|
|
|
|
{
|
|
|
|
|
string urlGithubReleases = @"http://api.github.com/repos/Hofknecht/SystemTrayMenu/releases";
|
|
|
|
|
HttpClient client = new();
|
|
|
|
|
|
|
|
|
|
// https://developer.github.com/v3/#user-agent-required
|
2022-11-13 09:07:15 +13:00
|
|
|
|
client.DefaultRequestHeaders.Add("User-Agent", "SystemTrayMenu/" + Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
2022-07-06 07:53:38 +12:00
|
|
|
|
|
|
|
|
|
// https://developer.github.com/v3/media/#request-specific-version
|
|
|
|
|
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3.text+json");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using HttpResponseMessage response = client.GetAsync(urlGithubReleases).Result;
|
|
|
|
|
using HttpContent content = response.Content;
|
|
|
|
|
string responseString = content.ReadAsStringAsync().Result;
|
|
|
|
|
releases = responseString.FromJson<List<Dictionary<string, object>>>();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"{nameof(CheckForUpdates)} failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (releases == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"{nameof(CheckForUpdates)} failed.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RemoveCurrentAndOlderVersions();
|
|
|
|
|
ShowNewVersionOrUpToDateDialog(showWhenUpToDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RemoveCurrentAndOlderVersions()
|
|
|
|
|
{
|
|
|
|
|
int releasesCount = releases.Count;
|
2022-08-10 05:54:22 +12:00
|
|
|
|
Version versionCurrent = Assembly.GetExecutingAssembly().GetName().Version;
|
2022-07-06 07:53:38 +12:00
|
|
|
|
for (int i = 0; i < releasesCount; i++)
|
|
|
|
|
{
|
2022-08-10 05:54:22 +12:00
|
|
|
|
string tagName = releases[i]["tag_name"].ToString();
|
2022-08-28 01:14:29 +12:00
|
|
|
|
Version versionGitHub = new(tagName.Replace("v", string.Empty));
|
2022-08-10 05:54:22 +12:00
|
|
|
|
if (versionGitHub.CompareTo(versionCurrent) < 1)
|
2022-07-06 07:53:38 +12:00
|
|
|
|
{
|
|
|
|
|
releases.RemoveRange(i, releasesCount - i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ShowNewVersionOrUpToDateDialog(bool showWhenUpToDate)
|
|
|
|
|
{
|
|
|
|
|
if (releases.Count > 0)
|
2022-11-13 09:07:15 +13:00
|
|
|
|
{
|
|
|
|
|
newVersionForm = new();
|
|
|
|
|
newVersionForm.textBox.Text = GetChangelog();
|
|
|
|
|
newVersionForm.Closed += (_, _) => newVersionForm = null;
|
|
|
|
|
newVersionForm.ShowDialog();
|
2022-07-06 07:53:38 +12:00
|
|
|
|
}
|
|
|
|
|
else if (showWhenUpToDate)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Translator.GetText("You have the latest version of SystemTrayMenu!"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the change log from current version up to the latest release version.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Change log summary or error text.</returns>
|
|
|
|
|
private static string GetChangelog()
|
|
|
|
|
{
|
|
|
|
|
string result = string.Empty;
|
|
|
|
|
string errorstr = "An error occurred during update check!" + Environment.NewLine;
|
|
|
|
|
|
|
|
|
|
if (releases == null)
|
|
|
|
|
{
|
|
|
|
|
return errorstr + "Could not receive changelog!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < releases.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> release = releases[i];
|
|
|
|
|
|
|
|
|
|
result += release["name"].ToString()
|
|
|
|
|
+ Environment.NewLine
|
|
|
|
|
+ release["body_text"].ToString()
|
|
|
|
|
.Replace("\n\n", Environment.NewLine)
|
|
|
|
|
.Replace("\n \n", Environment.NewLine)
|
|
|
|
|
+ Environment.NewLine + Environment.NewLine;
|
|
|
|
|
if (i < releases.Count)
|
|
|
|
|
{
|
|
|
|
|
result += "--------------------------------------------------" +
|
|
|
|
|
"-------------------------------------------------------"
|
|
|
|
|
+ Environment.NewLine;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = result.Replace("\n", Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"{nameof(GetChangelog)}", ex);
|
|
|
|
|
result = errorstr + ex.Message.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 09:07:15 +13:00
|
|
|
|
}
|