Work to fix bug NB#81989:
[modest] / src / modest-tny-send-queue.c
index 1139eba..fbeb4c7 100644 (file)
@@ -70,6 +70,15 @@ static void _on_msg_error_happened (TnySendQueue *self,
                                    GError *err, 
                                    gpointer user_data);
 
+static void _on_queue_start        (TnySendQueue *self, 
+                                   gpointer user_data);
+
+static void modest_tny_send_queue_add_async (TnySendQueue *self, 
+                                            TnyMsg *msg, 
+                                            TnySendQueueAddCallback callback, 
+                                            TnyStatusCallback status_callback, 
+                                            gpointer user_data);
+
 static TnyFolder* modest_tny_send_queue_get_outbox  (TnySendQueue *self);
 static TnyFolder* modest_tny_send_queue_get_sentbox (TnySendQueue *self);
 
@@ -96,6 +105,9 @@ struct _ModestTnySendQueuePrivate {
        /* Special folders */
        TnyFolder *outbox;
        TnyFolder *sentbox;
+
+       /* last was send receive operation?*/
+       gboolean requested_send_receive;
 };
 
 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -181,7 +193,10 @@ modest_tny_send_queue_to_string (ModestTnySendQueue *self)
 }
 
 static void
-_on_added_to_outbox (TnySendQueue *self, gboolean cancelled, TnyMsg *msg, GError *err,
+_on_added_to_outbox (TnySendQueue *self, 
+                    gboolean cancelled, 
+                    TnyMsg *msg, 
+                    GError *err,
                     gpointer user_data) 
 {
        ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
@@ -195,7 +210,6 @@ _on_added_to_outbox (TnySendQueue *self, gboolean cancelled, TnyMsg *msg, GError
 
        header = tny_msg_get_header (msg);
        msg_id = modest_tny_send_queue_get_msg_id (header);
-/*     msg_id = tny_header_get_message_id (header); */
        g_return_if_fail(msg_id != NULL);
 
        /* Put newly added message in WAITING state */
@@ -215,20 +229,6 @@ _on_added_to_outbox (TnySendQueue *self, gboolean cancelled, TnyMsg *msg, GError
        g_object_unref(G_OBJECT(header));
 }
 
-void
-modest_tny_send_queue_add (ModestTnySendQueue *self, TnyMsg *msg, GError **err)
-{
-       g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE(self));
-       g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
-
-       tny_camel_send_queue_add_async (TNY_CAMEL_SEND_QUEUE(self), 
-                                       msg, 
-                                       _on_added_to_outbox, 
-                                       NULL, 
-                                       NULL);
-}
-
-
 static void
 _add_message (ModestTnySendQueue *self, TnyHeader *header)
 {
@@ -277,6 +277,18 @@ _add_message (ModestTnySendQueue *self, TnyHeader *header)
        g_free(msg_uid);
 }
 
+static void 
+modest_tny_send_queue_add_async (TnySendQueue *self, 
+                                TnyMsg *msg, 
+                                TnySendQueueAddCallback callback, 
+                                TnyStatusCallback status_callback, 
+                                gpointer user_data)
+{
+       /* Call the superclass passing our own callback */
+       TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_async (self, msg, _on_added_to_outbox, NULL, NULL);
+}
+
+
 static TnyFolder*
 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
 {
@@ -339,8 +351,9 @@ modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
        parent_class            = g_type_class_peek_parent (klass);
        gobject_class->finalize = modest_tny_send_queue_finalize;
 
-       TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
-        TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
+       TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_async   = modest_tny_send_queue_add_async;
+       TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox  = modest_tny_send_queue_get_outbox;
+        TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox = modest_tny_send_queue_get_sentbox;
        klass->status_changed   = NULL;
 
        signals[STATUS_CHANGED_SIGNAL] =
@@ -403,6 +416,9 @@ modest_tny_send_queue_new (TnyCamelTransportAccount *account)
        g_signal_connect (G_OBJECT(self), "error-happened",
                          G_CALLBACK(_on_msg_error_happened),
                          NULL);
+       g_signal_connect (G_OBJECT (self), "queue-start",
+                         G_CALLBACK (_on_queue_start),
+                         NULL);
 
        /* Set outbox and sentbox */
        priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
@@ -411,6 +427,8 @@ modest_tny_send_queue_new (TnyCamelTransportAccount *account)
        priv->sentbox = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
                                                               TNY_FOLDER_TYPE_SENT);
 
+       priv->requested_send_receive = FALSE;
+
 
        headers = tny_simple_list_new ();       
        tny_folder_get_headers (priv->outbox, headers, TRUE, NULL);
@@ -429,8 +447,7 @@ modest_tny_send_queue_new (TnyCamelTransportAccount *account)
        modest_tny_send_queue_wakeup (self);
 
        /* Frees */
-       g_object_unref (G_OBJECT (headers));
-       g_object_unref (G_OBJECT (priv->outbox));
+       g_object_unref (headers);
        g_object_unref (iter);
 
        /* Do this at the end, because it'll call tny_send_queue_flush
@@ -501,12 +518,8 @@ modest_tny_send_queue_get_msg_id (TnyHeader *header)
 
 
 static void
-_on_msg_start_sending (TnySendQueue *self,
-                      TnyHeader *header,
-                      TnyMsg *msg,
-                      int done, 
-                      int total,
-                      gpointer user_data)
+_on_msg_start_sending (TnySendQueue *self, TnyHeader *header,
+                      TnyMsg *msg, int done, int total, gpointer user_data)
 {
        ModestTnySendQueuePrivate *priv = NULL;
        GList *item = NULL;
@@ -517,16 +530,20 @@ _on_msg_start_sending (TnySendQueue *self,
        
        /* Get message uid */
        msg_id = modest_tny_send_queue_get_msg_id (header);
-
-       /* Get status info */
-       item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
-
-       /* Set current status item */
-       info = item->data;
-       info->status = MODEST_TNY_SEND_QUEUE_SENDING;
-       g_signal_emit (self, signals[STATUS_CHANGED_SIGNAL], 0, info->msg_id, info->status);
-       priv->current = item;
-
+       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) {
+               /* Set current status item */
+               info = item->data;
+               info->status = MODEST_TNY_SEND_QUEUE_SENDING;
+               g_signal_emit (self, signals[STATUS_CHANGED_SIGNAL], 0, info->msg_id, info->status);
+               priv->current = item;
+       } else
+               g_warning ("%s: could not find item with id '%s'", __FUNCTION__, msg_id);
+       
        /* free */
        g_free (msg_id);
 }
