| 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 "fgqcanvasimageloader.h" |
| 19 | |
| 20 | #include <QDebug> |
| 21 | #include <QNetworkAccessManager> |
| 22 | #include <QStandardPaths> |
| 23 | #include <QDir> |
| 24 | #include <QFile> |
| 25 | #include <QFileInfo> |
| 26 | |
| 27 | class TransferSignalHolder : public QObject |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | public: |
| 31 | TransferSignalHolder(QObject* pr) : QObject(pr) { } |
| 32 | signals: |
| 33 | void trigger(); |
| 34 | }; |
| 35 | |
| 36 | FGQCanvasImageLoader::FGQCanvasImageLoader(QNetworkAccessManager* dl, QObject* pr) |
| 37 | : QObject(pr) |
| 38 | , m_downloader(dl) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void FGQCanvasImageLoader::onDownloadFinished() |
| 43 | { |
| 44 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 45 | |
| 46 | QPixmap pm; |
| 47 | if (!pm.loadFromData(reply->readAll())) { |
| 48 | qWarning() << "image loading failed"; |
| 49 | } else { |
| 50 | QByteArray imagePath = reply->property("image").toByteArray(); |
| 51 | m_cache.insert(imagePath, pm); |
| 52 | |
| 53 | // cache on disk also, so snapshots work |
| 54 | writeToDiskCache(imagePath, reply); |
| 55 | |
| 56 | TransferSignalHolder* signalHolder = reply->findChild<TransferSignalHolder*>("holder"); |
| 57 | if (signalHolder) { |
| 58 | qDebug() << "triggering image updates"; |
| 59 | signalHolder->trigger(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | m_transfers.removeOne(reply); |
| 64 | reply->deleteLater(); |
| 65 | } |
| 66 | |
| 67 | void FGQCanvasImageLoader::writeToDiskCache(QByteArray imagePath, QNetworkReply* reply) |
| 68 | { |
| 69 | QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); |
| 70 | QString absPath = cacheDir.absoluteFilePath(imagePath); |
| 71 | |
| 72 | QFileInfo finfo(imagePath); |
| 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 | f.write(reply->readAll()); |
| 81 | f.close(); |
| 82 | } |
| 83 | |
| 84 | void FGQCanvasImageLoader::setHost(QString hostName, int portNumber) |
| 85 | { |
| 86 | m_hostName = hostName; |
| 87 | m_port = portNumber; |
| 88 | } |
| 89 | |
| 90 | QPixmap FGQCanvasImageLoader::getImage(const QByteArray &imagePath) |
| 91 | { |
| 92 | if (m_cache.contains(imagePath)) { |
| 93 | // cached, easy |
| 94 | return m_cache.value(imagePath); |
| 95 | } |
| 96 | |
| 97 | QString diskCachePath = QStandardPaths::locate(QStandardPaths::CacheLocation, imagePath); |
| 98 | if (!diskCachePath.isEmpty()) { |
| 99 | QPixmap pix; |
| 100 | pix.load(diskCachePath); |
| 101 | m_cache.insert(imagePath, pix); |
| 102 | qDebug() << "loaded from on-disk cache:" << imagePath; |
| 103 | return pix; |
| 104 | } |
| 105 | |
| 106 | QUrl url; |
| 107 | url.setScheme("http"); |
| 108 | url.setHost(m_hostName); |
| 109 | url.setPort(m_port); |
| 110 | url.setPath("/aircraft-dir/" + imagePath); |
| 111 | |
| 112 | Q_FOREACH (QNetworkReply* transfer, m_transfers) { |
| 113 | if (transfer->url() == url) { |
| 114 | return QPixmap(); // transfer already active |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | QNetworkReply* reply = m_downloader->get(QNetworkRequest(url)); |
| 119 | reply->setProperty("image", imagePath); |
| 120 | |
| 121 | connect(reply, &QNetworkReply::finished, this, &FGQCanvasImageLoader::onDownloadFinished); |
| 122 | m_transfers.append(reply); |
| 123 | |
| 124 | return QPixmap(); |
| 125 | } |
| 126 | |
| 127 | void FGQCanvasImageLoader::connectToImageLoaded(const QByteArray &imagePath, QObject *receiver, const char *slot) |
| 128 | { |
| 129 | Q_FOREACH (QNetworkReply* transfer, m_transfers) { |
| 130 | if (transfer->property("image").toByteArray() == imagePath) { |
| 131 | QObject* signalHolder = transfer->findChild<QObject*>("holder"); |
| 132 | if (!signalHolder) { |
| 133 | signalHolder = new TransferSignalHolder(transfer); |
| 134 | signalHolder->setObjectName("holder"); |
| 135 | } |
| 136 | |
| 137 | connect(signalHolder, SIGNAL(trigger()), receiver, slot); |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | qWarning() << "no transfer active for" << imagePath; |
| 143 | } |
| 144 | |
| 145 | #include "fgqcanvasimageloader.moc" |