[go: up one dir, main page]

Menu

[r11]: / Pomme / iDevice.cs  Maximize  Restore  History

Download this file

218 lines (178 with data), 6.6 kB

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
namespace Pomme
{
public enum iDeviceType
{
None = 0,
iPod = 1,
iPhone = 2
}
public class iDevice
{
#region Constants
protected const int buffer_size = 65536;
protected const string iphone_driveletter = "#";
protected const string ipoddbpath = "iPod_Control\\iTunes\\";
protected const string dbname = "iTunesDB";
protected const string EX_IO_ERR = "There was an error accessing your iPod!";
#endregion // Constants
#region Properties
private iPhone phone = null;
private iDeviceType deviceType = iDeviceType.None;
private string driveLetter = null;
internal iPhone Phone
{
get { return phone; }
}
public iDeviceType DeviceType
{
get { return deviceType; }
}
public string DriveLetter
{
get { return driveLetter; }
}
#endregion // Properties
#region Constructor
public iDevice()
{
phone = new iPhone(); // We need this even if we load an ipod, in case we load a phone later
// Check if there is an iPod,
if ((driveLetter = findIpodDrive()) != null)
{
deviceType = iDeviceType.iPod;
}
else
{ // if not Register to find out if there is an iPhone
phone.Connect += new ConnectEventHandler(phone_Connect);
phone.Disconnect += new ConnectEventHandler(phone_Disconnect);
}
}
#endregion // Constructor
#region Events
void phone_Connect(object sender, ConnectEventArgs args)
{
if (phone.IsConnected && deviceType == iDeviceType.None)
{
deviceType = iDeviceType.iPhone;
driveLetter = iphone_driveletter;
}
}
void phone_Disconnect(object sender, ConnectEventArgs args)
{
if (!phone.IsConnected && deviceType == iDeviceType.iPhone)
{
deviceType = iDeviceType.None;
driveLetter = null;
// Todo : Use a callback to warn the host program?
}
}
#endregion // Events
#region Private Methods
private bool isIpod(string drive)
{
// Check if the specified drive is an ipod
// is the db file there?
try
{
string theFileName = drive + ":\\" + ipoddbpath + dbname;
FileInfo theFile = new FileInfo(theFileName);
return theFile.Exists;
}
catch
{
throw new IOException("Error accessing filesystem.");
}
}
private string findIpodDrive()
{
// Return drive letter of first ipod or null
for (char i = 'a'; i <= 'z'; i++)
{
if (isIpod(i.ToString()))
{
return i.ToString();
}
}
return null;
}
#endregion // Private Methods
#region Internal Methods
internal void CopyFromPhone(string devPath, string winPath)
{
// TODO: Deal with paths that refer to iPhone files
if (deviceType != iDeviceType.iPhone)
{
throw new IOException("Cannot copy: device is not a phone.");
}
byte[] buffer = new byte[buffer_size];
int len = 0;
try
{
FileStream dest = new FileStream(winPath, FileMode.Create, FileAccess.Write);
iPhoneFile source = iPhoneFile.OpenRead(phone, devPath);
do
{
len = source.Read(buffer, 0, buffer_size);
dest.Write(buffer, 0, len);
} while (len > 0);
source.Close();
dest.Close();
}
catch
{
throw new IOException("Error copying iPhone file.");
}
}
#endregion // Internal Methods
#region Public Methods
public bool CheckForDevice()
{
if (deviceType == iDeviceType.None)
{
if ((driveLetter = findIpodDrive()) != null)
{
deviceType = iDeviceType.iPod;
}
else if (phone != null && phone.IsConnected)
{
deviceType = iDeviceType.iPhone;
driveLetter = iphone_driveletter;
}
}
return (deviceType != iDeviceType.None);
}
#endregion // Public Methods
#region Static Methods
public static bool LocateiTunesMobileDeviceDLL()
{
// let's make sure we have the "iTunesMobileDevice.dll"
// somewhere where we can find it
const string subkey = "iTunesMobileDeviceDLL";
string keyName = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Apple Inc.\\Apple Mobile Device Support\\Shared";
string fileLoc, path;
if ((fileLoc = (string)Registry.GetValue(keyName, subkey, null)) == null)
{
keyName = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Apple Inc.\\Apple Mobile Device Support\\Shared";
fileLoc = (string)Registry.GetValue(keyName, subkey, null);
}
if (fileLoc != null)
{
path = fileLoc.Substring(0, fileLoc.LastIndexOf('\\'));
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + path);
}
if (!File.Exists("iTunesMobileDevice.dll") && !File.Exists(fileLoc))
{
//MessageBox.Show("iTunesMobileDevice.dll not found!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
#endregion // Static Methods
}
}