// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The drive menu.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.AttachedProperties
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Utilities;
using HandBrakeWPF.Commands;
using HandBrakeWPF.Model;
using HandBrakeWPF.ViewModels;
///
/// The drive menu.
///
public class DriveMenu
{
///
/// The show available drives property.
///
public static readonly DependencyProperty ShowAvailableDrivesProperty = DependencyProperty.RegisterAttached(
"ShowAvailableDrives",
typeof(bool),
typeof(DriveMenu),
new PropertyMetadata(false, OnShowAvailableDrivesChanged));
///
/// The get show available drives.
///
///
/// The element.
///
///
/// The .
///
public static Boolean GetShowAvailableDrives(MenuItem element)
{
bool result;
return bool.TryParse(element.GetValue(ShowAvailableDrivesProperty).ToString(), out result) && result;
}
///
/// The set show available drives.
///
///
/// The element.
///
///
/// The value.
///
public static void SetShowAvailableDrives(MenuItem element, Boolean value)
{
element.SetValue(ShowAvailableDrivesProperty, value);
}
///
/// The on show available drives changed.
///
///
/// The d.
///
///
/// The e.
///
private static void OnShowAvailableDrivesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MenuItem menu = d as MenuItem;
if (menu != null)
{
menu.SubmenuOpened -= MenuMouseDown;
menu.SubmenuOpened += MenuMouseDown;
}
}
///
/// The menu_ mouse down.
///
///
/// The sender.
///
///
/// The e.
///
private static void MenuMouseDown(object sender, RoutedEventArgs e)
{
MenuItem menu = sender as MenuItem;
MenuItem childMenuItem = e.OriginalSource as MenuItem;
if (childMenuItem != null && "Title Specific Scan".Equals(childMenuItem.Header))
{
return; // Skip, it's just a child menu.
}
if (menu != null)
{
MainViewModel mvm = menu.DataContext as MainViewModel;
if (mvm != null)
{
List 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
{
Text = string.Format("{0} ({1})", item.RootDirectory, item.VolumeLabel),
Command = new SourceMenuCommand(() => mvm.ProcessDrive(driveInformation)),
Tag = item,
IsDrive = true
})
{
mvm.SourceMenu.Add(menuItem);
}
}
else
{
throw new GeneralApplicationException(
"DEBUG - Datacontext wasn't set!", "Please report this on the forum.", null);
}
}
else
{
throw new GeneralApplicationException(
"DEBUG - Source Menu wasn't set!", "Please report this on the forum.", null);
}
}
}
}