// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // HandBrakes Main Window // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.ViewModels { using System; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.Diagnostics; using System.IO; using System.Windows; using Caliburn.Micro; using HandBrake.ApplicationServices; using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Parsing; using HandBrake.ApplicationServices.Services; using HandBrake.ApplicationServices.Services.Interfaces; using HandBrake.ApplicationServices.Utilities; using HandBrakeWPF.ViewModels.Interfaces; using Ookii.Dialogs.Wpf; /// /// HandBrakes Main Window /// [Export(typeof(IMainViewModel))] public class MainViewModel : ViewModelBase, IMainViewModel { #region Private Variables and Services /// /// The Backing field for the user setting service. /// private readonly IUserSettingService userSettingService; /// /// The Source Scan Service. /// private readonly IScan scanService; /// /// The Encode Service /// private readonly IEncode encodeService; /// /// The Encode Service /// private readonly IQueueProcessor queueProcessor; /// /// The preset service /// private readonly IPresetService presetService; /// /// HandBrakes Main Window Title /// private string windowName; /// /// The Source Label /// private string sourceLabel; /// /// The Toolbar Status Label /// private string programStatusLabel; /// /// Backing field for the scanned source. /// private Source scannedSource; /// /// Backing field for the selected title. /// private Title selectedTitle; #endregion /// /// Initializes a new instance of the class. /// The viewmodel for HandBrakes main window. /// /// /// The window manager. /// /// /// The User Setting Service /// /// /// The scan Service. /// /// /// The encode Service. /// /// /// The preset Service. /// [ImportingConstructor] public MainViewModel(IWindowManager windowManager, IUserSettingService userSettingService, IScan scanService, IEncode encodeService, IPresetService presetService) : base(windowManager) { this.userSettingService = userSettingService; this.scanService = scanService; this.encodeService = encodeService; this.presetService = presetService; this.queueProcessor = new QueueProcessor(Process.GetProcessesByName("HandBrake").Length); // Setup Properties this.WindowTitle = "HandBrake WPF Test Application"; this.CurrentTask = new EncodeTask(); this.ScannedSource = new Source(); // Setup Events this.scanService.ScanStared += this.ScanStared; this.scanService.ScanCompleted += this.ScanCompleted; this.scanService.ScanStatusChanged += this.ScanStatusChanged; this.queueProcessor.QueueCompleted += this.QueueCompleted; this.queueProcessor.QueuePaused += this.QueuePaused; this.queueProcessor.EncodeService.EncodeStarted += this.EncodeStarted; this.queueProcessor.EncodeService.EncodeStatusChanged += this.EncodeStatusChanged; } #region Properties /// /// Gets or sets TestProperty. /// public string WindowTitle { get { return this.windowName; } set { if (!object.Equals(this.windowName, value)) { this.windowName = value; } } } /// /// Gets a list of presets /// public ObservableCollection Presets { get { return this.presetService.Presets; } } /// /// Gets or sets The Current Encode Task that the user is building /// public EncodeTask CurrentTask { get; set; } /// /// Gets or sets the Last Scanned Source /// This object contains information about the scanned source. /// public Source ScannedSource { get { return this.scannedSource; } set { this.scannedSource = value; this.NotifyOfPropertyChange("ScannedSource"); } } /// /// Gets or sets SelectedTitle. /// public Title SelectedTitle { get { return this.selectedTitle; } set { if (!object.Equals(this.selectedTitle, value)) { this.selectedTitle = value; // Use the Path on the Title, or the Source Scan path if one doesn't exist. this.CurrentTask.Source = !string.IsNullOrEmpty(this.selectedTitle.SourceName) ? this.selectedTitle.SourceName : this.ScannedSource.ScanPath; this.CurrentTask.Title = value.TitleNumber; } } } /// /// Gets or sets the Source Label /// This indicates the status of scans. /// public string SourceLabel { get { return string.IsNullOrEmpty(this.sourceLabel) ? "Select 'Source' to continue" : this.sourceLabel; } set { if (!object.Equals(this.sourceLabel, value)) { this.sourceLabel = value; this.NotifyOfPropertyChange("SourceLabel"); } } } /// /// Gets or sets the Program Status Toolbar Label /// This indicates the status of HandBrake /// public string ProgramStatusLabel { get { return string.IsNullOrEmpty(this.programStatusLabel) ? "Ready" : this.sourceLabel; } set { if (!object.Equals(this.programStatusLabel, value)) { this.programStatusLabel = value; this.NotifyOfPropertyChange("ProgramStatusLabel"); } } } #endregion #region Load and Shutdown Handling /// /// Initialise this view model. /// public override void OnLoad() { // TODO } /// /// Shutdown this View /// public void Shutdown() { // Unsubscribe from Events. this.scanService.ScanStared -= this.ScanStared; this.scanService.ScanCompleted -= this.ScanCompleted; this.scanService.ScanStatusChanged -= this.ScanStatusChanged; this.queueProcessor.QueueCompleted -= this.QueueCompleted; this.queueProcessor.QueuePaused -= this.QueuePaused; this.queueProcessor.EncodeService.EncodeStarted -= this.EncodeStarted; this.queueProcessor.EncodeService.EncodeStatusChanged -= this.EncodeStatusChanged; } #endregion #region Menu and Taskbar /// /// Open the About Window /// public void OpenAboutApplication() { this.WindowManager.ShowWindow(new AboutViewModel(this.WindowManager, this.userSettingService)); } /// /// Open the Options Window /// public void OpenOptionsWindow() { this.WindowManager.ShowWindow(new OptionsViewModel(this.WindowManager, this.userSettingService)); } /// /// Open the Log Window /// public void OpenLogWindow() { this.WindowManager.ShowWindow(new LogViewModel(this.WindowManager)); } /// /// Open the Queue Window. /// public void OpenQueueWindow() { this.WindowManager.ShowWindow(new QueueViewModel(this.WindowManager)); } /// /// Launch the Help pages. /// public void LaunchHelp() { Process.Start("https://trac.handbrake.fr/wiki/HandBrakeGuide"); } /// /// Check for Updates. /// public void CheckForUpdates() { throw new NotImplementedException("Not Yet Implemented"); } /// /// Folder Scan /// public void FolderScan() { VistaFolderBrowserDialog dialog = new VistaFolderBrowserDialog { Description = "Please select a folder.", UseDescriptionForTitle = true }; dialog.ShowDialog(); this.StartScan(dialog.SelectedPath, 0); } /// /// File Scan /// public void FileScan() { VistaOpenFileDialog dialog = new VistaOpenFileDialog { Filter = "All files (*.*)|*.*" }; dialog.ShowDialog(); this.StartScan(dialog.FileName, 0); } /// /// Cancel a Scan /// public void CancelScan() { this.scanService.Stop(); } /// /// Start an Encode /// public void StartEncode() { // Santiy Checking. if (this.ScannedSource == null || this.CurrentTask == null) { throw new GeneralApplicationException("You must first scan a source.", string.Empty, null); } if (string.IsNullOrEmpty(this.CurrentTask.Destination)) { throw new GeneralApplicationException("The Destination field was empty.", "You must first set a destination for the encoded file.", null); } if (this.queueProcessor.IsProcessing) { throw new GeneralApplicationException("HandBrake is already encoding.", string.Empty, null); } if (File.Exists(this.CurrentTask.Destination)) { // TODO: File Overwrite warning. } // Create the Queue Task and Start Processing QueueTask task = new QueueTask(null) { Destination = this.CurrentTask.Destination, Task = this.CurrentTask, Query = QueryGeneratorUtility.GenerateQuery(this.CurrentTask), CustomQuery = false }; this.queueProcessor.QueueManager.Add(task); this.queueProcessor.Start(); } /// /// Pause an Encode /// public void PauseEncode() { this.queueProcessor.Pause(); } /// /// Stop an Encode. /// public void StopEncode() { this.encodeService.Stop(); } /// /// Shutdown the Application /// public void ExitApplication() { Application.Current.Shutdown(); } #endregion #region Main Window /// /// The Destination Path /// public void BrowseDestination() { VistaSaveFileDialog dialog = new VistaSaveFileDialog { Filter = "MP4 File (*.mp4)|Mkv File(*.mkv)" }; dialog.ShowDialog(); dialog.AddExtension = true; this.CurrentTask.Destination = dialog.FileName; this.NotifyOfPropertyChange("CurrentTask"); } #endregion #region Private Worker Methods /// /// Start a Scan /// /// /// The filename. /// /// /// The title. /// public void StartScan(string filename, int title) { // TODO // 1. Disable GUI. this.scanService.Scan(filename, title, this.userSettingService.GetUserSetting(ASUserSettingConstants.PreviewScanCount)); } #endregion #region Event Handlers /// /// Handle the Scan Status Changed Event. /// /// /// The Sender /// /// /// The EventArgs /// private void ScanStatusChanged(object sender, HandBrake.ApplicationServices.EventArgs.ScanProgressEventArgs e) { this.SourceLabel = "Scanning Title " + e.CurrentTitle + " of " + e.Titles; } /// /// Handle the Scan Completed Event /// /// /// The Sender /// /// /// The EventArgs /// private void ScanCompleted(object sender, HandBrake.ApplicationServices.EventArgs.ScanCompletedEventArgs e) { if (e.Successful) { this.scanService.SouceData.CopyTo(this.ScannedSource); this.NotifyOfPropertyChange("ScannedSource"); this.NotifyOfPropertyChange("ScannedSource.Titles"); } this.SourceLabel = "Scan Completed"; // TODO Re-enable GUI. } /// /// Handle the Scan Started Event /// /// /// The Sender /// /// /// The EventArgs /// private void ScanStared(object sender, EventArgs e) { // TODO - Disable relevant parts of the UI. } /// /// The Encode Status has changed Handler /// /// /// The Sender /// /// /// The Encode Progress Event Args /// private void EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EventArgs.EncodeProgressEventArgs e) { ProgramStatusLabel = string.Format( "{0:00.00}%, FPS: {1:000.0}, Avg FPS: {2:000.0}, Time Remaining: {3}, Elapsed: {4:hh\\:mm\\:ss}, Pending Jobs {5}", e.PercentComplete, e.CurrentFrameRate, e.AverageFrameRate, e.EstimatedTimeLeft, e.ElapsedTime, this.queueProcessor.QueueManager.Count); } /// /// Encode Started Handler /// /// /// The Sender /// /// /// The EventArgs /// private void EncodeStarted(object sender, EventArgs e) { // TODO Handle Updating the UI } /// /// The Queue has been paused handler /// /// /// The Sender /// /// /// The EventArgs /// private void QueuePaused(object sender, EventArgs e) { // TODO Handle Updating the UI } /// /// The Queue has completed handler /// /// /// The Sender /// /// /// The EventArgs /// private void QueueCompleted(object sender, EventArgs e) { // TODO Handle Updating the UI } #endregion } }