2006-04-06 [mones] 2.1.0cvs7
[claws.git] / src / addrindex.c
index 76f029361d908d50383e33f61c38e1d55dd4226e..96f8e8bcc291e242fec8b464b7f638623e31b5ca 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2004 Match Grun
+ * Copyright (C) 2001-2006 Match Grun and the Sylpheed-Claws team
  *
  * 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
@@ -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.
  */
 
 /*
@@ -28,8 +28,8 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 
-#include "intl.h"
 #include "mgutils.h"
 #include "addritem.h"
 #include "addrcache.h"
@@ -38,6 +38,7 @@
 #include "xml.h"
 #include "addrquery.h"
 #include "addr_compl.h"
+#include "utils.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
@@ -54,6 +55,7 @@
 #include "ldapserver.h"
 #include "ldapctrl.h"
 #include "ldapquery.h"
+#include "ldaputil.h"
 #endif
 
 #define TAG_ADDRESS_INDEX    "addressbook"
 /* New attributes */
 #define ATTAG_LDAP_DEFAULT    "default"
 
-#if 0
-N_("Common address")
-N_("Personal address")
-#endif
-
-#define DISP_NEW_COMMON       _("Common address")
-#define DISP_NEW_PERSONAL     _("Personal address")
+#define DISP_NEW_COMMON       _("Common addresses")
+#define DISP_NEW_PERSONAL     _("Personal addresses")
 
 /* Old address book */
 #define TAG_IF_OLD_COMMON     "common_address"
@@ -579,9 +576,7 @@ static gboolean addrindex_free_cache_cb( gpointer key, gpointer value, gpointer
  * Free hash table of address cache items.
  */
 static void addrindex_free_cache_hash( GHashTable *table ) {
-       g_hash_table_freeze( table );
        g_hash_table_foreach_remove( table, addrindex_free_cache_cb, NULL );
-       g_hash_table_thaw( table );
        g_hash_table_destroy( table );
 }
 
@@ -1080,37 +1075,6 @@ static void addrindex_write_fragment(
        }
 }
 
-#if 0
-static void addrindex_print_fragment_r(
-               const AddressIfFragment *fragment, FILE *stream, gint lvl )
-{
-       GList *node;
-       gint i;
-
-       for( i = 0; i < lvl; i++ )
-               fprintf( stream, "  " );
-       fprintf( stream, "Element:%s:\n", fragment->name );
-       node = fragment->attributes;
-       while( node ) {
-               AddressIfAttrib *nv = node->data;
-               for( i = 0; i < lvl; i++ )
-                       fprintf( stream, "  " );
-               fprintf( stream, "    %s : %s\n", nv->name, nv->value );
-               node = g_list_next( node );
-       }
-       node = fragment->children;
-       while( node ) {
-               AddressIfFragment *child = node->data;
-               addrindex_print_fragment_r( child, stream, 1+lvl );
-               node = g_list_next( node );
-       }
-}
-
-static void addrindex_print_fragment( const AddressIfFragment *fragment, FILE *stream ) {
-       addrindex_print_fragment_r( fragment, stream, 0 );
-}
-#endif
-
 /**
  * Read/parse address index file, creating a data source for a regular
  * intrinsic XML addressbook.
@@ -1337,6 +1301,7 @@ static void addrindex_parse_ldap_attrlist( XMLFile *file, LdapControl *ctl ) {
 
 }
 
+void ldapsvr_set_control( LdapServer *server, LdapControl *ctl );
 /**
  * Parse LDAP control data from XML file.
  * \param  file Index file.
@@ -1368,7 +1333,7 @@ static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
                gint ivalue = atoi( value );
 
                if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
-                       if( serverName ) g_free( serverName );
+                       g_free( serverName );
                        serverName = g_strdup( value );
                }
                else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
@@ -1387,7 +1352,7 @@ static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
                        ldapctl_set_bind_password( ctl, value );
                }
                else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
-                       if( criteria ) g_free( criteria );
+                       g_free( criteria );
                        criteria = g_strdup( value );
                }
                else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
@@ -1713,7 +1678,7 @@ gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
        fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
        addrIndex->retVal = MGU_OPEN_FILE;
 #ifdef DEV_STANDALONE
-       fp = fopen( fileSpec, "wb" );
+       fp = g_fopen( fileSpec, "wb" );
        g_free( fileSpec );
        if( fp ) {
                fputs( "<?xml version=\"1.0\" ?>\n", fp );
@@ -1722,8 +1687,7 @@ gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
        g_free( fileSpec );
        if( pfile ) {
                fp = pfile->fp;
-               fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
-                               conv_get_current_charset_str() );
+               fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n", CS_INTERNAL );
 #endif
                addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
                fputs( ">\n", fp );
@@ -1852,6 +1816,7 @@ static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
                if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
                        nn->remarks = g_strdup( element );
                }
+               g_free(element);
                xml_parse_next_tag(file);
        }
 }
@@ -2540,6 +2505,7 @@ LdapQuery *ldapsvr_new_dynamic_search(
                LdapServer *server, QueryRequest *req );
 LdapQuery *ldapsvr_new_explicit_search(
                LdapServer *server, QueryRequest *req, ItemFolder *folder );
+void ldapsvr_execute_query( LdapServer *server, LdapQuery *qry );
 
 #endif
 
@@ -2653,12 +2619,21 @@ gint addrindex_setup_explicit_search(
        QueryRequest *req;
        gint queryID;
        gchar *name;
+       gchar *mySearch;
 
        /* Name the query */
        name = g_strdup_printf( "Search '%s'", searchTerm );
 
        /* Set up query request */
