Reverted my previous changes, this will be fixed in another way.
[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 <tny-folder-change.h>
37 #include <tny-folder-observer.h>
38 #include <modest-tny-account.h>
39 #include <modest-runtime.h>
40 #include <modest-platform.h>
41 #include <widgets/modest-window-mgr.h>
42 #include <modest-marshal.h>
43 #include <string.h> /* strcmp */
44
45 /* 'private'/'protected' functions */
46 static void modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass);
47 static void modest_tny_send_queue_finalize   (GObject *obj);
48 static void modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class);
49
50 /* Signal handlers */ 
51 static void _on_msg_start_sending (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, int done, int total, gpointer user_data);
52 static void _on_msg_has_been_sent (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, int done, int total, gpointer user_data);
53 static void _on_msg_error_happened (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, GError *err, gpointer user_data);
54
55 static TnyFolder*modest_tny_send_queue_get_outbox (TnySendQueue *self);
56 static TnyFolder*modest_tny_send_queue_get_sentbox (TnySendQueue *self);
57
58 /* list my signals  */
59 enum {
60         STATUS_CHANGED,
61         /* MY_SIGNAL_1, */
62         /* MY_SIGNAL_2, */
63         LAST_SIGNAL
64 };
65
66 typedef struct _SendInfo SendInfo;
67 struct _SendInfo {
68         gchar* msg_id;
69         ModestTnySendQueueStatus status;
70 };
71
72 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
73 struct _ModestTnySendQueuePrivate {
74         /* Queued infos */
75         GQueue* queue;
76
77         /* The info that is currently being sent */
78         GList* current;
79 };
80
81 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
83                                                    ModestTnySendQueuePrivate))
84
85 /* globals */
86 static TnyCamelSendQueueClass *parent_class = NULL;
87
88 /* uncomment the following if you have defined any signals */
89 static guint signals[LAST_SIGNAL] = {0};
90
91 /*
92  * this thread actually tries to send all the mails in the outbox and keeps
93  * track of their state.
94  */
95
96 static int
97 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
98 {
99         return strcmp( ((SendInfo*)info)->msg_id, msg_id);
100 }
101
102 static void
103 modest_tny_send_queue_info_free(SendInfo *info)
104 {
105         g_free(info->msg_id);
106         g_slice_free(SendInfo, info);
107 }
108
109 static GList*
110 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
111 {
112         ModestTnySendQueuePrivate *priv;
113         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
114
115         return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
116 }
117
118 static void
119 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
120 {
121         ModestTnySendQueuePrivate *priv;
122         SendInfo *info;
123         TnyIterator *iter = NULL;
124         TnyFolder *outbox = NULL;
125         TnyList *headers = tny_simple_list_new ();
126         TnyHeader *header = NULL;
127
128         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
129         if(priv->current != NULL)
130         {
131                 info = priv->current->data;
132                 priv->current = NULL;
133
134                 /* Keep in list until retry, so that we know that this was suspended
135                  * by the user and not by an error. */
136                 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
137
138                 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
139         }
140
141         /* Set flags to supend sending operaiton (if removed, this is not necessary) */
142         if (!remove) {          
143                 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
144                 tny_folder_get_headers (outbox, headers, TRUE, err);
145                 if (err != NULL) goto frees;
146                 iter = tny_list_create_iterator (headers);
147                 while (!tny_iterator_is_done (iter)) {
148                         header = TNY_HEADER (tny_iterator_get_current (iter));
149                         if (header) {   
150                                 tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
151                                 tny_header_set_flags (header, TNY_HEADER_FLAG_SUSPENDED_PRIORITY);
152                                 tny_iterator_next (iter);
153                                 g_object_unref (header);
154                         }
155                 }
156                 
157                 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
158                 g_queue_free (priv->queue);
159                 priv->queue = g_queue_new();
160         }
161                 
162         /* Dont call super class implementaiton, becasue camel removes messages from outbox */
163         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
164
165  frees:
166         if (headers != NULL)
167                 g_object_unref (G_OBJECT (headers));
168         if (outbox != NULL) 
169                 g_object_unref (G_OBJECT (outbox));
170         if (iter != NULL) 
171                 g_object_unref (iter);
172 }
173
174 static void
175 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
176 {
177         ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
178         TnyHeader *header = NULL;
179         SendInfo *info = NULL;
180         GList* existing = NULL;
181         const gchar* msg_id = NULL;
182
183         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
184         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
185
186         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
187
188         /* TODO: We cannot access the message ID of the message here because
189          * it might  just have been created. Wait for it being added to outbox. */
190
191         header = tny_msg_get_header (msg);
192         msg_id = tny_header_get_message_id (header);
193         g_return_if_fail(msg_id != NULL);
194
195         /* Put newly added message in WAITING state */
196         existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
197         if(existing != NULL)
198         {
199                 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
200                 //        info->status == MODEST_TNY_SEND_QUEUE_FAILED);
201
202                 info = existing->data;
203                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
204         }
205         else
206         {
207                 
208                 info = g_slice_new (SendInfo);
209
210                 info->msg_id = strdup(msg_id);
211                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
212                 g_queue_push_tail (priv->queue, info);
213         }
214
215         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
216
217         g_object_unref(G_OBJECT(header));
218 }
219
220 static void
221 _add_message (ModestTnySendQueue *self, TnyHeader *header)
222 {
223         ModestWindowMgr *mgr = NULL;
224         ModestTnySendQueuePrivate *priv;
225         SendInfo *info = NULL;
226         GList* existing = NULL;
227         gchar* msg_uid = NULL;
228         ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_WAITING;
229         gboolean editing = FALSE;
230
231         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
232         g_return_if_fail (TNY_IS_HEADER(header));
233         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
234         
235         /* Check whether the mail is already in the queue */
236         msg_uid = modest_tny_send_queue_get_msg_id (header);
237         status = modest_tny_send_queue_get_msg_status (self, msg_uid);
238         switch (status) {
239         case MODEST_TNY_SEND_QUEUE_UNKNONW:
240         case MODEST_TNY_SEND_QUEUE_SUSPENDED:
241         case MODEST_TNY_SEND_QUEUE_FAILED:
242                 if (status != MODEST_TNY_SEND_QUEUE_SUSPENDED)
243                         tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
244                 
245                 /* Check if it already exists on queue */
246                 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
247                 if(existing != NULL) return;
248         
249                 /* Check if its being edited */
250                 mgr = modest_runtime_get_window_mgr ();
251                 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
252                 if (editing) return;
253                 
254                 /* Add new meesage info */
255                 info = g_slice_new (SendInfo);
256                 info->msg_id = strdup(msg_uid);
257                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
258                 g_queue_push_tail (priv->queue, info);
259                 break;
260         default:
261                 goto frees;
262         }
263
264         /* Free */
265  frees:
266         g_free(msg_uid);
267 }
268
269 static TnyFolder*
270 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
271 {
272         TnyFolder *folder;
273         TnyCamelTransportAccount *account;
274
275         g_return_val_if_fail (self, NULL);
276
277         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
278         if (!account) {
279                 g_printerr ("modest: no account for send queue\n");
280                 return NULL;
281         }
282         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
283                                                          TNY_FOLDER_TYPE_SENT);
284         g_object_unref (G_OBJECT(account));
285
286         return folder;
287 }
288
289
290 static TnyFolder*
291 modest_tny_send_queue_get_outbox (TnySendQueue *self)
292 {
293         TnyFolder *folder;
294         TnyCamelTransportAccount *account;
295
296         g_return_val_if_fail (self, NULL);
297
298         account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
299         if (!account) {
300                 g_printerr ("modest: no account for send queue\n");
301                 return NULL;
302         }
303         folder  = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
304                                                          TNY_FOLDER_TYPE_OUTBOX);
305
306         /* This vfunc's tinymail contract does not allow it to return NULL. */
307         if (!folder) {
308                 g_warning("%s: Returning NULL.\n", __FUNCTION__);
309         }
310
311         g_object_unref (G_OBJECT(account));
312
313         return folder;
314 }
315
316 GType
317 modest_tny_send_queue_get_type (void)
318 {
319         static GType my_type = 0;
320
321         if (my_type == 0) {
322                 static const GTypeInfo my_info = {
323                         sizeof(ModestTnySendQueueClass),
324                         NULL,           /* base init */
325                         NULL,           /* base finalize */
326                         (GClassInitFunc) modest_tny_send_queue_class_init,
327                         NULL,           /* class finalize */
328                         NULL,           /* class data */
329                         sizeof(ModestTnySendQueue),
330                         0,              /* n_preallocs */
331                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
332                         NULL
333                 };
334                 
335                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
336                                                   "ModestTnySendQueue",
337                                                   &my_info, 0);
338         }
339         return my_type;
340 }
341
342
343 static void
344 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
345 {
346         GObjectClass *gobject_class;
347
348         gobject_class = (GObjectClass*) klass;
349         
350         parent_class            = g_type_class_peek_parent (klass);
351         gobject_class->finalize = modest_tny_send_queue_finalize;
352
353         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
354         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
355         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
356         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
357         klass->status_changed   = NULL;
358
359         signals[STATUS_CHANGED] =
360                 g_signal_new ("status_changed",
361                               G_TYPE_FROM_CLASS (gobject_class),
362                               G_SIGNAL_RUN_FIRST,
363                               G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
364                               NULL, NULL,
365                               modest_marshal_VOID__STRING_INT,
366                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
367
368         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
369 }
370
371 static void
372 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
373 {
374         ModestTnySendQueuePrivate *priv;
375
376         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
377         priv->queue = g_queue_new();
378         priv->current = NULL;
379 }
380
381 static void
382 modest_tny_send_queue_finalize (GObject *obj)
383 {
384         ModestTnySendQueuePrivate *priv;
385                 
386         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
387
388         g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
389         g_queue_free (priv->queue);
390
391         G_OBJECT_CLASS(parent_class)->finalize (obj);
392 }
393
394 ModestTnySendQueue*
395 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
396 {
397         ModestTnySendQueue *self;
398         
399         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
400         
401         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
402         
403         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
404                                                     account); 
405
406         /* Connect signals to control when a msg is being or has been sent */
407         g_signal_connect (G_OBJECT(self), "msg-sending",
408                           G_CALLBACK(_on_msg_start_sending),
409                           NULL);                          
410         g_signal_connect (G_OBJECT(self), "msg-sent",
411                           G_CALLBACK(_on_msg_has_been_sent), 
412                           NULL);
413         g_signal_connect (G_OBJECT(self), "error-happened",
414                           G_CALLBACK(_on_msg_error_happened),
415                           NULL);
416         return self;
417 }
418
419
420
421 void
422 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
423 {
424         TnyIterator *iter = NULL;
425         TnyFolder *outbox = NULL;
426         TnyList *headers = tny_simple_list_new ();
427         TnyHeader *header = NULL;
428         GError *err = NULL;
429
430         outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
431         if (!outbox)
432                 return;
433
434         tny_folder_get_headers (outbox, headers, TRUE, &err);
435         if (err != NULL) 
436                 goto frees;
437
438         iter = tny_list_create_iterator (headers);
439         while (!tny_iterator_is_done (iter)) {
440                 header = TNY_HEADER (tny_iterator_get_current (iter));
441                 if (header) {   
442                         _add_message (self, header); 
443                         g_object_unref (header);
444                 }
445
446                 tny_iterator_next (iter);
447         }
448         
449         /* Flush send queue */
450         tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
451
452  frees:
453         if (headers != NULL)
454                 g_object_unref (G_OBJECT (headers));
455         if (outbox != NULL) 
456                 g_object_unref (G_OBJECT (outbox));
457         if (iter != NULL) 
458                 g_object_unref (iter);
459 }
460
461 gboolean
462 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
463                                          const gchar *msg_id)
464 {       
465         ModestTnySendQueueStatus status;
466         
467         g_return_val_if_fail (msg_id != NULL, FALSE); 
468         
469         status = modest_tny_send_queue_get_msg_status (self, msg_id);
470         return status == MODEST_TNY_SEND_QUEUE_SENDING;
471 }
472
473 gboolean
474 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
475 {       
476         ModestTnySendQueuePrivate *priv;
477         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
478         
479         return priv->current != NULL;
480 }
481
482 ModestTnySendQueueStatus
483 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
484 {
485   GList *item;
486   item = modest_tny_send_queue_lookup_info (self, msg_id);
487   if(item == NULL) return MODEST_TNY_SEND_QUEUE_UNKNONW;
488   return ((SendInfo*)item->data)->status;
489 }
490
491 gchar *
492 modest_tny_send_queue_get_msg_id (TnyHeader *header)
493 {
494         const gchar *uid = NULL;
495         gchar* msg_uid = NULL;
496         gchar **tmp = NULL;
497                 
498         /* Get message uid */
499         uid = tny_header_get_uid (header);
500         if (uid)
501                 tmp = g_strsplit (uid, "__", 2);
502         
503         if (tmp) {
504                 if (tmp[1] != NULL) 
505                         msg_uid = g_strconcat (tmp[0], "_", NULL);
506                 else 
507                         msg_uid = g_strdup(tmp[0]);
508
509                 /* free */
510                 g_strfreev(tmp);
511         }
512
513         return msg_uid;
514 }
515
516
517 static void
518 _on_msg_start_sending (TnySendQueue *self,
519                        TnyHeader *header,
520                        TnyMsg *msg,
521                        int done, 
522                        int total,
523                        gpointer user_data)
524 {
525         ModestTnySendQueuePrivate *priv = NULL;
526         GList *item = NULL;
527         SendInfo *info = NULL;
528         gchar *msg_id = NULL;
529
530         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
531         
532         /* Get message uid */
533         msg_id = modest_tny_send_queue_get_msg_id (header);
534
535         /* Get status info */
536         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
537         g_return_if_fail (item != NULL);
538         info = item->data;
539         info->status = MODEST_TNY_SEND_QUEUE_SENDING;
540         
541         /* Set current status item */
542         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
543         priv->current = item;
544
545         /* free */
546         g_free (msg_id);
547 }
548
549 static void 
550 _on_msg_has_been_sent (TnySendQueue *self,
551                        TnyHeader *header,
552                        TnyMsg *msg, 
553                        int done, 
554                        int total,
555                        gpointer user_data)
556 {
557         ModestTnySendQueuePrivate *priv;
558         GList *item;
559         gchar *msg_id = NULL;
560
561         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
562
563         /* Get message uid */
564         msg_id = modest_tny_send_queue_get_msg_id (header);
565         
566         /* Get status info */
567         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
568         g_return_if_fail(item != NULL);
569
570         /* Remove status info */
571         modest_tny_send_queue_info_free (item->data);
572         g_queue_delete_link (priv->queue, item);
573         priv->current = NULL;
574
575         modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
576
577         /* free */
578         g_free(msg_id);
579 }
580
581 static void 
582 _on_msg_error_happened (TnySendQueue *self,
583                         TnyHeader *header,
584                         TnyMsg *msg,
585                         GError *err,
586                         gpointer user_data)
587 {
588         ModestTnySendQueuePrivate *priv = NULL;
589         SendInfo *info = NULL;
590         GList *item = NULL;
591         gchar* msg_uid = NULL;
592         
593         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
594         
595         /* Get sending info (create new if it doesn not exist) */
596         msg_uid = modest_tny_send_queue_get_msg_id (header);
597         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), 
598                                                   msg_uid);     
599         if (item == NULL) {
600                 info = g_slice_new (SendInfo);
601                 info->msg_id = strdup(msg_uid);
602                 g_queue_push_tail (priv->queue, info);
603         }
604         else {
605                 info = item->data;
606         } 
607
608         /* Keep in queue so that we remember that the opertion has failed */
609         /* and was not just cancelled */
610         info->status = MODEST_TNY_SEND_QUEUE_FAILED;
611         
612         /* Notify status has changed */
613         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
614
615         /* free */
616         g_free(msg_uid);
617 }