// --------------------------------------------------------------------------------------------------------------------
//
// 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.Windows;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Model.Audio;
using HandBrake.ApplicationServices.Model.Subtitle;
using HandBrake.ApplicationServices.Parsing;
using HandBrake.ApplicationServices.Services;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrake.ApplicationServices.Utilities;
using HandBrake.Interop.Model.Encoding;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.ViewModels.Interfaces;
///
/// The Add Preset View Model
///
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;
///
/// The source.
///
private Title selectedTitle;
///
/// Initializes a new instance of the class.
///
///
/// The Preset Service
///
///
/// The Error Service
///
public AddPresetViewModel(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, UsePictureFilters = true };
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;
}
}
///
/// Prepare the Preset window to create a Preset Object later.
///
///
/// The Encode Task.
///
///
/// The title.
///
///
/// The audio Behaviours.
///
///
/// The subtitle Behaviours.
///
public void Setup(EncodeTask task, Title title, AudioBehaviours audioBehaviours, SubtitleBehaviours subtitleBehaviours)
{
this.Preset.Task = new EncodeTask(task);
this.Preset.AudioTrackBehaviours = audioBehaviours.Clone();
this.Preset.SubtitleTrackBehaviours = subtitleBehaviours.Clone();
this.selectedTitle = title;
switch (task.Anamorphic)
{
default:
this.SelectedPictureSettingMode = PresetPictureSettingsMode.Custom;
break;
case Anamorphic.Strict:
this.SelectedPictureSettingMode = PresetPictureSettingsMode.SourceMaximum;
break;
}
}
///
/// 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.", Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (this.presetService.CheckIfPresetExists(this.Preset.Name))
{
MessageBoxResult result = this.errorService.ShowMessageBox("A Preset with this name already exists. Would you like to overwrite it?", Resources.Error, MessageBoxButton.YesNo, MessageBoxImage.Error);
if (result == MessageBoxResult.No)
{
return;
}
}
if (this.SelectedPictureSettingMode == PresetPictureSettingsMode.SourceMaximum && this.selectedTitle == null)
{
this.errorService.ShowMessageBox("You must first scan a source to use the 'Source Maximum' Option.", Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
this.Preset.UsePictureFilters = this.Preset.UsePictureFilters;
this.Preset.PictureSettingsMode = this.SelectedPictureSettingMode;
// Setting W, H, MW and MH
if (this.SelectedPictureSettingMode == PresetPictureSettingsMode.None)
{
this.Preset.Task.MaxHeight = null;
this.Preset.Task.MaxWidth = null;
}
if (this.SelectedPictureSettingMode == PresetPictureSettingsMode.Custom)
{
this.Preset.Task.MaxWidth = this.CustomWidth;
this.Preset.Task.MaxHeight = this.CustomHeight;
this.Preset.Task.Width = null;
this.Preset.Task.Height = null;
}
if (this.SelectedPictureSettingMode == PresetPictureSettingsMode.SourceMaximum)
{
this.Preset.Task.MaxWidth = selectedTitle.Resolution.Width;
this.Preset.Task.MaxHeight = selectedTitle.Resolution.Height;
}
// Add the Preset
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();
}
}
}