Select contacts dialog for checking names is multiselection
[modest] / src / hildon2 / modest-address-book.c
index 783c02c..6a057ce 100644 (file)
@@ -62,7 +62,7 @@ static gchar *ui_get_formatted_email_id(gchar * current_given_name,
                                        gchar * current_sur_name, gchar * current_email_id);
 static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name, gboolean *canceled);
 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
-static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, gchar **contact_id, gboolean *canceled);
+static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, GSList **contact_id, gboolean *canceled);
 static gchar *unquote_string (const gchar *str);
 
 
@@ -724,28 +724,86 @@ add_to_address_book (const gchar* address)
        return TRUE;
 }
 
-static gboolean
-show_check_names_banner (gpointer userdata)
+typedef struct _CheckNamesInfo {
+       GtkWidget *banner;
+       guint show_banner_timeout;
+       guint hide_banner_timeout;
+       gboolean hide;
+       gboolean free_info;
+} CheckNamesInfo;
+
+static void
+hide_check_names_banner (CheckNamesInfo *info)
 {
-       GtkWidget **banner = (GtkWidget **) userdata;
+       if (info->show_banner_timeout > 0) {
+               g_source_remove (info->show_banner_timeout);
+               info->show_banner_timeout = 0;
+       }
+       if (info->hide_banner_timeout > 0) {
+               info->hide = TRUE;
+               return;
+       }
+
+       if (info->banner) {
+               gtk_widget_destroy (info->banner);
+               info->banner = NULL;
+               info->hide = FALSE;
+       }
 
-       gdk_threads_enter ();
-       *banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
-       gdk_threads_leave ();
+       if (info->free_info) {
+               g_slice_free (CheckNamesInfo, info);
+       }
+}
 
+static gboolean hide_banner_timeout_handler (CheckNamesInfo *info)
+{
+       info->hide_banner_timeout = 0;
+       if (info->hide) {
+               gtk_widget_destroy (info->banner);
+               info->banner = NULL;
+       }
+       if (info->free_info) {
+               g_slice_free (CheckNamesInfo, info);
+       }
        return FALSE;
 }
 
-static void
-hide_check_names_banner (GtkWidget **banner, guint banner_timeout)
+static gboolean show_banner_timeout_handler (CheckNamesInfo *info)
 {
-       if (*banner != NULL) {
-               gtk_widget_destroy (*banner);
-               *banner = NULL;
-       } else {
-               g_source_remove (banner_timeout);
+       info->show_banner_timeout = 0;
+       info->banner = hildon_banner_show_animation (NULL, NULL, _("mail_ib_checking_names"));
+       info->hide_banner_timeout = g_timeout_add (1000, (GSourceFunc) hide_banner_timeout_handler, (gpointer) info);
+       return FALSE;
+}
+
+static void show_check_names_banner (CheckNamesInfo *info)
+{
+       if (info->hide_banner_timeout > 0) {
+               g_source_remove (info->hide_banner_timeout);
+               info->hide_banner_timeout = 0;
        }
 
+       info->hide = FALSE;
+       if (info->show_banner_timeout > 0)
+               return;
+
+       if (info->banner == NULL) {
+               info->show_banner_timeout = g_timeout_add (500, (GSourceFunc) show_banner_timeout_handler, (gpointer) info);
+       }
+}
+
+static void clean_check_names_banner (CheckNamesInfo *info)
+{
+       if (info->hide_banner_timeout) {
+               info->free_info = TRUE;
+       } else {
+               if (info->show_banner_timeout) {
+                       g_source_remove (info->show_banner_timeout);
+               }
+               if (info->banner)
+                       gtk_widget_destroy (info->banner);
+               g_slice_free (CheckNamesInfo, info);
+       }
 }
 
 gboolean
@@ -810,20 +868,38 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor, gboolean updat
                        } else if (strstr (address, "@") == NULL) {
                                /* here goes searching in addressbook */
                                gboolean canceled;
-                               gchar *contact_id = NULL;
+                               GSList *contact_ids = NULL;
                                GSList *resolved_addresses = NULL;
 
-                               result = resolve_address (address, &resolved_addresses, &contact_id, &canceled);
+                               result = resolve_address (address, &resolved_addresses, &contact_ids, &canceled);
 
                                if (result) {
                                        gint new_length;
-                                       /* replace string */
-                                       modest_recpt_editor_replace_with_resolved_recipient (recpt_editor,
-                                                                                            &start_iter, &end_iter,
-                                                                                            resolved_addresses, 
-                                                                                            contact_id);
-                                       g_free (contact_id);
-                                       g_slist_foreach (resolved_addresses, (GFunc) g_free, NULL);
+                                       GSList *contact_ids_node, *resolved_addresses_node;
+
+                                       contact_ids_node = contact_ids;
+                                       resolved_addresses_node = resolved_addresses;
+
+                                       while (contact_ids_node != NULL) {
+                                               gchar *contact_id = (gchar *) contact_ids_node->data;
+                                               GSList *resolved_addresses_for_contact = 
+                                                       (GSList *) resolved_addresses_node->data;
+
+                                               /* replace string */
+                                               modest_recpt_editor_replace_with_resolved_recipient 
+                                                       (recpt_editor,
+                                                        &start_iter, &end_iter,
+                                                        resolved_addresses_for_contact, 
+                                                        contact_id);
+
+                                               g_free (contact_id);
+                                               g_slist_foreach (resolved_addresses_for_contact, (GFunc) g_free, NULL);
+                                               g_slist_free (resolved_addresses_for_contact);
+
+                                               contact_ids_node = g_slist_next (contact_ids_node);
+                                               resolved_addresses_node = g_slist_next (resolved_addresses_node);
+                                       }
+                                       g_slist_free (contact_ids);
                                        g_slist_free (resolved_addresses);
 
                                        /* update offset delta */
@@ -987,6 +1063,9 @@ select_contacts_for_name_dialog (const gchar *name)
                                                                                   _AB("addr_ti_dia_select_contacts"),
                                                                                   OSSO_ABOOK_CAPS_EMAIL,
                                                                                   OSSO_ABOOK_CONTACT_ORDER_NAME);
+               /* Enable multiselection */
+               osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
+                                                                 G_MAXUINT);
                osso_abook_contact_chooser_set_model (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
                                                      contact_model);
 
