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.
28 #include "addrselect.h"
29 #include "addressitem.h"
33 * Create a selection record from an address cache item.
34 * Enter: aio Item object.
36 AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
37 AddrSelectItem *item = NULL;
40 item = g_new0( AddrSelectItem, 1 );
41 item->objectType = aio->type;
42 item->addressItem = aio;
43 item->uid = g_strdup( aio->uid );
50 * Create a selection record from an address object (in tree node).
51 * Enter: obj Address object.
53 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
54 AddrSelectItem *item = NULL;
57 item = g_new0( AddrSelectItem, 1 );
58 item->objectType = addressbook_type2item( obj->type );
59 item->addressItem = NULL;
67 * Create a copy of a selection record.
68 * Enter: item Address entry to copy.
70 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
71 AddrSelectItem *copy = NULL;
74 copy = g_new0( AddrSelectItem, 1 );
75 copy->objectType = item->objectType;
76 copy->addressItem = item->addressItem;
77 copy->uid = g_strdup( item->uid );
78 copy->cacheID = g_strdup( item->cacheID );
84 * Free selection record.
86 void addrselect_item_free( AddrSelectItem *item ) {
89 g_free( item->cacheID );
90 item->objectType = ITEMTYPE_NONE;
91 item->addressItem = NULL;
101 void addrselect_set_cache_id( AddrSelectItem *item, const gchar *value ) {
102 g_return_if_fail( item != NULL );
103 item->cacheID = mgu_replace_string( item->cacheID, value );
106 void addrselect_item_print( AddrSelectItem *item, FILE *stream ) {
107 fprintf( stream, "Select Record\n" );
108 fprintf( stream, "obj type: %d\n", item->objectType );
109 fprintf( stream, " uid: %s\n", item->uid );
110 fprintf( stream, "cache id: %s\n", item->cacheID );
111 fprintf( stream, "---\n" );
115 * Create a new selection.
116 * Return: Initialized object.
118 AddrSelectList *addrselect_list_create() {
121 asl = g_new0( AddrSelectList, 1 );
122 asl->listSelect = NULL;
127 * Clear list of selection records.
128 * Enter: asl List to process.
130 void addrselect_list_clear( AddrSelectList *asl ) {
133 g_return_if_fail( asl != NULL );
134 node = asl->listSelect;
136 AddrSelectItem *item;
139 addrselect_item_free( item );
141 node = g_list_next( node );
143 g_list_free( asl->listSelect );
144 asl->listSelect = NULL;
148 * Free selection list.
149 * Enter: asl List to free.
151 void addrselect_list_free( AddrSelectList *asl ) {
152 g_return_if_fail( asl != NULL );
154 addrselect_list_clear( asl );
155 g_list_free( asl->listSelect );
156 asl->listSelect = NULL;
161 * Test whether selection is empty.
162 * Enter: asl List to test.
163 * Return: TRUE if list is empty.
165 gboolean addrselect_test_empty( AddrSelectList *asl ) {
166 g_return_val_if_fail( asl != NULL, TRUE );
167 return ( asl->listSelect == NULL );
171 * Return list of AddrSelectItem objects.
172 * Enter: asl List to process.
173 * Return: List of selection items. The list should should be g_list_free()
174 * when done. Items contained in the list should not be freed!!!
176 GList *addrselect_get_list( AddrSelectList *asl ) {
179 g_return_val_if_fail(asl != NULL, NULL);
181 node = asl->listSelect;
183 list = g_list_append( list, node->data );
184 node = g_list_next( node );
189 static gchar *addrselect_format_address( AddrItemObject * aio ) {
192 gchar *address = NULL;
194 if( aio->type == ADDR_ITEM_EMAIL ) {
195 ItemPerson *person = NULL;
196 ItemEMail *email = ( ItemEMail * ) aio;
198 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
199 if( email->address ) {
200 if( ADDRITEM_NAME(email) ) {
201 name = ADDRITEM_NAME(email);
202 if( *name == '\0' ) {
203 name = ADDRITEM_NAME(person);
206 else if( ADDRITEM_NAME(person) ) {
207 name = ADDRITEM_NAME(person);
210 buf = g_strdup( email->address );
212 address = email->address;
215 else if( aio->type == ADDR_ITEM_PERSON ) {
216 ItemPerson *person = ( ItemPerson * ) aio;
217 GList *node = person->listEMail;
219 name = ADDRITEM_NAME(person);
221 ItemEMail *email = ( ItemEMail * ) node->data;
222 address = email->address;
227 buf = g_strdup_printf( "%s <%s>", name, address );
230 buf = g_strdup( address );
237 * Print formatted addresses list to specified stream.
238 * Enter: asl List to process.
241 void addrselect_list_print( AddrSelectList *asl, FILE *stream ) {
244 g_return_if_fail( asl != NULL );
245 fprintf( stream, "show selection...>>>\n" );
246 node = asl->listSelect;
247 while( node != NULL ) {
248 AddrSelectItem *item;
253 aio = ( AddrItemObject * ) item->addressItem;
255 fprintf( stream, "- %d : '%s'\n", aio->type, aio->name );
256 if( aio->type == ADDR_ITEM_GROUP ) {
257 ItemGroup *group = ( ItemGroup * ) aio;
258 GList *node = group->listEMail;
260 ItemEMail *email = node->data;
261 addr = addrselect_format_address(
262 ( AddrItemObject * ) email );
264 fprintf( stream, "\tgrp >%s<\n", addr );
267 node = g_list_next( node );
271 addr = addrselect_format_address( aio );
273 fprintf( stream, "\t>%s<\n", addr );
279 fprintf( stream, "- NULL" );
281 node = g_list_next( node );
283 fprintf( stream, "show selection...<<<\n" );
287 * Print address items to specified stream.
288 * Enter: asl List to process.
291 void addrselect_list_show( AddrSelectList *asl, FILE *stream ) {
294 g_return_if_fail( asl != NULL );
295 fprintf( stream, "show selection...>>>\n" );
296 node = asl->listSelect;
297 while( node != NULL ) {
298 AddrSelectItem *item;
301 addrselect_item_print( item, stream );
302 node = g_list_next( node );
304 fprintf( stream, "show selection...<<<\n" );
308 * Test whether specified object is in list.
309 * Enter: list List to check.
310 * aio Object to test.
311 * Return item found, or NULL if not in list.
313 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
318 AddrSelectItem *item;
321 if( item->addressItem == aio ) return item;
322 node = g_list_next( node );
328 * Add a single object into the list.
329 * Enter: asl Address selection object.
330 * obj Address object.
331 * cacheID Cache ID. Should be g_free() after calling function.
332 * Return: Adjusted list.
334 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
335 AddrSelectItem *item;
337 g_return_if_fail( asl != NULL );
338 if( aio == NULL ) return;
340 /* Check whether object is in list */
341 if( addrselect_list_find( asl->listSelect, aio ) ) return;
343 if( aio->type == ADDR_ITEM_PERSON ||
344 aio->type == ADDR_ITEM_EMAIL ||
345 aio->type == ADDR_ITEM_GROUP ) {
346 item = addrselect_create_item( aio );
347 item->cacheID = g_strdup( cacheID );
348 asl->listSelect = g_list_append( asl->listSelect, item );
350 /* addrselect_list_show( asl, stdout ); */
354 * Add a single item into the list.
355 * Enter: asl Address selection object.
356 * item Address select item.
357 * cacheID Cache ID. Should be g_free() after calling function.
358 * Return: Adjusted list.
360 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
361 g_return_if_fail( asl != NULL );
362 if( item == NULL ) return;
364 /* Check whether object is in list */
365 if( g_list_find( asl->listSelect, item ) ) return;
367 item->cacheID = g_strdup( cacheID );
368 asl->listSelect = g_list_append( asl->listSelect, item );
372 * Remove specified object from list.
373 * Enter: asl Address selection object.
374 * aio Object to remove.
376 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
378 AddrSelectItem *item;
380 g_return_if_fail( asl != NULL );
381 if( aio == NULL ) return;
382 node = asl->listSelect;
385 if( item->addressItem == aio ) {
386 addrselect_item_free( item );
388 asl->listSelect = g_list_remove_link( asl->listSelect, node );
391 node = g_list_next( node );
393 /* addrselect_list_show( list, stdout ); */