* fix for NB#63391, where a notes-attachment would be opened in the browser instead
[modest] / src / maemo / modest-maemo-utils.c
index c21f44b..0d42cda 100644 (file)
@@ -34,6 +34,8 @@
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib-lowlevel.h>
 #include <glib.h>
+#include <glib/gstdio.h>
+#include <errno.h>
 #include <modest-runtime.h>
 #include <libgnomevfs/gnome-vfs.h>
 #include <tny-fs-stream.h>
@@ -239,27 +241,38 @@ modest_maemo_utils_file_exists (const gchar *filename)
 }
 
 TnyFsStream *
-modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path)
+modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path)
 {
-       TnyStream *tmp_fs_stream = NULL;
        gint fd;
        gchar *filepath = NULL;
-       gchar *template = NULL;
-
-       if (extension != NULL)
-               template = g_strdup_printf ("XXXXXX.%s", extension);
+       gchar *tmpdir;
+
+       /* make a random subdir under /tmp or /var/tmp */
+       tmpdir = g_strdup_printf ("%s/%d", g_get_tmp_dir (), (guint)random());
+       if (g_mkdir (tmpdir, 0755) == -1) {
+               g_warning ("%s: failed to create dir '%s': %s",
+                          __FUNCTION__, tmpdir, g_strerror(errno));
+               g_free (tmpdir);
+               return NULL;
+       }
 
-       fd = g_file_open_tmp (template, &filepath, NULL);
-       g_free (template);
-       if (path != NULL)
-               *path = filepath;
+       /* try to write the file there */
+       filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
+       fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
        if (fd == -1) {
-               g_message ("TODO BANNER: Error saving stream");
+               g_warning ("%s: failed to create '%s': %s",
+                          __FUNCTION__, filepath, g_strerror(errno));
+               g_free (tmpdir);
+               g_free (filepath);
                return NULL;
        }
-       tmp_fs_stream = tny_fs_stream_new (fd);
-       
-       return TNY_FS_STREAM (tmp_fs_stream);
+
+       g_free (tmpdir);
+
+       if (path)
+               *path = filepath;
+
+       return TNY_FS_STREAM (tny_fs_stream_new (fd));
 }
 
 typedef struct 
@@ -342,12 +355,11 @@ on_camel_account_get_supported_secure_authentication (
                                }
 
                                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));
-                               }
+
+                               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 (iter);
@@ -376,7 +388,31 @@ 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);
@@ -423,6 +459,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 = 
@@ -430,10 +469,36 @@ 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;