@@ -1004,34 +1083,38 @@ select_contacts_for_name_dialog (const gchar *name)
 static gboolean
 resolve_address (const gchar *address, 
                 GSList **resolved_addresses, 
-                gchar **contact_id,
+                GSList **contact_ids,
                 gboolean *canceled)
 {
        GList *resolved_contacts;
-       guint banner_timeout;
-       GtkWidget *banner = NULL;
+       CheckNamesInfo *info;;
 
        g_return_val_if_fail (canceled, FALSE);
 
+       *resolved_addresses = NULL;
+       *contact_ids = NULL;
        *canceled = FALSE;
-       banner_timeout = g_timeout_add (500, show_check_names_banner, &banner);
-
+       info = g_slice_new0 (CheckNamesInfo);
+       show_check_names_banner (info);
+       
        contact_model = osso_abook_contact_model_new ();
        if (!open_addressbook_sync ()) {
-               hide_check_names_banner (&banner, banner_timeout);
+               hide_check_names_banner (info);
                if (contact_model) {
                        g_object_unref (contact_model);
                        contact_model = NULL;
                }
+               clean_check_names_banner (info);
                return FALSE;
        }
-       hide_check_names_banner (&banner, banner_timeout);
+       hide_check_names_banner (info);
 
        resolved_contacts = get_contacts_for_name (address);
 
        if (resolved_contacts == NULL) {
                /* no matching contacts for the search string */
                modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
+               clean_check_names_banner (info);
                return FALSE;
        }
 
@@ -1040,30 +1123,36 @@ resolve_address (const gchar *address,
                g_list_free (resolved_contacts);
 
                resolved_contacts = select_contacts_for_name_dialog (address);
-               banner_timeout = g_timeout_add (500, show_check_names_banner, &banner);
-
        }
 
        /* get the resolved contacts (can be no contact) */
        if (resolved_contacts) {
-               gboolean found;
-               EContact *contact = (EContact *) resolved_contacts->data;
+               GList *node;
+               gboolean found = FALSE;
 
-               *resolved_addresses = get_recipients_for_given_contact (contact, canceled);
-               if (*resolved_addresses) {
-                       *contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
-                       found = TRUE;
-               } else {
-                       found = FALSE;
+               for (node = resolved_contacts; node != NULL; node = g_list_next (node)) {
+                       EContact *contact = (EContact *) node->data;
+                       GSList *resolved;
+                       gchar *contact_id;
+
+                       resolved = get_recipients_for_given_contact (contact, canceled);
+                       if (resolved) {
+                               contact_id = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
+                               *contact_ids = g_slist_append (*contact_ids, contact_id);
+                               found = TRUE;
+                               *resolved_addresses = g_slist_append (*resolved_addresses, resolved);
+                       }
                }
 
                g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
                g_list_free (resolved_contacts);
+               clean_check_names_banner (info);
 
                return found;
        } else {
                /* cancelled dialog to select more than one contact or
                 * selected no contact */
+               clean_check_names_banner (info);
                return FALSE;
        }