// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// Update Check Helper
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Helpers
{
using System;
using System.Windows;
using Caliburn.Micro;
using HandBrake.ApplicationServices;
using HandBrake.ApplicationServices.Model.General;
using HandBrake.ApplicationServices.Services;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrakeWPF.Services.Interfaces;
///
/// Update Check Helper
///
public class UpdateCheckHelper
{
///
/// Perform a startup update check. Abiding by user settings.
///
public static void PerformStartupUpdateCheck()
{
// Make sure it's running on the calling thread
IUserSettingService userSettingService = IoC.Get();
if (userSettingService.GetUserSetting(UserSettingConstants.UpdateStatus))
{
if (DateTime.Now.Subtract(userSettingService.GetUserSetting(UserSettingConstants.LastUpdateCheckDate)).TotalDays
> userSettingService.GetUserSetting(UserSettingConstants.DaysBetweenUpdateCheck))
{
userSettingService.SetUserSetting(UserSettingConstants.LastUpdateCheckDate, DateTime.Now);
string url = userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakePlatform).Contains("x86_64")
? userSettingService.GetUserSetting(UserSettingConstants.Appcast_x64)
: userSettingService.GetUserSetting(UserSettingConstants.Appcast_i686);
UpdateService.BeginCheckForUpdates(UpdateCheckDone, false,
url, userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeBuild),
userSettingService.GetUserSetting(UserSettingConstants.Skipversion));
}
}
}
///
/// Check for Updates.
///
public static void CheckForUpdates()
{
IUserSettingService userSettingService = IoC.Get();
userSettingService.SetUserSetting(UserSettingConstants.LastUpdateCheckDate, DateTime.Now);
string url = userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakePlatform).Contains("x86_64")
? userSettingService.GetUserSetting(UserSettingConstants.Appcast_x64)
: userSettingService.GetUserSetting(UserSettingConstants.Appcast_i686);
UpdateService.BeginCheckForUpdates(UpdateCheckDoneMenu, false,
url, userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeBuild),
userSettingService.GetUserSetting(UserSettingConstants.Skipversion));
}
///
/// Handle the Update Check Finishing.
///
///
/// The result.
///
private static void UpdateCheckDone(IAsyncResult result)
{
// Make sure it's running on the calling thread
IErrorService errorService = IoC.Get();
try
{
// Get the information about the new build, if any, and close the window
UpdateCheckInformation info = UpdateService.EndCheckForUpdates(result);
if (info.NewVersionAvailable)
{
errorService.ShowMessageBox(
"A New Update is Available", "Update available!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
errorService.ShowError("Unable to check for updates", "Please try again later, the update service may currently be down.", ex);
}
}
///
/// Handle the Update Check Finishing.
///
///
/// The result.
///
private static void UpdateCheckDoneMenu(IAsyncResult result)
{
// Make sure it's running on the calling thread
IErrorService errorService = IoC.Get();
try
{
// Get the information about the new build, if any, and close the window
UpdateCheckInformation info = UpdateService.EndCheckForUpdates(result);
if (info.NewVersionAvailable)
{
errorService.ShowMessageBox(
"A New Update is Available", "Update available!", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
errorService.ShowMessageBox(
"There is no new version at this time.", "No Updates", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
errorService.ShowError("Unable to check for updates", "Please try again later, the update service may currently be down.", ex);
}
}
}
}