/**
 * 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.
 */

#ifndef RESOURCEDATAPROVIDER_H
#define RESOURCEDATAPROVIDER_H

#include <QDeclarativeImageProvider>
#include <QImage>

class PurchasableItemBase;
class DeclarativePurchaseListModel;

/*!
  \class ResourceDataProvider
  \brief Product data provider. Product data consists of image, sound URLs z-order and
         visibility flags to locked and unlocked product resources
*/
class ResourceDataProvider : public QObject, public QDeclarativeImageProvider
{
    Q_OBJECT
public:
    /*!
      Constructor
    */
    ResourceDataProvider(const DeclarativePurchaseListModel &model);

    /*!
      Returns image in requested size depending on resource id
    */
    QImage requestImage ( const QString &id, QSize *size, const QSize &requestedSize );

    /*!
      Returns an URL depending on resource id
    */
    QString requestResourceUrl(const QString& id);

    /*!
      Interface to QML side to check if product resource indicated by id shall be visible
    */
    Q_INVOKABLE bool isProductVisible(const QString &id);

    /*!
      Interface to QML side to get z-order of product resource indicated by id
    */
    Q_INVOKABLE int productZorder(const QString &id);

private:
    //utility functions

    /*!
      Decrypts DRM content to image
    */
    void decryptDRMcontentToImage(QString &pathDRM, QImage &destIcon);

    /*!
      Decrypts DRM content to file
    */
    void decryptDRMcontentToFile(QString &pathDRM, const QString &destination);

    /*!
      Converts product resource id to index in 'productList' array
    */
    int itemToIndex(const QString &id);

private:
    //data
    struct ProductDefinition
    {
        const char          *key;           //product resource id
        const char          *lockedURL;     //product locked resource URL
        const char          *unlockedURL;   //product locked resource URL, if it equals to NULL
                                            // then when unlocked, resource is hidden in assembled
                                            // image
        const char          *productID;     // OVI Store product id
        const int           zOrder;         //z-order appearance in assembled image
        PurchasableItemBase *data;          //pointer to data in memory that represents the product
                                            // if product data pointer equals to NULL then
                                            // the product cannot be unlocked -- i.e. not for sale
    };

    static ProductDefinition                productList[];  // product definition list
    static const int                        productListSize;// product definition list size
    const DeclarativePurchaseListModel      *m_model;       // pointer to QML list model
};

#endif // RESOURCEDATAPROVIDER_H
