1 /*
2 *  RAL -- Rubrica Addressbook Library
3 *  file: telephone.h
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 
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <glib/gi18n-lib.h>
25 
26 #include "telephone.h"
27 #include "lookup.h"
28 #include "utils.h"
29 
30 /*   property enumetations
31 */
32 enum {
33   PHONE_PROP_0,
34   PHONE_NUMBER,
35   PHONE_TYPE
36 };
37 
38 
39 static RLookupTable table[] = {
40   {"home",          N_("home"),          R_TELEPHONE_HOME          },
41   {"work",          N_("work"),          R_TELEPHONE_WORK          },
42   {"cellphone",     N_("cellphone"),     R_TELEPHONE_CELLPHONE     },
43   {"fax",           N_("fax"),           R_TELEPHONE_FAX           },
44   {"pager",         N_("pager"),         R_TELEPHONE_PAGER         },
45   {"other",         N_("other"),         R_TELEPHONE_OTHER         },
46   {"operator",      N_("operator"),      R_TELEPHONE_OPERATOR      },
47   {"green",         N_("green"),         R_TELEPHONE_GREEN         },
48   {"customer care", N_("customer care"), R_TELEPHONE_CUSTOMER_CARE },
49   {"unknown",       N_("unknown"),       R_TELEPHONE_UNKNOWN       },
50   {NULL,            NULL,                R_TELEPHONE_INVALID       }
51 };
52 
53 
54 struct _RTelephonePrivate {
55   gchar* number;
56   gint   type;
57 
58   gboolean dispose_has_run;
59 };
60 
61 
62 #define R_TELEPHONE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
63                                     R_TELEPHONE_TYPE, RTelephonePrivate))
64 
65 
66 
67 static void r_telephone_class_init (RTelephoneClass* klass);
68 static void r_telephone_init       (RTelephone* obj);
69 
70 static void r_telephone_dispose    (RTelephone* obj);
71 static void r_telephone_finalize   (RTelephone* obj);
72 
73 
74 static void r_telephone_set_property (GObject* obj, guint property_id,
75 				      const GValue* value, GParamSpec* spec);
76 static void r_telephone_get_property (GObject* obj, guint property_id,
77 				      GValue* value, GParamSpec* spec);
78 
79 GType
r_telephone_get_type()80 r_telephone_get_type()
81 {
82   static GType tel_num_type = 0;
83 
84   if (!tel_num_type)
85     {
86       static const GTypeInfo tel_num_info =
87 	{
88 	  sizeof(RTelephoneClass),
89 	  NULL,
90 	  NULL,
91 	  (GClassInitFunc) r_telephone_class_init,
92 	  NULL,
93 	  NULL,
94 	  sizeof(RTelephone),
95 	  0,
96 	  (GInstanceInitFunc) r_telephone_init
97 	};
98 
99       tel_num_type = g_type_register_static (G_TYPE_OBJECT,
100 					     "RTelephone",
101 					     &tel_num_info, 0);
102     }
103 
104   return tel_num_type;
105 }
106 
107 
108 static void
r_telephone_class_init(RTelephoneClass * klass)109 r_telephone_class_init(RTelephoneClass* klass)
110 {
111   GObjectClass *class;
112   GParamSpec* pspec;
113 
114   class = G_OBJECT_CLASS (klass);
115 
116   class->dispose  = (GObjectFinalizeFunc) r_telephone_dispose;
117   class->finalize = (GObjectFinalizeFunc) r_telephone_finalize;
118 
119   class->set_property = r_telephone_set_property;
120   class->get_property = r_telephone_get_property;
121 
122   g_type_class_add_private (klass, sizeof(RTelephonePrivate));
123 
124   /**
125    * RTelephone:telephone-number:
126    *
127    * contact's telephone number
128    */
129   pspec = g_param_spec_string("telephone-number",
130 			      "telephone number",
131 			      "contact's telephone number",
132 			      NULL,
133 			      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
134   g_object_class_install_property(class, PHONE_NUMBER, pspec);
135 
136   /**
137    * RTelephone:telephone-type:
138    *
139    * telephone number's type. See #RTelephoneType for valid values
140    */
141   pspec = g_param_spec_int("telephone-type",
142 			   "telephone number's type",
143 			   "telephone number's type",
144 			   R_TELEPHONE_HOME,        // min
145 			   R_TELEPHONE_UNKNOWN,     // max
146 			   R_TELEPHONE_UNKNOWN,     // default
147 			   G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
148   g_object_class_install_property(class, PHONE_TYPE, pspec);
149 }
150 
151 
152 static void
r_telephone_init(RTelephone * self)153 r_telephone_init(RTelephone* self)
154 {
155   RTelephonePrivate* priv = R_TELEPHONE_GET_PRIVATE(self);
156 
157   priv->number          = NULL;
158   priv->dispose_has_run = FALSE;
159 }
160 
161 
162 static void
r_telephone_dispose(RTelephone * self)163 r_telephone_dispose (RTelephone* self)
164 {
165   RTelephonePrivate* priv;
166 
167   g_return_if_fail(IS_R_TELEPHONE(self));
168 
169   priv = R_TELEPHONE_GET_PRIVATE(self);
170   if (priv->dispose_has_run)
171     return;
172 
173   priv->dispose_has_run = TRUE;
174 }
175 
176 
177 static void
r_telephone_finalize(RTelephone * self)178 r_telephone_finalize (RTelephone* self)
179 {
180   RTelephonePrivate* priv;
181 
182   g_return_if_fail(IS_R_TELEPHONE(self));
183 
184   priv = R_TELEPHONE_GET_PRIVATE(self);
185   r_utils_free_string(priv->number);
186 }
187 
188 
189 
190 static void
r_telephone_set_property(GObject * obj,guint property_id,const GValue * value,GParamSpec * spec)191 r_telephone_set_property (GObject* obj, guint property_id,
192 			  const GValue* value, GParamSpec* spec)
193 {
194   RTelephone* self = (RTelephone*) obj;
195   RTelephonePrivate* priv = R_TELEPHONE_GET_PRIVATE(self);
196 
197   switch (property_id)
198     {
199     case PHONE_NUMBER:
200       g_free(priv->number);
201       priv->number = g_value_dup_string(value);
202       break;
203 
204     case PHONE_TYPE:
205       priv->type = g_value_get_int(value);
206       break;
207 
208     default:
209       break;
210     }
211 }
212 
213 
214 static void
r_telephone_get_property(GObject * obj,guint property_id,GValue * value,GParamSpec * spec)215 r_telephone_get_property (GObject* obj, guint property_id,
216 			  GValue* value, GParamSpec* spec)
217 {
218   RTelephone* self = (RTelephone*) obj;
219   RTelephonePrivate* priv = R_TELEPHONE_GET_PRIVATE(self);
220 
221   switch (property_id)
222     {
223     case PHONE_NUMBER:
224       g_value_set_string(value, priv->number);
225       break;
226 
227     case PHONE_TYPE:
228       g_value_set_int(value, priv->type);
229       break;
230 
231     default:
232       break;
233     }
234 }
235 
236 
237 /**
238  * r_telephone_new
239  *
240  * create a new #RTelephone
241  *
242  * returns: a #RTelephone*
243  */
244 RTelephone*
r_telephone_new(void)245 r_telephone_new(void)
246 {
247   RTelephone* telephone;
248 
249   telephone = g_object_new(r_telephone_get_type(), NULL);
250 
251   return telephone;
252 }
253 
254 
255 /**
256  * r_telephone_free
257  * @telephone: a #RTelephone
258  *
259  * free the #RTelephone*
260  */
261 void
r_telephone_free(RTelephone * telephone)262 r_telephone_free(RTelephone* telephone)
263 {
264   g_return_if_fail(IS_R_TELEPHONE(telephone));
265 
266   g_object_unref(telephone);
267 }
268 
269 
270 gboolean
r_telephone_search(RTelephone * telephone,const gchar * str)271 r_telephone_search (RTelephone *telephone, const gchar* str)
272 {
273   RTelephonePrivate* priv;
274 
275   g_return_val_if_fail(IS_R_TELEPHONE(telephone), FALSE);
276   g_return_val_if_fail(str != NULL, FALSE);
277 
278   priv = R_TELEPHONE_GET_PRIVATE(telephone);
279   if (g_strrstr(priv->number, str))
280     return TRUE;
281 
282   return FALSE;
283 }
284 
285 
286 
287 
288 /**
289  * r_telephone_check
290  * @telephone: a #RTelephone
291  * @property: an #RTelephone's property
292  * @value: the property's value (if set)
293  *
294  * check if the given property is set.
295  *
296  * returns: %FALSE if the property is %NULL, otherwise it return %TRUE and
297  * the content of the property is copied into value
298  **/
299 gboolean
r_telephone_check(RTelephone * telephone,const gchar * property,gchar ** value)300 r_telephone_check (RTelephone* telephone, const gchar* property, gchar** value)
301 {
302   gchar* tmp;
303 
304   g_return_val_if_fail(IS_R_TELEPHONE(telephone), FALSE);
305 
306   g_object_get(telephone, property, &tmp, NULL);
307 
308   if (tmp)
309     {
310       if (value)
311 	*value = tmp;
312 
313       return TRUE;
314     }
315 
316   return FALSE;
317 }
318 
319 
320 
321 /**
322  * r_telephone_get_number
323  * @telephone: a #RTelephone
324  *
325  * copy the given telephone
326  *
327  * returns: a new #RTelephone*
328  */
329 RTelephone*
r_telephone_copy(RTelephone * telephone)330 r_telephone_copy (RTelephone *telephone)
331 {
332   RTelephone* new;
333   RTelephoneType type;
334   gchar *number;
335 
336   g_return_val_if_fail(IS_R_TELEPHONE(telephone), NULL);
337 
338   new = r_telephone_new();
339 
340   g_object_get(G_OBJECT(telephone),
341 	       "telephone-number", &number,
342 	       "telephone-type", &type,
343 	       NULL);
344   g_object_set(G_OBJECT(new),
345 	       "telephone-number", number,
346 	       "telephone-type", type,
347 	       NULL);
348 
349   return new;
350 }
351 
352 
353 
354 /**
355  * r_telephone_lookup_str2enum
356  * @type: a gchar*
357  *
358  * encode the given string
359  *
360  * returns: a #RTelephoneType
361  */
362 RTelephoneType
r_telephone_lookup_str2enum(gchar * str)363 r_telephone_lookup_str2enum (gchar* str)
364 {
365   return (RTelephoneType) r_lookup_table_str2enum(table, str);
366 }
367 
368 
369 /**
370  * r_telephone_lookup_enum2str
371  * @type: a #RTelephoneType
372  *
373  * decode the given type
374  *
375  * returns: a gchar*
376  */
377 gchar*
r_telephone_lookup_enum2str(RTelephoneType type)378 r_telephone_lookup_enum2str (RTelephoneType type)
379 {
380   return r_lookup_table_enum2str(table, type);
381 }
382 
383 
384 gchar*
r_telephone_lookup_enum2lbl(RTelephoneType type)385 r_telephone_lookup_enum2lbl (RTelephoneType type)
386 {
387   return r_lookup_table_enum2lbl(table, type);
388 }
389 
390 
391 gchar*
r_telephone_lookup_str2lbl(gchar * str)392 r_telephone_lookup_str2lbl  (gchar* str)
393 {
394   return r_lookup_table_str2lbl(table, str);
395 }
396