summaryrefslogtreecommitdiffstats
path: root/objects/notifylist-object.c
blob: 890c2564096b1b600322681b4a663f46b9eaebef (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
#include <Python.h>
#include "pyirssi_irc.h"
#include "pymodule.h"
#include "notifylist-object.h"
#include "pycore.h"

#define NOTIFYLIST(nl) ((NOTIFYLIST_REC *)nl)

/* monitor "notifylist remove" signal */
static void notifylist_cleanup(NOTIFYLIST_REC *notifylist)
{
    PyNotifylist *pynotifylist = signal_get_user_data();

    if (notifylist == pynotifylist->data)
    {
        pynotifylist->data = NULL;
        pynotifylist->cleanup_installed = 0;
        signal_remove_data("notifylist remove", notifylist_cleanup, pynotifylist);
    }
}

static void PyNotifylist_dealloc(PyNotifylist *self)
{
    if (self->cleanup_installed)
        signal_remove_data("notifylist remove", notifylist_cleanup, self); 

    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *PyNotifylist_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyNotifylist *self;

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

    return (PyObject *)self;
}

/* Getters */
PyDoc_STRVAR(PyNotifylist_mask_doc,
    "Notify nick mask"
);
static PyObject *PyNotifylist_mask_get(PyNotifylist *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(NOTIFYLIST(self->data)->mask);
}

PyDoc_STRVAR(PyNotifylist_away_check_doc,
    "Notify away status changes"
);
static PyObject *PyNotifylist_away_check_get(PyNotifylist *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyBool_FromLong(NOTIFYLIST(self->data)->away_check);
}

PyDoc_STRVAR(PyNotifylist_idle_check_time_doc,
    "Notify when idle time is reset and idle was bigger than this (seconds)"
);
static PyObject *PyNotifylist_idle_check_time_get(PyNotifylist *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyLong_FromUnsignedLong(NOTIFYLIST(self->data)->idle_check_time);
}

/* specialized getters/setters */
static PyGetSetDef PyNotifylist_getseters[] = {
    {"mask", (getter)PyNotifylist_mask_get, NULL,
        PyNotifylist_mask_doc, NULL},
    {"away_check", (getter)PyNotifylist_away_check_get, NULL,
        PyNotifylist_away_check_doc, NULL},
    {"idle_check_time", (getter)PyNotifylist_idle_check_time_get, NULL,
        PyNotifylist_idle_check_time_doc, NULL},
    {NULL}
};

/* Methods */
PyDoc_STRVAR(PyNotifylist_ircnets_doc,
    "Return list of ircnets the notify is checked"
);
static PyObject *PyNotifylist_ircnets(PyNotifylist *self, PyObject *args)
{
    PyObject *list;
    char **nets;

    RET_NULL_IF_INVALID(self->data);

    nets = NOTIFYLIST(self->data)->ircnets;
    list = PyList_New(0);
    if (!list)
        return NULL;

    while (nets && *nets)
    {
        int ret;
        PyObject *str = PyString_FromString(*nets);

        if (!str)
        {
            Py_DECREF(list);
            return NULL;
        }

        ret = PyList_Append(list, str);
        Py_DECREF(str);
        if (ret != 0)
        {
            Py_DECREF(list);
            return NULL;
        }
        
        nets++;
    }

    return list;
}

PyDoc_STRVAR(PyNotifylist_ircnets_match_doc,
    "Return True if notify is checked in ircnet"
);
static PyObject *PyNotifylist_ircnets_match(PyNotifylist *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"ircnet", NULL};
    char *ircnet = "";

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, 
           &ircnet))
        return NULL;

    return PyBool_FromLong(notifylist_ircnets_match(self->data, ircnet));
}

/* Methods for object */
static PyMethodDef PyNotifylist_methods[] = {
    {"ircnets", (PyCFunction)PyNotifylist_ircnets, METH_NOARGS,
        PyNotifylist_ircnets_doc},
    {"ircnets_match", (PyCFunction)PyNotifylist_ircnets_match, METH_VARARGS | METH_KEYWORDS,
        PyNotifylist_ircnets_match_doc},
    {NULL}  /* Sentinel */
};

PyTypeObject PyNotifylistType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "Notifylist",            /*tp_name*/
    sizeof(PyNotifylist),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyNotifylist_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*/
    "PyNotifylist objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyNotifylist_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyNotifylist_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 */
    PyNotifylist_new,                 /* tp_new */
};


/* window item wrapper factory function */
PyObject *pynotifylist_new(void *notifylist)
{
    PyNotifylist *pynotifylist;

    pynotifylist = py_inst(PyNotifylist, PyNotifylistType);
    if (!pynotifylist)
        return NULL;

    pynotifylist->data = notifylist;
    pynotifylist->cleanup_installed = 1;
    signal_add_last_data("notifylist remove", notifylist_cleanup, pynotifylist);

    return (PyObject *)pynotifylist;
}

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

    if (PyType_Ready(&PyNotifylistType) < 0)
        return 0;
    
    Py_INCREF(&PyNotifylistType);
    PyModule_AddObject(py_module, "Notifylist", (PyObject *)&PyNotifylistType);

    return 1;
}