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 print_queue_item (gpointer data, gpointer user_data)
131 SendInfo *info = (SendInfo*)data;
134 switch (info->status) {
135 case MODEST_TNY_SEND_QUEUE_UNKNOWN: status = "UNKNOWN"; break;
136 case MODEST_TNY_SEND_QUEUE_WAITING: status = "WAITING"; break;
137 case MODEST_TNY_SEND_QUEUE_SUSPENDED: status = "SUSPENDED"; break;
138 case MODEST_TNY_SEND_QUEUE_SENDING: status = "SENDING"; break;
139 case MODEST_TNY_SEND_QUEUE_FAILED: status = "FAILED"; break;
140 default: status= "UNEXPECTED"; break;
143 g_debug ("\"%s\" => [%s]", info->msg_id, status);
147 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
149 ModestTnySendQueuePrivate *priv;
150 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
153 g_debug ("items in the send queue (%d):",
154 g_queue_get_length (priv->queue));
155 g_queue_foreach (priv->queue, print_queue_item, NULL);
158 return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
162 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
164 ModestTnySendQueuePrivate *priv;
166 TnyIterator *iter = NULL;
167 TnyFolder *outbox = NULL;
168 TnyList *headers = tny_simple_list_new ();
169 TnyHeader *header = NULL;
171 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
172 if(priv->current != NULL)
174 info = priv->current->data;
175 priv->current = NULL;
177 /* Keep in list until retry, so that we know that this was suspended
178 * by the user and not by an error. */
179 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
181 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
184 /* Set flags to supend sending operaiton (if removed, this is not necessary) */
186 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
188 g_warning ("%s: modest_tny_send_queue_get_outbox(..) returned NULL\n", __FUNCTION__);
191 tny_folder_get_headers (outbox, headers, TRUE, err);
192 if (err != NULL) goto frees;
193 iter = tny_list_create_iterator (headers);
194 while (!tny_iterator_is_done (iter)) {
195 header = TNY_HEADER (tny_iterator_get_current (iter));
197 tny_header_set_flag (header, TNY_HEADER_FLAG_SUSPENDED);
198 tny_iterator_next (iter);
199 g_object_unref (header);
203 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
204 g_queue_free (priv->queue);
205 priv->queue = g_queue_new();
208 /* Dont call super class implementaiton, becasue camel removes messages from outbox */
209 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
213 g_object_unref (G_OBJECT (headers));
215 g_object_unref (G_OBJECT (outbox));
217 g_object_unref (iter);
221 _on_added_to_outbox (TnySendQueue *self, gboolean cancelled, TnyMsg *msg, GError *err, gpointer user_data)
223 ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
224 TnyHeader *header = NULL;
225 SendInfo *info = NULL;
226 GList* existing = NULL;
227 const gchar* msg_id = NULL;
229 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
230 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
232 header = tny_msg_get_header (msg);
233 msg_id = modest_tny_send_queue_get_msg_id (header);
234 /* msg_id = tny_header_get_message_id (header); */
235 g_return_if_fail(msg_id != NULL);
237 /* Put newly added message in WAITING state */
238 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
241 info = existing->data;
242 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
247 info = g_slice_new (SendInfo);
249 info->msg_id = strdup(msg_id);
250 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
251 g_queue_push_tail (priv->queue, info);
254 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
256 g_object_unref(G_OBJECT(header));
260 modest_tny_send_queue_add (ModestTnySendQueue *self, TnyMsg *msg, GError **err)
262 g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE(self));
263 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
265 tny_camel_send_queue_add_async (TNY_CAMEL_SEND_QUEUE(self),
274 _add_message (ModestTnySendQueue *self, TnyHeader *header)
276 ModestWindowMgr *mgr = NULL;
277 ModestTnySendQueuePrivate *priv;
278 SendInfo *info = NULL;
279 GList* existing = NULL;
280 gchar* msg_uid = NULL;
281 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
282 gboolean editing = FALSE;
284 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
285 g_return_if_fail (TNY_IS_HEADER(header));
286 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
288 /* Check whether the mail is already in the queue */
289 msg_uid = modest_tny_send_queue_get_msg_id (header);
290 status = modest_tny_send_queue_get_msg_status (self, msg_uid);
292 case MODEST_TNY_SEND_QUEUE_UNKNOWN:
293 case MODEST_TNY_SEND_QUEUE_SUSPENDED:
294 case MODEST_TNY_SEND_QUEUE_FAILED:
296 /* Check if it already exists on queue */
297 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
301 /* Check if its being edited */
302 mgr = modest_runtime_get_window_mgr ();
303 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
307 /* Add new meesage info */
308 info = g_slice_new (SendInfo);
309 info->msg_id = strdup(msg_uid);
310 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
311 g_queue_push_tail (priv->queue, info);
322 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
325 TnyCamelTransportAccount *account;
327 g_return_val_if_fail (self, NULL);
329 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
331 g_printerr ("modest: no account for send queue\n");
334 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
335 TNY_FOLDER_TYPE_SENT);
336 g_object_unref (G_OBJECT(account));
343 modest_tny_send_queue_get_outbox (TnySendQueue *self)
346 TnyCamelTransportAccount *account;
348 g_return_val_if_fail (self, NULL);
350 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
352 g_printerr ("modest: no account for send queue\n");
355 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
356 TNY_FOLDER_TYPE_OUTBOX);
358 /* This vfunc's tinymail contract does not allow it to return NULL. */
360 g_warning("%s: Returning NULL.\n", __FUNCTION__);
363 g_object_unref (G_OBJECT(account));
369 modest_tny_send_queue_get_type (void)
371 static GType my_type = 0;
374 static const GTypeInfo my_info = {
375 sizeof(ModestTnySendQueueClass),
376 NULL, /* base init */
377 NULL, /* base finalize */
378 (GClassInitFunc) modest_tny_send_queue_class_init,
379 NULL, /* class finalize */
380 NULL, /* class data */
381 sizeof(ModestTnySendQueue),
383 (GInstanceInitFunc) modest_tny_send_queue_instance_init,
387 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
388 "ModestTnySendQueue",
396 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
398 GObjectClass *gobject_class;
400 gobject_class = (GObjectClass*) klass;
402 parent_class = g_type_class_peek_parent (klass);
403 gobject_class->finalize = modest_tny_send_queue_finalize;
405 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func = modest_tny_send_queue_get_outbox;
406 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
407 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func = modest_tny_send_queue_cancel;
408 klass->status_changed = NULL;
410 signals[STATUS_CHANGED] =
411 g_signal_new ("status_changed",
412 G_TYPE_FROM_CLASS (gobject_class),
414 G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
416 modest_marshal_VOID__STRING_INT,
417 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
419 g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
423 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
425 ModestTnySendQueuePrivate *priv;
427 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
428 priv->queue = g_queue_new();
429 priv->current = NULL;
433 modest_tny_send_queue_finalize (GObject *obj)
435 ModestTnySendQueuePrivate *priv;
437 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
439 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
440 g_queue_free (priv->queue);
442 G_OBJECT_CLASS(parent_class)->finalize (obj);
446 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
448 ModestTnySendQueue *self;
450 g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
452 self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
454 tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
457 /* Connect signals to control when a msg is being or has been sent */
458 g_signal_connect (G_OBJECT(self), "msg-sending",
459 G_CALLBACK(_on_msg_start_sending),
461 g_signal_connect (G_OBJECT(self), "msg-sent",
462 G_CALLBACK(_on_msg_has_been_sent),
464 g_signal_connect (G_OBJECT(self), "error-happened",
465 G_CALLBACK(_on_msg_error_happened),
473 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
475 TnyIterator *iter = NULL;
476 TnyFolder *outbox = NULL;
477 TnyList *headers = tny_simple_list_new ();
478 TnyHeader *header = NULL;
481 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
485 tny_folder_get_headers (outbox, headers, TRUE, &err);
489 iter = tny_list_create_iterator (headers);
490 while (!tny_iterator_is_done (iter)) {
491 header = TNY_HEADER (tny_iterator_get_current (iter));
493 _add_message (self, header);
494 g_object_unref (header);
497 tny_iterator_next (iter);
500 /* Flush send queue */
501 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
505 g_object_unref (G_OBJECT (headers));
507 g_object_unref (G_OBJECT (outbox));
509 g_object_unref (iter);
513 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
516 ModestTnySendQueueStatus status;
518 g_return_val_if_fail (msg_id != NULL, FALSE);
520 status = modest_tny_send_queue_get_msg_status (self, msg_id);
521 return status == MODEST_TNY_SEND_QUEUE_SENDING;
525 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
527 ModestTnySendQueuePrivate *priv;
528 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
530 return priv->current != NULL;
533 ModestTnySendQueueStatus
534 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
537 item = modest_tny_send_queue_lookup_info (self, msg_id);
538 if(item == NULL) return MODEST_TNY_SEND_QUEUE_UNKNOWN;
539 return ((SendInfo*)item->data)->status;
543 modest_tny_send_queue_get_msg_id (TnyHeader *header)
545 gchar* msg_uid = NULL;
546 const gchar *subject;
547 time_t date_received;
549 /* Get message uid */
550 subject = tny_header_get_subject (header);
551 date_received = tny_header_get_date_received (header);
553 msg_uid = g_strdup_printf ("%s %d", subject, (int) date_received);
560 _on_msg_start_sending (TnySendQueue *self,
567 ModestTnySendQueuePrivate *priv = NULL;
569 SendInfo *info = NULL;
570 gchar *msg_id = NULL;
572 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
574 /* Get message uid */
575 msg_id = modest_tny_send_queue_get_msg_id (header);
577 /* Get status info */
578 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
580 g_warning ("%s: item (%s) should not be NULL",
581 __FUNCTION__, msg_id ? msg_id : "<none>");
584 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
586 /* Set current status item */
587 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
588 priv->current = item;
596 _on_msg_has_been_sent (TnySendQueue *self,
603 ModestTnySendQueuePrivate *priv;
604 gchar *msg_id = NULL;
606 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
608 /* Get message uid */
609 msg_id = modest_tny_send_queue_get_msg_id (header);
611 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
613 /* Get status info */
614 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
617 /* Remove status info */
618 modest_tny_send_queue_info_free (item->data);
619 g_queue_delete_link (priv->queue, item);
620 priv->current = NULL;
622 modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
630 _on_msg_error_happened (TnySendQueue *self,
636 ModestTnySendQueuePrivate *priv = NULL;
637 SendInfo *info = NULL;
639 gchar* msg_uid = NULL;
641 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
643 /* Get sending info (create new if it doesn not exist) */
644 msg_uid = modest_tny_send_queue_get_msg_id (header);
645 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),
648 info = g_slice_new (SendInfo);
649 info->msg_id = (msg_uid != NULL)? strdup(msg_uid) : NULL;
650 g_queue_push_tail (priv->queue, info);
654 /* Keep in queue so that we remember that the opertion has failed */
655 /* and was not just cancelled */
656 info->status = MODEST_TNY_SEND_QUEUE_FAILED;
657 priv->current = NULL;
659 /* Notify status has changed */
660 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
667 fill_list_of_caches (gpointer key, gpointer value, gpointer userdata)
669 GSList **send_queues = (GSList **) userdata;
670 *send_queues = g_slist_prepend (*send_queues, value);
673 ModestTnySendQueueStatus
674 modest_tny_all_send_queues_get_msg_status (TnyHeader *header)
676 ModestCacheMgr *cache_mgr = NULL;
677 GHashTable *send_queue_cache = NULL;
678 ModestTnyAccountStore *accounts_store = NULL;
679 TnyList *accounts = NULL;
680 TnyIterator *iter = NULL;
681 TnyTransportAccount *account = NULL;
682 GSList *send_queues = NULL, *node;
683 /* get_msg_status returns suspended by default, so we want to detect changes */
684 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
685 ModestTnySendQueueStatus queue_status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
686 gchar *msg_uid = NULL;
687 ModestTnySendQueue *send_queue = NULL;
689 msg_uid = modest_tny_send_queue_get_msg_id (header);
690 cache_mgr = modest_runtime_get_cache_mgr ();
691 send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
692 MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
694 g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
695 if (send_queues == NULL) {
696 accounts = tny_simple_list_new ();
697 accounts_store = modest_runtime_get_account_store ();
698 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(accounts_store),
700 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
702 iter = tny_list_create_iterator (accounts);
703 while (!tny_iterator_is_done (iter)) {
704 account = TNY_TRANSPORT_ACCOUNT(tny_iterator_get_current (iter));
705 send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account));
706 g_object_unref(account);
708 queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
709 if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
710 status = queue_status;
713 tny_iterator_next (iter);
715 g_object_unref (iter);
716 g_object_unref (accounts);
719 for (node = send_queues; node != NULL; node = g_slist_next (node)) {
720 send_queue = MODEST_TNY_SEND_QUEUE (node->data);
722 queue_status = modest_tny_send_queue_get_msg_status (send_queue, msg_uid);
723 if (queue_status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
724 status = queue_status;
731 g_slist_free (send_queues);