// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // The Queue Selection View Model // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.ViewModels { using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using HandBrakeWPF.Model; using HandBrakeWPF.Properties; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Services.Presets.Model; using HandBrakeWPF.Services.Scan.Model; using HandBrakeWPF.ViewModels.Interfaces; /// /// The Queue Selection View Model /// public class QueueSelectionViewModel : ViewModelBase, IQueueSelectionViewModel { private readonly IErrorService errorService; private readonly IUserSettingService userSettingService; private bool orderedByDuration; private bool orderedByTitle; private Action> addToQueue; private string currentPreset; /// /// Initializes a new instance of the class. /// /// /// The Error Service /// /// /// The user Setting Service. /// public QueueSelectionViewModel(IErrorService errorService, IUserSettingService userSettingService) { this.errorService = errorService; this.userSettingService = userSettingService; this.Title = Resources.QueueSelectionViewModel_AddToQueue; this.TitleList = new BindingList(); this.OrderedByTitle = true; } /// /// Gets or sets the source. /// public string Source { get; set; } /// /// Gets or sets the selected titles. /// public BindingList TitleList { get; set; } /// /// Gets or sets the current preset. /// public string CurrentPreset { get { return this.currentPreset; } set { if (value == this.currentPreset) { return; } this.currentPreset = value; this.NotifyOfPropertyChange(() => this.CurrentPreset); } } /// /// Gets or sets a value indicating whether ordered by title. /// public bool OrderedByTitle { get { return this.orderedByTitle; } set { this.orderedByTitle = value; this.NotifyOfPropertyChange(() => OrderedByTitle); } } /// /// Gets or sets a value indicating whether ordered by duration. /// public bool OrderedByDuration { get { return this.orderedByDuration; } set { this.orderedByDuration = value; this.NotifyOfPropertyChange(() => OrderedByDuration); } } /// /// Gets a value indicating whether is auto naming enabled. /// public bool IsAutoNamingEnabled { get { return this.userSettingService.GetUserSetting(UserSettingConstants.AutoNaming); } } /// /// The order by title. /// public void OrderByTitle() { TitleList = new BindingList(TitleList.OrderBy(o => o.Title.TitleNumber).ToList()); this.NotifyOfPropertyChange(() => TitleList); this.OrderedByTitle = true; this.OrderedByDuration = false; } /// /// The order by duration. /// public void OrderByDuration() { TitleList = new BindingList(TitleList.OrderByDescending(o => o.Title.Duration).ToList()); this.NotifyOfPropertyChange(() => TitleList); this.OrderedByTitle = false; this.OrderedByDuration = true; } /// /// The select all. /// public void SelectAll() { foreach (var item in TitleList) { item.IsSelected = true; } } /// /// The select all. /// public void UnSelectAll() { foreach (var item in TitleList) { item.IsSelected = false; } } /// /// Add a Preset /// public void Add() { this.addToQueue(this.TitleList.Where(c => c.IsSelected)); this.Close(); } /// /// Cancel adding a preset /// public void Cancel() { this.TitleList.Clear(); this.Close(); } /// /// Close this window. /// public void Close() { this.TryClose(); } /// /// The setup. /// /// /// The scanned source. /// /// /// The src Name. /// /// /// The add Action. /// /// /// The preset. /// public void Setup(Source scannedSource, string srcName, Action> addAction, Preset preset) { this.TitleList.Clear(); this.addToQueue = addAction; if (scannedSource != null) { IEnumerable titles = orderedByTitle ? scannedSource.Titles : scannedSource.Titles.OrderByDescending(o => o.Duration).ToList(); foreach (Title item in titles) { SelectionTitle title = new SelectionTitle(item, srcName) { IsSelected = true }; TitleList.Add(title); } } if (preset != null) { this.CurrentPreset = string.Format(ResourcesUI.QueueSelection_UsingPreset, preset.Name); } this.NotifyOfPropertyChange(() => this.IsAutoNamingEnabled); } } }