summary file read/write routines.
[platform/upstream/evolution-data-server.git] / camel / camel-recipient.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-recipient.h : handle recipients (addresses) and recipiemt lists */
3
4 /* 
5  *
6  * Author : 
7  *  Bertrand Guiheneuf <bertrand@helixcode.com>
8  *
9  * Copyright 1999, 2000 HelixCode (http://www.helixcode.com) .
10  *
11  * This program is free software; you can redistribute it and/or 
12  * modify it under the terms of the GNU General Public License as 
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24  * USA
25  */
26
27
28 #include "glib.h"
29 #include "hash-table-utils.h"
30 #include "camel-recipient.h"
31
32
33 /**
34  * camel_recipient_table_new: Create a new recipient table object
35  * 
36  * 
37  * creates a new recipient table object. A recipient table 
38  * objects merely associates a recipient list (GList) to
39  * recipient types (as for example "To", "Cc" for mime
40  * maile messages 
41  *
42  * Return value: the newly created recipient table object
43  **/
44 CamelRecipientTable *
45 camel_recipient_table_new ()
46 {
47         CamelRecipientTable *recipient_table;
48
49         recipient_table = g_new0 (CamelRecipientTable, 1);
50         recipient_table->recipient_hash_table = g_hash_table_new (g_strcase_hash, g_strcase_equal);
51         recipient_table->ref_count = 1;
52         return recipient_table;
53 }
54
55
56 /**
57  * camel_recipient_table_ref: add a reference to a recipient table object
58  * @recipient_table: the recipient table object
59  * 
60  * Add a reference to a recipient table object. 
61  **/
62 void 
63 camel_recipient_table_ref (CamelRecipientTable *recipient_table)
64 {
65         g_return_if_fail (recipient_table);
66         recipient_table->ref_count += 1;
67 }
68
69
70
71 static void 
72 _free_recipient_list (gpointer key, gpointer value, gpointer user_data)
73 {
74         GList *recipient_list = (GList *)value;
75         gchar *recipient_name = (gchar *)key;
76
77         while (recipient_list) {
78                 g_free (recipient_list->data);
79                 recipient_list = recipient_list->next;
80         }
81
82         g_free (recipient_name);
83         
84 }
85
86 /**
87  * camel_recipient_table_free: Free a recipient table object
88  * @recipient_table: the recipient table object to free
89  * 
90  * Free a recipient table object. All recipients and recipient
91  * are freed. 
92  **/
93 void 
94 camel_recipient_table_free (CamelRecipientTable *recipient_table)
95 {
96         if (!recipient_table) return;
97
98         /* free each recipient list */
99         g_hash_table_foreach (recipient_table->recipient_hash_table, _free_recipient_list, NULL);
100         g_hash_table_destroy (recipient_table->recipient_hash_table);
101 }
102
103
104
105
106 /**
107  * camel_recipient_table_unref: Removes a reference to a recipient table object
108  * @recipient_table: the recipient table object
109  * 
110  * Removes a reference to the reference count of a recipient
111  * table object. If the reference count falls to zero, the 
112  * recipient table object is freed.
113  *
114  **/
115 void 
116 camel_recipient_table_unref (CamelRecipientTable *recipient_table)
117 {
118         if (!recipient_table) return;
119
120         recipient_table->ref_count -= 1;
121         if (recipient_table->ref_count <1)              
122                         camel_recipient_table_free (recipient_table);
123                 
124 }
125
126
127
128
129 /**
130  * camel_recipient_table_add: Add a recipient to a recipient table object.
131  * @recipient_table: The recipient table object
132  * @recipient_type: Recipient type string
133  * @recipient:  The recipient to add
134  * 
135  * Add a recipient to a recipient table object. 
136  * The recipient is appended to the list of recipients
137  * of type @recipient_type. @recipient and @recipient_type
138  * are duplicated if necessary and freed when 
139  * camel_recipient_table_free is called.
140  **/
141 void 
142 camel_recipient_table_add (CamelRecipientTable *recipient_table, 
143                            const gchar *recipient_type, 
144                            const gchar *recipient)
145 {
146         GList *recipients_list;
147         GList *existent_list;
148         
149         /* see if there is already a list for this recipient type */
150         existent_list = (GList *)g_hash_table_lookup (recipient_table->recipient_hash_table, recipient_type);
151         
152         
153         /* append the new recipient to the recipient list
154            if the existent_list is NULL, then a new GList is
155            automagically created */     
156         recipients_list = g_list_append (existent_list, (gpointer)g_strdup (recipient));
157         
158         if (!existent_list) /* if there was no recipient of this type create the section */
159                 g_hash_table_insert (recipient_table->recipient_hash_table, g_strdup (recipient_type), recipients_list);
160         
161  
162 }
163
164
165 /**
166  * camel_recipient_table_add_list: Add a full list of recipients to a recipient table.
167  * @recipient_table: The recipient table object
168  * @recipient_type:  Recipient type string.
169  * @recipient_list: Recipient list to add.
170  * 
171  * Add a full list of recipients to a recipient table.
172  * The new recipients are appended at the end of the
173  * existing recipient list corresponding to @recipient_type.
174  * Be careful, the list is used as is, and its element
175  * will be freed by camel_recipient_table_unref
176  **/
177 void 
178 camel_recipient_table_add_list (CamelRecipientTable *recipient_table, 
179                                 const gchar *recipient_type, 
180                                 GList *recipient_list)
181 {
182         GList *recipients_list;
183         GList *existent_list;
184         
185         /* see if there is already a list for this recipient type */
186         existent_list = (GList *)g_hash_table_lookup (recipient_table->recipient_hash_table, recipient_type);
187         
188         
189         if (existent_list) 
190                 g_list_concat (existent_list, recipient_list);
191         else 
192                 g_hash_table_insert (recipient_table->recipient_hash_table, g_strdup (recipient_type), recipients_list);                
193 }
194
195
196
197
198 /**
199  * camel_recipient_table_remove: Remove a recipient from a recipient table.
200  * @recipient_table: The recipient table object
201  * @recipient_type: Recipient type string.
202  * @recipient: Recipient to remove from the table
203  * 
204  * Remove a recipient from a recipient table. The recipient is
205  * only removed from the recipient list corresponding to 
206  * @recipient_type. The removed recipient is freed.
207  *
208  **/
209 void
210 camel_recipient_table_remove (CamelRecipientTable *recipient_table,
211                               const gchar *recipient_type,
212                               const gchar *recipient) 
213 {
214         GList *recipients_list;
215         GList *new_recipients_list;
216         GList *old_element;
217         gchar *old_recipient_type;
218         
219         /* if the recipient type section does not exist, do nothing */
220         if (! g_hash_table_lookup_extended (recipient_table->recipient_hash_table, 
221                                             recipient_type, 
222                                             (gpointer)&(old_recipient_type),
223                                             (gpointer)&(recipients_list)) 
224             ) return;
225         
226         /* look for the recipient to remove */
227         /* g_list_find_custom , use gpointer instead of gconstpointer */
228         old_element = g_list_find_custom (recipients_list, (gpointer)recipient, g_strcase_equal);
229         if (old_element) {
230                 /* if recipient exists, remove it */
231                 new_recipients_list =  g_list_remove_link (recipients_list, old_element);
232                 
233                 /* if glist head has changed, fix up hash table */
234                 if (new_recipients_list != recipients_list)
235                         g_hash_table_insert (recipient_table->recipient_hash_table, old_recipient_type, new_recipients_list);
236                 
237                 g_free( (gchar *)(old_element->data));
238                 g_list_free_1 (old_element);
239         }
240 }
241
242
243
244 /**
245  * camel_recipient_table_get: Get the recipients corresponding to a recipient type.
246  * @recipient_table: The recipient table object
247  * @recipient_type: Recipient type string.
248  * 
249  * Return the list of recipients corresponding to 
250  * @recipient_type. The returned list is not a copy 
251  * of the internal list used by the recipient table object
252  * but the list itself. It thus must not be freed.
253  * The recipients it contains can be modified.
254  * 
255  * Return value: The list of recipients.
256  **/
257 const GList *
258 camel_recipient_table_get (CamelRecipientTable *recipient_table,
259                            const gchar *recipient_type)
260 {
261         return (const GList *)g_hash_table_lookup (recipient_table->recipient_hash_table, recipient_type);
262 }
263
264
265
266
267 /**
268  * camel_recipient_foreach_recipient_type: Runs a function over over all recipients type lists.
269  * @recipient_table: The recipient table object.
270  * @func: The function to run.
271  * @user_data: User data to pass to the function.
272  * 
273  * Runs a function over over all recipients type lists.
274  **/
275 void
276 camel_recipient_foreach_recipient_type (CamelRecipientTable *recipient_table,
277                                         CRLFunc func,
278                                         gpointer user_data)
279 {
280         g_hash_table_foreach (recipient_table->recipient_hash_table, (GHFunc)func, user_data);
281 }