Commit 14287d8a authored by Rafaël Carré's avatar Rafaël Carré
Browse files

* #5695: addressbook: simplify

parent 0b998639
......@@ -138,7 +138,7 @@ sflphone_notify_voice_mail (const gchar* accountID , guint count)
static gboolean _is_direct_call (callable_obj_t * c)
{
if (g_strcasecmp (c->_accountID, EMPTY_ENTRY) == 0) {
if (g_strcasecmp (c->_accountID, "empty") == 0) {
if (!g_str_has_prefix (c->_peer_number, "sip:")) {
gchar * new_number = g_strconcat ("sip:", c->_peer_number, NULL);
g_free (c->_peer_number);
......
......@@ -47,7 +47,7 @@ gint is_callID_callstruct(gconstpointer a, gconstpointer b)
void calllist_add_contact (gchar *contact_name, gchar *contact_phone, contact_type_t type, GdkPixbuf *photo)
{
/* Check if the information is valid */
if (g_strcasecmp (contact_phone, "empty") == 0)
if (!contact_phone)
return;
callable_obj_t *new_call = create_new_call (CONTACT, CALL_STATE_DIALING, "", "", contact_name, contact_phone);
......
......@@ -39,10 +39,10 @@
void
addressbook_search (void (*search_cb)(GList *, gpointer), GtkEntry* entry, AddressBook_Config *addressbook_config)
{
const gchar* query = gtk_entry_get_text (GTK_ENTRY (entry));
printf("Addressbook: Search %s\n", query);
search_async_by_contacts (gtk_entry_get_text (GTK_ENTRY (entry)), addressbook_config->max_results, search_cb, addressbook_config);
search_async_by_contacts (gtk_entry_get_text(entry),
addressbook_config->max_results,
search_cb,
addressbook_config);
}
/**
......@@ -69,25 +69,10 @@ addressbook_is_active()
static void
addressbook_config_books(gchar **book_list)
{
gchar **config_book_uid;
book_data_t *book_data;
if (book_list == NULL) {
printf("Addresbook: Error: Book list is NULL (%s:%d)\n", __FILE__, __LINE__);
return;
}
for (config_book_uid = book_list; *config_book_uid; config_book_uid++) {
// Get corresponding book data
book_data = books_get_book_data_by_uid (*config_book_uid);
// If book_data exists
if (book_data == NULL) {
printf("Addressbook: Error: Could not open book (%s:%d)\n", __FILE__, __LINE__);
} else {
for (gchar **book = book_list; book && *book; book++) {
book_data_t *book_data = books_get_book_data_by_uid(*book);
if (book_data)
book_data->active = TRUE;
}
}
}
......@@ -97,9 +82,6 @@ addressbook_config_books(gchar **book_list)
GSList *
addressbook_get_books_data(gchar **book_list)
{
printf("Addressbook: Get books data\n");
// fill_books_data();
addressbook_config_books(book_list);
determine_default_addressbook();
......@@ -119,8 +101,6 @@ addressbook_get_book_data_by_uid(gchar *uid)
void
addressbook_init(gchar **book_list)
{
printf("Addressbook: Initialize addressbook\n");
fill_books_data();
addressbook_config_books(book_list);
determine_default_addressbook();
......@@ -130,20 +110,16 @@ addressbook_init(gchar **book_list)
}
void addressbook_set_search_type(AddrbookSearchType searchType) {
switch(searchType) {
case ABOOK_QUERY_IS:
set_current_addressbook_test(E_BOOK_QUERY_IS);
break;
case ABOOK_QUERY_BEGINS_WITH:
set_current_addressbook_test(E_BOOK_QUERY_BEGINS_WITH);
break;
case ABOOK_QUERY_CONTAINS:
set_current_addressbook_test(E_BOOK_QUERY_CONTAINS);
break;
default:
printf("Addressbook: Error: Unsupported search type");
break;
}
if (searchType > ABOOK_QUERY_CONTAINS)
return;
static const EBookQueryTest map[] = {
[ABOOK_QUERY_IS] = E_BOOK_QUERY_IS,
[ABOOK_QUERY_BEGINS_WITH] = E_BOOK_QUERY_BEGINS_WITH,
[ABOOK_QUERY_CONTAINS] = E_BOOK_QUERY_CONTAINS
};
set_current_addressbook_test(map[searchType]);
}
void addressbook_set_current_book(gchar *current) {
......
......@@ -40,7 +40,11 @@
#include <gtk/gtk.h>
typedef enum {ABOOK_QUERY_IS, ABOOK_QUERY_BEGINS_WITH, ABOOK_QUERY_CONTAINS} AddrbookSearchType;
typedef enum {
ABOOK_QUERY_IS,
ABOOK_QUERY_BEGINS_WITH,
ABOOK_QUERY_CONTAINS
} AddrbookSearchType;
/**
* Represent a contact entry
......
......@@ -57,20 +57,12 @@ typedef struct _Search_Handler_And_Data {
EBookQuery *equery;
} Search_Handler_And_Data;
static void
authenticate_source (EBook *);
/**
* The global addressbook list
*/
GSList *books_data = NULL;
static GSList *books_data = NULL;
static GStaticMutex books_data_mutex = G_STATIC_MUTEX_INIT;
/**
* Size of image that will be displayed in contact list
*/
static const int pixbuf_size = 32;
/**
* Current selected addressbook's uri and uid, initialized with default
*/
......@@ -80,43 +72,19 @@ static gchar *current_name = "Default";
static EBookQueryTest current_test = E_BOOK_QUERY_BEGINS_WITH;
/**
* Prototypes
*/
void empty_books_data();
/**
* Free a book data
*/
void
free_book_data (book_data_t *data)
{
g_free (data->name);
g_free (data->uid);
g_free (data->uri);
}
/**
* Public way to know if we can perform a search
*/
gboolean
books_ready()
{
gboolean returnValue;
g_static_mutex_lock(&books_data_mutex);
if (books_data == NULL) {
g_static_mutex_unlock(&books_data_mutex);
return FALSE;
}
gboolean ret = books_data && g_slist_length (books_data) > 0;
returnValue = (g_slist_length (books_data) > 0);
g_static_mutex_unlock(&books_data_mutex);
return returnValue;
return ret ;
}
/**
......@@ -125,32 +93,19 @@ books_ready()
gboolean
books_active()
{
GSList *book_list_iterator;
book_data_t *book_data;
gboolean ret = FALSE;
g_static_mutex_lock(&books_data_mutex);
if (books_data == NULL) {
printf("Addressbook: No books data (%s:%d)\n", __FILE__, __LINE__);
g_static_mutex_unlock(&books_data_mutex);
return FALSE;
}
// Iterate throw the list
for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
= book_list_iterator->next) {
book_data = (book_data_t *) book_list_iterator->data;
if (book_data->active) {
g_static_mutex_unlock(&books_data_mutex);
return TRUE;
for (GSList *iter = books_data; iter; iter = iter->next)
if (((book_data_t *) iter->data)->active) {
ret = TRUE;
break;
}
}
g_static_mutex_unlock(&books_data_mutex);
// If no result
return FALSE;
return ret;
}
/**
* Get a specific book data by UID
......@@ -158,36 +113,19 @@ books_active()
book_data_t *
books_get_book_data_by_uid (gchar *uid)
{
GSList *book_list_iterator;
book_data_t *book_data;
book_data_t *ret = NULL;
g_static_mutex_lock(&books_data_mutex);
if (books_data == NULL) {
printf("Addressbook: No books data (%s:%d)\n", __FILE__, __LINE__);
g_static_mutex_unlock(&books_data_mutex);
return NULL;
}
printf("Addressbook: Get book data by uid: %s\n", uid);
// Iterate throw the list
for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
= book_list_iterator->next) {
book_data = (book_data_t *) book_list_iterator->data;
if (strcmp (book_data->uid, uid) == 0) {
printf("Addressbook: Book %s found\n", uid);
g_static_mutex_unlock(&books_data_mutex);
return book_data;
for (GSList *iter = books_data; iter != NULL; iter = iter->next)
if (!strcmp (((book_data_t *)iter->data)->uid, uid) ) {
ret = iter->data;
break;
}
}
g_static_mutex_unlock(&books_data_mutex);
printf("Addressbook: Could not found Book %s\n", uid);
// If no result
return NULL;
return ret;
}
......@@ -198,31 +136,21 @@ books_get_book_data_by_uid (gchar *uid)
static EBookQuery*
create_query (const char* s, EBookQueryTest test, AddressBook_Config *conf)
{
EBookQuery *equery;
EBookQuery *queries[4];
// Create the query
int cpt = 0;
// We could also use E_BOOK_QUERY_IS or E_BOOK_QUERY_BEGINS_WITH instead of E_BOOK_QUERY_CONTAINS
queries[cpt++] = e_book_query_field_test (E_CONTACT_FULL_NAME, test, s);
if (conf->search_phone_home) {
if (conf->search_phone_home)
queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_HOME, test, s);
}
if (conf->search_phone_business) {
if (conf->search_phone_business)
queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_BUSINESS, test, s);
}
if (conf->search_phone_mobile) {
if (conf->search_phone_mobile)
queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_MOBILE, test, s);
}
equery = e_book_query_or (cpt, queries, TRUE);
return equery;
return e_book_query_or (cpt, queries, TRUE);
}
/**
......@@ -231,43 +159,37 @@ create_query (const char* s, EBookQueryTest test, AddressBook_Config *conf)
static GdkPixbuf*
pixbuf_from_contact (EContact *contact)
{
GdkPixbuf *pixbuf = NULL;
EContactPhoto *photo = e_contact_get (contact, E_CONTACT_PHOTO);
if (photo) {
GdkPixbufLoader *loader;
if (!photo)
return NULL;
loader = gdk_pixbuf_loader_new();
GdkPixbufLoader *loader;
if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
if (gdk_pixbuf_loader_write (loader, (guchar *) photo->data.inlined.data, photo->data.inlined.length, NULL)) {
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
}
loader = gdk_pixbuf_loader_new();
if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
if (gdk_pixbuf_loader_write (loader, (guchar *) photo->data.inlined.data, photo->data.inlined.length, NULL)) {
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
}
}
// If pixbuf has been found, check size and resize if needed
if (pixbuf) {
GdkPixbuf *tmp;
gint width = gdk_pixbuf_get_width (pixbuf);
gint height = gdk_pixbuf_get_height (pixbuf);
double scale = 1.0;
if (height > width) {
scale = pixbuf_size / (double) height;
} else {
scale = pixbuf_size / (double) width;
}
e_contact_photo_free (photo);
if (scale < 1.0) {
tmp = gdk_pixbuf_scale_simple (pixbuf, width * scale, height
* scale, GDK_INTERP_BILINEAR);
g_object_unref (pixbuf);
pixbuf = tmp;
}
}
if (!pixbuf)
return NULL;
e_contact_photo_free (photo);
// check size and resize if needed
gint width = gdk_pixbuf_get_width (pixbuf);
gint height = gdk_pixbuf_get_height (pixbuf);
double scale = 32 / (double) ((height > width) ? height : width);
if (scale < 1.0) {
GdkPixbuf *tmp = gdk_pixbuf_scale_simple (pixbuf, width * scale, height
* scale, GDK_INTERP_BILINEAR);
g_object_unref (pixbuf);
pixbuf = tmp;
}
return pixbuf;
......@@ -285,8 +207,6 @@ view_finish_callback (EBookView *book_view, Search_Handler_And_Data *had)
g_free (had);
printf("Addressbook: View finish, all book have beem processed\n");
if (book_view != NULL)
g_object_unref (book_view);
......@@ -294,75 +214,47 @@ view_finish_callback (EBookView *book_view, Search_Handler_And_Data *had)
had_handler (had_hits, had_user_data);
}
/**
* Callback called after a contact have been found in EDS by search_async_by_contacts.
*/
#ifdef LIBEDATASERVER_VERSION_2_32
void
static void
eds_query_result_cb (EBook *book, const GError *error, GList *contacts, gpointer user_data)
{
printf("Addressbook: Search Result callback called\n");
if (error) {
printf("Addressbook: Error: %s\n", error->message);
if (error)
return;
}
#else
static void
eds_query_result_cb (EBook *book, EBookStatus status, GList *contacts, gpointer user_data)
{
printf("Addressbook: Search Result callback called\n");
if (status != E_BOOK_ERROR_OK) {
printf("Addressbook: Error: ");
if (status != E_BOOK_ERROR_OK)
return;
}
#endif
Search_Handler_And_Data *had = (Search_Handler_And_Data *) user_data;
if (contacts == NULL) {
printf("Addressbook: No contact found\n");
had->search_handler (NULL, user_data);
return;
}
GList *l = NULL;
// make sure we have a new list of hits
had->hits = NULL;
l = contacts;
while (l) {
for (GList *l = contacts; l; l = g_list_next (l)) {
Hit *hit = g_new (Hit, 1);
EContact *contact = E_CONTACT(l->data);
if (hit) {
hit->photo = pixbuf_from_contact(contact);
hit->phone_business = g_strdup((char*)e_contact_get_const(contact, E_CONTACT_PHONE_BUSINESS));
hit->phone_home = g_strdup((char*)e_contact_get_const(contact, E_CONTACT_PHONE_HOME));
hit->phone_mobile = g_strdup((char*)e_contact_get_const(contact, E_CONTACT_PHONE_MOBILE));
hit->name = g_strdup((char*)e_contact_get_const(contact, E_CONTACT_NAME_OR_ORG));
// Get the photo contact
hit->photo = pixbuf_from_contact (E_CONTACT (l->data));
fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_BUSINESS, &hit->phone_business);
fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_HOME, &hit->phone_home);
fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_MOBILE, &hit->phone_mobile);
hit->name = g_strdup ( (char *) e_contact_get_const (E_CONTACT (l->data), E_CONTACT_NAME_OR_ORG));
if (!hit->name) {
hit->name = "";
}
if (hit) {
had->hits = g_list_append (had->hits, hit);
}
had->max_results_remaining--;
if (had->max_results_remaining <= 0) {
break;
}
}
l = g_list_next (l);
had->hits = g_list_append (had->hits, hit);
if (--had->max_results_remaining <= 0)
break;
}
view_finish_callback (NULL, had);
......@@ -379,52 +271,26 @@ eds_query_result_cb (EBook *book, EBookStatus status, GList *contacts, gpointer
void
eds_async_open_callback (EBook *book, const GError *error, gpointer closure)
{
printf("Addressbook: Open book callback\n");
ESource *source;
const gchar *uri;
if(error) {
printf("Addressbook: Error: %s\n", error->message);
if(error)
return;
}
#else
static void
eds_async_open_callback (EBook *book, EBookStatus status, gpointer closure)
{
ESource *source;
const gchar *uri;
if(status == E_BOOK_ERROR_OK) {
printf("Addressbook: Error\n");
if(status == E_BOOK_ERROR_OK)
return;
}
#endif
Search_Handler_And_Data *had = (Search_Handler_And_Data *) closure;
if (! (source = e_book_get_source (book))) {
printf("Addressbook: Error: while getting source\n");
}
if (! (uri = e_book_get_uri (book))) {
printf("Addressbook: Error while getting URI\n");
}
authenticate_source (book);
if (!e_book_is_opened (book)) {
// We must open the addressbook
if (!e_book_is_opened (book))
e_book_open (book, FALSE, NULL);
}
#ifdef LIBEDATASERVER_VERSION_2_32
if (!e_book_get_contacts_async (book, had->equery, eds_query_result_cb, had))
printf("Addressbook: Error: While querying addressbook\n");
e_book_get_contacts_async (book, had->equery, eds_query_result_cb, had);
#else
if (e_book_async_get_contacts (book, had->equery, eds_query_result_cb, had))
printf("Addressbook: Error: While querying addressbook\n");
e_book_async_get_contacts (book, had->equery, eds_query_result_cb, had);
#endif
}
......@@ -435,30 +301,10 @@ eds_async_open_callback (EBook *book, EBookStatus status, gpointer closure)
void
init_eds ()
{
GSList *book_list_iterator;
book_data_t *book_data;
printf ("Addressbook: Init evolution data server\n");
g_static_mutex_lock(&books_data_mutex);
if (books_data == NULL) {
printf ("Addressbook: No books data (%s:%d)\n", __FILE__, __LINE__);
g_static_mutex_unlock(&books_data_mutex);
return;
}
// init current with first addressbook if no default addressbook set
book_list_iterator = books_data;
book_data = (book_data_t *) book_list_iterator->data;
current_uri = book_data->uri;
current_uid = book_data->uid;
current_name = book_data->name;
// Iterate through list to find default addressbook
for (book_list_iterator = books_data; book_list_iterator != NULL;
book_list_iterator = book_list_iterator->next) {
book_data = (book_data_t *) book_list_iterator->data;
for (GSList *iter = books_data; iter != NULL; iter = iter->next) {
book_data_t *book_data = (book_data_t *) iter->data;
if (book_data->isdefault) {
current_uri = book_data->uri;
......@@ -467,115 +313,42 @@ init_eds ()
}
}
printf("END EVOLUTION INIT %s, %s, %s\n", current_uri, current_uid, current_name);
g_static_mutex_unlock(&books_data_mutex);
}
/**
* Authenticate this addressbook
*/
static void
authenticate_source (EBook *book)
{
(void) book; /* unused */
/*
const gchar *auth_domain;
const gchar *password = NULL;
const gchar *component_name;
const gchar *user = NULL;
const gchar *auth;
GError *err = NULL;
const gchar *uri;
ESource *source;
if ((source = e_book_get_source (book)) == NULL) {
DEBUG ("Addressbook: Error while getting source");
}
if ((uri = e_book_get_uri (book)) == NULL) {
DEBUG ("Addressbook: Error while getting URI");
}
auth_domain = e_source_get_property (source, "auth-domain");
auth = e_source_get_property (source, "auth");
if (auth && !strcmp ("ldap/simple-binddn", auth)) {
user = e_source_get_property (source, "binddn");
}
else if (auth && !strcmp ("plain/password", auth)) {
user = e_source_get_property (source, "user");
if (!user) {
user = e_source_get_property (source, "username");
}
} else {
user = e_source_get_property (source, "email_addr");
}
if (!user) {
user = "";