1 /*
2  * netstatus-util.c: Utility methods.
3  *
4  * Copyright (C) 2003 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * 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
19  * 02111-1307, USA.
20 
21  * Authors:
22  *      Mark McLoughlin <mark@skynet.ie>
23  */
24 
25 #include <config.h>
26 
27 #include "netstatus-util.h"
28 
29 #include <glib-object.h>
30 #include <glib/gi18n.h>
31 #include <string.h>
32 
33 #ifdef G_ENABLE_DEBUG
34 NetstatusDebugFlags _netstatus_debug_flags = NETSTATUS_DEBUG_NONE;
35 
36 void
netstatus_setup_debug_flags(void)37 netstatus_setup_debug_flags (void)
38 {
39   const char       *env_str;
40   static GDebugKey  debug_keys [] =
41     {
42       { "polling", NETSTATUS_DEBUG_POLLING }
43     };
44 
45   env_str = g_getenv ("NETSTATUS_DEBUG");
46 
47   if (env_str)
48     _netstatus_debug_flags |= g_parse_debug_string (env_str,
49 						    debug_keys,
50 						    G_N_ELEMENTS (debug_keys));
51 }
52 #endif /* G_ENABLE_DEBUG */
53 
54 GQuark
netstatus_error_quark(void)55 netstatus_error_quark (void)
56 {
57   static GQuark error_quark = 0;
58 
59   if (error_quark == 0)
60     error_quark = g_quark_from_static_string ("netstatus-error-quark");
61 
62   return error_quark;
63 }
64 
65 /* Remove this if there is ever a boxed type for
66  * GError with glib as standard.
67  */
68 GType
netstatus_g_error_get_type(void)69 netstatus_g_error_get_type (void)
70 {
71   static GType type_id = 0;
72 
73   if (type_id == 0)
74     type_id = g_boxed_type_register_static ("NetstatusGError",
75 					    (GBoxedCopyFunc) g_error_copy,
76 					    (GBoxedFreeFunc) g_error_free);
77 
78   return type_id;
79 }
80 
81 static NetstatusStats *
netstatus_stats_copy(NetstatusStats * stats)82 netstatus_stats_copy (NetstatusStats *stats)
83 {
84   return (NetstatusStats *)g_memdup (stats, sizeof (NetstatusStats));
85 }
86 
87 static void
netstatus_stats_free(NetstatusStats * stats)88 netstatus_stats_free (NetstatusStats *stats)
89 {
90   g_free (stats);
91 }
92 
93 GType
netstatus_stats_get_type(void)94 netstatus_stats_get_type (void)
95 {
96   static GType type_id = 0;
97 
98   if (type_id == 0)
99     type_id = g_boxed_type_register_static ("NetstatusStats",
100 					    (GBoxedCopyFunc) netstatus_stats_copy,
101 					    (GBoxedFreeFunc) netstatus_stats_free);
102 
103   return type_id;
104 }
105 
106 /* Adopt an existing error into our domain.
107  */
108 void
netstatus_adopt_error(GError * error,NetstatusError code)109 netstatus_adopt_error (GError         *error,
110 		       NetstatusError  code)
111 {
112   g_return_if_fail (error != NULL);
113 
114   error->domain = NETSTATUS_ERROR;
115   error->code   = code;
116 }
117 
118 void
netstatus_connect_signal_while_alive(gpointer object,const char * detailed_signal,GCallback func,gpointer func_data,gpointer alive_object)119 netstatus_connect_signal_while_alive (gpointer    object,
120 				      const char *detailed_signal,
121 				      GCallback   func,
122 				      gpointer    func_data,
123 				      gpointer    alive_object)
124 {
125   GClosure *closure;
126   GType     type;
127   guint     signal_id = 0;
128   GQuark    detail = 0;
129 
130   type = G_OBJECT_TYPE (object);
131 
132   if (!g_signal_parse_name (detailed_signal, type, &signal_id, &detail, FALSE))
133     {
134       g_warning (G_STRLOC ": unable to parse signal \"%s\" for type \"%s\"",
135 		 detailed_signal, g_type_name (type));
136       return;
137     }
138 
139   closure = g_cclosure_new (func, func_data, NULL);
140   g_object_watch_closure (G_OBJECT (alive_object), closure);
141   g_signal_connect_closure_by_id (object, signal_id, detail, closure, FALSE);
142 }
143 
144 const char *
netstatus_get_state_string(NetstatusState state)145 netstatus_get_state_string (NetstatusState state)
146 {
147   char *retval = NULL;
148 
149   switch (state)
150     {
151     case NETSTATUS_STATE_DISCONNECTED:
152       retval = _("Disconnected");
153       break;
154     case NETSTATUS_STATE_IDLE:
155       retval = _("Idle");
156       break;
157     case NETSTATUS_STATE_TX:
158       retval = _("Sending");
159       break;
160     case NETSTATUS_STATE_RX:
161       retval = _("Receiving");
162       break;
163     case NETSTATUS_STATE_TX_RX:
164       retval = _("Sending/Receiving");
165       break;
166     case NETSTATUS_STATE_ERROR:
167       retval = _("Error");
168       break;
169     default:
170       g_assert_not_reached ();
171       break;
172     }
173 
174   return retval;
175 }
176 
177 GList *
netstatus_list_insert_unique(GList * list,char * str)178 netstatus_list_insert_unique (GList *list,
179 			      char  *str)
180 {
181   GList *l;
182 
183   g_return_val_if_fail (str != NULL, list);
184 
185   for (l = list; l; l = l->next)
186     if (!strcmp (str, l->data))
187       return list;
188 
189   return g_list_prepend (list, str);
190 }
191