1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="QueueSelectionViewModel.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 Queue Selection View Model
7 // </summary>
8 // --------------------------------------------------------------------------------------------------------------------
9 
10 namespace HandBrakeWPF.ViewModels
11 {
12     using System;
13     using System.Collections.Generic;
14     using System.ComponentModel;
15     using System.Linq;
16 
17     using HandBrakeWPF.Model;
18     using HandBrakeWPF.Properties;
19     using HandBrakeWPF.Services.Interfaces;
20     using HandBrakeWPF.Services.Presets.Model;
21     using HandBrakeWPF.Services.Scan.Model;
22     using HandBrakeWPF.ViewModels.Interfaces;
23 
24     /// <summary>
25     /// The Queue Selection View Model
26     /// </summary>
27     public class QueueSelectionViewModel : ViewModelBase, IQueueSelectionViewModel
28     {
29         private readonly IErrorService errorService;
30         private readonly IUserSettingService userSettingService;
31         private bool orderedByDuration;
32         private bool orderedByTitle;
33         private bool orderedByName;
34         private Action<IEnumerable<SelectionTitle>> addToQueue;
35 
36         private string currentPreset;
37 
38         /// <summary>
39         /// Initializes a new instance of the <see cref="QueueSelectionViewModel"/> class.
40         /// </summary>
41         /// <param name="errorService">
42         /// The Error Service
43         /// </param>
44         /// <param name="userSettingService">
45         /// The user Setting Service.
46         /// </param>
QueueSelectionViewModel(IErrorService errorService, IUserSettingService userSettingService)47         public QueueSelectionViewModel(IErrorService errorService, IUserSettingService userSettingService)
48         {
49             this.errorService = errorService;
50             this.userSettingService = userSettingService;
51             this.Title = Resources.QueueSelectionViewModel_AddToQueue;
52             this.TitleList = new BindingList<SelectionTitle>();
53             this.OrderedByTitle = true;
54         }
55 
56         /// <summary>
57         /// Gets or sets the source.
58         /// </summary>
59         public string Source { get; set; }
60 
61         /// <summary>
62         /// Gets or sets the selected titles.
63         /// </summary>
64         public BindingList<SelectionTitle> TitleList { get; set; }
65 
66         /// <summary>
67         /// Gets or sets the current preset.
68         /// </summary>
69         public string CurrentPreset
70         {
71             get
72             {
73                 return this.currentPreset;
74             }
75             set
76             {
77                 if (value == this.currentPreset)
78                 {
79                     return;
80                 }
81                 this.currentPreset = value;
82                 this.NotifyOfPropertyChange(() => this.CurrentPreset);
83             }
84         }
85 
86         /// <summary>
87         /// Gets or sets a value indicating whether ordered by title.
88         /// </summary>
89         public bool OrderedByTitle
90         {
91             get
92             {
93                 return this.orderedByTitle;
94             }
95 
96             set
97             {
98                 this.orderedByTitle = value;
99                 this.NotifyOfPropertyChange(() => OrderedByTitle);
100             }
101         }
102 
103         /// <summary>
104         /// Gets or sets a value indicating whether ordered by duration.
105         /// </summary>
106         public bool OrderedByDuration
107         {
108             get
109             {
110                 return this.orderedByDuration;
111             }
112 
113             set
114             {
115                 this.orderedByDuration = value;
116                 this.NotifyOfPropertyChange(() => OrderedByDuration);
117             }
118         }
119 
120         /// <summary>
121         /// Gets or sets a value indicating whether ordered by name.
122         /// </summary>
123         public bool OrderedByName
124         {
125             get
126             {
127                 return this.orderedByName;
128             }
129 
130             set
131             {
132                 this.orderedByName = value;
133                 this.NotifyOfPropertyChange(() => OrderedByName);
134             }
135         }
136 
137         /// <summary>
138         /// Gets a value indicating whether is auto naming enabled.
139         /// </summary>
140         public bool IsAutoNamingEnabled
141         {
142             get
143             {
144                 return this.userSettingService.GetUserSetting<bool>(UserSettingConstants.AutoNaming);
145             }
146         }
147 
148         /// <summary>
149         /// The order by title.
150         /// </summary>
OrderByTitle()151         public void OrderByTitle()
152         {
153             TitleList = new BindingList<SelectionTitle>(TitleList.OrderBy(o => o.Title.TitleNumber).ToList());
154             this.NotifyOfPropertyChange(() => TitleList);
155             this.OrderedByTitle = true;
156             this.OrderedByDuration = false;
157             this.OrderedByName = false;
158         }
159 
160         /// <summary>
161         /// The order by duration.
162         /// </summary>
OrderByDuration()163         public void OrderByDuration()
164         {
165             TitleList = new BindingList<SelectionTitle>(TitleList.OrderByDescending(o => o.Title.Duration).ToList());
166             this.NotifyOfPropertyChange(() => TitleList);
167             this.OrderedByTitle = false;
168             this.OrderedByDuration = true;
169             this.OrderedByName = false;
170         }
171 
172         /// <summary>
173         /// The order by name.
174         /// </summary>
OrderByName()175         public void OrderByName()
176         {
177             TitleList = new BindingList<SelectionTitle>(TitleList.OrderBy(o => o.Title.SourceName).ToList());
178             this.NotifyOfPropertyChange(() => TitleList);
179             this.OrderedByTitle = false;
180             this.OrderedByDuration = false;
181             this.OrderedByName = true;
182         }
183 
184         /// <summary>
185         /// The select all.
186         /// </summary>
SelectAll()187         public void SelectAll()
188         {
189             foreach (var item in TitleList)
190             {
191                 item.IsSelected = true;
192             }
193         }
194 
195         /// <summary>
196         /// The select all.
197         /// </summary>
UnSelectAll()198         public void UnSelectAll()
199         {
200             foreach (var item in TitleList)
201             {
202                 item.IsSelected = false;
203             }
204         }
205 
206         /// <summary>
207         /// Add a Preset
208         /// </summary>
Add()209         public void Add()
210         {
211             this.addToQueue(this.TitleList.Where(c => c.IsSelected));
212             this.Close();
213         }
214 
215         /// <summary>
216         /// Cancel adding a preset
217         /// </summary>
Cancel()218         public void Cancel()
219         {
220             this.TitleList.Clear();
221             this.Close();
222         }
223 
224         /// <summary>
225         /// Close this window.
226         /// </summary>
Close()227         public void Close()
228         {
229             this.TryCloseAsync();
230         }
231 
232         /// <summary>
233         /// The setup.
234         /// </summary>
235         /// <param name="scannedSource">
236         /// The scanned source.
237         /// </param>
238         /// <param name="addAction">
239         /// The add Action.
240         /// </param>
241         /// <param name="preset">
242         /// The preset.
243         /// </param>
Setup(Source scannedSource, Action<IEnumerable<SelectionTitle>> addAction, Preset preset)244         public void Setup(Source scannedSource, Action<IEnumerable<SelectionTitle>> addAction, Preset preset)
245         {
246             this.TitleList.Clear();
247             this.addToQueue = addAction;
248 
249             if (scannedSource != null)
250             {
251                 IEnumerable<Title> titles = orderedByTitle
252                                          ? scannedSource.Titles
253                                          : scannedSource.Titles.OrderByDescending(o => o.Duration).ToList();
254 
255                 foreach (Title item in titles)
256                 {
257                     string srcName = scannedSource?.SourceName ?? item.DisplaySourceName;
258                     SelectionTitle title = new SelectionTitle(item, srcName) { IsSelected = true };
259                     TitleList.Add(title);
260                 }
261             }
262 
263             if (preset != null)
264             {
265                 this.CurrentPreset = string.Format(Resources.QueueSelection_UsingPreset, preset.Name);
266             }
267 
268             this.NotifyOfPropertyChange(() => this.IsAutoNamingEnabled);
269         }
270     }
271 }
272