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
}
}