| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "ImageHelper.h" |
| 19 | #include <QCoreApplication> |
| 20 | #include <QUrl> |
| 21 | #include <QFile> |
| 22 | #include <QPainter> |
| 23 | #include <QPainterPath> |
| 24 | #include <QCryptographicHash> |
| 25 | #include <QDebug> |
| 26 | #include <QImage> |
| 27 | #include <QString> |
| 28 | #include <QStringBuilder> |
| 29 | #include <QDir> |
| 30 | #include <QResource> |
| 31 | #include <QtConcurrent/QtConcurrentRun> |
| 32 | #include <QQmlNetworkAccessManagerFactory> |
| 33 | #include <QFuture> |
| 34 | #include <QtQuick> |
| 35 | #include <Paths.h> |
| 36 | |
| 37 | |
| 38 | QString ImageHelper::s_basePath; |
| 39 | QString ImageHelper::s_cachePath; |
| 40 | |
| 41 | ImageHelper::ImageHelper(QObject *parent) : |
| 42 | QObject(parent), m_icon(Paths::ActionIcon::NoIcon),m_size(0), m_verticalRatio(1.0), m_horizontalRatio(1.0), |
| 43 | m_color(Qt::transparent), m_nam(0), m_ready(false), m_requestSent(false), m_alreadySeen(false) |
| 44 | { |
| 45 | connect(this, SIGNAL(refresh()), this, SLOT(handleRefresh())); |
| 46 | |
| 47 | if (s_basePath.isEmpty()) { |
| 48 | // Our default base path is loading from qrc files. This can be overidden to point |
| 49 | // to any allowed path. |
| 50 | ImageHelper::s_basePath = QStringLiteral(":/actions"); |
| 51 | } |
| 52 | if (s_cachePath.isEmpty()) { |
| 53 | ImageHelper::s_cachePath = (Paths::standardCacheLocation() % "/.ImageCache"); |
| 54 | } |
| 55 | |
| 56 | connect(this, &ImageHelper::gravatarEmailChanged, [=]() { |
| 57 | QByteArray hash = QCryptographicHash::hash((m_gravatarEmail.toLower().toUtf8()),QCryptographicHash::Sha256).toHex(); |
| 58 | QString gravUrl = QStringLiteral("https://www.gravatar.com/avatar/%1?d=404&s=%2").arg(hash, QString::number(m_size)); |
| 59 | setGravatarUrl(QUrl(gravUrl)); |
| 60 | }); |
| 61 | connect(&m_watcher, SIGNAL(finished()), this, SLOT(writeUrlToQmlProperty())); |
| 62 | } |
| 63 | |
| 64 | ImageHelper::~ImageHelper() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void ImageHelper::setBasePath(const QString &basePath) |
| 69 | { |
| 70 | ImageHelper::s_basePath = basePath; |
| 71 | } |
| 72 | |
| 73 | void ImageHelper::setCachePath(const QString &cachePath) |
| 74 | { |
| 75 | ImageHelper::s_cachePath = cachePath; |
| 76 | } |
| 77 | |
| 78 | void ImageHelper::classBegin() |
| 79 | { |
| 80 | m_ready = false; |
| 81 | } |
| 82 | |
| 83 | void ImageHelper::componentComplete() |
| 84 | { |
| 85 | m_ready = true; |
| 86 | emit refresh(); |
| 87 | } |
| 88 | |
| 89 | void ImageHelper::setTarget(const QQmlProperty &target) |
| 90 | { |
| 91 | m_property = target; |
| 92 | emit refresh(); |
| 93 | } |
| 94 | |
| 95 | QUrl ImageHelper::gravatarUrl() const |
| 96 | { |
| 97 | return m_gravatarUrl; |
| 98 | } |
| 99 | |
| 100 | void ImageHelper::setGravatarUrl(const QUrl &gravurl) |
| 101 | { |
| 102 | if (gravurl.isEmpty() || !gravurl.isValid()) { |
| 103 | return; |
| 104 | } |
| 105 | if (m_gravatarEmail.isEmpty()) { |
| 106 | return; |
| 107 | } |
| 108 | if (m_gravatarUrl != gravurl) { |
| 109 | m_alreadySeen = false; |
| 110 | } |
| 111 | if (m_size == 0 || m_alreadySeen) { |
| 112 | return; |
| 113 | } |
| 114 | m_gravatarUrl = gravurl; |
| 115 | m_alreadySeen = true; |
| 116 | // TODO: THis isn't really scalable but we only have a few atm. Once we have some more it probably makes |
| 117 | // sense to move these to a qsettings file and query groups. |
| 118 | if (m_gravatarEmail.contains(QStringLiteral("ubports.com"))) { |
| 119 | if (m_property.isValid() && m_property.isWritable()) { |
| 120 | m_property.write(QUrl::fromLocalFile(QStringLiteral(":/provider/ubports-forum.jpg"))); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | if (m_gravatarEmail.contains(QStringLiteral("launchpad.net"))) { |
| 125 | if (m_property.isValid() && m_property.isWritable()) { |
| 126 | m_property.write(QUrl::fromLocalFile(QStringLiteral(":/provider/lp_logo.png"))); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | if (m_gravatarEmail.contains(QStringLiteral("plus.google.com"))) { |
| 131 | if (m_property.isValid() && m_property.isWritable()) { |
| 132 | m_property.write(QUrl::fromLocalFile(QStringLiteral(":/provider/Google_plus.png"))); |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | if (m_gravatarEmail.contains(QStringLiteral("[email protected]"))) { |
| 137 | if (m_property.isValid() && m_property.isWritable()) { |
| 138 | m_property.write(QUrl::fromLocalFile(QStringLiteral(":/provider/Google_Calendar.png"))); |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | if (m_gravatarEmail.contains(QStringLiteral("ubuntu-sdk-build-bot"))) { |
| 143 | if (m_property.isValid() && m_property.isWritable()) { |
| 144 | m_property.write(QUrl::fromLocalFile(QStringLiteral(":/provider/ubuntu-sdk-build-bot.png"))); |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | QString avatarCache(s_cachePath % "/" % m_gravatarEmail % QString::number(m_size) % ".png"); |
| 149 | if (QFile::exists(avatarCache)) { |
| 150 | if (m_property.isValid() && m_property.isWritable()) { |
| 151 | m_property.write(QUrl::fromLocalFile(avatarCache)); |
| 152 | } |
| 153 | } else { |
| 154 | if (!m_requestSent) { |
| 155 | m_nam = new QNetworkAccessManager(this); |
| 156 | connect(m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*))); |
| 157 | QNetworkRequest request(m_gravatarUrl); |
| 158 | m_nam->get(request); |
| 159 | m_requestSent = true; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void ImageHelper::handleRefresh() |
| 165 | { |
| 166 | if (m_ready) { |
| 167 | if ((Paths::ActionIcon)m_icon != Paths::ActionIcon::NoIcon && m_size > 0 && m_horizontalRatio > 0.0 && m_verticalRatio > 0.0) { |
| 168 | // So this is quite an intensive task so we move it to a seperate thread to not starve the gui |
| 169 | // benchmarking shows this greatly increases our load times in the message delegate, as this is a completely |
| 170 | // asynchronous job and we actually only do the converting to png the once and just return the cached |
| 171 | // version on each subsequent request. |
| 172 | SvgToPngWorker *worker = new SvgToPngWorker(this, ImageHelper::s_basePath, ImageHelper::s_cachePath); |
| 173 | m_future = QtConcurrent::run(worker, &SvgToPngWorker::convertSvgToPng, |
| 174 | m_size, m_horizontalRatio, m_verticalRatio, |
| 175 | m_color, Paths::iconUrl((Paths::ActionIcon)m_icon)); |
| 176 | m_watcher.setFuture(m_future); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void ImageHelper::finished(QNetworkReply *reply) |
| 182 | { |
| 183 | QString avatarCache(s_cachePath % "/" % m_gravatarEmail % QString::number(m_size) % ".png"); |
| 184 | if (reply->error() == QNetworkReply::NoError) { |
| 185 | GravatarWorker *worker = new GravatarWorker(this); |
| 186 | m_future = QtConcurrent::run(worker, &GravatarWorker::saveGravatar, reply->readAll(), avatarCache); |
| 187 | m_watcher.setFuture(m_future); |
| 188 | } else { |
| 189 | m_property.write(QUrl()); |
| 190 | } |
| 191 | // let's just ignore anything other than a NoError as we don't need to handle it. |
| 192 | reply->deleteLater(); |
| 193 | m_nam->deleteLater(); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | void ImageHelper::writeUrlToQmlProperty() |
| 198 | { |
| 199 | QUrl url = m_future.result(); |
| 200 | if (!url.isValid() || url.isEmpty()) { |
| 201 | return; |
| 202 | } |
| 203 | if (m_property.isValid() && m_property.isWritable()) { |
| 204 | m_property.write(url); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | SvgToPngWorker::SvgToPngWorker(QObject *parent, QString &basePath, QString &cachePath) : |
| 209 | QObject(parent), m_basePath(basePath), m_cachePath(cachePath) |
| 210 | { |
| 211 | |
| 212 | } |
| 213 | |
| 214 | QUrl SvgToPngWorker::convertSvgToPng(const int &size, const qreal &h, const qreal &v, const QColor &color, const QString &icon) |
| 215 | { |
| 216 | QUrl url; |
| 217 | if (!icon.isEmpty() && size > 0 && h > 0.0 && v > 0.0) { |
| 218 | QImage image (size * h, size * v, QImage::Format_ARGB32); |
| 219 | QString uri (icon |
| 220 | % "?color=" % (color.isValid() ? color.name() : QStringLiteral("none")) |
| 221 | % "&width=" % QString::number (image.width()) |
| 222 | % "&height=" % QString::number (image.height())); |
| 223 | QString hash (QCryptographicHash::hash(uri.toLocal8Bit(), QCryptographicHash::Md5).toHex()); |
| 224 | QString sourcePath = icon; |
| 225 | QString cachedPath = QStringLiteral("%1/.ImageCache/%2.png").arg(Paths::standardCacheLocation(), hash); |
| 226 | if(!QFile::exists(cachedPath)) { |
| 227 | QPainter painter(&image); |
| 228 | image.fill(Qt::transparent); |
| 229 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 230 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 231 | painter.setRenderHint(QPainter::HighQualityAntialiasing, true); |
| 232 | QSvgRenderer renderer; |
| 233 | if (QFile::exists(sourcePath)) { |
| 234 | renderer.load(sourcePath); |
| 235 | if (renderer.isValid()) { |
| 236 | renderer.render(&painter); |
| 237 | if (color.isValid() && color.alpha() > 0) { |
| 238 | QColor tmp (color); |
| 239 | for (int x = 0; x < image.width(); x++) { |
| 240 | for (int y = 0; y < image.height(); y++) { |
| 241 | tmp.setAlpha(qAlpha (image.pixel(x, y))); |
| 242 | image.setPixel(x, y, tmp.rgba()); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | QDir().mkpath(m_cachePath); |
| 247 | image.save(cachedPath, "PNG", 0); |
| 248 | url = QUrl::fromLocalFile(cachedPath); |
| 249 | } |
| 250 | } |
| 251 | else { |
| 252 | qWarning() << ">>> SvgToPngWorker: Can't render" << sourcePath << ", no such file!"; |
| 253 | } |
| 254 | } |
| 255 | else { |
| 256 | url = QUrl::fromLocalFile(cachedPath); |
| 257 | } |
| 258 | } |
| 259 | return url; |
| 260 | } |
| 261 | |
| 262 | GravatarWorker::GravatarWorker(QObject *parent) : QObject(parent) |
| 263 | { |
| 264 | } |
| 265 | |
| 266 | QUrl GravatarWorker::saveGravatar(const QByteArray &reply, const QString &path) |
| 267 | { |
| 268 | QImage *img = new QImage(); |
| 269 | img->loadFromData(reply, 0); |
| 270 | if (img->isNull()) { |
| 271 | qDebug() << ">>> GravatarWorker: Failed loading gravatar from reply"; |
| 272 | return QUrl(); |
| 273 | } |
| 274 | if (img->save(path, "PNG", 0)) { |
| 275 | return QUrl::fromLocalFile(path); |
| 276 | } |
| 277 | return QUrl(); |
| 278 | } |
| 279 | |
| 280 |