root/trunk/midgard/apis/python/py_midgard_object_attachment.c

Revision 16508, 3.9 kB (checked in by piotras, 6 months ago)

Defined NO_IMPORT_PYGOBJECT...

Line 
1 /*
2  * Copyright (C) 2008 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define NO_IMPORT_PYGOBJECT
24 #include "py_midgard.h"
25
26 #define ATTACHMENT_DEBUG(__name) \
27         CHECK_MGD; \
28         const gchar *cname = NULL; \
29         if(self) { \
30                 CLASS_METHOD_DEBUG(G_OBJECT_TYPE_NAME(G_OBJECT(self->obj)), __name); \
31         } \
32         if(cname == NULL) \
33                 CLASS_METHOD_DEBUG("midgard_dbobject", __name);
34
35 #define _FREE_PARAMETERS \
36                 guint _i; \
37         for(_i = 0; _i < n_params; _i++) { \
38                                 g_value_unset(&params[_i].value); \
39                         } \
40         g_free(params);
41
42 PyObject *
43 pymidgard_object_delete_attachments(PyGObject *self, PyObject *args)
44 {
45         ATTACHMENT_DEBUG("delete_attachments");
46
47         PyObject *props;
48        
49         if(!PyArg_ParseTuple(args, "O", &props))
50                 return NULL;
51
52         MgdObject *mobject = MIDGARD_OBJECT(self->obj);
53
54         guint n_params;
55         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
56        
57         gboolean rv = midgard_object_delete_attachments(mobject, n_params, params);
58
59         _FREE_PARAMETERS;
60
61         if(rv)
62                 Py_RETURN_TRUE;
63
64         Py_RETURN_FALSE;
65 }
66
67 PyObject *
68 pymidgard_object_purge_attachments(PyGObject *self, PyObject *args)
69 {
70         ATTACHMENT_DEBUG("purge_attachments");
71
72         PyObject *props;
73         gboolean delete_blobs = TRUE;
74         if(!PyArg_ParseTuple(args, "|bO", &delete_blobs, &props))
75                 return NULL;
76
77         MgdObject *mobject = MIDGARD_OBJECT(self->obj);
78
79         guint n_params;
80         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
81        
82         gboolean rv = midgard_object_purge_attachments(mobject, delete_blobs, n_params, params);
83
84         _FREE_PARAMETERS;
85
86         if(rv)
87                 Py_RETURN_TRUE;
88
89         Py_RETURN_FALSE;
90 }
91
92 PyObject *
93 pymidgard_object_list_attachments(PyGObject *self, PyObject *args)
94 {
95         ATTACHMENT_DEBUG("list_attachments");
96
97         guint i = 0;
98        
99         if(!PyArg_ParseTuple(args, ""))
100                 return NULL;
101
102         MgdObject *mobject = MIDGARD_OBJECT(self->obj);
103
104         MgdObject **objects = midgard_object_list_attachments(mobject);
105
106         if(!objects)
107                 return PyTuple_New(i);
108
109         while(objects[i] != NULL)
110                 i++;
111        
112         PyObject *list = PyTuple_New(i);
113        
114         OBJECTS2LIST(objects, list);
115        
116         g_free(objects);
117        
118         return list;
119 }
120
121 PyObject *
122 pymidgard_object_find_attachments(PyGObject *self, PyObject *args)
123 {
124         ATTACHMENT_DEBUG("find_attachments");
125
126         PyObject *props;
127
128         if(!PyArg_ParseTuple(args, "O", &props))
129                 return NULL;
130        
131         MgdObject *mobject = MIDGARD_OBJECT(self->obj);
132
133         guint n_params, i = 0;
134         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
135
136         MgdObject **objects = midgard_object_find_attachments(mobject, n_params, params);
137
138         _FREE_PARAMETERS;
139
140         if(!objects)
141                 return PyTuple_New(i);
142
143         while(objects[i] != NULL)
144                 i++;
145        
146         PyObject *list = PyTuple_New(i);
147        
148         OBJECTS2LIST(objects, list);
149        
150         g_free(objects);
151        
152         return list;
153 }
154
155 PyObject *
156 pymidgard_object_create_attachment(PyGObject *self, PyObject *args)
157 {
158         ATTACHMENT_DEBUG("create_attachment");
159
160         gchar *name = NULL;
161         gchar *title = NULL;
162         gchar *mimetype = NULL;
163
164         if(!PyArg_ParseTuple(args, "|s|s|s", &name, &title, &mimetype))
165                 return NULL;
166        
167         MgdObject *mobject = MIDGARD_OBJECT(self->obj);
168         MgdObject *att = midgard_object_create_attachment(mobject, name, title, mimetype);
169
170         if(att)
171                 return Py_BuildValue("O", pygobject_new(G_OBJECT(att)));
172
173         Py_INCREF(Py_None);
174         return Py_None;
175 }
Note: See TracBrowser for help on using the browser.