summaryrefslogtreecommitdiffstats
path: root/objects/theme-object.c
blob: 0f122176079cd820b1706d3584aaea5df2f841de (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
#include <Python.h>
#include "pyirssi.h"
#include "pymodule.h"
#include "theme-object.h"
#include "factory.h"
#include "pycore.h"

/* monitor "theme destroyed" signal */
static void theme_cleanup(THEME_REC *rec)
{
    PyTheme *pytheme = signal_get_user_data();
    if (pytheme->data == rec)
    {
        pytheme->data = NULL;
        pytheme->cleanup_installed = 0;
        signal_remove_data("theme destroyed", theme_cleanup, pytheme);
    }
}

static void PyTheme_dealloc(PyTheme *self)
{
    if (self->cleanup_installed)
        signal_remove_data("theme destroyed", theme_cleanup, self);
    
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *PyTheme_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyTheme *self;

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

    return (PyObject *)self;
}

/* Getters */
/* specialized getters/setters */
static PyGetSetDef PyTheme_getseters[] = {
    {NULL}
};

/* Methods */
PyDoc_STRVAR(PyTheme_format_expand_doc,
    "format_expand(format, flags=0) -> str or None\n"
);
static PyObject *PyTheme_format_expand(PyTheme *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"format", "flags", NULL};
    char *format = "";
    int flags = 0;
    char *ret;
    PyObject *pyret;

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, 
           &format, &flags))
        return NULL;

    if (flags == 0)
        ret = theme_format_expand(self->data, format);
    else
        ret = theme_format_expand_data(self->data, (const char **)&format, 'n', 'n',
                NULL, NULL, EXPAND_FLAG_ROOT | flags);

    if (ret)
    {
        pyret = PyString_FromString(ret);
        g_free(ret);
        return pyret;
    }

    Py_RETURN_NONE;
}

PyDoc_STRVAR(PyTheme_get_format_doc,
    "get_format(module, tag) -> str\n"
);
static PyObject *PyTheme_get_format(PyTheme *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"module", "tag", NULL};
    char *module = "";
    char *tag = "";
    THEME_REC *theme = self->data;
    FORMAT_REC *formats;
    MODULE_THEME_REC *modtheme; 
    int i;

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss", kwlist, 
           &module, &tag))
        return NULL;

    formats = g_hash_table_lookup(default_formats, module);
    if (!formats)
        return PyErr_Format(PyExc_KeyError, "unknown module, %s", module);

    for (i = 0; formats[i].def; i++)
    {
        if (formats[i].tag && !g_strcasecmp(formats[i].tag, tag))
        { 
            modtheme = g_hash_table_lookup(theme->modules, module);
            if (modtheme && modtheme->formats[i])
                return PyString_FromString(modtheme->formats[i]);
            else 
                return PyString_FromString(formats[i].def);
        }
    }
   
    return PyErr_Format(PyExc_KeyError, "unknown format tag, %s", tag);    
}

/* Methods for object */
static PyMethodDef PyTheme_methods[] = {
    {"format_expand", (PyCFunction)PyTheme_format_expand, METH_VARARGS | METH_KEYWORDS,
        PyTheme_format_expand_doc},
    {"get_format", (PyCFunction)PyTheme_get_format, METH_VARARGS | METH_KEYWORDS,
        PyTheme_get_format_doc},
    {NULL}  /* Sentinel */
};

PyTypeObject PyThemeType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "Theme",            /*tp_name*/
    sizeof(PyTheme),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyTheme_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*/
    0,           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyTheme_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyTheme_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 */
    PyTheme_new,                 /* tp_new */
};

/* Theme factory function */
PyObject *pytheme_new(void *td)
{
    PyTheme *pytheme;

    pytheme = py_inst(PyTheme, PyThemeType);
    if (!pytheme)
        return NULL;

    pytheme->data = td;
    signal_add_last_data("theme destroyed", theme_cleanup, pytheme);
    pytheme->cleanup_installed = 1;

    return (PyObject *)pytheme;
}

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

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

    return 1;
}