* Fixes several memory leaks
[modest] / src / maemo / modest-platform.c
index 4d61dbd..9ea8117 100644 (file)
@@ -55,6 +55,7 @@
 #include <libgnomevfs/gnome-vfs-mime-utils.h>
 #include <modest-account-settings-dialog.h>
 #include <easysetup/modest-easysetup-wizard-dialog.h>
+#include "modest-hildon-sort-dialog.h"
 #include <hildon/hildon-sound.h>
 #include <osso-mem.h>
 
@@ -530,6 +531,7 @@ entry_insert_text (GtkEditable *editable,
 
        chars = gtk_editable_get_chars (editable, 0, -1);
        chars_length = g_utf8_strlen (chars, -1);
+       g_free (chars);
 
        /* Show WID-INF036 */
        if (chars_length >= 20) {
@@ -586,165 +588,6 @@ entry_changed (GtkEditable *editable,
        g_free (chars);
 }
 
-static guint
-checked_hildon_sort_dialog_add_sort_key (HildonSortDialog *dialog, const gchar* key, guint max)
-{
-       gint sort_key;
-       
-       g_return_val_if_fail (dialog && HILDON_IS_SORT_DIALOG(dialog), 0);
-       g_return_val_if_fail (key, 0);
-       
-       sort_key = hildon_sort_dialog_add_sort_key (dialog, key);
-       if (sort_key < 0 || sort_key >= max) {
-               g_warning ("%s: out of range (%d) for %s", __FUNCTION__, sort_key, key);
-               return 0;
-       } else
-               return (guint)sort_key; 
-}
-
-
-static void
-launch_sort_headers_dialog (GtkWindow *parent_window,
-                           HildonSortDialog *dialog)
-{
-       ModestHeaderView *header_view = NULL;
-       GList *cols = NULL;
-       GtkSortType sort_type;
-       gint sort_key;
-       gint default_key = 0;
-       gint result;
-       gboolean outgoing = FALSE;
-       gint current_sort_colid = -1;
-       GtkSortType current_sort_type;
-       gint attachments_sort_id;
-       gint priority_sort_id;
-       GtkTreeSortable *sortable;
-       
-       /* Get header window */
-       if (MODEST_IS_MAIN_WINDOW (parent_window)) {
-               header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(parent_window),
-                                                                                     MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
-       }
-       if (!header_view)
-               return;
-       
-       /* Add sorting keys */
-       cols = modest_header_view_get_columns (header_view);
-       if (cols == NULL) 
-               return;
-#define SORT_ID_NUM 6
-       int sort_model_ids[SORT_ID_NUM];
-       int sort_ids[SORT_ID_NUM];
-
-       outgoing = (GPOINTER_TO_INT (g_object_get_data(G_OBJECT(cols->data), MODEST_HEADER_VIEW_COLUMN))==
-                   MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT);
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_sender_recipient"),
-                                                           SORT_ID_NUM);
-       if (outgoing) {
-               sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN;
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT;
-       } else {
-               sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN;
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN;
-       }
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_date"),
-                                                           SORT_ID_NUM);
-       if (outgoing) {
-               sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE;
-       } else {
-               sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE;
-       }
-       default_key = sort_key;
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_subject"),
-                                                           SORT_ID_NUM);
-       sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN;
-       if (outgoing)
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT;
-       else
-               sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN;
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_attachment"),
-                                                           SORT_ID_NUM);
-       sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN;
-       sort_ids[sort_key] = TNY_HEADER_FLAG_ATTACHMENTS;
-       attachments_sort_id = sort_key;
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_size"),
-                                                           SORT_ID_NUM);
-       sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN;
-       sort_ids[sort_key] = 0;
-
-       sort_key = checked_hildon_sort_dialog_add_sort_key (dialog, _("mcen_li_sort_priority"),
-                                                           SORT_ID_NUM);
-       sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN;
-       sort_ids[sort_key] = TNY_HEADER_FLAG_PRIORITY_MASK;
-       priority_sort_id = sort_key;
-       
-       sortable = GTK_TREE_SORTABLE (gtk_tree_model_filter_get_model
-                                     (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view)))));
-       /* Launch dialogs */
-       if (!gtk_tree_sortable_get_sort_column_id (sortable,
-                                                  &current_sort_colid, &current_sort_type)) {
-               hildon_sort_dialog_set_sort_key (dialog, default_key);
-               hildon_sort_dialog_set_sort_order (dialog, GTK_SORT_DESCENDING);
-       } else {
-               hildon_sort_dialog_set_sort_order (dialog, current_sort_type);
-               if (current_sort_colid == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN) {
-                       gpointer flags_sort_type_pointer;
-                       flags_sort_type_pointer = g_object_get_data (G_OBJECT (cols->data), MODEST_HEADER_VIEW_FLAG_SORT);
-                       if (GPOINTER_TO_INT (flags_sort_type_pointer) == TNY_HEADER_FLAG_PRIORITY_MASK)
-                               hildon_sort_dialog_set_sort_key (dialog, priority_sort_id);
-                       else
-                               hildon_sort_dialog_set_sort_key (dialog, attachments_sort_id);
-               } else {
-                       gint current_sort_keyid = 0;
-                       while (current_sort_keyid < 6) {
-                               if (sort_model_ids[current_sort_keyid] == current_sort_colid)
-                                       break;
-                               else 
-                                       current_sort_keyid++;
-                       }
-                       hildon_sort_dialog_set_sort_key (dialog, current_sort_keyid);
-               }
-       }
-
-       result = gtk_dialog_run (GTK_DIALOG (dialog));
-       if (result == GTK_RESPONSE_OK) {
-               sort_key = hildon_sort_dialog_get_sort_key (dialog);
-               if (sort_key < 0 || sort_key > SORT_ID_NUM -1) {
-                       g_warning ("%s: out of range (%d)", __FUNCTION__, sort_key);
-                       sort_key = 0;
-               }
-
-               sort_type = hildon_sort_dialog_get_sort_order (dialog);
-               if (sort_model_ids[sort_key] == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN) {
-                       g_object_set_data (G_OBJECT(cols->data), MODEST_HEADER_VIEW_FLAG_SORT,
-                                          GINT_TO_POINTER (sort_ids[sort_key]));
-                       /* This is a hack to make it resort rows always when flag fields are
-                        * selected. If we do not do this, changing sort field from priority to
-                        * attachments does not work */
-                       modest_header_view_sort_by_column_id (header_view, 0, sort_type);
-               } else {
-                       gtk_tree_view_column_set_sort_column_id (GTK_TREE_VIEW_COLUMN (cols->data), 
-                                                                sort_model_ids[sort_key]);
-               }
-
-               modest_header_view_sort_by_column_id (header_view, sort_model_ids[sort_key], sort_type);
-               gtk_tree_sortable_sort_column_changed (sortable);
-       }
-
-       modest_widget_memory_save (modest_runtime_get_conf (),
-                                  G_OBJECT (header_view), MODEST_CONF_HEADER_VIEW_KEY);
-       
-       /* free */
-       g_list_free(cols);      
-}
-
 
 
 static void
