summaryrefslogtreecommitdiff
path: root/interchange.cxx
blob: 21a1fb538154b33fc4433b3e7d4b774c5bc8b318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "interchange.hxx"
#include "interchange/ord.hxx"

namespace QInterchange {

	static Interchange* interchange;
	static char *mv_sku = nullptr, *mv_order_item = nullptr;
	static int sampleUrlLength = 0;

	Interchange::Interchange(const char* sampleURL, const char* image_Dir,
			const QString& cookie, const QString& certificate)
	{
		interchange = this;
#ifndef __EMSCRIPTEN__
		auto length = strlen(sampleURL);
		sampleUrlLength = length + (sampleURL[length - 1] != '/');
#endif
		interchange_init(sampleURL, image_Dir,
				cookie.isEmpty() ? nullptr
				: cookie.toLatin1().constData(),
				certificate.isEmpty() ? nullptr
				: certificate.toLatin1().constData());
	}

	Interchange::~Interchange()
	{
		interchange_cleanup();
	}

	void Interchange::page(QString const& path)
	{
		interchange_page(path.toLatin1().constData(),
				[](interchange_response* response) {
			interchange->emitPage(QString{response->url}
					.remove(0, sampleUrlLength),
					QString{response->data});
			interchange_free_response(response);
		});
	}

	void Interchange::catalog(QString const& prodGroup)
	{
		interchange_catalog(prodGroup.toLatin1().constData(), [](interchange_response* response) {
			interchange->emitCatalog(QString{response->data});
			interchange_free_response(response);
		}, nullptr);
	}

	void Interchange::allProducts()
	{
		catalog("All-Products");
	}

	void Interchange::product(QString const& sku)
	{
		interchange_product(sku.toLatin1().constData(), [](interchange_response* response) {
			interchange->emitProduct(QString{response->data});
			interchange_free_response(response);
		}, nullptr);
	}

	void Interchange::defaultCatalog(QString const& prodGroup)
	{
		interchange_catalog(prodGroup.toLatin1().constData(), nullptr, [](struct interchange_catalog* catalog) {
			interchange_free_catalog(catalog);
		});
	}

	void Interchange::defaultAllProducts()
	{
		defaultCatalog("All-Products");
	}

	void Interchange::order(const QString &sku, const QString &item,
			const int quantity)
	{
		mv_sku = (char *)malloc(sku.size() + 1);
		strcpy(mv_sku, sku.toLatin1().constData());
		if (item.isEmpty()) {
			mv_order_item = (char *)malloc(sku.size() + 1);
			strcpy(mv_order_item, sku.toLatin1().constData());
		} else {
			mv_order_item = (char *)malloc(item.size() + 1);
			strcpy(mv_order_item, item.toLatin1().constData());
		}
		interchange_ord_order(mv_sku, mv_order_item, quantity,
				[](interchange_response *response) {
			free(mv_sku);
			mv_sku = nullptr;
			free(mv_order_item);
			mv_order_item = nullptr;
			interchange->emitOrder(QString{response->data});
			interchange_free_response(response);
		});
	}

	void Interchange::emitPage(QString const& path, QString const& response)
	{
		emit gotPage(path, response);
	}

	void Interchange::emitCatalog(QString const& response)
	{
		emit gotCatalog(response);
	}

	void Interchange::emitProduct(QString const& response)
	{
		emit gotProduct(response);
	}

	void Interchange::emitOrder(const QString &response)
	{
		emit gotOrder(response);
	}
}