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-marshal.h"
32 #include "modest-mail-operation-queue.h"
33 #include "modest-runtime.h"
34 #include "modest-debug.h"
36 /* 'private'/'protected' functions */
37 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
38 static void modest_mail_operation_queue_init (ModestMailOperationQueue *obj);
39 static void modest_mail_operation_queue_finalize (GObject *obj);
42 on_operation_finished (ModestMailOperation *mail_op,
52 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
53 struct _ModestMailOperationQueuePrivate {
57 guint queue_empty_handler;
59 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60 MODEST_TYPE_MAIL_OPERATION_QUEUE, \
61 ModestMailOperationQueuePrivate))
63 static GObjectClass *parent_class = NULL;
65 static guint signals[NUM_SIGNALS] = {0};
68 modest_mail_operation_queue_get_type (void)
70 static GType my_type = 0;
72 static const GTypeInfo my_info = {
73 sizeof(ModestMailOperationQueueClass),
75 NULL, /* base finalize */
76 (GClassInitFunc) modest_mail_operation_queue_class_init,
77 NULL, /* class finalize */
78 NULL, /* class data */
79 sizeof(ModestMailOperationQueue),
81 (GInstanceInitFunc) modest_mail_operation_queue_init,
85 my_type = g_type_register_static (G_TYPE_OBJECT,
86 "ModestMailOperationQueue",
93 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
95 GObjectClass *gobject_class;
97 gobject_class = (GObjectClass*) klass;
98 parent_class = g_type_class_peek_parent (klass);
100 gobject_class->finalize = modest_mail_operation_queue_finalize;
102 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
105 * ModestMailOperationQueue::queue-changed
106 * @self: the #ModestMailOperationQueue that emits the signal
107 * @mail_op: the #ModestMailOperation affected
108 * @type: the type of change in the queue
109 * @user_data: user data set when the signal handler was connected
111 * Emitted whenever the contents of the queue change
113 signals[QUEUE_CHANGED_SIGNAL] =
114 g_signal_new ("queue-changed",
115 G_TYPE_FROM_CLASS (gobject_class),
117 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
119 modest_marshal_VOID__POINTER_INT,
120 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
123 * ModestMailOperationQueue::queue-empty
124 * @self: the #ModestMailOperationQueue that emits the signal
125 * @user_data: user data set when the signal handler was connected
127 * Issued whenever the queue is empty
129 signals[QUEUE_EMPTY_SIGNAL] =
130 g_signal_new ("queue-empty",
131 G_TYPE_FROM_CLASS (gobject_class),
133 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_empty),
135 g_cclosure_marshal_VOID__VOID,
141 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
143 ModestMailOperationQueuePrivate *priv;
145 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
147 priv->op_queue = g_queue_new ();
148 priv->queue_lock = g_mutex_new ();
150 priv->queue_empty_handler = 0;
154 print_queue_item (ModestMailOperation *op, const gchar* prefix)
156 gchar *op_str = modest_mail_operation_to_string (op);
158 prefix ? prefix : "",
164 on_finalize_foreach(gpointer op,
167 ModestMailOperationQueue *queue;
168 ModestMailOperationQueuePrivate *priv;
169 ModestMailOperation *mail_op;
171 queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
172 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
173 mail_op = MODEST_MAIL_OPERATION (op);
175 /* Simply remove from queue, but without emitting a
176 * QUEUE_CHANGED_SIGNAL because we are in finalize anyway and have
177 * the lock acquired. */
178 g_signal_handlers_disconnect_by_func (mail_op, G_CALLBACK (on_operation_finished), user_data);
180 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel/remove"););
182 modest_mail_operation_cancel (mail_op);
183 g_queue_remove (priv->op_queue, mail_op);
184 g_object_unref (G_OBJECT (mail_op));
188 modest_mail_operation_queue_finalize (GObject *obj)
190 ModestMailOperationQueuePrivate *priv;
192 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
194 g_mutex_lock (priv->queue_lock);
197 g_debug ("%s; items in queue: %d",
198 __FUNCTION__, g_queue_get_length (priv->op_queue));
199 g_queue_foreach (priv->op_queue, (GFunc)print_queue_item, "in queue");
203 if (priv->op_queue) {
205 if (!g_queue_is_empty (priv->op_queue)) {
206 g_queue_foreach (priv->op_queue,
207 (GFunc)on_finalize_foreach,
208 MODEST_MAIL_OPERATION_QUEUE (obj));
211 g_queue_free (priv->op_queue);
214 g_mutex_unlock (priv->queue_lock);
215 g_mutex_free (priv->queue_lock);
217 G_OBJECT_CLASS(parent_class)->finalize (obj);
220 ModestMailOperationQueue *
221 modest_mail_operation_queue_new (void)
223 ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
225 return MODEST_MAIL_OPERATION_QUEUE (self);
229 on_operation_finished (ModestMailOperation *mail_op,
232 ModestMailOperationQueue *queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
234 /* Remove operation from queue when finished */
235 modest_mail_operation_queue_remove (queue, mail_op);
239 modest_mail_operation_queue_add (ModestMailOperationQueue *self,
240 ModestMailOperation *mail_op)
242 ModestMailOperationQueuePrivate *priv;
244 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
245 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
247 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
249 g_mutex_lock (priv->queue_lock);
250 g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
251 g_mutex_unlock (priv->queue_lock);
253 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "add"););
255 /* Get notified when the operation ends to remove it from the
256 queue. We connect it using the *after* because we want to
257 let the other handlers for the finish function happen
259 g_signal_connect_after (G_OBJECT (mail_op),
260 "operation-finished",
261 G_CALLBACK (on_operation_finished),
264 /* Notify observers */
265 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
266 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
270 notify_queue_empty (gpointer user_data)
272 ModestMailOperationQueue *self = (ModestMailOperationQueue *) user_data;
273 ModestMailOperationQueuePrivate *priv;
276 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
278 g_mutex_lock (priv->queue_lock);
279 num_elements = priv->op_queue->length;
280 g_mutex_unlock (priv->queue_lock);
282 /* We re-check again that the queue is empty. It could happen
283 that we had issued a tny_send_queue_flush before the send
284 queue could add a mail operation to the queue as a response
285 to the "start-queue" signal, because that signal is issued
286 by tinymail in the main loop. Therefor it could happen that
287 we emit the queue-empty signal while a send-queue is still
288 waiting for the "start-queue" signal from tinymail, so the
289 send queue will never try to send the items because modest
290 is finalized before */
291 if (num_elements == 0) {
292 gdk_threads_enter ();
293 g_signal_emit (self, signals[QUEUE_EMPTY_SIGNAL], 0);
294 gdk_threads_leave ();
302 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
303 ModestMailOperation *mail_op)
305 ModestMailOperationQueuePrivate *priv;
306 ModestMailOperationStatus status;
309 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
310 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
312 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
314 g_mutex_lock (priv->queue_lock);
315 g_queue_remove (priv->op_queue, mail_op);
316 num_elements = priv->op_queue->length;
317 g_mutex_unlock (priv->queue_lock);
319 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "remove"););
321 g_signal_handlers_disconnect_by_func (G_OBJECT (mail_op),
322 G_CALLBACK (on_operation_finished),
325 /* Notify observers */
326 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
327 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
330 status = modest_mail_operation_get_status (mail_op);
331 if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
332 /* This is a sanity check. Shouldn't be needed, but
333 prevent possible application crashes. It's useful
334 also for detecting mail operations with invalid
335 status and error handling */
336 if (modest_mail_operation_get_error (mail_op) != NULL) {
337 modest_mail_operation_execute_error_handler (mail_op);
339 if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
340 g_warning ("%s: operation canceled \n", __FUNCTION__);
342 g_warning ("%s: possible error in a mail operation " \
343 "implementation. The status is not successful " \
344 "but the mail operation does not have any " \
345 "error set\n", __FUNCTION__);
350 g_object_unref (G_OBJECT (mail_op));
352 /* Emit the queue empty-signal. See the function to know why
353 we emit it in an idle */
354 if (num_elements == 0) {
355 if (priv->queue_empty_handler) {
356 g_source_remove (priv->queue_empty_handler);
357 priv->queue_empty_handler = 0;
359 priv->queue_empty_handler = g_idle_add_full (G_PRIORITY_LOW,
366 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
368 ModestMailOperationQueuePrivate *priv;
371 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
373 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
375 g_mutex_lock (priv->queue_lock);
376 length = g_queue_get_length (priv->op_queue);
377 g_mutex_unlock (priv->queue_lock);
383 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self,
384 ModestMailOperation *mail_op)
386 ModestMailOperationQueuePrivate *priv;
388 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
389 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
391 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
393 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
395 /* This triggers a progess_changed signal in which we remove
396 * the operation from the queue. */
397 modest_mail_operation_cancel (mail_op);
401 on_cancel_all_foreach (gpointer op, gpointer list)
405 new_list = (GSList**) list;
406 *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
410 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
412 ModestMailOperationQueuePrivate *priv;
413 GSList* operations_to_cancel = NULL;
416 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
418 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
420 /* Remember which operations to cancel. This is the only thing that
421 * is done while holding the lock, so we do not need to call
422 * functions from other files while holding the lock, which could
423 * lead to a deadlock if such a call re-enters into this queue and
424 * tries to acquire another lock. */
425 g_mutex_lock (priv->queue_lock);
426 g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
427 g_mutex_unlock (priv->queue_lock);
429 /* TODO: Reverse the list, to remove operations in order? */
431 for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
432 if (!MODEST_IS_MAIL_OPERATION(cur->data))
433 g_printerr ("modest: cur->data is not a valid mail operation\n");
435 modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
438 g_slist_free(operations_to_cancel);
448 on_find_by_source_foreach (gpointer op, gpointer data)
450 FindBySourceInfo *info = (FindBySourceInfo*) data;
452 if ( info->source == modest_mail_operation_get_source (MODEST_MAIL_OPERATION (op))) {
453 g_object_ref (G_OBJECT (op));
454 *(info->new_list) = g_slist_prepend (*(info->new_list), MODEST_MAIL_OPERATION (op));
459 modest_mail_operation_queue_get_by_source (
460 ModestMailOperationQueue *self,
463 ModestMailOperationQueuePrivate *priv;
464 GSList* found_operations= NULL;
465 FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
467 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
468 g_return_val_if_fail (source != NULL, NULL);
470 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
472 info->new_list = &found_operations;
473 info->source = source;
475 g_mutex_lock (priv->queue_lock);
476 g_queue_foreach (priv->op_queue, (GFunc) on_find_by_source_foreach, info);
477 g_mutex_unlock (priv->queue_lock);
481 return found_operations;
485 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
487 *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
492 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
496 ModestMailOperationQueuePrivate *priv;
498 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
500 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
502 len = g_queue_get_length (priv->op_queue);
503 str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
505 str = g_strdup_printf ("%s\n%s", str, "<empty>");
507 g_mutex_lock (priv->queue_lock);
508 g_queue_foreach (priv->op_queue, (GFunc)accumulate_mail_op_strings, &str);
509 g_mutex_unlock (priv->queue_lock);