+
+typedef struct {
+ ModestTnyAccountStore *account_store;
+ ModestTnyAccountStoreShutdownCallback callback;
+ gpointer userdata;
+ gint pending;
+} ShutdownOpData;
+
+static void
+account_shutdown_callback (TnyCamelAccount *account, gboolean canceled, GError *err, gpointer userdata)
+{
+ ShutdownOpData *op_data = (ShutdownOpData *) userdata;
+ op_data->pending--;
+ if (op_data->pending == 0) {
+ if (op_data->callback)
+ op_data->callback (op_data->account_store, op_data->userdata);
+ g_object_unref (op_data->account_store);
+ g_free (op_data);
+ } else {
+ g_object_unref (op_data->account_store);
+ }
+}
+
+static void
+account_shutdown (TnyAccount *account, ShutdownOpData *op_data)
+{
+ g_return_if_fail (account && TNY_IS_ACCOUNT(account));
+
+ if (TNY_IS_STORE_ACCOUNT (account) &&
+ !modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
+ goto frees;
+
+ /* Disconnect account */
+ if (tny_account_get_connection_status (account) == TNY_CONNECTION_STATUS_CONNECTED) {
+ tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(account), FALSE,
+ account_shutdown_callback, op_data);
+ return;
+ }
+
+ frees:
+ op_data->pending--;
+ g_object_unref (op_data->account_store);
+}
+
+
+void
+modest_tny_account_store_shutdown (ModestTnyAccountStore *self,
+ ModestTnyAccountStoreShutdownCallback callback,
+ gpointer userdata)
+{
+ gint i, num_accounts;
+ ShutdownOpData *op_data;
+ ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
+
+ /* Get references */
+ num_accounts = 0;
+ if (priv->store_accounts)
+ num_accounts += tny_list_get_length (priv->store_accounts);
+ if (priv->transport_accounts)
+ num_accounts += tny_list_get_length (priv->transport_accounts);
+
+ for (i = 0 ; i < num_accounts ; i++)
+ g_object_ref (self);
+
+ /* Create the helper object */
+ op_data = g_new0 (ShutdownOpData, 1);
+ op_data->callback = callback;
+ op_data->userdata = userdata;
+ op_data->pending = num_accounts;
+ op_data->account_store = self;
+
+ /* Destroy all accounts. Disconnect all accounts before they are destroyed */
+ if (priv->store_accounts) {
+ tny_list_foreach (priv->store_accounts, (GFunc)account_shutdown, op_data);
+ }
+
+ if (priv->transport_accounts) {
+ tny_list_foreach (priv->transport_accounts, (GFunc)account_shutdown, op_data);
+ }
+
+ if (op_data->pending == 0) {
+ if (op_data->callback)
+ op_data->callback (op_data->account_store, op_data->userdata);
+ g_free (op_data);
+ }
+}
+
+gboolean
+modest_tny_account_store_is_send_mail_blocked (ModestTnyAccountStore *self)
+{
+ ModestTnyAccountStorePrivate *priv;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ return priv->send_mail_blocked;
+}
+
+void
+modest_tny_account_store_set_send_mail_blocked (ModestTnyAccountStore *self,
+ gboolean blocked)
+{
+ ModestTnyAccountStorePrivate *priv;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ priv->send_mail_blocked = blocked;
+}
+
+static void
+count_remote_accounts (gpointer data, gpointer user_data)
+{
+ TnyFolderStore *account = TNY_FOLDER_STORE (data);
+ gint *count = (gint *) user_data;
+
+ if (modest_tny_folder_store_is_remote (account))
+ (*count)++;
+}
+
+guint
+modest_tny_account_store_get_num_remote_accounts (ModestTnyAccountStore *self)
+{
+ ModestTnyAccountStorePrivate *priv;
+ gint count = 0;
+
+ g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), 0);
+
+ /* Count remote accounts */
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
+ tny_list_foreach (priv->store_accounts, (GFunc) count_remote_accounts, &count);
+
+ return count;
+}
+
+static void
+init_send_queue (TnyAccount *account, gpointer user_data)
+{
+ modest_runtime_get_send_queue ((TnyTransportAccount *) account, TRUE);
+}
+
+void
+modest_tny_account_store_start_send_queues (ModestTnyAccountStore *self)
+{
+ ModestTnyAccountStorePrivate *priv;
+ TnyList *tmp;
+
+ g_return_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self));
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
+
+ /* We need to create a copy of the list because if the send
+ queues are created they'll directly access to the TnyList
+ of transport accounts, and thus we'll end up blocked in the
+ mutex the TnyList uses to synchronize accesses */
+ tmp = tny_list_copy (priv->transport_accounts);
+
+ /* Just instantiate them. They'll begin to listen for
+ connection changes to send messages ASAP */
+ tny_list_foreach (tmp, (GFunc) init_send_queue, NULL);
+ g_object_unref (tmp);
+}
+
+
+gboolean
+modest_tny_account_store_check_disk_full_error (ModestTnyAccountStore *self,
+ GtkWidget *parent_window,
+ GError *err,
+ TnyAccount *account,
+ const gchar *alternate)
+{
+ if (err == NULL)
+ return FALSE;
+
+ if (modest_tny_account_store_is_disk_full_error (self, err, account)) {
+ gboolean is_mcc = modest_tny_account_is_memory_card_account (account);
+ if (is_mcc && alternate) {
+ modest_platform_information_banner (parent_window, NULL, alternate);
+ } else {
+ gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+ modest_platform_information_banner (parent_window, NULL, msg);
+ g_free (msg);
+ }
+ } else if (err->code == TNY_SYSTEM_ERROR_MEMORY)
+ /* If the account was created in memory full
+ conditions then tinymail won't be able to
+ connect so it'll return this error code */
+ modest_platform_information_banner (parent_window,
+ NULL, _("emev_ui_imap_inbox_select_error"));
+ else
+ return FALSE;
+
+ return TRUE;
+}
+
+gboolean
+modest_tny_account_store_is_disk_full_error (ModestTnyAccountStore *self,
+ GError *error,
+ TnyAccount *account)
+{
+ gboolean enough_free_space = TRUE;
+ GnomeVFSURI *cache_dir_uri;
+ const gchar *cache_dir = NULL;
+ GnomeVFSFileSize free_space;
+
+ /* Cache dir is different in case we're using an external storage (like MMC account) */
+ if (account && modest_tny_account_is_memory_card_account (account))
+ cache_dir = g_getenv (MODEST_MMC1_VOLUMEPATH_ENV);
+
+ /* Get the default local cache dir */
+ if (!cache_dir)
+ cache_dir = tny_account_store_get_cache_dir ((TnyAccountStore *) self);
+
+ cache_dir_uri = gnome_vfs_uri_new (cache_dir);
+ if (cache_dir_uri) {
+ if (gnome_vfs_get_volume_free_space (cache_dir_uri, &free_space) == GNOME_VFS_OK) {
+ if (free_space < MODEST_TNY_ACCOUNT_STORE_MIN_FREE_SPACE)
+ enough_free_space = FALSE;
+ }
+ gnome_vfs_uri_unref (cache_dir_uri);
+ }
+
+ if ((error->code == TNY_SYSTEM_ERROR_MEMORY ||
+ /* When asking for a mail and no space left on device
+ tinymail returns this error */
+ error->code == TNY_SERVICE_ERROR_MESSAGE_NOT_AVAILABLE ||
+ /* When the folder summary could not be read or
+ written */
+ error->code == TNY_IO_ERROR_WRITE ||
+ error->code == TNY_IO_ERROR_READ) &&
+ !enough_free_space) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}