Query activesync for addressbooks too (if available).
authorJose Dapena Paz <jdapena@igalia.com>
Wed, 18 Nov 2009 11:40:24 +0000 (12:40 +0100)
committerJose Dapena Paz <jdapena@igalia.com>
Wed, 18 Nov 2009 13:15:53 +0000 (14:15 +0100)
src/dbus_api/Makefile.am
src/dbus_api/asdbus-bindings.h [new file with mode: 0644]
src/dbus_api/asdbus.c [new file with mode: 0644]
src/dbus_api/asdbus.h [new file with mode: 0644]
src/hildon2/modest-address-book.c

index be23501..f5e89b7 100644 (file)
@@ -49,7 +49,8 @@ noinst_LTLIBRARIES=\
 
 libmodest_dbus_api_la_SOURCES=              \
        modest-dbus-callbacks.h modest-dbus-callbacks.c \
-       modest-dbus-api.h
+       modest-dbus-api.h \
+       asdbus.h asdbus.c asdbus-bindings.h
 
 LDADD = \
        $(LIBMODEST_DBUS_CLIENT_LIBS)
diff --git a/src/dbus_api/asdbus-bindings.h b/src/dbus_api/asdbus-bindings.h
new file mode 100644 (file)
index 0000000..64f9aaf
--- /dev/null
@@ -0,0 +1,80 @@
+/* Generated by dbus-binding-tool;
+
+<?xml version="1.0" encoding="UTF-8" ?>
+<node name="/com/nokia/asdbus">
+    <interface name="com.nokia.asdbus">
+        <method name="resolveRecipients">
+            <arg type="as" name="names" direction="in" />
+            <arg type="(ia(siia(iss)))" name="reply" direction="out" />
+        </method>
+    </interface>
+</node>
+
+*/
+
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+
+G_BEGIN_DECLS
+
+#ifndef DBUS_GLIB_CLIENT_WRAPPERS_com_nokia_asdbus
+#define DBUS_GLIB_CLIENT_WRAPPERS_com_nokia_asdbus
+
+static
+#ifdef G_HAVE_INLINE
+inline
+#endif
+gboolean
+com_nokia_asdbus_resolve_recipients (DBusGProxy *proxy, const char ** IN_names, GValueArray** OUT_reply, GError **error)
+{
+    return dbus_g_proxy_call (proxy, "resolveRecipients", error,
+            G_TYPE_STRV, IN_names, G_TYPE_INVALID,
+            dbus_g_type_get_struct ("GValueArray",
+                G_TYPE_INT,  // status
+                dbus_g_type_get_collection ("GPtrArray",  // responces
+                    dbus_g_type_get_struct ("GValueArray",
+                        G_TYPE_STRING,  // to
+                        G_TYPE_INT, // status
+                        G_TYPE_INT, // num
+                        dbus_g_type_get_collection ("GPtrArray", // recipients
+                            dbus_g_type_get_struct ("GValueArray",
+                                G_TYPE_INT,
+                                G_TYPE_STRING,
+                                G_TYPE_STRING,
+                                G_TYPE_INVALID)),
+                        G_TYPE_INVALID)),
+                G_TYPE_INVALID),
+            OUT_reply,
+            G_TYPE_INVALID);
+}
+
+typedef void (*com_nokia_asdbus_resolve_recipients_reply) (DBusGProxy *proxy, GValueArray *OUT_reply, GError *error, gpointer userdata);
+
+static void
+com_nokia_asdbus_resolve_recipients_async_callback (DBusGProxy *proxy, DBusGProxyCall *call, void *user_data)
+{
+  DBusGAsyncData *data = (DBusGAsyncData*) user_data;
+  GError *error = NULL;
+  GValueArray* OUT_reply;
+  dbus_g_proxy_end_call (proxy, call, &error, dbus_g_type_get_struct ("GValueArray", G_TYPE_INT, dbus_g_type_get_collection ("GPtrArray", dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, dbus_g_type_get_collection ("GPtrArray", dbus_g_type_get_struct ("GValueArray", G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID)), G_TYPE_INVALID)), G_TYPE_INVALID), &OUT_reply, G_TYPE_INVALID);
+  (*(com_nokia_asdbus_resolve_recipients_reply)data->cb) (proxy, OUT_reply, error, data->userdata);
+  return;
+}
+
+static
+#ifdef G_HAVE_INLINE
+inline
+#endif
+DBusGProxyCall*
+com_nokia_asdbus_resolve_recipients_async (DBusGProxy *proxy, const char ** IN_names, com_nokia_asdbus_resolve_recipients_reply callback, gpointer userdata)
+
+{
+  DBusGAsyncData *stuff;
+  stuff = g_new (DBusGAsyncData, 1);
+  stuff->cb = G_CALLBACK (callback);
+  stuff->userdata = userdata;
+  return dbus_g_proxy_begin_call (proxy, "resolveRecipients", com_nokia_asdbus_resolve_recipients_async_callback, stuff, g_free, G_TYPE_STRV, IN_names, G_TYPE_INVALID);
+}
+#endif /* defined DBUS_GLIB_CLIENT_WRAPPERS_com_nokia_asdbus */
+
+G_END_DECLS
diff --git a/src/dbus_api/asdbus.c b/src/dbus_api/asdbus.c
new file mode 100644 (file)
index 0000000..d21eb11
--- /dev/null
@@ -0,0 +1,119 @@
+/* Copyright (c) 2006, 2008 Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "asdbus.h"
+#include "asdbus-bindings.h"
+#include <glib-object.h>
+#include "modest-runtime.h"
+#include "modest-protocol.h"
+#include "modest-protocol-registry.h"
+#include "modest-account-mgr.h"
+
+
+static
+void recipient_iter (const GValue *value, gpointer user_data)
+{
+       AsDbusRecipient *recipient = g_new0 (AsDbusRecipient, 1);
+       g_return_if_fail (dbus_g_type_is_struct (G_VALUE_TYPE (value)));
+       g_return_if_fail (3 >= dbus_g_type_get_struct_size (G_VALUE_TYPE (value)));
+       g_return_if_fail (G_TYPE_STRING == dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 1));
+       g_return_if_fail (G_TYPE_STRING == dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 2));
+       g_return_if_fail (dbus_g_type_struct_get (value,
+                       1, &recipient->display_name,
+                       2, &recipient->email_address,
+                       G_MAXUINT));
+       *((GList **)user_data) = g_list_append (*((GList **)user_data), recipient);
+}
+
+
+static
+void response_iter (const GValue *value, gpointer user_data)
+{
+       GValue recipients = {0,};
+       g_return_if_fail (dbus_g_type_is_struct (G_VALUE_TYPE (value)));
+       g_return_if_fail (4 >= dbus_g_type_get_struct_size (G_VALUE_TYPE (value)));
+       g_return_if_fail (dbus_g_type_is_collection (dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 3)));
+       g_return_if_fail (dbus_g_type_struct_get_member (value, 3, g_value_init (&recipients, dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 3))));
+       dbus_g_type_collection_value_iterate (&recipients, recipient_iter, user_data);
+}
+
+
+/* assuming g_type_init is already called */
+GList * asdbus_resolve_recipients (const gchar *name)
+{
+       GList *result = NULL;
+       DBusGConnection *bus = NULL;
+       GError *error = NULL;
+       DBusGProxy *asdbus = NULL;
+       GValueArray* reply = NULL;
+       ModestProtocol *protocol = NULL;
+       ModestProtocolType protocol_type;
+
+       protocol = modest_protocol_registry_get_protocol_by_name (
+                       modest_runtime_get_protocol_registry (),
+                       MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,  // MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS ?
+                       "activesync");
+       protocol_type = modest_protocol_get_type_id (protocol);
+    if (!modest_account_mgr_singleton_protocol_exists(modest_runtime_get_account_mgr(), protocol_type)) {
+               return NULL;
+       }
+
+       const gchar *names[2];
+       names[0] = name;
+       names[1] = NULL;
+
+       bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+       if (bus == NULL) {
+               /** FIXME: proper log */
+               g_printerr ("Failed to open connection to bus: %s\n", error->message);
+               g_error_free (error);
+               goto CLEANUP;
+       }
+
+       asdbus = dbus_g_proxy_new_for_name (bus, "com.nokia.asdbus", "/com/nokia/asdbus", "com.nokia.asdbus");
+
+       if (!com_nokia_asdbus_resolve_recipients (asdbus, names, &reply, &error)) {
+               g_printerr ("com_nokia_asdbus_many_args failed: %s\n", error->message);
+               g_error_free (error);
+               goto CLEANUP;
+       }
+
+       if (!reply) goto CLEANUP;
+       if (2 != reply->n_values) goto CLEANUP;
+       if (G_TYPE_INT != G_VALUE_TYPE (&reply->values[0])) goto CLEANUP;
+       if (!dbus_g_type_is_collection (G_VALUE_TYPE (&reply->values[1]))) goto CLEANUP;
+
+       dbus_g_type_collection_value_iterate (&reply->values[1], response_iter, &result);
+
+CLEANUP:
+       g_value_array_free (reply);
+       g_object_unref (asdbus);
+
+       return result;
+}
diff --git a/src/dbus_api/asdbus.h b/src/dbus_api/asdbus.h
new file mode 100644 (file)
index 0000000..4de34b2
--- /dev/null
@@ -0,0 +1,45 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#ifndef __MODEST_ASDBUS_H__
+#define __MODEST_ASDBUS_H__
+
+
+#include <glib.h>
+
+typedef struct {
+       gchar *display_name;
+       gchar *email_address;
+} AsDbusRecipient;
+
+GList * asdbus_resolve_recipients (const gchar *name);
+
+
+#endif /* __MODEST_ASDBUS_H__ */
index 01c6ea4..285cd2e 100644 (file)
@@ -51,6 +51,7 @@
 #include <gtk/gtktreeselection.h>
 #include <gtk/gtkentry.h>
 #include <modest-maemo-utils.h>
