diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-01-12 23:31:46 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-01-12 23:31:46 +0900 |
commit | f80065013935a0b32acafd4d96dda271441436fe (patch) | |
tree | 4d676173004aaace53d609be4683dbbb0d7aea7a | |
parent | 6d77401c5bf1d67d1660c0661c695ed241fdf58a (diff) | |
download | libquotient-f80065013935a0b32acafd4d96dda271441436fe.tar.gz libquotient-f80065013935a0b32acafd4d96dda271441436fe.zip |
Connection: files up/downloading support
-rw-r--r-- | connection.cpp | 42 | ||||
-rw-r--r-- | connection.h | 15 |
2 files changed, 57 insertions, 0 deletions
diff --git a/connection.cpp b/connection.cpp index e7646b82..935546b1 100644 --- a/connection.cpp +++ b/connection.cpp @@ -29,6 +29,7 @@ #include "jobs/roommessagesjob.h" #include "jobs/syncjob.h" #include "jobs/mediathumbnailjob.h" +#include "jobs/downloadfilejob.h" #include <QtNetwork/QDnsLookup> #include <QtCore/QFile> @@ -323,6 +324,47 @@ MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, int requestedWidth, return getThumbnail(url, QSize(requestedWidth, requestedHeight)); } +UploadContentJob* Connection::uploadContent(QIODevice* contentSource, + const QString& filename, const QString& contentType) const +{ + return callApi<UploadContentJob>(contentSource, filename, contentType); +} + +UploadContentJob* Connection::uploadFile(const QString& fileName, + const QString& contentType) +{ + auto sourceFile = new QFile(fileName); + if (sourceFile->open(QIODevice::ReadOnly)) + { + qCWarning(MAIN) << "Couldn't open" << sourceFile->fileName() + << "for reading"; + return nullptr; + } + return uploadContent(sourceFile, QFileInfo(*sourceFile).fileName(), + contentType); +} + +GetContentJob* Connection::getContent(const QString& mediaId) const +{ + auto idParts = splitMediaId(mediaId); + return callApi<GetContentJob>(idParts.front(), idParts.back()); +} + +GetContentJob* Connection::getContent(const QUrl& url) const +{ + return getContent(url.authority() + url.path()); +} + +DownloadFileJob* Connection::downloadFile(const QUrl& url, + const QString& localFilename) const +{ + auto mediaId = url.authority() + url.path(); + auto idParts = splitMediaId(mediaId); + auto* job = callApi<DownloadFileJob>(idParts.front(), idParts.back(), + localFilename); + return job; +} + ForgetRoomJob* Connection::forgetRoom(const QString& id) { // To forget is hard :) First we should ensure the local user is not diff --git a/connection.h b/connection.h index 1d483fe8..79d7d658 100644 --- a/connection.h +++ b/connection.h @@ -41,6 +41,9 @@ namespace QMatrixClient class PostReceiptJob; class MediaThumbnailJob; class JoinRoomJob; + class UploadContentJob; + class GetContentJob; + class DownloadFileJob; class Connection: public QObject { Q_OBJECT @@ -182,6 +185,18 @@ namespace QMatrixClient int requestedWidth, int requestedHeight) const; + // QIODevice* should already be open + virtual UploadContentJob* uploadContent(QIODevice* contentSource, + const QString& filename = {}, + const QString& contentType = {}) const; + virtual UploadContentJob* uploadFile(const QString& fileName, + const QString& contentType = {}); + virtual GetContentJob* getContent(const QString& mediaId) const; + GetContentJob* getContent(const QUrl& url) const; + // If localFilename is empty, a temporary file will be created + virtual DownloadFileJob* downloadFile(const QUrl& url, + const QString& localFilename = {}) const; + virtual JoinRoomJob* joinRoom(const QString& roomAlias); // Old API that will be abolished any time soon. DO NOT USE. |