@@ -860,21 +703,22 @@ modest_platform_run_folder_name_dialog (GtkWindow *parent_window,
                          G_CALLBACK (entry_changed),
                          dialog);
 
+
+       /* Some locales like pt_BR need this to get the full window
+          title shown */
+       gtk_widget_set_size_request (GTK_WIDGET (dialog), 300, -1);
+
        /* Create the hbox */
        hbox = gtk_hbox_new (FALSE, 12);
-       gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
 
        /* Add hbox to dialog */
        gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
                            hbox, FALSE, FALSE, 0);
-
-       gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
-       gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
-
-       /* Some locales like pt_BR need this to get the full window
-          title shown */
-       gtk_widget_set_size_request (GTK_WIDGET (dialog), 300, -1);
+       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
+                                    GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
+       gtk_widget_show_all (GTK_WIDGET(dialog));
                
        result = gtk_dialog_run (GTK_DIALOG(dialog));
        if (result == GTK_RESPONSE_ACCEPT)
@@ -964,9 +808,16 @@ modest_platform_run_rename_folder_dialog (GtkWindow *parent_window,
 
 
 static void
-on_destroy_dialog (GtkDialog *dialog)
+on_destroy_dialog (GtkWidget *dialog)
 {
-       gtk_widget_destroy (GTK_WIDGET(dialog));
+       /* This could happen when the dialogs get programatically
+          hidden or destroyed (for example when closing the
+          application while a dialog is being shown) */
+       if (!GTK_IS_WIDGET (dialog))
+               return;
+
+       gtk_widget_destroy (dialog);
+
        if (gtk_events_pending ())
                gtk_main_iteration ();
 }
@@ -980,14 +831,11 @@ modest_platform_run_confirmation_dialog (GtkWindow *parent_window,
        
        dialog = hildon_note_new_confirmation (parent_window, message);
        modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
-                                    GTK_WINDOW (dialog));
+                                    GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
 
        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
-       on_destroy_dialog (GTK_DIALOG(dialog));
-       
-       while (gtk_events_pending ())
-               gtk_main_iteration ();
+       on_destroy_dialog (dialog);
 
        return response;
 }
@@ -1006,14 +854,11 @@ modest_platform_run_confirmation_dialog_with_buttons (GtkWindow *parent_window,
                                                           button_cancel, GTK_RESPONSE_CANCEL,
                                                           NULL);
        modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
-                                    GTK_WINDOW (dialog));
+                                    GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
 
        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
-       on_destroy_dialog (GTK_DIALOG(dialog));
-       
-       while (gtk_events_pending ())
-               gtk_main_iteration ();
+       on_destroy_dialog (dialog);
 
        return response;
 }
@@ -1029,13 +874,11 @@ modest_platform_run_yes_no_dialog (GtkWindow *parent_window,
                                                           _("mcen_bd_yes"), GTK_RESPONSE_YES,
                                                           _("mcen_bd_no"), GTK_RESPONSE_NO,
                                                           NULL);
-       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog));
+       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
+                                    GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
        response = gtk_dialog_run (GTK_DIALOG (dialog));
        
-       on_destroy_dialog (GTK_DIALOG(dialog));
-
-       while (gtk_events_pending ())
-               gtk_main_iteration ();
+       on_destroy_dialog (dialog);
 
        return response;
 }
@@ -1050,16 +893,14 @@ modest_platform_run_information_dialog (GtkWindow *parent_window,
        GtkWidget *note;
        
        note = hildon_note_new_information (parent_window, message);
-       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
-                                    GTK_WINDOW (note));
+       if (block)
+               modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
+                                            GTK_WINDOW (note), GTK_WINDOW (parent_window));
        
        if (block) {
                gtk_dialog_run (GTK_DIALOG (note));
        
-               on_destroy_dialog (GTK_DIALOG (note));
-
-               while (gtk_events_pending ())
-                       gtk_main_iteration ();
+               on_destroy_dialog (note);
        } else {
                g_signal_connect_swapped (note,
                                          "response", 
@@ -1240,31 +1081,18 @@ modest_platform_connect_and_wait_if_network_folderstore (GtkWindow *parent_windo
        return result;
 }
 
-void
-modest_platform_run_sort_dialog (GtkWindow *parent_window,
-                                ModestSortDialogType type)
+GtkWidget *
+modest_platform_create_sort_dialog       (GtkWindow *parent_window)
 {
-       GtkWidget *dialog = NULL;
+       GtkWidget *dialog;
 
-       /* Build dialog */
-       dialog = hildon_sort_dialog_new (parent_window);
-       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
-                                    GTK_WINDOW (dialog));
+       dialog = modest_hildon_sort_dialog_new (parent_window);
 
        hildon_help_dialog_help_enable (GTK_DIALOG(dialog),
                                        "applications_email_sort",
                                        modest_maemo_utils_get_osso_context());
 
-       /* Fill sort keys */
-       switch (type) {
-       case MODEST_SORT_HEADERS:
-               launch_sort_headers_dialog (parent_window, 
-                                           HILDON_SORT_DIALOG(dialog));
-               break;
-       }
-       
-       /* Free */
-       on_destroy_dialog (GTK_DIALOG(dialog));
+       return dialog;
 }
 
 
@@ -1658,11 +1486,31 @@ modest_platform_information_banner (GtkWidget *parent,
                                    const gchar *icon_name,
                                    const gchar *text)
 {
-       GtkWidget *banner;
-       ModestWindowMgr *mgr;
+       GtkWidget *banner, *banner_parent = NULL;
+       ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
 
-       mgr = modest_runtime_get_window_mgr ();
-       banner = hildon_banner_show_information (parent, icon_name, text);
+       if (modest_window_mgr_num_windows (mgr) == 0)
+               return;
+
+       if (parent && GTK_IS_WINDOW (parent)) {
+               /* If the window is the active one then show the
+                  banner on top of this window */
+               if (gtk_window_is_active (GTK_WINDOW (parent)))
+                       banner_parent = parent;
+               /* If the window is not the topmost but it's visible
+                  (it's minimized for example) then show the banner
+                  with no parent */ 
+               else if (GTK_WIDGET_VISIBLE (parent))
+                       banner_parent = NULL;
+               /* If the window is hidden (like the main window when
+                  running in the background) then do not show
+                  anything */
+               else 
+                       return;
+       }
+
+
+       banner = hildon_banner_show_information (banner_parent, icon_name, text);
 
        modest_window_mgr_register_banner (mgr);
        g_object_ref (mgr);
@@ -1676,6 +1524,10 @@ modest_platform_information_banner_with_timeout (GtkWidget *parent,
                                                 gint timeout)
 {
        GtkWidget *banner;
+
+       if (modest_window_mgr_num_windows (modest_runtime_get_window_mgr ()) == 0)
+               return;
+
        banner = hildon_banner_show_information (parent, icon_name, text);
        hildon_banner_set_timeout(HILDON_BANNER(banner), timeout);
 }
@@ -1689,6 +1541,9 @@ modest_platform_animation_banner (GtkWidget *parent,
 
        g_return_val_if_fail (text != NULL, NULL);
 
+       if (modest_window_mgr_num_windows (modest_runtime_get_window_mgr ()) == 0)
+               return NULL;
+
        /* If the parent is not visible then do not show */
        if (parent && !GTK_WIDGET_VISIBLE (parent))
                return NULL;
@@ -1761,6 +1616,8 @@ on_timeout_check_account_is_online(CheckAccountIdleData* data)
 gboolean
 modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
 {
+       gboolean is_online;
+
        g_return_val_if_fail (account, FALSE);
        
        printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
@@ -1804,9 +1661,10 @@ modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
        g_main_loop_unref (data->loop);
        /* g_main_context_unref (context); */
 
+       is_online = data->is_online;
        g_slice_free (CheckAccountIdleData, data);
        
-       return data->is_online; 
+       return is_online;       
 }
 
 
@@ -1869,10 +1727,10 @@ modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
                          (gpointer) certificate);
        
        modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
-                                    GTK_WINDOW (note));
+                                    GTK_WINDOW (note), (GtkWindow *) main_win);
        response = gtk_dialog_run(GTK_DIALOG(note));
 
-       on_destroy_dialog (GTK_DIALOG(note));
+       on_destroy_dialog (note);
        g_free (question);
        
        return response == GTK_RESPONSE_OK;
@@ -1902,12 +1760,12 @@ modest_platform_run_alert_dialog (const gchar* prompt,
                GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_win), 
                                                                              prompt));
                modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
-                                            GTK_WINDOW (dialog));
+                                            GTK_WINDOW (dialog), (GtkWindow *) main_win);
                
                const int response = gtk_dialog_run (GTK_DIALOG (dialog));
                retval = (response == GTK_RESPONSE_YES) || (response == GTK_RESPONSE_OK);
                
-               on_destroy_dialog (GTK_DIALOG(dialog));         
+               on_destroy_dialog (dialog);             
        } else {
                /* Just show the error text and use the default response: */
                modest_platform_run_information_dialog (GTK_WINDOW (main_win), 
@@ -2212,15 +2070,6 @@ modest_platform_double_connect_and_perform (GtkWindow *parent_window,
 }
 
 GtkWidget *
-modest_platform_get_account_settings_dialog (ModestAccountSettings *settings)
-{
-       ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
-
-       modest_account_settings_dialog_set_account (dialog, settings);
-       return GTK_WIDGET (dialog);
-}
-
-GtkWidget *
 modest_platform_get_account_settings_wizard (void)
 {
        ModestEasysetupWizardDialog *dialog = modest_easysetup_wizard_dialog_new ();
@@ -2266,24 +2115,23 @@ modest_platform_get_current_connection (void)
 
 
 gboolean
-modest_platform_check_memory_low (ModestWindow *win)
+modest_platform_check_memory_low (ModestWindow *win,
+                                 gboolean visuals)
 {
        gboolean lowmem;
-
-       g_return_val_if_fail (win == NULL || MODEST_IS_WINDOW(win), FALSE);
        
        /* are we in low memory state? */
        lowmem = osso_mem_in_lowmem_state () ? TRUE : FALSE;
        
-       if (win && lowmem)
+       if (win && lowmem && visuals)
                modest_platform_run_information_dialog (
                        GTK_WINDOW(win),
                        dgettext("ke-recv","memr_ib_operation_disabled"),
                        TRUE);
 
        if (lowmem)
-               g_warning ("%s: low memory reached. disallowing some operations",
-                          __FUNCTION__);
+               g_debug ("%s: low memory reached. disallowing some operations",
+                        __FUNCTION__);
 
        return lowmem;
 }