summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services/DriveDetectService.cs
blob: 7f1b3f9b9047ee6c46d4d3ecb4b3db390e820040 (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
// --------------------------------------------------------------------------------------------------------------------
// <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();
            }
        }
    }
}