2005-11-07 [colin] 1.9.99cvs17
[claws.git] / src / ldif.c
index c5dcd7816c96bf01683b25736d38f46d9c23f981..60c277cabe94b53bf13162ed992e80e457ec6c59 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 /*
  */
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <sys/stat.h>
 
-#include "intl.h"
 #include "mgutils.h"
 #include "ldif.h"
 #include "addritem.h"
 #include "addrcache.h"
 
 #include "base64.h"
+#include "utils.h"
 
 #define        LDIF_SEP_TAG    ':'
 #define        LDIF_LANG_TAG   ';'
@@ -267,7 +268,7 @@ void ldif_print_file( LdifFile *ldifFile, FILE *stream ) {
 static gint ldif_open_file( LdifFile* ldifFile ) {
        /* printf( "Opening file\n" ); */
        if( ldifFile->path ) {
-               ldifFile->file = fopen( ldifFile->path, "rb" );
+               ldifFile->file = g_fopen( ldifFile->path, "rb" );
                if( ! ldifFile->file ) {
                        /* printf( "can't open %s\n", ldifFile->path ); */
                        ldifFile->retVal = MGU_OPEN_FILE;
@@ -305,26 +306,28 @@ static void ldif_close_file( LdifFile *ldifFile ) {
 static gchar *ldif_get_line( LdifFile *ldifFile ) {
        gchar buf[ LDIFBUFSIZE ];
        gint ch;
-       gchar *ptr;
+       int i = 0;
 
-       if( feof( ldifFile->file ) ) return NULL;
+       if( feof( ldifFile->file ) ) 
+               return NULL;
 
-       ptr = buf;
-       while( TRUE ) {
-               *ptr = '\0';
+       while( i < LDIFBUFSIZE-1 ) {
                ch = fgetc( ldifFile->file );
                if( ch == '\0' || ch == EOF ) {
-                       if( *buf == '\0' ) return NULL;
+                       if( i == 0 ) return NULL;
                        break;
                }
 #if HAVE_DOSISH_SYSTEM
 #else
-               if( ch == '\r' ) continue;
+               if( ch == '\r' ) 
+                       continue;
 #endif
-               if( ch == '\n' ) break;
-               *ptr = ch;
-               ptr++;
+               if( ch == '\n' ) 
+                       break;
+               buf[i] = ch;
+               i++;
        }
+       buf[i] = '\0';
 
        /* Return a copy of buffer */
        return g_strdup( buf );
@@ -482,6 +485,14 @@ static void ldif_build_items(
                        fullName = g_strdup_printf( "%s", lastName );
                }
        }
+       
+       if (!fullName || strlen(fullName) == 0) {
+               g_free(fullName);
+               fullName = NULL;
+               if (rec->listCName)
+                       fullName = g_strdup(rec->listCName->data);
+       }
+       
        if( fullName ) {
                g_strchug( fullName ); g_strchomp( fullName );
        }
@@ -576,19 +587,19 @@ static void ldif_add_value(
        }
        g_strstrip( val );
 
-       if( g_strcasecmp( nm, LDIF_TAG_COMMONNAME ) == 0 ) {
+       if( g_utf8_collate( nm, LDIF_TAG_COMMONNAME ) == 0 ) {
                rec->listCName = g_slist_append( rec->listCName, val );
        }
-       else if( g_strcasecmp( nm, LDIF_TAG_FIRSTNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, LDIF_TAG_FIRSTNAME ) == 0 ) {
                rec->listFName = g_slist_append( rec->listFName, val );
        }
-       else if( g_strcasecmp( nm, LDIF_TAG_LASTNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, LDIF_TAG_LASTNAME ) == 0 ) {
                rec->listLName = g_slist_append( rec->listLName, val );
        }
-       else if( g_strcasecmp( nm, LDIF_TAG_NICKNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, LDIF_TAG_NICKNAME ) == 0 ) {
                rec->listNName = g_slist_append( rec->listNName, val );
        }
-       else if( g_strcasecmp( nm, LDIF_TAG_EMAIL ) == 0 ) {
+       else if( g_utf8_collate( nm, LDIF_TAG_EMAIL ) == 0 ) {
                rec->listAddress = g_slist_append( rec->listAddress, val );
        }
        else {
@@ -722,7 +733,17 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                        if( lastTag ) {
                                /* Save record */
                                fullValue = mgu_list_coalesce( listValue );
-
+                               if (fullValue && last64) {
+                                       gchar *out = g_malloc(strlen(fullValue));
+                                       int len = 0;
+                                       if ((len = base64_decode(out, fullValue,
+                                                       strlen(fullValue))) >= 0) {
+                                               g_free(fullValue);
+                                               fullValue = out;
+                                               fullValue[len] = '\0';
+                                       } else
+                                               g_free(out);
+                               }
                                /* Base-64 encoded data */
                                /*
                                if( last64 ) {
@@ -763,6 +784,17 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                                        /* Save data */
                                                        fullValue =
                                                                mgu_list_coalesce( listValue );
+                                                       if (fullValue && last64) {
+                                                               gchar *out = g_malloc(strlen(fullValue));
+                                                               int len = 0;
+                                                               if ((len = base64_decode(out, fullValue,
+                                                                               strlen(fullValue))) >= 0) {
+                                                                       g_free(fullValue);
+                                                                       fullValue = out;
+                                                                       fullValue[len] = '\0';
+                                                               } else
+                                                                       g_free(out);
+                                                       }
                                                        /* Base-64 encoded data */
                                                        /*
                                                        if( last64 ) {
@@ -817,27 +849,27 @@ static void ldif_hash_add_list( GHashTable *table, GSList *list ) {
                        gchar *key = g_strdup( tag );
 
                        rec = ldif_create_fieldrec( tag );
-                       if( g_strcasecmp( tag, LDIF_TAG_DN ) == 0 ) {
+                       if( g_utf8_collate( tag, LDIF_TAG_DN ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( "dn" );
                        }
-                       else if( g_strcasecmp( tag, LDIF_TAG_COMMONNAME ) == 0 ) {
+                       else if( g_utf8_collate( tag, LDIF_TAG_COMMONNAME ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Display Name" ) );
                        }
-                       else if( g_strcasecmp( tag, LDIF_TAG_FIRSTNAME ) == 0 ) {
+                       else if( g_utf8_collate( tag, LDIF_TAG_FIRSTNAME ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "First Name" ) );
                        }
-                       else if( g_strcasecmp( tag, LDIF_TAG_LASTNAME ) == 0 ) {
+                       else if( g_utf8_collate( tag, LDIF_TAG_LASTNAME ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Last Name" ) );
                        }
-                       else if( g_strcasecmp( tag, LDIF_TAG_NICKNAME ) == 0 ) {
+                       else if( g_utf8_collate( tag, LDIF_TAG_NICKNAME ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Nick Name" ) );
                        }
-                       else if( g_strcasecmp( tag, LDIF_TAG_EMAIL ) == 0 ) {
+                       else if( g_utf8_collate( tag, LDIF_TAG_EMAIL ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "E-Mail Address" ) );
                        }
@@ -868,7 +900,7 @@ static gint ldif_field_compare( gconstpointer ptr1, gconstpointer ptr2 ) {
                        return -1;
                }
        }
-       return g_strcasecmp( rec1->tagName, rec2->tagName );
+       return g_utf8_collate( rec1->tagName, rec2->tagName );
 }
 
 /*
@@ -907,7 +939,6 @@ static void ldif_read_tag_list( LdifFile *ldifFile ) {
        /* Process file */
        while( ! flagEOF ) {
                gchar *line = ldif_get_line( ldifFile );
-
                posCur = ftell( ldifFile->file );
                if( ldifFile->cbProgress ) {
                        /* Call progress indicator */
@@ -947,7 +978,8 @@ static void ldif_read_tag_list( LdifFile *ldifFile ) {
                                if( tagName ) {
                                        /* Add tag to list */
                                        listTags = g_slist_append( listTags, tagName );
-                                       if( g_strcasecmp(
+
+                                       if( g_utf8_collate(
                                                tagName, LDIF_TAG_EMAIL ) == 0 )
                                        {
                                                flagMail = TRUE;