diff options
author | sr55 <[email protected]> | 2013-05-26 16:11:55 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2013-05-26 16:11:55 +0000 |
commit | bb402730363d0a2192015f5062f9fda409bf743e (patch) | |
tree | 9b1b2ba85af0659c3d1410fa0cc3bd4dc30c9388 /win/CS/HandBrakeWPF | |
parent | ab11d3012d39eaa0991edbd069c602241d63905c (diff) |
WinGui: Add an attached property to the source menu to handle drive detection rather than relying on callbacks from the OS.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5517 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/CS/HandBrakeWPF')
-rw-r--r-- | win/CS/HandBrakeWPF/AttachedProperties/DriveMenu.cs | 127 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/HandBrakeWPF.csproj | 3 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Model/SourceMenuItem.cs | 5 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/DriveDetectService.cs | 97 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs | 32 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs | 1 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 49 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Views/MainView.xaml | 5 |
8 files changed, 153 insertions, 166 deletions
diff --git a/win/CS/HandBrakeWPF/AttachedProperties/DriveMenu.cs b/win/CS/HandBrakeWPF/AttachedProperties/DriveMenu.cs new file mode 100644 index 000000000..1149b579e --- /dev/null +++ b/win/CS/HandBrakeWPF/AttachedProperties/DriveMenu.cs @@ -0,0 +1,127 @@ +// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="DriveMenu.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>
+// The drive menu.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.AttachedProperties
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Windows;
+ using System.Windows.Controls;
+ using System.Windows.Media.Imaging;
+
+ using HandBrake.ApplicationServices.Utilities;
+
+ using HandBrakeWPF.Commands;
+ using HandBrakeWPF.Model;
+ using HandBrakeWPF.ViewModels;
+
+ /// <summary>
+ /// The drive menu.
+ /// </summary>
+ public class DriveMenu
+ {
+ /// <summary>
+ /// The show available drives property.
+ /// </summary>
+ public static readonly DependencyProperty ShowAvailableDrivesProperty = DependencyProperty.RegisterAttached(
+ "ShowAvailableDrives",
+ typeof(Boolean),
+ typeof(DriveMenu),
+ new PropertyMetadata(false, OnShowAvailableDrivesChanged));
+
+ /// <summary>
+ /// The get show available drives.
+ /// </summary>
+ /// <param name="element">
+ /// The element.
+ /// </param>
+ /// <returns>
+ /// The <see cref="bool"/>.
+ /// </returns>
+ public static Boolean GetShowAvailableDrives(MenuItem element)
+ {
+ bool result;
+ return bool.TryParse(element.GetValue(ShowAvailableDrivesProperty).ToString(), out result) && result;
+ }
+
+ /// <summary>
+ /// The set show available drives.
+ /// </summary>
+ /// <param name="element">
+ /// The element.
+ /// </param>
+ /// <param name="value">
+ /// The value.
+ /// </param>
+ public static void SetShowAvailableDrives(MenuItem element, Boolean value)
+ {
+ element.SetValue(ShowAvailableDrivesProperty, value);
+ }
+
+ /// <summary>
+ /// The on show available drives changed.
+ /// </summary>
+ /// <param name="d">
+ /// The d.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private static void OnShowAvailableDrivesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ Menu menu = d as Menu;
+ if (menu != null)
+ {
+ menu.PreviewMouseDown -= MenuMouseDown;
+ menu.PreviewMouseDown += MenuMouseDown;
+ }
+ }
+
+ /// <summary>
+ /// The menu_ mouse down.
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private static void MenuMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+ {
+ Menu menu = sender as Menu;
+ if (menu != null)
+ {
+ MainViewModel mvm = menu.DataContext as MainViewModel;
+ if (mvm != null)
+ {
+ List<SourceMenuItem> remove = mvm.SourceMenu.Where(s => s.IsDrive).ToList();
+ foreach (var item in remove)
+ {
+ mvm.SourceMenu.Remove(item);
+ }
+
+ foreach (SourceMenuItem menuItem in from item in GeneralUtilities.GetDrives()
+ let driveInformation = item
+ select new SourceMenuItem
+ {
+ Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/disc_small.png")), Width = 16, Height = 16 },
+ Text = string.Format("{0} ({1})", item.RootDirectory, item.VolumeLabel),
+ Command = new SourceMenuCommand(() => mvm.ProcessDrive(driveInformation)),
+ Tag = item,
+ IsDrive = true
+ })
+ {
+ mvm.SourceMenu.Add(menuItem);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index 9e22a9dc6..bc8018c08 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -123,6 +123,7 @@ <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
+ <Compile Include="AttachedProperties\DriveMenu.cs" />
<Compile Include="AttachedProperties\MenuItemExtensions.cs" />
<Compile Include="Commands\CancelScanCommand.cs" />
<Compile Include="Commands\Interfaces\IAdvancedEncoderOptionsCommand.cs" />
@@ -157,9 +158,7 @@ <Compile Include="Helpers\GrowlCommunicator.cs" />
<Compile Include="Model\OptionsTab.cs" />
<Compile Include="Model\SelectionTitle.cs" />
- <Compile Include="Services\DriveDetectService.cs" />
<Compile Include="Services\EncodeServiceWrapper.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/Model/SourceMenuItem.cs b/win/CS/HandBrakeWPF/Model/SourceMenuItem.cs index b0c4a9766..f0b809849 100644 --- a/win/CS/HandBrakeWPF/Model/SourceMenuItem.cs +++ b/win/CS/HandBrakeWPF/Model/SourceMenuItem.cs @@ -47,6 +47,11 @@ namespace HandBrakeWPF.Model public ObservableCollection<SourceMenuItem> Children { get; set; }
/// <summary>
+ /// Gets or sets a value indicating whether is drive.
+ /// </summary>
+ public bool IsDrive { get; set; }
+
+ /// <summary>
/// Gets or sets the tag.
/// </summary>
public object Tag { get; set; }
diff --git a/win/CS/HandBrakeWPF/Services/DriveDetectService.cs b/win/CS/HandBrakeWPF/Services/DriveDetectService.cs deleted file mode 100644 index 3a4ef5113..000000000 --- a/win/CS/HandBrakeWPF/Services/DriveDetectService.cs +++ /dev/null @@ -1,97 +0,0 @@ -// --------------------------------------------------------------------------------------------------------------------
-// <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 System.Threading;
-
- 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)
- {
- ThreadPool.QueueUserWorkItem(
- delegate
- {
- 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()
- {
- if (watcher != null)
- {
- 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 deleted file mode 100644 index 16ef42a4f..000000000 --- a/win/CS/HandBrakeWPF/Services/Interfaces/IDriveDetectService.cs +++ /dev/null @@ -1,32 +0,0 @@ -// --------------------------------------------------------------------------------------------------------------------
-// <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 a384815ee..87a92824a 100644 --- a/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs +++ b/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs @@ -57,7 +57,6 @@ 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));
this.windsorContainer.Register(Component.For<IScanServiceWrapper>().ImplementedBy<ScanServiceWrapper>().LifeStyle.Is(LifestyleType.Singleton));
this.windsorContainer.Register(Component.For<IEncodeServiceWrapper>().ImplementedBy<EncodeServiceWrapper>().LifeStyle.Is(LifestyleType.Singleton));
this.windsorContainer.Register(Component.For<INotificationService>().ImplementedBy<NotificationService>().LifeStyle.Is(LifestyleType.Singleton));
diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index 54911cd81..5fad2dca6 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -11,6 +11,7 @@ namespace HandBrakeWPF.ViewModels {
using System;
using System.Collections.Generic;
+ using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
@@ -75,11 +76,6 @@ 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;
@@ -167,7 +163,7 @@ namespace HandBrakeWPF.ViewModels /// <summary>
/// The Source Menu Backing Field
/// </summary>
- private IEnumerable<SourceMenuItem> sourceMenu;
+ private BindingList<SourceMenuItem> sourceMenu;
/// <summary>
/// The last percentage complete value.
@@ -200,9 +196,6 @@ namespace HandBrakeWPF.ViewModels /// <param name="updateService">
/// The update Service.
/// </param>
- /// <param name="driveDetectService">
- /// The drive Detect Service.
- /// </param>
/// <param name="notificationService">
/// The notification Service.
/// *** Leave in Constructor. ***
@@ -212,7 +205,7 @@ namespace HandBrakeWPF.ViewModels /// *** Leave in Constructor. ***
/// </param>
public MainViewModel(IUserSettingService userSettingService, IScanServiceWrapper scanService, IEncodeServiceWrapper encodeService, IPresetService presetService,
- IErrorService errorService, IShellViewModel shellViewModel, IUpdateService updateService, IDriveDetectService driveDetectService, INotificationService notificationService,
+ IErrorService errorService, IShellViewModel shellViewModel, IUpdateService updateService, INotificationService notificationService,
IPrePostActionService whenDoneService)
{
this.scanService = scanService;
@@ -221,7 +214,6 @@ namespace HandBrakeWPF.ViewModels this.errorService = errorService;
this.shellViewModel = shellViewModel;
this.updateService = updateService;
- this.driveDetectService = driveDetectService;
this.userSettingService = userSettingService;
this.queueProcessor = IoC.Get<IQueueProcessor>();
@@ -348,7 +340,7 @@ namespace HandBrakeWPF.ViewModels /// <summary>
/// Gets or sets the source menu.
/// </summary>
- public IEnumerable<SourceMenuItem> SourceMenu
+ public BindingList<SourceMenuItem> SourceMenu
{
get
{
@@ -925,9 +917,7 @@ namespace HandBrakeWPF.ViewModels this.SelectedPreset = this.presetService.DefaultPreset;
// Populate the Source menu with drives.
- this.SourceMenu = this.GenerateSourceMenu();
-
- this.driveDetectService.StartDetection(this.DriveTrayChanged);
+ this.SourceMenu = new BindingList<SourceMenuItem>(this.GenerateSourceMenu());
// Log Cleaning
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.ClearOldLogs))
@@ -943,8 +933,6 @@ namespace HandBrakeWPF.ViewModels public void Shutdown()
{
// Shutdown Service
- this.driveDetectService.Close();
-
this.scanService.Shutdown();
this.encodeService.Shutdown();
@@ -1954,7 +1942,7 @@ namespace HandBrakeWPF.ViewModels /// <param name="item">
/// The item.
/// </param>
- private void ProcessDrive(object item)
+ public void ProcessDrive(object item)
{
if (item != null)
{
@@ -1968,7 +1956,7 @@ namespace HandBrakeWPF.ViewModels /// <returns>
/// The System.Collections.Generic.IEnumerable`1[T -> HandBrakeWPF.Model.SourceMenuItem].
/// </returns>
- private IEnumerable<SourceMenuItem> GenerateSourceMenu()
+ private IList<SourceMenuItem> GenerateSourceMenu()
{
List<SourceMenuItem> menuItems = new List<SourceMenuItem>();
@@ -1976,13 +1964,15 @@ namespace HandBrakeWPF.ViewModels {
Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/folder.png")), Width = 16, Height = 16 },
Text = "Open Folder",
- Command = new SourceMenuCommand(this.FolderScan)
+ Command = new SourceMenuCommand(this.FolderScan),
+ IsDrive = false
};
SourceMenuItem fileScan = new SourceMenuItem
{
Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/Movies.png")), Width = 16, Height = 16 },
Text = "Open File",
- Command = new SourceMenuCommand(this.FileScan)
+ Command = new SourceMenuCommand(this.FileScan),
+ IsDrive = false
};
SourceMenuItem titleSpecific = new SourceMenuItem { Text = "Title Specific Scan" };
@@ -1990,13 +1980,15 @@ namespace HandBrakeWPF.ViewModels {
Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/folder.png")), Width = 16, Height = 16 },
Text = "Open Folder",
- Command = new SourceMenuCommand(this.FolderScanTitleSpecific)
+ Command = new SourceMenuCommand(this.FolderScanTitleSpecific),
+ IsDrive = false
};
SourceMenuItem fileScanTitle = new SourceMenuItem
{
Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/Movies.png")), Width = 16, Height = 16 },
Text = "Open File",
- Command = new SourceMenuCommand(this.FileScanTitleSpecific)
+ Command = new SourceMenuCommand(this.FileScanTitleSpecific),
+ IsDrive = false
};
titleSpecific.Children.Add(folderScanTitle);
titleSpecific.Children.Add(fileScanTitle);
@@ -2015,21 +2007,14 @@ namespace HandBrakeWPF.ViewModels Image = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/HandBrake;component/Views/Images/disc_small.png")), Width = 16, Height = 16 },
Text = string.Format("{0} ({1})", item.RootDirectory, item.VolumeLabel),
Command = new SourceMenuCommand(() => this.ProcessDrive(driveInformation)),
- Tag = item
+ Tag = item,
+ IsDrive = true
});
return menuItems;
}
/// <summary>
- /// The drive tray changed.
- /// </summary>
- private void DriveTrayChanged()
- {
- Caliburn.Micro.Execute.OnUIThread(() => this.SourceMenu = this.GenerateSourceMenu());
- }
-
- /// <summary>
/// Allows the main window to respond to setting changes.
/// </summary>
/// <param name="sender">
diff --git a/win/CS/HandBrakeWPF/Views/MainView.xaml b/win/CS/HandBrakeWPF/Views/MainView.xaml index 92d0d0955..8d7575f54 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml @@ -5,7 +5,8 @@ xmlns:Converters="clr-namespace:HandBrakeWPF.Converters"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
- AllowDrop="True"
+ xmlns:attachedProperties="clr-namespace:HandBrakeWPF.AttachedProperties"
+ AllowDrop="True"
Background="#FFF0F0F0"
FontSize="11"
Micro:Message.Attach="[Event Loaded] = [Action Load]"
@@ -146,7 +147,7 @@ ToolBar.OverflowMode="Never"
ToolBarTray.IsLocked="True"
>
- <Menu Background="Transparent">
+ <Menu Background="Transparent" attachedProperties:DriveMenu.ShowAvailableDrives="true">
<MenuItem ItemsSource="{Binding SourceMenu}">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
|