Unity 8
Payments.cpp
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "Payments.h"
18 
19 #include <QLocale>
20 
21 #include <libpay/pay-package.h>
22 
23 
24 // The observer callback for the package we are watching.
25 static void observer(PayPackage* /* package */, const char* itemid, PayPackageItemStatus status, void* user_data) {
26  // This function is called in libpay's thread, so be careful what you call
27  // Emitting signals should be fine as long as they use Queued or Auto
28  // connections (the default)
29  // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
30 
31  Payments *self = static_cast<Payments*>(user_data);
32 
33  // If the item ID is different, ignore it.
34  if (itemid != self->storeItemId()) {
35  return;
36  }
37 
38  // FIXME: No error reporting from libpay, but we need to show some
39  // types of errors to the user. https://launchpad.net/bugs/1333403
40  switch (status) {
41  case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
42  Q_EMIT self->purchaseCompleted();
43  break;
44  case PAY_PACKAGE_ITEM_STATUS_PURCHASING:
45  self->setPurchasing(true);
46  break;
47  case PAY_PACKAGE_ITEM_STATUS_NOT_PURCHASED:
48  if (self->purchasing()) {
49  self->setPurchasing(false);
50  Q_EMIT self->purchaseCancelled();
51  }
52  break;
53  default:
54  break;
55  }
56 }
57 
58 Payments::Payments(QObject *parent)
59  : QObject(parent),
60  m_purchasing(false)
61 {
62  m_package = pay_package_new("click-scope");
63  pay_package_item_observer_install(m_package, observer, this);
64 }
65 
66 Payments::~Payments()
67 {
68  pay_package_item_observer_uninstall(m_package, observer, this);
69  pay_package_delete(m_package);
70 }
71 
72 QString Payments::currency() const
73 {
74  return m_currency;
75 }
76 
77 double Payments::price() const
78 {
79  return m_price;
80 }
81 
82 QString Payments::storeItemId() const
83 {
84  return m_store_item_id;
85 }
86 
87 QString Payments::formattedPrice() const
88 {
89  QLocale locale;
90  return locale.toCurrencyString(m_price, m_currency);
91 }
92 
93 bool Payments::purchasing() const
94 {
95  return m_purchasing;
96 }
97 
98 void Payments::setCurrency(const QString &currency)
99 {
100  if(m_currency != currency) {
101  m_currency = currency;
102  Q_EMIT currencyChanged(currency);
103  Q_EMIT formattedPriceChanged(formattedPrice());
104  }
105 }
106 
107 void Payments::setPrice(const double price)
108 {
109  if(m_price != price) {
110  m_price = price;
111  Q_EMIT priceChanged(price);
112  Q_EMIT formattedPriceChanged(formattedPrice());
113  }
114 }
115 
116 void Payments::setStoreItemId(const QString &store_item_id)
117 {
118  if (m_store_item_id != store_item_id) {
119  m_store_item_id = store_item_id;
120  Q_EMIT storeItemIdChanged(m_store_item_id);
121  }
122 
123  if (m_store_item_id.isEmpty()) {
124  return;
125  }
126 }
127 
128 void Payments::setPurchasing(bool is_purchasing)
129 {
130  m_purchasing = is_purchasing;
131 }
132 
133 void Payments::start()
134 {
135  pay_package_item_start_purchase(m_package, m_store_item_id.toLocal8Bit().data());
136 }