/** * @author Marcio Ribeiro * @version 1.0 */ #include #include #include #include #include "priceupdater.h" /** * Constructor. * * @param _deck deck. */ PriceUpdater::PriceUpdater(const DeckList *_deck) { nam = new QNetworkAccessManager(this); deck = _deck; } /** * Update the prices of the cards in deckList. */ void PriceUpdater::updatePrices() { QString q = "http://blacklotusproject.com/json/?cards="; QStringList cards = deck->getCardList(); for (int i = 0; i < cards.size(); ++i) { q += cards[i] + "|"; } QUrl url(q.replace(' ', '+')); QNetworkReply *reply = nam->get(QNetworkRequest(url)); connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished())); } /** * Called when the download of the json file with the prices is finished. */ void PriceUpdater::downloadFinished() { QMap cmap; InnerDecklistNode *listRoot = deck->getRoot(); for (int i = 0; i < listRoot->size(); i++) { InnerDecklistNode *currentZone = dynamic_cast(listRoot->at(i)); for (int j = 0; j < currentZone->size(); j++) { DecklistCardNode *currentCard = dynamic_cast(currentZone->at(j)); if (!currentCard) continue; cmap.insert(currentCard->getName().toLower(), currentCard); currentCard->setPrice(0); } } QNetworkReply *reply = static_cast(sender()); QByteArray result = reply->readAll(); QScriptValue sc; QScriptEngine engine; sc = engine.evaluate("value = " + result); if (sc.property("cards").isArray()) { QScriptValueIterator it(sc.property("cards")); while (it.hasNext()) { it.next(); QString name = it.value().property("name").toString().toLower(); float price = it.value().property("average").toString().toFloat(); DecklistCardNode *c = cmap[name]; if (!c) continue; if (c->getPrice() == 0 || c->getPrice() > price) { c->setPrice(price); } } } reply->deleteLater(); deleteLater(); emit finishedUpdate(); }