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 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
178 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
180 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
182 /* We cannot access the uid of the message here because it might
183 * just have been created. Wait for it being added to outbox. */
187 _add_message (ModestTnySendQueue *self, TnyHeader *header)
189 ModestWindowMgr *mgr = NULL;
190 ModestTnySendQueuePrivate *priv;
191 SendInfo *info = NULL;
192 GList* existing = NULL;
193 gchar* msg_uid = NULL;
194 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_WAITING;
195 gboolean editing = FALSE;
197 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
198 g_return_if_fail (TNY_IS_HEADER(header));
199 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
201 /* Check whether the mail is already in the queue */
202 msg_uid = modest_tny_send_queue_get_msg_id (header);
203 status = modest_tny_send_queue_get_msg_status (self, msg_uid);
205 case MODEST_TNY_SEND_QUEUE_UNKNONW:
206 case MODEST_TNY_SEND_QUEUE_SUSPENDED:
207 case MODEST_TNY_SEND_QUEUE_FAILED:
208 if (status != MODEST_TNY_SEND_QUEUE_SUSPENDED)
209 tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
211 /* Check if it already exists on queue */
212 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
213 if(existing != NULL) return;
215 /* Check if its being edited */
216 mgr = modest_runtime_get_window_mgr ();
217 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
220 /* Add new meesage info */
221 info = g_slice_new (SendInfo);
222 info->msg_id = strdup(msg_uid);
223 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
224 g_queue_push_tail (priv->queue, info);
236 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
239 TnyCamelTransportAccount *account;
241 g_return_val_if_fail (self, NULL);
243 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
245 g_printerr ("modest: no account for send queue\n");
248 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
249 TNY_FOLDER_TYPE_SENT);
250 g_object_unref (G_OBJECT(account));
257 modest_tny_send_queue_get_outbox (TnySendQueue *self)
260 TnyCamelTransportAccount *account;
262 g_return_val_if_fail (self, NULL);
264 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
266 g_printerr ("modest: no account for send queue\n");
269 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
270 TNY_FOLDER_TYPE_OUTBOX);
272 /* This vfunc's tinymail contract does not allow it to return NULL. */
274 g_warning("%s: Returning NULL.\n", __FUNCTION__);
277 g_object_unref (G_OBJECT(account));
283 modest_tny_send_queue_update(TnyFolderObserver *self,
284 TnyFolderChange *change)
286 ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
287 TnyHeader *header = NULL;
288 TnyMsg *msg = tny_folder_change_get_received_msg(change);
289 SendInfo *info = NULL;
290 GList* existing = NULL;
291 const gchar* msg_id = NULL;
292 TnyFolder* outbox = NULL;
296 outbox = tny_send_queue_get_outbox(TNY_SEND_QUEUE(self));
297 header = tny_msg_get_header(msg);
299 msg_id = tny_header_get_message_id (header);
300 g_assert(msg_id != NULL);
302 /* Put newly added message in WAITING state */
303 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
306 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
307 // info->status == MODEST_TNY_SEND_QUEUE_FAILED);
309 info = existing->data;
310 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
315 info = g_slice_new (SendInfo);
317 info->msg_id = strdup(msg_id);
318 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
319 g_queue_push_tail (priv->queue, info);
322 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
324 g_object_unref(G_OBJECT(msg));
325 g_object_unref(G_OBJECT(header));
326 g_object_unref(G_OBJECT(outbox));
331 modest_tny_send_queue_folder_observer_init(gpointer g, gpointer iface_data)
333 TnyFolderObserverIface *klass = (TnyFolderObserverIface *)g;
334 klass->update_func = modest_tny_send_queue_update;
338 modest_tny_send_queue_get_type (void)
340 static GType my_type = 0;
343 static const GTypeInfo my_info = {
344 sizeof(ModestTnySendQueueClass),
345 NULL, /* base init */
346 NULL, /* base finalize */
347 (GClassInitFunc) modest_tny_send_queue_class_init,
348 NULL, /* class finalize */
349 NULL, /* class data */
350 sizeof(ModestTnySendQueue),
352 (GInstanceInitFunc) modest_tny_send_queue_instance_init,
356 static const GInterfaceInfo tny_folder_observer_info = {
357 (GInterfaceInitFunc) modest_tny_send_queue_folder_observer_init,
362 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
363 "ModestTnySendQueue",
366 g_type_add_interface_static(my_type, TNY_TYPE_FOLDER_OBSERVER,
367 &tny_folder_observer_info);
374 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
376 GObjectClass *gobject_class;
378 gobject_class = (GObjectClass*) klass;
380 parent_class = g_type_class_peek_parent (klass);
381 gobject_class->finalize = modest_tny_send_queue_finalize;
383 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func = modest_tny_send_queue_add;
384 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func = modest_tny_send_queue_get_outbox;
385 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
386 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func = modest_tny_send_queue_cancel;
387 klass->status_changed = NULL;
389 signals[STATUS_CHANGED] =
390 g_signal_new ("status_changed",
391 G_TYPE_FROM_CLASS (gobject_class),
393 G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
395 modest_marshal_VOID__STRING_INT,
396 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
398 g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
402 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
404 ModestTnySendQueuePrivate *priv;
406 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
407 priv->queue = g_queue_new();
408 priv->current = NULL;
412 modest_tny_send_queue_finalize (GObject *obj)
414 ModestTnySendQueuePrivate *priv;
416 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
418 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
419 g_queue_free (priv->queue);
421 printf("Finalize called\n");
423 G_OBJECT_CLASS(parent_class)->finalize (obj);
427 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
429 ModestTnySendQueue *self;
431 g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
433 self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
435 tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
438 /* TODO: Does this produce cyclic references? */
439 tny_folder_add_observer(modest_tny_send_queue_get_outbox(TNY_SEND_QUEUE(self)),
440 TNY_FOLDER_OBSERVER(self));
442 /* Connect signals to control when a msg is being or has been sent */
443 g_signal_connect (G_OBJECT(self), "msg-sending",
444 G_CALLBACK(_on_msg_start_sending),
446 g_signal_connect (G_OBJECT(self), "msg-sent",
447 G_CALLBACK(_on_msg_has_been_sent),
449 g_signal_connect (G_OBJECT(self), "error-happened",
450 G_CALLBACK(_on_msg_error_happened),
458 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
460 TnyIterator *iter = NULL;
461 TnyFolder *outbox = NULL;
462 TnyList *headers = tny_simple_list_new ();
463 TnyHeader *header = NULL;
466 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
470 tny_folder_get_headers (outbox, headers, TRUE, &err);
474 iter = tny_list_create_iterator (headers);
475 while (!tny_iterator_is_done (iter)) {
476 header = TNY_HEADER (tny_iterator_get_current (iter));
478 _add_message (self, header);
479 g_object_unref (header);
482 tny_iterator_next (iter);
485 /* Flush send queue */
486 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
490 g_object_unref (G_OBJECT (headers));
492 g_object_unref (G_OBJECT (outbox));
494 g_object_unref (iter);
498 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
501 ModestTnySendQueueStatus status;
503 g_return_val_if_fail (msg_id != NULL, FALSE);
505 status = modest_tny_send_queue_get_msg_status (self, msg_id);
506 return status == MODEST_TNY_SEND_QUEUE_SENDING;
510 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
512 ModestTnySendQueuePrivate *priv;
513 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
515 return priv->current != NULL;
518 ModestTnySendQueueStatus
519 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
522 item = modest_tny_send_queue_lookup_info (self, msg_id);
523 if(item == NULL) return MODEST_TNY_SEND_QUEUE_UNKNONW;
524 return ((SendInfo*)item->data)->status;
528 modest_tny_send_queue_get_msg_id (TnyHeader *header)
530 const gchar *uid = NULL;
531 gchar* msg_uid = NULL;
534 /* Get message uid */
535 uid = tny_header_get_uid (header);
537 tmp = g_strsplit (uid, "__", 2);
541 msg_uid = g_strconcat (tmp[0], "_", NULL);
543 msg_uid = g_strdup(tmp[0]);
554 _on_msg_start_sending (TnySendQueue *self,
561 ModestTnySendQueuePrivate *priv = NULL;
563 SendInfo *info = NULL;
564 gchar *msg_id = NULL;
566 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
568 /* Get message uid */
569 msg_id = modest_tny_send_queue_get_msg_id (header);
571 /* Get status info */
572 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
573 g_return_if_fail (item != NULL);
575 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
577 /* Set current status item */
578 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
579 priv->current = item;
586 _on_msg_has_been_sent (TnySendQueue *self,
593 ModestTnySendQueuePrivate *priv;
595 gchar *msg_id = NULL;
597 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
599 /* Get message uid */
600 msg_id = modest_tny_send_queue_get_msg_id (header);
602 /* Get status info */
603 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
604 g_return_if_fail(item != NULL);
606 /* Remove status info */
607 modest_tny_send_queue_info_free (item->data);
608 g_queue_delete_link (priv->queue, item);
609 priv->current = NULL;
611 modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
618 _on_msg_error_happened (TnySendQueue *self,
624 ModestTnySendQueuePrivate *priv = NULL;
625 SendInfo *info = NULL;
627 gchar* msg_uid = NULL;
629 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
631 /* Get sending info (create new if it doesn not exist) */
632 msg_uid = modest_tny_send_queue_get_msg_id (header);
633 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),
636 info = g_slice_new (SendInfo);
637 info->msg_id = strdup(msg_uid);
638 g_queue_push_tail (priv->queue, info);
644 /* Keep in queue so that we remember that the opertion has failed */
645 /* and was not just cancelled */
646 info->status = MODEST_TNY_SEND_QUEUE_FAILED;
648 /* Notify status has changed */
649 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);