2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002 Match Grun
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Address list item selection objects.
27 #include "addrselect.h"
28 #include "addressitem.h"
32 * Create a selection record from an address cache item.
33 * Enter: aio Item object.
35 AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
36 AddrSelectItem *item = NULL;
39 item = g_new0( AddrSelectItem, 1 );
40 item->objectType = aio->type;
41 item->addressItem = aio;
42 item->uid = g_strdup( aio->uid );
49 * Create a selection record from an address object (in tree node).
50 * Enter: obj Address object.
52 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
53 AddrSelectItem *item = NULL;
56 item = g_new0( AddrSelectItem, 1 );
57 item->objectType = addressbook_type2item( obj->type );
58 item->addressItem = NULL;
66 * Create a copy of a selection record.
67 * Enter: item Address entry to copy.
69 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
70 AddrSelectItem *copy = NULL;
73 copy = g_new0( AddrSelectItem, 1 );
74 copy->objectType = item->objectType;
75 copy->addressItem = item->addressItem;
76 copy->uid = g_strdup( item->uid );
77 copy->cacheID = g_strdup( item->cacheID );
83 * Free selection record.
85 void addrselect_item_free( AddrSelectItem *item ) {
88 g_free( item->cacheID );
89 item->objectType = ITEMTYPE_NONE;
90 item->addressItem = NULL;
100 void addrselect_set_cache_id( AddrSelectItem *item, const gchar *value ) {
101 g_return_if_fail( item != NULL );
102 item->cacheID = mgu_replace_string( item->cacheID, value );
105 void addrselect_item_print( AddrSelectItem *item, FILE *stream ) {
106 fprintf( stream, "Select Record\n" );
107 fprintf( stream, "obj type: %d\n", item->objectType );
108 fprintf( stream, " uid: %s\n", item->uid );
109 fprintf( stream, "cache id: %s\n", item->cacheID );
110 fprintf( stream, "---\n" );
114 * Create a new selection.
115 * Return: Initialized object.
117 AddrSelectList *addrselect_list_create() {
120 asl = g_new0( AddrSelectList, 1 );
121 asl->listSelect = NULL;
126 * Clear list of selection records.
127 * Enter: asl List to process.
129 void addrselect_list_clear( AddrSelectList *asl ) {
132 g_return_if_fail( asl != NULL );
133 node = asl->listSelect;
135 AddrSelectItem *item;
138 addrselect_item_free( item );
140 node = g_list_next( node );
142 g_list_free( asl->listSelect );
143 asl->listSelect = NULL;
147 * Free selection list.
148 * Enter: asl List to free.
150 void addrselect_list_free( AddrSelectList *asl ) {
151 g_return_if_fail( asl != NULL );
153 addrselect_list_clear( asl );
154 g_list_free( asl->listSelect );
155 asl->listSelect = NULL;
160 * Test whether selection is empty.
161 * Enter: asl List to test.
162 * Return: TRUE if list is empty.
164 gboolean addrselect_test_empty( AddrSelectList *asl ) {
165 g_return_val_if_fail( asl != NULL, TRUE );
166 return ( asl->listSelect == NULL );
170 * Return list of AddrSelectItem objects.
171 * Enter: asl List to process.
172 * Return: List of selection items. The list should should be g_list_free()
173 * when done. Items contained in the list should not be freed!!!
175 GList *addrselect_get_list( AddrSelectList *asl ) {
178 g_return_val_if_fail(asl != NULL, NULL);
180 node = asl->listSelect;
182 list = g_list_append( list, node->data );
183 node = g_list_next( node );
188 static gchar *addrselect_format_address( AddrItemObject * aio ) {
191 gchar *address = NULL;
193 if( aio->type == ADDR_ITEM_EMAIL ) {
194 ItemPerson *person = NULL;
195 ItemEMail *email = ( ItemEMail * ) aio;
197 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
198 if( email->address ) {
199 if( ADDRITEM_NAME(email) ) {
200 name = ADDRITEM_NAME(email);
201 if( *name == '\0' ) {
202 name = ADDRITEM_NAME(person);
205 else if( ADDRITEM_NAME(person) ) {
206 name = ADDRITEM_NAME(person);
209 buf = g_strdup( email->address );
211 address = email->address;
214 else if( aio->type == ADDR_ITEM_PERSON ) {
215 ItemPerson *person = ( ItemPerson * ) aio;
216 GList *node = person->listEMail;
218 name = ADDRITEM_NAME(person);
220 ItemEMail *email = ( ItemEMail * ) node->data;
221 address = email->address;
226 buf = g_strdup_printf( "%s <%s>", name, address );
229 buf = g_strdup( address );
236 * Print formatted addresses list to specified stream.
237 * Enter: asl List to process.
240 void addrselect_list_print( AddrSelectList *asl, FILE *stream ) {
243 g_return_if_fail( asl != NULL );
244 fprintf( stream, "show selection...>>>\n" );
245 node = asl->listSelect;
246 while( node != NULL ) {
247 AddrSelectItem *item;
252 aio = ( AddrItemObject * ) item->addressItem;
254 fprintf( stream, "- %d : '%s'\n", aio->type, aio->name );
255 if( aio->type == ADDR_ITEM_GROUP ) {
256 ItemGroup *group = ( ItemGroup * ) aio;
257 GList *node = group->listEMail;
259 ItemEMail *email = node->data;
260 addr = addrselect_format_address(
261 ( AddrItemObject * ) email );
263 fprintf( stream, "\tgrp >%s<\n", addr );
266 node = g_list_next( node );
270 addr = addrselect_format_address( aio );
272 fprintf( stream, "\t>%s<\n", addr );
278 fprintf( stream, "- NULL" );
280 node = g_list_next( node );
282 fprintf( stream, "show selection...<<<\n" );
286 * Print address items to specified stream.
287 * Enter: asl List to process.
290 void addrselect_list_show( AddrSelectList *asl, FILE *stream ) {
293 g_return_if_fail( asl != NULL );
294 fprintf( stream, "show selection...>>>\n" );
295 node = asl->listSelect;
296 while( node != NULL ) {
297 AddrSelectItem *item;
300 addrselect_item_print( item, stream );
301 node = g_list_next( node );
303 fprintf( stream, "show selection...<<<\n" );
307 * Test whether specified object is in list.
308 * Enter: list List to check.
309 * aio Object to test.
310 * Return item found, or NULL if not in list.
312 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
317 AddrSelectItem *item;
320 if( item->addressItem == aio ) return item;
321 node = g_list_next( node );
327 * Add a single object into the list.
328 * Enter: asl Address selection object.
329 * obj Address object.
330 * cacheID Cache ID. Should be g_free() after calling function.
331 * Return: Adjusted list.
333 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
334 AddrSelectItem *item;
336 g_return_if_fail( asl != NULL );
337 if( aio == NULL ) return;
339 /* Check whether object is in list */
340 if( addrselect_list_find( asl->listSelect, aio ) ) return;
342 if( aio->type == ADDR_ITEM_PERSON ||
343 aio->type == ADDR_ITEM_EMAIL ||
344 aio->type == ADDR_ITEM_GROUP ) {
345 item = addrselect_create_item( aio );
346 item->cacheID = g_strdup( cacheID );
347 asl->listSelect = g_list_append( asl->listSelect, item );
349 /* addrselect_list_show( asl, stdout ); */
353 * Add a single item into the list.
354 * Enter: asl Address selection object.
355 * item Address select item.
356 * cacheID Cache ID. Should be g_free() after calling function.
357 * Return: Adjusted list.
359 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
360 g_return_if_fail( asl != NULL );
361 if( item == NULL ) return;
363 /* Check whether object is in list */
364 if( g_list_find( asl->listSelect, item ) ) return;
366 item->cacheID = g_strdup( cacheID );
367 asl->listSelect = g_list_append( asl->listSelect, item );
371 * Remove specified object from list.
372 * Enter: asl Address selection object.
373 * aio Object to remove.
375 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
377 AddrSelectItem *item;
379 g_return_if_fail( asl != NULL );
380 if( aio == NULL ) return;
381 node = asl->listSelect;
384 if( item->addressItem == aio ) {
385 addrselect_item_free( item );
387 asl->listSelect = g_list_remove_link( asl->listSelect, node );
390 node = g_list_next( node );
392 /* addrselect_list_show( list, stdout ); */