From e2432168fed60439aa461284e01af142829450ec Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 14 Jul 2012 12:09:06 +0000 Subject: WinGui: Working drive tray insert/remove detection. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4829 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/CS/HandBrakeWPF/HandBrakeWPF.csproj | 2 + win/CS/HandBrakeWPF/Services/DriveDetectService.cs | 89 ++++++++++++++++++++++ .../Services/Interfaces/IDriveDetectService.cs | 32 ++++++++ win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs | 1 + .../ViewModels/Interfaces/IMainViewModel.cs | 5 ++ win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 42 ++++------ win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs | 2 + win/CS/HandBrakeWPF/Views/MainView.xaml | 30 -------- win/CS/HandBrakeWPF/Views/MainView.xaml.cs | 4 +- 9 files changed, 148 insertions(+), 59 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Services/DriveDetectService.cs create mode 100644 win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs (limited to 'win/CS') diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index e6c2fba7f..b9b5a69da 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -127,6 +127,8 @@ + + diff --git a/win/CS/HandBrakeWPF/Services/DriveDetectService.cs b/win/CS/HandBrakeWPF/Services/DriveDetectService.cs new file mode 100644 index 000000000..7f1b3f9b9 --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/DriveDetectService.cs @@ -0,0 +1,89 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Drive Detection Helper. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services +{ + using System; + using System.Management; + + using HandBrakeWPF.Services.Interfaces; + + /// + /// Drive Detection Helper. + /// + public class DriveDetectService : IDriveDetectService + { + /// + /// The watcher. + /// + private ManagementEventWatcher watcher; + + /// + /// The detection action. + /// + private Action detectionAction; + + /// + /// The start detection. + /// + /// + /// The detection Action. + /// + public void StartDetection(Action action) + { + this.detectionAction = action; + + var options = new ConnectionOptions { EnablePrivileges = true }; + var scope = new ManagementScope(@"root\CIMV2", options); + + try + { + var query = new WqlEventQuery + { + EventClassName = "__InstanceModificationEvent", + WithinInterval = TimeSpan.FromSeconds(1), + Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5" // DriveType - 5: CDROM + }; + + this.watcher = new ManagementEventWatcher(scope, query); + this.watcher.EventArrived += this.WatcherEventArrived; + this.watcher.Start(); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + + /// + /// The close. + /// + public void Close() + { + this.watcher.Stop(); + } + + /// + /// The watcher_ event arrived. + /// + /// + /// The sender. + /// + /// + /// The EventArrivedEventArgs. + /// + private void WatcherEventArrived(object sender, EventArrivedEventArgs e) + { + if (this.detectionAction != null) + { + this.detectionAction(); + } + } + } +} diff --git a/win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs b/win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs new file mode 100644 index 000000000..16ef42a4f --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs @@ -0,0 +1,32 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the IDriveDetectService type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services.Interfaces +{ + using System; + + /// + /// The DriveDetectService interface. + /// + public interface IDriveDetectService + { + /// + /// The start detection. + /// + /// + /// The detection Action. + /// + void StartDetection(Action action); + + /// + /// Stop the watcher. Must be done before the app shuts down. + /// + void Close(); + } +} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs b/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs index 632b93877..dadae2026 100644 --- a/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs +++ b/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs @@ -53,6 +53,7 @@ namespace HandBrakeWPF.Startup // Services this.windsorContainer.Register(Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton)); + this.windsorContainer.Register(Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton)); // Shell this.windsorContainer.Register(Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton)); diff --git a/win/CS/HandBrakeWPF/ViewModels/Interfaces/IMainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/Interfaces/IMainViewModel.cs index 079ce3ad5..d719ce866 100644 --- a/win/CS/HandBrakeWPF/ViewModels/Interfaces/IMainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/Interfaces/IMainViewModel.cs @@ -68,5 +68,10 @@ namespace HandBrakeWPF.ViewModels.Interfaces /// The task. /// void EditQueueJob(EncodeTask task); + + /// + /// Shutdown this View + /// + void Shutdown(); } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index d9bc80b3f..aea8461bc 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -11,12 +11,10 @@ namespace HandBrakeWPF.ViewModels { using System; using System.Collections.Generic; - using System.ComponentModel; using System.ComponentModel.Composition; using System.Diagnostics; using System.IO; using System.Linq; - using System.Threading; using System.Windows; using System.Windows.Media.Imaging; @@ -82,6 +80,11 @@ namespace HandBrakeWPF.ViewModels /// private readonly IUpdateService updateService; + /// + /// The drive detect service. + /// + private readonly IDriveDetectService driveDetectService; + /// /// Backing field for the user setting service. /// @@ -186,9 +189,12 @@ namespace HandBrakeWPF.ViewModels /// /// The update Service. /// + /// + /// The drive Detect Service. + /// [ImportingConstructor] public MainViewModel(IWindowManager windowManager, IUserSettingService userSettingService, IScan scanService, IEncode encodeService, IPresetService presetService, - IErrorService errorService, IShellViewModel shellViewModel, IUpdateService updateService) + IErrorService errorService, IShellViewModel shellViewModel, IUpdateService updateService, IDriveDetectService driveDetectService) { GeneralUtilities.SetInstanceId(); @@ -199,9 +205,10 @@ namespace HandBrakeWPF.ViewModels this.errorService = errorService; this.shellViewModel = shellViewModel; this.updateService = updateService; + this.driveDetectService = driveDetectService; this.userSettingService = userSettingService; this.queueProcessor = IoC.Get(); - queueProcessor.QueueManager.ResetInstanceId(); + this.queueProcessor.QueueManager.ResetInstanceId(); // Setup Properties this.WindowTitle = "HandBrake"; @@ -796,6 +803,8 @@ namespace HandBrakeWPF.ViewModels // Populate the Source menu with drives. this.SourceMenu = this.GenerateSourceMenu(); + + this.driveDetectService.StartDetection(this.DriveTrayChanged); } /// @@ -803,6 +812,9 @@ namespace HandBrakeWPF.ViewModels /// public void Shutdown() { + // Shutdown Service + this.driveDetectService.Close(); + // Unsubscribe from Events. this.scanService.ScanStared -= this.ScanStared; this.scanService.ScanCompleted -= this.ScanCompleted; @@ -813,28 +825,6 @@ namespace HandBrakeWPF.ViewModels this.queueProcessor.EncodeService.EncodeStatusChanged -= this.EncodeStatusChanged; } - /// - /// Handle the Window Closing Event and warn the user if an encode is in-progress - /// - /// - /// The CancelEventArgs. - /// - public void HandleWindowClosing(CancelEventArgs e) - { - if (this.encodeService.IsEncoding) - { - MessageBoxResult result = this.errorService.ShowMessageBox("HandBrake is currently encoding. Closing now will abort your encode.\nAre you sure you wish to exit?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning); - if (result == MessageBoxResult.No) - { - e.Cancel = true; - } - else - { - this.encodeService.Stop(); - } - } - } - #endregion #region Menu and Taskbar diff --git a/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs index ef3f1a53a..adb1f587b 100644 --- a/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs @@ -162,11 +162,13 @@ namespace HandBrakeWPF.ViewModels { processor.Pause(); processor.EncodeService.Stop(); + this.MainViewModel.Shutdown(); return true; } return false; } + this.MainViewModel.Shutdown(); return true; } } diff --git a/win/CS/HandBrakeWPF/Views/MainView.xaml b/win/CS/HandBrakeWPF/Views/MainView.xaml index 7b78e50e3..5f6fe7f14 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml @@ -20,12 +20,6 @@ - - - - - - @@ -48,19 +42,6 @@ - - - - - - - - - - - - diff --git a/win/CS/HandBrakeWPF/Views/MainView.xaml.cs b/win/CS/HandBrakeWPF/Views/MainView.xaml.cs index c6bb63568..0c6c291c9 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml.cs +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml.cs @@ -9,10 +9,8 @@ namespace HandBrakeWPF.Views { - using System; using System.Windows; using System.Windows.Controls; - using System.Windows.Input; /// /// Interaction logic for MainView.xaml @@ -24,7 +22,7 @@ namespace HandBrakeWPF.Views /// public MainView() { - InitializeComponent(); + this.InitializeComponent(); } /// -- cgit v1.2.3