File indexing completed on 2024-02-25 05:10:13

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "downloadavartarurlmanager.h"
0008 
0009 #include "ruqola_debug.h"
0010 #include <QNetworkAccessManager>
0011 #include <QNetworkReply>
0012 
0013 DownloadAvartarUrlManager::DownloadAvartarUrlManager(QObject *parent)
0014     : QObject(parent)
0015     , mNetworkAccessManager(new QNetworkAccessManager(this))
0016 {
0017     connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &DownloadAvartarUrlManager::slotFileDownloaded);
0018     connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &DownloadAvartarUrlManager::slotSslErrors);
0019 }
0020 
0021 DownloadAvartarUrlManager::~DownloadAvartarUrlManager() = default;
0022 
0023 void DownloadAvartarUrlManager::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error)
0024 {
0025     reply->ignoreSslErrors(error);
0026 }
0027 
0028 void DownloadAvartarUrlManager::slotFileDownloaded(QNetworkReply *reply)
0029 {
0030     const QByteArray data = reply->readAll();
0031     reply->deleteLater();
0032     Q_EMIT avatarDownloaded(data, mAvatarUrl);
0033     deleteLater();
0034 }
0035 
0036 bool DownloadAvartarUrlManager::canStart() const
0037 {
0038     return !mAvatarUrl.isEmpty();
0039 }
0040 
0041 void DownloadAvartarUrlManager::start()
0042 {
0043     if (!canStart()) {
0044         qCWarning(RUQOLA_LOG) << " FileName is empty";
0045         deleteLater();
0046         return;
0047     }
0048     const QUrl url(mAvatarUrl);
0049     QNetworkRequest request(url);
0050     mNetworkAccessManager->get(request);
0051 }
0052 
0053 QString DownloadAvartarUrlManager::avatarUrl() const
0054 {
0055     return mAvatarUrl;
0056 }
0057 
0058 void DownloadAvartarUrlManager::setAvatarUrl(const QString &avatarUrl)
0059 {
0060     mAvatarUrl = avatarUrl;
0061 }
0062 
0063 #include "moc_downloadavartarurlmanager.cpp"