From 5b745b8f17f8acc6d3799eb3cb86e09c7ca99017 Mon Sep 17 00:00:00 2001 From: sr55 Date: Tue, 27 Dec 2011 22:52:43 +0000 Subject: WinGui: (WPF) Initial work to hookup the log viewer + some additional helper classes ported over form the WinForms version. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4390 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/CS/HandBrakeWPF/Services/ErrorService.cs | 61 +++++++-- .../Services/Interfaces/IErrorService.cs | 59 +++++++-- .../Services/Interfaces/IUpdateVersionService.cs | 22 ++++ .../HandBrakeWPF/Services/UpdateVersionService.cs | 146 +++++++++++++++++++++ 4 files changed, 267 insertions(+), 21 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Services/Interfaces/IUpdateVersionService.cs create mode 100644 win/CS/HandBrakeWPF/Services/UpdateVersionService.cs (limited to 'win/CS/HandBrakeWPF/Services') diff --git a/win/CS/HandBrakeWPF/Services/ErrorService.cs b/win/CS/HandBrakeWPF/Services/ErrorService.cs index c5d89d5cf..3228ab363 100644 --- a/win/CS/HandBrakeWPF/Services/ErrorService.cs +++ b/win/CS/HandBrakeWPF/Services/ErrorService.cs @@ -9,6 +9,7 @@ namespace HandBrakeWPF.Services { + using System; using System.Windows; using Interfaces; using Caliburn.Micro; @@ -20,11 +21,17 @@ namespace HandBrakeWPF.Services public class ErrorService : IErrorService { /// - /// Show an Exception Error Window. + /// Show an Exception Error Window /// - /// - /// - /// + /// + /// The message. + /// + /// + /// The solution. + /// + /// + /// The details. + /// public void ShowError(string message, string solution, string details) { IWindowManager windowManager = IoC.Get(); @@ -39,15 +46,51 @@ namespace HandBrakeWPF.Services } } + /// + /// Show an Exception Error Window + /// + /// + /// The message. + /// + /// + /// The solution. + /// + /// + /// The exception. + /// + public void ShowError(string message, string solution, Exception exception) + { + IWindowManager windowManager = IoC.Get(); + IErrorViewModel errorViewModel = IoC.Get(); + + if (windowManager != null && errorViewModel != null) + { + errorViewModel.ErrorMessage = message; + errorViewModel.Solution = solution; + errorViewModel.Details = exception.ToString(); + windowManager.ShowDialog(errorViewModel); + } + } + /// /// Show a Message Box. /// It is good practice to use this, so that if we ever introduce unit testing, the message boxes won't cause issues. /// - /// - /// - /// - /// - /// + /// + /// The message. + /// + /// + /// The header. + /// + /// + /// The buttons. + /// + /// + /// The image. + /// + /// + /// The MessageBoxResult Object + /// public MessageBoxResult ShowMessageBox(string message, string header, MessageBoxButton buttons, MessageBoxImage image) { return MessageBox.Show(message, header, buttons, image); diff --git a/win/CS/HandBrakeWPF/Services/Interfaces/IErrorService.cs b/win/CS/HandBrakeWPF/Services/Interfaces/IErrorService.cs index cba3a79b3..cc9ef968c 100644 --- a/win/CS/HandBrakeWPF/Services/Interfaces/IErrorService.cs +++ b/win/CS/HandBrakeWPF/Services/Interfaces/IErrorService.cs @@ -7,28 +7,63 @@ // // -------------------------------------------------------------------------------------------------------------------- -using System.Windows; - namespace HandBrakeWPF.Services.Interfaces { + using System; + using System.Windows; + + /// + /// The Interface to the Error Service. + /// public interface IErrorService { /// - /// Show an Error Window with debug output. + /// Show an Exception Error Window /// - /// - /// - /// + /// + /// The message. + /// + /// + /// The solution. + /// + /// + /// The details. + /// void ShowError(string message, string solution, string details); /// - /// Show a Message Box + /// Show an Exception Error Window + /// + /// + /// The message. + /// + /// + /// The solution. + /// + /// + /// The exception. + /// + void ShowError(string message, string solution, Exception exception); + + /// + /// Show a Message Box. + /// It is good practice to use this, so that if we ever introduce unit testing, the message boxes won't cause issues. /// - /// - /// - /// - /// - /// + /// + /// The message. + /// + /// + /// The header. + /// + /// + /// The buttons. + /// + /// + /// The image. + /// + /// + /// The MessageBoxResult Object + /// MessageBoxResult ShowMessageBox(string message, string header, MessageBoxButton buttons, MessageBoxImage image); } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateVersionService.cs b/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateVersionService.cs new file mode 100644 index 000000000..3a84e68fc --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/Interfaces/IUpdateVersionService.cs @@ -0,0 +1,22 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the IUpdateVersionService type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services.Interfaces +{ + /// + /// The IUpdateVersionService Interface + /// + public interface IUpdateVersionService + { + /// + /// Get's HandBrakes version data from the CLI. + /// + void SetCliVersionData(); + } +} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/UpdateVersionService.cs b/win/CS/HandBrakeWPF/Services/UpdateVersionService.cs new file mode 100644 index 000000000..4f99720db --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/UpdateVersionService.cs @@ -0,0 +1,146 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the UpdateVersionService type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services +{ + using System; + using System.Diagnostics; + using System.IO; + using System.Security.Cryptography; + using System.Text.RegularExpressions; + using System.Windows; + + using HandBrake.ApplicationServices; + using HandBrake.ApplicationServices.Services.Interfaces; + + using HandBrakeWPF.Services.Interfaces; + + /// + /// Update Version Service - Handlers Update and Versioning. + /// + public class UpdateVersionService : IUpdateVersionService + { + /// + /// The Backing field for the user setting service + /// + private readonly IUserSettingService userSettingService; + + /// + /// The Error Service Backing field. + /// + private readonly IErrorService errorService; + + /// + /// Initializes a new instance of the class. + /// + /// + /// The user setting service. + /// + /// + /// The error Service. + /// + public UpdateVersionService(IUserSettingService userSettingService, IErrorService errorService) + { + this.userSettingService = userSettingService; + this.errorService = errorService; + } + + /// + /// Get's HandBrakes version data from the CLI. + /// + public void SetCliVersionData() + { + string line; + + // 0 = SVN Build / Version + // 1 = Build Date + + // Get the SHA1 Hash of HandBrakeCLI + byte[] hash; + string starupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); + + using (Stream stream = File.OpenRead(Path.Combine(starupPath, "HandBrakeCLI.exe"))) + { + hash = SHA1.Create().ComputeHash(stream); + } + string base64Hash = Convert.ToBase64String(hash); + + // Compare the hash with the last known hash. If it's the same, return. + if (userSettingService.GetUserSetting(UserSettingConstants.CliExeHash) == base64Hash) + { + return; + } + + // It's not the same, so start the CLI to get it's version data. + Process cliProcess = new Process(); + ProcessStartInfo handBrakeCli = new ProcessStartInfo("HandBrakeCLI.exe", " -u -v0") + { + UseShellExecute = false, + RedirectStandardError = true, + RedirectStandardOutput = true, + CreateNoWindow = true + }; + cliProcess.StartInfo = handBrakeCli; + + try + { + cliProcess.Start(); + + // Retrieve standard output and report back to parent thread until the process is complete + TextReader stdOutput = cliProcess.StandardError; + + while (!cliProcess.HasExited) + { + line = stdOutput.ReadLine() ?? string.Empty; + Match m = Regex.Match(line, @"HandBrake ([svnM0-9.]*) \(([0-9]*)\)"); + Match platform = Regex.Match(line, @"- ([A-Za-z0-9\s ]*) -"); + + if (m.Success) + { + string version = m.Groups[1].Success ? m.Groups[1].Value : string.Empty; + string build = m.Groups[2].Success ? m.Groups[2].Value : string.Empty; + + int buildValue; + int.TryParse(build, out buildValue); + + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeBuild, buildValue); + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeVersion, version); + } + + if (platform.Success) + { + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakePlatform, platform.Value.Replace("-", string.Empty).Trim()); + } + + if (cliProcess.TotalProcessorTime.Seconds > 10) // Don't wait longer than 10 seconds. + { + Process cli = Process.GetProcessById(cliProcess.Id); + if (!cli.HasExited) + { + cli.Kill(); + } + } + } + + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeExeHash, base64Hash); + } + catch (Exception e) + { + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeBuild, string.Empty); + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakePlatform, string.Empty); + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeVersion, string.Empty); + userSettingService.SetUserSetting(ASUserSettingConstants.HandBrakeExeHash, string.Empty); + + this.errorService.ShowError("Unable to Initialise HandBrake", "This error is unrecoverable. Maybe try restarting.", e); + + Application.Current.Shutdown(); + } + } + } +} -- cgit v1.2.3