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.
24 #include "camel-mime-utils.h"
25 #include "camel-nntp-address.h"
34 G_DEFINE_TYPE (CamelNNTPAddress, camel_nntp_address, CAMEL_TYPE_ADDRESS)
36 /* since newsgropus are 7bit ascii, decode/unformat are the same */
38 nntp_address_decode (CamelAddress *address,
41 struct _camel_header_newsgroup *ha, *n;
42 gint count = address->addresses->len;
44 ha = camel_header_newsgroups_decode (raw);
46 for (n = ha; n; n = n->next)
47 camel_nntp_address_add (
48 CAMEL_NNTP_ADDRESS (address), n->newsgroup);
49 camel_header_newsgroups_free (ha);
52 return address->addresses->len - count;
55 /* since newsgropus are 7bit ascii, encode/format are the same */
57 nntp_address_encode (CamelAddress *address)
63 if (address->addresses->len == 0)
66 out = g_string_new ("");
68 for (i = 0; i < address->addresses->len; i++) {
70 g_string_append (out, ", ");
72 g_string_append (out, g_ptr_array_index (address->addresses, i));
76 g_string_free (out, FALSE);
82 nntp_address_cat (CamelAddress *dest,
87 g_assert (CAMEL_IS_NNTP_ADDRESS (source));
89 for (ii = 0; ii < source->addresses->len; ii++)
90 camel_nntp_address_add (
91 CAMEL_NNTP_ADDRESS (dest),
92 g_ptr_array_index (source->addresses, ii));
98 nntp_address_remove (CamelAddress *address,
101 if (index < 0 || index >= address->addresses->len)
104 g_free (g_ptr_array_index (address->addresses, index));
105 g_ptr_array_remove_index (address->addresses, index);
109 camel_nntp_address_class_init (CamelNNTPAddressClass *class)
111 CamelAddressClass *address_class;
113 address_class = CAMEL_ADDRESS_CLASS (class);
114 address_class->decode = nntp_address_decode;
115 address_class->encode = nntp_address_encode;
116 address_class->unformat = nntp_address_decode;
117 address_class->format = nntp_address_encode;
118 address_class->remove = nntp_address_remove;
119 address_class->cat = nntp_address_cat;
123 camel_nntp_address_init (CamelNNTPAddress *nntp_address)
128 * camel_nntp_address_new:
130 * Create a new CamelNNTPAddress object.
132 * Returns: A new CamelNNTPAddress object.
135 camel_nntp_address_new (void)
137 return g_object_new (CAMEL_TYPE_NNTP_ADDRESS, NULL);
141 * camel_nntp_address_add:
142 * @a: nntp address object
145 * Add a new nntp address to the address object. Duplicates are not added twice.
147 * Returns: Index of added entry, or existing matching entry.
150 camel_nntp_address_add (CamelNNTPAddress *a,
155 g_assert (CAMEL_IS_NNTP_ADDRESS (a));
157 index = ((CamelAddress *) a)->addresses->len;
158 for (i = 0; i < index; i++)
159 if (!strcmp (g_ptr_array_index (((CamelAddress *) a)->addresses, i), name))
162 g_ptr_array_add (((CamelAddress *) a)->addresses, g_strdup (name));
168 * camel_nntp_address_get:
169 * @a: nntp address object
170 * @index: address's array index
171 * @namep: Holder for the returned address, or NULL, if not required.
173 * Get the address at @index.
175 * Returns: TRUE if such an address exists, or FALSE otherwise.
178 camel_nntp_address_get (CamelNNTPAddress *a,
182 g_assert (CAMEL_IS_NNTP_ADDRESS (a));
184 if (index < 0 || index >= ((CamelAddress *) a)->addresses->len)
188 *namep = g_ptr_array_index( ((CamelAddress *)a)->addresses, index);