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(iMobileDeviceConnection* connection, const QString& path, QObject* parent)
|
|
|
|
: QIODevice(parent),
|
2010-08-09 21:24:17 +02:00
|
|
|
connection_(connection),
|
|
|
|
handle_(0),
|
2010-08-01 16:13:27 +02:00
|
|
|
path_(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AfcFile::~AfcFile() {
|
2010-08-08 21:11:34 +02:00
|
|
|
close();
|
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-09 21:24:17 +02:00
|
|
|
connection_->afc(), 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-09 21:24:17 +02:00
|
|
|
if (handle_) {
|
2010-08-14 13:16:11 +02:00
|
|
|
afc_file_close(connection_->afc(), handle_);
|
2010-08-09 21:24:17 +02:00
|
|
|
}
|
2010-08-01 14:19:21 +02:00
|
|
|
QIODevice::close();
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:13:27 +02:00
|
|
|
bool AfcFile::seek(qint64 pos) {
|
2010-08-09 21:24:17 +02:00
|
|
|
afc_error_t err = afc_file_seek(connection_->afc(), handle_, pos, SEEK_SET);
|
2010-08-01 14:19:21 +02:00
|
|
|
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;
|
2010-08-09 21:24:17 +02:00
|
|
|
afc_error_t err = afc_file_read(connection_->afc(), handle_, data, max_size, &bytes_read);
|
2010-08-01 14:19:21 +02:00
|
|
|
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;
|
2010-08-09 21:24:17 +02:00
|
|
|
afc_error_t err = afc_file_write(connection_->afc(), handle_, data, max_size, &bytes_written);
|
2010-08-01 14:19:21 +02:00
|
|
|
if (err != AFC_E_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_written;
|
|
|
|
}
|
2010-08-09 21:24:17 +02:00
|
|
|
|
|
|
|
qint64 AfcFile::size() const {
|
|
|
|
return connection_->GetFileInfo(path_, "st_size").toLongLong();
|
|
|
|
}
|