| 275 | } |
| 276 | |
| 277 | void AdBlockManager::loadSubscriptions() |
| 278 | { |
| 279 | if (!m_enabled) |
| 280 | return; |
| 281 | |
| 282 | QFile configFile(m_configFile); |
| 283 | if (!configFile.exists() || !configFile.open(QIODevice::ReadOnly)) |
| 284 | return; |
| 285 | |
| 286 | // Attempt to parse config/subscription info file |
| 287 | QByteArray configData = configFile.readAll(); |
| 288 | configFile.close(); |
| 289 | |
| 290 | // Add subscriptions to container |
| 291 | QJsonDocument configDoc(QJsonDocument::fromJson(configData)); |
| 292 | QJsonObject configObj = configDoc.object(); |
| 293 | QJsonObject subscriptionObj; |
| 294 | for (auto it = configObj.begin(); it != configObj.end(); ++it) |
| 295 | { |
| 296 | const QString key = it.key(); |
| 297 | if (key.compare(QLatin1String("requests_blocked")) == 0) |
| 298 | { |
| 299 | continue; |
| 300 | } |
| 301 | AdBlockSubscription subscription(key); |
| 302 | |
| 303 | subscriptionObj = it.value().toObject(); |
| 304 | subscription.setEnabled(subscriptionObj.value(QLatin1String("enabled")).toBool()); |
| 305 | |
| 306 | // Get last update as unix epoch value |
| 307 | bool ok; |
| 308 | uint lastUpdateUInt = subscriptionObj.value(QLatin1String("last_update")).toVariant().toUInt(&ok); |
| 309 | QDateTime lastUpdate = (ok && lastUpdateUInt > 0 ? QDateTime::fromTime_t(lastUpdateUInt) : QDateTime::currentDateTime()); |
| 310 | subscription.setLastUpdate(lastUpdate); |
| 311 | |
| 312 | // Attempt to get next update time as unix epoch value |
| 313 | uint nextUpdateUInt = subscriptionObj.value(QLatin1String("next_update")).toVariant().toUInt(&ok); |
| 314 | if (ok && nextUpdateUInt > 0) |
| 315 | { |
| 316 | QDateTime nextUpdate = QDateTime::fromTime_t(nextUpdateUInt); |
| 317 | subscription.setNextUpdate(nextUpdate); |
| 318 | } |
| 319 | |
| 320 | // Get source URL of the subscription |
| 321 | QString source = subscriptionObj.value(QLatin1String("source")).toString(); |
| 322 | if (!source.isEmpty()) |
| 323 | subscription.setSourceUrl(QUrl(source)); |
| 324 | |
| 325 | m_subscriptions.push_back(std::move(subscription)); |
| 326 | } |
| 327 | |
| 328 | extractFilters(); |
| 329 | } |
| 330 | |
| 331 | void AdBlockManager::clearFilters() |
| 332 | { |
nothing calls this directly
no test coverage detected