summaryrefslogtreecommitdiffstats
path: root/objects/factory.c
blob: 25ac6112a6bfe53b21e23059c6a3370452322a7d (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <Python.h>
#include "pyirssi.h"
#include "factory.h"

/* Irssi object factory works for all items with at least a type member.
 *
 * Use py_irssi_new() or py_irssi_chat_new() to get a new wrapper for an
 * IrssiObject or an IrssiChatObject, respectively.
 *
 * For objects not descending from IrssiObject or IrssiChatObject, you must
 * use the object-specific init function directly.
 */

#define MAKEKEY(type, chat) ((chat << 16 ) | type)
#define GETTYPE(key) (key & 0xffff)
#define GETCHAT(key) ((key >> 16) & 0xffff)


GHashTable *init_map = NULL;

static int init_objects(void);
static void register_chat(CHAT_PROTOCOL_REC *rec);
static void unregister_chat(CHAT_PROTOCOL_REC *rec);
static void insert_map(int type, int chat_type, InitFunc func);
static int remove_chat(void *key, void *value, void *chat_typep);
static void register_nonchat(void);
static InitFunc find_map(int type, int chat_type);

static int init_objects(void)
{
    if (!pyscript_init())
        return 0;

    /* order is somewhat important here */
    if (!base_objects_init())
        return 0;

    if (!window_item_object_init())
        return 0;

    if (!channel_object_init())
        return 0;

    if (!query_object_init())
        return 0;
    
    if (!server_object_init())
        return 0;

    if (!connect_object_init())
        return 0;

    if (!irc_server_object_init())
        return 0;

    if (!irc_connect_object_init())
        return 0;
   
    if (!irc_channel_object_init())
        return 0;

    if (!ban_object_init())
        return 0;

    if (!nick_object_init())
        return 0;

    if (!chatnet_object_init())
        return 0;

    if (!reconnect_object_init())
        return 0;

    if (!window_object_init())
        return 0;

    if (!textdest_object_init())
        return 0;

    if (!rawlog_object_init())
        return 0;

    if (!log_object_init())
        return 0;

    if (!logitem_object_init())
        return 0;

    if (!ignore_object_init())
        return 0;

    if (!dcc_object_init())
        return 0;

    if (!dcc_chat_object_init())
        return 0;
    
    if (!dcc_get_object_init())
        return 0;

    if (!dcc_send_object_init())
        return 0;
    
    if (!netsplit_object_init())
        return 0;

    if (!netsplit_server_object_init())
        return 0;

    if (!netsplit_channel_object_init())
        return 0;

    if (!notifylist_object_init())
        return 0;

    if (!process_object_init())
        return 0;

    if (!command_object_init())
        return 0;

    if (!theme_object_init())
        return 0;

    if (!statusbar_item_object_init())
        return 0;

    return 1;
}

static InitFunc find_map(int type, int chat_type)
{
    unsigned hash;

	g_return_val_if_fail(type <= 0xffff, NULL);
	g_return_val_if_fail(chat_type <= 0xffff, NULL);

    hash = MAKEKEY(type, chat_type);
    return g_hash_table_lookup(init_map, GUINT_TO_POINTER(hash));
}

static void insert_map(int type, int chat_type, InitFunc func)
{
    unsigned hash;

	g_return_if_fail(type <= 0xffff);
	g_return_if_fail(chat_type <= 0xffff);

    hash = MAKEKEY(type, chat_type);
    g_hash_table_insert(init_map, GUINT_TO_POINTER(hash), func);
}

static void register_chat(CHAT_PROTOCOL_REC *rec)
{
    int type;
    int chat_type;	
    int is_irc = 0;

    /* chat_type == rec->id ??? */
    chat_type = chat_protocol_lookup(rec->name);
	g_return_if_fail(chat_type >= 0 && chat_type < 0xffff);

    if (!g_strcasecmp(rec->name, "IRC"))
        is_irc = 1;
    
	type = module_get_uniq_id("SERVER", 0);
    if (is_irc)
        insert_map(type, chat_type, (InitFunc)pyirc_server_new);
    else
        insert_map(type, chat_type, (InitFunc)pyserver_new);

	type = module_get_uniq_id("SERVER CONNECT", 0);
    if (is_irc)
        insert_map(type, chat_type, (InitFunc)pyirc_connect_new);
    else
        insert_map(type, chat_type, (InitFunc)pyconnect_new);

	type = module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL");
    if (is_irc)
        insert_map(type, chat_type, (InitFunc)pyirc_channel_new); 
    else
        insert_map(type, chat_type, (InitFunc)pychannel_new);

	type = module_get_uniq_id_str("WINDOW ITEM TYPE", "QUERY");
    insert_map(type, chat_type, (InitFunc)pyquery_new);

	type = module_get_uniq_id("CHATNET", 0);
    insert_map(type, chat_type, (InitFunc)pychatnet_new); 

	type = module_get_uniq_id("NICK", 0);
    insert_map(type, chat_type, (InitFunc)pynick_new); 
}

/* register funcs for objects without a chat type */
static void register_nonchat(void)
{
    int type;
    int chat_type = 0xffff;

    type = module_get_uniq_id_str("DCC", "CHAT");
    insert_map(type, chat_type, (InitFunc)pydcc_chat_new);

    type = module_get_uniq_id_str("DCC", "GET");
    insert_map(type, chat_type, (InitFunc)pydcc_get_new);

    type = module_get_uniq_id_str("DCC", "SEND");
    insert_map(type, chat_type, (InitFunc)pydcc_send_new);

    type = module_get_uniq_id_str("DCC", "SERVER");
    insert_map(type, chat_type, (InitFunc)pydcc_new);
}

static int remove_chat(void *key, void *value, void *chat_typep)
{
    unsigned hash = GPOINTER_TO_UINT(key);
    int chat_type = GPOINTER_TO_INT(chat_type);

    if (GETCHAT(hash) == chat_type)
        return TRUE;

    return FALSE;
}

/* remove all items matching chat_type */
static void unregister_chat(CHAT_PROTOCOL_REC *rec)
{
    /*int chat_type = chat_protocol_lookup(rec->name);*/
    g_hash_table_foreach_remove(init_map, 
            (GHRFunc)remove_chat,
            GINT_TO_POINTER(rec->id));
}

PyObject *py_irssi_new(void *typeobj, int managed)
{
    IRSSI_BASE_REC *base = typeobj;
    InitFunc ifunc;
   
    if (!base)
        Py_RETURN_NONE;
    
    ifunc = find_map(base->type, 0xffff);

    if (ifunc)
        return ifunc(typeobj, managed);

    return PyErr_Format(PyExc_RuntimeError, "no initfunc for object type %d", base->type);
}

PyObject *py_irssi_chat_new(void *typeobj, int managed)
{
    IRSSI_CHAT_REC *chat = typeobj;
    InitFunc ifunc;
        
    if (!chat)
        Py_RETURN_NONE;
    
    ifunc = find_map(chat->type, chat->chat_type);

    if (ifunc)
        return ifunc(typeobj, managed);

    return PyErr_Format(PyExc_RuntimeError, "no initfunc for object type %d, chat_type %d", 
            chat->type, chat->chat_type);
}

PyObject *py_irssi_objlist_new(GSList *node, int managed, InitFunc init)
{
    PyObject *list = NULL;
  
    list = PyList_New(0);
    if (!list)
        goto error;
    
    for (; node != NULL; node = node->next)
    {
        int ret;
        PyObject *obj = init(node->data, managed);

        if (!obj)
            goto error;

        ret = PyList_Append(list, obj);
        Py_DECREF(obj);

        if (ret != 0)
            goto error;
    }

    return list;

error:
    Py_XDECREF(list);
    return NULL;
}

int factory_init(void)
{
    g_return_val_if_fail(init_map == NULL, 0);

    if (!init_objects())
        return 0;

    init_map = g_hash_table_new(g_direct_hash, g_direct_equal);
 	g_slist_foreach(chat_protocols, (GFunc) register_chat, NULL);
    register_nonchat();

	signal_add("chat protocol created", (SIGNAL_FUNC) register_chat);
	signal_add("chat protocol destroyed", (SIGNAL_FUNC) unregister_chat);
   
    return 1;
}

void factory_deinit(void)
{
    g_return_if_fail(init_map != NULL);

    g_hash_table_destroy(init_map);
    init_map = NULL;

	signal_remove("chat protocol created", (SIGNAL_FUNC) register_chat);
	signal_remove("chat protocol destroyed", (SIGNAL_FUNC) unregister_chat);
}