filetransferjob.cpp 6.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 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 "filetransferjob.h"

23
#include <qalgorithms.h>
24
#include <QFileInfo>
25

26
#include <KIO/RenameDialog>
27
28
#include <KLocalizedString>

29
#include "kdebugnamespace.h"
30

31
FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination): KJob()
32
{
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
33
    Q_ASSERT(destination.isLocalFile());
34
    //TODO: Make a precondition before calling this function that destination file exists
35
    mOrigin = origin;
36
37
    mSize = size;
    mWritten = 0;
38
    m_speedBytes = 0;
39
40
    mDestination = destination;
    mDestinationJob = 0;
41
    mDeviceName = i18nc("Device name that will appear on the jobs", "KDE-Connect");
42
43

    setCapabilities(Killable);
44
    kDebug(kdeconnect_kded()) << "FileTransferJob Downloading payload to" << destination;
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
45
46
47
48
}

void FileTransferJob::openFinished(KJob* job)
{
49
    kDebug(kdeconnect_kded()) << job->errorString();
50
51
52
53
}

void FileTransferJob::start()
{
54
    QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
55
    //kDebug(kdeconnect_kded()) << "FileTransferJob start";
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
}

void FileTransferJob::doStart()
{
    description(this, i18n("Receiving file over KDE-Connect"),
        QPair<QString, QString>(i18nc("File transfer origin", "From"),
        QString(mDeviceName))
    );
    KUrl destCheck = mDestination;
    if (QFile::exists(destCheck.path())) {
        QFileInfo destInfo(destCheck.path());
        KIO::RenameDialog *dialog = new KIO::RenameDialog(0,
            i18n("Incoming file exists"),
            KUrl(mDeviceName + ":/" + destCheck.fileName()),
            destCheck,
            KIO::M_OVERWRITE,
            mSize,
            destInfo.size(),
            -1,
            destInfo.created().toTime_t(),
            -1,
            destInfo.lastModified().toTime_t()
        );
        connect(this, SIGNAL(finished(KJob*)), dialog, SLOT(deleteLater()));
        connect(dialog, SIGNAL(finished(int)), SLOT(renameDone(int)));
        dialog->show();
        return;
    }

    startTransfer();
}

void FileTransferJob::renameDone(int result)
{
    KIO::RenameDialog *renameDialog = qobject_cast<KIO::RenameDialog*>(sender());
    switch (result) {
    case KIO::R_CANCEL:
        //The user cancelled, killing the job
        emitResult();
    case KIO::R_RENAME:
        mDestination = renameDialog->newDestUrl();
        break;
    case KIO::R_OVERWRITE:
    {
        // Delete the old file if exists
        QFile oldFile(mDestination.path());
        if (oldFile.exists()) {
            oldFile.remove();
        }
        break;
    }
    default:
        kWarning() << "Unknown Error";
        emitResult();
    }

    renameDialog->deleteLater();
    startTransfer();
}

void FileTransferJob::startTransfer()
{
118
119
120
    setTotalAmount(Bytes, mSize);
    setProcessedAmount(Bytes, 0);
    m_time = QTime::currentTime();
121
122
123
    description(this, i18n("Receiving file over KDE-Connect"),
                        QPair<QString, QString>(i18nc("File transfer origin", "From"),
                        QString(mDeviceName)),
124
125
126
127
128
129
130
                        QPair<QString, QString>(i18nc("File transfer destination", "To"), mDestination.path()));

    QFile(mDestination.path()).open(QIODevice::WriteOnly | QIODevice::Truncate); //HACK: KIO is so dumb it can't create the file if it doesn't exist
    mDestinationJob = KIO::open(mDestination, QIODevice::WriteOnly);
    connect(mDestinationJob, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
    connect(mDestinationJob, SIGNAL(result(KJob*)), this, SLOT(openFinished(KJob*)));

131
    //Open destination file
132
    mDestinationJob->start();
133
134
135
136
137
138
}

void FileTransferJob::open(KIO::Job* job)
{
    Q_UNUSED(job);

139
    //kDebug(kdeconnect_kded()) << "FileTransferJob open";
140

141
    if (!mOrigin) {
142
        kDebug(kdeconnect_kded()) << "FileTransferJob: Origin is null";
143
144
145
146
147
        return;
    }

    //Open source file
    mOrigin->open(QIODevice::ReadOnly);
148
149
    Q_ASSERT(mOrigin->isOpen());

150
151
    connect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
    connect(mOrigin.data(), SIGNAL(disconnected()),this, SLOT(sourceFinished()));
152
153
154
155
156
157
158
159
    if (mOrigin->bytesAvailable() > 0) readyRead();

}

void FileTransferJob::readyRead()
{
    int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
    QByteArray data = mOrigin->read(bytes);
160
    mDestinationJob->write(data);
161
162
    mWritten += data.size();
    setProcessedAmount(Bytes, mWritten);
163

164
    //kDebug(kdeconnect_kded()) << "readyRead" << mSize << mWritten << bytes;
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
165

166
    if (mSize > -1) {
167
168
169
170
171
172
173
174
175
176
177
        //If a least 1 second has passed since last update
        int secondsSinceLastTime = m_time.secsTo(QTime::currentTime());
        if (secondsSinceLastTime > 0 && m_speedBytes > 0) {
            float speed = (mWritten - m_speedBytes) / secondsSinceLastTime;
            emitSpeed(speed);

            m_time = QTime::currentTime();
            m_speedBytes = mWritten;
        } else if(m_speedBytes == 0) {
            m_speedBytes = mWritten;
        }
178
    }
179

180
181
    if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached
        mOrigin->close();
182
        //sourceFinished();
183
    } else if (mOrigin->bytesAvailable() > 0) {
184
        QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
185
    }
186
187
188
189
}

void FileTransferJob::sourceFinished()
{
190
    //Make sure we do not enter this function again
191
    disconnect(mOrigin.data(), SIGNAL(aboutToClose()),this, SLOT(sourceFinished()));
192
193
194

    //TODO: MD5 check the file
    if (mSize > -1 && mWritten != mSize) {
195
        kDebug(kdeconnect_kded()) << "Received incomplete file (" << mWritten << " of " << mSize << " bytes)";
196
197
        setError(1);
        setErrorText(i18n("Received incomplete file"));
198
    } else {
199
        kDebug(kdeconnect_kded()) << "Finished transfer" << mDestinationJob->url();
200
    }
201
202
    mDestinationJob->close();
    mDestinationJob->deleteLater();
203
204
205
    emitResult();
}

206
207
208
209
210
211
212
213
214
215
bool FileTransferJob::doKill()
{
    if (mDestinationJob) {
        mDestinationJob->close();
    }
    if (mOrigin) {
        mOrigin->close();
    }
    return true;
}