we're loading functions explicitly from dynamically loaded so's, so remove explicit...
[claws.git] / src / jpilot.c
index 78108a6a39fd19933c253540cc9b9baa26bea549..046cdb1dcab2c204dd60925c46354c1edd8ebb65 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
@@ -35,7 +35,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
-#include <dlfcn.h>
+#include <netinet/in.h>
 
 #ifdef HAVE_LIBPISOCK_PI_ARGS_H
 #  include <libpisock/pi-args.h>
@@ -51,6 +51,7 @@
 #include "addritem.h"
 #include "addrcache.h"
 #include "jpilot.h"
+#include "adbookbase.h"
 
 #define JPILOT_DBHOME_DIR   ".jpilot"
 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
@@ -138,12 +139,12 @@ typedef struct {
 
 /* Shamelessly copied from JPilot (libplugin.h) */
 typedef struct {
-   unsigned long header_len;
-   unsigned long header_version;
-   unsigned long rec_len;
-   unsigned long unique_id;
-   unsigned long rt; /* Record Type */
-   unsigned char attrib;
+       unsigned long header_len;
+       unsigned long header_version;
+       unsigned long rec_len;
+       unsigned long unique_id;
+       unsigned long rt; /* Record Type */
+       unsigned char attrib;
 } PC3RecordHeader;
 
 /*
@@ -152,15 +153,16 @@ typedef struct {
 JPilotFile *jpilot_create() {
        JPilotFile *pilotFile;
        pilotFile = g_new0( JPilotFile, 1 );
-       pilotFile->name = NULL;
+       pilotFile->type = ADBOOKTYPE_JPILOT;
+       pilotFile->addressCache = addrcache_create();
+       pilotFile->accessFlag = FALSE;
+       pilotFile->retVal = MGU_SUCCESS;
+
        pilotFile->file = NULL;
        pilotFile->path = NULL;
-       pilotFile->addressCache = addrcache_create();
        pilotFile->readMetadata = FALSE;
        pilotFile->customLabels = NULL;
        pilotFile->labelInd = NULL;
-       pilotFile->retVal = MGU_SUCCESS;
-       pilotFile->accessFlag = FALSE;
        pilotFile->havePC3 = FALSE;
        pilotFile->pc3ModifyTime = 0;
        return pilotFile;
@@ -181,7 +183,7 @@ JPilotFile *jpilot_create_path( const gchar *path ) {
 */
 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
        g_return_if_fail( pilotFile != NULL );
-       pilotFile->name = mgu_replace_string( pilotFile->name, value );
+       addrcache_set_name( pilotFile->addressCache, value );
 }
 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
        g_return_if_fail( pilotFile != NULL );
@@ -204,7 +206,7 @@ ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
 }
 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
        g_return_val_if_fail( pilotFile != NULL, NULL );
-       return pilotFile->name;
+       return addrcache_get_name( pilotFile->addressCache );
 }
 
 /*
@@ -281,9 +283,9 @@ GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
 }
 
 /*
- * Return filespec of PC3 file corresponding to JPilot PDB file.
- * Note: Filespec should be g_free() when done.
- */
+* Return filespec of PC3 file corresponding to JPilot PDB file.
+* Note: Filespec should be g_free() when done.
+*/
 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
        gchar *fileSpec, *r;
        gint i, len, pos;
@@ -349,7 +351,8 @@ static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
        gchar *pcFile;
 
        /* Check main file */
-       if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) ) return TRUE;
+       if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
+               return TRUE;
 
        /* Test PC3 file */
        if( ! pilotFile->havePC3 ) return FALSE;
@@ -382,21 +385,27 @@ gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
 void jpilot_free( JPilotFile *pilotFile ) {
        g_return_if_fail( pilotFile != NULL );
 
-       /* Free internal stuff */
-       g_free( pilotFile->path );
-
        /* Release custom labels */
        jpilot_clear_custom_labels( pilotFile );
 
        /* Clear cache */
        addrcache_clear( pilotFile->addressCache );
        addrcache_free( pilotFile->addressCache );
-       pilotFile->addressCache = NULL;
+
+       /* Free internal stuff */
+       g_free( pilotFile->path );
+
+       pilotFile->file = NULL;
+       pilotFile->path = NULL;
        pilotFile->readMetadata = FALSE;
-       pilotFile->accessFlag = FALSE;
        pilotFile->havePC3 = FALSE;
        pilotFile->pc3ModifyTime = 0;
 
+       pilotFile->type = ADBOOKTYPE_NONE;
+       pilotFile->addressCache = NULL;
+       pilotFile->accessFlag = FALSE;
+       pilotFile->retVal = MGU_SUCCESS;
+
        /* Now release file object */
        g_free( pilotFile );
 }