+#include <dbus_api/asdbus.h>
 
 static OssoABookContactModel *contact_model =  NULL;
 static EBook *book = NULL;
@@ -954,7 +955,7 @@ get_contacts_for_name (const gchar *name)
 
 
 static GList *
-select_contacts_for_name_dialog (const gchar *name)
+select_contacts_for_name_dialog (const gchar *name, GList *external_contacts)
 {
        EBookQuery *book_query = NULL;
        EBookView *book_view = NULL;
@@ -988,6 +989,36 @@ select_contacts_for_name_dialog (const gchar *name)
                                                                                   _AB("addr_ti_dia_select_contacts"),
                                                                                   OSSO_ABOOK_CAPS_EMAIL,
                                                                                   OSSO_ABOOK_CONTACT_ORDER_NAME);
+
+               if (external_contacts) {
+
+                       GList *row_list = NULL;
+                       while (external_contacts) {
+
+                               AsDbusRecipient *recipient = (AsDbusRecipient*)external_contacts->data;
+                               external_contacts = g_list_next (external_contacts);
+                               if (!recipient)
+                                       continue;
+
+                               char *uid = osso_abook_create_temporary_uid ();
+                               OssoABookContact *contact = osso_abook_contact_new ();
+                               osso_abook_contact_set_uid (contact, uid);
+                               e_contact_set (E_CONTACT (contact), E_CONTACT_FULL_NAME, recipient->display_name);
+                               osso_abook_contact_set_value (E_CONTACT (contact), EVC_EMAIL, recipient->email_address);
+
+                               OssoABookListStoreRow *row = osso_abook_list_store_row_new (contact);
+                               row_list = g_list_prepend (row_list, row);
+                               /* FIXME: unref row? */
+
+                               g_free (uid);
+                       }
+
+                       if (row_list) {
+                               osso_abook_list_store_merge_rows (OSSO_ABOOK_LIST_STORE (contact_model), row_list);
+                               g_list_free (row_list);
+                       }
+               }
+
                /* Enable multiselection */
                osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
                                                                  G_MAXUINT);
