2 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4 * Authors: Michael Zucchi <notzed@ximian.com>
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.
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.
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.
21 #include "camel-address.h"
23 G_DEFINE_TYPE (CamelAddress, camel_address, CAMEL_TYPE_OBJECT)
26 address_finalize (GObject *object)
28 CamelAddress *address = CAMEL_ADDRESS (object);
30 camel_address_remove (address, -1);
31 g_ptr_array_free (address->addresses, TRUE);
33 /* Chain up to parent's finalize() method. */
34 G_OBJECT_CLASS (camel_address_parent_class)->finalize (object);
38 camel_address_class_init (CamelAddressClass *class)
40 GObjectClass *object_class;
42 object_class = G_OBJECT_CLASS (class);
43 object_class->finalize = address_finalize;
47 camel_address_init (CamelAddress *address)
49 address->addresses = g_ptr_array_new ();
55 * Create a new #CamelAddress object.
57 * Returns: a new #CamelAddress object
60 camel_address_new (void)
62 return g_object_new (CAMEL_TYPE_ADDRESS, NULL);
66 * camel_address_new_clone:
67 * @addr: a #CamelAddress object
69 * Clone an existing address type.
71 * Returns: the cloned address
74 camel_address_new_clone (CamelAddress *addr)
78 new = g_object_new (G_OBJECT_TYPE (addr), NULL);
79 camel_address_cat (new, addr);
85 * camel_address_length:
86 * @addr: a #CamelAddress object
88 * Get the number of addresses stored in the address @addr.
90 * Returns: the number of addresses contained in @addr
93 camel_address_length (CamelAddress *addr)
95 return addr->addresses->len;
99 * camel_address_decode:
100 * @addr: a #CamelAddress object
101 * @raw: raw address description
103 * Construct a new address from a raw address field.
105 * Returns: the number of addresses parsed or %-1 on fail
108 camel_address_decode (CamelAddress *addr,
111 CamelAddressClass *class;
113 g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), -1);
115 class = CAMEL_ADDRESS_GET_CLASS (addr);
116 g_return_val_if_fail (class->decode != NULL, -1);
118 return class->decode (addr, raw);
122 * camel_address_encode:
123 * @addr: a #CamelAddress object
125 * Encode an address in a format suitable for a raw header.
127 * Returns: the encoded address
130 camel_address_encode (CamelAddress *addr)
132 CamelAddressClass *class;
134 g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), NULL);
136 class = CAMEL_ADDRESS_GET_CLASS (addr);
137 g_return_val_if_fail (class->encode != NULL, NULL);
139 return class->encode (addr);
143 * camel_address_unformat:
144 * @addr: a #CamelAddress object
145 * @raw: raw address description
147 * Attempt to convert a previously formatted and/or edited
148 * address back into internal form.
150 * Returns: the number of addresses parsed or %-1 on fail
153 camel_address_unformat (CamelAddress *addr,
156 CamelAddressClass *class;
158 g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), -1);
160 class = CAMEL_ADDRESS_GET_CLASS (addr);
161 g_return_val_if_fail (class->unformat != NULL, -1);
163 return class->unformat (addr, raw);
167 * camel_address_format:
168 * @addr: a #CamelAddress object
170 * Format an address in a format suitable for display.
172 * Returns: a newly allocated string containing the formatted addresses
175 camel_address_format (CamelAddress *addr)
177 CamelAddressClass *class;
179 g_return_val_if_fail (CAMEL_IS_ADDRESS (addr), NULL);
181 class = CAMEL_ADDRESS_GET_CLASS (addr);
182 g_return_val_if_fail (class->format != NULL, NULL);
184 return class->format (addr);
189 * @dest: destination #CamelAddress object
190 * @source: source #CamelAddress object
192 * Concatenate one address onto another. The addresses must
193 * be of the same type.
195 * Returns: the number of addresses concatenated
198 camel_address_cat (CamelAddress *dest,
199 CamelAddress *source)
201 CamelAddressClass *class;
203 g_return_val_if_fail (CAMEL_IS_ADDRESS (dest), -1);
204 g_return_val_if_fail (CAMEL_IS_ADDRESS (source), -1);
206 class = CAMEL_ADDRESS_GET_CLASS (dest);
207 g_return_val_if_fail (class->cat != NULL, -1);
209 return class->cat (dest, source);
213 * camel_address_copy:
214 * @dest: destination #CamelAddress object
215 * @source: source #CamelAddress object
217 * Copy the contents of one address into another.
219 * Returns: the number of addresses copied
222 camel_address_copy (CamelAddress *dest,
223 CamelAddress *source)
225 g_return_val_if_fail (CAMEL_IS_ADDRESS (dest), -1);
226 g_return_val_if_fail (CAMEL_IS_ADDRESS (source), -1);
228 camel_address_remove (dest, -1);
229 return camel_address_cat (dest, source);
233 * camel_address_remove:
234 * @addr: a #CamelAddress object
235 * @index: The address to remove, use %-1 to remove all address.
237 * Remove an address by index, or all addresses.
240 camel_address_remove (CamelAddress *addr,
243 CamelAddressClass *class;
245 g_return_if_fail (CAMEL_IS_ADDRESS (addr));
247 class = CAMEL_ADDRESS_GET_CLASS (addr);
248 g_return_if_fail (class->remove != NULL);
251 for (index = addr->addresses->len; index>-1; index--)
252 class->remove (addr, index);
254 class->remove (addr, index);