Fixed file transfer

Apparently KIO is so dumb it can't create files
Also we have lots of commented qDebugs now
This commit is contained in:
Albert Vaca 2013-09-27 00:01:59 +02:00
parent cfe13a739c
commit f53f70d7c4
6 changed files with 40 additions and 21 deletions

View file

@ -81,13 +81,16 @@ bool LanDeviceLink::sendPackage(NetworkPackage& np)
void LanDeviceLink::dataReceived() void LanDeviceLink::dataReceived()
{ {
//qDebug() << "LanDeviceLink dataReceived";
QByteArray data = mSocket->readAll(); QByteArray data = mSocket->readAll();
//qDebug() << "LanDeviceLink dataReceived" << data;
QList<QByteArray> packages = data.split('\n'); QList<QByteArray> packages = data.split('\n');
Q_FOREACH(const QByteArray& package, packages) { Q_FOREACH(const QByteArray& package, packages) {
if (package.length() < 3) continue; if (package.length() < 3) {
continue;
}
NetworkPackage unserialized(QString::null); NetworkPackage unserialized(QString::null);
NetworkPackage::unserialize(package, &unserialized); NetworkPackage::unserialize(package, &unserialized);
@ -109,15 +112,22 @@ void LanDeviceLink::dataReceived()
} else { } else {
if (unserialized.hasPayloadTransferInfo()) { if (unserialized.hasPayloadTransferInfo()) {
//Lets ignore unencrypted payloads qWarning() << "Ignoring unencrypted payload";
continue;
} }
Q_EMIT receivedPackage(unserialized); Q_EMIT receivedPackage(unserialized);
} }
} }
//qDebug() << "MOAR BYTES" << mSocket->bytesAvailable() << packages.length();
if (mSocket->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
}
} }
void LanDeviceLink::readyRead() void LanDeviceLink::readyRead()

View file

@ -45,6 +45,7 @@ void LoopbackLinkProvider::onNetworkChange(QNetworkSession::State state)
if (loopbackDeviceLink) { if (loopbackDeviceLink) {
delete loopbackDeviceLink; delete loopbackDeviceLink;
} }
loopbackDeviceLink = newLoopbackDeviceLink; loopbackDeviceLink = newLoopbackDeviceLink;
} }

View file

