// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The Add Preset View Model
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.ViewModels
{
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using Caliburn.Micro;
using HandBrake.ApplicationServices.Functions;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Services;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrake.ApplicationServices.Utilities;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.ViewModels.Interfaces;
///
/// The Add Preset View Model
///
[Export(typeof(IAddPresetViewModel))]
public class AddPresetViewModel : ViewModelBase, IAddPresetViewModel
{
///
/// Backing field for the Preset Service
///
private readonly IPresetService presetService;
///
/// Backing field for the error service
///
private readonly IErrorService errorService;
///
/// Backing fields for Selected Picture settings mode.
///
private PresetPictureSettingsMode selectedPictureSettingMode;
///
/// Backging field for show custom inputs
///
private bool showCustomInputs;
///
/// Initializes a new instance of the class.
///
///
/// The window manager.
///
///
/// The Preset Service
///
///
/// The Error Service
///
public AddPresetViewModel(IWindowManager windowManager, IPresetService presetService, IErrorService errorService)
{
this.presetService = presetService;
this.errorService = errorService;
this.Title = "Add Preset";
this.Preset = new Preset { IsBuildIn = false, IsDefault = false, Category = PresetService.UserPresetCatgoryName };
this.PictureSettingsModes = EnumHelper.GetEnumList();
}
///
/// Gets the Preset
///
public Preset Preset { get; private set; }
///
/// Gets or sets PictureSettingsModes.
///
public IEnumerable PictureSettingsModes { get; set; }
///
/// Gets or sets CustomWidth.
///
public int CustomWidth { get; set; }
///
/// Gets or sets CustomHeight.
///
public int CustomHeight { get; set; }
///
/// Gets or sets a value indicating whether ShowCustomInputs.
///
public bool ShowCustomInputs
{
get
{
return this.showCustomInputs;
}
set
{
this.showCustomInputs = value;
this.NotifyOfPropertyChange(() => this.ShowCustomInputs);
}
}
///
/// Gets or sets SelectedPictureSettingMode.
///
public PresetPictureSettingsMode SelectedPictureSettingMode
{
get
{
return this.selectedPictureSettingMode;
}
set
{
this.selectedPictureSettingMode = value;
this.ShowCustomInputs = value == PresetPictureSettingsMode.Custom;
if (SelectedPictureSettingMode == PresetPictureSettingsMode.Custom)
{
this.Preset.Task.MaxHeight = null;
this.Preset.Task.MaxWidth = null;
}
if (SelectedPictureSettingMode == PresetPictureSettingsMode.Custom)
{
this.Preset.Task.Width = this.CustomWidth;
this.Preset.Task.Height = this.CustomHeight;
this.Preset.Task.MaxHeight = null;
this.Preset.Task.MaxWidth = null;
}
if (SelectedPictureSettingMode == PresetPictureSettingsMode.SourceMaximum)
{
this.Preset.Task.MaxWidth = this.Preset.Task.Width;
this.Preset.Task.MaxHeight = this.Preset.Task.Height;
}
}
}
///
/// Prepare the Preset window to create a Preset Object later.
///
///
/// The Encode Task.
///
public void Setup(EncodeTask task)
{
this.Preset.Task = new EncodeTask(task);
}
///
/// Add a Preset
///
public void Add()
{
if (string.IsNullOrEmpty(this.Preset.Name))
{
this.errorService.ShowMessageBox("A Preset must have a Name. Please fill out the Preset Name field.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (this.presetService.CheckIfPresetExists(this.Preset.Name))
{
this.errorService.ShowMessageBox("A Preset with this name already exists. Please choose a new name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
this.Preset.UsePictureFilters = this.Preset.UsePictureFilters;
this.Preset.PictureSettingsMode = this.SelectedPictureSettingMode;
bool added = this.presetService.Add(this.Preset);
if (!added)
{
this.errorService.ShowMessageBox("Unable to add preset", "Unknown Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
else
{
this.Close();
}
}
///
/// Cancel adding a preset
///
public void Cancel()
{
this.Close();
}
///
/// Close this window.
///
public void Close()
{
this.TryClose();
}
}
}