2 * Copyright (C) 2005 Novell Inc.
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"
29 static int nntp_decode (CamelAddress *, const char *raw);
30 static char * nntp_encode (CamelAddress *);
31 static int nntp_cat (CamelAddress *dest, const CamelAddress *source);
32 static void nntp_remove (CamelAddress *, int index);
34 static void camel_nntp_address_class_init (CamelNNTPAddressClass *klass);
35 static void camel_nntp_address_init (CamelNNTPAddress *obj);
37 static CamelAddressClass *camel_nntp_address_parent;
45 camel_nntp_address_class_init(CamelNNTPAddressClass *klass)
47 CamelAddressClass *address = (CamelAddressClass *) klass;
49 camel_nntp_address_parent = CAMEL_ADDRESS_CLASS(camel_type_get_global_classfuncs(camel_address_get_type()));
51 address->decode = nntp_decode;
52 address->encode = nntp_encode;
53 address->unformat = nntp_decode;
54 address->format = nntp_encode;
55 address->remove = nntp_remove;
56 address->cat = nntp_cat;
60 camel_nntp_address_init(CamelNNTPAddress *obj)
65 camel_nntp_address_get_type(void)
67 static CamelType type = CAMEL_INVALID_TYPE;
69 if (type == CAMEL_INVALID_TYPE) {
70 type = camel_type_register(camel_address_get_type(), "CamelNNTPAddress",
71 sizeof (CamelNNTPAddress),
72 sizeof (CamelNNTPAddressClass),
73 (CamelObjectClassInitFunc) camel_nntp_address_class_init,
75 (CamelObjectInitFunc) camel_nntp_address_init,
82 /* since newsgropus are 7bit ascii, decode/unformat are the same */
84 nntp_decode(CamelAddress *a, const char *raw)
86 struct _camel_header_newsgroup *ha, *n;
87 int count = a->addresses->len;
89 ha = camel_header_newsgroups_decode(raw);
91 for (n = ha;n;n=n->next)
92 camel_nntp_address_add((CamelNNTPAddress *)a, n->newsgroup);
93 camel_header_newsgroups_free(ha);
96 return a->addresses->len - count;
99 /* since newsgropus are 7bit ascii, encode/format are the same */
101 nntp_encode(CamelAddress *a)
107 if (a->addresses->len == 0)
110 out = g_string_new("");
112 for (i = 0;i < a->addresses->len; i++) {
114 g_string_append(out, ", ");
116 g_string_append(out, g_ptr_array_index(a->addresses, i));
120 g_string_free(out, FALSE);
126 nntp_cat (CamelAddress *dest, const CamelAddress *source)
130 g_assert(CAMEL_IS_NNTP_ADDRESS(source));
132 for (i=0;i<source->addresses->len;i++)
133 camel_nntp_address_add((CamelNNTPAddress *)dest, g_ptr_array_index(source->addresses, i));
139 nntp_remove (CamelAddress *a, int index)
141 if (index < 0 || index >= a->addresses->len)
144 g_free(g_ptr_array_index(a->addresses, index));
145 g_ptr_array_remove_index(a->addresses, index);
149 * camel_nntp_address_new:
151 * Create a new CamelNNTPAddress object.
153 * Return value: A new CamelNNTPAddress object.
156 camel_nntp_address_new (void)
158 CamelNNTPAddress *new = CAMEL_NNTP_ADDRESS(camel_object_new(camel_nntp_address_get_type()));
163 * camel_nntp_address_add:
164 * @a: nntp address object
167 * Add a new nntp address to the address object. Duplicates are not added twice.
169 * Return value: Index of added entry, or existing matching entry.
172 camel_nntp_address_add (CamelNNTPAddress *a, const char *name)
176 g_assert(CAMEL_IS_NNTP_ADDRESS(a));
178 index = ((CamelAddress *)a)->addresses->len;
179 for (i=0;i<index;i++)
180 if (!strcmp(g_ptr_array_index(((CamelAddress *)a)->addresses, i), name))
183 g_ptr_array_add(((CamelAddress *)a)->addresses, g_strdup(name));
189 * camel_nntp_address_get:
190 * @a: nntp address object
191 * @index: address's array index
192 * @addressp: Holder for the returned address, or NULL, if not required.
194 * Get the address at @index.
196 * Return value: TRUE if such an address exists, or FALSE otherwise.
199 camel_nntp_address_get (const CamelNNTPAddress *a, int index, const char **namep)
201 g_assert(CAMEL_IS_NNTP_ADDRESS(a));
203 if (index < 0 || index >= ((CamelAddress *)a)->addresses->len)
207 *namep = g_ptr_array_index( ((CamelAddress *)a)->addresses, index);