1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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 */
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);
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);
55 static TnyFolder*modest_tny_send_queue_get_outbox (TnySendQueue *self);
56 static TnyFolder*modest_tny_send_queue_get_sentbox (TnySendQueue *self);
66 typedef struct _SendInfo SendInfo;
69 ModestTnySendQueueStatus status;
72 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
73 struct _ModestTnySendQueuePrivate {
77 /* The info that is currently being sent */
81 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82 MODEST_TYPE_TNY_SEND_QUEUE, \
83 ModestTnySendQueuePrivate))
86 static TnyCamelSendQueueClass *parent_class = NULL;
88 /* uncomment the following if you have defined any signals */
89 static guint signals[LAST_SIGNAL] = {0};
92 * this thread actually tries to send all the mails in the outbox and keeps
93 * track of their state.
97 on_modest_tny_send_queue_compare_id(gconstpointer info, gconstpointer msg_id)
99 return strcmp( ((SendInfo*)info)->msg_id, msg_id);
103 modest_tny_send_queue_info_free(SendInfo *info)
105 g_free(info->msg_id);
106 g_slice_free(SendInfo, info);
110 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
112 ModestTnySendQueuePrivate *priv;
113 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
115 return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
119 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
121 ModestTnySendQueuePrivate *priv;
123 TnyIterator *iter = NULL;
124 TnyFolder *outbox = NULL;
125 TnyList *headers = tny_simple_list_new ();
126 TnyHeader *header = NULL;
128 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
129 if(priv->current != NULL)
131 info = priv->current->data;
132 priv->current = NULL;
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;
138 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
141 /* Set flags to supend sending operaiton (if removed, this is not necessary) */
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));
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);
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();
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 */
167 g_object_unref (G_OBJECT (headers));
169 g_object_unref (G_OBJECT (outbox));
171 g_object_unref (iter);
175 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
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;
183 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
184 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
186 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
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. */
191 header = tny_msg_get_header (msg);
192 msg_id = tny_header_get_message_id (header);
193 g_return_if_fail(msg_id != NULL);
195 /* Put newly added message in WAITING state */
196 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
199 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
200 // info->status == MODEST_TNY_SEND_QUEUE_FAILED);
202 info = existing->data;
203 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
208 info = g_slice_new (SendInfo);
210 info->msg_id = strdup(msg_id);
211 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
212 g_queue_push_tail (priv->queue, info);
215 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
217 g_object_unref(G_OBJECT(header));
221 _add_message (ModestTnySendQueue *self, TnyHeader *header)
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;
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);
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);
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);
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;
249 /* Check if its being edited */
250 mgr = modest_runtime_get_window_mgr ();
251 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
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);
270 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
273 TnyCamelTransportAccount *account;
275 g_return_val_if_fail (self, NULL);
277 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
279 g_printerr ("modest: no account for send queue\n");
282 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
283 TNY_FOLDER_TYPE_SENT);
284 g_object_unref (G_OBJECT(account));
291 modest_tny_send_queue_get_outbox (TnySendQueue *self)
294 TnyCamelTransportAccount *account;
296 g_return_val_if_fail (self, NULL);
298 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
300 g_printerr ("modest: no account for send queue\n");
303 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
304 TNY_FOLDER_TYPE_OUTBOX);
306 /* This vfunc's tinymail contract does not allow it to return NULL. */
308 g_warning("%s: Returning NULL.\n", __FUNCTION__);
311 g_object_unref (G_OBJECT(account));
317 modest_tny_send_queue_get_type (void)
319 static GType 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),
331 (GInstanceInitFunc) modest_tny_send_queue_instance_init,
335 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
336 "ModestTnySendQueue",
344 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
346 GObjectClass *gobject_class;
348 gobject_class = (GObjectClass*) klass;
350 parent_class = g_type_class_peek_parent (klass);
351 gobject_class->finalize = modest_tny_send_queue_finalize;
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;
359 signals[STATUS_CHANGED] =
360 g_signal_new ("status_changed",
361 G_TYPE_FROM_CLASS (gobject_class),
363 G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
365 modest_marshal_VOID__STRING_INT,
366 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
368 g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
372 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
374 ModestTnySendQueuePrivate *priv;
376 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
377 priv->queue = g_queue_new();
378 priv->current = NULL;
382 modest_tny_send_queue_finalize (GObject *obj)
384 ModestTnySendQueuePrivate *priv;
386 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
388 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
389 g_queue_free (priv->queue);
391 G_OBJECT_CLASS(parent_class)->finalize (obj);
395 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
397 ModestTnySendQueue *self;
399 g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
401 self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
403 tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
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),
410 g_signal_connect (G_OBJECT(self), "msg-sent",
411 G_CALLBACK(_on_msg_has_been_sent),
413 g_signal_connect (G_OBJECT(self), "error-happened",
414 G_CALLBACK(_on_msg_error_happened),
422 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
424 TnyIterator *iter = NULL;
425 TnyFolder *outbox = NULL;
426 TnyList *headers = tny_simple_list_new ();
427 TnyHeader *header = NULL;
430 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
434 tny_folder_get_headers (outbox, headers, TRUE, &err);
438 iter = tny_list_create_iterator (headers);
439 while (!tny_iterator_is_done (iter)) {
440 header = TNY_HEADER (tny_iterator_get_current (iter));
442 _add_message (self, header);
443 g_object_unref (header);
446 tny_iterator_next (iter);
449 /* Flush send queue */
450 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
454 g_object_unref (G_OBJECT (headers));
456 g_object_unref (G_OBJECT (outbox));
458 g_object_unref (iter);
462 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
465 ModestTnySendQueueStatus status;
467 g_return_val_if_fail (msg_id != NULL, FALSE);
469 status = modest_tny_send_queue_get_msg_status (self, msg_id);
470 return status == MODEST_TNY_SEND_QUEUE_SENDING;
474 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
476 ModestTnySendQueuePrivate *priv;
477 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
479 return priv->current != NULL;
482 ModestTnySendQueueStatus
483 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
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;
492 modest_tny_send_queue_get_msg_id (TnyHeader *header)
494 const gchar *uid = NULL;
495 gchar* msg_uid = NULL;
498 /* Get message uid */
499 uid = tny_header_get_uid (header);
501 tmp = g_strsplit (uid, "__", 2);
505 msg_uid = g_strconcat (tmp[0], "_", NULL);
507 msg_uid = g_strdup(tmp[0]);
518 _on_msg_start_sending (TnySendQueue *self,
525 ModestTnySendQueuePrivate *priv = NULL;
527 SendInfo *info = NULL;
528 gchar *msg_id = NULL;
530 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
532 /* Get message uid */
533 msg_id = modest_tny_send_queue_get_msg_id (header);
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);
539 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
541 /* Set current status item */
542 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
543 priv->current = item;
550 _on_msg_has_been_sent (TnySendQueue *self,
557 ModestTnySendQueuePrivate *priv;
559 gchar *msg_id = NULL;
561 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
563 /* Get message uid */
564 msg_id = modest_tny_send_queue_get_msg_id (header);
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);
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;
575 modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
582 _on_msg_error_happened (TnySendQueue *self,
588 ModestTnySendQueuePrivate *priv = NULL;
589 SendInfo *info = NULL;
591 gchar* msg_uid = NULL;
593 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
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),
600 info = g_slice_new (SendInfo);
601 info->msg_id = strdup(msg_uid);
602 g_queue_push_tail (priv->queue, info);
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;
612 /* Notify status has changed */
613 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);