Sync Spanish manual
[claws.git] / src / addrselect.c
blob8f453799ee2e68de892c6800250e16c485219e6a
1 /*
2 * Claws Mail -- 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.
24 #include "config.h"
26 #include <stdio.h>
27 #include <glib.h>
29 #include "addrselect.h"
30 #include "addressitem.h"
31 #include "mgutils.h"
33 static AddrSelectItem *addrselect_create_item ( AddrItemObject *aio );
35 /**
36 * Create a selection record from an address cache item.
37 * \param aio Item object.
38 * \return Address select item.
40 static AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
41 AddrSelectItem *item = NULL;
43 if( aio ) {
44 item = g_new0( AddrSelectItem, 1 );
45 item->objectType = aio->type;
46 item->addressItem = aio;
47 item->uid = g_strdup( aio->uid );
48 item->cacheID = NULL;
50 return item;
53 /**
54 * Create a selection record from an address object (in tree node).
55 * \param obj Address object.
56 * \return Address select item.
58 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
59 AddrSelectItem *item = NULL;
61 if( obj ) {
62 item = g_new0( AddrSelectItem, 1 );
63 item->objectType = addressbook_type2item( obj->type );
64 item->addressItem = NULL;
65 item->uid = NULL;
66 item->cacheID = NULL;
68 return item;
71 /**
72 * Create a copy of a selection record.
73 * Enter: item Address entry to copy.
74 * \return Address select item.
76 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
77 AddrSelectItem *copy = NULL;
79 if( item ) {
80 copy = g_new0( AddrSelectItem, 1 );
81 copy->objectType = item->objectType;
82 copy->addressItem = item->addressItem;
83 copy->uid = g_strdup( item->uid );
84 copy->cacheID = g_strdup( item->cacheID );
86 return copy;
89 /**
90 * Free selection record.
91 * \return Address select item.
93 void addrselect_item_free( AddrSelectItem *item ) {
94 if( item ) {
95 g_free( item->uid );
96 g_free( item->cacheID );
97 item->objectType = ITEMTYPE_NONE;
98 item->addressItem = NULL;
99 item->uid = NULL;
100 item->cacheID = NULL;
102 g_free( item );
106 * Print address selection item.
107 * \param item Address select item.
108 * \param stream Output stream.
110 void addrselect_item_print( AddrSelectItem *item, FILE *stream ) {
111 fprintf( stream, "Select Record\n" );
112 fprintf( stream, "obj type: %d\n", item->objectType );
113 fprintf( stream, " uid: %s\n", item->uid );
114 fprintf( stream, "cache id: %s\n", item->cacheID );
115 fprintf( stream, "---\n" );
119 * Create a new address selection object.
120 * \return Initialized object.
122 AddrSelectList *addrselect_list_create() {
123 AddrSelectList *asl;
125 asl = g_new0( AddrSelectList, 1 );
126 asl->listSelect = NULL;
127 return asl;
131 * Clear list of selection records.
132 * \param asl List to process.
134 void addrselect_list_clear( AddrSelectList *asl ) {
135 GList *node;
137 cm_return_if_fail( asl != NULL );
138 node = asl->listSelect;
139 while( node ) {
140 AddrSelectItem *item;
142 item = node->data;
143 addrselect_item_free( item );
144 node->data = NULL;
145 node = g_list_next( node );
147 g_list_free( asl->listSelect );
148 asl->listSelect = NULL;
152 * Free selection list.
153 * \param asl List to free.
155 void addrselect_list_free( AddrSelectList *asl ) {
156 cm_return_if_fail( asl != NULL );
158 addrselect_list_clear( asl );
159 g_list_free( asl->listSelect );
160 asl->listSelect = NULL;
161 g_free( asl );
165 * Test whether selection is empty.
166 * \param asl List to test.
167 * \return <i>TRUE</i> if list is empty.
169 gboolean addrselect_test_empty( AddrSelectList *asl ) {
170 cm_return_val_if_fail( asl != NULL, TRUE );
171 return ( asl->listSelect == NULL );
175 * Return list of AddrSelectItem objects.
176 * \param asl List to process.
177 * \return List of selection items. The list should should be freed with
178 * <code>g_list_free()</code> when done. Items contained in the
179 * list should <b>not</b> be freed!!!
181 GList *addrselect_get_list( AddrSelectList *asl ) {
182 GList *node, *list;
184 cm_return_val_if_fail(asl != NULL, NULL);
185 list = NULL;
186 node = asl->listSelect;
187 while( node ) {
188 list = g_list_append( list, node->data );
189 node = g_list_next( node );
191 return list;
195 * Format address item.
196 * \param aio Item.
197 * \return Formatted address.
199 static gchar *addrselect_format_address( AddrItemObject * aio ) {
200 gchar *buf = NULL;
201 gchar *name = NULL;
202 gchar *address = NULL;
204 if( aio->type == ITEMTYPE_EMAIL ) {
205 ItemPerson *person = NULL;
206 ItemEMail *email = ( ItemEMail * ) aio;
208 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
209 if( email->address ) {
210 if( ADDRITEM_NAME(email) ) {
211 name = ADDRITEM_NAME(email);
212 if( *name == '\0' ) {
213 name = ADDRITEM_NAME(person);
216 else if( ADDRITEM_NAME(person) ) {
217 name = ADDRITEM_NAME(person);
219 else {
220 buf = g_strdup( email->address );
222 address = email->address;
225 else if( aio->type == ITEMTYPE_PERSON ) {
226 ItemPerson *person = ( ItemPerson * ) aio;
227 GList *node = person->listEMail;
229 name = ADDRITEM_NAME(person);
230 if( node ) {
231 ItemEMail *email = ( ItemEMail * ) node->data;
232 address = email->address;
235 if( address ) {
236 if( name && name[0] != '\0' ) {
237 if( strchr_with_skip_quote( name, '"', ',' ) )
238 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
239 else
240 buf = g_strdup_printf( "%s <%s>", name, address );
242 else {
243 buf = g_strdup( address );
246 return buf;
250 * Test whether specified object is in list.
251 * \param list List to check.
252 * \param aio Object to test.
253 * \param item found, or <i>NULL</i> if not in list.
255 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
256 GList *node;
258 node = list;
259 while( node ) {
260 AddrSelectItem *item;
262 item = node->data;
263 if( item->addressItem == aio ) return item;
264 node = g_list_next( node );
266 return NULL;
270 * Add a single object into the list.
271 * \param asl Address selection object.
272 * \param aio Address object.
273 * \param cacheID Cache ID. Should be freed after calling function.
275 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
276 AddrSelectItem *item;
278 cm_return_if_fail( asl != NULL );
279 if( aio == NULL ) return;
281 /* Check whether object is in list */
282 if( addrselect_list_find( asl->listSelect, aio ) ) return;
284 if( aio->type == ITEMTYPE_PERSON ||
285 aio->type == ITEMTYPE_EMAIL ||
286 aio->type == ITEMTYPE_GROUP ) {
287 item = addrselect_create_item( aio );
288 item->cacheID = g_strdup( cacheID );
289 asl->listSelect = g_list_append( asl->listSelect, item );
291 /* addrselect_list_show( asl, stdout ); */
295 * Add a single item into the list.
296 * \param asl Address selection object.
297 * \param item Address select item.
298 * \param cacheID Cache ID. Should be g_free() after calling function.
300 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
301 cm_return_if_fail( asl != NULL );
302 if( item == NULL ) return;
304 /* Check whether object is in list */
305 if( g_list_find( asl->listSelect, item ) ) return;
307 item->cacheID = g_strdup( cacheID );
308 asl->listSelect = g_list_append( asl->listSelect, item );
312 * Remove specified object from list.
313 * \param asl Address selection object.
314 * \param aio Object to remove.
316 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
317 GList *node;
318 AddrSelectItem *item;
320 cm_return_if_fail( asl != NULL );
321 if( aio == NULL ) return;
322 node = asl->listSelect;
323 while( node ) {
324 item = node->data;
325 if( item->addressItem == aio ) {
326 addrselect_item_free( item );
327 node->data = NULL;
328 asl->listSelect = g_list_remove_link( asl->listSelect, node );
329 break;
331 node = g_list_next( node );
333 /* addrselect_list_show( list, stdout ); */
337 * Build list of formatted addresses.
338 * \param asl List to process.
339 * \return List of addresses, formatted as character strings. List should be
340 * freed when no longer required.
342 GList *addrselect_build_list( AddrSelectList *asl ) {
343 GList *list;
344 GList *node;
346 cm_return_val_if_fail(asl != NULL, NULL);
347 list = NULL;
348 node = asl->listSelect;
349 while( node != NULL ) {
350 AddrSelectItem *item;
351 AddrItemObject *aio;
352 gchar *addr;
354 item = node->data;
355 aio = ( AddrItemObject * ) item->addressItem;
356 if( aio ) {
357 if( aio->type == ITEMTYPE_GROUP ) {
358 ItemGroup *group = ( ItemGroup * ) aio;
359 GList *node = group->listEMail;
360 while( node ) {
361 ItemEMail *email = node->data;
362 addr = addrselect_format_address(
363 ( AddrItemObject * ) email );
364 if( addr ) {
365 list = g_list_append( list, addr );
367 node = g_list_next( node );
370 else {
371 addr = addrselect_format_address( aio );
372 if( addr ) {
373 list = g_list_append( list, addr );
377 node = g_list_next( node );
379 return list;
383 * End of Source.