sync with 0.7.4cvs49
[claws.git] / src / addrselect.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * Address list item selection objects.
22  */
23
24 #include <stdio.h>
25 #include <glib.h>
26
27 #include "addritem.h"
28 #include "addrselect.h"
29 #include "addressitem.h"
30 #include "mgutils.h"
31
32 /*
33 * Create a selection record from an address cache item.
34 * Enter: aio Item object.
35 */
36 AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
37         AddrSelectItem *item = NULL;
38
39         if( aio ) {
40                 item = g_new0( AddrSelectItem, 1 );
41                 item->objectType = aio->type;
42                 item->addressItem = aio;
43                 item->uid = g_strdup( aio->uid );
44                 item->cacheID = NULL;
45         }
46         return item;
47 }
48
49 /*
50 * Create a selection record from an address object (in tree node).
51 * Enter: obj Address object.
52 */
53 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
54         AddrSelectItem *item = NULL;
55
56         if( obj ) {
57                 item = g_new0( AddrSelectItem, 1 );
58                 item->objectType = addressbook_type2item( obj->type );
59                 item->addressItem = NULL;
60                 item->uid = NULL;
61                 item->cacheID = NULL;
62         }
63         return item;
64 }
65
66 /*
67 * Create a copy of a selection record.
68 * Enter: item Address entry to copy.
69 */
70 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
71         AddrSelectItem *copy = NULL;
72
73         if( item ) {
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 );
79         }
80         return copy;
81 }
82
83 /*
84 * Free selection record.
85 */
86 void addrselect_item_free( AddrSelectItem *item ) {
87         if( item ) {
88                 g_free( item->uid );
89                 g_free( item->cacheID );
90                 item->objectType = ITEMTYPE_NONE;
91                 item->addressItem = NULL;
92                 item->uid = NULL;
93                 item->cacheID = NULL;
94         }
95         g_free( item );
96 }
97
98 /*
99 * Properties.
100 */
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 );
104 }
105
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" );
112 }
113
114 /*
115 * Create a new selection.
116 * Return: Initialized object.
117 */
118 AddrSelectList *addrselect_list_create() {
119         AddrSelectList *asl;
120
121         asl = g_new0( AddrSelectList, 1 );
122         asl->listSelect = NULL;
123         return asl;
124 }
125
126 /*
127 * Clear list of selection records.
128 * Enter: asl List to process.
129 */
130 void addrselect_list_clear( AddrSelectList *asl ) {
131         GList *node;
132
133         g_return_if_fail( asl != NULL );
134         node = asl->listSelect;
135         while( node ) {
136                 AddrSelectItem *item;
137
138                 item = node->data;
139                 addrselect_item_free( item );
140                 node->data = NULL;
141                 node = g_list_next( node );
142         }
143         g_list_free( asl->listSelect );
144         asl->listSelect = NULL;
145 }
146
147 /*
148 * Free selection list.
149 * Enter: asl List to free.
150 */
151 void addrselect_list_free( AddrSelectList *asl ) {
152         g_return_if_fail( asl != NULL );
153
154         addrselect_list_clear( asl );
155         g_list_free( asl->listSelect );
156         asl->listSelect = NULL;
157         g_free( asl );
158 }
159
160 /*
161 * Test whether selection is empty.
162 * Enter: asl List to test.
163 * Return: TRUE if list is empty.
164 */
165 gboolean addrselect_test_empty( AddrSelectList *asl ) {
166         g_return_val_if_fail( asl != NULL, TRUE );
167         return ( asl->listSelect == NULL );
168 }
169
170 /*
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!!!
175 */
176 GList *addrselect_get_list( AddrSelectList *asl ) {
177         GList *node, *list;
178
179         g_return_if_fail( asl != NULL );
180         list = NULL;
181         node = asl->listSelect;
182         while( node ) {
183                 list = g_list_append( list, node->data );
184                 node = g_list_next( node );
185         }
186         return list;
187 }
188
189 static gchar *addrselect_format_address( AddrItemObject * aio ) {
190         gchar *buf = NULL;
191         gchar *name = NULL;
192         gchar *address = NULL;
193
194         if( aio->type == ADDR_ITEM_EMAIL ) {
195                 ItemPerson *person = NULL;
196                 ItemEMail *email = ( ItemEMail * ) aio;
197
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);
204                                 }
205                         }
206                         else if( ADDRITEM_NAME(person) ) {
207                                 name = ADDRITEM_NAME(person);
208                         }
209                         else {
210                                 buf = g_strdup( email->address );
211                         }
212                         address = email->address;
213                 }
214         }
215         else if( aio->type == ADDR_ITEM_PERSON ) {
216                 ItemPerson *person = ( ItemPerson * ) aio;
217                 GList *node = person->listEMail;
218
219                 name = ADDRITEM_NAME(person);
220                 if( node ) {
221                         ItemEMail *email = ( ItemEMail * ) node->data;
222                         address = email->address;
223                 }
224         }
225         if( address ) {
226                 if( name ) {
227                         buf = g_strdup_printf( "%s <%s>", name, address );
228                 }
229                 else {
230                         buf = g_strdup( address );
231                 }
232         }
233         return buf;
234 }
235
236 /*
237 * Print formatted addresses list to specified stream.
238 * Enter: asl    List to process.
239 *        stream Stream.
240 */
241 void addrselect_list_print( AddrSelectList *asl, FILE *stream ) {
242         GList *node;
243
244         g_return_if_fail( asl != NULL );
245         fprintf( stream, "show selection...>>>\n" );
246         node = asl->listSelect;
247         while( node != NULL ) {
248                 AddrSelectItem *item;
249                 AddrItemObject *aio;
250                 gchar *addr;
251
252                 item = node->data;
253                 aio = ( AddrItemObject * ) item->addressItem;
254                 if( aio ) {
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;
259                                 while( node ) {
260                                         ItemEMail *email = node->data;
261                                         addr = addrselect_format_address(
262                                                 ( AddrItemObject * ) email );
263                                         if( addr ) {
264                                                 fprintf( stream, "\tgrp >%s<\n", addr );
265                                                 g_free( addr );
266                                         }
267                                         node = g_list_next( node );
268                                 }
269                         }
270                         else {
271                                 addr = addrselect_format_address( aio );
272                                 if( addr ) {
273                                         fprintf( stream, "\t>%s<\n", addr );
274                                         g_free( addr );
275                                 }
276                         }
277                 }
278                 else {
279                         fprintf( stream, "- NULL" );
280                 }
281                 node = g_list_next( node );
282         }
283         fprintf( stream, "show selection...<<<\n" );
284 }
285
286 /*
287 * Print address items to specified stream.
288 * Enter: asl    List to process.
289 *        stream Stream.
290 */
291 void addrselect_list_show( AddrSelectList *asl, FILE *stream ) {
292         GList *node;
293
294         g_return_if_fail( asl != NULL );
295         fprintf( stream, "show selection...>>>\n" );
296         node = asl->listSelect;
297         while( node != NULL ) {
298                 AddrSelectItem *item;
299
300                 item = node->data;
301                 addrselect_item_print( item, stream );
302                 node = g_list_next( node );
303         }
304         fprintf( stream, "show selection...<<<\n" );
305 }
306
307 /*
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.
312 */
313 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
314         GList *node;
315
316         node = list;
317         while( node ) {
318                 AddrSelectItem *item;
319
320                 item = node->data;
321                 if( item->addressItem == aio ) return item;
322                 node = g_list_next( node );
323         }
324         return NULL;
325 }
326
327 /*
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.
333 */
334 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
335         AddrSelectItem *item;
336
337         g_return_if_fail( asl != NULL );
338         if( aio == NULL ) return;
339
340         /* Check whether object is in list */
341         if( addrselect_list_find( asl->listSelect, aio ) ) return;
342
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 );
349         }
350         /* addrselect_list_show( asl, stdout ); */
351 }
352
353 /*
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.
359 */
360 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
361         g_return_if_fail( asl != NULL );
362         if( item == NULL ) return;
363
364         /* Check whether object is in list */
365         if( g_list_find( asl->listSelect, item ) ) return;
366
367         item->cacheID = g_strdup( cacheID );
368         asl->listSelect = g_list_append( asl->listSelect, item );
369 }
370
371 /*
372 * Remove specified object from list.
373 * Enter: asl  Address selection object.
374 *        aio  Object to remove.
375 */
376 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
377         GList *node;
378         AddrSelectItem *item;
379
380         g_return_if_fail( asl != NULL );
381         if( aio == NULL ) return;
382         node = asl->listSelect;
383         while( node ) {
384                 item = node->data;
385                 if( item->addressItem == aio ) {
386                         addrselect_item_free( item );
387                         node->data = NULL;
388                         asl->listSelect = g_list_remove_link( asl->listSelect, node );
389                         break;
390                 }
391                 node = g_list_next( node );
392         }
393         /* addrselect_list_show( list, stdout ); */
394 }
395
396 /*
397 * End of Source.
398 */
399
400