@@ -1012,7 +1043,7 @@ resolve_address (const gchar *address,
                 gboolean *canceled)
 {
        GList *resolved_contacts;
-       CheckNamesInfo *info;;
+       CheckNamesInfo *info;
 
        g_return_val_if_fail (canceled, FALSE);
 
@@ -1034,20 +1065,103 @@ resolve_address (const gchar *address,
        }
 
        resolved_contacts = get_contacts_for_name (address);
+       GList *external_contacts = asdbus_resolve_recipients (address);
        hide_check_names_banner (info);
 
-       if (resolved_contacts == NULL) {
+       if (NULL == resolved_contacts && NULL == external_contacts) {
                /* 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;
        }
 
-       if (g_list_length (resolved_contacts) > 1) {
+       /* check for duplicate emails and remove from external_contacts if any */
+       if (resolved_contacts && external_contacts) {
+
+               GList *node, *ex_node;
+
+               for (ex_node = external_contacts; ex_node != NULL; ex_node = g_list_next (ex_node)) {
+
+                       AsDbusRecipient *recipient = (AsDbusRecipient*)ex_node->data;
+                       if (!recipient)
+                               continue;
+
+                       for (node = resolved_contacts; node != NULL; node = g_list_next (node)) {
+
+                               EContact *contact = (EContact*)node->data;
+                               GList *emails = e_contact_get (contact, E_CONTACT_EMAIL);
+                               if (!emails)
+                                       continue;
+
+                               if (g_list_find_custom (emails, recipient->email_address, (GCompareFunc) compare_addresses)) {
+
+                                       g_free (recipient->display_name);
+                                       g_free (recipient->email_address);
+                                       g_free (recipient);
+                                       recipient = NULL;
+                                       ex_node->data = NULL;
+                               }
+
+                               g_list_foreach (emails, (GFunc) g_free, NULL);
+                               g_list_free (emails);
+                       }
+               }
+       }
+
+       if (g_list_length (resolved_contacts) + g_list_length (external_contacts) > 1) {
                /* show a dialog to select the contact from the resolved ones */
                g_list_free (resolved_contacts);
 
-               resolved_contacts = select_contacts_for_name_dialog (address);
+               resolved_contacts = select_contacts_for_name_dialog (address, external_contacts);
+
+               if (external_contacts) {
+
+                       GList *node;
+                       for (node = external_contacts; node != NULL; node = g_list_next (node)) {
+
+                               AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
+                               if (!recipient)
+                                       continue;
+
+                               g_free (recipient->display_name);
+                               g_free (recipient->email_address);
+                               g_free (recipient);
+                       }
+
+                       g_list_free (external_contacts);
+                       external_contacts = NULL;
+               }
+       }
+
+       if (external_contacts) {
+
+               gboolean found = FALSE;
+               GList *node;
+               for (node = external_contacts; node != NULL; node = g_list_next (node)) {
+
+                       AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
+                       if (!recipient)
+                               continue;
+
+                       GString *formatted_recipient = g_string_new (NULL);
+                       g_string_printf (formatted_recipient, "\"%s\" <%s>", recipient->display_name, recipient->email_address);
+
+                       /* FIXME: why we have to have list of lists? */
+                       GSList *formattedlist = g_slist_append(NULL, formatted_recipient->str);
+                       *resolved_addresses = g_slist_append (*resolved_addresses, formattedlist);
+                       /* FIXME: how important is an UID? */
+                       *contact_ids = g_slist_append (*contact_ids, g_strdup ("temp-uid"));
+                       found = TRUE;
+
+                       g_string_free (formatted_recipient, FALSE); /* character data segment is NOT freed */
+                       g_free (recipient->display_name);
+                       g_free (recipient->email_address);
+                       g_free (recipient);
+               }
+
+               g_list_free (external_contacts);
+               external_contacts = NULL;
+               return found;
        }
 
        /* get the resolved contacts (can be no contact) */