Clementine-audio-player-Mac.../src/devices/ilister.cpp

192 lines
5.0 KiB
C++
Raw Normal View History

#include "config.h"
#include "ilister.h"
#include "imobiledeviceconnection.h"
#include <QStringList>
#include <QtDebug>
iLister::iLister() {
}
iLister::~iLister() {
}
void iLister::Init() {
idevice_event_subscribe(&EventCallback, reinterpret_cast<void*>(this));
}
void iLister::EventCallback(const idevice_event_t* event, void* context) {
iLister* me = reinterpret_cast<iLister*>(context);
#ifdef IMOBILEDEVICE_USES_UDIDS
const char* uuid = event->udid;
#else
const char* uuid = event->uuid;
#endif
switch (event->event) {
case IDEVICE_DEVICE_ADD:
me->DeviceAddedCallback(uuid);
break;
case IDEVICE_DEVICE_REMOVE:
me->DeviceRemovedCallback(uuid);
break;
}
}
void iLister::DeviceAddedCallback(const char* uuid) {
DeviceInfo info = ReadDeviceInfo(uuid);
2010-08-01 13:24:25 +02:00
QString id = UniqueId(uuid);
QString name = MakeFriendlyName(id);
if (info.product_type == "iPhone 3,1" || info.product_type.startsWith("iPad")) {
// iPhone 4 and iPad unsupported by libgpod as of 0.7.94.
return;
}
2010-08-01 13:24:25 +02:00
{
QMutexLocker l(&mutex_);
devices_[id] = info;
2010-08-01 13:24:25 +02:00
}
emit DeviceAdded(id);
}
void iLister::DeviceRemovedCallback(const char* uuid) {
2010-08-01 13:24:25 +02:00
QString id = UniqueId(uuid);
{
QMutexLocker l(&mutex_);
if (!devices_.contains(id))
return;
devices_.remove(id);
}
emit DeviceRemoved(id);
}
2010-08-01 13:24:25 +02:00
QString iLister::UniqueId(const char *uuid) {
return "ithing/" + QString::fromUtf8(uuid);
}
QStringList iLister::DeviceUniqueIDs() {
return devices_.keys();
}
2010-08-16 01:26:04 +02:00
QVariantList iLister::DeviceIcons(const QString& id) {
return QVariantList() << "ipodtouchicon";
}
QString iLister::DeviceManufacturer(const QString& id) {
return "Apple";
}
QString iLister::DeviceModel(const QString& id) {
2010-08-01 13:24:25 +02:00
return LockAndGetDeviceInfo(id, &DeviceInfo::product_type);
}
quint64 iLister::DeviceCapacity(const QString& id) {
2010-08-01 13:24:25 +02:00
return LockAndGetDeviceInfo(id, &DeviceInfo::total_bytes);
}
quint64 iLister::DeviceFreeSpace(const QString& id) {
2010-08-01 13:24:25 +02:00
return LockAndGetDeviceInfo(id, &DeviceInfo::free_bytes);
}
QVariantMap iLister::DeviceHardwareInfo(const QString& id) {
QVariantMap ret;
2010-08-09 20:42:17 +02:00
ret[tr("Color")] = LockAndGetDeviceInfo(id, &DeviceInfo::colour);
ret["IMEI"] = LockAndGetDeviceInfo(id, &DeviceInfo::imei);
ret[tr("Password Protected")] = LockAndGetDeviceInfo(id, &DeviceInfo::password_protected);
ret[tr("Timezone")] = LockAndGetDeviceInfo(id, &DeviceInfo::timezone);
ret[tr("WiFi MAC Address")] = LockAndGetDeviceInfo(id, &DeviceInfo::wifi_mac);
ret[tr("Bluetooth MAC Address")] = LockAndGetDeviceInfo(id, &DeviceInfo::bt_mac);
return ret;
}
QString iLister::MakeFriendlyName(const QString& id) {
QString name = LockAndGetDeviceInfo(id, &DeviceInfo::name);
if (!name.isEmpty()) {
return name;
}
2010-08-01 13:24:25 +02:00
QString model_id = LockAndGetDeviceInfo(id, &DeviceInfo::product_type);
if (model_id.startsWith("iPhone")) {
QString version = model_id.right(3);
QChar major = version[0];
QChar minor = version[2];
if (major == '1' && minor == '1') {
return "iPhone";
}
if (major == '1' && minor == '2') {
return "iPhone 3G";
}
if (major == '2' && minor == '1') {
return "iPhone 3GS";
}
if (major == '3' && minor == '1') {
return "iPhone 4";
}
} else if (model_id.startsWith("iPod")) {
return "iPod Touch";
} else if (model_id.startsWith("iPad")) {
return "iPad";
}
return model_id;
}
2010-08-01 13:55:01 +02:00
QList<QUrl> iLister::MakeDeviceUrls(const QString& id) {
QList<QUrl> ret;
QString uuid = LockAndGetDeviceInfo(id, &DeviceInfo::uuid);
if (uuid.isEmpty())
return ret;
ret << QUrl("afc://" + uuid + "/");
return ret;
}
void iLister::UnmountDevice(const QString& id) { }
2010-08-01 13:24:25 +02:00
iLister::DeviceInfo iLister::ReadDeviceInfo(const char* uuid) {
2010-08-01 13:24:25 +02:00
DeviceInfo ret;
iMobileDeviceConnection conn(uuid);
2010-08-01 13:24:25 +02:00
ret.uuid = uuid;
ret.product_type = conn.GetProperty("ProductType").toString();
ret.free_bytes = conn.GetProperty("AmountDataAvailable", "com.apple.disk_usage").toULongLong();
ret.total_bytes = conn.GetProperty("TotalDataCapacity", "com.apple.disk_usage").toULongLong();
ret.name = conn.GetProperty("DeviceName").toString();
ret.colour = conn.GetProperty("DeviceColor").toString();
ret.imei = conn.GetProperty("InternationalMobileEquipmentIdentity").toString();
ret.hardware = conn.GetProperty("HardwareModel").toString();
ret.password_protected = conn.GetProperty("PasswordProtected").toBool();
ret.os_version = conn.GetProperty("ProductVersion").toString();
ret.timezone = conn.GetProperty("TimeZone").toString();
ret.wifi_mac = conn.GetProperty("WiFiAddress").toString();
ret.bt_mac = conn.GetProperty("BluetoothAddress").toString();
2010-08-01 13:24:25 +02:00
return ret;
}
void iLister::UpdateDeviceFreeSpace(const QString& id) {
{
QMutexLocker l(&mutex_);
if (!devices_.contains(id))
return;
DeviceInfo& info = devices_[id];
iMobileDeviceConnection conn(info.uuid);
info.free_bytes = conn.GetProperty("AmountDataAvailable", "com.apple.disk_usage").toULongLong();
}
emit DeviceChanged(id);
}