Error handling for the send queues
[modest] / src / modest-tny-send-queue.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 #include <modest-tny-send-queue.h>
32 #include <tny-simple-list.h>
33 #include <tny-iterator.h>
34 #include <tny-folder.h>
35 #include <tny-camel-msg.h>
36 #include <modest-tny-account.h>
37 #include <modest-marshal.h>
38 #include <string.h> /* strcmp */
39
40 /* 'private'/'protected' functions */
41 static void modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass);
42 static void modest_tny_send_queue_finalize   (GObject *obj);
43 static void modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class);
44
45 /* Signal handlers */ 
46 static void _on_msg_start_sending (TnySendQueue *self, TnyMsg *msg, gpointer user_data);
47 static void _on_msg_has_been_sent (TnySendQueue *self, TnyMsg *msg, gpointer user_data);
48 static void _on_msg_error_happened (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, GError *err, gpointer user_data);
49
50
51 /* list my signals  */
52 enum {
53         STATUS_CHANGED,
54         /* MY_SIGNAL_1, */
55         /* MY_SIGNAL_2, */
56         LAST_SIGNAL
57 };
58
59 typedef struct _SendInfo SendInfo;
60 struct _SendInfo {
61         gchar* msg_id;
62         ModestTnySendQueueStatus status;
63 };
64
65 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
66 struct _ModestTnySendQueuePrivate {
67         /* Queued infos */
68         GQueue* queue;
69
70         /* The info that is currently being sent */
71         GList* current;
72 };
73
74 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
75                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
76                                                    ModestTnySendQueuePrivate))
77
78 /* globals */
79 static TnyCamelSendQueueClass *parent_class = NULL;
80
81 /* uncomment the following if you have defined any signals */
82 static guint signals[LAST_SIGNAL] = {0};
83
84 /*
85  * this thread actually tries to send all the mails in the outbox and keeps
86  * track of their state.
87  */
88
89 static int
90 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
91 {
92         return strcmp( ((SendInfo*)info)->msg_id, msg_id);
93 }
94
95 static void
96 modest_tny_send_queue_info_free(SendInfo *info)
97 {
98         g_free(info->msg_id);
99         g_slice_free(SendInfo, info);
100 }
101
102 static GList*
103 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
104 {
105         ModestTnySendQueuePrivate *priv;
106         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
107
108         return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
109 }
110
111 static void
112 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
113 {
114         ModestTnySendQueuePrivate *priv;
115         SendInfo *info;
116
117         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
118         if(priv->current != NULL)
119         {
120                 info = priv->current->data;
121                 priv->current = NULL;
122
123                 /* Keep in list until retry, so that we know that this was suspended
124                  * by the user and not by an error. */
125                 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
126
127                 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
128         }
129
130         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
131 }
132
133 static void
134 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
135 {
136         ModestTnySendQueuePrivate *priv;
137         TnyHeader *header;
138         SendInfo *info = NULL;
139         GList* existing;
140         const gchar* msg_id;
141
142         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
143         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
144
145         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
146         header = tny_msg_get_header (msg);
147
148         /* FIXME: do something smart here... */
149
150         /* Note that this call actually sets the message id to something
151          * sensible. */ 
152         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
153
154         /* Check whether the mail is already in the queue */
155         msg_id = tny_header_get_message_id (header);
156         existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
157         if(existing != NULL)
158         {
159                 g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
160                          info->status == MODEST_TNY_SEND_QUEUE_FAILED);
161
162                 info = existing->data;
163                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
164         }
165         else
166         {
167                 
168                 info = g_slice_new (SendInfo);
169
170                 info->msg_id = strdup(msg_id);
171                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
172                 g_queue_push_tail (priv->queue, info);
173         }
174
175         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
176 }
177
178 static TnyFolder*
179 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
180 {
181         TnyFolder *folder;
182         TnyCamelTransportAccount *account;
183
184         g_return_val_if_fail (self, NULL);
185
186         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
187         if (!account) {
188                 g_printerr ("modest: no account for send queue\n");
189                 return NULL;
190         }
191         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
192                                                          TNY_FOLDER_TYPE_SENT);
193         g_object_unref (G_OBJECT(account));
194
195         return folder;
196 }
197
198
199 static TnyFolder*
200 modest_tny_send_queue_get_outbox (TnySendQueue *self)
201 {
202         TnyFolder *folder;
203         TnyCamelTransportAccount *account;
204
205         g_return_val_if_fail (self, NULL);
206
207         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
208         if (!account) {
209                 g_printerr ("modest: no account for send queue\n");
210                 return NULL;
211         }
212         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
213                                                          TNY_FOLDER_TYPE_OUTBOX);
214         g_object_unref (G_OBJECT(account));
215
216         return folder;
217 }
218
219 GType
220 modest_tny_send_queue_get_type (void)
221 {
222         static GType my_type = 0;
223
224         if (my_type == 0) {
225                 static const GTypeInfo my_info = {
226                         sizeof(ModestTnySendQueueClass),
227                         NULL,           /* base init */
228                         NULL,           /* base finalize */
229                         (GClassInitFunc) modest_tny_send_queue_class_init,
230                         NULL,           /* class finalize */
231                         NULL,           /* class data */
232                         sizeof(ModestTnySendQueue),
233                         0,              /* n_preallocs */
234                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
235                         NULL
236                 };
237                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
238                                                   "ModestTnySendQueue",
239                                                   &my_info, 0);
240         }
241         return my_type;
242 }
243
244
245 static void
246 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
247 {
248         GObjectClass *gobject_class;
249
250         gobject_class = (GObjectClass*) klass;
251         
252         parent_class            = g_type_class_peek_parent (klass);
253         gobject_class->finalize = modest_tny_send_queue_finalize;
254
255         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
256         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
257         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
258         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
259         klass->status_changed   = NULL;
260
261         signals[STATUS_CHANGED] =
262                 g_signal_new ("status_changed",
263                               G_TYPE_FROM_CLASS (gobject_class),
264                               G_SIGNAL_RUN_FIRST,
265                               G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
266                               NULL, NULL,
267                               modest_marshal_VOID__STRING_INT,
268                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
269
270         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
271 }
272
273 static void
274 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
275 {
276         ModestTnySendQueuePrivate *priv;
277
278         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
279         priv->queue = g_queue_new();
280         priv->current = NULL;
281 }
282
283 static void
284 modest_tny_send_queue_finalize (GObject *obj)
285 {
286         ModestTnySendQueuePrivate *priv;
287                 
288         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
289
290         g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
291         g_queue_free (priv->queue);
292
293         G_OBJECT_CLASS(parent_class)->finalize (obj);
294 }
295
296 ModestTnySendQueue*
297 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
298 {
299         ModestTnySendQueue *self;
300         
301         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
302         
303         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
304         
305         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
306                                                     account); 
307
308         /* Connect signals to control when a msg is being or has been sent */
309         /* TODO: this signal was implemented in tinymail camel send queue, but im
310            waiting for implement some unit tests nbefore commited changes */
311         if (FALSE) {
312                 g_signal_connect (G_OBJECT(self), "msg-sending",
313                                   G_CALLBACK(_on_msg_start_sending), 
314                                   NULL);
315         }
316                           
317         g_signal_connect (G_OBJECT(self), "msg-sent",
318                           G_CALLBACK(_on_msg_has_been_sent), 
319                           NULL);
320         g_signal_connect (G_OBJECT(self), "error-happened",
321                           G_CALLBACK(_on_msg_error_happened),
322                           NULL);
323         return self;
324 }
325
326
327
328 void
329 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
330 {
331         /* TODO: Rename this to tny_camel_send_queue_try_to_send() in tinymail 
332         and check that it works, without creating a second worker. */
333 /*      tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self)); */
334 }
335
336 #if 0
337 gboolean
338 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
339                                          const gchar *msg_id)
340 {       
341         ModestTnySendQueuePrivate *priv;
342         
343         g_return_val_if_fail (msg_id != NULL, FALSE); 
344         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
345         
346         if (modest_tny_send_queue_sending_in_progress(self))
347                 return g_ascii_strcasecmp(priv->current_msg_id, msg_id);
348         else 
349                 return FALSE;
350 }
351 #endif
352
353 gboolean
354 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
355 {       
356         ModestTnySendQueuePrivate *priv;
357         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
358         
359         return priv->current != NULL;
360 }
361
362 ModestTnySendQueueStatus
363 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
364 {
365   GList *item;
366   item = modest_tny_send_queue_lookup_info (self, msg_id);
367   if(item == NULL) return MODEST_TNY_SEND_QUEUE_SUSPENDED;
368   return ((SendInfo*)item->data)->status;
369 }
370
371 static void 
372 _on_msg_start_sending (TnySendQueue *self,
373                        TnyMsg *msg,
374                        gpointer user_data)
375 {
376         ModestTnySendQueuePrivate *priv;
377         TnyHeader *header;
378         GList *item;
379         SendInfo *info;
380
381         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
382
383         header = tny_msg_get_header(msg);
384         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header));
385
386         if (item != NULL)
387         {
388                 info = item->data;
389                 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
390
391                 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
392         }
393
394         priv->current = item;
395 }
396
397 static void 
398 _on_msg_has_been_sent (TnySendQueue *self,
399                        TnyMsg *msg, 
400                        gpointer user_data)
401 {
402         ModestTnySendQueuePrivate *priv;
403         TnyHeader *header;
404         GList *item;
405
406         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
407
408         header = tny_msg_get_header (msg);
409
410         /* TODO: Use this version as soon as the msg-sending
411          *  notification works */
412 #if 0
413         item = priv->current;
414         g_assert(item != NULL);
415 #else
416         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id(header));
417         g_assert(item != NULL);
418 #endif
419
420         modest_tny_send_queue_info_free (item->data);
421         g_queue_delete_link (priv->queue, item);
422         priv->current = NULL;
423 }
424
425 static void _on_msg_error_happened (TnySendQueue *self,
426                                     TnyHeader *header,
427                                     TnyMsg *msg,
428                                     GError *err,
429                                     gpointer user_data)
430 {
431         ModestTnySendQueuePrivate *priv;
432         SendInfo *info;
433         GList *item;
434
435         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
436
437         /* TODO: Use this version as soon as the msg-sending
438          *  notification works */
439 #if 0
440         item = priv->current;
441         g_assert(item != NULL);
442         info = priv->current->data;
443 #else
444         /* TODO: Why do we get the msg and its header separately? The docs
445          * don't really tell. */
446         g_assert(header == tny_msg_get_header (msg)); // ????
447         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header));
448         g_assert(item != NULL);
449         info = item->data;
450 #endif
451
452         /* Keep in queue so that we remember that the opertion has failed
453          * and was not just cancelled */
454         info->status = MODEST_TNY_SEND_QUEUE_FAILED;
455         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
456 }