summaryrefslogtreecommitdiffstats
path: root/src/objects/query-object.c
blob: fc0c285b5f5ab0aa4f6ae354f11d863999fc2712 (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
/* 
    irssi-python

    Copyright (C) 2006 Christopher Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <Python.h>
#include "pyirssi.h"
#include "pymodule.h"
#include "base-objects.h"
#include "window-item-object.h"
#include "query-object.h"
#include "server-object.h"
#include "pycore.h"

/* monitor "query destroyed" signal */
static void query_cleanup(QUERY_REC *query)
{
    PyQuery *pyquery = signal_get_user_data();

    if (query == pyquery->data)
    {
        pyquery->data = NULL;
        pyquery->cleanup_installed = 0;
        signal_remove_data("query destroyed", query_cleanup, pyquery);
    }
}

static void PyQuery_dealloc(PyQuery *self)
{
    if (self->cleanup_installed)
        signal_remove_data("query destroyed", query_cleanup, self);

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

/* Getters */
PyDoc_STRVAR(PyQuery_address_doc,
    "Host address of the queries nick"
);
static PyObject *PyQuery_address_get(PyQuery *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->address);
}

PyDoc_STRVAR(PyQuery_server_tag_doc,
    "Server tag used for this nick (doesn't get erased if server gets disconnected)"
);
static PyObject *PyQuery_server_tag_get(PyQuery *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->server_tag);
}

PyDoc_STRVAR(PyQuery_unwanted_doc,
    "1 if the other side closed or some error occured (DCC chats)"
);
static PyObject *PyQuery_unwanted_get(PyQuery *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    return PyBool_FromLong(self->data->unwanted);
}

/* specialized getters/setters */
static PyGetSetDef PyQuery_getseters[] = {
    {"address", (getter)PyQuery_address_get, NULL,
        PyQuery_address_doc, NULL},
    {"server_tag", (getter)PyQuery_server_tag_get, NULL,
        PyQuery_server_tag_doc, NULL},
    {"unwanted", (getter)PyQuery_unwanted_get, NULL,
        PyQuery_unwanted_doc, NULL},
    {NULL}
};

PyDoc_STRVAR(change_server_doc,
    "change_server(server) -> None\n"
    "\n"
    "Change the active server for the query.\n"
);
static PyObject *PyQuery_change_server(PyQuery *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"server", NULL};
    PyObject *server;

    RET_NULL_IF_INVALID(self->data);
    
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &server))
        return NULL;

    if (!pyserver_check(server))
        return PyErr_Format(PyExc_TypeError, "argument must be server object");
   
    query_change_server(self->data, ((PyServer*)server)->data);
    
    Py_RETURN_NONE;
}

/* Methods for object */
static PyMethodDef PyQuery_methods[] = {
    {"change_server", (PyCFunction)PyQuery_change_server, METH_VARARGS | METH_KEYWORDS, 
        change_server_doc},

    {NULL}  /* Sentinel */
};

PyTypeObject PyQueryType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "irssi.Query",            /*tp_name*/
    sizeof(PyQuery),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyQuery_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*/
    "PyQuery objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyQuery_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyQuery_getseters,        /* tp_getset */
    &PyWindowItemType,          /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    0,      /* tp_init */
    0,                         /* tp_alloc */
    0,                 /* tp_new */
};


/* query factory function */
PyObject *pyquery_new(void *query)
{
    static const char *BASE_NAME = "QUERY";
    PyObject *pyquery;

    pyquery = pywindow_item_sub_new(query, BASE_NAME, &PyQueryType);
    if (pyquery)
    {
        PyQuery *pyq = (PyQuery *)pyquery;
        signal_add_last_data("query destroyed", query_cleanup, pyq);
        pyq->cleanup_installed = 1;
    }

    return pyquery;
}

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

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

    return 1;
}