Add new functions.
authorMatch Grun <match@dimensional.com>
Tue, 2 Apr 2002 05:56:49 +0000 (05:56 +0000)
committerMatch Grun <match@dimensional.com>
Tue, 2 Apr 2002 05:56:49 +0000 (05:56 +0000)
ChangeLog.claws
src/mgutils.c
src/mgutils.h
src/mutt.c
src/mutt.h

index eff46c6d10f0099cac4f7bdcc24739b3019e0c01..9c05cc5309b929e0331718130474e713abb205cc 100644 (file)
@@ -1,3 +1,11 @@
+2002-04-01 [match]     0.7.4claws58
+
+       * src/mgutils.[ch]
+               added new functions.
+       * src/mutt.[ch]
+               remove excessive duplicate address generation
+               on import.
+
 2002-04-01 [alfons]    0.7.4claws57
        
        * src/summaryview.c
 2002-04-01 [alfons]    0.7.4claws57
        
        * src/summaryview.c
index f5fd3dcaf99f15137e14d1ac3ca4cbfa729f005a..4a407d4b556efc2012eb679c33b608f63d1ae37f 100644 (file)
@@ -277,6 +277,79 @@ GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
        return list;
 }
 
        return list;
 }
 
+/*
+ * Unescape characters by removing backslash character from input string.
+ * Enter: str String to process.
+ */
+void mgu_str_unescape( gchar *str ) {
+       gchar *p;
+       gint ilen;
+
+       p = str;
+       while( *p ) {
+               if( *p == '\\' ) {
+                       ilen = strlen( p + 1 );
+                       memmove( p, p + 1, ilen );
+               }
+               p++;
+       }
+}
+
+/*
+ * Replace leading and trailing characters (eg, quotes) in input string
+ * with spaces. Only matching non-blank characters that appear at both
+ * start and end of string are replaces. Control characters are also
+ * replaced with spaces.
+ * Enter: str    String to process.
+ *        chlea  Lead character to remove.
+ *        chtail Matching trailing character.
+ */
+void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
+       gchar *as;
+       gchar *ae;
+
+       /* Search forwards for first non-space match */
+       as = str;
+       ae = -1 + str + strlen( str );
+       while( as < ae ) {
+               if( *as != ' ' ) {
+                       if( *as == chlead ) {
+                               /* Search backwards from end for match */
+                               while( ae > as ) {
+                                       if( *ae != ' ' ) {
+                                               if( *ae == chtail ) {
+                                                       *as = ' ';
+                                                       *ae = ' ';
+                                                       return;
+                                               }
+                                               if( *ae < 32 ) {
+                                                       *ae = ' ';
+                                               }
+                                               else if( *ae == 127 ) {
+                                                       *ae = ' ';
+                                               }
+                                               else {
+                                                       return;
+                                               }
+                                       }
+                                       ae--;
+                               }
+                       }
+                       if( *as < 32 ) {
+                               *as = ' ';
+                       }
+                       else if( *as == 127 ) {
+                               *as = ' ';
+                       }
+                       else {
+                               return;
+                       }
+               }
+               as++;
+       }
+       return;
+}
+
 /*
 * End of Source.
 */
 /*
 * End of Source.
 */
index b960389d2fb17402f5709726e110dbb7ad21d644..1a22991e4ceaba7465890c2140831e85365ca770 100644 (file)
@@ -58,6 +58,9 @@ gchar *mgu_replace_string     ( gchar *str, const gchar *value );
 void mgu_clear_slist           ( GSList *list );
 void mgu_clear_list            ( GList *list );
 gchar *mgu_email_check_empty   ( gchar *address );
 void mgu_clear_slist           ( GSList *list );
 void mgu_clear_list            ( GList *list );
 gchar *mgu_email_check_empty   ( gchar *address );
-GList *mgu_parse_string                ( gchar *line, const gint maxTokens, gint *tokenCnt );
+GList *mgu_parse_string                ( gchar *line, const gint maxTokens,
+                                 gint *tokenCnt );
+void mgu_str_unescape          ( gchar *str );
+void mgu_str_ltc2space         ( gchar *str, gchar chlead, gchar chtail );
 
 #endif /* __MGUTILS_H__ */
 
 #endif /* __MGUTILS_H__ */
index 44205c9a37b18e7cd548d57a10e4830a1ac7549f..2cd173ac5a6c0ab65b4a8404eb01cfc313bf68fc 100644 (file)
@@ -30,6 +30,8 @@
 #include "addrcache.h"
 
 #define MUTT_HOME_FILE  ".muttrc"
 #include "addrcache.h"
 
 #define MUTT_HOME_FILE  ".muttrc"
