1 /*
2 * RAL -- Rubrica Addressbook Library
3 * file: ref.c
4 *
5 * Copyright (C) Nicola Fragale <nicolafragale@libero.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <glib.h>
22 #include <glib-object.h>
23
24 #include "ref.h"
25 #include "utils.h"
26
27
28 enum {
29 R_REF_TO = 1,
30 R_REF_FROM,
31 R_REF_NAME
32 };
33
34
35 /*
36 to: id of the card referenced
37 from: id of the card that reference this one
38 */
39 struct _RRefPrivate {
40 glong to; /* card's id: reference to card */
41 glong from; /* card's id: reference from card */
42 gchar* info; /* card's info */
43
44 gboolean dispose_has_run;
45 };
46
47
48 #define R_REF_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
49 R_REF_TYPE, RRefPrivate))
50
51 static void r_ref_class_init (RRefClass* klass);
52 static void r_ref_init (RRef* obj);
53
54 static void r_ref_dispose (RRef* obj);
55 static void r_ref_finalize (RRef* obj);
56
57
58 static void r_ref_set_property (GObject* obj, guint property_id,
59 const GValue* value, GParamSpec* spec);
60 static void r_ref_get_property (GObject* obj, guint property_id,
61 GValue* value, GParamSpec* spec);
62
63
64
65
66 GType
r_ref_get_type()67 r_ref_get_type()
68 {
69 static GType r_ref_type = 0;
70
71 if (!r_ref_type)
72 {
73 static const GTypeInfo r_ref_info =
74 {
75 sizeof(RRefClass),
76 NULL,
77 NULL,
78 (GClassInitFunc) r_ref_class_init,
79 NULL,
80 NULL,
81 sizeof(RRef),
82 0,
83 (GInstanceInitFunc) r_ref_init
84 };
85
86 r_ref_type = g_type_register_static (G_TYPE_OBJECT,
87 "RRef", &r_ref_info, 0);
88 }
89
90 return r_ref_type;
91 }
92
93
94 static void
r_ref_class_init(RRefClass * klass)95 r_ref_class_init(RRefClass* klass)
96 {
97 GObjectClass *parent_class;
98 GParamSpec* pspec;
99
100 parent_class = G_OBJECT_CLASS (klass);
101
102 parent_class->dispose = (GObjectFinalizeFunc) r_ref_dispose;
103 parent_class->finalize = (GObjectFinalizeFunc) r_ref_finalize;
104
105 parent_class->set_property = r_ref_set_property;
106 parent_class->get_property = r_ref_get_property;
107
108 g_type_class_add_private (klass, sizeof(RRefPrivate));
109
110
111 /* class property
112 */
113 /**
114 * RRef:ref-to
115 *
116 * the id of the referenced card by this one
117 */
118 pspec = g_param_spec_long("ref-to",
119 "ref card id",
120 "the id of the referenced card by this one",
121 G_MINLONG,
122 G_MAXLONG,
123 0,
124 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
125 g_object_class_install_property(parent_class, R_REF_TO, pspec);
126
127 /**
128 * RRef:ref-from:
129 *
130 * the id of the card that reference this one
131 */
132 pspec = g_param_spec_long("ref-from",
133 "ref card id",
134 "the id of the card that reference this one",
135 G_MINLONG,
136 G_MAXLONG,
137 0,
138 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
139 g_object_class_install_property(parent_class, R_REF_FROM, pspec);
140
141 /**
142 * RRef:ref-info
143 *
144 * the infos related to the card
145 */
146 pspec = g_param_spec_string("ref-info",
147 "card infos",
148 "the infos related to the card",
149 NULL,
150 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
151 g_object_class_install_property(parent_class, R_REF_NAME, pspec);
152 }
153
154
155 static void
r_ref_init(RRef * self)156 r_ref_init(RRef* self)
157 {
158 self->private = R_REF_GET_PRIVATE(self);
159
160 self->private->to = 0L;
161 self->private->from = 0L;
162 self->private->info = NULL;
163
164 self->private->dispose_has_run = FALSE;
165 }
166
167
168 static void
r_ref_dispose(RRef * self)169 r_ref_dispose (RRef* self)
170 {
171 RRefPrivate* private;
172
173 g_return_if_fail(IS_R_REF(self));
174
175 private = R_REF_GET_PRIVATE(self);
176 if (private->dispose_has_run)
177 return;
178
179 private->dispose_has_run = TRUE;
180 }
181
182
183 static void
r_ref_finalize(RRef * self)184 r_ref_finalize (RRef* self)
185 {
186 RRefPrivate* private;
187
188 g_return_if_fail(IS_R_REF(self));
189
190 private = R_REF_GET_PRIVATE(self);
191 r_utils_free_string(private->info);
192
193 g_free(private);
194 private = NULL;
195 }
196
197
198 static void
r_ref_set_property(GObject * obj,guint property_id,const GValue * value,GParamSpec * spec)199 r_ref_set_property (GObject* obj, guint property_id,
200 const GValue* value, GParamSpec* spec)
201 {
202 RRef* self = (RRef*) obj;
203 RRefPrivate* private = R_REF_GET_PRIVATE(self);
204
205 switch (property_id)
206 {
207 case R_REF_TO:
208 private->to = g_value_get_long(value);
209 break;
210
211 case R_REF_FROM:
212 private->from = g_value_get_long(value);
213 break;
214
215 case R_REF_NAME:
216 g_free(private->info);
217 private->info = g_value_dup_string(value);
218 break;
219
220 default:
221 break;
222 }
223 }
224
225
226 static void
r_ref_get_property(GObject * obj,guint property_id,GValue * value,GParamSpec * spec)227 r_ref_get_property (GObject* obj, guint property_id,
228 GValue* value, GParamSpec* spec)
229 {
230 RRef* self = (RRef*) obj;
231 RRefPrivate* private = R_REF_GET_PRIVATE(self);
232
233 switch (property_id)
234 {
235 case R_REF_TO:
236 g_value_set_long(value, private->to);
237 break;
238
239 case R_REF_FROM:
240 g_value_set_long(value, private->from);
241 break;
242
243 case R_REF_NAME:
244 g_value_set_string(value, private->info);
245 break;
246
247 default:
248 break;
249 }
250 }
251
252
253
254
255 /* Public
256 */
257 /**
258 * r_ref_new
259 * @id: the id of the referenced card
260 *
261 * create a #RRef
262 *
263 * returns: a new #RRef*
264 */
265 RRef*
r_ref_new(glong id)266 r_ref_new(glong id)
267 {
268 RRef* ref;
269
270 ref = g_object_new(r_ref_get_type(), "ref-to", id, NULL);
271
272 return ref;
273 }
274
275
276 /**
277 * r_ref_free
278 * @ref: a #RRef
279 *
280 * free the #RRef
281 */
282 void
r_ref_free(RRef * ref)283 r_ref_free(RRef* ref)
284 {
285 g_return_if_fail(IS_R_REF(ref));
286
287 g_object_unref(ref);
288 }
289
290
291
292 /**
293 * r_ref_copy
294 * @ref: a #RRef
295 *
296 * copy the given object
297 *
298 * returns: a new allocated #RRef*
299 */
300 RRef*
r_ref_copy(RRef * ref)301 r_ref_copy (RRef* ref)
302 {
303 RRef* new;
304 gulong to, from;
305 gchar* info;
306
307 g_return_val_if_fail(IS_R_REF(ref), NULL);
308
309 g_object_get(G_OBJECT(ref), "ref-to", &to, "ref-from", &from,
310 "ref-info", &info, NULL);
311
312 new = r_ref_new(to);
313 g_object_set(G_OBJECT(new), "ref-from", from, "ref-info", info, NULL);
314
315 return new;
316 }
317
318
319 /**
320 * r_ref_check
321 * @ref: a #RRef
322 * @property: an #RRef's property
323 * @value: the property's value (if set)
324 *
325 * check if the given property is set.
326 *
327 * returns: %FALSE if the property is %NULL, otherwise it return %TRUE and
328 * the content of the property is copied into value
329 **/
330 gboolean
r_ref_check(RRef * ref,const gchar * property,gchar ** value)331 r_ref_check (RRef* ref, const gchar* property, gchar** value)
332 {
333 gchar* tmp;
334
335 g_return_val_if_fail(IS_R_REF(ref), FALSE);
336
337 g_object_get(ref, property, &tmp, NULL);
338
339 if (tmp)
340 {
341 if (value)
342 *value = tmp;
343
344 return TRUE;
345 }
346
347 return FALSE;
348 }
349