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 status != MODEST_MAIL_OPERATION_STATUS_INVALID) {
333 /* This is a sanity check. Shouldn't be needed, but
334 prevent possible application crashes. It's useful
335 also for detecting mail operations with invalid
336 status and error handling */
337 if (modest_mail_operation_get_error (mail_op) != NULL) {
338 modest_mail_operation_execute_error_handler (mail_op);
340 if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
341 g_warning ("%s: operation canceled \n", __FUNCTION__);
343 g_warning ("%s: possible error in a mail operation " \
344 "implementation. The status is not successful " \
345 "but the mail operation does not have any " \
346 "error set\n", __FUNCTION__);
351 g_object_unref (G_OBJECT (mail_op));
353 /* Emit the queue empty-signal. See the function to know why
354 we emit it in an idle */
355 if (num_elements == 0) {
356 if (priv->queue_empty_handler) {
357 g_source_remove (priv->queue_empty_handler);
358 priv->queue_empty_handler = 0;
360 priv->queue_empty_handler = g_idle_add_full (G_PRIORITY_LOW,
367 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
369 ModestMailOperationQueuePrivate *priv;
372 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
374 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
376 g_mutex_lock (priv->queue_lock);
377 length = g_queue_get_length (priv->op_queue);
378 g_mutex_unlock (priv->queue_lock);
384 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self,
385 ModestMailOperation *mail_op)
387 ModestMailOperationQueuePrivate *priv;
389 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
390 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
392 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
394 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
396 /* This triggers a progess_changed signal in which we remove
397 * the operation from the queue. */
398 modest_mail_operation_cancel (mail_op);
402 on_cancel_all_foreach (gpointer op, gpointer list)
406 new_list = (GSList**) list;
407 *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
411 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
413 ModestMailOperationQueuePrivate *priv;
414 GSList* operations_to_cancel = NULL;
417 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
419 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
421 /* Remember which operations to cancel. This is the only thing that
422 * is done while holding the lock, so we do not need to call
423 * functions from other files while holding the lock, which could
424 * lead to a deadlock if such a call re-enters into this queue and
425 * tries to acquire another lock. */
426 g_mutex_lock (priv->queue_lock);
427 g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
428 g_mutex_unlock (priv->queue_lock);
430 /* TODO: Reverse the list, to remove operations in order? */
432 for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
433 if (!MODEST_IS_MAIL_OPERATION(cur->data))
434 g_printerr ("modest: cur->data is not a valid mail operation\n");
436 modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
439 g_slist_free(operations_to_cancel);
449 on_find_by_source_foreach (gpointer op, gpointer data)
451 FindBySourceInfo *info = (FindBySourceInfo*) data;
453 if ( info->source == modest_mail_operation_get_source (MODEST_MAIL_OPERATION (op))) {
454 g_object_ref (G_OBJECT (op));
455 *(info->new_list) = g_slist_prepend (*(info->new_list), MODEST_MAIL_OPERATION (op));
460 modest_mail_operation_queue_get_by_source (
461 ModestMailOperationQueue *self,
464 ModestMailOperationQueuePrivate *priv;
465 GSList* found_operations= NULL;
466 FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
468 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
469 g_return_val_if_fail (source != NULL, NULL);
471 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
473 info->new_list = &found_operations;
474 info->source = source;
476 g_mutex_lock (priv->queue_lock);
477 g_queue_foreach (priv->op_queue, (GFunc) on_find_by_source_foreach, info);
478 g_mutex_unlock (priv->queue_lock);
482 return found_operations;
486 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
488 *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
493 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
497 ModestMailOperationQueuePrivate *priv;
499 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
501 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
503 len = g_queue_get_length (priv->op_queue);
504 str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
506 str = g_strdup_printf ("%s\n%s", str, "<empty>");
508 g_mutex_lock (priv->queue_lock);
509 g_queue_foreach (priv->op_queue, (GFunc)accumulate_mail_op_strings, &str);
510 g_mutex_unlock (priv->queue_lock);