@@ -436,7 +445,7 @@ void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
        addrcache_print( pilotFile->addressCache, stream );
        fprintf( stream, "  ret val: %d\n", pilotFile->retVal );
        fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
-       fprintf( stream, " pc3 time: %d\n", pilotFile->pc3ModifyTime );
+       fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
        addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
 }
 
@@ -464,7 +473,7 @@ void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
        }
        addrcache_print( pilotFile->addressCache, stream );
        fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
-       fprintf( stream, " pc3 time: %d\n", pilotFile->pc3ModifyTime );
+       fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
 }
 
 /* Shamelessly copied from JPilot (libplugin.c) */
@@ -724,7 +733,7 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        num = fread(&l, sizeof(l), 1, pc_in);
        if (feof(pc_in)) {
                return -1;
-       }      
+       }
        if (num!=1) {
                return num;
        }
@@ -736,7 +745,7 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
        if (feof(pc_in)) {
                return -1;
-       }      
+       }
        if (num!=1) {
                return num;
        }
@@ -750,17 +759,17 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        PC3RecordHeader header;
        int rec_len, num;
        char *record;
-   
+
        if(feof(in)) {
-               return MGU_EOF;
+               return MGU_EOF;
        }
        num = read_header(in, &header);
        if (num < 1) {
                if (ferror(in)) {
-                       return MGU_ERROR_READ;
+                       return MGU_ERROR_READ;
                }
                if (feof(in)) {
-                       return MGU_EOF;
+                       return MGU_EOF;
                }
        }
        rec_len = header.rec_len;
@@ -772,7 +781,7 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        if (num != 1) {
                if (ferror(in)) {
                        free(record);
-                       return MGU_ERROR_READ;
+                       return MGU_ERROR_READ;
                }
        }
        br->rt = header.rt;
@@ -810,12 +819,12 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        recs_returned = 0;
 
        if( pilotFile->path == NULL ) {
-               return MGU_BAD_ARGS;
+               return MGU_BAD_ARGS;
        }
-       
+
        in = fopen( pilotFile->path, "r" );
        if (!in) {
-               return MGU_OPEN_FILE;
+               return MGU_OPEN_FILE;
        }
 
        /* Read the database header */
@@ -826,6 +835,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        return MGU_ERROR_READ;
                }
                if (feof(in)) {
+                       fclose(in);
                        return MGU_EOF;
                }
        }
@@ -835,14 +845,15 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        num_records = dbh.number_of_records;
        out_of_order = 0;
        prev_offset = 0;
-   
-       for (i=1; i<num_records+1; i++) {
+
+       for (i = 1; i < num_records + 1; i++) {
                num = fread(&rh, sizeof(record_header), 1, in);
                if (num != 1) {
                        if (ferror(in)) {
                                break;
                        }
                        if (feof(in)) {
+                               fclose(in);
                                return MGU_EOF;
                        }
                }
@@ -920,7 +931,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        temp_br->size = rec_size;
 
                        *records = g_list_append(*records, temp_br);
-        
+
                        recs_returned++;
                }
        }
@@ -933,7 +944,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        pc_in = fopen( pcFile, "r");
        g_free( pcFile );
 
-       if( pc_in==NULL ) {
+       if( pc_in == NULL ) {
                return MGU_SUCCESS;
        }
 
@@ -973,8 +984,8 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        return MGU_SUCCESS;
 }
 
-#define        FULLNAME_BUFSIZE   256
-#define        EMAIL_BUFSIZE      256
+#define FULLNAME_BUFSIZE       256
+#define EMAIL_BUFSIZE          256
 /*
  * Unpack address, building new data inside cache.
  */
