2007-08-31 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-maemo-utils.c
index 518fbd1..3f05e5b 100644 (file)
@@ -45,6 +45,7 @@
 #include "modest-hildon-includes.h"
 
 #include "modest-maemo-utils.h"
+#include "modest-platform.h"
 
 /*
  * For getting and tracking the Bluetooth name
@@ -203,6 +204,11 @@ modest_maemo_utils_get_device_name (void)
 gboolean 
 modest_maemo_utils_folder_writable (const gchar *filename)
 {
+       g_return_val_if_fail (filename, FALSE);
+
+       if (!filename)
+               return FALSE;
+       
        if (g_strncasecmp (filename, "obex", 4) != 0) {
                GnomeVFSFileInfo folder_info;
                gchar *folder;
@@ -277,9 +283,14 @@ on_idle_secure_auth_finished (gpointer user_data)
        ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
        /* Operation has finished, close the dialog. Control continues after
         * gtk_dialog_run in modest_maemo_utils_get_supported_secure_authentication_methods() */
-       gdk_threads_enter();
+
+       /* This is a GDK lock because we are an idle callback and
+        * the code below is or does Gtk+ code */
+
+       gdk_threads_enter(); /* CHECKED */
        gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT);
-       gdk_threads_leave();
+       gdk_threads_leave(); /* CHECKED */
+
        return FALSE;
 }
 
@@ -293,8 +304,6 @@ on_camel_account_get_supported_secure_authentication (
        ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
        g_return_if_fail (info);
 
-       gdk_threads_enter();
-
        /* Free everything if the actual action was canceled */
        if (info->cancel)
        {
@@ -324,18 +333,23 @@ on_camel_account_get_supported_secure_authentication (
                        GList *result = NULL;
                        TnyIterator* iter = tny_list_create_iterator(auth_types);
                        while (!tny_iterator_is_done(iter)) {
-                               const gchar *auth_name = tny_pair_get_name(TNY_PAIR(tny_iterator_get_current(iter)));
-                               printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
-                               ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs, 
-                                       auth_name);
-                               if (matching)
-                               {
-                                       result = g_list_append (result, GINT_TO_POINTER((ModestConnectionProtocol)matching->first));
+                               TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter));
+                               const gchar *auth_name = NULL;
+                               if (pair) {
+                                       auth_name = tny_pair_get_name(pair);
+                                       g_object_unref (pair);
+                                       pair = NULL;
                                }
+
+                               printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
+
+                               ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name);
+                               if(proto != MODEST_PROTOCOL_AUTH_NONE)
+                                               result = g_list_prepend(result, GINT_TO_POINTER(proto));
+
                                tny_iterator_next(iter);
                        }
-       
-                       g_object_unref(auth_types);
+                       g_object_unref (iter);
 
                        modest_pair_list_free (pairs);
        
@@ -347,8 +361,6 @@ on_camel_account_get_supported_secure_authentication (
                /* Close the dialog in a main thread */
                g_idle_add(on_idle_secure_auth_finished, info);
        }
-
-       gdk_threads_leave();
 }
 
 static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
@@ -363,11 +375,39 @@ static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user
        }
 }
 
-GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
+
+typedef struct
+{
+       GMainLoop* loop;
+} UserData;
+
+static UserData *user_data = NULL;
+
+static void
+on_account_online (TnyCamelAccount *account, GError *err)
+{
+       printf ("DEBUGa1: %s\n", __FUNCTION__);
+       
+       if (err) {
+               printf("DEBUG: %s: error=\n  %s\n", __FUNCTION__, err->message);        
+       }
+       
+       /* Allow the function that requested this callback to continue: */
+       /* TODO: Tinymail should really give us user_data with this callback. */
+       if (user_data && user_data->loop)
+               g_main_loop_quit (user_data->loop);
+}
+
+GList*
+modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
        const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
 {
        g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
        
+       /* We need a connection to get the capabilities; */
+       if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
+               return NULL;
+        
        /*
        result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
        */
@@ -390,7 +430,6 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra
                tny_account = NULL;
        }
 
