File indexing completed on 2024-05-05 04:45:34

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2013-2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 
0019 #include "icon.h"
0020 #include "../snore.h"
0021 #include "../snore_p.h"
0022 
0023 #include <QGuiApplication>
0024 #include <QNetworkAccessManager>
0025 #include <QNetworkReply>
0026 #include <QNetworkRequest>
0027 #include <QUrl>
0028 #include <QTime>
0029 
0030 using namespace Snore;
0031 
0032 QSet<QString> Icon::s_localImageCache;
0033 QMap<QUrl, Icon> Icon::s_downloadImageCache;
0034 
0035 Icon Icon::defaultIcon()
0036 {
0037     static Icon icon(QStringLiteral(":/root/snore.png"));
0038     return icon;
0039 }
0040 
0041 Icon Icon::fromWebUrl(const QUrl &url, int maxTime)
0042 {
0043     Icon icon = defaultIcon();
0044     qCDebug(SNORE) << url;
0045     if (!s_downloadImageCache.contains(url)) {
0046         qCDebug(SNORE) << "Downloading:" << url;
0047         QNetworkAccessManager *manager = new QNetworkAccessManager();
0048         QNetworkRequest request(url);
0049         QNetworkReply *reply = manager->get(request);
0050         QObject::connect(reply, &QNetworkReply::downloadProgress, [&](qint64 bytesReceived, qint64 bytesTotal) {
0051             qCDebug(SNORE) << "Downloading:" << url << bytesReceived / double(bytesTotal) * 100.0 << "%";
0052         });
0053 
0054         QTime time;
0055         time.start();
0056         while (!reply->isFinished() && time.elapsed() < maxTime) {
0057             qApp->processEvents(QEventLoop::AllEvents, maxTime);
0058         }
0059         if (reply->error() != QNetworkReply::NoError) {
0060             qCWarning(SNORE) << "Error downloading" << url << ":" << reply->errorString();
0061         } else {
0062             if (reply->isFinished()) {
0063                 QPixmap pix;
0064                 pix.loadFromData(reply->readAll());
0065                 icon = Icon(pix);
0066                 s_downloadImageCache.insert(url, icon);
0067                 qCDebug(SNORE) << url << "added to cache.";
0068             } else {
0069                 qCDebug(SNORE) << "Download of " << url << "timed out.";
0070             }
0071         }
0072 
0073         reply->close();
0074         reply->deleteLater();
0075         manager->deleteLater();
0076     } else {
0077         icon = s_downloadImageCache.value(url, defaultIcon());
0078         qCDebug(SNORE) << url << "from cache";
0079     }
0080     return icon;
0081 }
0082 
0083 Icon::Icon(const QPixmap &pixmap):
0084     QIcon(pixmap)
0085 {
0086 
0087 }
0088 
0089 Icon::Icon(const QIcon &other):
0090     QIcon(other)
0091 {
0092 
0093 }
0094 
0095 Icon::Icon(const QString &fileName):
0096     QIcon(fileName)
0097 {
0098 
0099 }
0100 
0101 QString Icon::localUrl(QSize size, Mode mode, State state)const
0102 {
0103     QString localFileName = SnoreCorePrivate::tempPath() + QLatin1Char('/') + QString::number(cacheKey()) + QLatin1String("_") + QString::number(size.width()) + QLatin1String("x") + QString::number(size.height()) + QLatin1String(".png");
0104     if (!s_localImageCache.contains(localFileName)) {
0105         QImage(pixmap(size, mode, state).toImage()).save(localFileName, "PNG");
0106         s_localImageCache.insert(localFileName);
0107     }
0108     return localFileName;
0109 }
0110