/ src / settings-ui / Settings.UI.Library / UpdatingSettings.cs
UpdatingSettings.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Globalization;
  7  using System.IO;
  8  using System.IO.Abstractions;
  9  using System.Text.Json;
 10  using System.Text.Json.Serialization;
 11  
 12  namespace Microsoft.PowerToys.Settings.UI.Library
 13  {
 14      public class UpdatingSettings
 15      {
 16          public enum UpdatingState
 17          {
 18              UpToDate = 0,
 19              ErrorDownloading,
 20              ReadyToDownload,
 21              ReadyToInstall,
 22              NetworkError,
 23          }
 24  
 25          // Gets or sets a value of the updating state
 26          [JsonPropertyName("state")]
 27          public UpdatingState State { get; set; }
 28  
 29          // Gets or sets a value of the release page url
 30          [JsonPropertyName("releasePageUrl")]
 31          public string ReleasePageLink { get; set; }
 32  
 33          // Gets or sets a value of the github last checked date
 34          [JsonPropertyName("githubUpdateLastCheckedDate")]
 35          public string LastCheckedDate { get; set; }
 36  
 37          // Gets or sets a value of the updating state
 38          [JsonPropertyName("downloadedInstallerFilename")]
 39          public string DownloadedInstallerFilename { get; set; }
 40  
 41          // Non-localizable strings: Files
 42          public const string SettingsFilePath = "\\Microsoft\\PowerToys\\";
 43          public const string SettingsFile = "UpdateState.json";
 44  
 45          public string NewVersion
 46          {
 47              get
 48              {
 49                  if (ReleasePageLink == null)
 50                  {
 51                      return string.Empty;
 52                  }
 53  
 54                  try
 55                  {
 56                      string version = ReleasePageLink.Substring(ReleasePageLink.LastIndexOf('/') + 1);
 57                      return version.Trim();
 58                  }
 59                  catch (Exception)
 60                  {
 61                  }
 62  
 63                  return string.Empty;
 64              }
 65          }
 66  
 67          public string LastCheckedDateLocalized
 68          {
 69              get
 70              {
 71                  try
 72                  {
 73                      if (LastCheckedDate == null)
 74                      {
 75                          return string.Empty;
 76                      }
 77  
 78                      long seconds = long.Parse(LastCheckedDate, CultureInfo.CurrentCulture);
 79                      var date = DateTimeOffset.FromUnixTimeSeconds(seconds).UtcDateTime;
 80                      return date.ToLocalTime().ToString(CultureInfo.CurrentCulture);
 81                  }
 82                  catch (Exception)
 83                  {
 84                  }
 85  
 86                  return string.Empty;
 87              }
 88          }
 89  
 90          public UpdatingSettings()
 91          {
 92              State = UpdatingState.UpToDate;
 93          }
 94  
 95          public static UpdatingSettings LoadSettings()
 96          {
 97              FileSystem fileSystem = new FileSystem();
 98              var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
 99              var file = localAppDataDir + SettingsFilePath + SettingsFile;
100  
101              if (fileSystem.File.Exists(file))
102              {
103                  try
104                  {
105                      FileSystemStream inputStream = fileSystem.File.Open(file, FileMode.Open);
106                      StreamReader reader = new StreamReader(inputStream);
107                      string data = reader.ReadToEnd();
108                      inputStream.Close();
109                      reader.Dispose();
110  
111                      return JsonSerializer.Deserialize<UpdatingSettings>(data);
112                  }
113                  catch (Exception)
114                  {
115                  }
116              }
117  
118              return null;
119          }
120  
121          public string ToJsonString()
122          {
123              return JsonSerializer.Serialize(this);
124          }
125      }
126  }