void ItemsManagerWorker::OnFirstTabReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); int index = 0; if (!doc.IsObject()) { QLOG_ERROR() << "Can't even fetch first tab. Failed to update items."; updating_ = false; return; } if (!doc.HasMember("tabs") || doc["tabs"].Size() == 0) { QLOG_WARN() << "There are no tabs, this should not happen, bailing out."; updating_ = false; return; } tabs_as_string_ = Util::RapidjsonSerialize(doc["tabs"]); QLOG_DEBUG() << "Received tabs list, there are" << doc["tabs"].Size() << "tabs"; tabs_.clear(); for (auto &tab : doc["tabs"]) { std::string label = tab["n"].GetString(); tabs_.push_back(label); if (index > 0) { ItemLocation location; location.set_type(ItemLocationType::STASH); location.set_tab_id(index); location.set_tab_label(label); if (!tab.HasMember("hidden") || !tab["hidden"].GetBool()) QueueRequest(MakeTabRequest(index), location); } ++index; } ItemLocation first_tab_location; first_tab_location.set_type(ItemLocationType::STASH); first_tab_location.set_tab_id(0); first_tab_location.set_tab_label(tabs_[0]); if (!doc["tabs"][0].HasMember("hidden") || !doc["tabs"][0]["hidden"].GetBool()) ParseItems(&doc["items"], first_tab_location, doc.GetAllocator()); total_needed_ = queue_.size() + 1; total_completed_ = 1; FetchItems(kThrottleRequests - 1); connect(signal_mapper_, SIGNAL(mapped(int)), this, SLOT(OnTabReceived(int))); reply->deleteLater(); }
void ItemsManagerWorker::OnFirstTabReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); if (!doc.IsObject()) { QLOG_ERROR() << "Can't even fetch first tab. Failed to update items."; updating_ = false; return; } if (!doc.HasMember("tabs") || doc["tabs"].Size() == 0) { QLOG_WARN() << "There are no tabs, this should not happen, bailing out."; updating_ = false; return; } tabs_as_string_ = Util::RapidjsonSerialize(doc["tabs"]); QLOG_DEBUG() << "Received tabs list, there are" << doc["tabs"].Size() << "tabs"; tabs_.clear(); // Create tab location objects for (auto &tab : doc["tabs"]) { std::string label = tab["n"].GetString(); auto index = tab["i"].GetInt(); // Ignore hidden locations if (!doc["tabs"][index].HasMember("hidden") || !doc["tabs"][index]["hidden"].GetBool()) tabs_.push_back(ItemLocation(index, label, ItemLocationType::STASH)); } // Immediately parse items received from this tab (first_fetch_tab_) and Queue requests for the others for (auto const &tab: tabs_) { auto index = tab.get_tab_id(); if (index == first_fetch_tab_) { ParseItems(&doc["items"], tab, doc.GetAllocator()); } else { QueueRequest(MakeTabRequest(index, tab), tab); } } total_needed_ = queue_.size() + 1; total_completed_ = 1; total_cached_ = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool() ? 1:0; FetchItems(kThrottleRequests - 1); connect(signal_mapper_, SIGNAL(mapped(int)), this, SLOT(OnTabReceived(int))); reply->deleteLater(); }
void ItemsManagerWorker::OnCharacterListReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); if (doc.HasParseError() || !doc.IsArray()) { QLOG_ERROR() << "Received invalid reply instead of character list. The reply was" << bytes.constData(); if (doc.HasParseError()) { QLOG_ERROR() << "The error was" << rapidjson::GetParseError_En(doc.GetParseError()); } updating_ = false; return; } QLOG_DEBUG() << "Received character list, there are" << doc.Size() << "characters"; for (auto &character : doc) { if (!character.HasMember("league") || !character.HasMember("name") || !character["league"].IsString() || !character["name"].IsString()) { QLOG_ERROR() << "Malformed character entry, the reply is most likely invalid" << bytes.constData(); continue; } if (character["league"].GetString() == league_) { std::string name = character["name"].GetString(); ItemLocation location; location.set_type(ItemLocationType::CHARACTER); location.set_character(name); QueueRequest(MakeCharacterRequest(name, location), location); } } // Fetch a single tab and also request tabs list. We can fetch any tab here with tabs list // appended, so prefer one that the user has already 'checked'. Default to index '1' which is // first user visible tab. first_fetch_tab_ = 1; for (auto const &tab : tabs_) { if (bo_manager_.GetRefreshChecked(tab)) { first_fetch_tab_ = tab.get_tab_id(); break; } } QNetworkReply *first_tab = network_manager_.get(MakeTabRequest(first_fetch_tab_, ItemLocation(), true)); connect(first_tab, SIGNAL(finished()), this, SLOT(OnFirstTabReceived())); reply->deleteLater(); }
void ItemsManagerWorker::OnCharacterListReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); if (doc.HasParseError() || !doc.IsArray()) { QLOG_ERROR() << "Received invalid reply instead of character list. The reply was" << bytes.constData(); if (doc.HasParseError()) { QLOG_ERROR() << "The error was" << rapidjson::GetParseError_En(doc.GetParseError()); } updating_ = false; StatusFinished(); return; } QLOG_INFO() << "Received character list, there are" << doc.Size() << "characters"; for (auto &character : doc) { if (!character.HasMember("league") || !character.HasMember("name") || !character["league"].IsString() || !character["name"].IsString()) { QLOG_ERROR() << "Malformed character entry, the reply is most likely invalid" << bytes.constData(); continue; } if (character["league"].GetString() == league_) { std::string name = character["name"].GetString(); ItemLocation location; location.set_type(ItemLocationType::CHARACTER); location.set_character(name); QueueRequest(MakeCharacterRequest(name), location); } } // now get first tab and tab list QNetworkReply *first_tab = network_manager_.get(MakeTabRequest(0, true)); connect(first_tab, SIGNAL(finished()), this, SLOT(OnFirstTabReceived())); reply->deleteLater(); }
void ItemsManagerWorker::OnFirstTabReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); int index = 0; if (!doc.IsObject()) { QLOG_ERROR() << "Can't even fetch first tab. Failed to update items."; updating_ = false; StatusFinished(); return; } if (!doc.HasMember("tabs") || doc["tabs"].Size() == 0) { QLOG_WARN() << "There are no tabs, this should not happen, bailing out."; updating_ = false; StatusFinished(); return; } tabs_as_string_ = Util::RapidjsonSerialize(doc["tabs"]); QLOG_INFO() << "Received tabs list, there are" << doc["tabs"].Size() << "tabs"; // Setup tab exclusions std::string exclusions = data_manager_.Get("tab_exclusions"); QList<QRegularExpression> expressions; rapidjson::Document exclusionDoc; exclusionDoc.Parse(exclusions.c_str()); if (exclusionDoc.IsArray()) { for (auto &excl : exclusionDoc) { QRegularExpression expr(excl.GetString()); if (!expr.pattern().isEmpty() && expr.isValid()) expressions.append(expr); } } int tabsParsed = 0; tabs_.clear(); for (auto &tab : doc["tabs"]) { std::string label = tab["n"].GetString(); bool skip = false; for (QRegularExpression expr : expressions) { QRegularExpressionMatch match = expr.match(QString::fromStdString(label)); if (match.isValid() && match.hasMatch()) { skip = true; break; } } if (!skip) { tabs_.push_back(label); ItemLocation location; location.set_type(ItemLocationType::STASH); location.set_tab_id(index); location.set_tab_label(label); if (index == 0) { // We already have this tab (it's the first one we get). // Parse the items now that we have them if (!doc["tabs"][0].HasMember("hidden") || !doc["tabs"][0]["hidden"].GetBool()) { ParseItems(&doc["items"], location, doc.GetAllocator()); ++tabsParsed; } } else if (!tab.HasMember("hidden") || !tab["hidden"].GetBool()) { // If it's not hidden, then we need to download QueueRequest(MakeTabRequest(index), location); } } ++index; } if (tabs_.size() == 0) { QLOG_WARN() << "There are no tabs to be downloaded. Try clearing your tab exclusions list."; updating_ = false; StatusFinished(); return; } // tabsParsed will be 1 if the first tab was included in the initial download total_needed_ = queue_.size() + tabsParsed; total_completed_ = tabsParsed; FetchItems(kThrottleRequests - tabsParsed); connect(signal_mapper_, SIGNAL(mapped(int)), this, SLOT(OnTabReceived(int))); reply->deleteLater(); }
void ItemsManagerWorker::OnFirstTabReceived() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender()); QByteArray bytes = reply->readAll(); rapidjson::Document doc; doc.Parse(bytes.constData()); int index = 0; if (!doc.IsObject()) { QLOG_ERROR() << "Can't even fetch first tab. Failed to update items."; updating_ = false; return; } if (!doc.HasMember("tabs") || doc["tabs"].Size() == 0) { QLOG_WARN() << "There are no tabs, this should not happen, bailing out."; updating_ = false; return; } tabs_as_string_ = Util::RapidjsonSerialize(doc["tabs"]); QLOG_INFO() << "Received tabs list, there are" << doc["tabs"].Size() << "tabs"; // Setup tab exclusions std::string exclusions = data_manager_.Get("tab_exclusions"); QList<QRegularExpression> expressions; rapidjson::Document exclusionDoc; exclusionDoc.Parse(exclusions.c_str()); if (exclusionDoc.IsArray()) { for (auto &excl : exclusionDoc) { expressions.append(QRegularExpression(excl.GetString())); } } tabs_.clear(); for (auto &tab : doc["tabs"]) { std::string label = tab["n"].GetString(); bool skip = false; for (QRegularExpression expr : expressions) { QRegularExpressionMatch match = expr.match(QString::fromStdString(label)); if (match.isValid() && match.hasMatch()) { skip = true; break; } } tabs_.push_back(label); if (index > 0 || skip) { ItemLocation location; location.set_type(ItemLocationType::STASH); location.set_tab_id(index); location.set_tab_label(label); if (!tab.HasMember("hidden") || !tab["hidden"].GetBool()) QueueRequest(MakeTabRequest(skip ? 0 : index), location); } ++index; } ItemLocation first_tab_location; first_tab_location.set_type(ItemLocationType::STASH); first_tab_location.set_tab_id(0); first_tab_location.set_tab_label(tabs_[0]); if (!doc["tabs"][0].HasMember("hidden") || !doc["tabs"][0]["hidden"].GetBool()) ParseItems(&doc["items"], first_tab_location, doc.GetAllocator()); total_needed_ = queue_.size() + 1; total_completed_ = 1; FetchItems(kThrottleRequests - 1); connect(signal_mapper_, SIGNAL(mapped(int)), this, SLOT(OnTabReceived(int))); reply->deleteLater(); }