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 g_return_val_if_fail (info && ((SendInfo*)info)->msg_id && msg_id, -1);
101 return strcmp( ((SendInfo*)info)->msg_id, msg_id);
105 modest_tny_send_queue_info_free(SendInfo *info)
107 g_free(info->msg_id);
108 g_slice_free(SendInfo, info);
112 modest_tny_send_queue_lookup_info (ModestTnySendQueue *self, const gchar *msg_id)
114 ModestTnySendQueuePrivate *priv;
115 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
117 return g_queue_find_custom (priv->queue, msg_id, on_modest_tny_send_queue_compare_id);
121 modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove, GError **err)
123 ModestTnySendQueuePrivate *priv;
125 TnyIterator *iter = NULL;
126 TnyFolder *outbox = NULL;
127 TnyList *headers = tny_simple_list_new ();
128 TnyHeader *header = NULL;
130 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
131 if(priv->current != NULL)
133 info = priv->current->data;
134 priv->current = NULL;
136 /* Keep in list until retry, so that we know that this was suspended
137 * by the user and not by an error. */
138 info->status = MODEST_TNY_SEND_QUEUE_SUSPENDED;
140 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
143 /* Set flags to supend sending operaiton (if removed, this is not necessary) */
145 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
146 tny_folder_get_headers (outbox, headers, TRUE, err);
147 if (err != NULL) goto frees;
148 iter = tny_list_create_iterator (headers);
149 while (!tny_iterator_is_done (iter)) {
150 header = TNY_HEADER (tny_iterator_get_current (iter));
152 tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
153 tny_header_set_flags (header, TNY_HEADER_FLAG_SUSPENDED_PRIORITY);
154 tny_iterator_next (iter);
155 g_object_unref (header);
159 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
160 g_queue_free (priv->queue);
161 priv->queue = g_queue_new();
164 /* Dont call super class implementaiton, becasue camel removes messages from outbox */
165 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove, err); /* FIXME */
169 g_object_unref (G_OBJECT (headers));
171 g_object_unref (G_OBJECT (outbox));
173 g_object_unref (iter);
177 modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg, GError **err)
179 ModestTnySendQueuePrivate *priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE(self);
180 TnyHeader *header = NULL;
181 SendInfo *info = NULL;
182 GList* existing = NULL;
183 const gchar* msg_id = NULL;
185 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
186 g_return_if_fail (TNY_IS_CAMEL_MSG(msg));
188 TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg, err); /* FIXME */
190 /* TODO: We cannot access the message ID of the message here because
191 * it might just have been created. Wait for it being added to outbox. */
193 header = tny_msg_get_header (msg);
194 msg_id = tny_header_get_message_id (header);
195 g_return_if_fail(msg_id != NULL);
197 /* Put newly added message in WAITING state */
198 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_id);
201 //g_assert(info->status == MODEST_TNY_SEND_QUEUE_SUSPENDED ||
202 // info->status == MODEST_TNY_SEND_QUEUE_FAILED);
204 info = existing->data;
205 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
210 info = g_slice_new (SendInfo);
212 info->msg_id = strdup(msg_id);
213 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
214 g_queue_push_tail (priv->queue, info);
217 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
219 g_object_unref(G_OBJECT(header));
223 _add_message (ModestTnySendQueue *self, TnyHeader *header)
225 ModestWindowMgr *mgr = NULL;
226 ModestTnySendQueuePrivate *priv;
227 SendInfo *info = NULL;
228 GList* existing = NULL;
229 gchar* msg_uid = NULL;
230 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_WAITING;
231 gboolean editing = FALSE;
233 g_return_if_fail (TNY_IS_SEND_QUEUE(self));
234 g_return_if_fail (TNY_IS_HEADER(header));
235 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
237 /* Check whether the mail is already in the queue */
238 msg_uid = modest_tny_send_queue_get_msg_id (header);
239 status = modest_tny_send_queue_get_msg_status (self, msg_uid);
241 case MODEST_TNY_SEND_QUEUE_UNKNONW:
242 case MODEST_TNY_SEND_QUEUE_SUSPENDED:
243 case MODEST_TNY_SEND_QUEUE_FAILED:
244 if (status != MODEST_TNY_SEND_QUEUE_SUSPENDED)
245 tny_header_unset_flags (header, TNY_HEADER_FLAG_PRIORITY);
247 /* Check if it already exists on queue */
248 existing = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE(self), msg_uid);
252 /* Check if its being edited */
253 mgr = modest_runtime_get_window_mgr ();
254 editing = modest_window_mgr_find_registered_header (mgr, header, NULL);
258 /* Add new meesage info */
259 info = g_slice_new (SendInfo);
260 info->msg_id = strdup(msg_uid);
261 info->status = MODEST_TNY_SEND_QUEUE_WAITING;
262 g_queue_push_tail (priv->queue, info);
273 modest_tny_send_queue_get_sentbox (TnySendQueue *self)
276 TnyCamelTransportAccount *account;
278 g_return_val_if_fail (self, NULL);
280 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
282 g_printerr ("modest: no account for send queue\n");
285 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
286 TNY_FOLDER_TYPE_SENT);
287 g_object_unref (G_OBJECT(account));
294 modest_tny_send_queue_get_outbox (TnySendQueue *self)
297 TnyCamelTransportAccount *account;
299 g_return_val_if_fail (self, NULL);
301 account = tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE(self));
303 g_printerr ("modest: no account for send queue\n");
306 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(account),
307 TNY_FOLDER_TYPE_OUTBOX);
309 /* This vfunc's tinymail contract does not allow it to return NULL. */
311 g_warning("%s: Returning NULL.\n", __FUNCTION__);
314 g_object_unref (G_OBJECT(account));
320 modest_tny_send_queue_get_type (void)
322 static GType my_type = 0;
325 static const GTypeInfo my_info = {
326 sizeof(ModestTnySendQueueClass),
327 NULL, /* base init */
328 NULL, /* base finalize */
329 (GClassInitFunc) modest_tny_send_queue_class_init,
330 NULL, /* class finalize */
331 NULL, /* class data */
332 sizeof(ModestTnySendQueue),
334 (GInstanceInitFunc) modest_tny_send_queue_instance_init,
338 my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE,
339 "ModestTnySendQueue",
347 modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass)
349 GObjectClass *gobject_class;
351 gobject_class = (GObjectClass*) klass;
353 parent_class = g_type_class_peek_parent (klass);
354 gobject_class->finalize = modest_tny_send_queue_finalize;
356 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func = modest_tny_send_queue_add;
357 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func = modest_tny_send_queue_get_outbox;
358 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox;
359 TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func = modest_tny_send_queue_cancel;
360 klass->status_changed = NULL;
362 signals[STATUS_CHANGED] =
363 g_signal_new ("status_changed",
364 G_TYPE_FROM_CLASS (gobject_class),
366 G_STRUCT_OFFSET (ModestTnySendQueueClass, status_changed),
368 modest_marshal_VOID__STRING_INT,
369 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
371 g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate));
375 modest_tny_send_queue_instance_init (GTypeInstance *instance, gpointer g_class)
377 ModestTnySendQueuePrivate *priv;
379 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (instance);
380 priv->queue = g_queue_new();
381 priv->current = NULL;
385 modest_tny_send_queue_finalize (GObject *obj)
387 ModestTnySendQueuePrivate *priv;
389 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (obj);
391 g_queue_foreach (priv->queue, (GFunc)modest_tny_send_queue_info_free, NULL);
392 g_queue_free (priv->queue);
394 G_OBJECT_CLASS(parent_class)->finalize (obj);
398 modest_tny_send_queue_new (TnyCamelTransportAccount *account)
400 ModestTnySendQueue *self;
402 g_return_val_if_fail (TNY_IS_CAMEL_TRANSPORT_ACCOUNT(account), NULL);
404 self = MODEST_TNY_SEND_QUEUE(g_object_new(MODEST_TYPE_TNY_SEND_QUEUE, NULL));
406 tny_camel_send_queue_set_transport_account (TNY_CAMEL_SEND_QUEUE(self),
409 /* Connect signals to control when a msg is being or has been sent */
410 g_signal_connect (G_OBJECT(self), "msg-sending",
411 G_CALLBACK(_on_msg_start_sending),
413 g_signal_connect (G_OBJECT(self), "msg-sent",
414 G_CALLBACK(_on_msg_has_been_sent),
416 g_signal_connect (G_OBJECT(self), "error-happened",
417 G_CALLBACK(_on_msg_error_happened),
425 modest_tny_send_queue_try_to_send (ModestTnySendQueue* self)
427 TnyIterator *iter = NULL;
428 TnyFolder *outbox = NULL;
429 TnyList *headers = tny_simple_list_new ();
430 TnyHeader *header = NULL;
433 outbox = modest_tny_send_queue_get_outbox (TNY_SEND_QUEUE(self));
437 tny_folder_get_headers (outbox, headers, TRUE, &err);
441 iter = tny_list_create_iterator (headers);
442 while (!tny_iterator_is_done (iter)) {
443 header = TNY_HEADER (tny_iterator_get_current (iter));
445 _add_message (self, header);
446 g_object_unref (header);
449 tny_iterator_next (iter);
452 /* Flush send queue */
453 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE(self));
457 g_object_unref (G_OBJECT (headers));
459 g_object_unref (G_OBJECT (outbox));
461 g_object_unref (iter);
465 modest_tny_send_queue_msg_is_being_sent (ModestTnySendQueue* self,
468 ModestTnySendQueueStatus status;
470 g_return_val_if_fail (msg_id != NULL, FALSE);
472 status = modest_tny_send_queue_get_msg_status (self, msg_id);
473 return status == MODEST_TNY_SEND_QUEUE_SENDING;
477 modest_tny_send_queue_sending_in_progress (ModestTnySendQueue* self)
479 ModestTnySendQueuePrivate *priv;
480 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
482 return priv->current != NULL;
485 ModestTnySendQueueStatus
486 modest_tny_send_queue_get_msg_status (ModestTnySendQueue *self, const gchar *msg_id)
489 item = modest_tny_send_queue_lookup_info (self, msg_id);
490 if(item == NULL) return MODEST_TNY_SEND_QUEUE_UNKNONW;
491 return ((SendInfo*)item->data)->status;
495 modest_tny_send_queue_get_msg_id (TnyHeader *header)
497 const gchar *uid = NULL;
498 gchar* msg_uid = NULL;
501 /* Get message uid */
502 uid = tny_header_get_uid (header);
504 tmp = g_strsplit (uid, "__", 2);
508 msg_uid = g_strconcat (tmp[0], "_", NULL);
510 msg_uid = g_strdup(tmp[0]);
521 _on_msg_start_sending (TnySendQueue *self,
528 ModestTnySendQueuePrivate *priv = NULL;
530 SendInfo *info = NULL;
531 gchar *msg_id = NULL;
533 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
535 /* Get message uid */
536 msg_id = modest_tny_send_queue_get_msg_id (header);
538 /* Get status info */
539 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
541 g_warning ("%s: item should not be NULL", __FUNCTION__);
544 info->status = MODEST_TNY_SEND_QUEUE_SENDING;
546 /* Set current status item */
547 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);
548 priv->current = item;
556 _on_msg_has_been_sent (TnySendQueue *self,
563 ModestTnySendQueuePrivate *priv;
565 gchar *msg_id = NULL;
567 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
569 /* Get message uid */
570 msg_id = modest_tny_send_queue_get_msg_id (header);
572 /* Get status info */
573 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self), msg_id);
575 g_warning ("%s: item should not be NULL", __FUNCTION__);
578 /* Remove status info */
579 modest_tny_send_queue_info_free (item->data);
580 g_queue_delete_link (priv->queue, item);
581 priv->current = NULL;
583 modest_platform_information_banner (NULL, NULL, _("mcen_ib_message_sent"));
591 _on_msg_error_happened (TnySendQueue *self,
597 ModestTnySendQueuePrivate *priv = NULL;
598 SendInfo *info = NULL;
600 gchar* msg_uid = NULL;
602 priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self);
604 /* Get sending info (create new if it doesn not exist) */
605 msg_uid = modest_tny_send_queue_get_msg_id (header);
606 item = modest_tny_send_queue_lookup_info (MODEST_TNY_SEND_QUEUE (self),
609 info = g_slice_new (SendInfo);
610 info->msg_id = strdup(msg_uid);
611 g_queue_push_tail (priv->queue, info);
615 /* Keep in queue so that we remember that the opertion has failed */
616 /* and was not just cancelled */
617 info->status = MODEST_TNY_SEND_QUEUE_FAILED;
619 /* Notify status has changed */
620 g_signal_emit (self, signals[STATUS_CHANGED], 0, info->msg_id, info->status);