* check for a valid foldername
[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-runtime.h>
38 #include <modest-platform.h>
39 #include <widgets/modest-window-mgr.h>
40 #include <modest-marshal.h>
41 #include <string.h> /* strcmp */
42
43 /* 'private'/'protected' functions */
44 static void modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass);
45 static void modest_tny_send_queue_finalize   (GObject *obj);
46 static void modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class);
47
48 /* Signal handlers */ 
49 static void _on_msg_start_sending (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, int done, int total, gpointer user_data);
50 static void _on_msg_has_been_sent (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, int done, int total, gpointer user_data);
51 static void _on_msg_error_happened (TnySendQueue *self, TnyHeader *header, TnyMsg *msg, GError *err, gpointer user_data);
52
53 static TnyFolder*modest_tny_send_queue_get_outbox (TnySendQueue *self);
54 static TnyFolder*modest_tny_send_queue_get_sentbox (TnySendQueue *self);
55
56 /* list my signals  */
57 enum {
58         STATUS_CHANGED,
59         /* MY_SIGNAL_1, */
60         /* MY_SIGNAL_2, */
61         LAST_SIGNAL
62 };
63
64 typedef struct _SendInfo SendInfo;
65 struct _SendInfo {
66         gchar* msg_id;
67         ModestTnySendQueueStatus status;
68 };
69
70 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
71 struct _ModestTnySendQueuePrivate {
72         /* Queued infos */
73         GQueue* queue;
74
75         /* The info that is currently being sent */
76         GList* current;
77 };
78
79 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                    MODEST_TYPE_TNY_SEND_QUEUE, \
81                                                    ModestTnySendQueuePrivate))
82
83 /* globals */
84 static TnyCamelSendQueueClass *parent_class = NULL;
85
86 /* uncomment the following if you have defined any signals */
87 static guint signals[LAST_SIGNAL] = {0};
88
89 /*
90  * this thread actually tries to send all the mails in the outbox and keeps
91  * track of their state.
92  */
93
94 static int
95 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
96 {
97         return strcmp( ((SendInfo*)info)->msg_id, msg_id);
98 }
99
100 static void
101 modest_tny_send_queue_info_free(SendInfo *info)
102 {
103         g_free(info->msg_id);
104         g_slice_free(SendInfo, info);
105 }
106
107 static GList*
108 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
109 {
110         ModestTnySendQueuePrivate *priv;
111         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
112
113         return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
114 }
115
116 static void
117 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
118 {
119         ModestTnySendQueuePrivate *priv;
120         SendInfo *info;
121         TnyIterator *iter = NULL;
122         TnyFolder *outbox = NULL;
123         TnyList *headers = tny_simple_list_new ();
124         TnyHeader *header = NULL;
125
126         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
127         if(priv->current != NULL)
128         {
129                 info = priv->current->data;
130                 priv->current = NULL;
131
132                 /* Keep in list until retry, so that we know that this was suspended
133                  * by the user and not by an error. */
134                 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
135
136                 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
137         }
138
139         /* Set flags to supend sending operaiton (if removed, this is not necessary) */
140         if (!remove) {          
141                 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
142                 tny_folder_get_headers (outbox, headers, TRUE, err);
143                 if (err != NULL) goto frees;
144                 iter = tny_list_create_iterator (headers);
145                 while (!tny_iterator_is_done (iter)) {
146                         header = TNY_HEADER (tny_iterator_get_current (iter));
147                         if (header) {   
148                                 tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
149                                 tny_header_set_flags (header, TNY_HEADER_FLAG_SUSPENDED_PRIORITY);
150                                 tny_iterator_next (iter);
151                                 g_object_unref (header);
152                         }
153                 }
154                 
155                 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
156                 g_queue_free (priv->queue);
157                 priv->queue = g_queue_new();
158         }
159                 
160         /* Dont call super class implementaiton, becasue camel removes messages from outbox */
161         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
162
163  frees:
164         if (headers != NULL)
165                 g_object_unref (G_OBJECT (headers));
166         if (outbox != NULL) 
167                 g_object_unref (G_OBJECT (outbox));
168         if (iter != NULL) 
169                 g_object_unref (iter);
170 }
171
172 static void
173 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
174 {
175         ModestTnySendQueuePrivate *priv;
176         TnyHeader *header = NULL;
177         SendInfo *info = NULL;
178         GList* existing = NULL;
179         const gchar* msg_id = NULL;
180
181         g_return_if_fail (TNY_IS_SEND_QUEUE(self));
182         g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
183
184         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
185         header = tny_msg_get_header (msg);
186         
187         /* Note that this call actually sets the message id to something
188          * sensible. */ 
189         TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
190
191         /* Check whether the mail is already in the queue */
192 /*      msg_id = tny_header_get_message_id (header); */
193         msg_id = tny_header_get_uid (header);   
194         g_return_if_fail (msg_id != NULL);
195         existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
196         if(existing != NULL)
197         {
198                 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
199                 //        info->status == MODEST_TNY_SEND_QUEUE_FAILED);
200
201                 info = existing->data;
202                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
203         }
204         else
205         {
206                 
207                 info = g_slice_new (SendInfo);
208
209                 info->msg_id = strdup(msg_id);
210                 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
211                 g_queue_push_tail (priv->queue, info);
212         }
213
214         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
215
216         /* free */
217         g_object_unref (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                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
335                                                   "ModestTnySendQueue",
336                                                   &my_info, 0);
337         }
338         return my_type;
339 }
340
341
342 static void
343 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
344 {
345         GObjectClass *gobject_class;
346
347         gobject_class = (GObjectClass*) klass;
348         
349         parent_class            = g_type_class_peek_parent (klass);
350         gobject_class->finalize = modest_tny_send_queue_finalize;
351
352         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
353         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
354         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
355         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
356         klass->status_changed   = NULL;
357
358         signals[STATUS_CHANGED] =
359                 g_signal_new ("status_changed",
360                               G_TYPE_FROM_CLASS (gobject_class),
361                               G_SIGNAL_RUN_FIRST,
362                               G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
363                               NULL, NULL,
364                               modest_marshal_VOID__STRING_INT,
365                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
366
367         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
368 }
369
370 static void
371 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
372 {
373         ModestTnySendQueuePrivate *priv;
374
375         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
376         priv->queue = g_queue_new();
377         priv->current = NULL;
378 }
379
380 static void
381 modest_tny_send_queue_finalize (GObject *obj)
382 {
383         ModestTnySendQueuePrivate *priv;
384                 
385         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
386
387         g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
388         g_queue_free (priv->queue);
389
390         G_OBJECT_CLASS(parent_class)->finalize (obj);
391 }
392
393 ModestTnySendQueue*
394 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
395 {
396         ModestTnySendQueue *self;
397         
398         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
399         
400         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
401         
402         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
403                                                     account); 
404
405         /* Connect signals to control when a msg is being or has been sent */
406         g_signal_connect (G_OBJECT(self), "msg-sending",
407                           G_CALLBACK(_on_msg_start_sending),
408                           NULL);                          
409         g_signal_connect (G_OBJECT(self), "msg-sent",
410                           G_CALLBACK(_on_msg_has_been_sent), 
411                           NULL);
412         g_signal_connect (G_OBJECT(self), "error-happened",
413                           G_CALLBACK(_on_msg_error_happened),
414                           NULL);
415         return self;
416 }
417
418
419
420 void
421 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
422 {
423         TnyIterator *iter = NULL;
424         TnyFolder *outbox = NULL;
425         TnyList *headers = tny_simple_list_new ();
426         TnyHeader *header = NULL;
427         GError *err = NULL;
428
429         outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
430         if (!outbox)
431                 return;
432
433         tny_folder_get_headers (outbox, headers, TRUE, &err);
434         if (err != NULL) 
435                 goto frees;
436
437         iter = tny_list_create_iterator (headers);
438         while (!tny_iterator_is_done (iter)) {
439                 header = TNY_HEADER (tny_iterator_get_current (iter));
440                 if (header) {   
441                         _add_message (self, header); 
442                         g_object_unref (header);
443                 }
444
445                 tny_iterator_next (iter);
446         }
447         
448         /* Flush send queue */
449         tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
450
451  frees:
452         if (headers != NULL)
453                 g_object_unref (G_OBJECT (headers));
454         if (outbox != NULL) 
455                 g_object_unref (G_OBJECT (outbox));
456         if (iter != NULL) 
457                 g_object_unref (iter);
458 }
459
460 gboolean
461 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
462                                          const gchar *msg_id)
463 {       
464         ModestTnySendQueueStatus status;
465         
466         g_return_val_if_fail (msg_id != NULL, FALSE); 
467         
468         status = modest_tny_send_queue_get_msg_status (self, msg_id);
469         return status == MODEST_TNY_SEND_QUEUE_SENDING;
470 }
471
472 gboolean
473 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
474 {       
475         ModestTnySendQueuePrivate *priv;
476         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
477         
478         return priv->current != NULL;
479 }
480
481 ModestTnySendQueueStatus
482 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
483 {
484   GList *item;
485   item = modest_tny_send_queue_lookup_info (self, msg_id);
486   if(item == NULL) return MODEST_TNY_SEND_QUEUE_UNKNONW;
487   return ((SendInfo*)item->data)->status;
488 }
489
490 gchar *
491 modest_tny_send_queue_get_msg_id (TnyHeader *header)
492 {
493         const gchar *uid = NULL;
494         gchar* msg_uid = NULL;
495         gchar **tmp = NULL;
496                 
497         /* Get message uid */
498         uid = tny_header_get_uid (header);
499         tmp = g_strsplit (uid, "__", 2);
500         if (tmp[1] != NULL) 
501                 msg_uid = g_strconcat (tmp[0], "_", NULL);
502         else 
503                 msg_uid = g_strdup(tmp[0]);
504
505         /* free */
506         g_strfreev(tmp);
507
508         return msg_uid;
509 }
510
511
512 static void
513 _on_msg_start_sending (TnySendQueue *self,
514                        TnyHeader *header,
515                        TnyMsg *msg,
516                        int done, 
517                        int total,
518                        gpointer user_data)
519 {
520         ModestTnySendQueuePrivate *priv = NULL;
521         GList *item = NULL;
522         SendInfo *info = NULL;
523         gchar *msg_id = NULL;
524
525         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
526         
527         /* Get message uid */
528         msg_id = modest_tny_send_queue_get_msg_id (header);
529
530         /* Get status info */
531         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
532         g_return_if_fail (item != NULL);
533         info = item->data;
534         info->status = MODEST_TNY_SEND_QUEUE_SENDING;
535         
536         /* Set current status item */
537         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
538         priv->current = item;
539
540         /* free */
541         g_free (msg_id);
542 }
543
544 static void 
545 _on_msg_has_been_sent (TnySendQueue *self,
546                        TnyHeader *header,
547                        TnyMsg *msg, 
548                        int done, 
549                        int total,
550                        gpointer user_data)
551 {
552         ModestTnySendQueuePrivate *priv;
553         GList *item;
554         gchar *msg_id = NULL;
555
556         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
557
558         /* Get message uid */
559         msg_id = modest_tny_send_queue_get_msg_id (header);
560         
561         /* Get status info */
562         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
563         g_return_if_fail(item != NULL);
564
565         /* Remove status info */
566         modest_tny_send_queue_info_free (item->data);
567         g_queue_delete_link (priv->queue, item);
568         priv->current = NULL;
569
570         modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
571
572         /* free */
573         g_free(msg_id);
574 }
575
576 static void 
577 _on_msg_error_happened (TnySendQueue *self,
578                         TnyHeader *header,
579                         TnyMsg *msg,
580                         GError *err,
581                         gpointer user_data)
582 {
583         ModestTnySendQueuePrivate *priv = NULL;
584         SendInfo *info = NULL;
585         GList *item = NULL;
586         gchar* msg_uid = NULL;
587         
588         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
589         
590         /* Get sending info (create new if it doesn not exist) */
591         msg_uid = modest_tny_send_queue_get_msg_id (header);
592         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), 
593                                                   msg_uid);     
594         if (item == NULL) {
595                 info = g_slice_new (SendInfo);
596                 info->msg_id = strdup(msg_uid);
597                 g_queue_push_tail (priv->queue, info);
598         }
599         else {
600                 info = item->data;
601         } 
602
603         /* Keep in queue so that we remember that the opertion has failed */
604         /* and was not just cancelled */
605         info->status = MODEST_TNY_SEND_QUEUE_FAILED;
606         
607         /* Notify status has changed */
608         g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
609
610         /* free */
611         g_free(msg_uid);
612 }