@@ -543,9 +560,6 @@ _on_msg_has_been_sent (TnySendQueue *self,
        gchar *msg_id = NULL;
        GList *item;
 
-       /* TODO: Sometimes tinymail does not return a header, need to check */
-       g_return_if_fail (TNY_IS_HEADER (header));
-
        priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
 
        /* Get message uid */
@@ -555,13 +569,20 @@ _on_msg_has_been_sent (TnySendQueue *self,
 
        /* Get status info */
        item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
+
+
+       /* TODO: note that item=NULL must not happen, but I found that
+          tinymail is issuing the message-sent signal twice, because
+          tny_camel_send_queue_update is called twice for each
+          message sent. This must be fixed in tinymail. Sergio */
+       if (item) {
+               /* Remove status info */
+               modest_tny_send_queue_info_free (item->data);
+               g_queue_delete_link (priv->queue, item);
+               priv->current = NULL;
                
-       /* Remove status info */
-       modest_tny_send_queue_info_free (item->data);
-       g_queue_delete_link (priv->queue, item);
-       priv->current = NULL;
-               
-       modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));     
+               modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
+       }
 
        /* free */
        g_free(msg_id);
@@ -575,31 +596,52 @@ _on_msg_error_happened (TnySendQueue *self,
                        gpointer user_data)
 {
        ModestTnySendQueuePrivate *priv = NULL;
-       SendInfo *info = NULL;
-       GList *item = NULL;
-       gchar* msg_uid = NULL;
        
        priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
-       
-       /* Get sending info (create new if it doesn not exist) */
-       msg_uid = modest_tny_send_queue_get_msg_id (header);
-       item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), 
-                                                 msg_uid);     
-       info = item->data;
-
-       /* 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);
+       /* Note that header could be NULL. Tinymail notifies about
+          generic send queue errors with this signal as well, and
+          those notifications are not bound to any particular header
+          or message */
+       if (header) {
+               SendInfo *info = NULL;
+               GList *item = NULL;
+               gchar* msg_uid = NULL;
+
+               /* Get sending info (create new if it doesn not exist) */
+               msg_uid = modest_tny_send_queue_get_msg_id (header);
+               item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), 
+                                                         msg_uid);     
+               
+               info = item->data;
+
+               /* 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)
+{
+       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);
 }
 
 static void 
@@ -717,7 +759,32 @@ modest_tny_send_queue_wakeup (ModestTnySendQueue *self)
                tny_iterator_next (iter);
        }
 
+       /* Make changes persistent on disk */
+       tny_folder_sync_async (priv->outbox, FALSE, NULL, NULL, NULL);
+
        /* Frees */
        g_object_unref (iter);
        g_object_unref (G_OBJECT (headers));
 }
+
+gboolean 
+modest_tny_send_queue_get_requested_send_receive (ModestTnySendQueue *self)
+{
+       ModestTnySendQueuePrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (self), FALSE);
+       priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
+
+       return priv->requested_send_receive;
+}
+
+void 
+modest_tny_send_queue_set_requested_send_receive (ModestTnySendQueue *self, gboolean requested_send_receive)
+{
+       ModestTnySendQueuePrivate *priv;
+
+       g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE (self));
+       priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
+
+       priv->requested_send_receive = requested_send_receive;
+}