]> sjero.net Git - linphone/blob - coreapi/address.c
Merge branch 'master' into dev_sal
[linphone] / coreapi / address.c
1 /*
2 linphone
3 Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphonecore.h"
21 #include "lpconfig.h"
22 #include "private.h"
23 #include <eXosip2/eXosip.h>
24
25 /**
26  * @addtogroup linphone_address
27  * @{
28 **/
29
30 /**
31  * Constructs a LinphoneAddress object by parsing the user supplied address,
32  * given as a string.
33 **/
34 LinphoneAddress * linphone_address_new(const char *uri){
35         osip_from_t *from;
36         osip_from_init(&from);
37         if (osip_from_parse(from,uri)!=0){
38                 osip_from_free(from);
39                 return NULL;
40         }
41         return from;
42 }
43
44 /**
45  * Clones a LinphoneAddress object.
46 **/
47 LinphoneAddress * linphone_address_clone(const LinphoneAddress *uri){
48         osip_from_t *ret=NULL;
49         osip_from_clone(uri,&ret);
50         return ret;
51 }
52
53 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
54
55 /**
56  * Returns the address scheme, normally "sip".
57 **/
58 const char *linphone_address_get_scheme(const LinphoneAddress *u){
59         return null_if_empty(u->url->scheme);
60 }
61
62 /**
63  * Returns the display name.
64 **/
65 const char *linphone_address_get_display_name(const LinphoneAddress* u){
66         return null_if_empty(u->displayname);
67 }
68
69 /**
70  * Returns the username.
71 **/
72 const char *linphone_address_get_username(const LinphoneAddress *u){
73         return null_if_empty(u->url->username);
74 }
75
76 /**
77  * Returns the domain name.
78 **/
79 const char *linphone_address_get_domain(const LinphoneAddress *u){
80         return null_if_empty(u->url->host);
81 }
82
83 /**
84  * Sets the display name.
85 **/
86 void linphone_address_set_display_name(LinphoneAddress *u, const char *display_name){
87         if (u->displayname!=NULL){
88                 osip_free(u->displayname);
89                 u->displayname=NULL;
90         }
91         if (display_name!=NULL)
92                 u->displayname=osip_strdup(display_name);
93 }
94
95 /**
96  * Sets the username.
97 **/
98 void linphone_address_set_username(LinphoneAddress *uri, const char *username){
99         if (uri->url->username!=NULL){
100                 osip_free(uri->url->username);
101                 uri->url->username=NULL;
102         }
103         if (username)
104                 uri->url->username=osip_strdup(username);
105 }
106
107 /**
108  * Sets the domain.
109 **/
110 void linphone_address_set_domain(LinphoneAddress *uri, const char *host){
111         if (uri->url->host!=NULL){
112                 osip_free(uri->url->host);
113                 uri->url->host=NULL;
114         }
115         if (host)
116                 uri->url->host=osip_strdup(host);
117 }
118
119 /**
120  * Sets the port number.
121 **/
122 void linphone_address_set_port(LinphoneAddress *uri, const char *port){
123         if (uri->url->port!=NULL){
124                 osip_free(uri->url->port);
125                 uri->url->port=NULL;
126         }
127         if (port)
128                 uri->url->port=osip_strdup(port);
129 }
130
131 /**
132  * Sets the port number.
133 **/
134 void linphone_address_set_port_int(LinphoneAddress *uri, int port){
135         char tmp[12];
136         if (port==5060){
137                 /*this is the default, special case to leave the port field blank*/
138                 linphone_address_set_port(uri,NULL);
139                 return;
140         }
141         snprintf(tmp,sizeof(tmp),"%i",port);
142         linphone_address_set_port(uri,tmp);
143 }
144
145 /**
146  * Removes address's tags and uri headers so that it is displayable to the user.
147 **/
148 void linphone_address_clean(LinphoneAddress *uri){
149         osip_generic_param_freelist(&uri->gen_params);
150 }
151
152 /**
153  * Returns the address as a string.
154  * The returned char * must be freed by the application. Use ms_free().
155 **/
156 char *linphone_address_as_string(const LinphoneAddress *u){
157         char *tmp,*ret;
158         osip_from_to_str(u,&tmp);
159         ret=ms_strdup(tmp);
160         osip_free(tmp);
161         return ret;
162 }
163
164 /**
165  * Returns the SIP uri only as a string, that is display name is removed.
166  * The returned char * must be freed by the application. Use ms_free().
167 **/
168 char *linphone_address_as_string_uri_only(const LinphoneAddress *u){
169         char *tmp=NULL,*ret;
170         osip_uri_to_str(u->url,&tmp);
171         ret=ms_strdup(tmp);
172         osip_free(tmp);
173         return ret;
174 }
175
176 /**
177  * Destroys a LinphoneAddress object.
178 **/
179 void linphone_address_destroy(LinphoneAddress *u){
180         osip_from_free(u);
181 }
182
183
184 /** @} */