| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "JdenticonProvider.h" |
| 6 | |
| 7 | #include <QApplication> |
| 8 | #include <QPainter> |
| 9 | #include <QPainterPath> |
| 10 | #include <QPluginLoader> |
| 11 | #include <QSvgRenderer> |
| 12 | |
| 13 | #include <mtxclient/crypto/client.hpp> |
| 14 | |
| 15 | #include "Logging.h" |
| 16 | #include "jdenticoninterface.h" |
| 17 | |
| 18 | namespace Jdenticon { |
| 19 | JdenticonInterface * |
| 20 | getJdenticonInterface() |
| 21 | { |
| 22 | static JdenticonInterface *interface = nullptr; |
| 23 | static bool interfaceExists{true}; |
| 24 | |
| 25 | if (interface == nullptr && interfaceExists) { |
| 26 | QPluginLoader pluginLoader(QStringLiteral("qtjdenticon")); |
| 27 | QObject *plugin = pluginLoader.instance(); |
| 28 | if (plugin) { |
| 29 | interface = qobject_cast<JdenticonInterface *>(plugin); |
| 30 | if (interface) { |
| 31 | nhlog::ui()->info("Loaded jdenticon plugin."); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if (!interface) { |
| 36 | nhlog::ui()->info("jdenticon plugin not found."); |
| 37 | interfaceExists = false; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return interface; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static QPixmap |
| 46 | clipRadius(QPixmap img, double radius) |
| 47 | { |
| 48 | QPixmap out(img.size()); |
| 49 | out.fill(Qt::transparent); |
| 50 | |
| 51 | QPainter painter(&out); |
| 52 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 53 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 54 | |
| 55 | QPainterPath ppath; |
| 56 | ppath.addRoundedRect(img.rect(), radius, radius, Qt::SizeMode::RelativeSize); |
| 57 | |
| 58 | painter.setClipPath(ppath); |
| 59 | painter.drawPixmap(img.rect(), img); |
| 60 | |
| 61 | return out; |
| 62 | } |
| 63 | |
| 64 | JdenticonResponse::JdenticonResponse(const QString &key, double radius, const QSize &requestedSize) |
| 65 | { |
| 66 | auto runnable = new JdenticonRunnable(key, radius, requestedSize); |
| 67 | connect(runnable, &JdenticonRunnable::done, this, &JdenticonResponse::handleDone); |
| 68 | QThreadPool::globalInstance()->start(runnable); |
| 69 | } |
| 70 | |
| 71 | JdenticonRunnable::JdenticonRunnable(const QString &key, double radius, const QSize &requestedSize) |
| 72 | : m_key(key) |
| 73 | , m_radius{radius} |
| 74 | , m_requestedSize(requestedSize.isValid() ? requestedSize : QSize(100, 100)) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | JdenticonRunnable::run() |
| 80 | { |
| 81 | QPixmap pixmap(m_requestedSize); |
| 82 | pixmap.fill(Qt::transparent); |
| 83 | |
| 84 | auto jdenticon = Jdenticon::getJdenticonInterface(); |
| 85 | if (!jdenticon) { |
| 86 | emit done(pixmap.toImage()); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | QPainter painter; |
| 91 | painter.begin(&pixmap); |
| 92 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 93 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 94 | |
| 95 | try { |
| 96 | QSvgRenderer renderer{ |
| 97 | jdenticon->generate(m_key, static_cast<std::uint16_t>(m_requestedSize.width())).toUtf8()}; |
| 98 | renderer.render(&painter); |
| 99 | } catch (std::exception &e) { |
| 100 | nhlog::ui()->error( |
| 101 | "caught {} in jdenticonprovider, key '{}'", e.what(), m_key.toStdString()); |
| 102 | } |
| 103 | |
| 104 | painter.end(); |
| 105 | |
| 106 | pixmap = clipRadius(pixmap, m_radius); |
| 107 | |
| 108 | emit done(pixmap.toImage()); |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | JdenticonProvider::isAvailable() |
| 113 | { |
| 114 | return Jdenticon::getJdenticonInterface() != nullptr; |
| 115 | } |
| 116 | |
| 117 | #include "moc_JdenticonProvider.cpp" |