inital gtk2 patch
[claws.git] / src / vcard.c
index 0f06b3d9ae948730f41d1fa34557903795060480..fe3ee5b19456d5a9ead852cb31bf4d36fcb60d3c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001 Match Grun
+ * Copyright (C) 2001-2002 Match Grun
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -31,6 +31,7 @@
 #include "vcard.h"
 #include "addritem.h"
 #include "addrcache.h"
+#include "adbookbase.h"
 
 #define GNOMECARD_DIR     ".gnome"
 #define GNOMECARD_FILE    "GnomeCard"
 VCardFile *vcard_create() {
        VCardFile *cardFile;
        cardFile = g_new0( VCardFile, 1 );
-       cardFile->name = NULL;
-       cardFile->path = NULL;
-       cardFile->file = NULL;
-       cardFile->bufptr = cardFile->buffer;
+       cardFile->type = ADBOOKTYPE_VCARD;
        cardFile->addressCache = addrcache_create();
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
+
+       cardFile->file = NULL;
+       cardFile->path = NULL;
+       cardFile->bufptr = cardFile->buffer;
        return cardFile;
 }
 
@@ -60,8 +61,7 @@ VCardFile *vcard_create() {
 */
 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
        g_return_if_fail( cardFile != NULL );
-       cardFile->name = mgu_replace_string( cardFile->name, value );
-       g_strstrip( cardFile->name );
+       addrcache_set_name( cardFile->addressCache, value );
 }
 void vcard_set_file( VCardFile* cardFile, const gchar *value ) {
        g_return_if_fail( cardFile != NULL );
@@ -71,29 +71,35 @@ void vcard_set_file( VCardFile* cardFile, const gchar *value ) {
 }
 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
        g_return_if_fail( cardFile != NULL );
-       cardFile->accessFlag = value;
+       cardFile->addressCache->accessFlag = value;
 }
 
 /*
 * Test whether file was modified since last access.
 * Return: TRUE if file was modified.
 */
