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

Revision 24366, 4.2 kB (checked in by piotras, 3 months ago)

Repleace MgdObject? with MidgardObject?. Refs #1515

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         MidgardObject *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_has_attachments(PyGObject *self, PyObject *args)
69 {
70         ATTACHMENT_DEBUG("has_attachments");
71
72         MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
73        
74         gboolean rv = midgard_object_has_attachments(mobject);
75
76         if(rv)
77                 Py_RETURN_TRUE;
78
79         Py_RETURN_FALSE;
80 }
81
82
83 PyObject *
84 pymidgard_object_purge_attachments(PyGObject *self, PyObject *args)
85 {
86         ATTACHMENT_DEBUG("purge_attachments");
87
88         PyObject *props;
89         gboolean delete_blobs = TRUE;
90         if(!PyArg_ParseTuple(args, "|bO", &delete_blobs, &props))
91                 return NULL;
92
93         MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
94
95         guint n_params;
96         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
97        
98         gboolean rv = midgard_object_purge_attachments(mobject, delete_blobs, n_params, params);
99
100         _FREE_PARAMETERS;
101
102         if(rv)
103                 Py_RETURN_TRUE;
104
105         Py_RETURN_FALSE;
106 }
107
108 PyObject *
109 pymidgard_object_list_attachments(PyGObject *self, PyObject *args)
110 {
111         ATTACHMENT_DEBUG("list_attachments");
112
113         guint i = 0;
114        
115         if(!PyArg_ParseTuple(args, ""))
116                 return NULL;
117
118         MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
119
120         MidgardObject **objects = midgard_object_list_attachments(mobject);
121
122         if(!objects)
123                 return PyTuple_New(i);
124
125         while(objects[i] != NULL)
126                 i++;
127        
128         PyObject *list = PyTuple_New(i);
129        
130         OBJECTS2LIST(objects, list);
131        
132         g_free(objects);
133        
134         return list;
135 }
136
137 PyObject *
138 pymidgard_object_find_attachments(PyGObject *self, PyObject *args)
139 {
140         ATTACHMENT_DEBUG("find_attachments");
141
142         PyObject *props;
143
144         if(!PyArg_ParseTuple(args, "O", &props))
145                 return NULL;
146        
147         MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
148
149         guint n_params, i = 0;
150         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
151
152         MidgardObject **objects = midgard_object_find_attachments(mobject, n_params, params);
153
154         _FREE_PARAMETERS;
155
156         if(!objects)
157                 return PyTuple_New(i);
158
159         while(objects[i] != NULL)
160                 i++;
161        
162         PyObject *list = PyTuple_New(i);
163        
164         OBJECTS2LIST(objects, list);
165        
166         g_free(objects);
167        
168         return list;
169 }
170
171 PyObject *
172 pymidgard_object_create_attachment(PyGObject *self, PyObject *args)
173 {
174         ATTACHMENT_DEBUG("create_attachment");
175
176         gchar *name = NULL;
177         gchar *title = NULL;
178         gchar *mimetype = NULL;
179
180         if(!PyArg_ParseTuple(args, "|s|s|s", &name, &title, &mimetype))
181                 return NULL;
182        
183         MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
184         MidgardObject *att = midgard_object_create_attachment(mobject, name, title, mimetype);
185
186         if(att)
187                 return Py_BuildValue("O", pygobject_new(G_OBJECT(att)));
188
189         Py_RETURN_NONE;
190 }
Note: See TracBrowser for help on using the browser.