+#define MUTTBUFSIZE     2048
+#define        MUTT_TAG_ALIAS  "alias"
 
 /*
 * Create new object.
 
 /*
 * Create new object.
@@ -39,8 +41,8 @@ MuttFile *mutt_create() {
        muttFile = g_new0( MuttFile, 1 );
        muttFile->path = NULL;
        muttFile->file = NULL;
        muttFile = g_new0( MuttFile, 1 );
        muttFile->path = NULL;
        muttFile->file = NULL;
-       muttFile->bufptr = muttFile->buffer;
        muttFile->retVal = MGU_SUCCESS;
        muttFile->retVal = MGU_SUCCESS;
+       muttFile->uniqTable = g_hash_table_new( g_str_hash, g_str_equal );
        muttFile->cbProgress = NULL;
        return muttFile;
 }
        muttFile->cbProgress = NULL;
        return muttFile;
 }
@@ -66,6 +68,16 @@ void mutt_set_callback( MuttFile *muttFile, void *func ) {
        muttFile->cbProgress = func;
 }
 
        muttFile->cbProgress = func;
 }
 
+/*
+ * Free key in table.
+ */
+static gint mutt_free_table_vis( gpointer key, gpointer value, gpointer data ) {
+       g_free( key );
+       key = NULL;
+       value = NULL;
+       return TRUE;
+}
+
 /*
 * Free up object by releasing internal memory.
 */
 /*
 * Free up object by releasing internal memory.
 */
