2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Address list item selection objects.
27 #include "addrselect.h"
28 #include "addressitem.h"
31 static AddrSelectItem *addrselect_create_item ( AddrItemObject *aio );
34 * Create a selection record from an address cache item.
35 * \param aio Item object.
36 * \return Address select item.
38 static AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
39 AddrSelectItem *item = NULL;
42 item = g_new0( AddrSelectItem, 1 );
43 item->objectType = aio->type;
44 item->addressItem = aio;
45 item->uid = g_strdup( aio->uid );
52 * Create a selection record from an address object (in tree node).
53 * \param obj Address object.
54 * \return Address select item.
56 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
57 AddrSelectItem *item = NULL;
60 item = g_new0( AddrSelectItem, 1 );
61 item->objectType = addressbook_type2item( obj->type );
62 item->addressItem = NULL;
70 * Create a copy of a selection record.
71 * Enter: item Address entry to copy.
72 * \return Address select item.
74 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
75 AddrSelectItem *copy = NULL;
78 copy = g_new0( AddrSelectItem, 1 );
79 copy->objectType = item->objectType;
80 copy->addressItem = item->addressItem;
81 copy->uid = g_strdup( item->uid );
82 copy->cacheID = g_strdup( item->cacheID );
88 * Free selection record.
89 * \return Address select item.
91 void addrselect_item_free( AddrSelectItem *item ) {
94 g_free( item->cacheID );
95 item->objectType = ITEMTYPE_NONE;
96 item->addressItem = NULL;
104 * Print address selection item.
105 * \param item Address select item.
106 * \param stream Output stream.
108 void addrselect_item_print( AddrSelectItem *item, FILE *stream ) {
109 fprintf( stream, "Select Record\n" );
110 fprintf( stream, "obj type: %d\n", item->objectType );
111 fprintf( stream, " uid: %s\n", item->uid );
112 fprintf( stream, "cache id: %s\n", item->cacheID );
113 fprintf( stream, "---\n" );
117 * Create a new address selection object.
118 * \return Initialized object.
120 AddrSelectList *addrselect_list_create() {
123 asl = g_new0( AddrSelectList, 1 );
124 asl->listSelect = NULL;
129 * Clear list of selection records.
130 * \param asl List to process.
132 void addrselect_list_clear( AddrSelectList *asl ) {
135 cm_return_if_fail( asl != NULL );
136 node = asl->listSelect;
138 AddrSelectItem *item;
141 addrselect_item_free( item );
143 node = g_list_next( node );
145 g_list_free( asl->listSelect );
146 asl->listSelect = NULL;
150 * Free selection list.
151 * \param asl List to free.
153 void addrselect_list_free( AddrSelectList *asl ) {
154 cm_return_if_fail( asl != NULL );
156 addrselect_list_clear( asl );
157 g_list_free( asl->listSelect );
158 asl->listSelect = NULL;
163 * Test whether selection is empty.
164 * \param asl List to test.
165 * \return <i>TRUE</i> if list is empty.
167 gboolean addrselect_test_empty( AddrSelectList *asl ) {
168 cm_return_val_if_fail( asl != NULL, TRUE );
169 return ( asl->listSelect == NULL );
173 * Return list of AddrSelectItem objects.
174 * \param asl List to process.
175 * \return List of selection items. The list should should be freed with
176 * <code>g_list_free()</code> when done. Items contained in the
177 * list should <b>not</b> be freed!!!
179 GList *addrselect_get_list( AddrSelectList *asl ) {
182 cm_return_val_if_fail(asl != NULL, NULL);
184 node = asl->listSelect;
186 list = g_list_append( list, node->data );
187 node = g_list_next( node );
193 * Format address item.
195 * \return Formatted address.
197 static gchar *addrselect_format_address( AddrItemObject * aio ) {
200 gchar *address = NULL;
202 if( aio->type == ITEMTYPE_EMAIL ) {
203 ItemPerson *person = NULL;
204 ItemEMail *email = ( ItemEMail * ) aio;
206 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
207 if( email->address ) {
208 if( ADDRITEM_NAME(email) ) {
209 name = ADDRITEM_NAME(email);
210 if( *name == '\0' ) {
211 name = ADDRITEM_NAME(person);
214 else if( ADDRITEM_NAME(person) ) {
215 name = ADDRITEM_NAME(person);
218 buf = g_strdup( email->address );
220 address = email->address;
223 else if( aio->type == ITEMTYPE_PERSON ) {
224 ItemPerson *person = ( ItemPerson * ) aio;
225 GList *node = person->listEMail;
227 name = ADDRITEM_NAME(person);
229 ItemEMail *email = ( ItemEMail * ) node->data;
230 address = email->address;
235 buf = g_strdup_printf( "%s <%s>", name, address );
238 buf = g_strdup( address );
245 * Test whether specified object is in list.
246 * \param list List to check.
247 * \param aio Object to test.
248 * \param item found, or <i>NULL</i> if not in list.
250 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
255 AddrSelectItem *item;
258 if( item->addressItem == aio ) return item;
259 node = g_list_next( node );
265 * Add a single object into the list.
266 * \param asl Address selection object.
267 * \param aio Address object.
268 * \param cacheID Cache ID. Should be freed after calling function.
270 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
271 AddrSelectItem *item;
273 cm_return_if_fail( asl != NULL );
274 if( aio == NULL ) return;
276 /* Check whether object is in list */
277 if( addrselect_list_find( asl->listSelect, aio ) ) return;
279 if( aio->type == ITEMTYPE_PERSON ||
280 aio->type == ITEMTYPE_EMAIL ||
281 aio->type == ITEMTYPE_GROUP ) {
282 item = addrselect_create_item( aio );
283 item->cacheID = g_strdup( cacheID );
284 asl->listSelect = g_list_append( asl->listSelect, item );
286 /* addrselect_list_show( asl, stdout ); */
290 * Add a single item into the list.
291 * \param asl Address selection object.
292 * \param item Address select item.
293 * \param cacheID Cache ID. Should be g_free() after calling function.
295 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
296 cm_return_if_fail( asl != NULL );
297 if( item == NULL ) return;
299 /* Check whether object is in list */
300 if( g_list_find( asl->listSelect, item ) ) return;
302 item->cacheID = g_strdup( cacheID );
303 asl->listSelect = g_list_append( asl->listSelect, item );
307 * Remove specified object from list.
308 * \param asl Address selection object.
309 * \param aio Object to remove.
311 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
313 AddrSelectItem *item;
315 cm_return_if_fail( asl != NULL );
316 if( aio == NULL ) return;
317 node = asl->listSelect;
320 if( item->addressItem == aio ) {
321 addrselect_item_free( item );
323 asl->listSelect = g_list_remove_link( asl->listSelect, node );
326 node = g_list_next( node );
328 /* addrselect_list_show( list, stdout ); */
332 * Build list of formatted addresses.
333 * \param asl List to process.
334 * \return List of addresses, formatted as character strings. List should be
335 * freed when no longer required.
337 GList *addrselect_build_list( AddrSelectList *asl ) {
341 cm_return_val_if_fail(asl != NULL, NULL);
343 node = asl->listSelect;
344 while( node != NULL ) {
345 AddrSelectItem *item;
350 aio = ( AddrItemObject * ) item->addressItem;
352 if( aio->type == ITEMTYPE_GROUP ) {
353 ItemGroup *group = ( ItemGroup * ) aio;
354 GList *node = group->listEMail;
356 ItemEMail *email = node->data;
357 addr = addrselect_format_address(
358 ( AddrItemObject * ) email );
360 list = g_list_append( list, addr );
362 node = g_list_next( node );
366 addr = addrselect_format_address( aio );
368 list = g_list_append( list, addr );
372 node = g_list_next( node );