summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr55 <[email protected]>2012-07-14 12:09:06 +0000
committersr55 <[email protected]>2012-07-14 12:09:06 +0000
commite2432168fed60439aa461284e01af142829450ec (patch)
tree846d4f0c61f86aab02d20d77e3b10050bdc01bd3
parent2425840702b5e319165384f73a51874349ee6d84 (diff)
WinGui: Working drive tray insert/remove detection.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4829 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--win/CS/HandBrakeWPF/HandBrakeWPF.csproj2
-rw-r--r--win/CS/HandBrakeWPF/Services/DriveDetectService.cs89
-rw-r--r--win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs32
-rw-r--r--win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs1
-rw-r--r--win/CS/HandBrakeWPF/ViewModels/Interfaces/IMainViewModel.cs5
-rw-r--r--win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs42
-rw-r--r--win/CS/HandBrakeWPF/ViewModels/ShellViewModel.cs2
-rw-r--r--win/CS/HandBrakeWPF/Views/MainView.xaml30
-rw-r--r--win/CS/HandBrakeWPF/Views/MainView.xaml.cs4
9 files changed, 148 insertions, 59 deletions
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 @@
<Compile Include="Converters\Options\OptionsTabConverter.cs" />
<Compile Include="Converters\Subtitles\SubtitlesQueueDisplayConverter.cs" />
<Compile Include="Converters\Video\VideoEncoderConverter.cs" />
+ <Compile Include="Services\DriveDetectService.cs" />
+ <Compile Include="Services\Interfaces\IDriveDetectService.cs" />
<Compile Include="Model\ShellWindow.cs" />
<Compile Include="Model\SourceMenuItem.cs" />
<Compile Include="Model\UpdateCheckInformation.cs" />
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 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="DriveDetectService.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Drive Detection Helper.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services
+{
+ using System;
+ using System.Management;
+
+ using HandBrakeWPF.Services.Interfaces;
+
+ /// <summary>
+ /// Drive Detection Helper.
+ /// </summary>
+ public class DriveDetectService : IDriveDetectService
+ {
+ /// <summary>
+ /// The watcher.
+ /// </summary>
+ private ManagementEventWatcher watcher;
+
+ /// <summary>
+ /// The detection action.
+ /// </summary>
+ private Action detectionAction;
+
+ /// <summary>
+ /// The start detection.
+ /// </summary>
+ /// <param name="action">
+ /// The detection Action.
+ /// </param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// The close.
+ /// </summary>
+ public void Close()
+ {
+ this.watcher.Stop();
+ }
+
+ /// <summary>
+ /// The watcher_ event arrived.
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EventArrivedEventArgs.
+ /// </param>
+ 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 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="IDriveDetectService.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Defines the IDriveDetectService type.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services.Interfaces
+{
+ using System;
+
+ /// <summary>
+ /// The DriveDetectService interface.
+ /// </summary>
+ public interface IDriveDetectService
+ {
+ /// <summary>
+ /// The start detection.
+ /// </summary>
+ /// <param name="action">
+ /// The detection Action.
+ /// </param>
+ void StartDetection(Action action);
+
+ /// <summary>
+ /// Stop the watcher. Must be done before the app shuts down.
+ /// </summary>
+ 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<IUpdateService>().ImplementedBy<UpdateService>().LifeStyle.Is(LifestyleType.Singleton));
+ this.windsorContainer.Register(Component.For<IDriveDetectService>().ImplementedBy<DriveDetectService>().LifeStyle.Is(LifestyleType.Singleton));
// Shell
this.windsorContainer.Register(Component.For<IErrorService>().ImplementedBy<ErrorService>().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.
/// </param>
void EditQueueJob(EncodeTask task);
+
+ /// <summary>
+ /// Shutdown this View
+ /// </summary>
+ 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;
@@ -83,6 +81,11 @@ namespace HandBrakeWPF.ViewModels
private readonly IUpdateService updateService;
/// <summary>
+ /// The drive detect service.
+ /// </summary>
+ private readonly IDriveDetectService driveDetectService;
+
+ /// <summary>
/// Backing field for the user setting service.
/// </summary>
private readonly IUserSettingService userSettingService;
@@ -186,9 +189,12 @@ namespace HandBrakeWPF.ViewModels
/// <param name="updateService">
/// The update Service.
/// </param>
+ /// <param name="driveDetectService">
+ /// The drive Detect Service.
+ /// </param>
[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<IQueueProcessor>();
- 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);
}
/// <summary>
@@ -803,6 +812,9 @@ namespace HandBrakeWPF.ViewModels
/// </summary>
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;
}
- /// <summary>
- /// Handle the Window Closing Event and warn the user if an encode is in-progress
- /// </summary>
- /// <param name="e">
- /// The CancelEventArgs.
- /// </param>
- 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 @@
<Micro:Parameter Value="$eventArgs" />
</Micro:ActionMessage>
</i:EventTrigger>
-
- <i:EventTrigger EventName="Closing">
- <Micro:ActionMessage MethodName="HandleWindowClosing">
- <Micro:Parameter Value="$eventArgs" />
- </Micro:ActionMessage>
- </i:EventTrigger>
</i:Interaction.Triggers>
<UserControl.Resources>
@@ -48,19 +42,6 @@
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
- <DataTemplate x:Key="presetsTemplate">
- <StackPanel>
- <TextBlock Text="{Binding Path=Name}" />
- </StackPanel>
- </DataTemplate>
-
- <HierarchicalDataTemplate x:Key="presetsCategoryTemplate"
- ItemsSource="{Binding Path=Items}"
- ItemTemplate="{StaticResource presetsTemplate}"
- >
- <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
- </HierarchicalDataTemplate>
-
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0,2,0,2" />
<Style.Triggers>
@@ -181,17 +162,6 @@
<Setter Property="Icon" Value="{Binding Image}" />
</Style>
</MenuItem.ItemContainerStyle>
- <!--<HierarchicalDataTemplate DataType="{x:Type Model:SourceMenuItem}"
- ItemsSource="{Binding Path=Children}">
- <HierarchicalDataTemplate.ItemContainerStyle>
- <Style TargetType="MenuItem">
- <Setter Property="Command" Value="{Binding Command}" />
- <Setter Property="Icon" Value="{Binding Image}" />
- </Style>
- </HierarchicalDataTemplate.ItemContainerStyle>
- <TextBlock Text="{Binding Text}" />
- </HierarchicalDataTemplate>-->
-
</MenuItem>
</Menu>
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;
/// <summary>
/// Interaction logic for MainView.xaml
@@ -24,7 +22,7 @@ namespace HandBrakeWPF.Views
/// </summary>
public MainView()
{
- InitializeComponent();
+ this.InitializeComponent();
}
/// <summary>