2007-06-25 Murray Cumming <murrayc@murrayc.com>
[modest] / src / dbus_api / modest-dbus-callbacks.c
index b7a0437..3c4f70b 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <glib/gstdio.h>
+#ifdef MODEST_HAVE_HILDON0_WIDGETS
+#include <libgnomevfs/gnome-vfs-mime-utils.h>
+#else
 #include <libgnomevfs/gnome-vfs-mime.h>
+#endif
 #include <tny-fs-stream.h>
 
 #include <tny-list.h>
@@ -957,15 +961,14 @@ static void
 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
 {
        ModestDBusSearchFlags dbus_flags;
-       ModestSearch  search;
        DBusMessage  *reply = NULL;
        dbus_bool_t  res;
        dbus_int64_t sd_v;
        dbus_int64_t ed_v;
        dbus_int32_t flags_v;
        dbus_uint32_t size_v;
-       char *folder;
-       char *query;
+       const char *folder;
+       const char *query;
        time_t start_date;
        time_t end_date;
        GList *hits;
@@ -979,7 +982,7 @@ on_dbus_method_search (DBusConnection *con, DBusMessage *message)
        res = dbus_message_get_args (message,
                                     &error,
                                     DBUS_TYPE_STRING, &query,
-                                    DBUS_TYPE_STRING, &folder,
+                                    DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
                                     DBUS_TYPE_INT64, &sd_v,
                                     DBUS_TYPE_INT64, &ed_v,
                                     DBUS_TYPE_INT32, &flags_v,
@@ -990,14 +993,27 @@ on_dbus_method_search (DBusConnection *con, DBusMessage *message)
        start_date = (time_t) sd_v;
        end_date = (time_t) ed_v;
 
+       ModestSearch search;
        memset (&search, 0, sizeof (search));
+       
+       /* Remember what folder we are searching in:
+        *
+        * Note that we don't copy the strings, 
+        * because this struct will only be used for the lifetime of this function.
+        */
+       search.folder = folder;
+
+   /* Remember the text to search for: */
 #ifdef MODEST_HAVE_OGS
        search.query  = query;
 #endif
+
+       /* Other criteria: */
        search.before = start_date;
        search.after  = end_date;
        search.flags  = 0;
 
+       /* Text to serach for in various parts of the message: */
        if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
                search.flags |= MODEST_SEARCH_SUBJECT;
                search.subject = query;
@@ -1037,6 +1053,10 @@ on_dbus_method_search (DBusConnection *con, DBusMessage *message)
        search.flags |= MODEST_SEARCH_USE_OGS;
        g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
 #endif
+
+       /* Note that this currently gets folders and messages from the servers, 
+        * which can take a long time. libmodest_dbus_client_search() can timeout, 
+        * reporting no results, if this takes a long time: */
        hits = modest_search_all_accounts (&search);
 
        reply = dbus_message_new_method_return (message);
@@ -1083,9 +1103,9 @@ get_folders_result_to_message (DBusMessage *reply,
        GList *list_iter = folder_ids;
        for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
                
-               const gchar *folder_id = (const gchar*)list_iter->data;
-               if (folder_id) {
-                       g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_id);      
+               const gchar *folder_name = (const gchar*)list_iter->data;
+               if (folder_name) {
+                       /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
                        
                        DBusMessageIter struct_iter;
                        dbus_message_iter_open_container (&array_iter,
@@ -1093,12 +1113,7 @@ get_folders_result_to_message (DBusMessage *reply,
                                                          NULL,
                                                          &struct_iter);
        
-                       dbus_message_iter_append_basic (&struct_iter,
-                                                       DBUS_TYPE_STRING,
-                                                       &folder_id);
-       
                        /* name: */
-                       const gchar *folder_name = (const gchar*)list_iter->data;
                        dbus_message_iter_append_basic (&struct_iter,
                                                        DBUS_TYPE_STRING,
                                                        &folder_name); /* The string will be copied. */
@@ -1119,35 +1134,70 @@ get_folders_result_to_message (DBusMessage *reply,
        return reply;
 }
 
-void add_folders_to_list (TnyFolderStore *folder_store, GList** list)
+static void
+add_single_folder_to_list (TnyFolder *folder, GList** list)
 {
-       if (!folder_store)
+       if (!folder)
                return;
                
        /* Add this folder to the list: */
-       if (TNY_IS_FOLDER (folder_store)) {
-               const gchar * folder_name = tny_folder_get_name (TNY_FOLDER (folder_store));
-               if (folder_name)
-                       *list = g_list_append(*list, g_strdup (folder_name));
+       /*
+       const gchar * folder_name = tny_folder_get_name (folder);
+       if (folder_name)
+               *list = g_list_append(*list, g_strdup (folder_name));
+       else {
+       */
+               /* osso-global-search only uses one string,
+                * so ID is the only thing that could possibly identify a folder.
+                * TODO: osso-global search should probably be changed to 
+                * take an ID and a Name.
+                */
+               const gchar * id =  tny_folder_get_id (folder);
+               if (id && strlen(id))
+                       *list = g_list_append(*list, g_strdup (id));
+               /*
+               else {
+                       g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);     
+               }
+               
        }
+       */
+}
+
+static void
+add_folders_to_list (TnyFolderStore *folder_store, GList** list)
+{
+       if (!folder_store)
+               return;
+       
+       /* Add this folder to the list: */
+       if (TNY_IS_FOLDER (folder_store)) {
+               add_single_folder_to_list (TNY_FOLDER (folder_store), list);
+       }       
+       
                
        /* Recurse into child folders: */
                
        /* Get the folders list: */
+       /*
        TnyFolderStoreQuery *query = tny_folder_store_query_new ();
        tny_folder_store_query_add_item (query, NULL, 
                TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
+       */
        TnyList *all_folders = tny_simple_list_new ();
        tny_folder_store_get_folders (folder_store,
                                      all_folders,
-                                     query,
+                                     NULL /* query */,
                                      NULL /* error */);
 
        TnyIterator *iter = tny_list_create_iterator (all_folders);
        while (!tny_iterator_is_done (iter)) {
-               TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
-
-               add_folders_to_list (folder, list);
+               TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
+               if (TNY_IS_FOLDER_STORE (folder))
+                       add_folders_to_list (TNY_FOLDER_STORE (folder), list);
+               else {
+                       add_single_folder_to_list (TNY_FOLDER (folder), list);
+               }
                
                tny_iterator_next (iter);
        }
@@ -1178,15 +1228,29 @@ on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
                g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
        } 
                
+       printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
        g_free (account_name);
        account_name = NULL;
        
        GList *folder_names = NULL;
        add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
-       
 
        g_object_unref (account);
        account = NULL;
+       
+       
+       /* Also add the folders from the local folders account,
+        * because they are (currently) used with all accounts:
+        * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
+        */
+       TnyAccount *account_local = 
+               modest_tny_account_store_get_local_folders_account (
+                       TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
+       add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
+
+       g_object_unref (account_local);
+       account_local = NULL;
+
 
        /* Put the result in a DBus reply: */
        reply = dbus_message_new_method_return (message);