| 1 | // |
| 2 | // Copyright (C) 2017 James Turner [email protected] |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU General Public License as |
| 6 | // published by the Free Software Foundation; either version 2 of the |
| 7 | // License, or (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, but |
| 10 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | #include "fgqcanvasfontcache.h" |
| 19 | |
| 20 | #include <QNetworkAccessManager> |
| 21 | #include <QStandardPaths> |
| 22 | #include <QDebug> |
| 23 | #include <QUrl> |
| 24 | #include <QNetworkReply> |
| 25 | #include <QFontDatabase> |
| 26 | #include <QFile> |
| 27 | |
| 28 | FGQCanvasFontCache::FGQCanvasFontCache(QNetworkAccessManager* nam, QObject *parent) |
| 29 | : QObject(parent) |
| 30 | , m_downloader(nam) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | QFont FGQCanvasFontCache::fontForName(QByteArray name, bool* ok) |
| 35 | { |
| 36 | if (m_cache.contains(name)) { |
| 37 | if (ok) { |
| 38 | *ok = true; |
| 39 | } |
| 40 | return m_cache.value(name); // easy! |
| 41 | } |
| 42 | |
| 43 | lookupFile(name); |
| 44 | if (m_cache.contains(name)) { |
| 45 | if (ok) { |
| 46 | *ok = true; |
| 47 | } |
| 48 | return m_cache.value(name); |
| 49 | } |
| 50 | |
| 51 | if (ok) { |
| 52 | *ok = false; |
| 53 | } |
| 54 | |
| 55 | return QFont(); // default font |
| 56 | } |
| 57 | |
| 58 | void FGQCanvasFontCache::setHost(QString hostName, int portNumber) |
| 59 | { |
| 60 | m_hostName = hostName; |
| 61 | m_port = portNumber; |
| 62 | } |
| 63 | |
| 64 | void FGQCanvasFontCache::onFontDownloadFinished() |
| 65 | { |
| 66 | QByteArray fontPath = sender()->property("font").toByteArray(); |
| 67 | qDebug() << "finished download of " << fontPath; |
| 68 | |
| 69 | QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); |
| 70 | QString absPath = cacheDir.absoluteFilePath(fontPath); |
| 71 | |
| 72 | QFileInfo finfo(fontPath); |
| 73 | cacheDir.mkpath(finfo.dir().path()); |
| 74 | |
| 75 | QFile f(absPath); |
| 76 | if (!f.open(QIODevice::WriteOnly)) { |
| 77 | qWarning() << "failed to open cache file" << f.fileName(); |
| 78 | } |
| 79 | |
| 80 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 81 | Q_ASSERT(m_transfers.contains(reply)); |
| 82 | |
| 83 | f.write(reply->readAll()); |
| 84 | f.close(); |
| 85 | |
| 86 | m_transfers.removeOne(reply); |
| 87 | |
| 88 | // call ourselves again now it's cached; |
| 89 | lookupFile(fontPath); |
| 90 | } |
| 91 | |
| 92 | void FGQCanvasFontCache::onFontDownloadError(QNetworkReply::NetworkError) |
| 93 | { |
| 94 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 95 | qWarning() << "font download failed:" << reply->errorString(); |
| 96 | } |
| 97 | |
| 98 | void FGQCanvasFontCache::lookupFile(QByteArray name) |
| 99 | { |
| 100 | QString path = QStandardPaths::locate(QStandardPaths::CacheLocation, name); |
| 101 | if (!path.isEmpty()) { |
| 102 | qDebug() << "found font" << name << "at path" << path; |
| 103 | |
| 104 | int fontFamilyId = QFontDatabase::addApplicationFont(path); |
| 105 | if (fontFamilyId >= 0) { |
| 106 | QStringList families = QFontDatabase::applicationFontFamilies(fontFamilyId); |
| 107 | qDebug() << "families are:" << families; |
| 108 | |
| 109 | // compute a QFont and cache |
| 110 | QFont font(families.front()); |
| 111 | m_cache.insert(name, font); |
| 112 | return; |
| 113 | } else { |
| 114 | qWarning() << "Failed to load font into QFontDatabase:" << path; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (m_hostName.isEmpty()) { |
| 119 | qWarning() << "host name not specified"; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | QUrl url; |
| 124 | url.setScheme("http"); |
| 125 | url.setHost(m_hostName); |
| 126 | url.setPort(m_port); |
| 127 | url.setPath("/Fonts/" + name); |
| 128 | |
| 129 | Q_FOREACH (QNetworkReply* transfer, m_transfers) { |
| 130 | if (transfer->url() == url) { |
| 131 | return; // transfer already active |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | qDebug() << "reqeusting font" << url; |
| 136 | QNetworkReply* reply = m_downloader->get(QNetworkRequest(url)); |
| 137 | reply->setProperty("font", name); |
| 138 | |
| 139 | connect(reply, &QNetworkReply::finished, this, &FGQCanvasFontCache::onFontDownloadFinished); |
| 140 | connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 141 | this, SLOT(onFontDownloadError(QNetworkReply::NetworkError))); |
| 142 | m_transfers.append(reply); |
| 143 | } |
| 144 | |
| 145 |