Corrected the number of unread messages for plug-in based protocols
[modest] / src / dbus_api / modest-dbus-callbacks.c
index 8c48486..c1a8f11 100644 (file)
@@ -1531,8 +1531,17 @@ on_idle_show_memory_low (gpointer user_data)
 static gboolean
 on_idle_present_modal (gpointer user_data)
 {
+       GtkWindow *current, *transient;
        gdk_threads_enter ();
-       gtk_window_present (user_data);
+       current = (GtkWindow *) user_data;
+       while (GTK_IS_DIALOG (current)) {
+               transient = gtk_window_get_transient_for (GTK_WINDOW (current));
+               if (transient == NULL)
+                       break;
+               else
+                       current = transient;
+       }
+       gtk_window_present (current);
        gdk_threads_leave ();
 
        return FALSE;
@@ -1643,6 +1652,8 @@ modest_dbus_req_handler(const gchar * interface, const gchar * method,
        DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
        DBUS_TYPE_STRING_AS_STRING \
        DBUS_TYPE_STRING_AS_STRING \
+       DBUS_TYPE_STRING_AS_STRING \
+       DBUS_TYPE_INT64_AS_STRING \
        DBUS_TYPE_ARRAY_AS_STRING \
        ACCOUNT_HIT_DBUS_TYPE \
        DBUS_STRUCT_END_CHAR_AS_STRING
@@ -1896,12 +1907,15 @@ typedef struct {
        DBusConnection *con;
        DBusMessage *message;
        GList *account_hits_list;
+       ModestMailOperation *mail_op;
 } GetUnreadMessagesHelper;
 
 typedef struct {
        gchar *account_id;
        gchar *account_name;
+       gchar *store_protocol;
        gchar *mailbox_id;
+       gint unread_count;
        GList *header_list;
 } AccountHits;
 
@@ -1925,6 +1939,8 @@ static void return_results (GetUnreadMessagesHelper *helper)
                        AccountHits *ah = (AccountHits *) node->data;
                        const char *account_id;
                        const char *account_name;
+                       const char *store_protocol;
+                       gint64 unread_count;
                        DBusMessageIter ah_struct_iter;
                        DBusMessageIter sh_array_iter;
                        GList *result_node;
@@ -1935,12 +1951,20 @@ static void return_results (GetUnreadMessagesHelper *helper)
                                                          &ah_struct_iter);
                        account_id = ah->account_id;
                        account_name = ah->account_name;
+                       store_protocol = ah->store_protocol;
+                       unread_count = ah->unread_count;
                        dbus_message_iter_append_basic (&ah_struct_iter,
                                                        DBUS_TYPE_STRING,
                                                        &account_id);
                        dbus_message_iter_append_basic (&ah_struct_iter,
                                                        DBUS_TYPE_STRING,
                                                        &account_name);
+                       dbus_message_iter_append_basic (&ah_struct_iter,
+                                                       DBUS_TYPE_STRING,
+                                                       &store_protocol);
+                       dbus_message_iter_append_basic (&ah_struct_iter,
+                                                       DBUS_TYPE_INT64,
+                                                       &unread_count);
 
                        dbus_message_iter_open_container (&ah_struct_iter,
                                                          DBUS_TYPE_ARRAY,
@@ -1974,6 +1998,7 @@ static void return_results (GetUnreadMessagesHelper *helper)
                                                           &ah_struct_iter); 
                        g_free (ah->account_id);
                        g_free (ah->account_name);
+                       g_free (ah->store_protocol);
                        g_list_free (ah->header_list);
                }
 
@@ -1987,6 +2012,9 @@ static void return_results (GetUnreadMessagesHelper *helper)
        g_list_free (helper->account_hits_list);
        dbus_message_unref (helper->message);
        g_object_unref (helper->accounts_list);
+       modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
+                                           helper->mail_op);
+       g_object_unref (helper->mail_op);
        g_slice_free (GetUnreadMessagesHelper, helper);
 }
 
