Changeset View
Changeset View
Standalone View
Standalone View
core/filetransferjob.cpp
Context not available. | |||||
31 | 31 | | |||
---|---|---|---|---|---|
32 | FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, qint64 size, const QUrl& destination) | 32 | FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, qint64 size, const QUrl& destination) | ||
33 | : KJob() | 33 | : KJob() | ||
34 | , mOrigin(origin) | 34 | , m_origin(origin) | ||
35 | , mReply(Q_NULLPTR) | 35 | , m_reply(Q_NULLPTR) | ||
36 | , mFrom(QStringLiteral("KDE Connect")) | 36 | , m_from(QStringLiteral("KDE Connect")) | ||
37 | , mDestination(destination) | 37 | , m_destination(destination) | ||
38 | , mSpeedBytes(0) | 38 | , m_speedBytes(0) | ||
39 | , mWritten(0) | 39 | , m_written(0) | ||
40 | { | 40 | { | ||
41 | Q_ASSERT(mOrigin); | 41 | Q_ASSERT(m_origin); | ||
42 | Q_ASSERT(mOrigin->isReadable()); | 42 | Q_ASSERT(m_origin->isReadable()); | ||
43 | if (mDestination.scheme().isEmpty()) { | 43 | if (m_destination.scheme().isEmpty()) { | ||
44 | qCWarning(KDECONNECT_CORE) << "Destination QUrl" << mDestination << "lacks a scheme. Setting its scheme to 'file'."; | 44 | qCWarning(KDECONNECT_CORE) << "Destination QUrl" << m_destination << "lacks a scheme. Setting its scheme to 'file'."; | ||
45 | mDestination.setScheme(QStringLiteral("file")); | 45 | m_destination.setScheme(QStringLiteral("file")); | ||
46 | } | 46 | } | ||
47 | 47 | | |||
48 | if (size >= 0) { | 48 | if (size >= 0) { | ||
Context not available. | |||||
62 | void FileTransferJob::doStart() | 62 | void FileTransferJob::doStart() | ||
63 | { | 63 | { | ||
64 | description(this, i18n("Receiving file over KDE Connect"), | 64 | description(this, i18n("Receiving file over KDE Connect"), | ||
65 | { i18nc("File transfer origin", "From"), mFrom } | 65 | { i18nc("File transfer origin", "From"), m_from } | ||
66 | ); | 66 | ); | ||
67 | 67 | | |||
68 | if (mDestination.isLocalFile() && QFile::exists(mDestination.toLocalFile())) { | 68 | if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) { | ||
69 | setError(2); | 69 | setError(2); | ||
70 | setErrorText(i18n("Filename already present")); | 70 | setErrorText(i18n("Filename already present")); | ||
71 | emitResult(); | 71 | emitResult(); | ||
72 | return; | 72 | return; | ||
73 | } | 73 | } | ||
74 | 74 | | |||
75 | if (mOrigin->bytesAvailable()) | 75 | if (m_origin->bytesAvailable()) | ||
76 | startTransfer(); | 76 | startTransfer(); | ||
77 | connect(mOrigin.data(), &QIODevice::readyRead, this, &FileTransferJob::startTransfer); | 77 | connect(m_origin.data(), &QIODevice::readyRead, this, &FileTransferJob::startTransfer); | ||
78 | } | 78 | } | ||
79 | 79 | | |||
80 | void FileTransferJob::startTransfer() | 80 | void FileTransferJob::startTransfer() | ||
81 | { | 81 | { | ||
82 | setProcessedAmount(Bytes, 0); | 82 | setProcessedAmount(Bytes, 0); | ||
83 | mTimer.start(); | 83 | m_timer.start(); | ||
84 | description(this, i18n("Receiving file over KDE Connect"), | 84 | description(this, i18n("Receiving file over KDE Connect"), | ||
85 | { i18nc("File transfer origin", "From"), mFrom }, | 85 | { i18nc("File transfer origin", "From"), m_from }, | ||
86 | { i18nc("File transfer destination", "To"), mDestination.toLocalFile() }); | 86 | { i18nc("File transfer destination", "To"), m_destination.toLocalFile() }); | ||
87 | 87 | | |||
88 | QNetworkRequest req(mDestination); | 88 | QNetworkRequest req(m_destination); | ||
89 | req.setHeader(QNetworkRequest::ContentLengthHeader, totalAmount(Bytes)); | 89 | req.setHeader(QNetworkRequest::ContentLengthHeader, totalAmount(Bytes)); | ||
90 | mReply = Daemon::instance()->networkAccessManager()->put(req, mOrigin.data()); | 90 | m_reply = Daemon::instance()->networkAccessManager()->put(req, m_origin.data()); | ||
91 | 91 | | |||
92 | connect(mReply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 /*bytesTotal*/) { | 92 | connect(m_reply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 /*bytesTotal*/) { | ||
93 | setProcessedAmount(Bytes, bytesSent); | 93 | setProcessedAmount(Bytes, bytesSent); | ||
94 | 94 | | |||
95 | const auto elapsed = mTimer.elapsed(); | 95 | const auto elapsed = m_timer.elapsed(); | ||
96 | if (elapsed > 0) { | 96 | if (elapsed > 0) { | ||
97 | emitSpeed(bytesSent / elapsed); | 97 | emitSpeed(bytesSent / elapsed); | ||
98 | } | 98 | } | ||
99 | }); | 99 | }); | ||
100 | connect(mReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), | 100 | connect(m_reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), | ||
101 | this, &FileTransferJob::transferFailed); | 101 | this, &FileTransferJob::transferFailed); | ||
102 | connect(mReply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished); | 102 | connect(m_reply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished); | ||
103 | } | 103 | } | ||
104 | 104 | | |||
105 | void FileTransferJob::transferFailed(QNetworkReply::NetworkError error) | 105 | void FileTransferJob::transferFailed(QNetworkReply::NetworkError error) | ||
106 | { | 106 | { | ||
107 | qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << mReply->errorString(); | 107 | qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << m_reply->errorString(); | ||
108 | setError(error); | 108 | setError(error); | ||
109 | setErrorText(i18n("Received incomplete file: %1", mReply->errorString())); | 109 | setErrorText(i18n("Received incomplete file: %1", m_reply->errorString())); | ||
110 | emitResult(); | 110 | emitResult(); | ||
111 | 111 | | |||
112 | mReply->close(); | 112 | m_reply->close(); | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | void FileTransferJob::transferFinished() | 115 | void FileTransferJob::transferFinished() | ||
116 | { | 116 | { | ||
117 | //TODO: MD5-check the file | 117 | //TODO: MD5-check the file | ||
118 | qCDebug(KDECONNECT_CORE) << "Finished transfer" << mDestination; | 118 | qCDebug(KDECONNECT_CORE) << "Finished transfer" << m_destination; | ||
119 | 119 | | |||
120 | emitResult(); | 120 | emitResult(); | ||
121 | } | 121 | } | ||
122 | 122 | | |||
123 | bool FileTransferJob::doKill() | 123 | bool FileTransferJob::doKill() | ||
124 | { | 124 | { | ||
125 | if (mReply) { | 125 | if (m_reply) { | ||
126 | mReply->close(); | 126 | m_reply->close(); | ||
127 | } | 127 | } | ||
128 | if (mOrigin) { | 128 | if (m_origin) { | ||
129 | mOrigin->close(); | 129 | m_origin->close(); | ||
130 | } | 130 | } | ||
131 | return true; | 131 | return true; | ||
132 | } | 132 | } | ||
Context not available. |