These changes are reuired to make the Windows installer build.
[pidgin-git.git] / libpurple / media / candidate.c
blobabed4a010a5cda13b63a4a936c394a5d2f464bd6
1 /**
2 * @file candidate.c Candidate for Media API
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #include "candidate.h"
29 /** @copydoc _PurpleMediaCandidateClass */
30 typedef struct _PurpleMediaCandidateClass PurpleMediaCandidateClass;
31 /** @copydoc _PurpleMediaCandidatePrivate */
32 typedef struct _PurpleMediaCandidatePrivate PurpleMediaCandidatePrivate;
34 #define PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(obj) \
35 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
36 PURPLE_TYPE_MEDIA_CANDIDATE, \
37 PurpleMediaCandidatePrivate))
40 struct _PurpleMediaCandidateClass
42 GObjectClass parent_class;
45 struct _PurpleMediaCandidate
47 GObject parent;
50 G_DEFINE_TYPE(PurpleMediaCandidate, purple_media_candidate, G_TYPE_OBJECT);
52 struct _PurpleMediaCandidatePrivate
54 gchar *foundation;
55 guint component_id;
56 gchar *ip;
57 guint16 port;
58 gchar *base_ip;
59 guint16 base_port;
60 PurpleMediaNetworkProtocol proto;
61 guint32 priority;
62 PurpleMediaCandidateType type;
63 gchar *username;
64 gchar *password;
65 guint ttl;
68 enum {
69 PROP_CANDIDATE_0,
70 PROP_FOUNDATION,
71 PROP_COMPONENT_ID,
72 PROP_IP,
73 PROP_PORT,
74 PROP_BASE_IP,
75 PROP_BASE_PORT,
76 PROP_PROTOCOL,
77 PROP_PRIORITY,
78 PROP_TYPE,
79 PROP_USERNAME,
80 PROP_PASSWORD,
81 PROP_TTL,
84 static void
85 purple_media_candidate_init(PurpleMediaCandidate *info)
87 PurpleMediaCandidatePrivate *priv =
88 PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(info);
89 priv->foundation = NULL;
90 priv->component_id = 0;
91 priv->ip = NULL;
92 priv->port = 0;
93 priv->base_ip = NULL;
94 priv->proto = PURPLE_MEDIA_NETWORK_PROTOCOL_UDP;
95 priv->priority = 0;
96 priv->type = PURPLE_MEDIA_CANDIDATE_TYPE_HOST;
97 priv->username = NULL;
98 priv->password = NULL;
99 priv->ttl = 0;
102 static void
103 purple_media_candidate_finalize(GObject *info)
105 PurpleMediaCandidatePrivate *priv =
106 PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(info);
108 g_free(priv->foundation);
109 g_free(priv->ip);
110 g_free(priv->base_ip);
111 g_free(priv->username);
112 g_free(priv->password);
115 static void
116 purple_media_candidate_set_property (GObject *object, guint prop_id,
117 const GValue *value, GParamSpec *pspec)
119 PurpleMediaCandidatePrivate *priv;
120 g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object));
122 priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(object);
124 switch (prop_id) {
125 case PROP_FOUNDATION:
126 g_free(priv->foundation);
127 priv->foundation = g_value_dup_string(value);
128 break;
129 case PROP_COMPONENT_ID:
130 priv->component_id = g_value_get_uint(value);
131 break;
132 case PROP_IP:
133 g_free(priv->ip);
134 priv->ip = g_value_dup_string(value);
135 break;
136 case PROP_PORT:
137 priv->port = g_value_get_uint(value);
138 break;
139 case PROP_BASE_IP:
140 g_free(priv->base_ip);
141 priv->base_ip = g_value_dup_string(value);
142 break;
143 case PROP_BASE_PORT:
144 priv->base_port = g_value_get_uint(value);
145 break;
146 case PROP_PROTOCOL:
147 priv->proto = g_value_get_enum(value);
148 break;
149 case PROP_PRIORITY:
150 priv->priority = g_value_get_uint(value);
151 break;
152 case PROP_TYPE:
153 priv->type = g_value_get_enum(value);
154 break;
155 case PROP_USERNAME:
156 g_free(priv->username);
157 priv->username = g_value_dup_string(value);
158 break;
159 case PROP_PASSWORD:
160 g_free(priv->password);
161 priv->password = g_value_dup_string(value);
162 break;
163 case PROP_TTL:
164 priv->ttl = g_value_get_uint(value);
165 break;
166 default:
167 G_OBJECT_WARN_INVALID_PROPERTY_ID(
168 object, prop_id, pspec);
169 break;
173 static void
174 purple_media_candidate_get_property (GObject *object, guint prop_id,
175 GValue *value, GParamSpec *pspec)
177 PurpleMediaCandidatePrivate *priv;
178 g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object));
180 priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(object);
182 switch (prop_id) {
183 case PROP_FOUNDATION:
184 g_value_set_string(value, priv->foundation);
185 break;
186 case PROP_COMPONENT_ID:
187 g_value_set_uint(value, priv->component_id);
188 break;
189 case PROP_IP:
190 g_value_set_string(value, priv->ip);
191 break;
192 case PROP_PORT:
193 g_value_set_uint(value, priv->port);
194 break;
195 case PROP_BASE_IP:
196 g_value_set_string(value, priv->base_ip);
197 break;
198 case PROP_BASE_PORT:
199 g_value_set_uint(value, priv->base_port);
200 break;
201 case PROP_PROTOCOL:
202 g_value_set_enum(value, priv->proto);
203 break;
204 case PROP_PRIORITY:
205 g_value_set_uint(value, priv->priority);
206 break;
207 case PROP_TYPE:
208 g_value_set_enum(value, priv->type);
209 break;
210 case PROP_USERNAME:
211 g_value_set_string(value, priv->username);
212 break;
213 case PROP_PASSWORD:
214 g_value_set_string(value, priv->password);
215 break;
216 case PROP_TTL:
217 g_value_set_uint(value, priv->ttl);
218 break;
219 default:
220 G_OBJECT_WARN_INVALID_PROPERTY_ID(
221 object, prop_id, pspec);
222 break;
226 static void
227 purple_media_candidate_class_init(PurpleMediaCandidateClass *klass)
229 GObjectClass *gobject_class = (GObjectClass*)klass;
231 gobject_class->finalize = purple_media_candidate_finalize;
232 gobject_class->set_property = purple_media_candidate_set_property;
233 gobject_class->get_property = purple_media_candidate_get_property;
235 g_object_class_install_property(gobject_class, PROP_FOUNDATION,
236 g_param_spec_string("foundation",
237 "Foundation",
238 "The foundation of the candidate.",
239 NULL,
240 G_PARAM_READWRITE));
242 g_object_class_install_property(gobject_class, PROP_COMPONENT_ID,
243 g_param_spec_uint("component-id",
244 "Component ID",
245 "The component id of the candidate.",
246 0, G_MAXUINT, 0,
247 G_PARAM_READWRITE));
249 g_object_class_install_property(gobject_class, PROP_IP,
250 g_param_spec_string("ip",
251 "IP Address",
252 "The IP address of the candidate.",
253 NULL,
254 G_PARAM_READWRITE));
256 g_object_class_install_property(gobject_class, PROP_PORT,
257 g_param_spec_uint("port",
258 "Port",
259 "The port of the candidate.",
260 0, G_MAXUINT16, 0,
261 G_PARAM_READWRITE));
263 g_object_class_install_property(gobject_class, PROP_BASE_IP,
264 g_param_spec_string("base-ip",
265 "Base IP",
266 "The internal IP address of the candidate.",
267 NULL,
268 G_PARAM_READWRITE));
270 g_object_class_install_property(gobject_class, PROP_BASE_PORT,
271 g_param_spec_uint("base-port",
272 "Base Port",
273 "The internal port of the candidate.",
274 0, G_MAXUINT16, 0,
275 G_PARAM_READWRITE));
277 g_object_class_install_property(gobject_class, PROP_PROTOCOL,
278 g_param_spec_enum("protocol",
279 "Protocol",
280 "The protocol of the candidate.",
281 PURPLE_TYPE_MEDIA_NETWORK_PROTOCOL,
282 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP,
283 G_PARAM_READWRITE));
285 g_object_class_install_property(gobject_class, PROP_PRIORITY,
286 g_param_spec_uint("priority",
287 "Priority",
288 "The priority of the candidate.",
289 0, G_MAXUINT32, 0,
290 G_PARAM_READWRITE));
292 g_object_class_install_property(gobject_class, PROP_TYPE,
293 g_param_spec_enum("type",
294 "Type",
295 "The type of the candidate.",
296 PURPLE_TYPE_MEDIA_CANDIDATE_TYPE,
297 PURPLE_MEDIA_CANDIDATE_TYPE_HOST,
298 G_PARAM_READWRITE));
300 g_object_class_install_property(gobject_class, PROP_USERNAME,
301 g_param_spec_string("username",
302 "Username",
303 "The username used to connect to the candidate.",
304 NULL,
305 G_PARAM_READWRITE));
307 g_object_class_install_property(gobject_class, PROP_PASSWORD,
308 g_param_spec_string("password",
309 "Password",
310 "The password use to connect to the candidate.",
311 NULL,
312 G_PARAM_READWRITE));
314 g_object_class_install_property(gobject_class, PROP_TTL,
315 g_param_spec_uint("ttl",
316 "TTL",
317 "The TTL of the candidate.",
318 0, G_MAXUINT, 0,
319 G_PARAM_READWRITE));
321 g_type_class_add_private(klass, sizeof(PurpleMediaCandidatePrivate));
324 PurpleMediaCandidate *
325 purple_media_candidate_new(const gchar *foundation, guint component_id,
326 PurpleMediaCandidateType type,
327 PurpleMediaNetworkProtocol proto,
328 const gchar *ip, guint port)
330 return g_object_new(PURPLE_TYPE_MEDIA_CANDIDATE,
331 "foundation", foundation,
332 "component-id", component_id,
333 "type", type,
334 "protocol", proto,
335 "ip", ip,
336 "port", port, NULL);
339 PurpleMediaCandidate *
340 purple_media_candidate_copy(PurpleMediaCandidate *candidate)
342 PurpleMediaCandidatePrivate *priv;
343 PurpleMediaCandidate *new_candidate;
345 if (candidate == NULL)
346 return NULL;
348 priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(candidate);
350 new_candidate = purple_media_candidate_new(priv->foundation,
351 priv->component_id, priv->type, priv->proto,
352 priv->ip, priv->port);
353 g_object_set(new_candidate,
354 "base-ip", priv->base_ip,
355 "base-port", priv->base_port,
356 "priority", priv->priority,
357 "username", priv->username,
358 "password", priv->password,
359 "ttl", priv->ttl, NULL);
360 return new_candidate;
363 GList *
364 purple_media_candidate_list_copy(GList *candidates)
366 GList *new_list = NULL;
368 for (; candidates; candidates = g_list_next(candidates)) {
369 new_list = g_list_prepend(new_list,
370 purple_media_candidate_copy(candidates->data));
373 new_list = g_list_reverse(new_list);
374 return new_list;
377 void
378 purple_media_candidate_list_free(GList *candidates)
380 for (; candidates; candidates =
381 g_list_delete_link(candidates, candidates)) {
382 g_object_unref(candidates->data);
386 gchar *
387 purple_media_candidate_get_foundation(PurpleMediaCandidate *candidate)
389 gchar *foundation;
390 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
391 g_object_get(candidate, "foundation", &foundation, NULL);
392 return foundation;
395 guint
396 purple_media_candidate_get_component_id(PurpleMediaCandidate *candidate)
398 guint component_id;
399 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
400 g_object_get(candidate, "component-id", &component_id, NULL);
401 return component_id;
404 gchar *
405 purple_media_candidate_get_ip(PurpleMediaCandidate *candidate)
407 gchar *ip;
408 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
409 g_object_get(candidate, "ip", &ip, NULL);
410 return ip;
413 guint16
414 purple_media_candidate_get_port(PurpleMediaCandidate *candidate)
416 guint port;
417 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
418 g_object_get(candidate, "port", &port, NULL);
419 return port;
422 gchar *
423 purple_media_candidate_get_base_ip(PurpleMediaCandidate *candidate)
425 gchar *base_ip;
426 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
427 g_object_get(candidate, "base-ip", &base_ip, NULL);
428 return base_ip;
431 guint16
432 purple_media_candidate_get_base_port(PurpleMediaCandidate *candidate)
434 guint base_port;
435 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
436 g_object_get(candidate, "base_port", &base_port, NULL);
437 return base_port;
440 PurpleMediaNetworkProtocol
441 purple_media_candidate_get_protocol(PurpleMediaCandidate *candidate)
443 PurpleMediaNetworkProtocol protocol;
444 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate),
445 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP);
446 g_object_get(candidate, "protocol", &protocol, NULL);
447 return protocol;
450 guint32
451 purple_media_candidate_get_priority(PurpleMediaCandidate *candidate)
453 guint priority;
454 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
455 g_object_get(candidate, "priority", &priority, NULL);
456 return priority;
459 PurpleMediaCandidateType
460 purple_media_candidate_get_candidate_type(PurpleMediaCandidate *candidate)
462 PurpleMediaCandidateType type;
463 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate),
464 PURPLE_MEDIA_CANDIDATE_TYPE_HOST);
465 g_object_get(candidate, "type", &type, NULL);
466 return type;
469 gchar *
470 purple_media_candidate_get_username(PurpleMediaCandidate *candidate)
472 gchar *username;
473 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
474 g_object_get(candidate, "username", &username, NULL);
475 return username;
478 gchar *
479 purple_media_candidate_get_password(PurpleMediaCandidate *candidate)
481 gchar *password;
482 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL);
483 g_object_get(candidate, "password", &password, NULL);
484 return password;
487 guint
488 purple_media_candidate_get_ttl(PurpleMediaCandidate *candidate)
490 guint ttl;
491 g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0);
492 g_object_get(candidate, "ttl", &ttl, NULL);
493 return ttl;