summaryrefslogtreecommitdiffstats
path: root/plug-ins/pygimp/pygimp-parasite.c
blob: 279cfbd8c69f4c61d9b1a6e68c4644e9f2c04bc7 (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
/* -*- Mode: C; c-basic-offset: 4 -*-
 * Gimp-Python - allows the writing of Gimp plugins in Python.
 * Copyright (C) 1997-2002  James Henstridge <james@daa.com.au>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "pygimp.h"

static PyObject *
para_copy(PyGimpParasite *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":copy"))
	return NULL;

    return pygimp_parasite_new(gimp_parasite_copy(self->para));
}

static PyObject *
para_is_type(PyGimpParasite *self, PyObject *args)
{
    char *name;

    if (!PyArg_ParseTuple(args, "s:is_type", &name))
	return NULL;

    return PyInt_FromLong(gimp_parasite_is_type(self->para, name));
}

static PyObject *
para_has_flag(PyGimpParasite *self, PyObject *args)
{
    int flag;

    if (!PyArg_ParseTuple(args, "i:has_flag", &flag))
	return NULL;

    return PyInt_FromLong(gimp_parasite_has_flag(self->para, flag));
}


static PyMethodDef para_methods[] = {
    {"copy",	(PyCFunction)para_copy,	METH_VARARGS},
    {"is_type",	(PyCFunction)para_is_type,	METH_VARARGS},
    {"has_flag",(PyCFunction)para_has_flag,	METH_VARARGS},

    {NULL,		NULL}		/* sentinel */
};

static PyObject *
para_get_is_persistent(PyGimpParasite *self, void *closure)
{
    return PyBool_FromLong(gimp_parasite_is_persistent(self->para));
}

static PyObject *
para_get_is_undoable(PyGimpParasite *self, void *closure)
{
    return PyBool_FromLong(gimp_parasite_is_undoable(self->para));
}

static PyObject *
para_get_flags(PyGimpParasite *self, void *closure)
{
    return PyInt_FromLong(gimp_parasite_flags(self->para));
}

static PyObject *
para_get_name(PyGimpParasite *self, void *closure)
{
    return PyString_FromString(gimp_parasite_name(self->para));
}

static PyObject *
para_get_data(PyGimpParasite *self, void *closure)
{
    return PyString_FromStringAndSize(gimp_parasite_data(self->para),
				      gimp_parasite_data_size(self->para));
}

static PyGetSetDef para_getsets[] = {
    { "is_persistent", (getter)para_get_is_persistent, (setter)0 },
    { "is_undoable", (getter)para_get_is_undoable, (setter)0 },
    { "flags", (getter)para_get_flags, (setter)0 },
    { "name", (getter)para_get_name, (setter)0 },
    { "data", (getter)para_get_data, (setter)0 },
    { NULL, (getter)0, (setter)0 },
};

static void
para_dealloc(PyGimpParasite *self)
{
    gimp_parasite_free(self->para);
    PyObject_DEL(self);
}

static PyObject *
para_repr(PyGimpParasite *self)
{
    PyObject *s;

    s = PyString_FromFormat("<parasite %s>", gimp_parasite_name(self->para));

    return s;
}

static PyObject *
para_str(PyGimpParasite *self)
{
    return PyString_FromStringAndSize(gimp_parasite_data(self->para),
				      gimp_parasite_data_size(self->para));
}

static int
para_init(PyGimpParasite *self, PyObject *args, PyObject *kwargs)
{
    char *name;
    int flags, size;
    guint8 *data;

    static char *kwlist[] = { "name", "flags", "data", NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "sis#:gimp.Parasite.__init__", kwlist,
				     &name, &flags,
				     &data, &size))
	return -1;

    self->para = gimp_parasite_new(name, flags, size, data);

    if (!self->para) {
	PyErr_Format(pygimp_error, "could not create parasite '%s'", name);
	return -1;
    }

    return 0;
}


PyTypeObject PyGimpParasite_Type = {
    PyObject_HEAD_INIT(NULL)
    0,                                  /* ob_size */
    "gimp.Parasite",                    /* tp_name */
    sizeof(PyGimpParasite),             /* tp_basicsize */
    0,                                  /* tp_itemsize */
    /* methods */
    (destructor)para_dealloc,           /* tp_dealloc */
    (printfunc)0,                       /* tp_print */
    (getattrfunc)0,                     /* tp_getattr */
    (setattrfunc)0,                     /* tp_setattr */
    (cmpfunc)0,                         /* tp_compare */
    (reprfunc)para_repr,                /* tp_repr */
    0,                                  /* tp_as_number */
    0,                                  /* tp_as_sequence */
    0,                                  /* tp_as_mapping */
    (hashfunc)0,                        /* tp_hash */
    (ternaryfunc)0,                     /* tp_call */
    (reprfunc)para_str,                 /* tp_str */
    (getattrofunc)0,                    /* tp_getattro */
    (setattrofunc)0,                    /* tp_setattro */
    0,					/* tp_as_buffer */
    Py_TPFLAGS_DEFAULT,	                /* tp_flags */
    NULL, /* Documentation string */
    (traverseproc)0,			/* tp_traverse */
    (inquiry)0,				/* tp_clear */
    (richcmpfunc)0,			/* tp_richcompare */
    0,					/* tp_weaklistoffset */
    (getiterfunc)0,			/* tp_iter */
    (iternextfunc)0,			/* tp_iternext */
    para_methods,			/* tp_methods */
    0,					/* tp_members */
    para_getsets,			/* tp_getset */
    (PyTypeObject *)0,			/* tp_base */
    (PyObject *)0,			/* tp_dict */
    0,					/* tp_descr_get */
    0,					/* tp_descr_set */
    0,					/* tp_dictoffset */
    (initproc)para_init,                /* tp_init */
    (allocfunc)0,			/* tp_alloc */
    (newfunc)0,				/* tp_new */
};

PyObject *
pygimp_parasite_new(GimpParasite *para)
{
    PyGimpParasite *self;

    if (!para) {
	Py_INCREF(Py_None);
	return Py_None;
    }

    self = PyObject_NEW(PyGimpParasite, &PyGimpParasite_Type);

    if (self == NULL)
	return NULL;

    self->para = para;

    return (PyObject *)self;
}