1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-recipient.h : handle recipients (addresses) and recipiemt lists */
7 * Bertrand Guiheneuf <bertrand@helixcode.com>
9 * Copyright 1999, 2000 HelixCode (http://www.helixcode.com) .
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.
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.
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
29 #include "hash-table-utils.h"
30 #include "camel-recipient.h"
34 * camel_recipient_table_new: Create a new recipient table object
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
42 * Return value: the newly created recipient table object
45 camel_recipient_table_new ()
47 CamelRecipientTable *recipient_table;
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;
57 * camel_recipient_table_ref: add a reference to a recipient table object
58 * @recipient_table: the recipient table object
60 * Add a reference to a recipient table object.
63 camel_recipient_table_ref (CamelRecipientTable *recipient_table)
65 g_return_if_fail (recipient_table);
66 recipient_table->ref_count += 1;
72 _free_recipient_list (gpointer key, gpointer value, gpointer user_data)
74 GList *recipient_list = (GList *)value;
75 gchar *recipient_name = (gchar *)key;
77 while (recipient_list) {
78 g_free (recipient_list->data);
79 recipient_list = recipient_list->next;
82 g_free (recipient_name);
87 * camel_recipient_table_free: Free a recipient table object
88 * @recipient_table: the recipient table object to free
90 * Free a recipient table object. All recipients and recipient
94 camel_recipient_table_free (CamelRecipientTable *recipient_table)
96 if (!recipient_table) return;
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);
107 * camel_recipient_table_unref: Removes a reference to a recipient table object
108 * @recipient_table: the recipient table object
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.
116 camel_recipient_table_unref (CamelRecipientTable *recipient_table)
118 if (!recipient_table) return;
120 recipient_table->ref_count -= 1;
121 if (recipient_table->ref_count <1)
122 camel_recipient_table_free (recipient_table);
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
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.
142 camel_recipient_table_add (CamelRecipientTable *recipient_table,
143 const gchar *recipient_type,
144 const gchar *recipient)
146 GList *recipients_list;
147 GList *existent_list;
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);
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));
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);
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.
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
178 camel_recipient_table_add_list (CamelRecipientTable *recipient_table,
179 const gchar *recipient_type,
180 GList *recipient_list)
182 GList *recipients_list;
183 GList *existent_list;
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);
190 g_list_concat (existent_list, recipient_list);
192 g_hash_table_insert (recipient_table->recipient_hash_table, g_strdup (recipient_type), recipients_list);
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
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.
210 camel_recipient_table_remove (CamelRecipientTable *recipient_table,
211 const gchar *recipient_type,
212 const gchar *recipient)
214 GList *recipients_list;
215 GList *new_recipients_list;
217 gchar *old_recipient_type;
219 /* if the recipient type section does not exist, do nothing */
220 if (! g_hash_table_lookup_extended (recipient_table->recipient_hash_table,
222 (gpointer)&(old_recipient_type),
223 (gpointer)&(recipients_list))
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);
230 /* if recipient exists, remove it */
231 new_recipients_list = g_list_remove_link (recipients_list, old_element);
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);
237 g_free( (gchar *)(old_element->data));
238 g_list_free_1 (old_element);
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.
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.
255 * Return value: The list of recipients.
258 camel_recipient_table_get (CamelRecipientTable *recipient_table,
259 const gchar *recipient_type)
261 return (const GList *)g_hash_table_lookup (recipient_table->recipient_hash_table, recipient_type);
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.
273 * Runs a function over over all recipients type lists.
276 camel_recipient_foreach_recipient_type (CamelRecipientTable *recipient_table,
280 g_hash_table_foreach (recipient_table->recipient_hash_table, (GHFunc)func, user_data);