1 /*
2 *  RAL -- Rubrica Addressbook Library
3 *  file: net.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 
22 #include <glib.h>
23 #include <glib-object.h>
24 
25 #include "net.h"
26 #include "utils.h"
27 
28 enum {
29   NET_URL = 1,
30   NET_TYPE
31 };
32 
33 typedef struct _NetConversion {
34   gint   token;
35   gchar* label;
36 } NetConversion;
37 
38 
39 struct _RNetAddressPrivate {
40   gchar* url;
41   gint   type;
42 
43   gboolean dispose_has_run;
44 };
45 
46 #define R_NET_ADDRESS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
47                                       R_NET_ADDRESS_TYPE, RNetAddressPrivate))
48 
49 static NetConversion table[] = {
50   {R_NET_ADDRESS_WEB,        "web"       },
51   {R_NET_ADDRESS_EMAIL,      "email"     },
52   {R_NET_ADDRESS_EKIGA,      "ekiga"     },
53   {R_NET_ADDRESS_IRC,        "irc"       },
54   {R_NET_ADDRESS_IRC_AIM,    "aim"       },
55   {R_NET_ADDRESS_IRC_ICQ,    "icq"       },
56   {R_NET_ADDRESS_IRC_JABBER, "jabber"    },
57   {R_NET_ADDRESS_IRC_YAHOO,  "yahoo"     },
58   {R_NET_ADDRESS_IRC_MSN,    "msn"       },
59   {R_NET_ADDRESS_WORK_WEB,   "work web"  },
60   {R_NET_ADDRESS_WORK_EMAIL, "work email"},
61   {R_NET_ADDRESS_UNKNOWN,    "unknown"   },
62 };
63 
64 
65 
66 static void r_net_address_class_init (RNetAddressClass* klass);
67 static void r_net_address_init       (RNetAddress* obj);
68 
69 static void r_net_address_dispose    (RNetAddress* obj);
70 static void r_net_address_finalize   (RNetAddress* obj);
71 
72 
73 static void r_net_address_set_property (GObject* obj, guint property_id,
74 					const GValue* value, GParamSpec* spec);
75 static void r_net_address_get_property (GObject* obj, guint property_id,
76 					GValue* value, GParamSpec* spec);
77 
78 
79 GType
r_net_address_get_type()80 r_net_address_get_type()
81 {
82   static GType r_net_address_type = 0;
83 
84   if (!r_net_address_type)
85     {
86       static const GTypeInfo r_net_address_info =
87 	{
88 	  sizeof(RNetAddressClass),
89 	  NULL,
90 	  NULL,
91 	  (GClassInitFunc) r_net_address_class_init,
92 	  NULL,
93 	  NULL,
94 	  sizeof(RNetAddress),
95 	  0,
96 	  (GInstanceInitFunc) r_net_address_init
97 	};
98 
99       r_net_address_type = g_type_register_static (G_TYPE_OBJECT,
100 						   "RNetAddress",
101 						   &r_net_address_info, 0);
102     }
103 
104   return r_net_address_type;
105 }
106 
107 
108 static void
r_net_address_class_init(RNetAddressClass * klass)109 r_net_address_class_init(RNetAddressClass* klass)
110 {
111   GObjectClass *class;
112   GParamSpec* pspec;
113 
114   class  = G_OBJECT_CLASS (klass);
115 
116   class->dispose  = (GObjectFinalizeFunc) r_net_address_dispose;
117   class->finalize = (GObjectFinalizeFunc) r_net_address_finalize;
118 
119   class->set_property = r_net_address_set_property;
120   class->get_property = r_net_address_get_property;
121 
122   g_type_class_add_private (klass, sizeof(RNetAddressPrivate));
123 
124   /**
125    * RNetAddress:url:
126    *
127    * the net address uri
128    */
129   pspec = g_param_spec_string("url",
130 			      "url",
131 			      "the net address uri",
132 			      NULL,
133 			      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
134   g_object_class_install_property(class, NET_URL, pspec);
135 
136   /**
137    * RNetAddress:url-type:
138    *
139    * the uri's type, see #RNetAddressType for valid values
140    */
141   pspec = g_param_spec_int("url-type",
142 			   "url's type",
143 			   "url's type",
144 			   R_NET_ADDRESS_WEB,
145 			   R_NET_ADDRESS_UNKNOWN,
146 			   R_NET_ADDRESS_UNKNOWN,
147 			   G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
148   g_object_class_install_property(class, NET_TYPE, pspec);
149 }
150 
151 
152 
153 static void
r_net_address_init(RNetAddress * self)154 r_net_address_init(RNetAddress* self)
155 {
156   RNetAddressPrivate* priv = R_NET_ADDRESS_GET_PRIVATE(self);
157 
158   priv->url  = NULL;
159   priv->dispose_has_run = FALSE;
160 }
161 
162 
163 static void
r_net_address_dispose(RNetAddress * self)164 r_net_address_dispose (RNetAddress* self)
165 {
166   RNetAddressPrivate* priv;
167 
168   g_return_if_fail(IS_R_NET_ADDRESS(self));
169 
170   priv = R_NET_ADDRESS_GET_PRIVATE(self);
171   if (priv->dispose_has_run)
172     return;
173 
174   priv->dispose_has_run = TRUE;
175 }
176 
177 
178 static void
r_net_address_finalize(RNetAddress * self)179 r_net_address_finalize (RNetAddress* self)
180 {
181   RNetAddressPrivate* priv;
182 
183   g_return_if_fail(IS_R_NET_ADDRESS(self));
184 
185   priv = R_NET_ADDRESS_GET_PRIVATE(self);
186   r_utils_free_string(priv->url);
187 }
188 
189 
190 static void
r_net_address_set_property(GObject * obj,guint property_id,const GValue * value,GParamSpec * spec)191 r_net_address_set_property (GObject* obj, guint property_id,
192 			    const GValue* value, GParamSpec* spec)
193 {
194   RNetAddress* self = (RNetAddress*) obj;
195   RNetAddressPrivate* priv = R_NET_ADDRESS_GET_PRIVATE(self);;
196 
197   switch (property_id)
198     {
199     case NET_URL:
200       g_free(priv->url);
201       priv->url = g_value_dup_string(value);
202       break;
203 
204     case NET_TYPE:
205       priv->type = g_value_get_int(value);
206       break;
207 
208     default:
209       break;
210     }
211 }
212 
213 
214 static void
r_net_address_get_property(GObject * obj,guint property_id,GValue * value,GParamSpec * spec)215 r_net_address_get_property (GObject* obj, guint property_id,
216 			    GValue* value, GParamSpec* spec)
217 {
218   RNetAddress* self = (RNetAddress*) obj;
219   RNetAddressPrivate* priv = R_NET_ADDRESS_GET_PRIVATE(self);;
220 
221   switch (property_id)
222     {
223     case NET_URL:
224       g_value_set_string(value, priv->url);
225       break;
226 
227     case NET_TYPE:
228       g_value_set_int(value, priv->type);
229       break;
230 
231     default:
232       break;
233     }
234 }
235 
236 
237 /**
238  * r_net_address_new
239  *
240  * create a new #RNetAddress
241  *
242  * returns: a #RNetAddress*
243  */
244 RNetAddress*
r_net_address_new(void)245 r_net_address_new(void)
246 {
247   RNetAddress* net;
248 
249   net = g_object_new(r_net_address_get_type(), NULL);
250 
251   return net;
252 }
253 
254 
255 /**
256  * r_net_address_free
257  * @net: a #RNetAddress
258  *
259  * free the memory owned by net
260  */
261 void
r_net_address_free(RNetAddress * net)262 r_net_address_free(RNetAddress* net)
263 {
264   g_return_if_fail(IS_R_NET_ADDRESS(net));
265 
266   g_object_unref(net);
267 }
268 
269 
270 
271 gboolean
r_net_address_search(RNetAddress * net,const gchar * str)272 r_net_address_search (RNetAddress *net, const gchar* str)
273 {
274   RNetAddressPrivate* priv;
275 
276   g_return_val_if_fail(IS_R_NET_ADDRESS(net), FALSE);
277   g_return_val_if_fail(str != NULL, FALSE);
278 
279   priv = R_NET_ADDRESS_GET_PRIVATE(net);
280   if (g_strrstr(priv->url, str))
281     return TRUE;
282 
283   return FALSE;
284 }
285 
286 
287 
288 
289 /**
290  * r_net_address_copy
291  * @net: a #RNetAddress
292  *
293  * copy the given net address.
294  *
295  * returns: a new allocated #RNetAddress*
296  */
297 RNetAddress*
r_net_address_copy(RNetAddress * net)298 r_net_address_copy (RNetAddress *net)
299 {
300   RNetAddress* new;
301   RNetAddressType type;
302   gchar *url;
303 
304   g_return_val_if_fail(IS_R_NET_ADDRESS(net), NULL);
305 
306   new = r_net_address_new();
307 
308   g_object_get(G_OBJECT(net), "url", &url, "url-type", &type, NULL);
309   g_object_set(G_OBJECT(new), "url", url,  "url-type", type, NULL);
310 
311   return new;
312 }
313 
314 
315 /**
316  * r_net_address_decode_type
317  * @type: a #RNetAddressType
318  *
319  * decode the #RNetAddressType given type
320  *
321  * returns: a gchar*
322  */
323 gchar*
r_net_address_decode_type(RNetAddressType type)324 r_net_address_decode_type (RNetAddressType type)
325 {
326   if ((type < R_NET_ADDRESS_WEB) || (type > R_NET_ADDRESS_UNKNOWN))
327     return "unknown";
328 
329   return table[type].label;
330 }
331 
332 
333 /**
334  * r_net_address_get_decoded_type
335  * @type: gchar*
336  *
337  * decode the string returned by r_net_address_get_url_type()
338  *
339  * returns: a #RNetAddressType
340  */
341 RNetAddressType
r_net_address_encode_type(gchar * type)342 r_net_address_encode_type (gchar* type)
343 {
344   RNetAddressType ret = R_NET_ADDRESS_UNKNOWN;
345 
346   if (!type)
347     return ret;
348 
349   if (g_ascii_strcasecmp(type, "web") == 0)
350     ret = R_NET_ADDRESS_WEB;
351   if (g_ascii_strcasecmp(type, "work web") == 0)
352     ret = R_NET_ADDRESS_WORK_WEB;
353   else if (g_ascii_strcasecmp(type, "email") == 0)
354     ret = R_NET_ADDRESS_EMAIL;
355   else if (g_ascii_strcasecmp(type, "work email") == 0)
356     ret = R_NET_ADDRESS_WORK_EMAIL;
357   else if (g_ascii_strcasecmp(type, "ekiga") == 0)
358     ret = R_NET_ADDRESS_EKIGA;
359   else if (g_ascii_strcasecmp(type, "irc") == 0)
360     ret = R_NET_ADDRESS_IRC;
361   else if (g_ascii_strcasecmp(type, "aim") == 0)
362     ret = R_NET_ADDRESS_IRC_AIM;
363   else if (g_ascii_strcasecmp(type, "jabber") == 0)
364     ret = R_NET_ADDRESS_IRC_JABBER;
365   else if (g_ascii_strcasecmp(type, "icq") == 0)
366     ret = R_NET_ADDRESS_IRC_ICQ;
367   else if (g_ascii_strcasecmp(type, "yahoo") == 0)
368     ret = R_NET_ADDRESS_IRC_YAHOO;
369   else if (g_ascii_strcasecmp(type, "msn") == 0)
370     ret = R_NET_ADDRESS_IRC_MSN;
371 
372   return ret;
373 }
374 
375 
376 /**
377  * r_net_address_check
378  * @net: a #RNetAddress
379  * @property: an #RNetAddress's property
380  * @value: the property's value (if set)
381  *
382  * check if the given property is set.
383  *
384  * returns: %FALSE if the property is %NULL, otherwise it return %TRUE and
385  * the content of the property is copied into value
386  **/
387 gboolean
r_net_address_check(RNetAddress * net,const gchar * property,gchar ** value)388 r_net_address_check (RNetAddress* net, const gchar* property, gchar** value)
389 {
390   gchar* tmp;
391 
392   g_return_val_if_fail(IS_R_NET_ADDRESS(net), FALSE);
393 
394   g_object_get(net, property, &tmp, NULL);
395 
396   if (tmp)
397     {
398       if (value)
399 	*value = tmp;
400 
401       return TRUE;
402     }
403 
404   return FALSE;
405 }
406