downloadjob.cpp 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "downloadjob.h"
22

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
23
#ifndef Q_OS_WIN
24 25 26 27
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
28
#endif
29

30
#include "kdeconnectconfig.h"
31
#include "lanlinkprovider.h"
32
#include "core/core_debug.h"
33

34
DownloadJob::DownloadJob(const QHostAddress& address, const QVariantMap& transferInfo)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
35
    : KJob()
36 37 38 39
    , m_address(address)
    , m_port(transferInfo[QStringLiteral("port")].toInt())
    , m_socket(new QSslSocket)
    , m_buffer(new QBuffer)
40
{
41
    LanLinkProvider::configureSslSocket(m_socket.data(), transferInfo.value(QStringLiteral("deviceId")).toString(), true);
42

43
    connect(m_socket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketFailed(QAbstractSocket::SocketError)));
44
//     connect(mSocket.data(), &QAbstractSocket::stateChanged, [](QAbstractSocket::SocketState state){ qDebug() << "statechange" << state; });
45 46
}

47 48
DownloadJob::~DownloadJob()
{
49

50 51 52 53
}

void DownloadJob::start()
{
54
    //TODO: Timeout?
55
    // Cannot use read only, might be due to ssl handshake, getting QIODevice::ReadOnly error and no connection
56
    m_socket->connectToHostEncrypted(m_address.toString(), m_port, QIODevice::ReadWrite);
57

58
    bool b = m_buffer->open(QBuffer::ReadWrite);
59
    Q_ASSERT(b);
60 61
}

62
void DownloadJob::socketFailed(QAbstractSocket::SocketError error)
63
{
64
    if (error != QAbstractSocket::RemoteHostClosedError) { //remote host closes when finishes
65
        qWarning(KDECONNECT_CORE) << "error..." << m_socket->errorString();
66
        setError(error + 1);
67
        setErrorText(m_socket->errorString());
68
    } else {
69 70 71
        auto ba = m_socket->readAll();
        m_buffer->write(ba);
        m_buffer->seek(0);
72
    }
73 74 75
    emitResult();
}

76
QSharedPointer<QIODevice> DownloadJob::getPayload()
77
{
78
    return m_buffer.staticCast<QIODevice>();
79
}