@@ -1001,7 +1012,7 @@ static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder
                addrEnt = addr.entry;
                attrib = buf->attrib;
                unique_id = buf->unique_id;
-               cat_id = attrib & 0x0F;
+               cat_id = attrib & 0x0F;
 
                *fullName = *bufEMail = '\0';
                if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
@@ -1048,8 +1059,8 @@ static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder
                                        email = addritem_create_item_email();
                                        addritem_email_set_address( email, bufEMail );
                                        addrcache_id_email( pilotFile->addressCache, email );
-                                       addrcache_person_add_email(
-                                               pilotFile->addressCache, person, email );
+                                       addrcache_person_add_email
+                                               pilotFile->addressCache, person, email );
                                }
                        }
                }
@@ -1074,10 +1085,9 @@ static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder
                                        addritem_email_set_address( email, bufEMail );
                                        addritem_email_set_remarks( email, ai->labels[ind] );
                                        addrcache_id_email( pilotFile->addressCache, email );
-                                       addrcache_person_add_email(
-                                               pilotFile->addressCache, person, email );
+                                       addrcache_person_add_email
+                                               pilotFile->addressCache, person, email );
                                }
-
                        }
 
                        node = g_list_next( node );
@@ -1086,15 +1096,13 @@ static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder
                if( person->listEMail ) {
                        if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
                                /* Add to specified category */
-                               addrcache_folder_add_person(
-                                       pilotFile->addressCache, folderInd[cat_id], person );
-                       }
-                       else {
+                               addrcache_folder_add_person
+                                       ( pilotFile->addressCache, folderInd[cat_id], person );
+                       } else {
                                /* Add to root folder */
                                addrcache_add_person( pilotFile->addressCache, person );
                        }
-               }
-               else {
+               } else {
                        addritem_free_item_person( person );
                        person = NULL;
                }
@@ -1195,9 +1203,6 @@ static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
                free(buf);
        }
        if( num <= 0 ) {
-               /*
-               fprintf( stderr, "error reading '%s'\n", pilotFile->path );
-               */
                pilotFile->retVal = MGU_ERROR_READ;
                return pilotFile->retVal;
        }
@@ -1320,6 +1325,7 @@ GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
 /*
 * Load list with character strings of label names. Only none blank names
 * are loaded.
+* Return: list of labels. Should by g_free()'d when done.
 */
 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
        gint i;
@@ -1504,8 +1510,9 @@ gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
 * Validate that all parameters specified.
 * Return: TRUE if data is good.
 */
-gboolean jpilot_validate( const JPilotFile *pilotFile ) {
+gboolean jpilot_validate( JPilotFile *pilotFile ) {
        gboolean retVal;
+       gchar *name;
 
        g_return_val_if_fail( pilotFile != NULL, FALSE );
 
@@ -1516,8 +1523,9 @@ gboolean jpilot_validate( const JPilotFile *pilotFile ) {
        else {
                retVal = FALSE;
        }
-       if( pilotFile->name ) {
-               if( strlen( pilotFile->name ) < 1 ) retVal = FALSE;
+       name = jpilot_get_name( pilotFile );
+       if( name ) {
+               if( strlen( name ) < 1 ) retVal = FALSE;
        }
        else {
                retVal = FALSE;
@@ -1598,7 +1606,7 @@ gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName
        if( labelName ) {
                node = pilotFile->customLabels;
                while( node ) {
-                       if( g_strcasecmp( labelName, node->data ) == 0 ) {
+                       if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
                                retVal = TRUE;
                                break;
                        }
@@ -1613,26 +1621,6 @@ gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName
 * Return: TRUE if library available.
 */
 gboolean jpilot_test_pilot_lib( void ) {
-       void *handle, *fun;
-
-       handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
-       if( ! handle ) {
-               return FALSE;
-       }
-
-       /* Test for symbols we need */
-       fun = dlsym( handle, "unpack_Address" );
-       if( ! fun ) {
-               dlclose( handle );
-               return FALSE;
-       }
-
-       fun = dlsym( handle, "unpack_AddressAppInfo" );
-       if( ! fun ) {
-               dlclose( handle );
-               return FALSE;
-       }
-       dlclose( handle );
        return TRUE;
 }