+ /* Keep in queue so that we remember that the opertion has failed */
+ /* and was not just cancelled */
+ if (err->code == TNY_SYSTEM_ERROR_CANCEL) {
+ info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
+ } else {
+ info->status = MODEST_TNY_SEND_QUEUE_FAILED;
+ }
+ priv->current = NULL;
+
+ /* Notify status has changed */
+ g_signal_emit (self, signals[STATUS_CHANGED_SIGNAL], 0, info->msg_id, info->status);
+
+ /* free */
+ g_free(msg_uid);
+ }
+}
+
+static void
+_on_queue_start (TnySendQueue *self,
+ gpointer data)
+{
+ ModestTnySendQueuePrivate *priv;
+ ModestMailOperation *mail_op;
+
+ mail_op = modest_mail_operation_new (NULL);
+ modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
+ mail_op);
+ modest_mail_operation_run_queue (mail_op, MODEST_TNY_SEND_QUEUE (self));
+ g_object_unref (mail_op);
+
+ priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
+ priv->sending = TRUE;
+}
+
+static void
+on_queue_stop_get_headers_async_cb (TnyFolder *folder,
+ gboolean cancelled,
+ TnyList *headers,
+ GError *err,
+ gpointer user_data)
+{
+ ModestTnySendQueue *self = (ModestTnySendQueue *) user_data;
+ TnyIterator *iter;
+
+ if (cancelled || err)
+ goto end;
+
+ /* Update the info about headers */
+ iter = tny_list_create_iterator (headers);
+ while (!tny_iterator_is_done (iter)) {
+ TnyHeader *header;
+
+ header = (TnyHeader *) tny_iterator_get_current (iter);
+ if (header) {
+ gchar *msg_id = NULL;
+ GList *item = NULL;
+
+ /* Get message uid */
+ msg_id = modest_tny_send_queue_get_msg_id (header);
+ if (msg_id)
+ item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
+ else
+ g_warning ("%s: could not get msg-id for header", __FUNCTION__);
+
+ if (item) {
+ SendInfo *info;
+ /* Set current status item */
+ info = item->data;
+ if (tny_header_get_flags (header) & TNY_HEADER_FLAG_SUSPENDED) {
+ info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
+ g_signal_emit (self, signals[STATUS_CHANGED_SIGNAL], 0,
+ info->msg_id, info->status);
+ }
+ } else {
+ g_warning ("%s: could not find item with id '%s'", __FUNCTION__, msg_id);
+ }
+ g_object_unref (header);
+ }
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+
+ end:
+ /* Unrefs */
+ g_object_unref (headers);
+ g_object_unref (self);
+}
+
+static void
+_on_queue_stop (TnySendQueue *self,
+ gpointer data)
+{
+ ModestTnySendQueuePrivate *priv;
+
+ priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
+ priv->sending = FALSE;
+
+ if (priv->suspend) {
+ TnyList *headers;
+ priv->suspend = FALSE;
+
+ /* Update the state of messages in the queue */
+ headers = tny_simple_list_new ();
+ tny_folder_get_headers_async (priv->outbox, headers, TRUE,
+ on_queue_stop_get_headers_async_cb,
+ NULL, g_object_ref (self));
+ }
+}
+
+static void
+fill_list_of_caches (gpointer key, gpointer value, gpointer userdata)
+{
+ GSList **send_queues = (GSList **) userdata;
+ *send_queues = g_slist_prepend (*send_queues, value);
+}
+
+/* This function shouldn't be here. Move it to another place. Sergio */
+ModestTnySendQueueStatus
+modest_tny_all_send_queues_get_msg_status (TnyHeader *header)
+{
+ ModestCacheMgr *cache_mgr = NULL;
+ GHashTable *send_queue_cache = NULL;
+ ModestTnyAccountStore *accounts_store = NULL;
+ TnyList *accounts = NULL;
+ TnyIterator *iter = NULL;
+ TnyTransportAccount *account = NULL;
+ GSList *send_queues = NULL, *node;
+ /* get_msg_status returns suspended by default, so we want to detect changes */
+ ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
+ ModestTnySendQueueStatus queue_status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
+ gchar *msg_uid = NULL;
+ ModestTnySendQueue *send_queue = NULL;
+
+ g_return_val_if_fail (TNY_IS_HEADER(header), MODEST_TNY_SEND_QUEUE_UNKNOWN);
+
+ msg_uid = modest_tny_send_queue_get_msg_id (header);
+ cache_mgr = modest_runtime_get_cache_mgr ();
+ send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
+ MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
+
+ g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
+ if (send_queues == NULL) {
+ accounts = tny_simple_list_new ();
+ accounts_store = modest_runtime_get_account_store ();
+ tny_account_store_get_accounts (TNY_ACCOUNT_STORE(accounts_store),
+ accounts,
+ TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
+
+ iter = tny_list_create_iterator (accounts);
+ while (!tny_iterator_is_done (iter)) {
+ account = TNY_TRANSPORT_ACCOUNT(tny_iterator_get_current (iter));
+ send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account), TRUE);
+ g_object_unref(account);
+ if (TNY_IS_SEND_QUEUE (send_queue)) {
+ queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
+ if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
+ status = queue_status;
+ break;
+ }
+ }
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+ g_object_unref (accounts);
+ }
+ else {
+ for (node = send_queues; node != NULL; node = g_slist_next (node)) {
+ send_queue = MODEST_TNY_SEND_QUEUE (node->data);
+
+ queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
+ if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
+ status = queue_status;
+ break;
+ }
+ }
+ }