+ UtilIdleData *data = g_slice_new0 (UtilIdleData);
+
+ GMainContext *context = NULL; /* g_main_context_new (); */
+ data->loop = g_main_loop_new (context, FALSE /* not running */);
+
+ /* Cause the function to be run in an idle-handler, which is always
+ * in the main thread:
+ */
+ if (!connect_request_in_progress) {
+ printf ("DEBUG: %s: First request\n", __FUNCTION__);
+ connect_request_in_progress = TRUE;
+ g_idle_add (&on_idle_connect_and_wait, data);
+ }
+ else {
+ printf ("DEBUG: %s: nth request\n", __FUNCTION__);
+ g_idle_add_full (G_PRIORITY_LOW, &on_idle_wait_for_previous_connect_to_finish, data, NULL);
+ }
+
+ /* This main loop will run until the idle handler has stopped it: */
+ printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
+ GDK_THREADS_LEAVE();
+ g_main_loop_run (data->loop);
+ GDK_THREADS_ENTER();
+ printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
+ connect_request_in_progress = FALSE;
+ printf ("DEBUG: %s: Finished\n", __FUNCTION__);
+ g_main_loop_unref (data->loop);
+ /* g_main_context_unref (context); */
+
+ g_slice_free (UtilIdleData, data);
+
+ gboolean result = tny_device_is_online (device);
+
+ if (result)
+ set_account_to_online (account);
+
+ return result;
+}
+
+gboolean modest_platform_connect_and_wait_if_network_account (GtkWindow *parent_window, TnyAccount *account)
+{
+ if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
+ if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
+ !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
+ /* This must be a maildir account, which does not require a connection: */
+ return TRUE;
+ }
+ }
+
+ return modest_platform_connect_and_wait (parent_window, account);
+}
+
+gboolean modest_platform_connect_and_wait_if_network_folderstore (GtkWindow *parent_window, TnyFolderStore *folder_store)
+{
+ if (!folder_store)
+ return TRUE; /* Maybe it is something local. */
+
+ gboolean result = TRUE;
+ if (TNY_IS_FOLDER (folder_store)) {
+ /* Get the folder's parent account: */
+ TnyAccount *account = tny_folder_get_account(TNY_FOLDER (folder_store));
+ if (account != NULL) {
+ result = modest_platform_connect_and_wait_if_network_account (NULL, account);
+ g_object_unref (account);
+ }
+ } else if (TNY_IS_ACCOUNT (folder_store)) {
+ /* Use the folder store as an account: */
+ result = modest_platform_connect_and_wait_if_network_account (NULL, TNY_ACCOUNT (folder_store));
+ }
+
+ return result;