aboutsummaryrefslogtreecommitdiff
blob: 328a5726eea0a741c9d19934a97fd9126620f547 (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
#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)
{
	if (!l || !l->object)
		return NULL;

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

	return ret;
}

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

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

	return ret;
}

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

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

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

	if (!obj)
		return EXIT_FAILURE;

	PyObject *success = PyList_GetItem(obj, 1);
	if (success == Py_None)
		return EXIT_FAILURE;
	
	return EXIT_SUCCESS;
}

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

	int ret;

	if (PyObject_IsTrue(obj))
		ret = EXIT_SUCCESS;
	else
		ret = EXIT_FAILURE;
	
	Py_DECREF(obj);

	return ret;
}

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

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

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

	if (!obj)
		return NULL;

	PyObject *result = PyList_GetItem(obj, 0);
	char* tmp = PyBytes_AsString(result);
	char* ret = malloc((strlen(tmp) + 1) * sizeof(char));
	strcpy(ret, tmp);
	Py_DECREF(result);

	return ret;
	//TODO:also return 'official' an 'supported' (see laymanapi.h)
}

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

	if (l)
		free(l);
}