File indexing completed on 2025-01-05 04:29:55

0001 /**
0002  * SPDX-FileCopyrightText: 2023 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "networkconnectionmanager.h"
0008 #include "networkconnectionmanagerlogging.h"
0009 
0010 #include <QNetworkInformation>
0011 
0012 #include "settingsmanager.h"
0013 
0014 NetworkConnectionManager::NetworkConnectionManager(QObject *parent)
0015     : QObject(parent)
0016 {
0017     m_backendAvailable = QNetworkInformation::loadDefaultBackend();
0018 
0019     if (m_backendAvailable) {
0020         connect(QNetworkInformation::instance(), &QNetworkInformation::reachabilityChanged, this, [this]() {
0021             Q_EMIT networkReachableChanged();
0022             Q_EMIT feedUpdatesAllowedChanged();
0023             Q_EMIT episodeDownloadsAllowedChanged();
0024             Q_EMIT imageDownloadsAllowedChanged();
0025             Q_EMIT streamingAllowedChanged();
0026         });
0027         connect(QNetworkInformation::instance(), &QNetworkInformation::isMeteredChanged, this, [this]() {
0028             Q_EMIT networkReachableChanged();
0029             Q_EMIT feedUpdatesAllowedChanged();
0030             Q_EMIT episodeDownloadsAllowedChanged();
0031             Q_EMIT imageDownloadsAllowedChanged();
0032             Q_EMIT streamingAllowedChanged();
0033         });
0034     }
0035 
0036     connect(SettingsManager::self(), &SettingsManager::checkNetworkStatusChanged, this, [this]() {
0037         Q_EMIT networkReachableChanged();
0038         Q_EMIT feedUpdatesAllowedChanged();
0039         Q_EMIT episodeDownloadsAllowedChanged();
0040         Q_EMIT imageDownloadsAllowedChanged();
0041         Q_EMIT streamingAllowedChanged();
0042     });
0043 
0044     connect(SettingsManager::self(), &SettingsManager::allowMeteredFeedUpdatesChanged, this, &NetworkConnectionManager::feedUpdatesAllowedChanged);
0045     connect(SettingsManager::self(), &SettingsManager::allowMeteredEpisodeDownloadsChanged, this, &NetworkConnectionManager::episodeDownloadsAllowedChanged);
0046     connect(SettingsManager::self(), &SettingsManager::allowMeteredImageDownloadsChanged, this, &NetworkConnectionManager::imageDownloadsAllowedChanged);
0047     connect(SettingsManager::self(), &SettingsManager::allowMeteredStreamingChanged, this, &NetworkConnectionManager::streamingAllowedChanged);
0048 }
0049 
0050 bool NetworkConnectionManager::networkReachable() const
0051 {
0052     bool reachable = true;
0053     if (m_backendAvailable) {
0054         reachable = (QNetworkInformation::instance()->reachability() != QNetworkInformation::Reachability::Disconnected
0055                      || !SettingsManager::self()->checkNetworkStatus());
0056     }
0057 
0058     qCDebug(kastsNetworkConnectionManager) << "networkReachable()" << reachable;
0059 
0060     return reachable;
0061 }
0062 
0063 bool NetworkConnectionManager::feedUpdatesAllowed() const
0064 {
0065     bool allowed = true;
0066     if (m_backendAvailable) {
0067         allowed = (networkReachable()
0068                    && (!QNetworkInformation::instance()->isMetered() || SettingsManager::self()->allowMeteredFeedUpdates()
0069                        || !SettingsManager::self()->checkNetworkStatus()));
0070     }
0071 
0072     qCDebug(kastsNetworkConnectionManager) << "FeedUpdatesAllowed()" << allowed;
0073 
0074     return allowed;
0075 }
0076 
0077 bool NetworkConnectionManager::episodeDownloadsAllowed() const
0078 {
0079     bool allowed = true;
0080     if (m_backendAvailable) {
0081         allowed = (networkReachable()
0082                    && (!QNetworkInformation::instance()->isMetered() || SettingsManager::self()->allowMeteredEpisodeDownloads()
0083                        || !SettingsManager::self()->checkNetworkStatus()));
0084     }
0085 
0086     qCDebug(kastsNetworkConnectionManager) << "EpisodeDownloadsAllowed()" << allowed;
0087 
0088     return allowed;
0089 }
0090 
0091 bool NetworkConnectionManager::imageDownloadsAllowed() const
0092 {
0093     bool allowed = true;
0094     if (m_backendAvailable) {
0095         allowed = (networkReachable()
0096                    && (!QNetworkInformation::instance()->isMetered() || SettingsManager::self()->allowMeteredImageDownloads()
0097                        || !SettingsManager::self()->checkNetworkStatus()));
0098     }
0099 
0100     qCDebug(kastsNetworkConnectionManager) << "ImageDownloadsAllowed()" << allowed;
0101 
0102     return allowed;
0103 }
0104 
0105 bool NetworkConnectionManager::streamingAllowed() const
0106 {
0107     bool allowed = true;
0108     if (m_backendAvailable) {
0109         allowed = (networkReachable()
0110                    && (!QNetworkInformation::instance()->isMetered() || SettingsManager::self()->allowMeteredStreaming()
0111                        || !SettingsManager::self()->checkNetworkStatus()));
0112     }
0113 
0114     qCDebug(kastsNetworkConnectionManager) << "StreamingAllowed()" << allowed;
0115 
0116     return allowed;
0117 }