Fix bug #1918: allow searching of local address books
authorCharles Lehner <charles@claws-mail.org>
Fri, 4 Dec 2015 22:51:16 +0000 (17:51 -0500)
committerCharles Lehner <charles@claws-mail.org>
Fri, 4 Dec 2015 23:32:29 +0000 (18:32 -0500)
Based on code in Sylpheed by Hiroyuki from 2009-11-26

src/addressbook.c

index 9aa8d30..60c5f02 100644 (file)
@@ -210,6 +210,9 @@ static void addressbook_person_collapse_node        (GtkCMCTree     *ctree,
                                                 GList          *node,
                                                 gpointer       *data );
 
                                                 GList          *node,
                                                 gpointer       *data );
 
+static void addressbook_entry_activated                (GtkWidget      *widget,
+                                                gpointer        data);
+
 static gboolean addressbook_list_button_pressed        (GtkWidget      *widget,
                                                 GdkEventButton *event,
                                                 gpointer        data);
 static gboolean addressbook_list_button_pressed        (GtkWidget      *widget,
                                                 GdkEventButton *event,
                                                 gpointer        data);
@@ -1117,6 +1120,8 @@ static void addressbook_create(void)
        g_signal_connect(G_OBJECT(entry), "key_press_event",
                         G_CALLBACK(addressbook_entry_key_pressed),
                         NULL);
        g_signal_connect(G_OBJECT(entry), "key_press_event",
                         G_CALLBACK(addressbook_entry_key_pressed),
                         NULL);
+       g_signal_connect(G_OBJECT(entry), "activate",
+                        G_CALLBACK(addressbook_entry_activated), NULL);
 
        if (!prefs_common.addressbook_use_editaddress_dialog) {
                editaddress_vbox = gtk_vbox_new(FALSE, 4);
 
        if (!prefs_common.addressbook_use_editaddress_dialog) {
                editaddress_vbox = gtk_vbox_new(FALSE, 4);
@@ -2372,13 +2377,11 @@ static void addressbook_list_row_selected( GtkCMCTree *clist,
                                           gint column,
                                           gpointer data )
 {
                                           gint column,
                                           gpointer data )
 {
-       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
        AddrItemObject *aio = NULL;
        AddressObject *pobj = NULL;
        AdapterDSource *ads = NULL;
        AddressDataSource *ds = NULL;
 
        AddrItemObject *aio = NULL;
        AddressObject *pobj = NULL;
        AdapterDSource *ads = NULL;
        AddressDataSource *ds = NULL;
 
-       gtk_entry_set_text( entry, "" );
        addrbook.listSelected = node;
 
        pobj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected );
        addrbook.listSelected = node;
 
        pobj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected );
@@ -2425,6 +2428,11 @@ static void addressbook_list_row_unselected( GtkCMCTree *ctree,
                addressbook_edit_person_invalidate(NULL, NULL, NULL);
 }
 
                addressbook_edit_person_invalidate(NULL, NULL, NULL);
 }
 
+static void addressbook_entry_activated(GtkWidget *widget, gpointer data)
+{
+       addressbook_lup_clicked(NULL, NULL);
+}
+
 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
                                                GdkEventButton *event,
                                                gpointer data)
 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
                                                GdkEventButton *event,
                                                gpointer data)
@@ -2537,6 +2545,7 @@ static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
                        canLookup = TRUE;
        }
        if (obj->type == ADDR_DATASOURCE) {
                        canLookup = TRUE;
        }
        if (obj->type == ADDR_DATASOURCE) {
+               canLookup = TRUE;
                ads = ADAPTER_DSOURCE(obj);
                ds = ads->dataSource;
                if( !ds )
                ads = ADAPTER_DSOURCE(obj);
                ds = ads->dataSource;
                if( !ds )
@@ -2552,10 +2561,9 @@ static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
                }
                canEdit = TRUE;
                canTreeCopy = TRUE;
                }
                canEdit = TRUE;
                canTreeCopy = TRUE;
-               if( iface->externalQuery )
-                       canLookup = TRUE;
        }
        else if (obj->type == ADDR_ITEM_FOLDER) {
        }
        else if (obj->type == ADDR_ITEM_FOLDER) {
+               canLookup = TRUE;
                ds = addressbook_find_datasource( node );
                if( !ds )
                        goto just_set_sens;
                ds = addressbook_find_datasource( node );
                if( !ds )
                        goto just_set_sens;
@@ -2574,11 +2582,11 @@ static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
 
                if( iface->externalQuery ) {
                        /* Enable deletion of LDAP folder */
 
                if( iface->externalQuery ) {
                        /* Enable deletion of LDAP folder */
-                       canLookup = TRUE;
                        canDelete = TRUE;
                }
        }
        else if (obj->type == ADDR_ITEM_GROUP) {
                        canDelete = TRUE;
                }
        }
        else if (obj->type == ADDR_ITEM_GROUP) {
+               canLookup = TRUE;
                ds = addressbook_find_datasource( node );
                if( !ds )
                        goto just_set_sens;
                ds = addressbook_find_datasource( node );
                if( !ds )
                        goto just_set_sens;
@@ -3521,9 +3529,32 @@ static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *emai
        return str;
 }
 
        return str;
 }
 
