summaryrefslogtreecommitdiffstats
path: root/src/laymanapi.c
blob: 4509511226a9a4475a2d1181e38a0d77ad715dce (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <Python.h>
#include "interpreter.h"
#include "laymanapi.h"

/*
 * FIXME: free memory !!!
 */

struct LaymanAPI
{
	PyObject *object;
};

OverlayInfo strToInfo(const char* str)
{
	OverlayInfo ret;
	return ret;
}

LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output)
{
	PyObject *obj = executeFunction("layman.api", "LaymanAPI", "Oii", _bareConfigObject(config), report_error, output);
	if (!obj)
		return NULL;

	LaymanAPI *ret = malloc(sizeof(LaymanAPI));
	ret->object = obj;

	return ret;
}

StringList* laymanAPIGetAvailable(LaymanAPI* l, int reload)
{
	if (!l || !l->object)
		return NULL;

	PyObject *obj = PyObject_CallMethod(l->object, "get_available", "(i)", reload);
	StringList *ret = listToCList(obj);
	Py_DECREF(obj);

	return ret;
}

/*StringList* laymanAPIGetInstalled(LaymanAPI* l)
{
	return laymanAPIGetInstalled(l, 0);
}*/

StringList* laymanAPIGetInstalled(LaymanAPI* l, int reload)
{
	if (!l || !l->object)
		return NULL;

	PyObject *obj = PyObject_CallMethod(l->object, "get_installed", "(i)", reload);
	StringList* ret = listToCList(obj);
	Py_DECREF(obj);

	return ret;
}

int laymanAPISync(LaymanAPI* l, const char* overlay, int verbose)
{
	if (!l || !l->object)
		return 0;

	PyObject *obj = PyObject_CallMethod(l->object, "sync", "(si)", overlay, verbose);
	if (!obj)
		return 0;

	int ret;
	if (PyObject_IsTrue(obj))
		ret = 1;
	else
		ret = 0;
	
	Py_DECREF(obj);
	
	return ret;
}

int laymanAPIFetchRemoteList(LaymanAPI* l)
{
	if (!l || !l->object)
		return 0;
	
	PyObject *obj = PyObject_CallMethod(l->object, "fetch_remote_list", NULL);

	int ret;

	if (!PyObject_IsTrue(obj)) //FIXME : does this work ? It seems to return 1 when false and 0 when true
		ret = 1;
	else
		ret = 0;
	
	Py_DECREF(obj);

	return ret;
}

OverlayInfo *laymanAPIGetInfo(LaymanAPI* l, const char* overlay)
{
	if (!l || !l->object || !overlay)
		return NULL;

	PyObject *list = PyList_New(1);
	PyList_SetItem(list, 0, PyBytes_FromString(overlay));

	PyObject *obj = PyObject_CallMethod(l->object, "get_info", "(O)", list);
	Py_DECREF(list);

	if (!obj || !PyDict_Check(obj))
	{
		if (obj)
		{
			Py_DECREF(obj);
		}
		return NULL;
	}

	PyObject *tuple = PyDict_GetItemString(obj, overlay);

	if (!tuple || !PyTuple_Check(tuple))
	{
		if (tuple)
		{
			Py_DECREF(tuple);
		}
		Py_DECREF(obj);

		return NULL;
	}

	PyObject *text = PyTuple_GetItem(tuple, 0);
	PyObject *official = PyTuple_GetItem(tuple, 1);
	PyObject *supported = PyTuple_GetItem(tuple, 2);

	OverlayInfo *oi = malloc(sizeof(OverlayInfo));

	char* tmp = PyBytes_AsString(text);
	oi->text = malloc((strlen(tmp) + 1) * sizeof(char));
	strcpy(oi->text, tmp);

	oi->official = PyObject_IsTrue(official);
	oi->supported = PyObject_IsTrue(supported);

	Py_DECREF(obj);
	Py_DECREF(tuple);
	Py_DECREF(text);
	Py_DECREF(official);
	Py_DECREF(supported);

	return oi;
}

void laymanAPIFree(LaymanAPI* l)
{
	if (l && l->object)
	{
		Py_DECREF(l->object);
	}

	if (l)
		free(l);
}