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

View file

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

View file

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

View file

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

View file

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

View file

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