summary refs log tree commit diff
path: root/src/JdenticonProvider.cpp
blob: fd7ee137530907b9708e4fa49d7e16e1c2155552 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "JdenticonProvider.h"

#include <QApplication>
#include <QDir>
#include <QPainter>
#include <QPluginLoader>
#include <QSvgRenderer>

#include <mtxclient/crypto/client.hpp>

#include "Cache.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"
#include "jdenticoninterface.h"

JdenticonResponse::JdenticonResponse(const QString &key, const QSize &requestedSize)
  : m_key(key)
  , m_requestedSize(requestedSize.isValid() ? requestedSize : QSize(100, 100))
  , m_pixmap{m_requestedSize}
  , jdenticonInterface_{Jdenticon::getJdenticonInterface()}
{
        setAutoDelete(false);
}

void
JdenticonResponse::run()
{
        m_pixmap.fill(Qt::transparent);
        QPainter painter{&m_pixmap};
        QSvgRenderer renderer{
          jdenticonInterface_->generate(m_key, m_requestedSize.width()).toUtf8()};
        //        m_image = QImage::fromData(jdenticonInterface_->generate(m_key,
        //        size->width()).toUtf8());
        renderer.render(&painter);

        emit finished();
}

namespace Jdenticon {
JdenticonInterface *
getJdenticonInterface()
{
        static JdenticonInterface *interface = nullptr;

        if (interface == nullptr) {
                QDir pluginsDir(qApp->applicationDirPath());

                bool plugins = pluginsDir.cd("plugins");
                if (plugins) {
                        for (QString fileName : pluginsDir.entryList(QDir::Files)) {
                                QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
                                QObject *plugin = pluginLoader.instance();
                                if (plugin) {
                                        interface = qobject_cast<JdenticonInterface *>(plugin);
                                        if (interface) {
                                                nhlog::ui()->info("Loaded jdenticon plugin.");
                                                break;
                                        }
                                }
                        }
                } else {
                        nhlog::ui()->info("jdenticon plugin not found.");
                }
        }

        return interface;
}
}