sync with latest 0.9.0pre1
[claws.git] / src / addressbook.c
index 0901b40865cadd224b59caf2165d9a43e40d8b0d..8d5bdc2c35ea3ba68789276436eee9f1d7490567 100644 (file)
@@ -526,7 +526,6 @@ void addressbook_destroy() {
        if( _clipBoard_ != NULL ) {
                addrclip_free( _clipBoard_ );
        }
-       addrcompl_teardown();
        if( _addressIndex_ != NULL ) {
               addrindex_free_index( _addressIndex_ );
        }
@@ -1117,7 +1116,7 @@ gchar *addressbook_format_address( AddrItemObject * aio ) {
        }
        if( address ) {
                if( name && name[0] != '\0' ) {
-                       if( strchr_with_skip_quote( name, '"', ',' ) )
+                       if( name[0] != '"' && strpbrk( name, ",.[]<>" ) != NULL )
                                buf = g_strdup_printf( "\"%s\" <%s>", name, address );
                        else
                                buf = g_strdup_printf( "%s <%s>", name, address );
@@ -2378,6 +2377,7 @@ static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget
                        person = ( ItemPerson * ) ADDRITEM_PARENT(email);
                        if( addressbook_edit_person( abf, NULL, person, TRUE ) == NULL ) return;
                        addressbook_folder_refresh_one_person( clist, person );
+                       invalidate_address_completion();
                        return;
                }
        }
@@ -2385,6 +2385,7 @@ static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget
                /* Edit person - basic page */
                ItemPerson *person = ( ItemPerson * ) obj;
                if( addressbook_edit_person( abf, NULL, person, FALSE ) == NULL ) return;
+               invalidate_address_completion();
                addressbook_folder_refresh_one_person( clist, person );
                return;
        }
@@ -2483,9 +2484,11 @@ static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
        }
 }
 
-static void addressbook_folder_load_one_person( GtkCTree *clist, ItemPerson *person,  
-                                               AddressTypeControlItem *atci, AddressTypeControlItem *atciMail) {
-       
+static void addressbook_folder_load_one_person(
+               GtkCTree *clist, ItemPerson *person,
+               AddressTypeControlItem *atci,
+               AddressTypeControlItem *atciMail )
+{
        GtkCTreeNode *nodePerson = NULL;
        GtkCTreeNode *nodeEMail = NULL;
        gchar *text[N_COLS];
@@ -2577,8 +2580,10 @@ static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node
        addrbook.listSelected = NULL;
        gtk_ctree_remove_node( clist, node );
        addressbook_menubar_set_sensitive( FALSE );
-       addressbook_menuitem_set_sensitive( gtk_ctree_node_get_row_data( 
-                                               GTK_CTREE(clist), addrbook.treeSelected ), addrbook.treeSelected );
+       addressbook_menuitem_set_sensitive(
+               gtk_ctree_node_get_row_data(
+                       GTK_CTREE(clist), addrbook.treeSelected ),
+               addrbook.treeSelected );
 }
 
 static void addressbook_folder_refresh_one_person( GtkCTree *clist, ItemPerson *person ) {
@@ -2788,8 +2793,10 @@ static void addressbook_set_clist( AddressObject *obj ) {
                        /* Load root folder */
                        ItemFolder *rootFolder = NULL;
                        rootFolder = addrindex_ds_get_root_folder( ds );
-                       addressbook_folder_load_person( ctreelist, addrindex_ds_get_root_folder( ds ) );
-                       addressbook_folder_load_group( ctreelist, addrindex_ds_get_root_folder( ds ) );
+                       addressbook_folder_load_person(
+                               ctreelist, addrindex_ds_get_root_folder( ds ) );
+                       addressbook_folder_load_group(
+                               ctreelist, addrindex_ds_get_root_folder( ds ) );
                }
        }
        else {
@@ -2854,7 +2861,8 @@ AdapterDSource *addressbook_create_ds_adapter( AddressDataSource *ds,
 }
 
 void addressbook_ads_set_name( AdapterDSource *adapter, gchar *value ) {
-       ADDRESS_OBJECT_NAME(adapter) = mgu_replace_string( ADDRESS_OBJECT_NAME(adapter), value );
+       ADDRESS_OBJECT_NAME(adapter) =
+               mgu_replace_string( ADDRESS_OBJECT_NAME(adapter), value );
 }
 
 /*
@@ -2885,8 +2893,10 @@ static void addressbook_load_tree( void ) {
                                        ds = nodeDS->data;
                                        newNode = NULL;
                                        name = addrindex_ds_get_name( ds );
-                                       ads = addressbook_create_ds_adapter( ds, atci->objectType, name );
-                                       newNode = addressbook_add_object( node, ADDRESS_OBJECT(ads) );
+                                       ads = addressbook_create_ds_adapter(
+                                                       ds, atci->objectType, name );
+                                       newNode = addressbook_add_object(
+                                                       node, ADDRESS_OBJECT(ads) );
                                        nodeDS = g_list_next( nodeDS );
                                }
                                gtk_ctree_expand( ctree, node );
@@ -2991,7 +3001,6 @@ void addressbook_read_file( void ) {
        }
 
        addrIndex = addrindex_create_index();
-       addrcompl_initialize();
 
        /* Use new address book index. */
        addrindex_set_file_path( addrIndex, get_rc_dir() );
@@ -3145,9 +3154,8 @@ static GtkCTreeNode *addressbook_node_add_folder(
                newNode = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
                                atci->iconXpm, atci->maskXpm, atci->iconXpm, atci->maskXpm,
                                atci->treeLeaf, atci->treeExpand );
-               if( newNode )
-                       gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
-                               addressbook_free_treenode );
+               gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
+                       addressbook_free_treenode );
        }
 
        listItems = itemFolder->listFolder;
