From 34cdeaae9369af5dce3ce3a4a7756c033bc44cc5 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Mon, 3 Jul 2006 06:00:18 +0000 Subject: worked on formats. working on themes now; testing adding functions and methods to objects git-svn-id: http://svn.irssi.org/repos/irssi-python@4295 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- objects/textdest-object.c | 129 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 16 deletions(-) (limited to 'objects/textdest-object.c') diff --git a/objects/textdest-object.c b/objects/textdest-object.c index 9dd94d6..2ad6523 100644 --- a/objects/textdest-object.c +++ b/objects/textdest-object.c @@ -5,13 +5,20 @@ #include "factory.h" #include "pycore.h" -/* XXX: no cleanup signal for textdest */ +static int pytextdest_setup(PyTextDest *pytdest, void *td, int owned); +/* XXX: no cleanup signal for textdest */ static void PyTextDest_dealloc(PyTextDest *self) { Py_XDECREF(self->window); Py_XDECREF(self->server); - + + if (self->owned) + { + g_free((char*)self->data->target); + g_free(self->data); + } + self->ob_type->tp_free((PyObject*)self); } @@ -26,6 +33,57 @@ static PyObject *PyTextDest_new(PyTypeObject *type, PyObject *args, PyObject *kw return (PyObject *)self; } +/* init function to create the textdest */ +PyDoc_STRVAR(PyTextDest_doc, + "__init__(target, level=MSGLEVEL_CLIENTNOTICE, server=None, window=None)\n" + "\n" + "Create a TextDest\n" +); +static int PyTextDest_init(PyTextDest *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"target", "level", "server", "window", NULL}; + char *target; + int level = MSGLEVEL_CLIENTNOTICE; + PyObject *server = NULL, *window = NULL; + TEXT_DEST_REC *dest; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ioo", kwlist, + &target, &level, &server, &window)) + return -1; + + if (server == Py_None) + server = NULL; + if (window == Py_None) + window = NULL; + + if (server && !pyserver_check(server)) + { + PyErr_Format(PyExc_TypeError, "arg 3 isnt server"); + return -1; + } + + if (window && !pywindow_check(window)) + { + PyErr_Format(PyExc_TypeError, "arg 4 isnt window"); + return -1; + } + + if (self->data) + { + PyErr_Format(PyExc_RuntimeError, "TextDest already wrapped"); + return -1; + } + + dest = g_new0(TEXT_DEST_REC, 1); + format_create_dest(dest, DATA(server), g_strdup(target), level, DATA(window)); + + if (!pytextdest_setup(self, dest, 1)) + return -1; + + return 0; +} + +/* Getters */ PyDoc_STRVAR(PyTextDest_window_doc, "Window where the text will be written" ); @@ -97,8 +155,32 @@ static PyGetSetDef PyTextDest_getseters[] = { {NULL} }; +/* Methods */ +PyDoc_STRVAR(PyTextDest_prnt_doc, + "prnt(str) -> None\n" + "\n" + "Print str to TextDest\n" +); +static PyObject *PyTextDest_prnt(PyTextDest *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"str", NULL}; + char *str = ""; + + RET_NULL_IF_INVALID(self->data); + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, + &str)) + return NULL; + + printtext_dest(self->data, "%s", str); + + Py_RETURN_NONE; +} + /* Methods for object */ static PyMethodDef PyTextDest_methods[] = { + {"prnt", (PyCFunction)PyTextDest_prnt, METH_VARARGS | METH_KEYWORDS, + PyTextDest_prnt_doc}, {NULL} /* Sentinel */ }; @@ -124,7 +206,7 @@ PyTypeObject PyTextDestType = { 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "PyTextDest objects", /* tp_doc */ + PyTextDest_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -139,38 +221,53 @@ PyTypeObject PyTextDestType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + (initproc)PyTextDest_init, /* tp_init */ 0, /* tp_alloc */ PyTextDest_new, /* tp_new */ }; - -/* TextDest factory function */ -PyObject *pytextdest_new(void *td) +static int pytextdest_setup(PyTextDest *pytdest, void *td, int owned) { PyObject *window, *server; - PyTextDest *pytdest; TEXT_DEST_REC *tdest = td; - window = py_irssi_chat_new(tdest->window, 1); - if (!window) - return NULL; + if (tdest->window) + { + window = pywindow_new(tdest->window); + if (!window) + return 0; + } server = py_irssi_chat_new(tdest->server, 1); if (!server) { Py_DECREF(window); - return NULL; + return 0; } - pytdest = py_inst(PyTextDest, PyTextDestType); - if (!pytdest) - return NULL; - pytdest->data = td; pytdest->window = window; pytdest->server = server; + pytdest->owned = owned; + return 1; +} + +/* TextDest factory function */ +PyObject *pytextdest_new(void *td) +{ + PyTextDest *pytdest; + + pytdest = py_inst(PyTextDest, PyTextDestType); + if (!pytdest) + return NULL; + + if (!pytextdest_setup(pytdest, td, 0)) + { + Py_DECREF(pytdest); + return NULL; + } + return (PyObject *)pytdest; } -- cgit