A bit more room for alertpanel messages
[claws.git] / src / addrselect.c
blob8d967cf61f958a57a57f48fe11596f9f2281e287
1 /*
2 * Sylpheed -- 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 <stdio.h>
25 #include <glib.h>
27 #include "addrselect.h"
28 #include "addressitem.h"
29 #include "mgutils.h"
31 static AddrSelectItem *addrselect_create_item ( AddrItemObject *aio );
33 /**
34 * Create a selection record from an address cache item.
35 * \param aio Item object.
36 * \return Address select item.
38 static AddrSelectItem *addrselect_create_item( AddrItemObject *aio ) {
39 AddrSelectItem *item = NULL;
41 if( aio ) {
42 item = g_new0( AddrSelectItem, 1 );
43 item->objectType = aio->type;
44 item->addressItem = aio;
45 item->uid = g_strdup( aio->uid );
46 item->cacheID = NULL;
48 return item;
51 /**
52 * Create a selection record from an address object (in tree node).
53 * \param obj Address object.
54 * \return Address select item.
56 AddrSelectItem *addrselect_create_node( AddressObject *obj ) {
57 AddrSelectItem *item = NULL;
59 if( obj ) {
60 item = g_new0( AddrSelectItem, 1 );
61 item->objectType = addressbook_type2item( obj->type );
62 item->addressItem = NULL;
63 item->uid = NULL;
64 item->cacheID = NULL;
66 return item;
69 /**
70 * Create a copy of a selection record.
71 * Enter: item Address entry to copy.
72 * \return Address select item.
74 AddrSelectItem *addrselect_item_copy( AddrSelectItem *item ) {
75 AddrSelectItem *copy = NULL;
77 if( item ) {
78 copy = g_new0( AddrSelectItem, 1 );
79 copy->objectType = item->objectType;
80 copy->addressItem = item->addressItem;
81 copy->uid = g_strdup( item->uid );
82 copy->cacheID = g_strdup( item->cacheID );
84 return copy;
87 /**
88 * Free selection record.
89 * \return Address select item.
91 void addrselect_item_free( AddrSelectItem *item ) {
92 if( item ) {
93 g_free( item->uid );
94 g_free( item->cacheID );
95 item->objectType = ITEMTYPE_NONE;
96 item->addressItem = NULL;
97 item->uid = NULL;
98 item->cacheID = NULL;
100 g_free( item );
104 * Print address selection item.
105 * \param item Address select item.
106 * \param stream Output stream.
108 void addrselect_item_print( AddrSelectItem *item, FILE *stream ) {
109 fprintf( stream, "Select Record\n" );
110 fprintf( stream, "obj type: %d\n", item->objectType );
111 fprintf( stream, " uid: %s\n", item->uid );
112 fprintf( stream, "cache id: %s\n", item->cacheID );
113 fprintf( stream, "---\n" );
117 * Create a new address selection object.
118 * \return Initialized object.
120 AddrSelectList *addrselect_list_create() {
121 AddrSelectList *asl;
123 asl = g_new0( AddrSelectList, 1 );
124 asl->listSelect = NULL;
125 return asl;
129 * Clear list of selection records.
130 * \param asl List to process.
132 void addrselect_list_clear( AddrSelectList *asl ) {
133 GList *node;
135 cm_return_if_fail( asl != NULL );
136 node = asl->listSelect;
137 while( node ) {
138 AddrSelectItem *item;
140 item = node->data;
141 addrselect_item_free( item );
142 node->data = NULL;
143 node = g_list_next( node );
145 g_list_free( asl->listSelect );
146 asl->listSelect = NULL;
150 * Free selection list.
151 * \param asl List to free.
153 void addrselect_list_free( AddrSelectList *asl ) {
154 cm_return_if_fail( asl != NULL );
156 addrselect_list_clear( asl );
157 g_list_free( asl->listSelect );
158 asl->listSelect = NULL;
159 g_free( asl );
163 * Test whether selection is empty.
164 * \param asl List to test.
165 * \return <i>TRUE</i> if list is empty.
167 gboolean addrselect_test_empty( AddrSelectList *asl ) {
168 cm_return_val_if_fail( asl != NULL, TRUE );
169 return ( asl->listSelect == NULL );
173 * Return list of AddrSelectItem objects.
174 * \param asl List to process.
175 * \return List of selection items. The list should should be freed with
176 * <code>g_list_free()</code> when done. Items contained in the
177 * list should <b>not</b> be freed!!!
179 GList *addrselect_get_list( AddrSelectList *asl ) {
180 GList *node, *list;
182 cm_return_val_if_fail(asl != NULL, NULL);
183 list = NULL;
184 node = asl->listSelect;
185 while( node ) {
186 list = g_list_append( list, node->data );
187 node = g_list_next( node );
189 return list;
193 * Format address item.
194 * \param aio Item.
195 * \return Formatted address.
197 static gchar *addrselect_format_address( AddrItemObject * aio ) {
198 gchar *buf = NULL;
199 gchar *name = NULL;
200 gchar *address = NULL;
202 if( aio->type == ITEMTYPE_EMAIL ) {
203 ItemPerson *person = NULL;
204 ItemEMail *email = ( ItemEMail * ) aio;
206 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
207 if( email->address ) {
208 if( ADDRITEM_NAME(email) ) {
209 name = ADDRITEM_NAME(email);
210 if( *name == '\0' ) {
211 name = ADDRITEM_NAME(person);
214 else if( ADDRITEM_NAME(person) ) {
215 name = ADDRITEM_NAME(person);
217 else {
218 buf = g_strdup( email->address );
220 address = email->address;
223 else if( aio->type == ITEMTYPE_PERSON ) {
224 ItemPerson *person = ( ItemPerson * ) aio;
225 GList *node = person->listEMail;
227 name = ADDRITEM_NAME(person);
228 if( node ) {
229 ItemEMail *email = ( ItemEMail * ) node->data;
230 address = email->address;
233 if( address ) {
234 if( name && name[0] != '\0' ) {
235 if( strchr_with_skip_quote( name, '"', ',' ) )
236 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
237 else
238 buf = g_strdup_printf( "%s <%s>", name, address );
240 else {
241 buf = g_strdup( address );
244 return buf;
248 * Test whether specified object is in list.
249 * \param list List to check.
250 * \param aio Object to test.
251 * \param item found, or <i>NULL</i> if not in list.
253 static AddrSelectItem *addrselect_list_find( GList *list, AddrItemObject *aio ) {
254 GList *node;
256 node = list;
257 while( node ) {
258 AddrSelectItem *item;
260 item = node->data;
261 if( item->addressItem == aio ) return item;
262 node = g_list_next( node );
264 return NULL;
268 * Add a single object into the list.
269 * \param asl Address selection object.
270 * \param aio Address object.
271 * \param cacheID Cache ID. Should be freed after calling function.
273 void addrselect_list_add_obj( AddrSelectList *asl, AddrItemObject *aio, gchar *cacheID ) {
274 AddrSelectItem *item;
276 cm_return_if_fail( asl != NULL );
277 if( aio == NULL ) return;
279 /* Check whether object is in list */
280 if( addrselect_list_find( asl->listSelect, aio ) ) return;
282 if( aio->type == ITEMTYPE_PERSON ||
283 aio->type == ITEMTYPE_EMAIL ||
284 aio->type == ITEMTYPE_GROUP ) {
285 item = addrselect_create_item( aio );
286 item->cacheID = g_strdup( cacheID );
287 asl->listSelect = g_list_append( asl->listSelect, item );
289 /* addrselect_list_show( asl, stdout ); */
293 * Add a single item into the list.
294 * \param asl Address selection object.
295 * \param item Address select item.
296 * \param cacheID Cache ID. Should be g_free() after calling function.
298 void addrselect_list_add( AddrSelectList *asl, AddrSelectItem *item, gchar *cacheID ) {
299 cm_return_if_fail( asl != NULL );
300 if( item == NULL ) return;
302 /* Check whether object is in list */
303 if( g_list_find( asl->listSelect, item ) ) return;
305 item->cacheID = g_strdup( cacheID );
306 asl->listSelect = g_list_append( asl->listSelect, item );
310 * Remove specified object from list.
311 * \param asl Address selection object.
312 * \param aio Object to remove.
314 void addrselect_list_remove( AddrSelectList *asl, AddrItemObject *aio ) {
315 GList *node;
316 AddrSelectItem *item;
318 cm_return_if_fail( asl != NULL );
319 if( aio == NULL ) return;
320 node = asl->listSelect;
321 while( node ) {
322 item = node->data;
323 if( item->addressItem == aio ) {
324 addrselect_item_free( item );
325 node->data = NULL;
326 asl->listSelect = g_list_remove_link( asl->listSelect, node );
327 break;
329 node = g_list_next( node );
331 /* addrselect_list_show( list, stdout ); */
335 * Build list of formatted addresses.
336 * \param asl List to process.
337 * \return List of addresses, formatted as character strings. List should be
338 * freed when no longer required.
340 GList *addrselect_build_list( AddrSelectList *asl ) {
341 GList *list;
342 GList *node;
344 cm_return_val_if_fail(asl != NULL, NULL);
345 list = NULL;
346 node = asl->listSelect;
347 while( node != NULL ) {
348 AddrSelectItem *item;
349 AddrItemObject *aio;
350 gchar *addr;
352 item = node->data;
353 aio = ( AddrItemObject * ) item->addressItem;
354 if( aio ) {
355 if( aio->type == ITEMTYPE_GROUP ) {
356 ItemGroup *group = ( ItemGroup * ) aio;
357 GList *node = group->listEMail;
358 while( node ) {
359 ItemEMail *email = node->data;
360 addr = addrselect_format_address(
361 ( AddrItemObject * ) email );
362 if( addr ) {
363 list = g_list_append( list, addr );
365 node = g_list_next( node );
368 else {
369 addr = addrselect_format_address( aio );
370 if( addr ) {
371 list = g_list_append( list, addr );
375 node = g_list_next( node );
377 return list;
381 * End of Source.