@@ -3177,6 +3185,9 @@ void addressbook_export_to_file( void ) {
                if( _addressIndex_->retVal != MGU_SUCCESS ) {
                        addrindex_print_index( _addressIndex_, stdout );
                }
+
+               /* Notify address completion of new data */
+               invalidate_address_completion();
        }
 }
 
@@ -3429,11 +3440,9 @@ static void addressbook_ldap_lookup( AdapterDSource *ads, gchar *sLookup ) {
  */
 static void addressbook_lup_clicked( GtkButton *button, gpointer data ) {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       AddressObject *pobj;
        AddressObject *obj;
        AdapterDSource *ads = NULL;
        gchar *sLookup;
-       GtkCTreeNode *node = NULL, *parentNode = NULL;
 
        if( ! addrbook.treeSelected ) return;
        if( GTK_CTREE_ROW( addrbook.treeSelected )->level == 1 ) return;
@@ -3443,25 +3452,16 @@ static void addressbook_lup_clicked( GtkButton *button, gpointer data ) {
 
        sLookup = gtk_editable_get_chars( GTK_EDITABLE(addrbook.entry), 0, -1 );
        g_strchomp( sLookup );
-       /* printf( "addressbook_lup_clicked/Lookup: '%s'\n", sLookup ); */
 
        if( obj->type == ADDR_DATASOURCE ) {
-               /* printf( "I am a datasource\n" ); */
                ads = ADAPTER_DSOURCE(obj);
-       }
-       else {
-               /* printf( "Test my parent\n" ); */
-               parentNode = GTK_CTREE_ROW(addrbook.treeSelected)->parent;
-               obj = gtk_ctree_node_get_row_data( ctree, parentNode );
-               if( obj->type == ADDR_DATASOURCE ) {
-                       ads = ADAPTER_DSOURCE(obj);
-               }
-       }
 #ifdef USE_LDAP
-       if( ads && ads->subType == ADDR_LDAP ) {
-               addressbook_ldap_lookup( ads, sLookup );
+               if( ads->subType == ADDR_LDAP ) {
+                       addressbook_ldap_lookup( ads, sLookup );
+               }
+#endif /* USE_LDAP */
        }
-#endif
+
        g_free( sLookup );
 }
 
@@ -3855,6 +3855,88 @@ gboolean addressbook_add_contact( const gchar *name, const gchar *address, const
        return TRUE;
 }
 
+/* **********************************************************************
+* Address completion support.
+* ***********************************************************************
+*/
+
+/*
+* This function is used by the address completion function to load
+* addresses.
+* Enter: callBackFunc Function to be called when an address is
+*                     to be loaded.
+* Return: TRUE if data loaded, FALSE if address index not loaded.
+*/
+gboolean addressbook_load_completion( gint (*callBackFunc) ( const gchar *, const gchar *, const gchar * ) ) {
+       AddressDataSource *ds;
+       GList *nodeIf, *nodeDS;
+       GList *listP, *nodeP;
+       GList *nodeM;
+       gchar *sName, *sAddress, *sAlias, *sFriendly;
+
+       debug_print( "addressbook_load_completion\n" );
+
+       if( _addressIndex_ == NULL ) return FALSE;
+
+       nodeIf = addrindex_get_interface_list( _addressIndex_ );
+       while( nodeIf ) {
+               AddressInterface *interface = nodeIf->data;
+               nodeDS = interface->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 );
+                       }
+
+                       /* Get all persons */
+                       listP = addrindex_ds_get_all_persons( ds );
+                       nodeP = listP;
+                       while( nodeP ) {
+                               ItemPerson *person = nodeP->data;
+                               nodeM = person->listEMail;
+
+                               /* Figure out name to use */
+                               sName = person->nickName;
+                               if( sName == NULL || *sName == '\0' ) {
+                                       sName = ADDRITEM_NAME(person);
+                               }
+
+                               /* Process each E-Mail address */
+                               while( nodeM ) {
+                                       ItemEMail *email = nodeM->data;
+                                       /* Have mail */
+                                       sFriendly = sName;
+                                       sAddress = email->address;
+                                       if( sAddress || *sAddress != '\0' ) {
+                                               sAlias = ADDRITEM_NAME(email);
+                                               if( sAlias && *sAlias != '\0' ) {
+                                                       sFriendly = sAlias;
+                                               }
+                                               ( callBackFunc ) ( sFriendly, sAddress, sName );
+                                       }
+
+                                       nodeM = g_list_next( nodeM );
+                               }
+                               nodeP = g_list_next( nodeP );
+                       }
+                       /* Free up the list */
+                       g_list_free( listP );
+
+                       nodeDS = g_list_next( nodeDS );
+               }
+               nodeIf = g_list_next( nodeIf );
+       }
+       debug_print( "addressbook_load_completion... done\n" );
+
+       return TRUE;
+}
+
 /* **********************************************************************
 * Address Import.
 * ***********************************************************************
@@ -3890,6 +3972,9 @@ static void addressbook_import_ldif_cb() {
                                                newNode );
                                        addrbook.treeSelected = newNode;
                                }
+
+                               /* Notify address completion */
+                               invalidate_address_completion();
                        }
                }
        }
