Added bug 90185 to changelog
[modest] / src / modest-utils.c
index 2a5981c..1469803 100644 (file)
@@ -43,6 +43,9 @@
 #include <modest-defs.h>
 #include "modest-utils.h"
 #include "modest-platform.h"
+#include "modest-account-mgr-helpers.h"
+#include "modest-text-utils.h"
+#include <modest-local-folder-info.h>
 
 GQuark
 modest_utils_get_supported_secure_authentication_error_quark (void)
@@ -59,16 +62,18 @@ modest_utils_folder_writable (const gchar *filename)
                return FALSE;
        
        if (g_strncasecmp (filename, "obex", 4) != 0) {
-               GnomeVFSFileInfo folder_info;
+               GnomeVFSFileInfo *folder_info;
                gchar *folder;
                folder = g_path_get_dirname (filename);
-               gnome_vfs_get_file_info (folder, &folder_info,
+               folder_info = gnome_vfs_file_info_new ();
+               gnome_vfs_get_file_info (folder, folder_info,
                                         GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
                g_free (folder);
-               if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
-                     (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
+               if (!((folder_info->permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
+                     (folder_info->permissions & GNOME_VFS_PERM_USER_WRITE))) {
                        return FALSE;
                }
+               gnome_vfs_file_info_unref (folder_info);
        }
        return TRUE;
 }
@@ -79,6 +84,8 @@ modest_utils_file_exists (const gchar *filename)
        GnomeVFSURI *uri = NULL;
        gboolean result = FALSE;
 
+       g_return_val_if_fail (filename, FALSE);
+       
        uri = gnome_vfs_uri_new (filename);
        if (uri) {
                result = gnome_vfs_uri_exists (uri);
@@ -96,19 +103,20 @@ modest_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base,
        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);
+       g_return_val_if_fail (orig_name && strlen(orig_name) != 0, NULL);
+
        if (strlen(orig_name) > 200) {
                g_warning ("%s: filename too long ('%s')",
                           __FUNCTION__, orig_name);
                return NULL;
        }
-       
+
        if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) {
                g_warning ("%s: filename contains '/' character(s) (%s)",
                           __FUNCTION__, orig_name);
                return NULL;
        }
-               
+
        /* make a random subdir under /tmp or /var/tmp */
        if (hash_base != NULL) {
                hash_number = g_str_hash (hash_base);
@@ -138,7 +146,7 @@ modest_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base,
                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));                     
+                                       __FUNCTION__, filepath, g_strerror(errno));
                        g_free (filepath);
                        g_free (tmpdir);
                        return NULL;
@@ -186,18 +194,18 @@ on_idle_secure_auth_finished (gpointer user_data)
 }
 
 static void
