2010-08-01 14:19:21 +02:00
|
|
|
#include "afcfile.h"
|
2010-08-01 16:13:27 +02:00
|
|
|
#include "imobiledeviceconnection.h"
|
2010-08-01 14:19:21 +02:00
|
|
|
|
|
|
|
#include <libimobiledevice/afc.h>
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
AfcFile::AfcFile(afc_client_t client, const QString& path, QObject* parent)
|
|
|
|
: QIODevice(parent),
|
|
|
|
client_(client),
|
|
|
|
path_(path)
|
|
|
|
{
|
2010-08-01 14:19:21 +02:00
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
AfcFile::AfcFile(iMobileDeviceConnection* connection, const QString& path, QObject* parent)
|
|
|
|
: QIODevice(parent),
|
|
|
|
client_(connection->afc()),
|
|
|
|
path_(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AfcFile::~AfcFile() {
|
2010-08-01 14:19:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
bool AfcFile::open(QIODevice::OpenMode mode) {
|
2010-08-01 14:19:21 +02:00
|
|
|
afc_file_mode_t afc_mode;
|
|
|
|
switch (mode) {
|
|
|
|
case ReadOnly:
|
|
|
|
afc_mode = AFC_FOPEN_RDONLY;
|
|
|
|
break;
|
|
|
|
case WriteOnly:
|
|
|
|
afc_mode = AFC_FOPEN_WRONLY;
|
|
|
|
break;
|
|
|
|
case ReadWrite:
|
|
|
|
afc_mode = AFC_FOPEN_RW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
afc_mode = AFC_FOPEN_RW;
|
|
|
|
}
|
|
|
|
afc_error_t err = afc_file_open(
|
2010-08-01 16:13:27 +02:00
|
|
|
client_, path_.toUtf8().constData(), afc_mode, &handle_);
|
2010-08-01 14:19:21 +02:00
|
|
|
if (err != AFC_E_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QIODevice::open(mode);
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
void AfcFile::close() {
|
2010-08-01 14:19:21 +02:00
|
|
|
afc_file_close(client_, handle_);
|
|
|
|
QIODevice::close();
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
bool AfcFile::seek(qint64 pos) {
|
2010-08-01 14:19:21 +02:00
|
|
|
afc_error_t err = afc_file_seek(client_, handle_, pos, SEEK_SET);
|
|
|
|
if (err != AFC_E_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QIODevice::seek(pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
qint64 AfcFile::readData(char* data, qint64 max_size) {
|
2010-08-01 14:19:21 +02:00
|
|
|
uint32_t bytes_read = 0;
|
|
|
|
afc_error_t err = afc_file_read(client_, handle_, data, max_size, &bytes_read);
|
|
|
|
if (err != AFC_E_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
qint64 AfcFile::writeData(const char* data, qint64 max_size) {
|
2010-08-01 14:19:21 +02:00
|
|
|
uint32_t bytes_written = 0;
|
|
|
|
afc_error_t err = afc_file_write(client_, handle_, data, max_size, &bytes_written);
|
|
|
|
if (err != AFC_E_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_written;
|
|
|
|
}
|