@@ -3925,6 +4010,9 @@ static void addressbook_import_mutt_cb() {
                                                newNode );
                                        addrbook.treeSelected = newNode;
                                }
+
+                               /* Notify address completion */
+                               invalidate_address_completion();
                        }
                }
        }
@@ -3960,6 +4048,9 @@ static void addressbook_import_pine_cb() {
                                                newNode );
                                        addrbook.treeSelected = newNode;
                                }
+
+                               /* Notify address completion */
+                               invalidate_address_completion();
                        }
                }
        }
@@ -3996,6 +4087,9 @@ void addressbook_harvest(
                                                ADDRESS_OBJECT(ads) );
                        }
                }
+
+               /* Notify address completion */
+               invalidate_address_completion();
        }
 }
 
@@ -4026,116 +4120,4 @@ static void addressbook_export_html_cb( void ) {
 /*
 * End of Source.
 */
-/* **********************************************************************
-* Address completion and search support.
-* ***********************************************************************
-*/
-
-/**
- * Setup search for specified search string.
- * \param searchTerm Search string.
- * \param target     Target data.
- * \param callBack   Call back function.
- * \return ID allotcated to this query, or 0 if none.
- */
-gint addressbook_setup_search(
-       const gchar *searchTerm, const gpointer target,
-       AddrSearchCallbackFunc callback )
-{
-       if( _addressIndex_ == NULL ) {
-               printf( "address index not loaded\n" );
-               return 0;
-       }
-       return addrindex_setup_search( _addressIndex_, searchTerm, target, callback );
-}
-
-/**
- * Perform the previously registered search.
- * \param  queryID ID of search query to be executed.
- * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
- *         failed.
- */
-gboolean addressbook_start_search( const gint queryID ) {
-       if( _addressIndex_ == NULL ) {
-               printf( "address index not loaded\n" );
-               return FALSE;
-       }
-       return addrindex_start_search( _addressIndex_, queryID );
-}
-
-/**
- * Stop the previously registered search.
- * \param queryID ID of search query to stop.
- */
-void addressbook_stop_search( const gint queryID ){
-       if( _addressIndex_ == NULL ) {
-               /* printf( "address index not loaded\n" ); */
-               return;
-       }
-       addrindex_stop_search( _addressIndex_, queryID );
-}
-
-/**
- * Read all address interfaces and data sources.
- */
-void addressbook_read_all( void ) {
-       if( _addressIndex_ == NULL ) {
-               /* Load index file */
-               /* printf( "address index not loaded\n" ); */
-               /* addressbook_read_file(); */
-       }
-
-       if( ! addrindex_get_loaded( _addressIndex_ ) ) {
-               /* Read all address books */
-               addrindex_read_all( _addressIndex_ );
-       }
-       /*
-       else {
-               printf( "Address data already loaded!!!!!!!!!!!!!!!\n" );
-       }
-       */
-}
-
-/**
- * Perform a simple search of all non-query type data sources for specified
- * search term. If several entries are found, only the first item is
- * returned. Interfaces that require a time-consuming "query" are ignored for
- * this search.
- *
- * \param  searchTerm Search term to find. Typically an email address.
- * \return Reference to a single E-Mail object that was found in the address
- *         book, or <i>NULL</i> if nothing found. This should *NOT* be freed
- *         when done.
- */
-ItemEMail *addressbook_quick_search_single( const gchar *searchTerm ) {
-       if( _addressIndex_ == NULL ) {
-               /* printf( "address index not loaded\n" ); */
-               return NULL;
-       }
-       return addrindex_quick_search_single( _addressIndex_, searchTerm );
-}
-
-/**
- * Perform a simple search of all non-query type data sources for specified
- * search term. If several entries are found, only the first item is
- * returned. Interfaces that require a time-consuming "query" are ignored for
- * this search.
- *
- * \param  addrIndex  Address index object.
- * \param  searchTerm Search term to find. Typically an email address.
- * \return List of references to zero or mail E-Mail object that was found in
- *         the address books, or <i>NULL</i> if nothing found. This list
- *         *SHOULD* be freed when done.
- */
-GList *addressbook_quick_search_list( const gchar *searchTerm )
-{
-       if( _addressIndex_ == NULL ) {
-               printf( "address index not loaded\n" );
-               return NULL;
-       }
-       return addrindex_quick_search_list( _addressIndex_, searchTerm );
-}
-/*
-* End of Source.
-*/