-gboolean vcard_get_modified( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
+gboolean vcard_get_modified( VCardFile *cardFile ) {
+       g_return_val_if_fail( cardFile != NULL, FALSE );
+       cardFile->addressCache->modified =
+               addrcache_check_file( cardFile->addressCache, cardFile->path );
+       return cardFile->addressCache->modified;
 }
-gboolean vcard_get_accessed( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
+gboolean vcard_get_accessed( VCardFile *cardFile ) {
+       g_return_val_if_fail( cardFile != NULL, FALSE );
+       return cardFile->addressCache->accessFlag;
 }
 
 /*
 * Test whether file was read.
 * Return: TRUE if file was read.
 */
-gboolean vcard_get_read_flag( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return vcardFile->addressCache->dataRead;
+gboolean vcard_get_read_flag( VCardFile *cardFile ) {
+       g_return_val_if_fail( cardFile != NULL, FALSE );
+       return cardFile->addressCache->dataRead;
+}
+void vcard_set_read_flag( VCardFile *cardFile, const gboolean value ) {
+       g_return_if_fail( cardFile != NULL );
+       cardFile->addressCache->dataRead = value;
 }
 
 /*
@@ -111,7 +117,7 @@ ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
 }
 gchar *vcard_get_name( VCardFile *cardFile ) {
        g_return_val_if_fail( cardFile != NULL, NULL );
-       return cardFile->name;
+       return addrcache_get_name( cardFile->addressCache );
 }
 
 /*
@@ -140,25 +146,24 @@ void vcard_free( VCardFile *cardFile ) {
        /* Close file */
        if( cardFile->file ) fclose( cardFile->file );
 
-       /* Free internal stuff */
-       g_free( cardFile->name );
-       g_free( cardFile->path );
-
        /* Clear cache */
        addrcache_clear( cardFile->addressCache );
        addrcache_free( cardFile->addressCache );
 
+       /* Free internal stuff */
+       g_free( cardFile->path );
+
        /* Clear pointers */
        cardFile->file = NULL;
-       cardFile->name = NULL;
        cardFile->path = NULL;
+       cardFile->bufptr = NULL;
+
+       cardFile->type = ADBOOKTYPE_NONE;
        cardFile->addressCache = NULL;
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
 
        /* Now release file object */
        g_free( cardFile );
-
 }
 
 /*
@@ -168,7 +173,6 @@ void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
        g_return_if_fail( cardFile != NULL );
 
        fprintf( stream, "VCardFile:\n" );
-       fprintf( stream, "     name: '%s'\n", cardFile->name );
        fprintf( stream, "file spec: '%s'\n", cardFile->path );
        fprintf( stream, "  ret val: %d\n",   cardFile->retVal );
        addrcache_print( cardFile->addressCache, stream );
@@ -185,7 +189,7 @@ static gint vcard_open_file( VCardFile* cardFile ) {
        /* fprintf( stdout, "Opening file\n" ); */
        cardFile->addressCache->dataRead = FALSE;
        if( cardFile->path ) {
-               cardFile->file = fopen( cardFile->path, "r" );
+               cardFile->file = fopen( cardFile->path, "rb" );
                if( ! cardFile->file ) {
                        /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
                        cardFile->retVal = MGU_OPEN_FILE;
@@ -277,10 +281,8 @@ static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
        while( line ) {
                listQP = g_slist_append( listQP, line );
                len = strlen( line ) - 1;
-               if( len > 0 ) {
-                       if( line[ len ] != '=' ) break;
-                       line[ len ] = '\0';
-               }
+               if( line[ len ] != '=' ) break;
+               line[ len ] = '\0';
                line = vcard_get_line( cardFile );
        }
 
@@ -301,6 +303,7 @@ static gchar *vcard_get_tagname( char* line, gchar dlm ) {
        gint len = 0;
        gchar *tag = NULL;
        gchar *lptr = line;
+
        while( *lptr++ ) {
                if( *lptr == dlm ) {
                        len = lptr - line;
@@ -342,29 +345,12 @@ static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
        return value;
 }
 
-/*
-* Dump linked lists of character strings (for debug).
-*/
-static void vcard_dump_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList *listID, FILE *stream ) {
-       fprintf( stream, "dump name\n" );
-       fprintf( stream, "------------\n" );
-       mgu_print_list( listName, stdout );
-       fprintf( stream, "dump address\n" );
-       fprintf( stream, "------------\n" );
-       mgu_print_list( listAddr, stdout );
-       fprintf( stream, "dump remarks\n" );
-       fprintf( stdout, "------------\n" );
-       mgu_print_list( listRem, stdout );
-       fprintf( stream, "dump id\n" );
-       fprintf( stdout, "------------\n" );
-       mgu_print_list( listID, stdout );
-}
-
 /*
 * Build an address list entry and append to list of address items.
 */
-static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *listAddr, GSList *listRem,
-                               GSList *listID )
+static void vcard_build_items(
+       VCardFile *cardFile, GSList *listName, GSList *listAddr,
+       GSList *listRem, GSList *listID )
 {
        GSList *nodeName = listName;
        GSList *nodeID = listID;
@@ -383,7 +369,8 @@ static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *li
                                if( nodeRemarks ) {
                                        if( str ) {
                                                if( g_strcasecmp( str, "internet" ) != 0 ) {
-                                                       if( *str != '\0' ) addritem_email_set_remarks( email, str );
+                                                       if( *str != '\0' )
+                                                               addritem_email_set_remarks( email, str );
                                                }
                                        }
                                }
@@ -396,14 +383,14 @@ static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *li
                if( person->listEMail ) {
                        addrcache_id_person( cardFile->addressCache, person );
                        addrcache_add_person( cardFile->addressCache, person );
+                       if( nodeID ) {
+                               str = nodeID->data;
+                               addritem_person_set_external_id( person, str );
+                       }
                }
                else {
                        addritem_free_item_person( person );
                }
-               if( nodeID ) {
-                       str = nodeID->data;
-                       addritem_person_set_external_id( person, str );
-               }
                nodeName = g_slist_next( nodeName );
                nodeID = g_slist_next( nodeID );
        }
@@ -474,61 +461,70 @@ static void vcard_read_file( VCardFile *cardFile ) {
 
                /* Parse line */
                tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
-               if( tagtemp ) {
-                       /* fprintf( stdout, "\ttemp:  %s\n", tagtemp ); */
-                       tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
-                       tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
-                       tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
-                       if( tagname == NULL ) {
-                               tagname = tagtemp;
-                               tagtemp = NULL;
-                       }
+               if( tagtemp == NULL ) {
+                       g_free( line );
+                       continue;
+               }
+
+               /* fprintf( stdout, "\ttemp:  %s\n", tagtemp ); */
+               tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
+               if( tagvalue == NULL ) {
+                       g_free( tagtemp );
+                       g_free( line );
+                       continue;
+               }
 
-                       /* fprintf( stdout, "\tname:  %s\n", tagname ); */
-                       /* fprintf( stdout, "\ttype:  %s\n", tagtype ); */
-                       /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
+               tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
+               tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
+               if( tagname == NULL ) {
+                       tagname = tagtemp;
+                       tagtemp = NULL;
+               }
 
-                       if( tagvalue ) {
-                               if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
-                                       /* Quoted-Printable: could span multiple lines */
-                                       tagvalue = vcard_read_qp( cardFile, tagvalue );
-                                       vcard_unescape_qp( tagvalue );
-                                       /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
-                               }
+               /* fprintf( stdout, "\tname:  %s\n", tagname ); */
+               /* fprintf( stdout, "\ttype:  %s\n", tagtype ); */
+               /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
 
-                               if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
-                                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                       /* fprintf( stdout, "start card\n" ); */
-                                       vcard_free_lists( listName, listAddress, listRemarks, listID );
-                                       listName = listAddress = listRemarks = listID = NULL;
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
-                                       /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
-                                       listName = g_slist_append( listName, g_strdup( tagvalue ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
-                                       /* fprintf( stdout, "- address: %s\n", tagvalue ); */
-                                       listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
-                                       listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
-                                       /* fprintf( stdout, "- id: %s\n", tagvalue ); */
-                                       listID = g_slist_append( listID, g_strdup( tagvalue ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
-                                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                       /* vCard is complete */
-                                       /* fprintf( stdout, "end card\n--\n" ); */
-                                       /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
-                                       vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
-                                       vcard_free_lists( listName, listAddress, listRemarks, listID );
-                                       listName = listAddress = listRemarks = listID = NULL;
-                               }
-                               g_free( tagvalue );
-                       }
-                       g_free( tagname );
-                       g_free( tagtype );
+               if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
+                       /* Quoted-Printable: could span multiple lines */
+                       tagvalue = vcard_read_qp( cardFile, tagvalue );
+                       vcard_unescape_qp( tagvalue );
+                       /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
+               }
+
+               if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
+                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                       /* fprintf( stdout, "start card\n" ); */
+                       vcard_free_lists( listName, listAddress, listRemarks, listID );
+                       listName = listAddress = listRemarks = listID = NULL;
+               }
+               if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
+                       /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
+                       listName = g_slist_append( listName, g_strdup( tagvalue ) );
+               }
+               if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
+                       /* fprintf( stdout, "- address: %s\n", tagvalue ); */
+                       listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
+                       listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
                }
+               if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
+                       /* fprintf( stdout, "- id: %s\n", tagvalue ); */
+                       listID = g_slist_append( listID, g_strdup( tagvalue ) );
+               }
+               if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
+                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                       /* vCard is complete */
+                       /* fprintf( stdout, "end card\n--\n" ); */
+                       /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
+                       vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
+                       vcard_free_lists( listName, listAddress, listRemarks, listID );
+                       listName = listAddress = listRemarks = listID = NULL;
+               }
+
+               g_free( tagname );
+               g_free( tagtype );
+               g_free( tagvalue );
+               g_free( tagtemp );
                g_free( line );
                line = NULL;
        }
@@ -548,7 +544,7 @@ gint vcard_read_data( VCardFile *cardFile ) {
        g_return_val_if_fail( cardFile != NULL, -1 );
 
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
+       cardFile->addressCache->accessFlag = FALSE;
        if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
                addrcache_clear( cardFile->addressCache );
                vcard_open_file( cardFile );
@@ -601,6 +597,7 @@ GList *vcard_get_all_persons( VCardFile *cardFile ) {
 */
 gboolean vcard_validate( const VCardFile *cardFile ) {
        gboolean retVal;
+       gchar *name;
 
        g_return_val_if_fail( cardFile != NULL, FALSE );
 
@@ -611,8 +608,9 @@ gboolean vcard_validate( const VCardFile *cardFile ) {
        else {
                retVal = FALSE;
        }
-       if( cardFile->name ) {
-               if( strlen( cardFile->name ) < 1 ) retVal = FALSE;
+       name = addrcache_get_name( cardFile->addressCache );
+       if( name ) {
+               if( strlen( name ) < 1 ) retVal = FALSE;
        }
        else {
                retVal = FALSE;
@@ -628,7 +626,7 @@ gboolean vcard_validate( const VCardFile *cardFile ) {
 *      be g_free() when done.
 */
 gchar *vcard_find_gnomecard( void ) {
-       gchar *homedir;
+       const gchar *homedir;
        gchar buf[ WORK_BUFLEN ];
        gchar str[ WORK_BUFLEN ];
        gchar *fileSpec;
@@ -651,7 +649,7 @@ gchar *vcard_find_gnomecard( void ) {
        strcat( str, GNOMECARD_FILE );
 
        fileSpec = NULL;
-       if( ( fp = fopen( str, "r" ) ) != NULL ) {
+       if( ( fp = fopen( str, "rb" ) ) != NULL ) {
                /* Read configuration file */
                lenlbl = strlen( GNOMECARD_SECTION );
                while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
@@ -709,37 +707,45 @@ gint vcard_test_read_file( const gchar *fileSpec ) {
 
                        /* Parse line */
                        tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
-                       if( tagtemp ) {
-                               tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
-                               tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
-                               tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
-                               if( tagname == NULL ) {
-                                       tagname = tagtemp;
-                                       tagtemp = NULL;
-                               }
+                       if( tagtemp == NULL ) {
+                               g_free( line );
+                               continue;
+                       }
 
-                               if( tagvalue ) {
-                                       if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
-                                               /* Quoted-Printable: could span multiple lines */
-                                               tagvalue = vcard_read_qp( cardFile, tagvalue );
-                                               vcard_unescape_qp( tagvalue );
-                                       }
-                                       if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
-                                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                               haveStart = TRUE;
-                                       }
-                                       if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
-                                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                               /* vCard is complete */
-                                               if( haveStart ) cardFile->retVal = MGU_SUCCESS;
-                                       }
-                                       g_free( tagvalue );
-                               }
-                               g_free( tagname );
-                               g_free( tagtype );
+                       tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
+                       if( tagvalue == NULL ) {
+                               g_free( tagtemp );
+                               g_free( line );
+                               continue;
                        }
+
+                       tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
+                       tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
+                       if( tagname == NULL ) {
+                               tagname = tagtemp;
+                               tagtemp = NULL;
+                       }
+
+                       if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
+                               /* Quoted-Printable: could span multiple lines */
+                               tagvalue = vcard_read_qp( cardFile, tagvalue );
+                               vcard_unescape_qp( tagvalue );
+                       }
+                       if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
+                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                               haveStart = TRUE;
+                       }
+                       if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
+                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                               /* vCard is complete */
+                               if( haveStart ) cardFile->retVal = MGU_SUCCESS;
+                       }
+
+                       g_free( tagname );
+                       g_free( tagtype );
+                       g_free( tagvalue );
+                       g_free( tagtemp );
                        g_free( line );
-                       line = NULL;
                }
                vcard_close_file( cardFile );
        }