summaryrefslogtreecommitdiffstats
path: root/src/pystatusbar.c
blob: 8537718cdab8e96630ae90611df182ab74cd0652 (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
#include "pystatusbar.h"
#include "pyirssi.h"
#include "factory.h"

typedef struct
{
    char *name;
    PyObject *script;
    PyObject *handler;
} PY_BAR_ITEM_REC;

/* Map: item name -> bar item obj */
static GHashTable *py_bar_items = NULL;

static void py_add_bar_handler(const char *iname, PyObject *script, PyObject *handler)
{
    PY_BAR_ITEM_REC *sitem;

    sitem = g_new0(PY_BAR_ITEM_REC, 1);
    sitem->name = g_strdup(iname);
    sitem->script = script;
    sitem->handler = handler;
    Py_INCREF(script);
    Py_INCREF(handler);

    g_hash_table_insert(py_bar_items, sitem->name, sitem);
}

static void py_destroy_handler(PY_BAR_ITEM_REC *sitem)
{
    statusbar_item_unregister(sitem->name);

    g_free(sitem->name); /* destroy key */
    Py_DECREF(sitem->script);
    Py_DECREF(sitem->handler);
    g_free(sitem);
}

static void py_statusbar_proxy_call(SBAR_ITEM_REC *item, int sizeonly, PY_BAR_ITEM_REC *sitem)
{
    PyObject *pybaritem;
    PyObject *ret;

    g_return_if_fail(PyCallable_Check(sitem->handler));

    pybaritem = pystatusbar_item_new(item);
    if (!pybaritem)
    {
        PyErr_Print();
        pystatusbar_item_unregister(sitem->name);
    }

    ret = PyObject_CallFunction(sitem->handler, "Oi", pybaritem, sizeonly);
    if (!ret)
    {
        PyErr_Print();
        pystatusbar_item_unregister(sitem->name);
    }
    else
        Py_DECREF(ret);
}

static void py_statusbar_proxy(SBAR_ITEM_REC *item, int sizeonly)
{
    PY_BAR_ITEM_REC *sitem;    

    sitem = g_hash_table_lookup(py_bar_items, item->config->name);
    if (sitem)
        py_statusbar_proxy_call(item, sizeonly, sitem);
    else
    {
        statusbar_item_default_handler(item, sizeonly, NULL, "", TRUE);
        g_critical("unknown handler for Python statusbar proxy: %s", item->config->name);
    }
}

void pystatusbar_item_register(PyObject *script, const char *sitem, 
        const char *value, PyObject *func)
{
    if (func)
    {
        g_return_if_fail(PyCallable_Check(func));
        py_add_bar_handler(sitem, script, func);
    }

    statusbar_item_register(sitem, value, func? py_statusbar_proxy : NULL);
}

/* remove selected status bar item handler */
void pystatusbar_item_unregister(const char *iname)
{
    if (!g_hash_table_remove(py_bar_items, iname))
        statusbar_item_unregister(iname);
}

/* remove all statusbar item handlers for script */
/* XXX: Only status bar items registered with a handler are stored in the hash table.
 * Items registered with only a value are not stored, so there is no way to unregister 
 * them when the script is unloaded.
 */
static int py_check_clean(char *key, PY_BAR_ITEM_REC *value, PyObject *script)
{
    if (value->script == script)
        return 1;

    return 0;
}

void pystatusbar_cleanup_script(PyObject *script)
{
    g_hash_table_foreach_remove(py_bar_items, (GHRFunc)py_check_clean, script);
}

void pystatusbar_init(void)
{
    g_return_if_fail(py_bar_items == NULL);

    /* key is freed by destroy_handler */
    py_bar_items = g_hash_table_new_full(g_str_hash, g_str_equal,
            NULL, (GDestroyNotify)py_destroy_handler);
}

/* XXX: this must be called after cleaning up all the loaded scripts */
void pystatusbar_deinit(void)
{
    g_return_if_fail(py_bar_items != NULL);
    g_return_if_fail(g_hash_table_size(py_bar_items) == 0);

    g_hash_table_destroy(py_bar_items);
    py_bar_items = NULL;
}