]> sjero.net Git - linphone/blob - coreapi/address.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[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                 ms_error("Cannot create LinphoneAddress, bad uri [%s]",uri);
40                 return NULL;
41         }
42         return from;
43 }
44
45 /**
46  * Clones a LinphoneAddress object.
47 **/
48 LinphoneAddress * linphone_address_clone(const LinphoneAddress *uri){
49         osip_from_t *ret=NULL;
50         osip_from_clone(uri,&ret);
51         return ret;
52 }
53
54 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
55
56 /**
57  * Returns the address scheme, normally "sip".
58 **/
59 const char *linphone_address_get_scheme(const LinphoneAddress *u){
60         return null_if_empty(u->url->scheme);
61 }
62
63 /**
64  * Returns the display name.
65 **/
66 const char *linphone_address_get_display_name(const LinphoneAddress* u){
67         return null_if_empty(u->displayname);
68 }
69
70 /**
71  * Returns the username.
72 **/
73 const char *linphone_address_get_username(const LinphoneAddress *u){
74         return null_if_empty(u->url->username);
75 }
76
77 /**
78  * Returns the domain name.
79 **/
80 const char *linphone_address_get_domain(const LinphoneAddress *u){
81         return null_if_empty(u->url->host);
82 }
83
84 /**
85  * Sets the display name.
86 **/
87 void linphone_address_set_display_name(LinphoneAddress *u, const char *display_name){
88         if (u->displayname!=NULL){
89                 osip_free(u->displayname);
90                 u->displayname=NULL;
91         }
92         if (display_name!=NULL)
93                 u->displayname=osip_strdup(display_name);
94 }
95
96 /**
97  * Sets the username.
98 **/
99 void linphone_address_set_username(LinphoneAddress *uri, const char *username){
100         if (uri->url->username!=NULL){
101                 osip_free(uri->url->username);
102                 uri->url->username=NULL;
103         }
104         if (username)
105                 uri->url->username=osip_strdup(username);
106 }
107
108 /**
109  * Sets the domain.
110 **/
111 void linphone_address_set_domain(LinphoneAddress *uri, const char *host){
112         if (uri->url->host!=NULL){
113                 osip_free(uri->url->host);
114                 uri->url->host=NULL;
115         }
116         if (host)
117                 uri->url->host=osip_strdup(host);
118 }
119
120 /**
121  * Sets the port number.
122 **/
123 void linphone_address_set_port(LinphoneAddress *uri, const char *port){
124         if (uri->url->port!=NULL){
125                 osip_free(uri->url->port);
126                 uri->url->port=NULL;
127         }
128         if (port)
129                 uri->url->port=osip_strdup(port);
130 }
131
132 /**
133  * Sets the port number.
134 **/
135 void linphone_address_set_port_int(LinphoneAddress *uri, int port){
136         char tmp[12];
137         if (port==5060){
138                 /*this is the default, special case to leave the port field blank*/
139                 linphone_address_set_port(uri,NULL);
140                 return;
141         }
142         snprintf(tmp,sizeof(tmp),"%i",port);
143         linphone_address_set_port(uri,tmp);
144 }
145
146 /**
147  * Removes address's tags and uri headers so that it is displayable to the user.
148 **/
149 void linphone_address_clean(LinphoneAddress *uri){
150         osip_generic_param_freelist(&uri->gen_params);
151 }
152
153 /**
154  * Returns the address as a string.
155  * The returned char * must be freed by the application. Use ms_free().
156 **/
157 char *linphone_address_as_string(const LinphoneAddress *u){
158         char *tmp,*ret;
159         osip_from_to_str(u,&tmp);
160         ret=ms_strdup(tmp);
161         osip_free(tmp);
162         return ret;
163 }
164
165 /**
166  * Returns the SIP uri only as a string, that is display name is removed.
167  * The returned char * must be freed by the application. Use ms_free().
168 **/
169 char *linphone_address_as_string_uri_only(const LinphoneAddress *u){
170         char *tmp=NULL,*ret;
171         osip_uri_to_str(u->url,&tmp);
172         ret=ms_strdup(tmp);
173         osip_free(tmp);
174         return ret;
175 }
176
177 /**
178  * Destroys a LinphoneAddress object.
179 **/
180 void linphone_address_destroy(LinphoneAddress *u){
181         osip_from_free(u);
182 }
183
184
185 /** @} */