Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[platform/upstream/evolution-data-server.git] / camel / camel-nntp-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 <stdio.h>
22 #include <string.h>
23
24 #include "camel-mime-utils.h"
25 #include "camel-nntp-address.h"
26
27 #define d(x)
28
29 struct _address {
30         gchar *name;
31         gchar *address;
32 };
33
34 G_DEFINE_TYPE (CamelNNTPAddress, camel_nntp_address, CAMEL_TYPE_ADDRESS)
35
36 /* since newsgropus are 7bit ascii, decode/unformat are the same */
37 static gint
38 nntp_address_decode (CamelAddress *address,
39                      const gchar *raw)
40 {
41         struct _camel_header_newsgroup *ha, *n;
42         gint count = address->addresses->len;
43
44         ha = camel_header_newsgroups_decode (raw);
45         if (ha) {
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);
50         }
51
52         return address->addresses->len - count;
53 }
54
55 /* since newsgropus are 7bit ascii, encode/format are the same */
56 static gchar *
57 nntp_address_encode (CamelAddress *address)
58 {
59         gint i;
60         GString *out;
61         gchar *ret;
62
63         if (address->addresses->len == 0)
64                 return NULL;
65
66         out = g_string_new ("");
67
68         for (i = 0; i < address->addresses->len; i++) {
69                 if (i != 0)
70                         g_string_append (out, ", ");
71
72                 g_string_append (out, g_ptr_array_index (address->addresses, i));
73         }
74
75         ret = out->str;
76         g_string_free (out, FALSE);
77
78         return ret;
79 }
80
81 static gint
82 nntp_address_cat (CamelAddress *dest,
83                   CamelAddress *source)
84 {
85         gint ii;
86
87         g_assert (CAMEL_IS_NNTP_ADDRESS (source));
88
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));
93
94         return ii;
95 }
96
97 static void
98 nntp_address_remove (CamelAddress *address,
99                      gint index)
100 {
101         if (index < 0 || index >= address->addresses->len)
102                 return;
103
104         g_free (g_ptr_array_index (address->addresses, index));
105         g_ptr_array_remove_index (address->addresses, index);
106 }
107
108 static void
109 camel_nntp_address_class_init (CamelNNTPAddressClass *class)
110 {
111         CamelAddressClass *address_class;
112
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;
120 }
121
122 static void
123 camel_nntp_address_init (CamelNNTPAddress *nntp_address)
124 {
125 }
126
127 /**
128  * camel_nntp_address_new:
129  *
130  * Create a new CamelNNTPAddress object.
131  *
132  * Returns: A new CamelNNTPAddress object.
133  **/
134 CamelNNTPAddress *
135 camel_nntp_address_new (void)
136 {
137         return g_object_new (CAMEL_TYPE_NNTP_ADDRESS, NULL);
138 }
139
140 /**
141  * camel_nntp_address_add:
142  * @a: nntp address object
143  * @name:
144  *
145  * Add a new nntp address to the address object.  Duplicates are not added twice.
146  *
147  * Returns: Index of added entry, or existing matching entry.
148  **/
149 gint
150 camel_nntp_address_add (CamelNNTPAddress *a,
151                         const gchar *name)
152 {
153         gint index, i;
154
155         g_assert (CAMEL_IS_NNTP_ADDRESS (a));
156
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))
160                         return i;
161
162         g_ptr_array_add (((CamelAddress *) a)->addresses, g_strdup (name));
163
164         return index;
165 }
166
167 /**
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.
172  *
173  * Get the address at @index.
174  *
175  * Returns: TRUE if such an address exists, or FALSE otherwise.
176  **/
177 gboolean
178 camel_nntp_address_get (CamelNNTPAddress *a,
179                         gint index,
180                         const gchar **namep)
181 {
182         g_assert (CAMEL_IS_NNTP_ADDRESS (a));
183
184         if (index < 0 || index >= ((CamelAddress *) a)->addresses->len)
185                 return FALSE;
186
187         if (namep)
188                 *namep = g_ptr_array_index( ((CamelAddress *)a)->addresses, index);
189
190         return TRUE;
191 }