Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[platform/upstream/evolution-data-server.git] / camel / camel-address.c
1 /*
2  *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
3  *
4  *  Authors: Michael Zucchi <notzed@ximian.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
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 GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "camel-address.h"
22
23 G_DEFINE_TYPE (CamelAddress, camel_address, CAMEL_TYPE_OBJECT)
24
25 static void
26 address_finalize (GObject *object)
27 {
28         CamelAddress *address = CAMEL_ADDRESS (object);
29
30         camel_address_remove (address, -1);
31         g_ptr_array_free (address->addresses, TRUE);
32
33         /* Chain up to parent's finalize() method. */
34         G_OBJECT_CLASS (camel_address_parent_class)->finalize (object);
35 }
36
37 static void
38 camel_address_class_init (CamelAddressClass *class)
39 {
40         GObjectClass *object_class;
41
42         object_class = G_OBJECT_CLASS (class);
43         object_class->finalize = address_finalize;
44 }
45
46 static void
47 camel_address_init (CamelAddress *address)
48 {
49         address->addresses = g_ptr_array_new ();
50 }
51
52 /**
53  * camel_address_new:
54  *
55  * Create a new #CamelAddress object.
56  *
57  * Returns: a new #CamelAddress object
58  **/
59 CamelAddress *
60 camel_address_new (void)
61 {
62         return g_object_new (CAMEL_TYPE_ADDRESS, NULL);
63 }
64
65 /**
66  * camel_address_new_clone:
67  * @addr: a #CamelAddress object
68  *
69  * Clone an existing address type.
70  *
71  * Returns: the cloned address
72  **/
73 CamelAddress *
74 camel_address_new_clone (CamelAddress *addr)
75 {
76         CamelAddress *new;
77
78         new = g_object_new (G_OBJECT_TYPE (addr), NULL);
79         camel_address_cat (new, addr);
80
81         return new;
82 }
83
84 /**
85  * camel_address_length:
86  * @addr: a #CamelAddress object
87  *
88  * Get the number of addresses stored in the address @addr.
89  *
90  * Returns: the number of addresses contained in @addr
91  **/
92 gint
93 camel_address_length (CamelAddress *addr)
94 {
95         return addr->addresses->len;
96 }
97
98 /**
99  * camel_address_decode:
100  * @addr: a #CamelAddress object
101  * @raw: raw address description
102  *
103  * Construct a new address from a raw address field.
104  *
105  * Returns: the number of addresses parsed or %-1 on fail
106  **/
107 gint
108 camel_address_decode (CamelAddress *addr,
109                       const gchar *raw)
110 {
111         CamelAddressClass *class;
112
113         g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), -1);
114
115         class = CAMEL_ADDRESS_GET_CLASS (addr);
116         g_return_val_if_fail (class->decode != NULL, -1);
117
118         return class->decode (addr, raw);
119 }
120
121 /**
122  * camel_address_encode:
123  * @addr: a #CamelAddress object
124  *
125  * Encode an address in a format suitable for a raw header.
126  *
127  * Returns: the encoded address
128  **/
129 gchar *
130 camel_address_encode (CamelAddress *addr)
131 {
132         CamelAddressClass *class;
133
134         g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), NULL);
135
136         class = CAMEL_ADDRESS_GET_CLASS (addr);
137         g_return_val_if_fail (class->encode != NULL, NULL);
138
139         return class->encode (addr);
140 }
141
142 /**
143  * camel_address_unformat:
144  * @addr: a #CamelAddress object
145  * @raw: raw address description
146  *
147  * Attempt to convert a previously formatted and/or edited
148  * address back into internal form.
149  *
150  * Returns: the number of addresses parsed or %-1 on fail
151  **/
152 gint
153 camel_address_unformat (CamelAddress *addr,
154                         const gchar *raw)
155 {
156         CamelAddressClass *class;
157
158         g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), -1);
159
160         class = CAMEL_ADDRESS_GET_CLASS (addr);
161         g_return_val_if_fail (class->unformat != NULL, -1);
162
163         return class->unformat (addr, raw);
164 }
165
166 /**
167  * camel_address_format:
168  * @addr: a #CamelAddress object
169  *
170  * Format an address in a format suitable for display.
171  *
172  * Returns: a newly allocated string containing the formatted addresses
173  **/
174 gchar *
175 camel_address_format (CamelAddress *addr)
176 {
177         CamelAddressClass *class;
178
179         g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), NULL);
180
181         class = CAMEL_ADDRESS_GET_CLASS (addr);
182         g_return_val_if_fail (class->format != NULL, NULL);
183
184         return class->format (addr);
185 }
186
187 /**
188  * camel_address_cat:
189  * @dest: destination #CamelAddress object
190  * @source: source #CamelAddress object
191  *
192  * Concatenate one address onto another. The addresses must
193  * be of the same type.
194  *
195  * Returns: the number of addresses concatenated
196  **/
197 gint
198 camel_address_cat (CamelAddress *dest,
199                    CamelAddress *source)
200 {
201         CamelAddressClass *class;
202
203         g_return_val_if_fail (CAMEL_IS_ADDRESS (dest), -1);
204         g_return_val_if_fail (CAMEL_IS_ADDRESS (source), -1);
205
206         class = CAMEL_ADDRESS_GET_CLASS (dest);
207         g_return_val_if_fail (class->cat != NULL, -1);
208
209         return class->cat (dest, source);
210 }
211
212 /**
213  * camel_address_copy:
214  * @dest: destination #CamelAddress object
215  * @source: source #CamelAddress object
216  *
217  * Copy the contents of one address into another.
218  *
219  * Returns: the number of addresses copied
220  **/
221 gint
222 camel_address_copy (CamelAddress *dest,
223                     CamelAddress *source)
224 {
225         g_return_val_if_fail (CAMEL_IS_ADDRESS (dest), -1);
226         g_return_val_if_fail (CAMEL_IS_ADDRESS (source), -1);
227
228         camel_address_remove (dest, -1);
229         return camel_address_cat (dest, source);
230 }
231
232 /**
233  * camel_address_remove:
234  * @addr: a #CamelAddress object
235  * @index: The address to remove, use %-1 to remove all address.
236  *
237  * Remove an address by index, or all addresses.
238  **/
239 void
240 camel_address_remove (CamelAddress *addr,
241                       gint index)
242 {
243         CamelAddressClass *class;
244
245         g_return_if_fail (CAMEL_IS_ADDRESS (addr));
246
247         class = CAMEL_ADDRESS_GET_CLASS (addr);
248         g_return_if_fail (class->remove != NULL);
249
250         if (index == -1) {
251                 for (index = addr->addresses->len; index>-1; index--)
252                         class->remove (addr, index);
253         } else
254                 class->remove (addr, index);
255 }