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