+
+typedef struct
+{
+ GMainLoop* loop;
+ TnyAccount *account;
+ gboolean is_online;
+ gint count_tries;
+} CheckAccountIdleData;
+
+#define NUMBER_OF_TRIES 10 /* Try approx every second, ten times. */
+
+static gboolean
+on_timeout_check_account_is_online(gpointer user_data)
+{
+ printf ("DEBUG: %s:\n", __FUNCTION__);
+ CheckAccountIdleData *data = (CheckAccountIdleData*)user_data;
+
+ if (!data) {
+ g_warning ("%s: data is NULL.\n", __FUNCTION__);
+ }
+
+ if (!(data->account)) {
+ g_warning ("%s: data->account is NULL.\n", __FUNCTION__);
+ }
+
+ if (data && data->account) {
+ printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (data->account));
+ }
+
+ gboolean stop_trying = FALSE;
+ if (data && data->account &&
+ /* We want to wait until TNY_CONNECTION_STATUS_INIT has changed to something else,
+ * after which the account is likely to be usable, or never likely to be usable soon: */
+ (tny_account_get_connection_status (data->account) != TNY_CONNECTION_STATUS_INIT) )
+ {
+ data->is_online = TRUE;
+
+ stop_trying = TRUE;
+ }
+ else {
+ /* Give up if we have tried too many times: */
+ if (data->count_tries >= NUMBER_OF_TRIES)
+ {
+ stop_trying = TRUE;
+ }
+ else {
+ /* Wait for another timeout: */
+ ++(data->count_tries);
+ }
+ }
+
+ if (stop_trying) {
+ /* Allow the function that requested this idle callback to continue: */
+ if (data->loop)
+ g_main_loop_quit (data->loop);
+
+ if (data->account)
+ g_object_unref (data->account);
+
+ return FALSE; /* Don't call this again. */
+ } else {
+ return TRUE; /* Call this timeout callback again. */
+ }
+}
+
+/* Return TRUE immediately if the account is already online,
+ * otherwise check every second for NUMBER_OF_TRIES seconds and return TRUE as
+ * soon as the account is online, or FALSE if the account does
+ * not become online in the NUMBER_OF_TRIES seconds.
+ * This is useful when the D-Bus method was run immediately after
+ * the application was started (when using D-Bus activation),
+ * because the account usually takes a short time to go online.
+ * The return value is maybe not very useful.
+ */
+gboolean
+modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
+{
+ g_return_val_if_fail (account, FALSE);
+
+ printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
+
+ if (!tny_device_is_online (modest_runtime_get_device())) {
+ printf ("DEBUG: %s: device is offline.\n", __FUNCTION__);
+ return FALSE;
+ }
+
+ /* The local_folders account never seems to leave TNY_CONNECTION_STATUS_INIT,
+ * so we avoid wait unnecessarily: */
+ if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
+ !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account) ) {
+ return TRUE;
+ }
+
+ printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n",
+ __FUNCTION__, tny_account_get_connection_status (account));
+
+ /* The POP & IMAP store accounts seem to be TNY_CONNECTION_STATUS_DISCONNECTED,
+ * and that seems to be an OK time to use them. Maybe it's just TNY_CONNECTION_STATUS_INIT that
+ * we want to avoid. */
+ if (tny_account_get_connection_status (account) != TNY_CONNECTION_STATUS_INIT)
+ return TRUE;
+
+ /* This blocks on the result: */
+ CheckAccountIdleData *data = g_slice_new0 (CheckAccountIdleData);
+ data->is_online = FALSE;
+ data->account = account;
+ g_object_ref (data->account);
+ data->count_tries = 0;
+
+ GMainContext *context = NULL; /* g_main_context_new (); */
+ data->loop = g_main_loop_new (context, FALSE /* not running */);
+
+ g_timeout_add (1000, on_timeout_check_account_is_online, data);
+
+ /* This main loop will run until the idle handler has stopped it: */
+ g_main_loop_run (data->loop);
+
+ g_main_loop_unref (data->loop);
+ /* g_main_context_unref (context); */
+
+ g_slice_free (CheckAccountIdleData, data);
+
+ return data->is_online;
+}
+
+
+
+static void
+on_cert_dialog_response (GtkDialog *dialog, gint response_id, const gchar* cert)
+{
+ /* 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));
+ gtk_widget_destroy (note);
+ }
+}
+
+
+gboolean
+modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
+ const gchar *certificate)
+{
+ GtkWidget *note;
+ gint response;
+ GtkWindow *main_win =
+ (GtkWindow*)modest_window_mgr_get_main_window (modest_runtime_get_window_mgr());
+
+ gchar *question = g_strdup_printf (_("mcen_nc_unknown_certificate"),
+ server_name);
+
+ note = hildon_note_new_confirmation_add_buttons (
+ main_win,
+ question,
+ _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK,
+ _("mcen_bd_view"), GTK_RESPONSE_HELP, /* abusing this... */
+ _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL,
+ NULL, NULL);
+
+ g_signal_connect (G_OBJECT(note), "response",
+ G_CALLBACK(on_cert_dialog_response),
+ (gpointer) certificate);
+
+ modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
+ GTK_WINDOW (note));
+ response = gtk_dialog_run(GTK_DIALOG(note));
+
+ on_destroy_dialog (GTK_DIALOG(note));
+ g_free (question);
+
+ return response == GTK_RESPONSE_OK;
+}
+
+
+
+gboolean
+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 ());
+
+ gboolean retval = TRUE;
+ if (is_question) {
+ /* The Tinymail documentation says that we should show Yes and No buttons,
+ * when it is a question.
+ * Obviously, we need tinymail to use more specific error codes instead,
+ * so we know what buttons to show. */
+ GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_window),
+ prompt));
+ modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
+ GTK_WINDOW (dialog));
+
+ const int response = gtk_dialog_run (GTK_DIALOG (dialog));
+ retval = (response == GTK_RESPONSE_YES) || (response == GTK_RESPONSE_OK);
+
+ on_destroy_dialog (GTK_DIALOG(dialog));
+ } else {
+ /* Just show the error text and use the default response: */
+ modest_platform_run_information_dialog (GTK_WINDOW (main_window),
+ prompt);
+ }
+ return retval;
+}