Esempio n. 1
0
void Updater::update()
{
    if( QMLSettingsSingleton::instance()->url().isValid() )
    {
        requestNext();

        qDebug() << "Starting update ...";
    }
}
Esempio n. 2
0
void Updater::headersReceived()
{
    QNetworkReply * reply = qobject_cast<QNetworkReply *>( sender() );

    Q_ASSERT( 0 != reply );

    qDebug() << " Headers received " << reply->url() << "\nStatus: " << reply->error();

    if( GettingHeaders == m_state )
    {
        if( QNetworkReply::NoError == reply->error() )
        {
            QDateTime lastModified = reply->header( QNetworkRequest::LastModifiedHeader ).toDateTime();

            if( AdvertisementsList::instance()->at( m_currentAdIndex ).lastModified() == lastModified )
            {
                // ad didn't changed. Go to next one if any
                requestNext();
            }
            else
            {
                m_pkg = new Package( m_currentAdIndex );
                m_pkg->setLastModified( lastModified );

                requestFile();
            }
        }
        else
        {
            qDebug() << "Error occured during headers request" << reply->errorString();
            // try to get next one
            requestNext();
        }
    }
    else
    {
        // this should not happen
        Q_ASSERT( false );
    }

    // schedule delete of replay. We don't need it anymore
    reply->deleteLater();
}
/**
 * Update the prices of the cards in deckList.
 */
void DBPriceUpdater::updatePrices()
{
    QString base = "https://api.deckbrew.com/mtg/cards", q = "";
    QStringList cards = deck->getCardList();
    muidMap.clear();
    urls.clear();
    CardInfo * card;
    int muid;
    SetList sets;
    bool bNotFirst=false;

    for (int i = 0; i < cards.size(); ++i) {
        card = db->getCard(cards[i], false);
        sets = card->getSets();
        for(int j = 0; j < sets.size(); ++j)
        {
            muid=card->getMuId(sets[j]->getShortName());

            if (!muid) {
                continue;
            }

            //qDebug() << "muid " << muid << " card: " << cards[i] << endl;
            if(bNotFirst)
            {
                q += QString("&m=%1").arg(muid);
            } else {
                q += QString("?m=%1").arg(muid);
                bNotFirst = true;
            }
            muidMap.insert(muid, cards[i]);

            if(q.length() > 240)
            {
                urls.append(base + q);
                bNotFirst=false;
                q = "";
            }
        }
    }
    if(q.length() > 0)
        urls.append(base + q);

    requestNext();
}
Esempio n. 4
0
void Updater::packageUnpacked(bool ok)
{
    qDebug() << "Package unpacked " << ok;

    // disconnect package signal. It is not needed anymore
    disconnect( m_pkg, &Package::packageReady, this, &Updater::packageUnpacked );

    if( false != ok )
    {
        // update model
        AdvertisementsList::instance()->replace( m_currentAdIndex, m_pkg );
    }
    else
    {
        m_pkg->deleteLater();
    }

    requestNext();
}
Esempio n. 5
0
void Updater::requestFile()
{
    qDebug() << "Reguesting file " << m_currentAdUrl;

    QNetworkRequest request( m_currentAdUrl );
    QNetworkReply *reply = gNetworkAccessManager->get( request );

    if( m_pkg->openTmpFile() )
    {
        connect( reply, &QIODevice::readyRead, m_pkg, &Package::newDataAvailable );
        connect( reply, &QNetworkReply::finished, this, &Updater::downloadFinished );

        m_state = Downloading;
    }
    else
    {
        delete m_pkg;
        m_pkg = nullptr;

        requestNext();
    }
}
Esempio n. 6
0
void Updater::downloadFinished()
{
    QNetworkReply * reply = qobject_cast<QNetworkReply *>( sender() );

    Q_ASSERT( 0 == reply );

    qDebug() << " File downloaded " << reply->url() << "\nStatus: " << reply->error();

    if( Downloading == m_state )
    {
        if( QNetworkReply::NoError == reply->error() )
        {
            connect( m_pkg, &Package::packageReady, this, &Updater::packageUnpacked );

            m_pkg->unpack();

            m_state = Unpacking;
        }
        else
        {
            delete m_pkg;
            m_pkg = nullptr;
            // skip to next one
            qDebug() << "File downloaded with error " << reply->errorString();
            requestNext();
        }
    }
    else
    {
        delete m_pkg;
        m_pkg = nullptr;
        // this should not happened
        Q_ASSERT(false);
    }

    // schedule delete of replay. We don't need it anymore
    reply->deleteLater();
}
/**
 * Called when the download of the json file with the prices is finished.
 */
void DBPriceUpdater::downloadFinished()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    bool ok;
    QString tmp = QString(reply->readAll());

    // Errors are incapsulated in an object, check for them first
    QVariantMap resultMap = QtJson::Json::parse(tmp, ok).toMap();
    if (!ok) {
        QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
        reply->deleteLater();
        if(urls.isEmpty())
        {
            deleteLater();
            emit finishedUpdate();
        } else {
            requestNext();
        }
    }

    if(resultMap.contains("errors"))
    {
        QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices:") + 
            "<br/>" +
#if QT_VERSION < 0x050000
            Qt::escape(resultMap["errors"].toList().first().toString())
#else
            resultMap["errors"].toList().first().toString().toHtmlEscaped()
#endif
        );
        reply->deleteLater();
        if(urls.isEmpty())
        {
            deleteLater();
            emit finishedUpdate();
        } else {
            requestNext();
        }
    }

    // Good results are a list
    QVariantList resultList = QtJson::Json::parse(tmp, ok).toList();
    if (!ok) {
        QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
        reply->deleteLater();
        if(urls.isEmpty())
        {
            deleteLater();
            emit finishedUpdate();
        } else {
            requestNext();
        }
    }

    QMap<QString, float> cardsPrice;
    QListIterator<QVariant> it(resultList);
    while (it.hasNext()) {
        QVariantMap cardMap = it.next().toMap();


        // get sets list
        QList<QVariant> editions = cardMap.value("editions").toList();
        foreach (QVariant ed, editions)
        {
            // retrieve card name "as we know it" from the muid
            QVariantMap edition = ed.toMap();
            QString set = edition.value("set_id").toString();

            int muid = edition.value("multiverse_id").toString().toInt();
            if(!muidMap.contains(muid))
                continue;

            QString name=muidMap.value(muid);
            // Prices are in USD cents
            float price = edition.value("price").toMap().value("median").toString().toFloat() / 100;
            //qDebug() << "card " << name << " set " << set << " price " << price << endl;

            /**
            * Make sure Masters Edition (MED) isn't the set, as it doesn't
            * physically exist. Also check the price to see that the cheapest set
            * ends up as the final price.
            */
            if (set != "MED" && price > 0 && (!cardsPrice.contains(name) || cardsPrice.value(name) > price))
                cardsPrice.insert(name, price);
        }
    }
Esempio n. 8
0
void ShowInfoTab::next()
{
    emit requestNext(_current->id(), _current->channel());
}