Fix progress bar in the view message window:
[modest] / src / maemo / modest-maemo-utils.c
index 8fe546c..a28a8d9 100644 (file)
@@ -47,6 +47,7 @@
 #include <tny-camel-pop-store-account.h>
 #include "modest-hildon-includes.h"
 
+#include <modest-defs.h>
 #include "modest-maemo-utils.h"
 #include "modest-platform.h"
 
 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
                           "',member='" BTNAME_SIG_CHANGED "'"
 
-GQuark modest_maemo_utils_get_supported_secure_authentication_error_quark (void)
+
+static osso_context_t *__osso_context = NULL; /* urgh global */
+
+osso_context_t *
+modest_maemo_utils_get_osso_context (void)
+{
+       if (!__osso_context) 
+               g_warning ("%s: __osso_context == NULL", __FUNCTION__);
+       
+       return __osso_context;
+}
+
+void
+modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
+{
+       g_return_if_fail (osso_context);
+       __osso_context = osso_context;
+}
+
+
+GQuark
+modest_maemo_utils_get_supported_secure_authentication_error_quark (void)
 {
        return g_quark_from_static_string("modest-maemo-utils-get-supported-secure-authentication-error-quark");
 }
@@ -242,11 +264,12 @@ modest_maemo_utils_file_exists (const gchar *filename)
 }
 
 TnyFsStream *
-modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path)
+modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path)
 {
        gint fd;
        gchar *filepath = NULL;
        gchar *tmpdir;
+       guint hash_number;
 
        /* hmmm... maybe we need a modest_text_utils_validate_file_name? */
        g_return_val_if_fail (orig_name || strlen(orig_name) == 0, NULL);
@@ -263,23 +286,39 @@ modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path)
        }
                
        /* 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) {
+       if (hash_base != NULL) {
+               hash_number = g_str_hash (hash_base);
+       } else {
+               hash_number = (guint) random ();
+       }
+       tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number);
+       if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) {
                g_warning ("%s: failed to create dir '%s': %s",
                           __FUNCTION__, tmpdir, g_strerror(errno));
                g_free (tmpdir);
                return NULL;
        }
 
-       /* 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_warning ("%s: failed to create '%s': %s",
-                          __FUNCTION__, filepath, g_strerror(errno));
+       /* don't overwrite if it already exists, even if it is writable */
+       if (modest_maemo_utils_file_exists (filepath)) {
+               if (path!=NULL) {
+                       *path = filepath;
+               } else {
+                       g_free (filepath);
+               }
                g_free (tmpdir);
-               g_free (filepath);
                return NULL;
+       } else {
+               /* try to write the file there */
+               fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+               if (fd == -1) {
+                       g_warning ("%s: failed to create '%s': %s",
+                                       __FUNCTION__, filepath, g_strerror(errno));                     
+                       g_free (filepath);
+                       g_free (tmpdir);
+                       return NULL;
+               }
        }
 
        g_free (tmpdir);
@@ -395,7 +434,8 @@ on_camel_account_get_supported_secure_authentication (
        }
 }
 
-static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
+static void
+on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
 {
        if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT)
        {
@@ -657,24 +697,31 @@ modest_maemo_toggle_action_set_active_block_notify (GtkToggleAction *action, gbo
 }
 
 
-
 FILE*
 modest_maemo_open_mcc_mapping_file (void)
 {
        FILE* result;
        
-       /* Load the file one line at a time: */
-#ifdef MODEST_HILDON_VERSION_0
-       const gchar* filepath = PROVIDER_DATA_DIR "/mcc_mapping";
-#else
-       const gchar* filepath = "/usr/share/operator-wizard/mcc_mapping";
-#endif /*MODEST_HILDON_VERSION_0*/
+       const gchar* path;
+       const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
+       const gchar* path2 = MODEST_MCC_MAPPING;
        
-       result = fopen (filepath, "r"); 
-       if (!result) 
-               g_printerr ("modest: failed to open mcc mapping file");
+       if (access(path1, R_OK) == 0) 
+               path = path1;
+       else if (access(path2, R_OK) == 0)
+               path = path2;
+       else {
+               g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
+                          __FUNCTION__, path1, path2);
+               return NULL;
+       }
        
+       result = fopen (path, "r");
+       if (!result) {
+               g_warning ("%s: error opening mapping file '%s': %s",
+                          __FUNCTION__, path, strerror(errno));
+               return NULL;
+       }
        return result;
 }
 
-