+static gboolean addressbook_match_item(const gchar *name,
+                                      const gchar *email_alias,
+                                      const gchar *addr,
+                                      const gchar *remarks,
+                                      const gchar *str)
+{
+       if (!name)
+               return FALSE;
+       if (!str || str[0] == '\0')
+               return TRUE;
+       if (strcasestr(name, str))
+               return TRUE;
+       else if (email_alias && strcasestr(email_alias, str))
+               return TRUE;
+       else if (addr && strcasestr(addr, str))
+               return TRUE;
+       else if (remarks && strcasestr(remarks, str))
+               return TRUE;
+
+       return FALSE;
+}
+
 static void addressbook_load_group( GtkCMCTree *clist, ItemGroup *itemGroup ) {
        GList *items = itemGroup->listEMail;
        AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
 static void addressbook_load_group( GtkCMCTree *clist, ItemGroup *itemGroup ) {
        GList *items = itemGroup->listEMail;
        AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
+       const gchar *search_str = gtk_entry_get_text(GTK_ENTRY(addrbook.entry));
        for( ; items != NULL; items = g_list_next( items ) ) {
                GtkCMCTreeNode *nodeEMail = NULL;
                gchar *text[N_LIST_COLS];
        for( ; items != NULL; items = g_list_next( items ) ) {
                GtkCMCTreeNode *nodeEMail = NULL;
                gchar *text[N_LIST_COLS];
@@ -3534,6 +3565,13 @@ static void addressbook_load_group( GtkCMCTree *clist, ItemGroup *itemGroup ) {
                if( ! email ) continue;
 
                person = ( ItemPerson * ) ADDRITEM_PARENT(email);
                if( ! email ) continue;
 
                person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+
+               if( !addressbook_match_item(ADDRITEM_NAME(person),
+                                           ADDRITEM_NAME(email),
+                                           email->address, email->remarks,
+                                           search_str))
+                       continue;
+
                str = addressbook_format_item_clist( person, email );
                if( str ) {
                        text[COL_NAME] = addressbook_set_col_name_guard(str);
                str = addressbook_format_item_clist( person, email );
                if( str ) {
                        text[COL_NAME] = addressbook_set_col_name_guard(str);
@@ -3659,13 +3697,33 @@ static void addressbook_folder_load_person( GtkCMCTree *clist, ItemFolder *itemF
        GList *items;
        AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
        AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
        GList *items;
        AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
        AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
+       const gchar *search_str;
 
        if( atci == NULL ) return;
        if( atciMail == NULL ) return;
 
 
        if( atci == NULL ) return;
        if( atciMail == NULL ) return;
 
+       search_str = gtk_entry_get_text(GTK_ENTRY(addrbook.entry));
+
        /* Load email addresses */
        items = addritem_folder_get_person_list( itemFolder );
        for( ; items != NULL; items = g_list_next( items ) ) {
        /* Load email addresses */
        items = addritem_folder_get_person_list( itemFolder );
        for( ; items != NULL; items = g_list_next( items ) ) {
+               ItemPerson *person;
+               GList *node;
+               ItemEMail *email;
+
+               person = (ItemPerson *)items->data;
+               if (!person)
+                       continue;
+               node = person->listEMail;
+               if (node && node->data) {
+                       email = node->data;
+                       if (!addressbook_match_item(ADDRITEM_NAME(person), ADDRITEM_NAME(email), email->address, email->remarks, search_str))
+                               continue;
+               } else {
+                       if (!addressbook_match_item(ADDRITEM_NAME(person), NULL, NULL, NULL, search_str))
+                               continue;
+               }
+
                addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
        }
        /* Free up the list */
                addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
        }
        /* Free up the list */
@@ -3718,15 +3776,23 @@ static void addressbook_folder_remove_one_person( GtkCMCTree *clist, ItemPerson
 static void addressbook_folder_load_group( GtkCMCTree *clist, ItemFolder *itemFolder ) {
        GList *items;
        AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
 static void addressbook_folder_load_group( GtkCMCTree *clist, ItemFolder *itemFolder ) {
        GList *items;
        AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
+       const gchar *search_str;
 
        /* Load any groups */
        if( ! atci ) return;
 
        /* Load any groups */
        if( ! atci ) return;
+
+       search_str = gtk_entry_get_text(GTK_ENTRY(addrbook.entry));
+
        items = addritem_folder_get_group_list( itemFolder );
        for( ; items != NULL; items = g_list_next( items ) ) {
                GtkCMCTreeNode *nodeGroup = NULL;
                gchar *text[N_LIST_COLS];
                ItemGroup *group = items->data;
                if( group == NULL ) continue;
        items = addritem_folder_get_group_list( itemFolder );
        for( ; items != NULL; items = g_list_next( items ) ) {
                GtkCMCTreeNode *nodeGroup = NULL;
                gchar *text[N_LIST_COLS];
                ItemGroup *group = items->data;
                if( group == NULL ) continue;
+               if( !addressbook_match_item(ADDRITEM_NAME(group),
+                                           NULL, NULL, NULL, search_str) )
+                       continue;
+
                text[COL_NAME] = ADDRITEM_NAME(group);
                text[COL_ADDRESS] = "";
                text[COL_REMARKS] = "";
                text[COL_NAME] = ADDRITEM_NAME(group);
                text[COL_ADDRESS] = "";
                text[COL_REMARKS] = "";
@@ -4640,6 +4706,14 @@ static void addressbook_lup_clicked( GtkButton *button, gpointer data ) {
        obj = gtk_cmctree_node_get_row_data( ctree, node );
        if( obj == NULL ) return;
 
        obj = gtk_cmctree_node_get_row_data( ctree, node );
        if( obj == NULL ) return;
 
+       if (obj->type != ADDR_DATASOURCE ||
+                       ADAPTER_DSOURCE(obj)->subType != ADDR_LDAP) {
+               addressbook_set_clist(
+                               gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
+                                       addrbook.treeSelected),
+                               TRUE);
+       }
+
        ds = addressbook_find_datasource( node );
        if( ds == NULL ) return;
 
        ds = addressbook_find_datasource( node );
        if( ds == NULL ) return;