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 <modest-debug.h>
44 #include <string.h> /* strcmp */
46 /* 'private'/'protected' functions */
47 static void modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass);
48 static void modest_tny_send_queue_finalize (GObject *obj);
49 static void modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class);
52 static void _on_msg_start_sending (TnySendQueue *self,
59 static void _on_msg_has_been_sent (TnySendQueue *self,
66 static void _on_msg_error_happened (TnySendQueue *self,
72 static TnyFolder* modest_tny_send_queue_get_outbox (TnySendQueue *self);
73 static TnyFolder* modest_tny_send_queue_get_sentbox (TnySendQueue *self);
83 typedef struct _SendInfo SendInfo;
86 ModestTnySendQueueStatus status;
89 typedef struct _ModestTnySendQueuePrivate ModestTnySendQueuePrivate;
90 struct _ModestTnySendQueuePrivate {
94 /* The info that is currently being sent */
98 #define MODEST_TNY_SEND_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99 MODEST_TYPE_TNY_SEND_QUEUE, \
100 ModestTnySendQueuePrivate))
103 static TnyCamelSendQueueClass *parent_class = NULL;
105 /* uncomment the following if you have defined any signals */
106 static guint signals[LAST_SIGNAL] = {0};
109 * this thread actually tries to send all the mails in the outbox and keeps
110 * track of their state.
114 on_modest_tny_send_queue_compare_id (gconstpointer info, gconstpointer msg_id)
116 g_return_val_if_fail (info && ((SendInfo*)info)->msg_id && msg_id, -1);
118 return strcmp( ((SendInfo*)info)->msg_id, msg_id);
122 modest_tny_send_queue_info_free (SendInfo *info)
124 g_free(info->msg_id);
125 g_slice_free(SendInfo, info);
129 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
131 ModestTnySendQueuePrivate *priv;
132 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
134 return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
139 queue_item_to_string (gpointer data, gchar **user_data)
141 SendInfo *info = (SendInfo*)data;
145 if (!(user_data && *user_data))
148 switch (info->status) {
149 case MODEST_TNY_SEND_QUEUE_UNKNOWN: status = "UNKNOWN"; break;
150 case MODEST_TNY_SEND_QUEUE_WAITING: status = "WAITING"; break;
151 case MODEST_TNY_SEND_QUEUE_SUSPENDED: status = "SUSPENDED"; break;
152 case MODEST_TNY_SEND_QUEUE_SENDING: status = "SENDING"; break;
153 case MODEST_TNY_SEND_QUEUE_FAILED: status = "FAILED"; break;
154 default: status= "UNEXPECTED"; break;
157 tmp = g_strdup_printf ("%s\"%s\" => [%s]\n",
158 *user_data, info->msg_id, status);
164 modest_tny_send_queue_to_string (ModestTnySendQueue *self)
167 ModestTnySendQueuePrivate *priv;
169 g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE(self), NULL);
170 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
172 str = g_strdup_printf ("items in the send queue: %d\n",
173 g_queue_get_length (priv->queue));
175 g_queue_foreach (priv->queue, (GFunc)queue_item_to_string, &str);
181 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
183 ModestTnySendQueuePrivate *priv;
185 TnyIterator *iter = NULL;
186 TnyFolder *outbox = NULL;
187 TnyList *headers = tny_simple_list_new ();
188 TnyHeader *header = NULL;
190 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
191 if(priv->current != NULL)
193 info = priv->current->data;
194 priv->current = NULL;
196 /* Keep in list until retry, so that we know that this was suspended
197 * by the user and not by an error. */
198 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
200 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
203 /* Set flags to supend sending operaiton (if removed, this is not necessary) */
205 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
207 g_warning ("%s: modest_tny_send_queue_get_outbox(..) returned NULL\n", __FUNCTION__);
210 tny_folder_get_headers (outbox, headers, TRUE, err);
211 if (err != NULL) goto frees;
212 iter = tny_list_create_iterator (headers);
213 while (!tny_iterator_is_done (iter)) {
214 header = TNY_HEADER (tny_iterator_get_current (iter));
216 tny_header_set_flag (header, TNY_HEADER_FLAG_SUSPENDED);
217 tny_iterator_next (iter);
218 g_object_unref (header);
222 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
223 g_queue_free (priv->queue);
224 priv->queue = g_queue_new();
227 /* Dont call super class implementaiton, becasue camel removes messages from outbox */
228 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
232 g_object_unref (G_OBJECT (headers));
234 g_object_unref (G_OBJECT (outbox));
236 g_object_unref (iter);
240 _on_added_to_outbox (TnySendQueue *self, gboolean cancelled, TnyMsg *msg, GError *err,
243 ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
244 TnyHeader *header = NULL;
245 SendInfo *info = NULL;
246 GList* existing = NULL;
247 gchar* msg_id = NULL;
249 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
250 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
252 header = tny_msg_get_header (msg);
253 msg_id = modest_tny_send_queue_get_msg_id (header);
254 /* msg_id = tny_header_get_message_id (header); */
255 g_return_if_fail(msg_id != NULL);
257 /* Put newly added message in WAITING state */
258 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
259 if(existing != NULL) {
260 info = existing->data;
261 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
263 info = g_slice_new (SendInfo);
264 info->msg_id = msg_id;
265 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
266 g_queue_push_tail (priv->queue, info);
269 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
271 g_object_unref(G_OBJECT(header));
275 modest_tny_send_queue_add (ModestTnySendQueue *self, TnyMsg *msg, GError **err)
277 g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE(self));
278 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
280 tny_camel_send_queue_add_async (TNY_CAMEL_SEND_QUEUE(self),
289 _add_message (ModestTnySendQueue *self, TnyHeader *header)
291 ModestWindowMgr *mgr = NULL;
292 ModestTnySendQueuePrivate *priv;
293 SendInfo *info = NULL;
294 GList* existing = NULL;
295 gchar* msg_uid = NULL;
296 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
297 gboolean editing = FALSE;
299 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
300 g_return_if_fail (TNY_IS_HEADER(header));
301 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
303 /* Check whether the mail is already in the queue */
304 msg_uid = modest_tny_send_queue_get_msg_id (header);
305 status = modest_tny_send_queue_get_msg_status (self, msg_uid);
307 case MODEST_TNY_SEND_QUEUE_UNKNOWN:
308 case MODEST_TNY_SEND_QUEUE_SUSPENDED:
309 case MODEST_TNY_SEND_QUEUE_FAILED:
311 /* Check if it already exists on queue */
312 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
316 /* Check if its being edited */
317 mgr = modest_runtime_get_window_mgr ();
318 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
322 /* Add new meesage info */
323 info = g_slice_new (SendInfo);
324 info->msg_id = strdup(msg_uid);
325 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
326 g_queue_push_tail (priv->queue, info);
337 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
340 TnyCamelTransportAccount *account;
342 g_return_val_if_fail (self, NULL);
344 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
346 g_printerr ("modest: no account for send queue\n");
349 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
350 TNY_FOLDER_TYPE_SENT);
351 g_object_unref (G_OBJECT(account));
358 modest_tny_send_queue_get_outbox (TnySendQueue *self)
361 TnyCamelTransportAccount *account;
363 g_return_val_if_fail (self, NULL);
365 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
367 g_printerr ("modest: no account for send queue\n");
370 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
371 TNY_FOLDER_TYPE_OUTBOX);
373 /* This vfunc's tinymail contract does not allow it to return NULL. */
375 g_warning("%s: Returning NULL.\n", __FUNCTION__);
378 g_object_unref (G_OBJECT(account));
384 modest_tny_send_queue_get_type (void)
386 static GType my_type = 0;
389 static const GTypeInfo my_info = {
390 sizeof(ModestTnySendQueueClass),
391 NULL, /* base init */
392 NULL, /* base finalize */
393 (GClassInitFunc) modest_tny_send_queue_class_init,
394 NULL, /* class finalize */
395 NULL, /* class data */
396 sizeof(ModestTnySendQueue),
398 (GInstanceInitFunc) modest_tny_send_queue_instance_init,
402 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
403 "ModestTnySendQueue",
411 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
413 GObjectClass *gobject_class;
415 gobject_class = (GObjectClass*) klass;
417 parent_class = g_type_class_peek_parent (klass);
418 gobject_class->finalize = modest_tny_send_queue_finalize;
420 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func = modest_tny_send_queue_get_outbox;
421 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
422 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func = modest_tny_send_queue_cancel;
423 klass->status_changed = NULL;
425 signals[STATUS_CHANGED] =
426 g_signal_new ("status_changed",
427 G_TYPE_FROM_CLASS (gobject_class),
429 G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
431 modest_marshal_VOID__STRING_INT,
432 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
434 g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
438 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
440 ModestTnySendQueuePrivate *priv;
442 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
443 priv->queue = g_queue_new();
444 priv->current = NULL;
448 modest_tny_send_queue_finalize (GObject *obj)
450 ModestTnySendQueuePrivate *priv;
452 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
454 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
455 g_queue_free (priv->queue);
457 G_OBJECT_CLASS(parent_class)->finalize (obj);
461 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
463 ModestTnySendQueue *self;
465 g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
467 self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
469 tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
472 /* Connect signals to control when a msg is being or has been sent */
473 g_signal_connect (G_OBJECT(self), "msg-sending",
474 G_CALLBACK(_on_msg_start_sending),
476 g_signal_connect (G_OBJECT(self), "msg-sent",
477 G_CALLBACK(_on_msg_has_been_sent),
479 g_signal_connect (G_OBJECT(self), "error-happened",
480 G_CALLBACK(_on_msg_error_happened),
488 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
490 TnyIterator *iter = NULL;
491 TnyFolder *outbox = NULL;
492 TnyList *headers = tny_simple_list_new ();
493 TnyHeader *header = NULL;
496 g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE(self));
498 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
502 tny_folder_get_headers (outbox, headers, TRUE, &err);
506 iter = tny_list_create_iterator (headers);
507 while (!tny_iterator_is_done (iter)) {
508 header = TNY_HEADER (tny_iterator_get_current (iter));
510 _add_message (self, header);
511 g_object_unref (header);
514 tny_iterator_next (iter);
517 /* Flush send queue */
518 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
522 g_object_unref (G_OBJECT (headers));
524 g_object_unref (G_OBJECT (outbox));
526 g_object_unref (iter);
530 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
533 ModestTnySendQueueStatus status;
535 g_return_val_if_fail (msg_id != NULL, FALSE);
537 status = modest_tny_send_queue_get_msg_status (self, msg_id);
538 return status == MODEST_TNY_SEND_QUEUE_SENDING;
542 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
544 ModestTnySendQueuePrivate *priv;
546 g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE(self), FALSE);
548 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
550 return priv->current != NULL;
553 ModestTnySendQueueStatus
554 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
558 g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE(self), MODEST_TNY_SEND_QUEUE_UNKNOWN);
559 g_return_val_if_fail (msg_id, MODEST_TNY_SEND_QUEUE_UNKNOWN);
561 item = modest_tny_send_queue_lookup_info (self, msg_id);
563 return MODEST_TNY_SEND_QUEUE_UNKNOWN;
565 return ((SendInfo*)item->data)->status;
569 modest_tny_send_queue_get_msg_id (TnyHeader *header)
571 gchar* msg_uid = NULL;
572 const gchar *subject;
573 time_t date_received;
575 g_return_val_if_fail (header && TNY_IS_HEADER(header), NULL);
577 /* Get message uid */
578 subject = tny_header_get_subject (header);
579 date_received = tny_header_get_date_received (header);
581 msg_uid = g_strdup_printf ("%s %d", subject, (int) date_received);
588 _on_msg_start_sending (TnySendQueue *self,
595 ModestTnySendQueuePrivate *priv = NULL;
597 SendInfo *info = NULL;
598 gchar *msg_id = NULL;
600 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
602 /* Get message uid */
603 msg_id = modest_tny_send_queue_get_msg_id (header);
605 /* Get status info */
606 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
608 g_warning ("%s: item (%s) should not be NULL",
609 __FUNCTION__, msg_id ? msg_id : "<none>");
612 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
614 /* Set current status item */
615 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
616 priv->current = item;
624 _on_msg_has_been_sent (TnySendQueue *self,
631 ModestTnySendQueuePrivate *priv;
632 gchar *msg_id = NULL;
635 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
637 /* Get message uid */
638 msg_id = modest_tny_send_queue_get_msg_id (header);
640 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
642 /* Get status info */
643 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
646 /* Remove status info */
647 modest_tny_send_queue_info_free (item->data);
648 g_queue_delete_link (priv->queue, item);
649 priv->current = NULL;
651 modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
659 _on_msg_error_happened (TnySendQueue *self,
665 ModestTnySendQueuePrivate *priv = NULL;
666 SendInfo *info = NULL;
668 gchar* msg_uid = NULL;
670 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
672 /* Get sending info (create new if it doesn not exist) */
673 msg_uid = modest_tny_send_queue_get_msg_id (header);
674 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),
677 info = g_slice_new (SendInfo);
678 info->msg_id = (msg_uid != NULL)? strdup(msg_uid) : NULL;
679 g_queue_push_tail (priv->queue, info);
683 /* Keep in queue so that we remember that the opertion has failed */
684 /* and was not just cancelled */
685 info->status = MODEST_TNY_SEND_QUEUE_FAILED;
686 priv->current = NULL;
688 /* Notify status has changed */
689 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
696 fill_list_of_caches (gpointer key, gpointer value, gpointer userdata)
698 GSList **send_queues = (GSList **) userdata;
699 *send_queues = g_slist_prepend (*send_queues, value);
702 ModestTnySendQueueStatus
703 modest_tny_all_send_queues_get_msg_status (TnyHeader *header)
705 ModestCacheMgr *cache_mgr = NULL;
706 GHashTable *send_queue_cache = NULL;
707 ModestTnyAccountStore *accounts_store = NULL;
708 TnyList *accounts = NULL;
709 TnyIterator *iter = NULL;
710 TnyTransportAccount *account = NULL;
711 GSList *send_queues = NULL, *node;
712 /* get_msg_status returns suspended by default, so we want to detect changes */
713 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
714 ModestTnySendQueueStatus queue_status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
715 gchar *msg_uid = NULL;
716 ModestTnySendQueue *send_queue = NULL;
718 g_return_val_if_fail (TNY_IS_HEADER(header), MODEST_TNY_SEND_QUEUE_UNKNOWN);
720 msg_uid = modest_tny_send_queue_get_msg_id (header);
721 cache_mgr = modest_runtime_get_cache_mgr ();
722 send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
723 MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
725 g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
726 if (send_queues == NULL) {
727 accounts = tny_simple_list_new ();
728 accounts_store = modest_runtime_get_account_store ();
729 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(accounts_store),
731 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
733 iter = tny_list_create_iterator (accounts);
734 while (!tny_iterator_is_done (iter)) {
735 account = TNY_TRANSPORT_ACCOUNT(tny_iterator_get_current (iter));
736 send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account));
737 g_object_unref(account);
739 queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
740 if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
741 status = queue_status;
744 tny_iterator_next (iter);
746 g_object_unref (iter);
747 g_object_unref (accounts);
750 for (node = send_queues; node != NULL; node = g_slist_next (node)) {
751 send_queue = MODEST_TNY_SEND_QUEUE (node->data);
753 queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
754 if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
755 status = queue_status;
762 g_slist_free (send_queues);