2007-08-27 Armin Burgmeier <armin@openismus.com>
authorArmin Burgmeier <armin@openismus.com>
Mon, 27 Aug 2007 13:40:24 +0000 (13:40 +0000)
committerArmin Burgmeier <armin@openismus.com>
Mon, 27 Aug 2007 13:40:24 +0000 (13:40 +0000)
* src/modest-tny-send-queue.c: Implement TnyFolderObserver and
observe outbox for added messages to assign them a status. Previously,
this was done in modest_tny_send_queue_add(), but the message id might
not yet be known at that point. This reduces warnings when sending
mail, though I am not sure how this even worked before.

pmo-trunk-r3093

ChangeLog2
src/modest-tny-send-queue.c

index ead5f93..33f40c4 100644 (file)
@@ -1,3 +1,11 @@
+2007-08-27  Armin Burgmeier  <armin@openismus.com>
+
+       * src/modest-tny-send-queue.c: Implement TnyFolderObserver and
+       observe outbox for added messages to assign them a status. Previously,
+       this was done in modest_tny_send_queue_add(), but the message id might
+       not yet be known at that point. This reduces warnings when sending
+       mail, though I am not sure how this even worked before.
+
 2007-08-27  Murray Cumming  <murrayc@murrayc.com>
 
        * src/maemo/modest-account-settings-dialog.c:
index eb4c1c3..25afcc3 100644 (file)
@@ -33,6 +33,8 @@
 #include <tny-iterator.h>
 #include <tny-folder.h>
 #include <tny-camel-msg.h>
+#include <tny-folder-change.h>
+#include <tny-folder-observer.h>
 #include <modest-tny-account.h>
 #include <modest-runtime.h>
 #include <modest-platform.h>
@@ -172,49 +174,13 @@ modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
 static void
 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
 {
-       ModestTnySendQueuePrivate *priv;
-       TnyHeader *header = NULL;
-       SendInfo *info = NULL;
-       GList* existing = NULL;
-       const gchar* msg_id = NULL;
-
        g_return_if_fail (TNY_IS_SEND_QUEUE(self));
        g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
 
-       priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
-       header = tny_msg_get_header (msg);
-       
-       /* Note that this call actually sets the message id to something
-        * sensible. */ 
        TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
 
-       /* Check whether the mail is already in the queue */
-/*     msg_id = tny_header_get_message_id (header); */
-       msg_id = tny_header_get_uid (header);   
-       g_return_if_fail (msg_id != NULL);
-       existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
-       if(existing != NULL)
-       {
-               //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
-               //        info->status == MODEST_TNY_SEND_QUEUE_FAILED);
-
-               info = existing->data;
-               info->status = MODEST_TNY_SEND_QUEUE_WAITING;
-       }
-       else
-       {
-               
-               info = g_slice_new (SendInfo);
-
-               info->msg_id = strdup(msg_id);
-               info->status = MODEST_TNY_SEND_QUEUE_WAITING;
-               g_queue_push_tail (priv->queue, info);
-       }
-
-       g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
-
-       /* free */
-       g_object_unref (header);
+       /* We cannot access the uid of the message here because it might 
+        * just have been created. Wait for it being added to outbox. */
 }
 
 static void
@@ -313,6 +279,61 @@ modest_tny_send_queue_get_outbox (TnySendQueue *self)
        return folder;
 }
 
+static void
+modest_tny_send_queue_update(TnyFolderObserver *self,
+                             TnyFolderChange *change)
+{
+       ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
+       TnyHeader *header = NULL;
+       TnyMsg *msg = tny_folder_change_get_received_msg(change);
+       SendInfo *info = NULL;
+       GList* existing = NULL;
+       const gchar* msg_id = NULL;
+       TnyFolder* outbox = NULL;
+
+       if(msg)
+       {
+               outbox = tny_send_queue_get_outbox(TNY_SEND_QUEUE(self));
+               header = tny_msg_get_header(msg);
+
+               msg_id = tny_header_get_message_id (header);
+               g_assert(msg_id != NULL);
+
+               /* Put newly added message in WAITING state */
+               existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
+               if(existing != NULL)
+               {
+                       //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
+                       //        info->status == MODEST_TNY_SEND_QUEUE_FAILED);
+
+                       info = existing->data;
+                       info->status = MODEST_TNY_SEND_QUEUE_WAITING;
+               }
+               else
+               {
+                       
+                       info = g_slice_new (SendInfo);
+
+                       info->msg_id = strdup(msg_id);
+                       info->status = MODEST_TNY_SEND_QUEUE_WAITING;
+                       g_queue_push_tail (priv->queue, info);
+               }
+
+               g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
+
+               g_object_unref(G_OBJECT(msg));
+               g_object_unref(G_OBJECT(header));
+               g_object_unref(G_OBJECT(outbox));
+       }
+}
+
+static void
+modest_tny_send_queue_folder_observer_init(gpointer g, gpointer iface_data)
+{
+       TnyFolderObserverIface *klass = (TnyFolderObserverIface *)g;
+       klass->update_func = modest_tny_send_queue_update;
+}
+
 GType
 modest_tny_send_queue_get_type (void)
 {
@@ -331,9 +352,19 @@ modest_tny_send_queue_get_type (void)
                        (GInstanceInitFunc) modest_tny_send_queue_instance_init,
                        NULL
                };
+               
+               static const GInterfaceInfo tny_folder_observer_info = {
+                       (GInterfaceInitFunc) modest_tny_send_queue_folder_observer_init,
+                       NULL,
+                       NULL
+               };
+               
                my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
                                                  "ModestTnySendQueue",
                                                  &my_info, 0);
+
+               g_type_add_interface_static(my_type, TNY_TYPE_FOLDER_OBSERVER,
+                       &tny_folder_observer_info);
        }
        return my_type;
 }
@@ -387,6 +418,8 @@ modest_tny_send_queue_finalize (GObject *obj)
        g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
        g_queue_free (priv->queue);
 
+       printf("Finalize called\n");
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -402,6 +435,10 @@ modest_tny_send_queue_new (TnyCamelTransportAccount *account)
        tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
                                                    account); 
 
+       /* TODO: Does this produce cyclic references? */
+       tny_folder_add_observer(modest_tny_send_queue_get_outbox(TNY_SEND_QUEUE(self)),
+                               TNY_FOLDER_OBSERVER(self));
+
        /* Connect signals to control when a msg is being or has been sent */
        g_signal_connect (G_OBJECT(self), "msg-sending",
                          G_CALLBACK(_on_msg_start_sending),