NativeApp.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Updates;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Updates;
  8. namespace MediaBrowser.ServerApplication.Native
  9. {
  10. /// <summary>
  11. /// Class NativeApp
  12. /// </summary>
  13. public static class NativeApp
  14. {
  15. /// <summary>
  16. /// Shutdowns this instance.
  17. /// </summary>
  18. public static void Shutdown()
  19. {
  20. MainStartup.Shutdown();
  21. }
  22. /// <summary>
  23. /// Restarts this instance.
  24. /// </summary>
  25. public static void Restart()
  26. {
  27. MainStartup.Restart();
  28. }
  29. /// <summary>
  30. /// Determines whether this instance [can self restart].
  31. /// </summary>
  32. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  33. public static bool CanSelfRestart
  34. {
  35. get
  36. {
  37. return MainStartup.CanSelfRestart;
  38. }
  39. }
  40. /// <summary>
  41. /// Gets a value indicating whether [supports automatic run at startup].
  42. /// </summary>
  43. /// <value><c>true</c> if [supports automatic run at startup]; otherwise, <c>false</c>.</value>
  44. public static bool SupportsAutoRunAtStartup
  45. {
  46. get
  47. {
  48. return true;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets a value indicating whether this instance can self update.
  53. /// </summary>
  54. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  55. public static bool CanSelfUpdate
  56. {
  57. get
  58. {
  59. return MainStartup.CanSelfUpdate;
  60. }
  61. }
  62. public static void PreventSystemStandby()
  63. {
  64. SystemHelper.ResetStandbyTimer();
  65. }
  66. internal enum EXECUTION_STATE : uint
  67. {
  68. ES_NONE = 0,
  69. ES_SYSTEM_REQUIRED = 0x00000001,
  70. ES_DISPLAY_REQUIRED = 0x00000002,
  71. ES_USER_PRESENT = 0x00000004,
  72. ES_AWAYMODE_REQUIRED = 0x00000040,
  73. ES_CONTINUOUS = 0x80000000
  74. }
  75. public class SystemHelper
  76. {
  77. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  78. static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
  79. public static void ResetStandbyTimer()
  80. {
  81. EXECUTION_STATE es = SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED);
  82. }
  83. }
  84. public static async Task<CheckForUpdateResult> CheckForApplicationUpdate(Version currentVersion,
  85. PackageVersionClass updateLevel,
  86. IInstallationManager installationManager,
  87. CancellationToken cancellationToken,
  88. IProgress<double> progress)
  89. {
  90. var availablePackages = await installationManager.GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
  91. var version = installationManager.GetLatestCompatibleVersion(availablePackages, "MBServer", null, currentVersion, updateLevel);
  92. var versionObject = version == null || string.IsNullOrWhiteSpace(version.versionStr) ? null : new Version(version.versionStr);
  93. var isUpdateAvailable = versionObject != null && versionObject > currentVersion;
  94. return versionObject != null ?
  95. new CheckForUpdateResult { AvailableVersion = versionObject.ToString(), IsUpdateAvailable = isUpdateAvailable, Package = version } :
  96. new CheckForUpdateResult { AvailableVersion = currentVersion.ToString(), IsUpdateAvailable = false };
  97. }
  98. }
  99. }