aboutsummaryrefslogtreecommitdiff
path: root/lib/networkaccessmanager.cpp
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2021-08-31 21:47:10 +0200
committerTobias Fella <fella@posteo.de>2021-09-05 22:09:17 +0200
commit4bab0f2ef2c68b478d669f90557d6bef6332e823 (patch)
tree59059f327120c318ee216c01040ee4aad8851ad0 /lib/networkaccessmanager.cpp
parent06a8ef6ebed5962117121486059ba46dc7f6d4f9 (diff)
downloadlibquotient-4bab0f2ef2c68b478d669f90557d6bef6332e823.tar.gz
libquotient-4bab0f2ef2c68b478d669f90557d6bef6332e823.zip
Implement the mxc protocol in the NetworkAccessManager
Allows images to be loaded using the NetworkAccessManager instead of an ImageProvider
Diffstat (limited to 'lib/networkaccessmanager.cpp')
-rw-r--r--lib/networkaccessmanager.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/networkaccessmanager.cpp b/lib/networkaccessmanager.cpp
index a94ead34..e4132957 100644
--- a/lib/networkaccessmanager.cpp
+++ b/lib/networkaccessmanager.cpp
@@ -5,6 +5,11 @@
#include <QtCore/QCoreApplication>
#include <QtNetwork/QNetworkReply>
+#include "accountregistry.h"
+#include "mxcreply.h"
+#include "connection.h"
+
+#include "room.h"
using namespace Quotient;
@@ -56,7 +61,58 @@ NetworkAccessManager::~NetworkAccessManager() = default;
QNetworkReply* NetworkAccessManager::createRequest(
Operation op, const QNetworkRequest& request, QIODevice* outgoingData)
{
+ if(request.url().scheme() == QStringLiteral("mxc")) {
+ const auto fragment = request.url().fragment();
+ const auto fragmentParts = fragment.split(QLatin1Char('/'));
+ const auto mediaId = request.url().toString(QUrl::RemoveScheme | QUrl::RemoveFragment);
+ if(fragmentParts.size() == 3) {
+ auto connection = AccountRegistry::instance().get(fragmentParts[0]);
+ if(!connection) {
+ qWarning() << "Connection not found";
+ return nullptr;
+ }
+ auto room = connection->room(fragmentParts[1]);
+ if(!room) {
+ qWarning() << "Room not found";
+ return nullptr;
+ }
+ QNetworkRequest r(request);
+ r.setUrl(QUrl(QStringLiteral("%1/_matrix/media/r0/download/%2").arg(connection->homeserver().toString(), mediaId)));
+ auto reply = createRequest(QNetworkAccessManager::GetOperation, r);
+ return new MxcReply(reply, room, fragmentParts[2]);
+ } else if(fragmentParts.size() == 1) {
+ auto connection = AccountRegistry::instance().get(fragment);
+ if(!connection) {
+ qWarning() << "Connection not found";
+ return nullptr;
+ }
+ QNetworkRequest r(request);
+ r.setUrl(QUrl(QStringLiteral("%1/_matrix/media/r0/download/%2").arg(connection->homeserver().toString(), mediaId)));
+ auto reply = createRequest(QNetworkAccessManager::GetOperation, r);
+ return new MxcReply(reply);
+ } else {
+ qWarning() << "Invalid request";
+ return nullptr;
+ }
+ }
auto reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
reply->ignoreSslErrors(d->ignoredSslErrors);
return reply;
}
+
+QStringList NetworkAccessManager::supportedSchemesImplementation() const
+{
+ auto schemes = QNetworkAccessManager::supportedSchemesImplementation();
+ schemes += QStringLiteral("mxc");
+ return schemes;
+}
+
+QUrl NetworkAccessManager::urlForRoomEvent(Room *room, const QString &eventId, const QString &mediaId)
+{
+ return QUrl(QStringLiteral("mxc:%1#%2/%3/%4").arg(mediaId, room->connection()->userId(), room->id(), eventId));
+}
+
+QUrl NetworkAccessManager::urlForFile(Connection *connection, const QString &mediaId)
+{
+ return QUrl(QStringLiteral("mxc:%1#%2").arg(mediaId, connection->userId()));
+} \ No newline at end of file