diff options
Diffstat (limited to 'connection.cpp')
-rw-r--r-- | connection.cpp | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/connection.cpp b/connection.cpp index 53a38f0d..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> @@ -187,8 +188,7 @@ void Connection::Private::connectWithToken(const QString& user, data->setToken(accessToken.toLatin1()); data->setDeviceId(deviceId); qCDebug(MAIN) << "Using server" << data->baseUrl() << "by user" - << userId - << "from device" << deviceId; + << userId << "from device" << deviceId; emit q->connected(); } @@ -298,9 +298,24 @@ RoomMessagesJob* Connection::getMessages(Room* room, const QString& from) const return callApi<RoomMessagesJob>(room->id(), from); } +inline auto splitMediaId(const QString& mediaId) +{ + auto idParts = mediaId.split('/'); + Q_ASSERT_X(idParts.size() == 2, __FUNCTION__, + "mediaId should have a form 'serverName/localMediaId' (without apostrophes)"); + return idParts; +} + +MediaThumbnailJob* Connection::getThumbnail(const QString& mediaId, QSize requestedSize) const +{ + auto idParts = splitMediaId(mediaId); + return callApi<MediaThumbnailJob>(idParts.front(), idParts.back(), + requestedSize); +} + MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, QSize requestedSize) const { - return callApi<MediaThumbnailJob>(url, requestedSize); + return getThumbnail(url.authority() + url.path(), requestedSize); } MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, int requestedWidth, @@ -309,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 |