From f93ce617afdba769738f8be17e3f80391231b822 Mon Sep 17 00:00:00 2001 From: sr55 Date: Tue, 27 Nov 2012 16:32:13 +0000 Subject: WinGui: General code tidy-up and fix a couple of possible points that could throw exceptions. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5083 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../Services/PresetService.cs | 50 ++++++++-------------- win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs | 13 ++++-- win/CS/HandBrakeWPF/Views/ShellView.xaml.cs | 11 +++-- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/win/CS/HandBrake.ApplicationServices/Services/PresetService.cs b/win/CS/HandBrake.ApplicationServices/Services/PresetService.cs index 9e31173ae..5edfb4e1e 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/PresetService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/PresetService.cs @@ -19,8 +19,6 @@ namespace HandBrake.ApplicationServices.Services using System.Text.RegularExpressions; using System.Xml.Serialization; - using Caliburn.Micro; - using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Services.Interfaces; @@ -32,13 +30,6 @@ namespace HandBrake.ApplicationServices.Services /// public class PresetService : IPresetService { - /** - * TODO: - * - Wire this into the Forms and WPF UI's - * - Note: This is untested so far. It'll likely need fixes before it can be used. - * - Maybe change the collection to a dictionary to allow easier lookups? - **/ - #region Private Variables /// @@ -143,14 +134,10 @@ namespace HandBrake.ApplicationServices.Services // Update the presets file this.UpdatePresetFiles(); return true; - } - else - { - this.Update(preset); - return true; } - - return false; + + this.Update(preset); + return true; } /// @@ -447,15 +434,16 @@ namespace HandBrake.ApplicationServices.Services { if (File.Exists(this.builtInPresetFile)) { - StreamReader reader = new StreamReader(this.builtInPresetFile); - List list = (List)Ser.Deserialize(reader); - foreach (Preset preset in list) + using (StreamReader reader = new StreamReader(this.builtInPresetFile)) { - preset.IsBuildIn = true; // Older versions did not have this flag so explicitly make sure it is set. - this.presets.Add(preset); + List list = (List)Ser.Deserialize(reader); + foreach (Preset preset in list) + { + preset.IsBuildIn = true; + // Older versions did not have this flag so explicitly make sure it is set. + this.presets.Add(preset); + } } - - reader.Close(); } } catch (Exception exc) @@ -470,14 +458,14 @@ namespace HandBrake.ApplicationServices.Services { if (File.Exists(this.userPresetFile)) { - StreamReader reader = new StreamReader(this.userPresetFile); - List list = (List)Ser.Deserialize(reader); - foreach (Preset preset in list) + using (StreamReader reader = new StreamReader(this.userPresetFile)) { - this.presets.Add(preset); + List list = (List)Ser.Deserialize(reader); + foreach (Preset preset in list) + { + this.presets.Add(preset); + } } - - reader.Close(); } } catch (Exception exc) @@ -503,15 +491,11 @@ namespace HandBrake.ApplicationServices.Services using (FileStream strm = new FileStream(this.builtInPresetFile, FileMode.Create, FileAccess.Write)) { Ser.Serialize(strm, this.presets.Where(p => p.IsBuildIn).ToList()); - strm.Close(); - strm.Dispose(); } using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Create, FileAccess.Write)) { Ser.Serialize(strm, this.presets.Where(p => p.IsBuildIn == false).ToList()); - strm.Close(); - strm.Dispose(); } } catch (Exception exc) diff --git a/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs index 2633b5885..a64378a49 100644 --- a/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs @@ -147,7 +147,7 @@ namespace HandBrakeWPF.ViewModels public bool CanClose() { IQueueProcessor processor = IoC.Get(); - if (processor.EncodeService.IsEncoding) + if (processor != null && processor.EncodeService.IsEncoding) { MessageBoxResult result = errorService.ShowMessageBox( @@ -160,13 +160,20 @@ namespace HandBrakeWPF.ViewModels { processor.Pause(); processor.EncodeService.Stop(); - this.MainViewModel.Shutdown(); + if (this.MainViewModel != null) + { + this.MainViewModel.Shutdown(); + } + return true; } return false; } - this.MainViewModel.Shutdown(); + if (this.MainViewModel != null) + { + this.MainViewModel.Shutdown(); + } return true; } } diff --git a/win/CS/HandBrakeWPF/Views/ShellView.xaml.cs b/win/CS/HandBrakeWPF/Views/ShellView.xaml.cs index 77b0d858c..8a086ea09 100644 --- a/win/CS/HandBrakeWPF/Views/ShellView.xaml.cs +++ b/win/CS/HandBrakeWPF/Views/ShellView.xaml.cs @@ -60,10 +60,15 @@ namespace HandBrakeWPF.Views /// protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { - bool canClose = ((IShellViewModel)this.DataContext).CanClose(); - if (!canClose) + IShellViewModel shellViewModel = this.DataContext as IShellViewModel; + + if (shellViewModel != null) { - e.Cancel = true; + bool canClose = shellViewModel.CanClose(); + if (!canClose) + { + e.Cancel = true; + } } base.OnClosing(e); -- cgit v1.2.3