1 // -------------------------------------------------------------------------------------------------------------------- 2 // <copyright file="ManagePresetViewModel.cs" company="HandBrake Project (http://handbrake.fr)"> 3 // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. 4 // </copyright> 5 // <summary> 6 // The Add Preset View Model 7 // </summary> 8 // -------------------------------------------------------------------------------------------------------------------- 9 10 namespace HandBrakeWPF.ViewModels 11 { 12 using System.Windows; 13 14 using Caliburn.Micro; 15 16 using HandBrakeWPF.Properties; 17 using HandBrakeWPF.Services.Interfaces; 18 using HandBrakeWPF.Services.Presets; 19 using HandBrakeWPF.Services.Presets.Interfaces; 20 using HandBrakeWPF.Services.Presets.Model; 21 using HandBrakeWPF.ViewModels.Interfaces; 22 23 /// <summary> 24 /// The Add Preset View Model 25 /// </summary> 26 public class ManagePresetViewModel : ViewModelBase, IManagePresetViewModel 27 { 28 private readonly IPresetService presetService; 29 private readonly IErrorService errorService; 30 private readonly IWindowManager windowManager; 31 private Preset existingPreset; 32 33 /// <summary> 34 /// Initializes a new instance of the <see cref="ManagePresetViewModel"/> class. 35 /// </summary> 36 /// <param name="presetService"> 37 /// The Preset Service 38 /// </param> 39 /// <param name="errorService"> 40 /// The Error Service 41 /// </param> 42 /// <param name="windowManager"> 43 /// The window Manager. 44 /// </param> ManagePresetViewModel(IPresetService presetService, IErrorService errorService, IWindowManager windowManager)45 public ManagePresetViewModel(IPresetService presetService, IErrorService errorService, IWindowManager windowManager) 46 { 47 this.presetService = presetService; 48 this.errorService = errorService; 49 this.windowManager = windowManager; 50 this.Title = "Manage Preset"; 51 this.Preset = new Preset { IsBuildIn = false, IsDefault = false, Category = PresetService.UserPresetCategoryName }; 52 } 53 54 /// <summary> 55 /// Gets the Preset 56 /// </summary> 57 public Preset Preset { get; private set; } 58 59 /// <summary> 60 /// Prepare the Preset window to create a Preset Object later. 61 /// </summary> 62 /// <param name="presetToEdit"> 63 /// The preset To Edit. 64 /// </param> Setup(Preset presetToEdit)65 public void Setup(Preset presetToEdit) 66 { 67 this.Preset = new Preset(presetToEdit); // Clone. We will not touch the existing object. 68 this.existingPreset = presetToEdit; 69 } 70 71 /// <summary> 72 /// Add a Preset 73 /// </summary> Save()74 public void Save() 75 { 76 if (string.IsNullOrEmpty(this.Preset.Name)) 77 { 78 this.errorService.ShowMessageBox( 79 Resources.AddPresetViewModel_PresetMustProvideName, 80 Resources.Error, 81 MessageBoxButton.OK, 82 MessageBoxImage.Error); 83 return; 84 } 85 86 if (this.presetService.CheckIfPresetExists(this.Preset.Name)) 87 { 88 MessageBoxResult result = 89 this.errorService.ShowMessageBox( 90 Resources.AddPresetViewModel_PresetWithSameNameOverwriteWarning, 91 Resources.Error, 92 MessageBoxButton.YesNo, 93 MessageBoxImage.Error); 94 if (result == MessageBoxResult.No) 95 { 96 return; 97 } 98 } 99 100 // Save the Preset 101 this.presetService.Replace(this.existingPreset, this.Preset); 102 this.Close(); 103 } 104 105 /// <summary> 106 /// Cancel adding a preset 107 /// </summary> Cancel()108 public void Cancel() 109 { 110 this.Close(); 111 } 112 113 /// <summary> 114 /// Close this window. 115 /// </summary> Close()116 public void Close() 117 { 118 this.TryCloseAsync(); 119 } 120 } 121 } 122