2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 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 * General functions for create common address book entries.
28 #include "addressitem.h"
32 * Create new address item.
34 AddressItem *mgu_create_address_item( AddressObjectType type ) {
36 item = g_new( AddressItem, 1 );
37 ADDRESS_OBJECT(item)->type = type;
41 item->externalID = NULL;
42 item->categoryID = ADDRESS_ITEM_CAT_UNKNOWN;
47 * Create new address item.
49 AddressItem *mgu_create_address( void ) {
51 item = g_new( AddressItem, 1 );
52 ADDRESS_OBJECT(item)->type = ADDR_ITEM;
56 item->externalID = NULL;
57 item->categoryID = ADDRESS_ITEM_CAT_UNKNOWN;
62 * Create copy of specified address item.
64 AddressItem *mgu_copy_address_item( AddressItem *item ) {
65 AddressItem *itemNew = NULL;
67 itemNew = mgu_create_address_item( ADDRESS_OBJECT(item)->type );
68 itemNew->name = g_strdup( item->name );
69 itemNew->address = g_strdup( item->address );
70 itemNew->remarks = g_strdup( item->remarks );
71 itemNew->externalID = g_strdup( item->externalID );
72 itemNew->categoryID = item->categoryID;
80 void mgu_free_address( AddressItem *item ) {
81 g_return_if_fail( item != NULL );
83 /* Free internal stuff */
85 g_free( item->address );
86 g_free( item->remarks );
87 g_free( item->externalID );
91 item->externalID = NULL;
92 item->categoryID = ADDRESS_ITEM_CAT_UNKNOWN;
94 /* Now release item */
99 * Refresh internal variables to force a reload.
101 void mgu_refresh_cache( AddressCache *cache ) {
102 cache->dataRead = FALSE;
103 cache->modified = TRUE;
104 cache->modifyTime = 0;
108 * Free up address list.
110 void mgu_free_address_list( GList *addrList ) {
114 /* Free data in the list */
118 mgu_free_address( item );
120 node = g_list_next( node );
123 /* Now release linked list object */
124 g_list_free( addrList );
130 void mgu_clear_cache( AddressCache *cache ) {
133 g_return_if_fail( cache != NULL );
135 /* Free data in the list */
136 mgu_free_address_list( cache->addressList );
137 cache->addressList = NULL;
138 mgu_refresh_cache( cache );
142 * Clear the cache by setting pointers to NULL and free list.
143 * Note that individual items are not free'd.
145 void mgu_clear_cache_null( AddressCache *cache ) {
147 g_return_if_fail( cache != NULL );
149 /* Free data in the list */
150 node = cache->addressList;
153 node = g_list_next( node );
156 /* Now release linked list object */
157 g_list_free( cache->addressList );
158 cache->addressList = NULL;
164 AddressCache *mgu_create_cache( void ) {
166 cache = g_new( AddressCache, 1 );
167 cache->addressList = NULL;
168 cache->dataRead = FALSE;
169 cache->modified = FALSE;
170 cache->modifyTime = 0;
175 * Create new address item.
177 void mgu_free_cache( AddressCache *cache ) {
178 mgu_clear_cache( cache );
179 cache->addressList = NULL;
183 * Print address item.
185 void mgu_print_address( AddressItem *item, FILE *stream ) {
186 g_return_if_fail( item != NULL );
187 fprintf( stream, "addr item:\n" );
188 fprintf( stream, "\tname: '%s'\n", item->name );
189 fprintf( stream, "\taddr: '%s'\n", item->address );
190 fprintf( stream, "\trems: '%s'\n", item->remarks );
191 fprintf( stream, "\tid : '%s'\n", item->externalID );
192 fprintf( stream, "\tcatg: '%d'\n", item->categoryID );
193 fprintf( stream, "---\n" );
197 * Print linked list containing address item(s).
199 void mgu_print_address_list( GList *addrList, FILE *stream ) {
201 g_return_if_fail( addrList != NULL );
203 /* Now process the list */
206 gpointer *gptr = node->data;
207 AddressItem *item = ( AddressItem * ) gptr;
208 mgu_print_address( item, stream );
209 node = g_list_next( node );
214 * Print address cache.
216 void mgu_print_cache( AddressCache *cache, FILE *stream ) {
218 g_return_if_fail( cache != NULL );
219 fprintf( stream, "AddressCache:\n" );
220 fprintf( stream, "modified : %s\n", cache->modified ? "yes" : "no" );
221 fprintf( stream, "data read: %s\n", cache->dataRead ? "yes" : "no" );
223 /* Now process the list */
224 node = cache->addressList;
229 item = ( AddressItem * ) gptr;
230 mgu_print_address( item, stream );
231 node = g_list_next( node );
236 * Dump linked list of character strings (for debug).
238 void mgu_print_list( GSList *list, FILE *stream ) {
241 fprintf( stream, "\t- >%s<\n", node->data );
242 node = g_slist_next( node );
247 * Dump linked list of character strings (for debug).
249 void mgu_print_dlist( GList *list, FILE *stream ) {
252 fprintf( stream, "\t- >%s<\n", node->data );
253 node = g_list_next( node );
258 * Check whether file has changed by comparing with cache.
259 * return: TRUE if file has changed.
261 gboolean mgu_check_file( AddressCache *cache, gchar *path ) {
263 struct stat filestat;
266 if( 0 == lstat( path, &filestat ) ) {
267 if( filestat.st_mtime == cache->modifyTime ) retVal = FALSE;
274 * Save file time to cache.
275 * return: TRUE if time marked.
277 gboolean mgu_mark_cache( AddressCache *cache, gchar *path ) {
278 gboolean retVal = FALSE;
279 struct stat filestat;
281 if( 0 == lstat( path, &filestat ) ) {
282 cache->modifyTime = filestat.st_mtime;
290 * Free linked list of character strings.
292 void mgu_free_list( GSList *list ) {
295 g_free( node->data );
297 node = g_slist_next( node );
299 g_slist_free( list );
303 * Free linked list of character strings.
305 void mgu_free_dlist( GList *list ) {
308 g_free( node->data );
310 node = g_list_next( node );
316 * Coalesce linked list of characaters into one long string.
318 gchar *mgu_list_coalesce( GSList *list ) {
325 if( ! list ) return NULL;
327 // Calculate maximum length of text
332 len += 1 + strlen( str );
333 node = g_slist_next( node );
336 // Create new buffer.
337 buf = g_new( gchar, len+1 );
343 strcpy( start, str );
345 node = g_slist_next( node );
351 * Add address item to cache.
353 void mgu_add_cache( AddressCache *cache, AddressItem *addrItem ) {
354 cache->addressList = g_list_append( cache->addressList, addrItem );
355 cache->modified = TRUE;
358 struct mgu_error_entry {
363 static const struct mgu_error_entry mgu_error_list[] = {
364 { MGU_SUCCESS, "Success" },
365 { MGU_BAD_ARGS, "Bad arguments" },
366 { MGU_NO_FILE, "File not specified" },
367 { MGU_OPEN_FILE, "Error opening file" },
368 { MGU_ERROR_READ, "Error reading file" },
369 { MGU_EOF, "End of file encountered" },
370 { MGU_OO_MEMORY, "Error allocating memory" },
371 { MGU_BAD_FORMAT, "Bad file format" },
372 { MGU_LDAP_CONNECT, "Error connecting to LDAP server" },
373 { MGU_LDAP_INIT, "Error initializing LDAP" },
374 { MGU_LDAP_BIND, "Error binding to LDAP server" },
375 { MGU_LDAP_SEARCH, "Error searching LDAP database" },
376 { MGU_LDAP_TIMEOUT, "Timeout performing LDAP operation" },
377 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
378 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
379 { MGU_LDAP_NOENTRIES, "No LDAP entries found for search criteria" },
383 static const struct mgu_error_entry *mgu_error_find( gint err ) {
385 for ( i = 0; mgu_error_list[i].e_code != -999; i++ ) {
386 if ( err == mgu_error_list[i].e_code )
387 return & mgu_error_list[i];
393 * Return error message for specified error code.
395 gchar *mgu_error2string( gint err ) {
396 const struct mgu_error_entry *e;
397 e = mgu_error_find( err );
398 return ( e != NULL ) ? e->e_reason : "Unknown error";