summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/AttachedProperties/DriveMenu.cs
blob: 1149b579ebfc75247d878bd09dc38dcc484bad2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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);
                    }
                }
            }
        }
    }
}