Use g_list_free_full instead of manual iterations
[pidgin-git.git] / libpurple / media / candidate.c
bloba9e34a91e80180b15244304ffcc89e11bdf9889a
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
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 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "candidate.h"
24 /**
25 * PurpleMediaCandidate:
27 * An opaque structure representing a network candidate (IP Address and port
28 * pair).
30 struct _PurpleMediaCandidate
32 GObject parent;
35 typedef struct
37 gchar *foundation;
38 guint component_id;
39 gchar *ip;
40 guint16 port;
41 gchar *base_ip;
42 guint16 base_port;
43 PurpleMediaNetworkProtocol proto;
44 guint32 priority;
45 PurpleMediaCandidateType type;
46 gchar *username;
47 gchar *password;
48 guint ttl;
49 } PurpleMediaCandidatePrivate;
51 enum {
52 PROP_CANDIDATE_0,
53 PROP_FOUNDATION,
54 PROP_COMPONENT_ID,
55 PROP_IP,
56 PROP_PORT,
57 PROP_BASE_IP,
58 PROP_BASE_PORT,
59 PROP_PROTOCOL,
60 PROP_PRIORITY,
61 PROP_TYPE,
62 PROP_USERNAME,
63 PROP_PASSWORD,
64 PROP_TTL,
67 G_DEFINE_TYPE_WITH_PRIVATE(PurpleMediaCandidate, purple_media_candidate,
68 G_TYPE_OBJECT);
70 static void
71 purple_media_candidate_init(PurpleMediaCandidate *info)
73 PurpleMediaCandidatePrivate *priv =
74 purple_media_candidate_get_instance_private(info);
75 priv->foundation = NULL;
76 priv->component_id = 0;
77 priv->ip = NULL;
78 priv->port = 0;
79 priv->base_ip = NULL;
80 priv->proto = PURPLE_MEDIA_NETWORK_PROTOCOL_UDP;
81 priv->priority = 0;
82 priv->type = PURPLE_MEDIA_CANDIDATE_TYPE_HOST;
83 priv->username = NULL;
84 priv->password = NULL;
85 priv->ttl = 0;
88 static void
89 purple_media_candidate_finalize(GObject *info)
91 PurpleMediaCandidatePrivate *priv =
92 purple_media_candidate_get_instance_private(
93 PURPLE_MEDIA_CANDIDATE(info));
95 g_free(priv->foundation);
96 g_free(priv->ip);
97 g_free(priv->base_ip);
98 g_free(priv->username);
99 g_free(priv->password);
101 G_OBJECT_CLASS(purple_media_candidate_parent_class)->finalize(info);
104 static void
105 purple_media_candidate_set_property (GObject *object, guint prop_id,
106 const GValue *value, GParamSpec *pspec)
108 PurpleMediaCandidatePrivate *priv;
109 g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object));
111 priv = purple_media_candidate_get_instance_private(
112 PURPLE_MEDIA_CANDIDATE(object));
114 switch (prop_id) {
115 case PROP_FOUNDATION:
116 g_free(priv->foundation);
117 priv->foundation = g_value_dup_string(value);
118 break;
119 case PROP_COMPONENT_ID:
120 priv->component_id = g_value_get_uint(value);
121 break;
122 case PROP_IP:
123 g_free(priv->ip);
124 priv->ip = g_value_dup_string(value);
125 break;
126 case PROP_PORT:
127 priv->port = g_value_get_uint(value);
128 break;
129 case PROP_BASE_IP:
130 g_free(priv->base_ip);
131 priv->base_ip = g_value_dup_string(value);
132 break;
133 case PROP_BASE_PORT:
134 priv->base_port = g_value_get_uint(value);
135 break;
136 case PROP_PROTOCOL:
137 priv->proto = g_value_get_enum(value);
138 break;
139 case PROP_PRIORITY:
140 priv->priority = g_value_get_uint(value);
141 break;
142 case PROP_TYPE:
143 priv->type = g_value_get_enum(value);
144 break;
145 case PROP_USERNAME:
146 g_free(priv->username);
147 priv->username = g_value_dup_string(value);
148 break;
149 case PROP_PASSWORD:
150 g_free(priv->password);
151 priv->password = g_value_dup_string(value);
152 break;
153 case PROP_TTL:
154 priv->ttl = g_value_get_uint(value);
155 break;
156 default:
157 G_OBJECT_WARN_INVALID_PROPERTY_ID(
158 object, prop_id, pspec);
159 break;
163 static void
164 purple_media_candidate_get_property (GObject *object, guint prop_id,
165 GValue *value, GParamSpec *pspec)
167 PurpleMediaCandidatePrivate *priv;
168 g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object));
170 priv = purple_media_candidate_get_instance_private(
171 PURPLE_MEDIA_CANDIDATE(object));
173 switch (prop_id) {
174 case PROP_FOUNDATION:
175 g_value_set_string(value, priv->foundation);
176 break;
177 case PROP_COMPONENT_ID:
178 g_value_set_uint(value, priv->component_id);
179 break;
180 case PROP_IP:
181 g_value_set_string(value, priv->ip);
182 break;
183 case PROP_PORT:
184 g_value_set_uint(value, priv->port);
185 break;
186 case PROP_BASE_IP:
187 g_value_set_string(value, priv->base_ip);
188 break;
189 case PROP_BASE_PORT:
190 g_value_set_uint(value, priv->base_port);
191 break;
192 case PROP_PROTOCOL:
193 g_value_set_enum(value, priv->proto);
194 break;
195 case PROP_PRIORITY:
196 g_value_set_uint(value, priv->priority);
197 break;
198 case PROP_TYPE:
199 g_value_set_enum(value, priv->type);
200 break;
201 case PROP_USERNAME:
202 g_value_set_string(value, priv->username);
203 break;
204 case PROP_PASSWORD:
205 g_value_set_string(value, priv->password);
206 break;
207 case PROP_TTL:
208 g_value_set_uint(value, priv->ttl);
209 break;
210 default:
211 G_OBJECT_WARN_INVALID_PROPERTY_ID(
212 object, prop_id, pspec);
213 break;
217 static void
218 purple_media_candidate_class_init(PurpleMediaCandidateClass *klass)
220 GObjectClass *gobject_class = (GObjectClass*)klass;
222 gobject_class->finalize = purple_media_candidate_finalize;
223 gobject_class->set_property = purple_media_candidate_set_property;
224 gobject_class->get_property = purple_media_candidate_get_property;
226 g_object_class_install_property(gobject_class, PROP_FOUNDATION,
227 g_param_spec_string("foundation",
228 "Foundation",
229 "The foundation of the candidate.",
230 NULL,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property(gobject_class, PROP_COMPONENT_ID,
234 g_param_spec_uint("component-id",
235 "Component ID",
236 "The component id of the candidate.",
237 0, G_MAXUINT, 0,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property(gobject_class, PROP_IP,
241 g_param_spec_string("ip",
242 "IP Address",
243 "The IP address of the candidate.",
244 NULL,
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 g_object_class_install_property(gobject_class, PROP_PORT,
248 g_param_spec_uint("port",
249 "Port",
250 "The port of the candidate.",
251 0, G_MAXUINT16, 0,
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property(gobject_class, PROP_BASE_IP,
255 g_param_spec_string("base-ip",
256 "Base IP",
257 "The internal IP address of the candidate.",
258 NULL,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property(gobject_class, PROP_BASE_PORT,
262 g_param_spec_uint("base-port",
263 "Base Port",
264 "The internal port of the candidate.",
265 0, G_MAXUINT16, 0,
266 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268 g_object_class_install_property(gobject_class, PROP_PROTOCOL,
269 g_param_spec_enum("protocol",
270 "Protocol",
271 "The protocol of the candidate.",
272 PURPLE_TYPE_MEDIA_NETWORK_PROTOCOL,
273 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 g_object_class_install_property(gobject_class, PROP_PRIORITY,
277 g_param_spec_uint("priority",
278 "Priority",
279 "The priority of the candidate.",
280 0, G_MAXUINT32, 0,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 g_object_class_install_property(gobject_class, PROP_TYPE,
284 g_param_spec_enum("type",
285 "Type",
286 "The type of the candidate.",
287 PURPLE_TYPE_MEDIA_CANDIDATE_TYPE,
288 PURPLE_MEDIA_CANDIDATE_TYPE_HOST,
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property(gobject_class, PROP_USERNAME,
292 g_param_spec_string("username",
293 "Username",
294 "The username used to connect to the candidate.",
295 NULL,
296 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property(gobject_class, PROP_PASSWORD,
299 g_param_spec_string("password",
300 "Password",
301 "The password use to connect to the candidate.",
302 NULL,
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305 g_object_class_install_property(gobject_class, PROP_TTL,
306 g_param_spec_uint("ttl",
307 "TTL",
308 "The TTL of the candidate.",
309 0, G_MAXUINT, 0,
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 PurpleMediaCandidate *
314 purple_media_candidate_new(const gchar *foundation, guint component_id,
315 PurpleMediaCandidateType type,
316 PurpleMediaNetworkProtocol proto,
317 const gchar *ip, guint port)
319 return g_object_new(PURPLE_TYPE_MEDIA_CANDIDATE,
320 "foundation", foundation,
321 "component-id", component_id,
322 "type", type,
323 "protocol", proto,
324 "ip", ip,
325 "port", port, NULL);
328 PurpleMediaCandidate *
329 purple_media_candidate_copy(PurpleMediaCandidate *candidate)
331 PurpleMediaCandidatePrivate *priv;
332 PurpleMediaCandidate *new_candidate;
334 if (candidate == NULL)
335 return NULL;
337 priv = purple_media_candidate_get_instance_private(candidate);
339 new_candidate = purple_media_candidate_new(priv->foundation,
340 priv->component_id, priv->type, priv->proto,
341 priv->ip, priv->port);
342 g_object_set(new_candidate,
343 "base-ip", priv->base_ip,
344 "base-port", priv->base_port,
345 "priority", priv->priority,
346 "username", priv->username,
347 "password", priv->password,
348 "ttl", priv->ttl, NULL);
349 return new_candidate;
352 GList *
353 purple_media_candidate_list_copy(GList *candidates)
355 GList *new_list = NULL;
357 for (; candidates; candidates = g_list_next(candidates)) {
358 new_list = g_list_prepend(new_list,
359 purple_media_candidate_copy(candidates->data));
362 new_list = g_list_reverse(new_list);
363 return new_list;
366 void
367 purple_media_candidate_list_free(GList *candidates)
369 g_list_free_full(candidates, g_object_unref);
372 gchar *
373 purple_media_candidate_get_foundation(PurpleMediaCandidate *candidate)
375 gchar *foundation;
376 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
377 g_object_get(candidate, "foundation", &foundation, NULL);
378 return foundation;
381 guint
382 purple_media_candidate_get_component_id(PurpleMediaCandidate *candidate)
384 guint component_id;
385 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
386 g_object_get(candidate, "component-id", &component_id, NULL);
387 return component_id;
390 gchar *
391 purple_media_candidate_get_ip(PurpleMediaCandidate *candidate)
393 gchar *ip;
394 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
395 g_object_get(candidate, "ip", &ip, NULL);
396 return ip;
399 guint16
400 purple_media_candidate_get_port(PurpleMediaCandidate *candidate)
402 guint port;
403 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
404 g_object_get(candidate, "port", &port, NULL);
405 return port;
408 gchar *
409 purple_media_candidate_get_base_ip(PurpleMediaCandidate *candidate)
411 gchar *base_ip;
412 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
413 g_object_get(candidate, "base-ip", &base_ip, NULL);
414 return base_ip;
417 guint16
418 purple_media_candidate_get_base_port(PurpleMediaCandidate *candidate)
420 guint base_port;
421 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
422 g_object_get(candidate, "base_port", &base_port, NULL);
423 return base_port;
426 PurpleMediaNetworkProtocol
427 purple_media_candidate_get_protocol(PurpleMediaCandidate *candidate)
429 PurpleMediaNetworkProtocol protocol;
430 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate),
431 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP);
432 g_object_get(candidate, "protocol", &protocol, NULL);
433 return protocol;
436 guint32
437 purple_media_candidate_get_priority(PurpleMediaCandidate *candidate)
439 guint priority;
440 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
441 g_object_get(candidate, "priority", &priority, NULL);
442 return priority;
445 PurpleMediaCandidateType
446 purple_media_candidate_get_candidate_type(PurpleMediaCandidate *candidate)
448 PurpleMediaCandidateType type;
449 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate),
450 PURPLE_MEDIA_CANDIDATE_TYPE_HOST);
451 g_object_get(candidate, "type", &type, NULL);
452 return type;
455 gchar *
456 purple_media_candidate_get_username(PurpleMediaCandidate *candidate)
458 gchar *username;
459 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
460 g_object_get(candidate, "username", &username, NULL);
461 return username;
464 gchar *
465 purple_media_candidate_get_password(PurpleMediaCandidate *candidate)
467 gchar *password;
468 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
469 g_object_get(candidate, "password", &password, NULL);
470 return password;
473 guint
474 purple_media_candidate_get_ttl(PurpleMediaCandidate *candidate)
476 guint ttl;
477 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
478 g_object_get(candidate, "ttl", &ttl, NULL);
479 return ttl;