PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/CommandsDialogs/BrowseDialog/FormUpdates.cs

https://github.com/qgppl/gitextensions
C# | 213 lines | 183 code | 30 blank | 0 comment | 21 complexity | 426b542130539bdadfb8131a06d2aa29 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using Git.hub;
  8. using GitCommands.Config;
  9. using GitCommands;
  10. using ResourceManager;
  11. namespace GitUI.CommandsDialogs.BrowseDialog
  12. {
  13. public partial class FormUpdates : GitExtensionsForm
  14. {
  15. #region Translation
  16. private readonly TranslationString _newVersionAvailable =
  17. new TranslationString("There is a new version {0} of Git Extensions available");
  18. private readonly TranslationString _noUpdatesFound =
  19. new TranslationString("No updates found");
  20. #endregion
  21. public IWin32Window OwnerWindow;
  22. public Version CurrentVersion;
  23. public bool UpdateFound;
  24. public string UpdateUrl;
  25. public string NewVersion;
  26. public FormUpdates(Version currentVersion)
  27. {
  28. InitializeComponent();
  29. Translate();
  30. UpdateFound = false;
  31. progressBar1.Visible = true;
  32. CurrentVersion = currentVersion;
  33. UpdateUrl = "";
  34. NewVersion = "";
  35. progressBar1.Style = ProgressBarStyle.Marquee;
  36. }
  37. private void CloseButtonClick(object sender, EventArgs e)
  38. {
  39. Close();
  40. }
  41. public void SearchForUpdatesAndShow(IWin32Window aOwnerWindow, bool alwaysShow)
  42. {
  43. OwnerWindow = aOwnerWindow;
  44. new Thread(SearchForUpdates).Start();
  45. if (alwaysShow)
  46. ShowDialog(aOwnerWindow);
  47. }
  48. private void SearchForUpdates()
  49. {
  50. try
  51. {
  52. Client github = new Client();
  53. Repository gitExtRepo = github.getRepository("gitextensions", "gitextensions");
  54. if (gitExtRepo == null)
  55. return;
  56. var configData = gitExtRepo.GetRef("heads/configdata");
  57. if (configData == null)
  58. return;
  59. var tree = configData.GetTree();
  60. if (tree == null)
  61. return;
  62. var releases = tree.Tree.Where(entry => "GitExtensions.releases".Equals(entry.Path, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
  63. if (releases != null && releases.Blob.Value != null)
  64. {
  65. CheckForNewerVersion(releases.Blob.Value.GetContent());
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. this.InvokeSync((state) =>
  71. {
  72. if (Visible)
  73. {
  74. ExceptionUtils.ShowException(this, ex, string.Empty, true);
  75. }
  76. }, null);
  77. Done();
  78. }
  79. }
  80. void CheckForNewerVersion(string releases)
  81. {
  82. var versions = ReleaseVersion.Parse(releases);
  83. var updates = ReleaseVersion.GetNewerVersions(CurrentVersion, AppSettings.CheckForReleaseCandidates, versions);
  84. var update = updates.OrderBy(version => version.Version).LastOrDefault();
  85. if (update != null)
  86. {
  87. UpdateFound = true;
  88. UpdateUrl = update.DownloadPage;
  89. NewVersion = update.Version.ToString();
  90. Done();
  91. return;
  92. }
  93. UpdateUrl = "";
  94. UpdateFound = false;
  95. Done();
  96. }
  97. private void Done()
  98. {
  99. this.InvokeSync(o =>
  100. {
  101. progressBar1.Visible = false;
  102. if (UpdateFound)
  103. {
  104. btnDownloadNow.Enabled = true;
  105. UpdateLabel.Text = string.Format(_newVersionAvailable.Text, NewVersion);
  106. linkChangeLog.Visible = true;
  107. if (!Visible)
  108. ShowDialog(OwnerWindow);
  109. }
  110. else
  111. {
  112. UpdateLabel.Text = _noUpdatesFound.Text;
  113. }
  114. }, this);
  115. }
  116. private void linkChangeLog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  117. {
  118. Process.Start("https://github.com/gitextensions/gitextensions/blob/master/GitUI/Resources/ChangeLog.md");
  119. }
  120. private void btnDownloadNow_Click(object sender, EventArgs e)
  121. {
  122. try
  123. {
  124. Process.Start(UpdateUrl);
  125. }
  126. catch (System.ComponentModel.Win32Exception)
  127. {
  128. }
  129. }
  130. }
  131. public enum ReleaseType
  132. {
  133. Major,
  134. HotFix,
  135. ReleaseCandidate
  136. }
  137. public class ReleaseVersion
  138. {
  139. public Version Version;
  140. public ReleaseType ReleaseType;
  141. public string DownloadPage;
  142. public static ReleaseVersion FromSection(ConfigSection section)
  143. {
  144. Version ver;
  145. try
  146. {
  147. ver = new Version(section.SubSection);
  148. }
  149. catch (Exception e)
  150. {
  151. System.Diagnostics.Debug.WriteLine(e);
  152. return null;
  153. }
  154. var version = new ReleaseVersion()
  155. {
  156. Version = ver,
  157. ReleaseType = ReleaseType.Major,
  158. DownloadPage = section.GetValue("DownloadPage")
  159. };
  160. Enum.TryParse<ReleaseType>(section.GetValue("ReleaseType"), true, out version.ReleaseType);
  161. return version;
  162. }
  163. public static IEnumerable<ReleaseVersion> Parse(string versionsStr)
  164. {
  165. ConfigFile cfg = new ConfigFile("", true);
  166. cfg.LoadFromString(versionsStr);
  167. var sections = cfg.GetConfigSections("Version");
  168. sections = sections.Concat(cfg.GetConfigSections("RCVersion"));
  169. return sections.Select(FromSection).Where(version => version != null);
  170. }
  171. public static IEnumerable<ReleaseVersion> GetNewerVersions(
  172. Version currentVersion,
  173. bool checkForReleaseCandidates,
  174. IEnumerable<ReleaseVersion> availableVersions)
  175. {
  176. var versions = availableVersions.Where(version =>
  177. version.ReleaseType == ReleaseType.Major ||
  178. version.ReleaseType == ReleaseType.HotFix ||
  179. checkForReleaseCandidates && version.ReleaseType == ReleaseType.ReleaseCandidate);
  180. return versions.Where(version => version.Version.CompareTo(currentVersion) > 0);
  181. }
  182. }
  183. }