/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#include "itemstatusimageprovider.h"

#include <QDebug>

#include "declarativepurchaselistmodel.h"
#include "productcatalogmodel.h"
#include "purchasableitembase.h"

ItemstatusImageProvider::ItemstatusImageProvider(ProductCatalogModel &model):
    QDeclarativeImageProvider(QDeclarativeImageProvider::Image),
    m_model(&model)
{
}

QImage ItemstatusImageProvider::requestImage(const QString &url,
                                             QSize *size,
                                             const QSize &requestedSize)
{
    bool ok = false;
    //NOTE: see workaround for http://bugreports.qt.nokia.com/browse/QTBUG-14900 in 'main.qml'
    int ignorelength = url.indexOf("ignoringpart");
    QString id((ignorelength > 0) ? url.left(ignorelength) : url);
    int index = id.toInt(&ok);

    Q_ASSERT(ok); //design time sanity check

    //NOTE: return value is created in the stack and uses copy constructor
    QImage image = m_model->createIcon(index);
    
    if (size){
        *size = QSize(image.width(), image.height());
    }

    return image;
}