-       req = qrymgr_add_request( searchTerm, callBackEnd, callBackEntry );
+       if (!strcmp(searchTerm, "*"))
+               mySearch = g_strdup("*@");
+       else
+               mySearch = g_strdup(searchTerm);
+       
+       req = qrymgr_add_request( mySearch, callBackEnd, callBackEntry );
+
+       g_free(mySearch);
+
        qryreq_set_search_type( req, ADDRSEARCH_EXPLICIT );
        queryID = req->queryID;
 
@@ -2771,6 +2746,9 @@ void addrindex_remove_results( AddressDataSource *ds, ItemFolder *folder ) {
                queryID = ADDRQUERY_ID(qry);
                /* printf( "calling ldapquery_remove_results...queryID=%d\n", queryID ); */
                delFlag = ldapquery_remove_results( qry );
+               if (delFlag) {
+                       ldapqry_free( qry );
+               }
                /* printf( "calling ldapquery_remove_results...done\n" ); */
                /*
                if( delFlag ) {
@@ -2872,8 +2850,86 @@ gboolean addrindex_load_completion(
        return TRUE;
 }
 
-/*
- * End of Source.
+/**
+ * This function can be used to collect information about
+ * addressbook entries that contain a specific attribute.
+ *
+ * \param attr         Name of attribute to look for
+ * \param callBackFunc Function to be called when a matching attribute was found
+ * \return <i>TRUE</i>
  */
+gboolean addrindex_load_person_attribute(
+               const gchar *attr,
+               gint (*callBackFunc) ( ItemPerson *, const gchar * ) )
+{
+       AddressDataSource *ds;
+       GList *nodeIf, *nodeDS;
+       GList *listP, *nodeP;
+       GList *nodeA;
 
+       nodeIf = addrindex_get_interface_list( _addressIndex_ );
+       while( nodeIf ) {
+               gchar *cur_bname;
+               AddressInterface *iface = nodeIf->data;
 
+               nodeIf = g_list_next( nodeIf );
+               if( ! iface->useInterface ) {
+                       continue;
+               }
+               if( iface->externalQuery ) {
+                       continue;
+               }
+               nodeDS = iface->listSource;
+               while( nodeDS ) {
+                       ds = nodeDS->data;
+
+                       /* Read address book */
+                       if( addrindex_ds_get_modify_flag( ds ) ) {
+                               addrindex_ds_read_data( ds );
+                       }
+
+                       if( ! addrindex_ds_get_read_flag( ds ) ) {
+                               addrindex_ds_read_data( ds );
+                       }
+
+                       /* Check addressbook name */
+                       cur_bname = addrindex_ds_get_name( ds );
+
+                       /* Get all persons */
+                       listP = addrindex_ds_get_all_persons( ds );
+                       nodeP = listP;
+                       while( nodeP ) {
+                               ItemPerson *person = nodeP->data;
+
+                               /* Return all ItemPerson's if attr is NULL */
+                               if( attr == NULL ) {
+                                       callBackFunc(person, cur_bname);
+                               }
+
+                               /* Return ItemPerson's with specific attribute */
+                               else {
+                                       nodeA = person->listAttrib;
+                                       /* Process each User Attribute */
+                                       while( nodeA ) {
+                                               UserAttribute *attrib = nodeA->data;
+                                               if( attrib->name && 
+                                                   !strcmp( attrib->name,attr ) ) {
+                                                       callBackFunc(person, cur_bname);
+                                               }
+                                               nodeA = g_list_next( nodeA );
+                                       }
+                               }
+                               nodeP = g_list_next( nodeP );
+                       }
+                       /* Free up the list */
+                       g_list_free( listP );
+
+                       nodeDS = g_list_next( nodeDS );
+               }
+       }
+       return TRUE;
+}
+
+/*
+ * End of Source.
+ */