1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Purchasing module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3-COMM$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "qinappstoreqmltype_p.h"
30#include <QtPurchasing/qinappstore.h>
31
32QT_BEGIN_NAMESPACE
33
34/*!
35 \qmltype Store
36 \inqmlmodule QtPurchasing
37 \since QtPurchasing 1.0
38 \ingroup qtpurchasing
39 \brief Access point to the external market place for in-app purchases.
40
41 When using the Qt Purchasing API in QML, the application should instantiate
42 one Store and then instantiate products as children of this store. The products
43 created as children of the Store object will automatically be queried from the
44 external market place if one is available on the current platform.
45
46 The following example registers a store with three products, two consumable
47 products and one unlockable.
48 \qml
49 Store {
50 Product {
51 identifier: "myConsumableProduct1"
52 type: Product.Consumable
53
54 // ...
55 }
56
57 Product {
58 identifier: "myConsumableProduct2"
59 type: Product.Consumable
60
61 // ...
62 }
63
64 Product {
65 identifier: "myUnlockableProduct"
66 type: Product.Unlockable
67
68 // ...
69 }
70
71 // ...
72 }
73 \endqml
74 */
75
76static void addProduct(QQmlListProperty<QInAppProductQmlType> *property, QInAppProductQmlType *product)
77{
78 QInAppStoreQmlType *store = qobject_cast<QInAppStoreQmlType *>(object: property->object);
79 Q_ASSERT(store != 0);
80 product->setStore(store);
81
82 QList<QInAppProductQmlType *> *products = reinterpret_cast<QList<QInAppProductQmlType *> *>(property->data);
83 Q_ASSERT(products != 0);
84
85 products->append(t: product);
86}
87
88static int productCount(QQmlListProperty<QInAppProductQmlType> *property)
89{
90 QList<QInAppProductQmlType *> *products = reinterpret_cast<QList<QInAppProductQmlType *> *>(property->data);
91 Q_ASSERT(products != 0);
92
93 return products->size();
94}
95
96static void clearProducts(QQmlListProperty<QInAppProductQmlType> *property)
97{
98 QList<QInAppProductQmlType *> *products = reinterpret_cast<QList<QInAppProductQmlType *> *>(property->data);
99 Q_ASSERT(products != 0);
100
101 for (int i=0; i<products->size(); ++i) {
102 QInAppProductQmlType *product = products->at(i);
103 product->setStore(0);
104 }
105 products->clear();
106}
107
108static QInAppProductQmlType *productAt(QQmlListProperty<QInAppProductQmlType> *property, int index)
109{
110 QList<QInAppProductQmlType *> *m_products = reinterpret_cast<QList<QInAppProductQmlType *> *>(property->data);
111 Q_ASSERT(m_products != 0);
112
113 return m_products->at(i: index);
114}
115
116QInAppStoreQmlType::QInAppStoreQmlType(QObject *parent)
117 : QObject(parent)
118 , m_store(new QInAppStore(this))
119{
120}
121
122QInAppStore *QInAppStoreQmlType::store() const
123{
124 return m_store;
125}
126
127QQmlListProperty<QInAppProductQmlType> QInAppStoreQmlType::products()
128{
129 return QQmlListProperty<QInAppProductQmlType>(this, &m_products, &addProduct, &productCount, &productAt, &clearProducts);
130}
131
132/*!
133 \qmlmethod QtPurchasing::Store::restorePurchases()
134
135 Calling this method will cause onPurchaseRestored handlers to be called
136 asynchronously on previously purchased unlockable products. This can be
137 used to restore purchases for unlockable products when the application is
138 run by the same user on multiple devices, or for example if the application
139 has been uninstalled and reinstalled on the device so that the purchase data
140 has been lost.
141
142 \note On some platforms, such as iOS, this will require the user to input their
143 password to launch the restore process. On other platforms, such as Android,
144 it is not typically needed, as the onPurchaseSucceeded handler will be called
145 on any previously purchased unlockable product if the application data is
146 removed.
147 */
148void QInAppStoreQmlType::restorePurchases()
149{
150 m_store->restorePurchases();
151}
152
153QT_END_NAMESPACE
154

source code of qtpurchasing/src/imports/purchasing/qinappstoreqmltype.cpp