|
| 1 | +#include "appledb.h" |
| 2 | + |
| 3 | +#include "simplerequest.h" |
| 4 | +#include "utils.h" |
| 5 | + |
| 6 | +#include <QDir> |
| 7 | +#include <QFile> |
| 8 | +#include <QJsonArray> |
| 9 | +#include <QJsonDocument> |
| 10 | +#include <QJsonObject> |
| 11 | +#include <QRegularExpression> |
| 12 | +#include <QSaveFile> |
| 13 | + |
| 14 | +AppleDB::AppleDB(QObject* parent) |
| 15 | + : QObject(parent) |
| 16 | + , m_request(new SimpleRequest()) |
| 17 | + , m_localFilePath(GetDirectory(DIRECTORY_TYPE::LOCALDATA) + LOCAL_FILE_NAME) |
| 18 | + , m_isRefreshing(false) |
| 19 | +{ |
| 20 | + LoadLocal(); |
| 21 | +} |
| 22 | + |
| 23 | +AppleDB::~AppleDB() |
| 24 | +{ |
| 25 | + delete m_request; |
| 26 | +} |
| 27 | + |
| 28 | +void AppleDB::EnsureDownloadedAtLaunch() |
| 29 | +{ |
| 30 | + if (!QFile::exists(m_localFilePath)) |
| 31 | + RefreshFromNetwork(false); |
| 32 | +} |
| 33 | + |
| 34 | +QString AppleDB::ResolveProductType(const QString& productType, const std::function<void(const QString&)>& onResolved) |
| 35 | +{ |
| 36 | + if (productType.isEmpty()) |
| 37 | + return ""; |
| 38 | + |
| 39 | + const QString display = BuildDisplayName(productType); |
| 40 | + if (display != productType) |
| 41 | + return display; |
| 42 | + |
| 43 | + if (onResolved) |
| 44 | + m_pendingCallbacks[productType].append(onResolved); |
| 45 | + RefreshFromNetwork(true); |
| 46 | + return EstimateSeries(productType); |
| 47 | +} |
| 48 | + |
| 49 | +void AppleDB::LoadLocal() |
| 50 | +{ |
| 51 | + QFile file(m_localFilePath); |
| 52 | + if (!file.exists()) |
| 53 | + return; |
| 54 | + if (!file.open(QIODevice::ReadOnly)) |
| 55 | + return; |
| 56 | + |
| 57 | + ParseAndStore(file.readAll(), false); |
| 58 | +} |
| 59 | + |
| 60 | +void AppleDB::RefreshFromNetwork(bool force) |
| 61 | +{ |
| 62 | + if (m_isRefreshing) |
| 63 | + return; |
| 64 | + if (!force && QFile::exists(m_localFilePath)) |
| 65 | + return; |
| 66 | + |
| 67 | + m_isRefreshing = true; |
| 68 | + m_request->Download(REMOTE_URL, |
| 69 | + [this](SimpleRequest::RequestState state, int, QNetworkReply::NetworkError error, QByteArray data) |
| 70 | + { |
| 71 | + if (state != SimpleRequest::RequestState::STATE_FINISH && |
| 72 | + state != SimpleRequest::RequestState::STATE_ERROR) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (state == SimpleRequest::RequestState::STATE_FINISH && error == QNetworkReply::NoError) |
| 78 | + ParseAndStore(data, true); |
| 79 | + |
| 80 | + m_isRefreshing = false; |
| 81 | + ResolvePending(); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +bool AppleDB::ParseAndStore(const QByteArray& jsonData, bool persistToDisk) |
| 86 | +{ |
| 87 | + QJsonParseError error; |
| 88 | + const QJsonDocument document = QJsonDocument::fromJson(jsonData, &error); |
| 89 | + if (error.error != QJsonParseError::NoError || !document.isArray()) |
| 90 | + return false; |
| 91 | + |
| 92 | + QHash<QString, QString> parsed; |
| 93 | + const QJsonArray devices = document.array(); |
| 94 | + for (const QJsonValue& value : devices) |
| 95 | + { |
| 96 | + if (!value.isObject()) |
| 97 | + continue; |
| 98 | + |
| 99 | + const QJsonObject device = value.toObject(); |
| 100 | + const QString name = device["name"].toString(); |
| 101 | + if (name.isEmpty()) |
| 102 | + continue; |
| 103 | + |
| 104 | + const QJsonValue identifierValue = device["identifier"]; |
| 105 | + if (identifierValue.isArray()) |
| 106 | + { |
| 107 | + for (const QJsonValue& identifier : identifierValue.toArray()) |
| 108 | + { |
| 109 | + const QString productType = identifier.toString(); |
| 110 | + if (!productType.isEmpty()) |
| 111 | + parsed[productType] = name; |
| 112 | + } |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + const QString productType = identifierValue.toString(); |
| 117 | + if (!productType.isEmpty()) |
| 118 | + parsed[productType] = name; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (parsed.isEmpty()) |
| 123 | + return false; |
| 124 | + |
| 125 | + m_deviceNameByIdentifier = parsed; |
| 126 | + if (persistToDisk) |
| 127 | + { |
| 128 | + QDir().mkpath(GetDirectory(DIRECTORY_TYPE::LOCALDATA)); |
| 129 | + QSaveFile file(m_localFilePath); |
| 130 | + if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
| 131 | + { |
| 132 | + file.write(jsonData); |
| 133 | + file.commit(); |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +QString AppleDB::BuildDisplayName(const QString& productType) const |
| 140 | +{ |
| 141 | + const QString resolved = m_deviceNameByIdentifier.value(productType); |
| 142 | + if (resolved.isEmpty()) |
| 143 | + return productType; |
| 144 | + return resolved; |
| 145 | +} |
| 146 | + |
| 147 | +QString AppleDB::EstimateSeries(const QString& productType) const |
| 148 | +{ |
| 149 | + QRegularExpressionMatch match = QRegularExpression("^iPhone(\\d+),\\d+$").match(productType); |
| 150 | + if (match.hasMatch()) |
| 151 | + { |
| 152 | + const int hardwareSeries = match.captured(1).toInt(); |
| 153 | + int estimatedSeries = hardwareSeries; |
| 154 | + if (hardwareSeries >= 11) |
| 155 | + estimatedSeries = hardwareSeries - 1; |
| 156 | + else if (hardwareSeries >= 8) |
| 157 | + estimatedSeries = hardwareSeries - 2; |
| 158 | + |
| 159 | + return QString("Estimated iPhone %1 series").arg(estimatedSeries); |
| 160 | + } |
| 161 | + return "Unknown model"; |
| 162 | +} |
| 163 | + |
| 164 | +void AppleDB::ResolvePending() |
| 165 | +{ |
| 166 | + const auto pendingMap = m_pendingCallbacks; |
| 167 | + m_pendingCallbacks.clear(); |
| 168 | + |
| 169 | + for (auto it = pendingMap.constBegin(); it != pendingMap.constEnd(); ++it) |
| 170 | + { |
| 171 | + const QString productType = it.key(); |
| 172 | + const QString display = BuildDisplayName(productType); |
| 173 | + const QString resolved = display == productType ? EstimateSeries(productType) : display; |
| 174 | + for (const auto& callback : it.value()) |
| 175 | + callback(resolved); |
| 176 | + } |
| 177 | +} |
0 commit comments