diff --git a/cockatrice/resources/icon_update.png b/cockatrice/resources/icon_update.png new file mode 100644 index 00000000..825c0aee Binary files /dev/null and b/cockatrice/resources/icon_update.png differ diff --git a/cockatrice/src/priceupdater.cpp b/cockatrice/src/priceupdater.cpp new file mode 100644 index 00000000..e9f1a695 --- /dev/null +++ b/cockatrice/src/priceupdater.cpp @@ -0,0 +1,81 @@ +/** + * @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(); +} diff --git a/cockatrice/src/priceupdater.h b/cockatrice/src/priceupdater.h new file mode 100644 index 00000000..a95252eb --- /dev/null +++ b/cockatrice/src/priceupdater.h @@ -0,0 +1,28 @@ +#ifndef PRICEUPDATER_H +#define PRICEUPDATER_H + +#include +#include "decklist.h" + +class QNetworkAccessManager; + +/** + * Price Updater. + * + * @author Marcio Ribeiro + */ +class PriceUpdater : public QObject +{ + Q_OBJECT +private: + const DeckList *deck; + QNetworkAccessManager *nam; +signals: + void finishedUpdate(); +private slots: + void downloadFinished(); +public: + PriceUpdater(const DeckList *deck); + void updatePrices(); +}; +#endif