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

/*XXX: no Reconnect cleanup/destroy sig. Maybe value copy the two members? */

static void PyReconnect_dealloc(PyReconnect *self)
{
    Py_XDECREF(self->connect);
    self->ob_type->tp_free((PyObject*)self);
}

static PyObject *PyReconnect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyReconnect *self;

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

    return (PyObject *)self;
}

/* Getters */
PyDoc_STRVAR(PyReconnect_tag_doc,
    "Unique numeric tag"
);
static PyObject *PyReconnect_tag_get(PyReconnect *self, void *closure)
{
    RECONNECT_REC *data = self->data;
    RET_NULL_IF_INVALID(self->data);

    return PyInt_FromLong(data->tag);
}

PyDoc_STRVAR(PyReconnect_next_connect_doc,
    "Unix time stamp when the next connection occurs"
);
static PyObject *PyReconnect_next_connect_get(PyReconnect *self, void *closure)
{
    RECONNECT_REC *data = self->data;
    RET_NULL_IF_INVALID(self->data);

    return PyLong_FromUnsignedLong(data->next_connect);
}

PyDoc_STRVAR(PyReconnect_connect_doc,
    "Connection object"
);
static PyObject *PyReconnect_connect_get(PyReconnect *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_OBJ_OR_NONE(self->connect);
}

/* specialized getters/setters */
static PyGetSetDef PyReconnect_getseters[] = {
    {"tag", (getter)PyReconnect_tag_get, NULL,
        PyReconnect_tag_doc, NULL},
    {"next_connect", (getter)PyReconnect_next_connect_get, NULL,
        PyReconnect_next_connect_doc, NULL},
    {"connect", (getter)PyReconnect_connect_get, NULL,
        PyReconnect_connect_doc, NULL},
    {NULL}
};

/* Methods for object */
static PyMethodDef PyReconnect_methods[] = {
    {NULL}  /* Sentinel */
};

PyTypeObject PyReconnectType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "Reconnect",            /*tp_name*/
    sizeof(PyReconnect),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyReconnect_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*/
    "PyReconnect objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyReconnect_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyReconnect_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 */
    PyReconnect_new,                 /* tp_new */
};


/* window item wrapper factory function */
PyObject *pyreconnect_new(void *recon)
{
    RECONNECT_REC *rec = recon;
    PyObject *connect;
    PyReconnect *pyrecon;

    /* XXX: get a managed connect because there's no signals to manage reconnect */
    connect = py_irssi_chat_new(rec->conn, 1);
    if (!connect)
        return NULL;

    pyrecon = py_inst(PyReconnect, PyReconnectType);
    if (!pyrecon)
        return NULL;

    pyrecon->data = recon;
    pyrecon->connect = connect;

    return (PyObject *)pyrecon;
}

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

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

    return 1;
}