@@ -2008,11 +2036,16 @@ static void get_unread_messages_get_headers_cb (TnyFolder *self,
        AccountHits *account_hits;
        const gchar *folder_id;
        const gchar *bar;
+       ModestProtocolType store_protocol_type;
+       ModestProtocol *store_protocol;
+       gint unread_count;
+       ModestProtocolRegistry *registry;
 
        acc_iterator = tny_list_create_iterator (helper->accounts_list);
        account = TNY_ACCOUNT (tny_iterator_get_current (acc_iterator));
 
        headers_iterator = tny_list_create_iterator (headers);
+       unread_count = 0;
        while (!tny_iterator_is_done (headers_iterator)) {
                TnyHeader *header;
                TnyHeaderFlags flags;
@@ -2020,7 +2053,8 @@ static void get_unread_messages_get_headers_cb (TnyFolder *self,
                header = TNY_HEADER (tny_iterator_get_current (headers_iterator));
                flags = tny_header_get_flags (header);
                if (!(flags & TNY_HEADER_FLAG_SEEN)) {
-                 result_list = g_list_insert_sorted (result_list, g_object_ref (header), (GCompareFunc) headers_cmp);
+                       unread_count++;
+                       result_list = g_list_insert_sorted (result_list, g_object_ref (header), (GCompareFunc) headers_cmp);
                        if (members_count == helper->unread_msgs_count) {
                                g_object_unref (result_list->data);
                                result_list = g_list_delete_link (result_list, result_list);
@@ -2034,11 +2068,22 @@ static void get_unread_messages_get_headers_cb (TnyFolder *self,
        }
        g_object_unref (headers_iterator);
 
+       registry = modest_runtime_get_protocol_registry ();
+       store_protocol_type = modest_tny_account_get_protocol_type (account);
+
+       /* Get the number of unread messages for plug-in based accounts */
+       if (modest_protocol_registry_protocol_type_is_provider (registry, store_protocol_type)) {
+               unread_count = tny_folder_get_unread_count (self);
+       }
+
        account_hits = g_slice_new (AccountHits);
-       account_hits->account_id = g_strdup (tny_account_get_id (account));
+       account_hits->account_id = g_strdup (modest_tny_account_get_parent_modest_account_name_for_server_account (account));
        account_hits->account_name = g_strdup (tny_account_get_name (account));
+       store_protocol = modest_protocol_registry_get_protocol_by_type (registry, store_protocol_type);
+       account_hits->store_protocol = g_strdup (modest_protocol_get_name (store_protocol));
        account_hits->header_list = result_list;
        account_hits->mailbox_id = NULL;
+       account_hits->unread_count = unread_count;
 
        folder_id = tny_folder_get_id (self);
        bar = g_strstr_len (folder_id, -1, "/");
@@ -2057,92 +2102,6 @@ static void get_unread_messages_get_headers_cb (TnyFolder *self,
 
 }
 
-static TnyFolder *
-find_inbox (TnyFolderStore *fs)
-{
-       TnyList *folders;
-       GError *err = NULL;
-       TnyFolder *folder = NULL;
-       folders = TNY_LIST (tny_simple_list_new ());
-
-       tny_folder_store_get_folders (fs, folders, NULL, FALSE, &err);
-       if (err == NULL) {
-               TnyIterator *iterator;
-
-               for (iterator = tny_list_create_iterator (folders);
-                    !tny_iterator_is_done (iterator);
-                    tny_iterator_next (iterator)) {
-                       TnyFolder *current;
-
-                       current = TNY_FOLDER (tny_iterator_get_current (iterator));
-                       if (tny_folder_get_folder_type (current) == TNY_FOLDER_TYPE_INBOX) {
-                               folder = current;
-                               break;
-                       }
-                       g_object_unref (current);
-               }
-               g_object_unref (iterator);
-       }
-       g_object_unref (folders);
-
-       return folder;
-}
-
-static TnyList *
-get_inboxes (TnyAccount *account)
-{
-       ModestProtocolType store_protocol;
-       TnyList *result;
-       gboolean mailboxes_protocol;
-
-       result= TNY_LIST (tny_simple_list_new ());
-       store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
-                                                               tny_account_get_id (account));
-       mailboxes_protocol = 
-               modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
-                                                               store_protocol,
-                                                               MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
-       if (mailboxes_protocol) {
-               /* Currently we disable the support for obtaining the results of multimailbox accounts */
-#ifndef DISABLE_GET_UNREAD_MSGS_FOR_MULTI_MAILBOX
-               TnyList *mailboxes;
-               GError *err = NULL;
-               mailboxes = TNY_LIST (tny_simple_list_new ());
-
-               tny_folder_store_get_folders (TNY_FOLDER_STORE (account), mailboxes, NULL, FALSE, &err);
-               if (err == NULL) {
-                       TnyIterator *iterator;
-
-                       for (iterator = tny_list_create_iterator (mailboxes);
-                            !tny_iterator_is_done (iterator);
-                            tny_iterator_next (iterator)) {
-                               TnyFolder *mailbox;
-                               TnyFolder *inbox;
-
-                               mailbox = TNY_FOLDER (tny_iterator_get_current (iterator));
-                               inbox = find_inbox (TNY_FOLDER_STORE (mailbox));
-                               if (inbox) {
-                                       tny_list_prepend (result, G_OBJECT (inbox));
-                                       g_object_unref (inbox);
-                               }
-                               g_object_unref (mailbox);
-                       }
-                       g_object_unref (iterator);
-               }
-               g_object_unref (mailboxes);
-#endif
-       } else {
-               TnyFolder *inbox;
-               inbox = find_inbox (TNY_FOLDER_STORE (account));
-               if (inbox) {
-                       tny_list_prepend (result, G_OBJECT (inbox));
-                       g_object_unref (inbox);
-               }
-       }
-
-       return result;
-}
-
 static void
 get_unread_messages_get_headers (GetUnreadMessagesHelper *helper)
 {
@@ -2179,6 +2138,31 @@ get_unread_messages_get_headers (GetUnreadMessagesHelper *helper)
        g_object_unref (iterator);
 }
 
+static void get_account_folders_cb (TnyFolderStore *self, gboolean cancelled, TnyList *list, GError *err, gpointer user_data)
+{
+       GetUnreadMessagesHelper *helper = (GetUnreadMessagesHelper *) user_data;
+       TnyIterator *iterator;
+
+       helper->inboxes_list =  TNY_LIST (tny_simple_list_new ());
+       iterator = tny_list_create_iterator (list);
+       while (!tny_iterator_is_done (iterator)) {
+               TnyFolder *folder;
+
+               folder = TNY_FOLDER (tny_iterator_get_current (iterator));
+               if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX) {
+                       tny_list_prepend (helper->inboxes_list, G_OBJECT (folder));
+                       g_object_unref (folder);
+                       break;
+               }
+               g_object_unref (folder);
+               tny_iterator_next (iterator);
+       }
+       g_object_unref (iterator);
+       
+       get_unread_messages_get_headers (helper);
+}
+
+
 static void
 get_unread_messages_get_account (GetUnreadMessagesHelper *helper)
 {
@@ -2191,10 +2175,13 @@ get_unread_messages_get_account (GetUnreadMessagesHelper *helper)
                return_results (helper);
        } else {
                TnyAccount *account = NULL;
+               TnyList *folders_list;
 
                account = TNY_ACCOUNT (tny_iterator_get_current (iterator));
-               helper->inboxes_list = get_inboxes (account);
-               get_unread_messages_get_headers (helper);
+               folders_list = TNY_LIST (tny_simple_list_new ());
+               tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders_list, NULL,
+                                                   FALSE, get_account_folders_cb, NULL, helper);
+               g_object_unref (folders_list);
                g_object_unref (account);
 
        }
@@ -2240,6 +2227,9 @@ on_dbus_method_get_unread_messages (DBusConnection *con, DBusMessage *message)
        helper->account_hits_list = NULL;
        helper->inboxes_list = NULL;
        helper->accounts_list = TNY_LIST (tny_simple_list_new ());
+       helper->mail_op = modest_mail_operation_new (NULL);
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
+                                        helper->mail_op);
 
        g_idle_add ((GSourceFunc) on_idle_get_unread_messages, helper);
 }