@ -27,17 +27,27 @@
FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination): KJob() FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination): KJob()
{ {
Q_ASSERT(destination.isLocalFile());
QFile(destination.path()).open(QIODevice::WriteOnly | QIODevice::Truncate); //HACK: KIO is so dumb it can't create the file if it doesn't exist
mDestination = KIO::open(destination, QIODevice::WriteOnly); mDestination = KIO::open(destination, QIODevice::WriteOnly);
connect(mDestination, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
connect(mDestination, SIGNAL(result(KJob*)), this, SLOT(openFinished(KJob*)));
mOrigin = origin; mOrigin = origin;
mSize = size; mSize = size;
mWritten = 0; mWritten = 0;
qDebug() << "Downloading payload to" << destination; qDebug() << "FileTransferJob Downloading payload to" << destination;
}
void FileTransferJob::openFinished(KJob* job)
{
qDebug() << job->errorString();
} }
void FileTransferJob::start() void FileTransferJob::start()
{ {
//qDebug() << "FileTransferJob start";
//Open destination file //Open destination file
connect(mDestination, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
mDestination->start(); mDestination->start();
} }
@ -45,7 +55,7 @@ void FileTransferJob::open(KIO::Job* job)
{ {
Q_UNUSED(job); Q_UNUSED(job);
qDebug() << "open"; //qDebug() << "FileTransferJob open";
if (!mOrigin) { if (!mOrigin) {
qDebug() << "FileTransferJob: Origin is null"; qDebug() << "FileTransferJob: Origin is null";
@ -64,21 +74,17 @@ void FileTransferJob::open(KIO::Job* job)
void FileTransferJob::readyRead() void FileTransferJob::readyRead()
{ {
qDebug() << "readyRead";
//Copy a chunk of data
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable()); int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
QByteArray data = mOrigin->read(bytes); QByteArray data = mOrigin->read(bytes);
mDestination->write(data); mDestination->write(data);
mWritten += bytes; mWritten += bytes;
//qDebug() << "readyRead" << mSize << mWritten << bytes;
if (mSize > -1) { if (mSize > -1) {
setPercent((mWritten*100)/mSize); setPercent((mWritten*100)/mSize);
} }
qDebug() << mSize << mWritten << bytes;
if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached
qDebug() << "No more data to read"; qDebug() << "No more data to read";
disconnect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead())); disconnect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
@ -90,8 +96,6 @@ void FileTransferJob::readyRead()
void FileTransferJob::sourceFinished() void FileTransferJob::sourceFinished()
{ {
qDebug() << "sourceFinished";
//Make sure we do not enter this function again //Make sure we do not enter this function again
disconnect(mOrigin.data(), SIGNAL(aboutToClose()),this, SLOT(sourceFinished())); disconnect(mOrigin.data(), SIGNAL(aboutToClose()),this, SLOT(sourceFinished()));
@ -104,7 +108,8 @@ void FileTransferJob::sourceFinished()
} else { } else {
qDebug() << "Finished transfer" << mDestination->url(); qDebug() << "Finished transfer" << mDestination->url();
} }
mDestination->close();
mDestination->deleteLater();
emitResult(); emitResult();
} }

View file

@ -44,6 +44,7 @@ public Q_SLOTS:
void readyRead(); void readyRead();
void open(KIO::Job*); void open(KIO::Job*);
void sourceFinished(); void sourceFinished();
void openFinished(KJob*);
private: private:
QSharedPointer<QIODevice> mOrigin; QSharedPointer<QIODevice> mOrigin;

View file

@ -23,7 +23,6 @@
#include "../../filetransferjob.h" #include "../../filetransferjob.h"
#include <QDebug> #include <QDebug>
#include <QDir>
#include <QDBusConnection> #include <QDBusConnection>
#include <KNotification> #include <KNotification>
@ -34,8 +33,9 @@ NotificationsDbusInterface::NotificationsDbusInterface(Device* device, QObject *
: QDBusAbstractAdaptor(parent) : QDBusAbstractAdaptor(parent)
, mDevice(device) , mDevice(device)
, mLastId(0) , mLastId(0)
, imagesDir(QDir::temp().absoluteFilePath("kdeconnect"))
{ {
imagesDir.mkpath(imagesDir.absolutePath());
} }
NotificationsDbusInterface::~NotificationsDbusInterface() NotificationsDbusInterface::~NotificationsDbusInterface()
@ -56,8 +56,8 @@ void NotificationsDbusInterface::processPackage(const NetworkPackage& np)
QString destination; QString destination;
if (np.hasPayload()) { if (np.hasPayload()) {
//TODO: Store the image with extension? QString filename = KMD5(np.get<QString>("appName").toLatin1()).hexDigest(); //TODO: Store with extension?
destination = QDir::temp().absoluteFilePath("kdeconnect/"+KMD5(np.get<QString>("appName").toLatin1()).hexDigest()); destination = imagesDir.absoluteFilePath(filename);
FileTransferJob* job = np.createPayloadTransferJob(destination); FileTransferJob* job = np.createPayloadTransferJob(destination);
job->start(); job->start();
} }

View file

@ -25,6 +25,7 @@
#include <QHash> #include <QHash>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QDir>
#include "../../device.h" #include "../../device.h"
#include "notification.h" #include "notification.h"
@ -59,6 +60,7 @@ private /*attributes*/:
QHash<QString, Notification*> mNotifications; QHash<QString, Notification*> mNotifications;
QHash<QString, QString> mInternalIdToPublicId; QHash<QString, QString> mInternalIdToPublicId;
int mLastId; int mLastId;
QDir imagesDir;
}; };