* fix for NB#67837, NB#63633
[modest] / src / modest-tny-send-queue.c
index 25afcc3..aa30899 100644 (file)
@@ -96,6 +96,8 @@ static guint signals[LAST_SIGNAL] = {0};
 static int
 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
 {
+       g_return_val_if_fail (info && ((SendInfo*)info)->msg_id && msg_id, -1);
+       
        return strcmp( ((SendInfo*)info)->msg_id, msg_id);
 }
 
@@ -172,17 +174,61 @@ modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
 }
 
 static void
-modest_tny_send_queue_add (TnySendQueue *self, 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);
+       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));
 
-       TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
+       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);
 
-       /* We cannot access the uid of the message here because it might 
-        * just have been created. Wait for it being added to outbox. */
+       /* 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(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_send_queue_add_async (TNY_SEND_QUEUE(self), 
+                                 msg, 
+                                 _on_added_to_outbox, 
+                                 NULL, 
+                                 NULL);
 }
 
+
 static void
 _add_message (ModestTnySendQueue *self, TnyHeader *header)
 {
@@ -210,12 +256,14 @@ _add_message (ModestTnySendQueue *self, TnyHeader *header)
                
                /* Check if it already exists on queue */
                existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
-               if(existing != NULL) return;
-       
+               if(existing != NULL)
+                       break;
+               
                /* Check if its being edited */
                mgr = modest_runtime_get_window_mgr ();
                editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
-               if (editing) return;
+               if (editing)
+                       break;
                
                /* Add new meesage info */
                info = g_slice_new (SendInfo);
@@ -224,11 +272,10 @@ _add_message (ModestTnySendQueue *self, TnyHeader *header)
                g_queue_push_tail (priv->queue, info);
                break;
        default:
-               goto frees;
+               break;
        }
 
        /* Free */
- frees:
        g_free(msg_uid);
 }
 
@@ -279,61 +326,6 @@ 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)
 {
@@ -353,18 +345,9 @@ modest_tny_send_queue_get_type (void)
                        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;
 }
@@ -380,7 +363,6 @@ 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)->add_func         = modest_tny_send_queue_add;
        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)->cancel_func      = modest_tny_send_queue_cancel;
@@ -418,8 +400,6 @@ 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);
 }
 
@@ -435,10 +415,6 @@ 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),
@@ -570,13 +546,16 @@ _on_msg_start_sending (TnySendQueue *self,
 
        /* Get status info */
        item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
-       g_return_if_fail (item != NULL);
-       info = item->data;
-       info->status = MODEST_TNY_SEND_QUEUE_SENDING;
-       
-       /* Set current status item */
-       g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
-       priv->current = item;
+       if (!item) 
+               g_warning  ("%s: item should not be NULL", __FUNCTION__);
+       else {
+               info = item->data;
+               info->status = MODEST_TNY_SEND_QUEUE_SENDING;
+               
+               /* Set current status item */
+               g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
+               priv->current = item;
+       }
 
        /* free */
        g_free (msg_id);
@@ -601,14 +580,17 @@ _on_msg_has_been_sent (TnySendQueue *self,
        
        /* Get status info */
        item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
-       g_return_if_fail(item != NULL);
-
-       /* Remove status info */
-       modest_tny_send_queue_info_free (item->data);
-       g_queue_delete_link (priv->queue, item);
-       priv->current = NULL;
+       if (!item)
+               g_warning ("%s: item should not be NULL", __FUNCTION__);
+       else {
 
-       modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
+               /* 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"));
+       }
 
        /* free */
        g_free(msg_id);
@@ -636,10 +618,8 @@ _on_msg_error_happened (TnySendQueue *self,
                info = g_slice_new (SendInfo);
                info->msg_id = strdup(msg_uid);
                g_queue_push_tail (priv->queue, info);
-       }
-       else {
+       } else
                info = item->data;
-       } 
 
        /* Keep in queue so that we remember that the opertion has failed */
        /* and was not just cancelled */