* Fixes: NB#59203
[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 gboolean
337 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
338                                          const gchar *msg_id)
339 {       
340         ModestTnySendQueueStatus status;
341         
342         g_return_val_if_fail (msg_id != NULL, FALSE); 
343         
344         status = modest_tny_send_queue_get_msg_status (self, msg_id);
345         return status == MODEST_TNY_SEND_QUEUE_SENDING;
346 }
347
348 gboolean
349 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
350 {       
351         ModestTnySendQueuePrivate *priv;
352         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
353         
354         return priv->current != NULL;
355 }
356
357 ModestTnySendQueueStatus
358 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
359 {
360   GList *item;
361   item = modest_tny_send_queue_lookup_info (self, msg_id);
362   if(item == NULL) return MODEST_TNY_SEND_QUEUE_SUSPENDED;
363   return ((SendInfo*)item->data)->status;
364 }
365
366 /* static void  */
367 /* _on_msg_start_sending (TnySendQueue *self, */
368 /*                     TnyMsg *msg, */
369 /*                     gpointer user_data) */
370 /* { */
371 /*      ModestTnySendQueuePrivate *priv; */
372 /*      TnyHeader *header; */
373 /*      GList *item; */
374 /*      SendInfo *info; */
375
376 /*      priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); */
377
378 /*      header = tny_msg_get_header(msg); */
379 /*      item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header)); */
380
381 /*      if (item != NULL) */
382 /*      { */
383 /*              info = item->data; */
384 /*              info->status = MODEST_TNY_SEND_QUEUE_SENDING; */
385
386 /*              g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status); */
387 /*      } */
388
389 /*      priv->current = item; */
390 /* } */
391
392 static void 
393 _on_msg_has_been_sent (TnySendQueue *self,
394                        TnyMsg *msg, 
395                        gpointer user_data)
396 {
397         ModestTnySendQueuePrivate *priv;
398         TnyHeader *header;
399         GList *item;
400
401         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
402
403         header = tny_msg_get_header (msg);
404
405         /* TODO: Use this version as soon as the msg-sending
406          *  notification works */
407 #if 0
408         item = priv->current;
409         g_assert(item != NULL);
410 #else
411         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id(header));
412         g_assert(item != NULL);
413 #endif
414
415         modest_tny_send_queue_info_free (item->data);
416         g_queue_delete_link (priv->queue, item);
417         priv->current = NULL;
418 }
419
420 static void _on_msg_error_happened (TnySendQueue *self,
421                                     TnyHeader *header,
422                                     TnyMsg *msg,
423                                     GError *err,
424                                     gpointer user_data)
425 {
426         ModestTnySendQueuePrivate *priv;
427         SendInfo *info;
428         GList *item;
429
430         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
431
432         /* TODO: Use this version as soon as the msg-sending
433          *  notification works */
434 #if 0
435         item = priv->current;
436         g_assert(item != NULL);
437         info = priv->current->data;
438 #else
439         /* TODO: Why do we get the msg and its header separately? The docs
440          * don't really tell. */
441         g_assert(header == tny_msg_get_header (msg)); // ????
442         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header));
443         g_assert(item != NULL);
444         info = item->data;
445 #endif
446
447         /* Keep in queue so that we remember that the opertion has failed
448          * and was not just cancelled */
449         info->status = MODEST_TNY_SEND_QUEUE_FAILED;
450         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
451 }