-       /* TODO: Handle connection requests. */
        
        if (!tny_account) {
                g_printerr ("%s could not create tny account.", __FUNCTION__);
@@ -407,6 +446,9 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra
        /* Required for POP, at least */
        tny_account_set_user (tny_account, username);
                               
+       if(port > 0)
+               tny_account_set_port (tny_account, port);
+               
        /* Set the session for the account, so we can use it: */
        ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
        TnySessionCamel *session = 
@@ -414,19 +456,46 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra
        g_return_val_if_fail (session, NULL);
        tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
        
-       if(port > 0)
-               tny_account_set_port (tny_account, port);
-               
-
+       
+       /* This blocks on the result: */
+       /* TODO: Fix tinymail to take user_data for the callback instead of using one static instance: */
+       if (user_data)  {
+               g_slice_free (UserData, user_data);
+               user_data = NULL;
+       }
+       
+       user_data = g_slice_new0 (UserData);
+       user_data->loop = g_main_loop_new (NULL, FALSE /* not running */);
+
+       /* We get a warning if we don't do use tny_camel_account_set_online():
+        * GLIB CRITICAL ** camel-lite - camel_service_query_auth_types: assertion `service != NULL' failed.
+        */
+       tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (tny_account), TRUE, &on_account_online);
+       printf ("DEBUGa2: %s\n", __FUNCTION__);
+       
+       /* This main loop will run until the idle handler has stopped it: */
+       printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
+       GDK_THREADS_LEAVE();
+       g_main_loop_run (user_data->loop);
+       GDK_THREADS_ENTER();
+       printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
+       g_main_loop_unref (user_data->loop);
+       /* g_main_context_unref (context); */
+
+       g_slice_free (UserData, user_data);
+       user_data = NULL;
+       
+       
        /* Ask camel to ask the server, asynchronously: */
        ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
        info->result = NULL;
        info->cancel = FALSE;
        info->error = NULL;
        info->progress = gtk_progress_bar_new();
+       /* TODO: Need logical_ID for the title: */
        info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
                                                   parent_window, GTK_DIALOG_MODAL,
-                                                  GTK_STOCK_CANCEL,
+                                                  _("mcen_bd_dialog_cancel"),
                                                   GTK_RESPONSE_REJECT,
                                                   NULL);
        //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
@@ -525,6 +594,55 @@ modest_maemo_show_information_note_and_forget (GtkWindow *parent_window, const g
        gtk_widget_show (GTK_WIDGET (dialog));
 }
 
+#if 0
+static void
+on_hide (GtkDialog *dialog, gpointer user_data)
+{
+       /* Just destroy the dialog: */
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+#endif
+
+#if 0 /* Not used now. */
+/* user_data for the idle callback: */
+typedef struct 
+{
+       GtkWindow *parent_window;
+       gchar *message;
+} ModestIdleNoteInfo;
+
+static gboolean
+on_idle_show_information(gpointer user_data)
+{
+       ModestIdleNoteInfo *info = (ModestIdleNoteInfo*)user_data;
+       
+       modest_maemo_show_information_note_and_forget (info->parent_window, info->message);
+       
+       g_free (info->message);
+       g_slice_free (ModestIdleNoteInfo, info);
+       
+       return FALSE; /* Don't call this again. */
+}
+
+void modest_maemo_show_information_note_in_main_context_and_forget (GtkWindow *parent_window, const gchar* message)
+{
+       ModestIdleNoteInfo *info = g_slice_new (ModestIdleNoteInfo);
+       info->parent_window = parent_window;
+       info->message = g_strdup (message);
+       
+       g_idle_add (on_idle_show_information, info);
+}
+#endif
+
+void modest_maemo_show_dialog_and_forget (GtkWindow *parent_window, GtkDialog *dialog)
+{
+       gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
+       
+       /* Destroy the dialog when it is closed: */
+       g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (on_response), NULL);
+       gtk_widget_show (GTK_WIDGET (dialog));
+}
+
 
 
 void
@@ -535,3 +653,26 @@ modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, gboolean thumbable
        hildon_helper_set_thumb_scrollbar (win, thumbable);
 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
 }
+
+void
+modest_maemo_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value)
+{
+       GSList *proxies = NULL;
+
+       g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
+
+       for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
+            proxies != NULL; proxies = g_slist_next (proxies)) {
+               GtkWidget *widget = (GtkWidget *) proxies->data;
+               gtk_action_block_activate_from (GTK_ACTION (action), widget);
+       }
+
+       gtk_toggle_action_set_active (action, value);
+
+       for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
+            proxies != NULL; proxies = g_slist_next (proxies)) {
+               GtkWidget *widget = (GtkWidget *) proxies->data;
+               gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
+       }
+
+}