// --------------------------------------------------------------------------------------------------------------------
//
// 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.Windows;
using Caliburn.Micro;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Parsing;
using HandBrake.ApplicationServices.Services;
using HandBrake.ApplicationServices.Services.Interfaces;
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;
#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 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()
{
throw new NotImplementedException("Not Yet Implemented");
}
///
/// Pause an Encode
///
public void PauseEncode()
{
throw new NotImplementedException("Not Yet Implemented");
}
///
/// Stop an Encode.
///
public void StopEncode()
{
throw new NotImplementedException("Not Yet Implemented");
}
///
/// Shutdown the Application
///
public void ExitApplication()
{
Application.Current.Shutdown();
}
#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(UserSettingConstants.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)
{
//
}
///
/// 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
}
}