bbad20251cbc42b4a8afe561e1ad9fce23ec4355
[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 static TnyFolder*modest_tny_send_queue_get_outbox (TnySendQueue *self);
51 static TnyFolder*modest_tny_send_queue_get_sentbox (TnySendQueue *self);
52
53 /* list my signals  */
54 enum {
55         STATUS_CHANGED,
56         /* MY_SIGNAL_1, */
57         /* MY_SIGNAL_2, */
58         LAST_SIGNAL
59 };
60
61 typedef struct _SendInfo SendInfo;
62 struct _SendInfo {
63         gchar* msg_id;
64         ModestTnySendQueueStatus status;
65 };
66
67 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
68 struct _ModestTnySendQueuePrivate {
69         /* Queued infos */
70         GQueue* queue;
71
72         /* The info that is currently being sent */
73         GList* current;
74 };
75
76 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
77                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
78                                                    ModestTnySendQueuePrivate))
79
80 /* globals */
81 static TnyCamelSendQueueClass *parent_class = NULL;
82
83 /* uncomment the following if you have defined any signals */
84 static guint signals[LAST_SIGNAL] = {0};
85
86 /*
87  * this thread actually tries to send all the mails in the outbox and keeps
88  * track of their state.
89  */
90
91 static int
92 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
93 {
94         return strcmp( ((SendInfo*)info)->msg_id, msg_id);
95 }
96
97 static void
98 modest_tny_send_queue_info_free(SendInfo *info)
99 {
100         g_free(info->msg_id);
101         g_slice_free(SendInfo, info);
102 }
103
104 static GList*
105 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
106 {
107         ModestTnySendQueuePrivate *priv;
108         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
109
110         return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
111 }
112
113 static void
114 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
115 {
116         ModestTnySendQueuePrivate *priv;
117         SendInfo *info;
118
119         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
120         if(priv->current != NULL)
121         {
122                 info = priv->current->data;
123                 priv->current = NULL;
124
125                 /* Keep in list until retry, so that we know that this was suspended
126                  * by the user and not by an error. */
127                 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
128
129                 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
130         }
131
132         /* Set flags to supend sending operaiton (if removed, this is not necessary) */
133         if (!remove) {
134                 TnyIterator *iter = NULL;
135                 TnyFolder *outbox = NULL;
136                 TnyList *headers = tny_simple_list_new ();
137                 outbox = modest_tny_send_queue_get_outbox (self);
138                 tny_folder_get_headers (outbox, headers, TRUE, err);
139                 if (err != NULL && *err != NULL) goto frees;
140                 iter = tny_list_create_iterator (headers);
141                 while (!tny_iterator_is_done (iter)) {
142                         TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
143                         tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
144                         tny_header_set_flags (header, TNY_HEADER_FLAG_SUSPENDED_PRIORITY);
145                         g_object_unref (header);
146                         tny_iterator_next (iter);
147                 }
148         frees:
149                 g_object_unref (G_OBJECT (headers));
150                 g_object_unref (G_OBJECT (outbox));
151         }
152                 
153         /* Dont call super class implementaiton, becasue camel removes messages from outbox */
154         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
155 }
156
157 static void
158 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
159 {
160         ModestTnySendQueuePrivate *priv;
161         TnyHeader *header;
162         SendInfo *info = NULL;
163         GList* existing;
164         const gchar* msg_id;
165
166         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
167         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
168
169         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
170         header = tny_msg_get_header (msg);
171
172         /* FIXME: do something smart here... */
173
174         /* Note that this call actually sets the message id to something
175          * sensible. */ 
176         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
177
178         /* Check whether the mail is already in the queue */
179         msg_id = tny_header_get_message_id (header);
180         existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
181         if(existing != NULL)
182         {
183                 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
184                 //        info->status == MODEST_TNY_SEND_QUEUE_FAILED);
185
186                 info = existing->data;
187                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
188         }
189         else
190         {
191                 
192                 info = g_slice_new (SendInfo);
193
194                 info->msg_id = strdup(msg_id);
195                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
196                 g_queue_push_tail (priv->queue, info);
197         }
198
199         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
200 }
201
202 static TnyFolder*
203 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
204 {
205         TnyFolder *folder;
206         TnyCamelTransportAccount *account;
207
208         g_return_val_if_fail (self, NULL);
209
210         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
211         if (!account) {
212                 g_printerr ("modest: no account for send queue\n");
213                 return NULL;
214         }
215         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
216                                                          TNY_FOLDER_TYPE_SENT);
217         g_object_unref (G_OBJECT(account));
218
219         return folder;
220 }
221
222
223 static TnyFolder*
224 modest_tny_send_queue_get_outbox (TnySendQueue *self)
225 {
226         TnyFolder *folder;
227         TnyCamelTransportAccount *account;
228
229         g_return_val_if_fail (self, NULL);
230
231         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
232         if (!account) {
233                 g_printerr ("modest: no account for send queue\n");
234                 return NULL;
235         }
236         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
237                                                          TNY_FOLDER_TYPE_OUTBOX);
238
239         /* This vfunc's tinymail contract does not allow it to return NULL. */
240         if (!folder) {
241                 g_warning("%s: Returning NULL.\n", __FUNCTION__);
242         }
243
244         g_object_unref (G_OBJECT(account));
245
246         return folder;
247 }
248
249 GType
250 modest_tny_send_queue_get_type (void)
251 {
252         static GType my_type = 0;
253
254         if (my_type == 0) {
255                 static const GTypeInfo my_info = {
256                         sizeof(ModestTnySendQueueClass),
257                         NULL,           /* base init */
258                         NULL,           /* base finalize */
259                         (GClassInitFunc) modest_tny_send_queue_class_init,
260                         NULL,           /* class finalize */
261                         NULL,           /* class data */
262                         sizeof(ModestTnySendQueue),
263                         0,              /* n_preallocs */
264                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
265                         NULL
266                 };
267                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
268                                                   "ModestTnySendQueue",
269                                                   &my_info, 0);
270         }
271         return my_type;
272 }
273
274
275 static void
276 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
277 {
278         GObjectClass *gobject_class;
279
280         gobject_class = (GObjectClass*) klass;
281         
282         parent_class            = g_type_class_peek_parent (klass);
283         gobject_class->finalize = modest_tny_send_queue_finalize;
284
285         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
286         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
287         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
288         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
289         klass->status_changed   = NULL;
290
291         signals[STATUS_CHANGED] =
292                 g_signal_new ("status_changed",
293                               G_TYPE_FROM_CLASS (gobject_class),
294                               G_SIGNAL_RUN_FIRST,
295                               G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
296                               NULL, NULL,
297                               modest_marshal_VOID__STRING_INT,
298                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
299
300         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
301 }
302
303 static void
304 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
305 {
306         ModestTnySendQueuePrivate *priv;
307
308         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
309         priv->queue = g_queue_new();
310         priv->current = NULL;
311 }
312
313 static void
314 modest_tny_send_queue_finalize (GObject *obj)
315 {
316         ModestTnySendQueuePrivate *priv;
317                 
318         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
319
320         g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
321         g_queue_free (priv->queue);
322
323         G_OBJECT_CLASS(parent_class)->finalize (obj);
324 }
325
326 ModestTnySendQueue*
327 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
328 {
329         ModestTnySendQueue *self;
330         
331         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
332         
333         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
334         
335         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
336                                                     account); 
337
338         /* Connect signals to control when a msg is being or has been sent */
339         /* TODO: this signal was implemented in tinymail camel send queue, but im
340            waiting for implement some unit tests nbefore commited changes */
341 /*      if (FALSE) { */
342 /*              g_signal_connect (G_OBJECT(self), "msg-sending", */
343 /*                                G_CALLBACK(_on_msg_start_sending),  */
344 /*                                NULL); */
345 /*      } */
346                           
347         g_signal_connect (G_OBJECT(self), "msg-sent",
348                           G_CALLBACK(_on_msg_has_been_sent), 
349                           NULL);
350         g_signal_connect (G_OBJECT(self), "error-happened",
351                           G_CALLBACK(_on_msg_error_happened),
352                           NULL);
353         return self;
354 }
355
356
357
358 void
359 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
360 {
361         /* Flush send queue */
362         tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
363 }
364
365 gboolean
366 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
367                                          const gchar *msg_id)
368 {       
369         ModestTnySendQueueStatus status;
370         
371         g_return_val_if_fail (msg_id != NULL, FALSE); 
372         
373         status = modest_tny_send_queue_get_msg_status (self, msg_id);
374         return status == MODEST_TNY_SEND_QUEUE_SENDING;
375 }
376
377 gboolean
378 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
379 {       
380         ModestTnySendQueuePrivate *priv;
381         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
382         
383         return priv->current != NULL;
384 }
385
386 ModestTnySendQueueStatus
387 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
388 {
389   GList *item;
390   item = modest_tny_send_queue_lookup_info (self, msg_id);
391   if(item == NULL) return MODEST_TNY_SEND_QUEUE_SUSPENDED;
392   return ((SendInfo*)item->data)->status;
393 }
394
395 /* static void  */
396 /* _on_msg_start_sending (TnySendQueue *self, */
397 /*                     TnyMsg *msg, */
398 /*                     gpointer user_data) */
399 /* { */
400 /*      ModestTnySendQueuePrivate *priv; */
401 /*      TnyHeader *header; */
402 /*      GList *item; */
403 /*      SendInfo *info; */
404
405 /*      priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); */
406
407 /*      header = tny_msg_get_header(msg); */
408 /*      item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header)); */
409
410 /*      if (item != NULL) */
411 /*      { */
412 /*              info = item->data; */
413 /*              info->status = MODEST_TNY_SEND_QUEUE_SENDING; */
414
415 /*              g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status); */
416 /*      } */
417
418 /*      priv->current = item; */
419 /* } */
420
421 static void 
422 _on_msg_has_been_sent (TnySendQueue *self,
423                        TnyMsg *msg, 
424                        gpointer user_data)
425 {
426         ModestTnySendQueuePrivate *priv;
427         TnyHeader *header;
428         GList *item;
429
430         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
431
432         header = tny_msg_get_header (msg);
433
434         /* TODO: Use this version as soon as the msg-sending
435          *  notification works */
436 #if 0
437         item = priv->current;
438         g_assert(item != NULL);
439 #else
440         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id(header));
441         g_assert(item != NULL);
442 #endif
443
444         modest_tny_send_queue_info_free (item->data);
445         g_queue_delete_link (priv->queue, item);
446         priv->current = NULL;
447 }
448
449 static void _on_msg_error_happened (TnySendQueue *self,
450                                     TnyHeader *header,
451                                     TnyMsg *msg,
452                                     GError *err,
453                                     gpointer user_data)
454 {
455 /*      ModestTnySendQueuePrivate *priv; */
456 /*      SendInfo *info; */
457 /*      GList *item; */
458 /*      TnyHeader *msg_header; */
459
460 /*      priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); */
461
462         /* TODO: Use this version as soon as the msg-sending
463          *  notification works */
464 /* #if 0 */
465 /*      item = priv->current; */
466 /*      g_assert(item != NULL); */
467 /*      info = priv->current->data; */
468 /* #else */
469 /*      /\* TODO: Why do we get the msg and its header separately? The docs */
470 /*       * don't really tell. *\/ */
471 /*      g_assert(header == tny_msg_get_header (msg)); // ???? */
472 /*      msg_header = tny_msg_get_header (msg); */
473 /*      item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),  */
474 /*                                                tny_header_get_message_id (msg_header)); */
475 /*      g_object_unref (msg_header); */
476 /*      g_assert(item != NULL); */
477 /*      info = item->data; */
478 /* #endif */
479
480         /* Keep in queue so that we remember that the opertion has failed
481          * and was not just cancelled */
482 /*      info->status = MODEST_TNY_SEND_QUEUE_FAILED; */
483 /*      g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status); */
484 }