-on_camel_account_get_supported_secure_authentication (
-  TnyCamelAccount *self, gboolean cancelled,
-  TnyList *auth_types, GError *err, 
-  gpointer user_data)
+on_camel_account_get_supported_secure_authentication (TnyCamelAccount *self, gboolean cancelled,
+       TnyList *auth_types, GError *err, gpointer user_data)
 {
+       g_return_if_fail (TNY_IS_CAMEL_ACCOUNT(self));
+       g_return_if_fail (TNY_IS_LIST(auth_types));
+       
        ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
        g_return_if_fail (info);
        
 
        /* Free everything if the actual action was canceled */
-       if (info->cancel)
-       {
+       if (info->cancel) {
                /* The operation was canceled and the ownership of the info given to us
                 * so that we could still check the cancel flag. */
                g_slice_free (ModestGetSupportedAuthInfo, info);
@@ -205,8 +213,7 @@ on_camel_account_get_supported_secure_authentication (
        }
        else
        {
-               if (err)
-               {
+               if (err) {
                        if (info->error) {
                                g_error_free (info->error);
                                info->error = NULL;
@@ -218,10 +225,9 @@ on_camel_account_get_supported_secure_authentication (
                if (!auth_types) {
                        g_warning ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
                }
-               else if (tny_list_get_length(auth_types) == 0) {
+               else if (tny_list_get_length(auth_types) == 0) 
                        g_warning ("DEBUG: %s: auth_types is an empty TnyList.\n", __FUNCTION__);
-               } else
-               {
+               else {
                        ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
   
                        /* Get the enum value for the strings: */
@@ -261,8 +267,9 @@ on_camel_account_get_supported_secure_authentication (
 static void
 on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
 {
-       if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT)
-       {
+       g_return_if_fail (GTK_IS_WIDGET(dialog));
+       
+       if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT) {
                ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
                g_return_if_fail(info);
                /* This gives the ownership of the info to the worker thread. */
@@ -270,6 +277,26 @@ on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
                info->cancel = TRUE;
        }
 }
+typedef struct {
+       GtkProgressBar *progress;
+       gboolean not_finished;
+} KeepPulsing;
+
+static gboolean
+keep_pulsing (gpointer user_data)
+{
+       KeepPulsing *info = (KeepPulsing *) user_data;
+       
+       gtk_progress_bar_pulse (info->progress);
+
+       if (!info->not_finished) {
+               g_object_unref (info->progress);
+               g_slice_free (KeepPulsing, info);
+               return FALSE;
+       }
+       
+       return TRUE;
+}
 
 GList*
 modest_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
@@ -336,8 +363,11 @@ modest_utils_get_supported_secure_authentication_methods (ModestTransportStorePr
        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"),
+
+       /* FIXME: the title (first arg) here is empty; there should be 'accountwizard_fi_authentication', 
+        *  but that does not exist yet; see bug #82487. so, for now, we simply leave it empty
+         */
+       info->dialog = gtk_dialog_new_with_buttons(" ", 
                                                   parent_window, GTK_DIALOG_MODAL,
                                                   _("mcen_bd_dialog_cancel"),
                                                   GTK_RESPONSE_REJECT,
@@ -347,12 +377,19 @@ modest_utils_get_supported_secure_authentication_methods (ModestTransportStorePr
        g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
        
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
-                         gtk_label_new("Checking for supported authentication types..."));
+                         gtk_label_new(_("emev_ni_checking_supported_auth_methods")));
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
        gtk_widget_show_all(info->dialog);
-       gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
+
+       KeepPulsing *pi = g_slice_new (KeepPulsing);
+       pi->progress = (GtkProgressBar *) g_object_ref (info->progress);
+       pi->not_finished = TRUE;
+       
+       /* Starts the pulsing of the progressbar */
+       g_timeout_add (500, keep_pulsing, pi);
        
        printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
+       
        tny_camel_account_get_supported_secure_authentication (
                TNY_CAMEL_ACCOUNT (tny_account),
                on_camel_account_get_supported_secure_authentication,
@@ -361,6 +398,9 @@ modest_utils_get_supported_secure_authentication_methods (ModestTransportStorePr
 
        gtk_dialog_run (GTK_DIALOG (info->dialog));
        
+       pi->not_finished = FALSE;
+       /* pi is freed in the timeout itself to avoid a GCond here */
+       
        gtk_widget_destroy(info->dialog);
                        
        GList *result = info->result;
@@ -396,6 +436,9 @@ void
 modest_utils_show_dialog_and_forget (GtkWindow *parent_window, 
                                     GtkDialog *dialog)
 {
+       g_return_if_fail (GTK_IS_WINDOW(parent_window));
+       g_return_if_fail (GTK_IS_DIALOG(dialog));
+
        gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
        
        /* Destroy the dialog when it is closed: */
@@ -430,3 +473,93 @@ modest_utils_toggle_action_set_active_block_notify (GtkToggleAction *action, gbo
 
 }
 
+
+gint 
+modest_list_index (TnyList *list, GObject *object)
+{
+       TnyIterator *iter;
+       gint index = 0;
+
+       g_return_val_if_fail (TNY_IS_LIST(list), -1);
+       g_return_val_if_fail (G_IS_OBJECT(object), -1);
+       
+       iter = tny_list_create_iterator (list);
+       while (!tny_iterator_is_done (iter)) {
+               GObject *current = tny_iterator_get_current (iter);
+
+               g_object_unref (current);
+               if (current == object)
+                       break;
+
+               tny_iterator_next (iter);
+               index++;
+       }
+
+       if (tny_iterator_is_done (iter))
+               index = -1;
+       g_object_unref (iter);
+       return index;
+}
+
+guint64 
+modest_utils_get_available_space (const gchar *maildir_path)
+{
+       gchar *folder;
+       gchar *uri_string;
+       GnomeVFSURI *uri;
+       GnomeVFSFileSize size;
+
+       folder = modest_local_folder_info_get_maildir_path (maildir_path);
+       uri_string = gnome_vfs_get_uri_from_local_path (folder);
+       uri = gnome_vfs_uri_new (uri_string);
+       g_free (folder);
+       g_free (uri_string);
+
+       if (uri) {
+               if (gnome_vfs_get_volume_free_space (uri, &size) != GNOME_VFS_OK)
+                       size = 0;
+               gnome_vfs_uri_unref (uri);
+       } else {
+               size = 0;
+       }
+
+       return (guint64) size;
+}
+
+gchar *
+modest_utils_get_account_name_from_recipient (const gchar *from_header)
+{
+       gchar *account_name = NULL;
+       ModestAccountMgr *mgr = NULL;
+       GSList *accounts = NULL, *node = NULL;
+
+       g_return_val_if_fail (from_header, NULL);
+
+       mgr = modest_runtime_get_account_mgr ();
+       accounts = modest_account_mgr_account_names (mgr, TRUE);
+               
+       for (node = accounts; node != NULL; node = g_slist_next (node)) {
+               gchar *from = 
+                       modest_account_mgr_get_from_string (mgr, node->data);
+                       
+               if (from) {
+                       gchar *from_email = 
+                               modest_text_utils_get_email_address (from);
+                               
+                       if (from_email) {
+                               if (!modest_text_utils_utf8_strcmp (from_header, from_email, TRUE)) {
+                                       account_name = g_strdup (node->data);
+                                       g_free (from);
+                                       g_free (from_email);
+                                       break;
+                               }
+                               g_free (from_email);
+                       }
+                       g_free (from);
+               }
+       }
+       g_slist_foreach (accounts, (GFunc) g_free, NULL);
+       g_slist_free (accounts);
+
+       return account_name;
+}