* src/modest-text-utils.[ch]:
[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
215         /* This vfunc's tinymail contract does not allow it to return NULL. */
216         if (!folder) {
217                 g_warning("%s: Returning NULL.\n", __FUNCTION__);
218         }
219
220         g_object_unref (G_OBJECT(account));
221
222         return folder;
223 }
224
225 GType
226 modest_tny_send_queue_get_type (void)
227 {
228         static GType my_type = 0;
229
230         if (my_type == 0) {
231                 static const GTypeInfo my_info = {
232                         sizeof(ModestTnySendQueueClass),
233                         NULL,           /* base init */
234                         NULL,           /* base finalize */
235                         (GClassInitFunc) modest_tny_send_queue_class_init,
236                         NULL,           /* class finalize */
237                         NULL,           /* class data */
238                         sizeof(ModestTnySendQueue),
239                         0,              /* n_preallocs */
240                         (GInstanceInitFunc) modest_tny_send_queue_instance_init,
241                         NULL
242                 };
243                 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
244                                                   "ModestTnySendQueue",
245                                                   &my_info, 0);
246         }
247         return my_type;
248 }
249
250
251 static void
252 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
253 {
254         GObjectClass *gobject_class;
255
256         gobject_class = (GObjectClass*) klass;
257         
258         parent_class            = g_type_class_peek_parent (klass);
259         gobject_class->finalize = modest_tny_send_queue_finalize;
260
261         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func         = modest_tny_send_queue_add;
262         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func  = modest_tny_send_queue_get_outbox;
263         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
264         TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func      = modest_tny_send_queue_cancel;
265         klass->status_changed   = NULL;
266
267         signals[STATUS_CHANGED] =
268                 g_signal_new ("status_changed",
269                               G_TYPE_FROM_CLASS (gobject_class),
270                               G_SIGNAL_RUN_FIRST,
271                               G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
272                               NULL, NULL,
273                               modest_marshal_VOID__STRING_INT,
274                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
275
276         g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
277 }
278
279 static void
280 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
281 {
282         ModestTnySendQueuePrivate *priv;
283
284         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
285         priv->queue = g_queue_new();
286         priv->current = NULL;
287 }
288
289 static void
290 modest_tny_send_queue_finalize (GObject *obj)
291 {
292         ModestTnySendQueuePrivate *priv;
293                 
294         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
295
296         g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
297         g_queue_free (priv->queue);
298
299         G_OBJECT_CLASS(parent_class)->finalize (obj);
300 }
301
302 ModestTnySendQueue*
303 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
304 {
305         ModestTnySendQueue *self;
306         
307         g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
308         
309         self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
310         
311         tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
312                                                     account); 
313
314         /* Connect signals to control when a msg is being or has been sent */
315         /* TODO: this signal was implemented in tinymail camel send queue, but im
316            waiting for implement some unit tests nbefore commited changes */
317 /*      if (FALSE) { */
318 /*              g_signal_connect (G_OBJECT(self), "msg-sending", */
319 /*                                G_CALLBACK(_on_msg_start_sending),  */
320 /*                                NULL); */
321 /*      } */
322                           
323         g_signal_connect (G_OBJECT(self), "msg-sent",
324                           G_CALLBACK(_on_msg_has_been_sent), 
325                           NULL);
326         g_signal_connect (G_OBJECT(self), "error-happened",
327                           G_CALLBACK(_on_msg_error_happened),
328                           NULL);
329         return self;
330 }
331
332
333
334 void
335 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
336 {
337         /* TODO: Rename this to tny_camel_send_queue_try_to_send() in tinymail 
338         and check that it works, without creating a second worker. */
339 /*      tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self)); */
340 }
341
342 gboolean
343 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
344                                          const gchar *msg_id)
345 {       
346         ModestTnySendQueueStatus status;
347         
348         g_return_val_if_fail (msg_id != NULL, FALSE); 
349         
350         status = modest_tny_send_queue_get_msg_status (self, msg_id);
351         return status == MODEST_TNY_SEND_QUEUE_SENDING;
352 }
353
354 gboolean
355 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
356 {       
357         ModestTnySendQueuePrivate *priv;
358         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
359         
360         return priv->current != NULL;
361 }
362
363 ModestTnySendQueueStatus
364 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
365 {
366   GList *item;
367   item = modest_tny_send_queue_lookup_info (self, msg_id);
368   if(item == NULL) return MODEST_TNY_SEND_QUEUE_SUSPENDED;
369   return ((SendInfo*)item->data)->status;
370 }
371
372 /* static void  */
373 /* _on_msg_start_sending (TnySendQueue *self, */
374 /*                     TnyMsg *msg, */
375 /*                     gpointer user_data) */
376 /* { */
377 /*      ModestTnySendQueuePrivate *priv; */
378 /*      TnyHeader *header; */
379 /*      GList *item; */
380 /*      SendInfo *info; */
381
382 /*      priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); */
383
384 /*      header = tny_msg_get_header(msg); */
385 /*      item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id (header)); */
386
387 /*      if (item != NULL) */
388 /*      { */
389 /*              info = item->data; */
390 /*              info->status = MODEST_TNY_SEND_QUEUE_SENDING; */
391
392 /*              g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status); */
393 /*      } */
394
395 /*      priv->current = item; */
396 /* } */
397
398 static void 
399 _on_msg_has_been_sent (TnySendQueue *self,
400                        TnyMsg *msg, 
401                        gpointer user_data)
402 {
403         ModestTnySendQueuePrivate *priv;
404         TnyHeader *header;
405         GList *item;
406
407         priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
408
409         header = tny_msg_get_header (msg);
410
411         /* TODO: Use this version as soon as the msg-sending
412          *  notification works */
413 #if 0
414         item = priv->current;
415         g_assert(item != NULL);
416 #else
417         item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), tny_header_get_message_id(header));
418         g_assert(item != NULL);
419 #endif
420
421         modest_tny_send_queue_info_free (item->data);
422         g_queue_delete_link (priv->queue, item);
423         priv->current = NULL;
424 }
425
426 static void _on_msg_error_happened (TnySendQueue *self,
427                                     TnyHeader *header,
428                                     TnyMsg *msg,
429                                     GError *err,
430                                     gpointer user_data)
431 {
432 /*      ModestTnySendQueuePrivate *priv; */
433 /*      SendInfo *info; */
434 /*      GList *item; */
435 /*      TnyHeader *msg_header; */
436
437 /*      priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); */
438
439         /* TODO: Use this version as soon as the msg-sending
440          *  notification works */
441 /* #if 0 */
442 /*      item = priv->current; */
443 /*      g_assert(item != NULL); */
444 /*      info = priv->current->data; */
445 /* #else */
446 /*      /\* TODO: Why do we get the msg and its header separately? The docs */
447 /*       * don't really tell. *\/ */
448 /*      g_assert(header == tny_msg_get_header (msg)); // ???? */
449 /*      msg_header = tny_msg_get_header (msg); */
450 /*      item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),  */
451 /*                                                tny_header_get_message_id (msg_header)); */
452 /*      g_object_unref (msg_header); */
453 /*      g_assert(item != NULL); */
454 /*      info = item->data; */
455 /* #endif */
456
457         /* Keep in queue so that we remember that the opertion has failed
458          * and was not just cancelled */
459 /*      info->status = MODEST_TNY_SEND_QUEUE_FAILED; */
460 /*      g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status); */
461 }