@@ -78,10 +90,17 @@ void mutt_free( MuttFile *muttFile ) {
        /* Free internal stuff */
        g_free( muttFile->path );
 
        /* Free internal stuff */
        g_free( muttFile->path );
 
+       /* Free unique address table */
+       g_hash_table_freeze( muttFile->uniqTable );
+       g_hash_table_foreach_remove( muttFile->uniqTable, mutt_free_table_vis, NULL );
+       g_hash_table_thaw( muttFile->uniqTable );
+       g_hash_table_destroy( muttFile->uniqTable );
+
        /* Clear pointers */
        muttFile->file = NULL;
        muttFile->path = NULL;
        muttFile->retVal = MGU_SUCCESS;
        /* Clear pointers */
        muttFile->file = NULL;
        muttFile->path = NULL;
        muttFile->retVal = MGU_SUCCESS;
+       muttFile->uniqTable = NULL;
        muttFile->cbProgress = NULL;
 
        /* Now release file object */
        muttFile->cbProgress = NULL;
 
        /* Now release file object */
@@ -103,11 +122,9 @@ void mutt_print_file( MuttFile *muttFile, FILE *stream ) {
 * return: TRUE if file opened successfully.
 */
 static gint mutt_open_file( MuttFile* muttFile ) {
 * return: TRUE if file opened successfully.
 */
 static gint mutt_open_file( MuttFile* muttFile ) {
-       /* printf( "Opening file\n" ); */
        if( muttFile->path ) {
                muttFile->file = fopen( muttFile->path, "rb" );
                if( ! muttFile->file ) {
        if( muttFile->path ) {
                muttFile->file = fopen( muttFile->path, "rb" );
                if( ! muttFile->file ) {
-                       /* printf( "can't open %s\n", muttFile->path ); */
                        muttFile->retVal = MGU_OPEN_FILE;
                        return muttFile->retVal;
                }
                        muttFile->retVal = MGU_OPEN_FILE;
                        return muttFile->retVal;
                }
@@ -119,8 +136,6 @@ static gint mutt_open_file( MuttFile* muttFile ) {
        }
 
        /* Setup a buffer area */
        }
 
        /* Setup a buffer area */
-       muttFile->buffer[0] = '\0';
-       muttFile->bufptr = muttFile->buffer;
        muttFile->retVal = MGU_SUCCESS;
        return muttFile->retVal;
 }
        muttFile->retVal = MGU_SUCCESS;
        return muttFile->retVal;
 }
@@ -177,14 +192,18 @@ static gchar *mutt_get_line( MuttFile *muttFile, gboolean *flagCont ) {
 }
 
 /*
 }
 
 /*
-* Parsed address data.
-*/
+ * Parsed address data.
+ */
 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
 struct _Mutt_ParsedRec_ {
        gchar *address;
        gchar *name;
 };
 
 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
 struct _Mutt_ParsedRec_ {
        gchar *address;
        gchar *name;
 };
 
+/*
+ * Free data record.
+ * Enter: rec Data record.
+ */
 static mutt_free_rec( Mutt_ParsedRec *rec ) {
        if( rec ) {
                g_free( rec->address );
 static mutt_free_rec( Mutt_ParsedRec *rec ) {
        if( rec ) {
                g_free( rec->address );
@@ -195,7 +214,11 @@ static mutt_free_rec( Mutt_ParsedRec *rec ) {
        }
 }
 
        }
 }
 
-void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
+/*
+ * Print data record.
+ * Enter: rec    Data record.
+ *        stream File.
+ */void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
        fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
 }
 
        fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
 }
 
@@ -302,7 +325,6 @@ static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
                rec->address = address;
                rec->name = name;
                list = g_slist_append( list, rec );
                rec->address = address;
                rec->name = name;
                list = g_slist_append( list, rec );
-
                cnt++;
 
                /* mutt_print_rec( rec, stdout ); */
                cnt++;
 
                /* mutt_print_rec( rec, stdout ); */
@@ -312,23 +334,73 @@ static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
 }
 
 /*
 }
 
 /*
-* Build address book entries.
-* Enter: aliasName Alias,
-*        listAddr  List of address items.
-*        addrCount Address list count.
-*        cache     Cache to update.
-*/
+ * Insert person and address into address cache.
+ * Enter: muttFile MUTT control data.
+ *        cache    Address cache.
+ *        address  E-Mail address.
+ *        name     Name.
+ * Return: E-Mail object, either inserted or found in hash table.
+ */
+static ItemEMail *mutt_insert_table(
+               MuttFile *muttFile, AddressCache *cache, gchar *address,
+               gchar *name )
+{
+       ItemPerson *person;
+       ItemEMail *email;
+       gchar *key;
+
+       /* Test whether address already in hash table */
+       key = g_strdup( address );
+       g_strdown( key );
+       email = g_hash_table_lookup( muttFile->uniqTable, key );
+
+       if( email == NULL ) {
+               /* No - create person */
+               person = addritem_create_item_person();
+               addritem_person_set_common_name( person, name );
+               addrcache_id_person( cache, person );
+               addrcache_add_person( cache, person );
+
+               /* Add email for person */
+               email = addritem_create_item_email();
+               addritem_email_set_address( email, address );
+               addrcache_id_email( cache, email );
+               addrcache_person_add_email( cache, person, email );
+
+               /* Insert entry */
+               g_hash_table_insert( muttFile->uniqTable, key, email );
+       }
+       else {
+               /* Yes - update person with longest name */
+               person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+               if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
+                       addritem_person_set_common_name( person, name );
+               }
+
+               /* Free up */
+               g_free( key );
+       }
+
+       return email;
+}
+
+/*
+ * Build address book entries.
+ * Enter: muttFile  MUTT control data.
+ *        cache     Address cache.
+ *        aliasName Alias,
+ *        listAddr  List of address items.
+ *        addrCount Address list count.
+ */
 static void mutt_build_address(
 static void mutt_build_address(
-               gchar *aliasName, GSList *listAddr, gint addrCount,
-               AddressCache *cache )
+               MuttFile *muttFile, AddressCache *cache,
+               gchar *aliasName, GSList *listAddr, gint addrCount )
 {
        GSList *node = NULL;
 {
        GSList *node = NULL;
-       ItemPerson *person;
        ItemEMail *email;
        ItemGroup *group;
        Mutt_ParsedRec *rec;
 
        ItemEMail *email;
        ItemGroup *group;
        Mutt_ParsedRec *rec;
 
-       email = NULL;
        group = NULL;
        if( listAddr != NULL && addrCount > 1 ) {
                group = addritem_create_item_group();
        group = NULL;
        if( listAddr != NULL && addrCount > 1 ) {
                group = addritem_create_item_group();
@@ -337,24 +409,14 @@ static void mutt_build_address(
                addrcache_add_group( cache, group );
        }
 
                addrcache_add_group( cache, group );
        }
 
+       email = NULL;
        node = listAddr;
        while( node ) {
                rec = node->data;
 
        node = listAddr;
        while( node ) {
                rec = node->data;
 
-               /* Create person */
-               person = addritem_create_item_person();
-               addritem_person_set_common_name( person, rec->name );
-               addrcache_id_person( cache, person );
-               addrcache_add_person( cache, person );
-               if( addrCount < 2 ) {
-                       addritem_person_set_nick_name( person, aliasName );
-               }
-
-               /* Add email for person */
-               email = addritem_create_item_email();
-               addritem_email_set_address( email, rec->address );
-               addrcache_id_email( cache, email );
-               addrcache_person_add_email( cache, person, email );
+               /* Insert person/email */
+               email = mutt_insert_table(
+                               muttFile, cache, rec->address, rec->name );
 
                /* Add email to group */
                if( group ) {
 
                /* Add email to group */
                if( group ) {
@@ -362,18 +424,17 @@ static void mutt_build_address(
                }
 
                mutt_free_rec( rec );
                }
 
                mutt_free_rec( rec );
-               rec = NULL;
-
                node = g_slist_next( node );
        }
 }
 
 /*
                node = g_slist_next( node );
        }
 }
 
 /*
-* Parse address line adn build address items.
-* Enter: line  Data record.
-*        cache Address cache.
-*/
-static void mutt_build_items( gchar *line, AddressCache *cache ) {
+ * Parse address line adn build address items.
+ * Enter: muttFile MUTT control data.
+ *        cache    Address cache.
+ *        line     Data record.
+ */
+static void mutt_build_items( MuttFile *muttFile, AddressCache *cache, gchar *line ) {
        GList *list, *node;
        gint tCount, aCount;
        gchar *aliasTag, *aliasName, *recipient;
        GList *list, *node;
        gint tCount, aCount;
        gchar *aliasTag, *aliasName, *recipient;
@@ -396,13 +457,13 @@ static void mutt_build_items( gchar *line, AddressCache *cache ) {
        recipient = node->data;
 
        addrList = NULL;
        recipient = node->data;
 
        addrList = NULL;
-       if( strcmp( aliasTag, "alias" ) == 0 ) {
+       if( strcmp( aliasTag, MUTT_TAG_ALIAS ) == 0 ) {
                aCount = 0;
                /* printf( "aliasName :%s:\n", aliasName ); */
                /* printf( "recipient :%s:\n", recipient ); */
                addrList = mutt_parse_rcplist( recipient, &aCount );
                /* printf( "---\n" ); */
                aCount = 0;
                /* printf( "aliasName :%s:\n", aliasName ); */
                /* printf( "recipient :%s:\n", recipient ); */
                addrList = mutt_parse_rcplist( recipient, &aCount );
                /* printf( "---\n" ); */
-               mutt_build_address( aliasName, addrList, aCount, cache );
+               mutt_build_address( muttFile, cache, aliasName, addrList, aCount );
        }
 
        mgu_free_dlist( list );
        }
 
        mgu_free_dlist( list );
@@ -411,8 +472,10 @@ static void mutt_build_items( gchar *line, AddressCache *cache ) {
 }
 
 /*
 }
 
 /*
-* Read file data into address cache.
-*/
+ * Read file data into address cache.
+ * Enter: muttFile MUTT control data.
+ *        cache Address cache.
+ */
 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
        GSList *listValue = NULL;
        gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
        GSList *listValue = NULL;
        gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
@@ -440,7 +503,7 @@ static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
                        /* Save data */
                        lineValue = mgu_list_coalesce( listValue );
                        if( lineValue ) {
                        /* Save data */
                        lineValue = mgu_list_coalesce( listValue );
                        if( lineValue ) {
-                               mutt_build_items( lineValue, cache );
+                               mutt_build_items( muttFile, cache, lineValue );
                        }
                        g_free( lineValue );
                        lineValue = NULL;
                        }
                        g_free( lineValue );
                        lineValue = NULL;
index 4cbd2e7a972ff72c8b4b9b9614b0dd86e877ebe0..2aa41e5bb9018f0ae2c948c82a4e8c5da67c4b78 100644 (file)
 
 #include "addrcache.h"
 
 
 #include "addrcache.h"
 
-#define MUTTBUFSIZE         2048
-
-#define        MUTT_TAG_ALIAS      "alias"
-
 /*
 * Typical MUTT entry:
 *
 /*
 * Typical MUTT entry:
 *
@@ -57,9 +53,8 @@ typedef struct _MuttFile MuttFile;
 struct _MuttFile {
        FILE  *file;
        gchar *path;
 struct _MuttFile {
        FILE  *file;
        gchar *path;
-       gchar *bufptr;
-       gchar buffer[ MUTTBUFSIZE ];
        gint  retVal;
        gint  retVal;
+       GHashTable *uniqTable;
        void  (*cbProgress)( void *, void *, void * );
 };
 
        void  (*cbProgress)( void *, void *, void * );
 };