-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConnectedDevicesManagerImpl.cs
188 lines (160 loc) · 6.33 KB
/
ConnectedDevicesManagerImpl.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using Notpod.Configuration12;
namespace Notpod
{
/// <summary>
/// Implementation of IConnectedDevicesManger.
/// </summary>
public class ConnectedDevicesManagerImpl : IConnectedDevicesManager
{
private DeviceConfiguration deviceConfig;
private Hashtable connectedDevices = new Hashtable();
#region IConnectedDevicesManager Members
public event DeviceConnectedEventHandler DeviceConnected;
public event DeviceDisconnectedEventHandler DeviceDisconnected;
/// <summary>
/// <see cref="Notpod.IConnectedDevicesManagerImpl#DeviceConfiguration"/>
/// </summary>
public DeviceConfiguration DeviceConfig
{
get
{
return deviceConfig;
}
set
{
deviceConfig = value;
}
}
/// <summary>
/// <see cref="Notpod.IConnectedDevicesManagerImpl#Synchronize(DriveInfo[])"/>
/// </summary>
/// <param name="drives"></param>
public void Synchronize(ArrayList drives)
{
CheckForDisconnectedDevices(drives);
CheckForConnectedDevices(drives);
}
/// <summary>
/// Check if any of the registered devices has been removed from the system.
/// </summary>
/// <param name="drives">List of currently available removable drives.</param>
private void CheckForDisconnectedDevices(ArrayList drives)
{
IEnumerator keys = connectedDevices.Keys.GetEnumerator();
while (keys.MoveNext())
{
string driveLetter = (string)keys.Current;
bool found = false;
Device device = (Device)connectedDevices[keys.Current];
//Loop through all drive info objects to look for the current drive.
foreach (DriveInfo di in drives)
{
Device recognized = RecognizeDevice(di);
if (di.Name == driveLetter && (recognized != null && recognized.Name == device.Name))
{
found = true;
break;
}
}
if (!found)
{
RemoveDevice(driveLetter);
OnDeviceDisconnected(driveLetter, device);
keys = connectedDevices.Keys.GetEnumerator();
}
}
}
/// <summary>
/// Check for connected devices.
/// </summary>
/// <param name="drives">List of currently connected removable drives.</param>
private void CheckForConnectedDevices(ArrayList drives)
{
//Loop over all DriveInfo's and check if any of them matches
//the defined patterns for devices that Notpod recognizes.
foreach (DriveInfo di in drives)
{
Device recognized = RecognizeDevice(di);
if (recognized == null)
continue;
if (connectedDevices.ContainsKey(di.Name))
continue;
connectedDevices.Add(di.Name, recognized);
OnDeviceConnected(di, recognized);
}
}
/// <summary>
/// Check if a drive is recognized as a supported device.
/// </summary>
/// <param name="drive">The drive where the device is located.</param>
/// <returns>Device with device info if it has been recognized, null otherwise.</returns>
private Device RecognizeDevice(DriveInfo drive)
{
foreach (Device d in deviceConfig.Devices)
{
if(Directory.Exists(drive.Name + d.RecognizePattern) ||
File.Exists(drive.Name + d.RecognizePattern) ||
drive.VolumeLabel == d.RecognizePattern)
{
return d;
}
}
return null;
}
/// <summary>
/// Remove a device from the list of disconnected devices.
/// </summary>
/// <param name="drive">The name of the drive where the device was connected.</param>
private void RemoveDevice(string drive)
{
connectedDevices.Remove(drive);
}
/// <summary>
/// Method for sending out device disconnected events.
/// </summary>
/// <param name="driveName">The name of the drive where the device was located.</param>
/// <param name="device">Device object describing the device that was disconnected.</param>
protected void OnDeviceDisconnected(string driveName, Device device)
{
if (DeviceDisconnected != null)
{
CDMEventArgs args = new CDMEventArgs(null, device);
DeviceDisconnected(this, driveName, args);
}
}
/// <summary>
/// Method for dispatching device connected events.
/// </summary>
/// <param name="drive">The drive where the device is connected.</param>
/// <param name="device">Info on the connected device.</param>
protected void OnDeviceConnected(DriveInfo drive, Device device)
{
if (DeviceConnected != null)
{
CDMEventArgs args = new CDMEventArgs(drive, device);
DeviceConnected(this, args);
}
}
/// <summary>
/// <see cref="Notpod.IConnectedDevicesManager#GetConnectedDevices()"/>
/// </summary>
/// <returns>A Hashtable with Device objects representing the connected devices.</returns>
public ICollection GetConnectedDevices()
{
return connectedDevices.Values;
}
/// <summary>
/// <see cref="Notpod.IConnectedDevicesManager#GetConnectedDevicesWithDrives()"/>
/// </summary>
public Hashtable GetConnectedDevicesWithDrives()
{
return connectedDevices;
}
#endregion
}
}