summaryrefslogtreecommitdiffstats
path: root/objects/base-objects.c
blob: 3601593234d4f8d53d948da9701fb7dc198e79fa (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <Python.h>
#include "structmember.h"
#include "pymodule.h"
#include "base-objects.h"
#include "pyirssi.h"

/* This is the base type for most, if not all, Irssi objects with a type
   id. The user can find the type name, type id, and check if the object is
   wrapping a valid Irssi record. */

/* member IDs */
enum
{
    M_BASE_TYPE,
    M_BASE_NAME,
    M_BASE_VALID,
};

static void PyIrssiBase_dealloc(PyIrssiBase *self)
{
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *
PyIrssiBase_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyIrssiBase *self;

    self = (PyIrssiBase *)type->tp_alloc(type, 0);
    if (!self)
        return NULL;

    return (PyObject *)self;
}

static PyObject *PyIrssiBase_get(PyIrssiBase *self, void *closure)
{
    int member = GPOINTER_TO_INT(closure);

    /* If the user passed the valid member, don't trigger an exception */
    if (member != M_BASE_VALID)
        RET_NULL_IF_INVALID(self->data);

    switch (member)
    {
        case M_BASE_TYPE:
            return PyInt_FromLong(self->data->type);
        case M_BASE_NAME:
            RET_AS_STRING_OR_NONE(self->base_name);
        case M_BASE_VALID:
            if (self->data != NULL)
                Py_RETURN_TRUE;
            else
                Py_RETURN_FALSE;
    }

    INVALID_MEMBER(member);
}

/* specialized getters/setters */
static PyGetSetDef PyIrssiBase_getseters[] = {
    {"type_id", (getter)PyIrssiBase_get, NULL, 
        "Irssi's type id for object", 
        GINT_TO_POINTER(M_BASE_TYPE)},

    {"type", (getter)PyIrssiBase_get, NULL, 
        "Irssi's name for object", 
        GINT_TO_POINTER(M_BASE_NAME)},

    {"valid", (getter)PyIrssiBase_get, NULL, 
        "True if the object is valid", 
        GINT_TO_POINTER(M_BASE_VALID)},
    {NULL}
};

/* Methods for object */
static PyMethodDef PyIrssiBase_methods[] = {
    /* {"somemeth", (PyCFunction)PyIrssiBase_name, METH_NOARGS, "docstr"}, */
    {NULL}  /* Sentinel */
};

PyTypeObject PyIrssiBaseType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "IrssiBase",            /*tp_name*/
    sizeof(PyIrssiBase),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyIrssiBase_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "PyIrssiBase objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyIrssiBase_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyIrssiBase_getseters,        /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    0,      /* tp_init */
    0,                         /* tp_alloc */
    PyIrssiBase_new,                 /* tp_new */
};


/* IrssiChatBase is a base type for any object with a chat type. The user
   can find the chat type string name with the chat_type member or
   the type id with the chat_type_id member. It inherits from IrssiBase
   so the type, valid, and type_id members are visible to the user, too */

/* member IDs */
enum
{
    M_CHAT_CHAT_TYPE,
    M_CHAT_CHAT_NAME,
};

static void
PyIrssiChatBase_dealloc(PyIrssiChatBase *self)
{
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *
PyIrssiChatBase_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyIrssiChatBase *self;

    self = (PyIrssiChatBase *)type->tp_alloc(type, 0);
    if (!self)
        return NULL;

    return (PyObject *)self;
}

static PyObject *PyIrssiChatBase_get(PyIrssiChatBase *self, void *closure)
{
    int member = GPOINTER_TO_INT(closure);

    RET_NULL_IF_INVALID(self->data);

    switch (member)
    {
        case M_CHAT_CHAT_TYPE:
            return PyInt_FromLong(self->data->chat_type);
        case M_CHAT_CHAT_NAME:
        {
            CHAT_PROTOCOL_REC *rec = chat_protocol_find_id(self->data->chat_type);
            if (rec)
                RET_AS_STRING_OR_NONE(rec->name);
            else
                Py_RETURN_NONE;
        }
    }

    INVALID_MEMBER(member);
}

//specialized getters/setters
static PyGetSetDef PyIrssiChatBase_getseters[] = {
    {"chat_type_id", (getter)PyIrssiChatBase_get, NULL, 
        "Chat Type id", 
        GINT_TO_POINTER(M_CHAT_CHAT_TYPE)},

    {"chat_type", (getter)PyIrssiChatBase_get, NULL, 
        "Chat Name", 
        GINT_TO_POINTER(M_CHAT_CHAT_NAME)},
    {NULL}
};

static PyMethodDef PyIrssiChatBase_methods[] = {
    {NULL}  /* Sentinel */
};

PyTypeObject PyIrssiChatBaseType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "IrssiChatBase",            /*tp_name*/
    sizeof(PyIrssiChatBase),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyIrssiChatBase_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "PyIrssiChatBase objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyIrssiChatBase_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyIrssiChatBase_getseters,        /* tp_getset */
    &PyIrssiBaseType,         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    0,      /* tp_init */
    0,                         /* tp_alloc */
    PyIrssiChatBase_new,                 /* tp_new */
};

int base_objects_init(void) 
{
    g_return_val_if_fail(py_module != NULL, 0);

    if (PyType_Ready(&PyIrssiBaseType) < 0)
        return 0;
    if (PyType_Ready(&PyIrssiChatBaseType) < 0)
        return 0;

    Py_INCREF(&PyIrssiBaseType);
    Py_INCREF(&PyIrssiChatBaseType);
    PyModule_AddObject(py_module, "IrssiBase", (PyObject *)&PyIrssiBaseType);
    PyModule_AddObject(py_module, "IrssiChatBase", (PyObject *)&PyIrssiChatBaseType);

    return 1;
}