Skip to content
Snippets Groups Projects
Commit 01abacdc authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

Make the search asynchronous

parent a7154567
No related branches found
No related tags found
No related merge requests found
......@@ -225,34 +225,12 @@ show_history_tab(GtkToggleToolButton *toggle_tool_button UNUSED,
}
static void
show_contacts_tab(GtkToggleToolButton *toggle_tool_button UNUSED,
gpointer user_data UNUSED)
{
handler_async_search (GList *hits, gpointer user_data) {
GtkTreeSelection *sel;
GList *results;
GList *i;
call_t *j;
gchar* msg;
// temporary display in status bar
msg = g_strdup("Contacts");
statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT);
g_free(msg);
// freeing calls
while((j = (call_t *)g_queue_pop_tail (contacts->callQueue)) != NULL)
{
free_call_t(j);
}
// reset previous results
reset_call_tree(contacts);
call_list_reset(contacts);
// do a synchronous search
results = search_sync (gtk_entry_get_text(GTK_ENTRY(filter_entry)), 50);
for (i = results; i != NULL; i = i->next)
for (i = hits; i != NULL; i = i->next)
{
Hit *entry;
entry = i->data;
......@@ -267,7 +245,7 @@ show_contacts_tab(GtkToggleToolButton *toggle_tool_button UNUSED,
}
free_hit(entry);
}
g_list_free(results);
g_list_free(hits);
active_calltree = contacts;
gtk_widget_hide(current_calls->tree);
......@@ -277,6 +255,36 @@ show_contacts_tab(GtkToggleToolButton *toggle_tool_button UNUSED,
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (active_calltree->view));
g_signal_emit_by_name(sel, "changed");
toolbar_update_buttons();
}
static void
show_contacts_tab(GtkToggleToolButton *toggle_tool_button UNUSED,
gpointer user_data UNUSED)
{
call_t *j;
gchar* msg;
// temporary display in status bar
msg = g_strdup("Contacts");
statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT);
g_free(msg);
// freeing calls
while((j = (call_t *)g_queue_pop_tail (contacts->callQueue)) != NULL)
{
free_call_t(j);
}
// reset previous results
reset_call_tree(contacts);
call_list_reset(contacts);
// do a synchronous search
//results = search_sync (gtk_entry_get_text(GTK_ENTRY(filter_entry)), 50);
// do an asynchronous search
search_async (gtk_entry_get_text (GTK_ENTRY (filter_entry)), 50, &handler_async_search, NULL);
}
void create_new_entry_in_contactlist (gchar *contact_name, gchar *contact_phone, contact_type_t type){
......
......@@ -30,6 +30,14 @@
#include <pango/pango.h>
#include "eds.h"
typedef struct _Handler_And_Data {
SearchAsyncHandler handler;
gpointer user_data;
GList *hits;
int max_results_remaining;
int book_views_remaining;
} Handler_And_Data;
static GSList *books = NULL;
static EContactField search_fields[] = { E_CONTACT_FULL_NAME, E_CONTACT_PHONE_BUSINESS, E_CONTACT_NICKNAME, 0 };
......@@ -198,6 +206,114 @@ search_sync (const char *query,
return hits;
}
static void
view_finish (EBookView *book_view, Handler_And_Data *had)
{
SearchAsyncHandler had_handler = had->handler;
GList *had_hits = had->hits;
gpointer had_user_data = had->user_data;
g_free (had);
g_return_if_fail (book_view != NULL);
g_object_unref (book_view);
had_handler (had_hits, had_user_data);
}
static void
view_contacts_added_cb (EBookView *book_view, GList *contacts, gpointer user_data)
{
Handler_And_Data *had = (Handler_And_Data *) user_data;
if (had->max_results_remaining <= 0) {
e_book_view_stop (book_view);
had->book_views_remaining--;
if (had->book_views_remaining == 0) {
view_finish (book_view, had);
return;
}
}
for (; contacts != NULL; contacts = g_list_next (contacts)) {
EContact *contact;
Hit *hit;
gchar *number;
contact = E_CONTACT (contacts->data);
hit = g_new (Hit, 1);
//hit->pixbuf = pixbuf_from_contact (contact);
/* Get business phone information */
fetch_information_from_contact (contact, E_CONTACT_PHONE_BUSINESS, &number);
hit->phone_business = g_strdup (number);
/* Get home phone information */
fetch_information_from_contact (contact, E_CONTACT_PHONE_HOME, &number);
hit->phone_home = g_strdup (number);
/* Get mobile phone information */
fetch_information_from_contact (contact, E_CONTACT_PHONE_MOBILE, &number);
hit->phone_mobile = g_strdup (number);
hit->name = g_strdup ((char*) e_contact_get_const (contact, E_CONTACT_NAME_OR_ORG));
if(! hit->name)
hit->name = "";
had->hits = g_list_append (had->hits, hit);
had->max_results_remaining--;
if (had->max_results_remaining <= 0) {
e_book_view_stop (book_view);
had->book_views_remaining--;
if (had->book_views_remaining == 0) {
view_finish (book_view, had);
}
break;
}
}
}
static void
view_completed_cb (EBookView *book_view, EBookViewStatus status, gpointer user_data)
{
Handler_And_Data *had = (Handler_And_Data *) user_data;
had->book_views_remaining--;
if (had->book_views_remaining == 0) {
view_finish (book_view, had);
}
}
void
search_async (const char *query,
int max_results,
SearchAsyncHandler handler,
gpointer user_data)
{
GSList *iter;
EBookQuery* book_query = create_query (query);
Handler_And_Data *had = g_new (Handler_And_Data, 1);
had->handler = handler;
had->user_data = user_data;
had->hits = NULL;
had->max_results_remaining = max_results;
had->book_views_remaining = 0;
for (iter = books; iter != NULL; iter = iter->next) {
EBook *book = (EBook *) iter->data;
EBookView *book_view = NULL;
e_book_get_book_view (book, book_query, NULL, max_results, &book_view, NULL);
if (book_view != NULL) {
had->book_views_remaining++;
g_signal_connect (book_view, "contacts_added", (GCallback) view_contacts_added_cb, had);
g_signal_connect (book_view, "sequence_complete", (GCallback) view_completed_cb, had);
e_book_view_start (book_view);
}
}
if (had->book_views_remaining == 0) {
g_free (had);
}
e_book_query_unref (book_query);
}
void fetch_information_from_contact (EContact *contact, EContactField field, gchar **info){
gchar *to_fetch;
......
......@@ -44,8 +44,15 @@ typedef struct _Hit
void free_hit (Hit *h);
typedef void (* SearchAsyncHandler) (GList *hits, gpointer user_data);
void init (void);
void search_async (const char *query,
int max_results,
SearchAsyncHandler handler,
gpointer user_data);
GList * search_sync (const char *query, int max_results);
void fetch_information_from_contact (EContact *contact, EContactField field, gchar **info);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment