* bugfix: register the new dialog as the new modal dialog; this
[modest] / src / maemo / modest-platform.c
index ae94a93..586a455 100644 (file)
@@ -54,6 +54,7 @@
 #include <modest-text-utils.h>
 #include "modest-tny-folder.h"
 #include <string.h>
+#include <libgnomevfs/gnome-vfs-mime-utils.h>
 
 
 #define HILDON_OSSO_URI_ACTION "uri-action"
@@ -75,12 +76,40 @@ on_modest_conf_update_interval_changed (ModestConf* self,
        }
 }
 
+
+
+static gboolean
+check_required_files (void)
+{
+       FILE *mcc_file = modest_maemo_open_mcc_mapping_file ();
+       if (!mcc_file) {
+               g_printerr ("modest: check for mcc file failed\n");
+               return FALSE;
+       } else 
+               fclose (mcc_file);
+
+       if (access (MODEST_PROVIDERS_DATA_PATH, R_OK) != 0) {
+               g_printerr ("modest: cannot find providers data\n");
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+
+
 gboolean
 modest_platform_init (int argc, char *argv[])
 {
        osso_hw_state_t hw_state = { 0 };
        DBusConnection *con;    
 
+       if (!check_required_files ()) {
+               g_printerr ("modest: missing required files\n");
+               return FALSE;
+       }
+
+       
        osso_context =
                osso_initialize(PACKAGE,PACKAGE_VERSION,
                                FALSE, NULL);   
@@ -94,7 +123,7 @@ modest_platform_init (int argc, char *argv[])
                return FALSE;
 
        }
-       
+
        /* Add a D-Bus handler to be used when the main osso-rpc 
         * D-Bus handler has not handled something.
         * We use this for D-Bus methods that need to use more complex types 
@@ -177,45 +206,6 @@ modest_platform_get_new_device (void)
        return TNY_DEVICE (tny_maemo_conic_device_new ());
 }
 
-
-const gchar*
-guess_mime_type_from_name (const gchar* name)
-{
-       int i;
-       const static gchar* mime_type;
-       const static gchar* mime_map[][2] = {
-               { ".note.html", "text/note"}, /* for the osso_notes program */
-               { ".deb",       "application/x-deb"},
-               { ".install",   "application/x-install-instructions"},
-               { ".html",      "text/html"}, 
-               { ".htm",       "text/html"}, 
-               { ".pdf",       "application/pdf"},
-               { ".doc",       "application/msword"},
-               { ".xls",       "application/excel"},
-               { ".png",       "image/png" },
-               { ".gif",       "image/gif" },
-               { ".jpg",       "image/jpeg"},
-               { ".jpeg",      "image/jpeg"},
-               { ".mp3",       "audio/mp3" }
-       };
-
-       mime_type = "application/octet-stream";
-
-       if (name) {
-               gchar* lc_name = g_utf8_strdown (name, -1);
-               for (i = 0; i != G_N_ELEMENTS(mime_map); ++i) {
-                       if (g_str_has_suffix (lc_name, mime_map[i][0])) {
-                               mime_type = mime_map[i][1];
-                               break;
-                       }
-               }
-               g_free (lc_name);
-       }
-       
-       return mime_type;
-}
-
-
 gchar*
 modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
                                    gchar **effective_mime_type)
@@ -223,9 +213,9 @@ modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
        GString *mime_str = NULL;
        gchar *icon_name  = NULL;
        gchar **icons, **cursor;
-
+       
        if (!mime_type || !g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
-               mime_str = g_string_new (guess_mime_type_from_name(name));
+               mime_str = g_string_new (gnome_vfs_get_mime_type_for_name (name));
        else {
                mime_str = g_string_new (mime_type);
                g_string_ascii_down (mime_str);
@@ -259,14 +249,12 @@ modest_platform_activate_uri (const gchar *uri)
        HildonURIAction *action;
        gboolean result = FALSE;
        GSList *actions, *iter = NULL;
-       const gchar *scheme;
        
        g_return_val_if_fail (uri, FALSE);
        if (!uri)
                return FALSE;
-
-       scheme = hildon_uri_get_scheme_from_uri (uri, NULL);
-       actions = hildon_uri_get_actions (scheme, NULL);
+       
+       actions = hildon_uri_get_actions_by_uri (uri, -1, NULL);
        
        for (iter = actions; iter; iter = g_slist_next (iter)) {
                action = (HildonURIAction*) iter->data;
@@ -283,7 +271,7 @@ modest_platform_activate_uri (const gchar *uri)
                }
        }
        
-       /* if we could open it with email, try something else */
+       /* if we could not open it with email, try something else */
        if (!result)
                result = hildon_uri_open (uri, NULL, NULL);     
                
@@ -371,14 +359,12 @@ activate_uri_popup_item (GtkMenuItem *menu_item,
 gboolean 
 modest_platform_show_uri_popup (const gchar *uri)
 {
-       gchar *scheme;
        GSList *actions_list;
 
        if (uri == NULL)
                return FALSE;
-       
-       scheme = hildon_uri_get_scheme_from_uri (uri, NULL);
-       actions_list = hildon_uri_get_actions (scheme, NULL);
+
+       actions_list = hildon_uri_get_actions_by_uri (uri, -1, NULL);
        if (actions_list != NULL) {
                GSList *node;
                GtkWidget *menu = gtk_menu_new ();
@@ -424,8 +410,7 @@ modest_platform_show_uri_popup (const gchar *uri)
        } else {
                hildon_banner_show_information (NULL, NULL, _("mcen_ib_unsupported_link"));
        }
-       
-       g_free (scheme);
+
        return TRUE;
 }
 
@@ -563,7 +548,7 @@ launch_sort_headers_dialog (GtkWindow *parent_window,
        /* 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_WIDGET_TYPE_HEADER_VIEW));
+                                                                                     MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
        }
        if (!header_view) return;
 
@@ -711,6 +696,7 @@ on_response (GtkDialog *dialog,
 }
 
 
+
 static gint
 modest_platform_run_folder_name_dialog (GtkWindow *parent_window,
                                        TnyFolderStore *parent,
@@ -776,11 +762,8 @@ modest_platform_run_folder_name_dialog (GtkWindow *parent_window,
                            hbox, FALSE, FALSE, 0);
        
        gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
-       
        gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
-
-
-
+       
        result = gtk_dialog_run (GTK_DIALOG(dialog));
        if (result == GTK_RESPONSE_ACCEPT)
                *folder_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
@@ -863,42 +846,77 @@ modest_platform_run_rename_folder_dialog (GtkWindow *parent_window,
                                                       folder_name);
 }
 
+
+
+static void
+on_destroy_dialog (GtkDialog *dialog)
+{
+       if (dialog == modest_window_mgr_get_modal_dialog (modest_runtime_get_window_mgr()))
+               modest_window_mgr_set_modal_dialog (modest_runtime_get_window_mgr(),
+                                                   NULL);
+       gtk_widget_destroy (GTK_WIDGET(dialog));
+}
+
+
+/* is there already a modal dialog? if so, return TRUE, if not set this
+ * dialog to be the registered one */
+static void
+check_modal_and_set_maybe (GtkDialog *dialog)
+{
+       GtkDialog *old_modal;
+
+       old_modal =
+               modest_window_mgr_get_modal_dialog (modest_runtime_get_window_mgr());
+
+       if (!old_modal) {       
+               gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
+       } else {
+               /* un-modalize the old one; the one on top should be the
+                * modal one */
+               gtk_window_set_modal (GTK_WINDOW(old_modal), FALSE);    
+               gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
+       }
+
+       /* this will be the new modal dialog */
+       modest_window_mgr_set_modal_dialog (modest_runtime_get_window_mgr(),
+                                           dialog);
+}
+
 gint
 modest_platform_run_confirmation_dialog (GtkWindow *parent_window,
                                         const gchar *message)
 {
        GtkWidget *dialog;
        gint response;
-
+       
        dialog = hildon_note_new_confirmation (parent_window, message);
-       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
+       check_modal_and_set_maybe (GTK_DIALOG(dialog));
 
        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-
+       on_destroy_dialog (GTK_DIALOG(dialog));
+       
        while (gtk_events_pending ())
                gtk_main_iteration ();
 
        return response;
 }
-
+       
 gint
 modest_platform_run_yes_no_dialog (GtkWindow *parent_window,
                                   const gchar *message)
 {
        GtkWidget *dialog;
        gint response;
-
+       
        dialog = hildon_note_new_confirmation_add_buttons (parent_window, message,
                                                           _("mcen_bd_yes"), GTK_RESPONSE_YES,
                                                           _("mcen_bd_no"), GTK_RESPONSE_NO,
                                                           NULL);
-       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
-
+       check_modal_and_set_maybe (GTK_DIALOG(dialog));
        response = gtk_dialog_run (GTK_DIALOG (dialog));
-
-       gtk_widget_destroy (GTK_WIDGET (dialog));
+       
+       on_destroy_dialog (GTK_DIALOG(dialog));
 
        while (gtk_events_pending ())
                gtk_main_iteration ();
@@ -906,20 +924,23 @@ modest_platform_run_yes_no_dialog (GtkWindow *parent_window,
        return response;
 }
 
+
+
 void
 modest_platform_run_information_dialog (GtkWindow *parent_window,
                                        const gchar *message)
 {
-       GtkWidget *dialog;
-
-       dialog = hildon_note_new_information (parent_window, message);
-
-       g_signal_connect_swapped (dialog,
+       GtkWidget *note;
+       
+       note = hildon_note_new_information (parent_window, message);
+       check_modal_and_set_maybe (GTK_DIALOG(note));
+       
+       g_signal_connect_swapped (note,
                                  "response", 
-                                 G_CALLBACK (gtk_widget_destroy),
-                                 dialog);
+                                 G_CALLBACK (on_destroy_dialog),
+                                 note);
 
-       gtk_widget_show_all (dialog);
+       gtk_widget_show_all (note);
 }
 
 
@@ -1002,13 +1023,9 @@ set_account_to_online (TnyAccount *account)
                 * a) They fundamentally need network access, so they can't really be offline.
                 * b) That might cause a transport connection to happen too early.
                 */
-               GError *error = NULL;
-               tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (account), TRUE, &error);
-               if (error) {
-                       g_warning ("%s: tny_camel_account_set_online() returned a GError:\n  %s\n", 
-                               __FUNCTION__, error->message);
-                       g_error_free (error);   
-               }
+
+               /* The last argument is user_data, the NULL before that is the callback */
+               tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (account), TRUE, NULL, NULL);
        }
        #endif
 }
@@ -1149,7 +1166,7 @@ modest_platform_run_sort_dialog (GtkWindow *parent_window,
 
        /* Build dialog */
        dialog = hildon_sort_dialog_new (parent_window);
-       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
+       check_modal_and_set_maybe (GTK_DIALOG(dialog));
        
        /* Fill sort keys */
        switch (type) {
@@ -1160,7 +1177,7 @@ modest_platform_run_sort_dialog (GtkWindow *parent_window,
        }
        
        /* Free */
-       gtk_widget_destroy (GTK_WIDGET (dialog));
+       on_destroy_dialog (GTK_DIALOG(dialog));
 }
 
 
@@ -1259,15 +1276,19 @@ void
 modest_platform_on_new_header_received (TnyHeader *header)
 {
 #ifdef MODEST_HAVE_HILDON_NOTIFY
-       HildonNotification *not;
+       HildonNotification *notification;
        gchar *url = NULL;
        TnyFolder *folder = NULL;
+       const gchar *subject;
 
-       /* Create a new notification. TODO: per-mail data needed */
-       not = hildon_notification_new (tny_header_get_from (header),
-                                      tny_header_get_subject (header),
-                                      "qgn_list_messagin_mail_unread",
-                                      NULL);
+       subject = tny_header_get_subject (header);
+       if (!subject || strlen(subject) == 0)
+               subject = _("mail_va_no_subject");
+       
+       notification = hildon_notification_new (tny_header_get_from (header),
+                                               subject,
+                                               "qgn_list_messagin",
+                                               NULL);
 
        folder = tny_header_get_folder (header);
        url = g_strdup_printf ("%s/%s", 
@@ -1275,7 +1296,7 @@ modest_platform_on_new_header_received (TnyHeader *header)
                               tny_header_get_uid (header));
        g_object_unref (folder);
 
-       hildon_notification_add_dbus_action(not,
+       hildon_notification_add_dbus_action(notification,
                                            "default",
                                            "Cancel",
                                            MODEST_DBUS_SERVICE,
@@ -1286,22 +1307,27 @@ modest_platform_on_new_header_received (TnyHeader *header)
                                            -1);
        g_free (url);
        
-       /* Play sound SR-SND-18 */
-       hildon_notification_set_sound
-               (not, "/usr/share/sounds/ui-new_email.wav");
-       notify_notification_set_hint_int32 (NOTIFY_NOTIFICATION (not), "dialog-type", 4);
-
+       /* Play sound if the user wants */
+       if (modest_conf_get_bool (modest_runtime_get_conf (), 
+                                 MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, 
+                                 NULL)) {
+               hildon_notification_set_sound (HILDON_NOTIFICATION(notification),
+                                              "/usr/share/sounds/ui-new_email.wav");
+       }
+       
        /* Set the led pattern */
-       notify_notification_set_hint_string(NOTIFY_NOTIFICATION (not), 
+       notify_notification_set_hint_int32 (NOTIFY_NOTIFICATION(notification),
+                                           "dialog-type", 4);
+       notify_notification_set_hint_string(NOTIFY_NOTIFICATION(notification), 
                                            "led-pattern", 
                                            "PatternCommunicationEmail");
 
        /* Notify. We need to do this in an idle because this function
           could be called from a thread */
-       if (!notify_notification_show (NOTIFY_NOTIFICATION (not), NULL))
+       if (!notify_notification_show (NOTIFY_NOTIFICATION(notification), NULL))
                g_error ("Failed to send notification");
-               
-       g_object_unref (not);
+       
+       g_object_unref (notification);
 #endif /*MODEST_HAVE_HILDON_NOTIFY*/
 }
 
@@ -1525,14 +1551,14 @@ modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
 static void
 on_cert_dialog_response (GtkDialog *dialog, gint response_id,  const gchar* cert)
 {
-       // handle ok/cancel in the normal way
-       if (response_id != GTK_RESPONSE_HELP)
-               gtk_dialog_response (dialog, response_id);
-       else {
-               // GTK_RESPONSE_HELP means we need to show the certificate
+       /* GTK_RESPONSE_HELP means we need to show the certificate */
+       if (response_id == GTK_RESPONSE_HELP) {
                GtkWidget *note;
                gchar *msg;
                
+               /* Do not close the dialog */
+               g_signal_stop_emission_by_name (dialog, "response");
+
                msg = g_strdup_printf (_("mcen_ni_view_unknown_certificate"), cert);    
                note = hildon_note_new_information (GTK_WINDOW(dialog), msg);
                gtk_dialog_run (GTK_DIALOG(note));
@@ -1550,7 +1576,7 @@ modest_platform_run_certificate_conformation_dialog (const gchar* server_name,
        GtkWindow *main_win =
                (GtkWindow*)modest_window_mgr_get_main_window (modest_runtime_get_window_mgr());
 
-       gchar *question = g_strdup_printf (_("mcen_mc_unknown_certificate"),
+       gchar *question = g_strdup_printf (_("mcen_nc_unknown_certificate"),
                                           server_name);
        
        note = hildon_note_new_confirmation_add_buttons  (
@@ -1561,11 +1587,14 @@ modest_platform_run_certificate_conformation_dialog (const gchar* server_name,
                _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL,
                NULL, NULL);
        
-       g_signal_connect (G_OBJECT(note), "response", G_CALLBACK(on_cert_dialog_response),
-                         (gpointer)certificate);
+       g_signal_connect (G_OBJECT(note), "response", 
+                         G_CALLBACK(on_cert_dialog_response),
+                         (gpointer) certificate);
+       
+       check_modal_and_set_maybe (GTK_DIALOG(note));
        response = gtk_dialog_run(GTK_DIALOG(note));
 
-       gtk_widget_destroy(GTK_WIDGET(note));
+       on_destroy_dialog (GTK_DIALOG(note));
        g_free (question);
        
        return response;
@@ -1574,7 +1603,8 @@ modest_platform_run_certificate_conformation_dialog (const gchar* server_name,
 
 
 gboolean
-modest_platform_run_alert_dialog (const gchar* prompt, gboolean is_question)
+modest_platform_run_alert_dialog (const gchar* prompt, 
+                                 gboolean is_question)
 {      
        ModestWindow *main_window = 
                modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
@@ -1587,15 +1617,16 @@ modest_platform_run_alert_dialog (const gchar* prompt, gboolean is_question)
                 * so we know what buttons to show. */
                GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_window), 
                                                                              prompt));
+               check_modal_and_set_maybe (GTK_DIALOG(dialog));
+               
                const int response = gtk_dialog_run (GTK_DIALOG (dialog));
                retval = (response == GTK_RESPONSE_YES) || (response == GTK_RESPONSE_OK);
                
-               gtk_widget_destroy (dialog);
-               
+               on_destroy_dialog (GTK_DIALOG(dialog));         
        } else {
                /* Just show the error text and use the default response: */
-               modest_maemo_show_information_note_and_forget(GTK_WINDOW (main_window), 
-                                                             prompt);
+               modest_platform_run_information_dialog (GTK_WINDOW (main_window), 
+                                                       prompt);
        }
        return retval;
 }