Skip to content

Instantly share code, notes, and snippets.

@sixones
Created May 30, 2009 21:15
Show Gist options
  • Save sixones/120638 to your computer and use it in GitHub Desktop.
Save sixones/120638 to your computer and use it in GitHub Desktop.
#include "mtphandler.h"
bool MTPHandler::__LIBMTPInitialised = false;
MTPHandler::MTPHandler()
: _mtpDevices(0), _connectedDevice(0), _deviceConnected(false)
{
if (!MTPHandler::__LIBMTPInitialised)
{
LIBMTP_Init();
MTPHandler::__LIBMTPInitialised = true;
}
}
MTPHandler::~MTPHandler()
{
if (this->_mtpDevices != NULL)
{
LIBMTP_Release_Device_List(this->_mtpDevices);
}
}
Device* MTPHandler::connectDevice()
{
LIBMTP_raw_device_t *rawDevices;
LIBMTP_error_number_t e;
int cRawDevices;
bool detectedSuccessfully = false;
e = LIBMTP_Detect_Raw_Devices(&rawDevices, &cRawDevices);
switch (e)
{
case LIBMTP_ERROR_PTP_LAYER:
break;
case LIBMTP_ERROR_USB_LAYER:
break;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
break;
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
printf("No device found");
break;
case LIBMTP_ERROR_STORAGE_FULL:
break;
case LIBMTP_ERROR_CONNECTING:
break;
case LIBMTP_ERROR_CANCELLED:
break;
case LIBMTP_ERROR_NONE:
detectedSuccessfully = true;
break;
case LIBMTP_ERROR_GENERAL:
default:
printf("Unknown libmtp error, report to developers.");
break;
}
if (detectedSuccessfully)
{
LIBMTP_mtpdevice_t *mtpDevice = 0;
for (int i = 0; i < cRawDevices; i++)
{
mtpDevice = LIBMTP_Open_Raw_Device(&rawDevices[i]);
if (mtpDevice == NULL)
{
printf("Could not open raw device: %s", i + 1);
continue;
}
this->_deviceConnected = true;
break;
}
if (this->_deviceConnected)
{
Device *device = new Device();
char *friendlyName = LIBMTP_Get_Friendlyname(mtpDevice);
if (friendlyName != NULL)
{
device->friendlyName(friendlyName);
// free(friendlyName) STV-QUERY
}
char *modelName = LIBMTP_Get_Modelname(mtpDevice);
if (modelName)
{
device->modelName(modelName);
}
printf("Found device %s\n", device->modelName());
this->_connectedDevice = device;
}
}
return this->_connectedDevice;
}
QVarLengthArray<Device> MTPHandler::getDevices(QVarLengthArray<Device> &devices)
{
LIBMTP_mtpdevice_t *mtpDevices, *mtpDevice;
LIBMTP_error_number_t e;
bool detectedSuccessfully = false;
e = LIBMTP_Get_Connected_Devices(&mtpDevices);
this->_mtpDevices = mtpDevices;
switch (e)
{
case LIBMTP_ERROR_PTP_LAYER:
break;
case LIBMTP_ERROR_USB_LAYER:
break;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
break;
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
printf("No device found");
break;
case LIBMTP_ERROR_STORAGE_FULL:
break;
case LIBMTP_ERROR_CONNECTING:
break;
case LIBMTP_ERROR_CANCELLED:
break;
case LIBMTP_ERROR_NONE:
detectedSuccessfully = true;
break;
case LIBMTP_ERROR_GENERAL:
default:
printf("Unknown libmtp error, report to developers.");
break;
}
if (detectedSuccessfully)
{
for (mtpDevice = this->_mtpDevices; mtpDevice != NULL; mtpDevice = mtpDevice->next)
{
Device *device = new Device();
char *friendlyName = LIBMTP_Get_Friendlyname(mtpDevice);
if (friendlyName != NULL)
{
device->friendlyName(friendlyName);
// free(friendlyName) STV-QUERY
}
printf("Found device %s\n", device->friendlyName());
devices.append(*device);
}
}
return devices;
}
#ifndef ZENSES_HANDLERS_MTPHANDLER
#define ZENSES_HANDLERS_MTPHANDLER
#include <QVarLengthArray>
#include "device.h"
#include "libmtp.h"
#include "track.h"
class MTPHandler
{
public:
MTPHandler();
~MTPHandler();
Device* connectDevice();
QVarLengthArray<Device> getDevices(QVarLengthArray<Device> &devices);
//QVarLengthArray<Track> getTracks();
protected:
LIBMTP_mtpdevice_t *_mtpDevices;
Device *_connectedDevice;
bool _deviceConnected;
// Should only initialise libmtp once
static bool __LIBMTPInitialised;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment