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 {
58 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59 MODEST_TYPE_MAIL_OPERATION_QUEUE, \
60 ModestMailOperationQueuePrivate))
62 static GObjectClass *parent_class = NULL;
64 static guint signals[NUM_SIGNALS] = {0};
67 modest_mail_operation_queue_get_type (void)
69 static GType my_type = 0;
71 static const GTypeInfo my_info = {
72 sizeof(ModestMailOperationQueueClass),
74 NULL, /* base finalize */
75 (GClassInitFunc) modest_mail_operation_queue_class_init,
76 NULL, /* class finalize */
77 NULL, /* class data */
78 sizeof(ModestMailOperationQueue),
80 (GInstanceInitFunc) modest_mail_operation_queue_init,
84 my_type = g_type_register_static (G_TYPE_OBJECT,
85 "ModestMailOperationQueue",
92 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
94 GObjectClass *gobject_class;
96 gobject_class = (GObjectClass*) klass;
97 parent_class = g_type_class_peek_parent (klass);
99 gobject_class->finalize = modest_mail_operation_queue_finalize;
101 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
104 * ModestMailOperationQueue::queue-changed
105 * @self: the #ModestMailOperationQueue that emits the signal
106 * @mail_op: the #ModestMailOperation affected
107 * @type: the type of change in the queue
108 * @user_data: user data set when the signal handler was connected
110 * Emitted whenever the contents of the queue change
112 signals[QUEUE_CHANGED_SIGNAL] =
113 g_signal_new ("queue-changed",
114 G_TYPE_FROM_CLASS (gobject_class),
116 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
118 modest_marshal_VOID__POINTER_INT,
119 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
122 * ModestMailOperationQueue::queue-empty
123 * @self: the #ModestMailOperationQueue that emits the signal
124 * @user_data: user data set when the signal handler was connected
126 * Issued whenever the queue is empty
128 signals[QUEUE_EMPTY_SIGNAL] =
129 g_signal_new ("queue-empty",
130 G_TYPE_FROM_CLASS (gobject_class),
132 G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_empty),
134 g_cclosure_marshal_VOID__VOID,
140 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
142 ModestMailOperationQueuePrivate *priv;
144 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
146 priv->op_queue = g_queue_new ();
147 priv->queue_lock = g_mutex_new ();
152 print_queue_item (ModestMailOperation *op, const gchar* prefix)
154 gchar *op_str = modest_mail_operation_to_string (op);
156 prefix ? prefix : "",
162 on_finalize_foreach(gpointer op,
165 ModestMailOperationQueue *queue;
166 ModestMailOperationQueuePrivate *priv;
167 ModestMailOperation *mail_op;
169 queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
170 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
171 mail_op = MODEST_MAIL_OPERATION (op);
173 /* Simply remove from queue, but without emitting a
174 * QUEUE_CHANGED_SIGNAL because we are in finalize anyway and have
175 * the lock acquired. */
176 g_signal_handlers_disconnect_by_func (mail_op, G_CALLBACK (on_operation_finished), user_data);
178 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel/remove"););
180 modest_mail_operation_cancel (mail_op);
181 g_queue_remove (priv->op_queue, mail_op);
182 g_object_unref (G_OBJECT (mail_op));
186 modest_mail_operation_queue_finalize (GObject *obj)
188 ModestMailOperationQueuePrivate *priv;
190 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
192 g_mutex_lock (priv->queue_lock);
195 g_debug ("%s; items in queue: %d",
196 __FUNCTION__, g_queue_get_length (priv->op_queue));
197 g_queue_foreach (priv->op_queue, (GFunc)print_queue_item, "in queue");
201 if (priv->op_queue) {
203 if (!g_queue_is_empty (priv->op_queue)) {
204 g_queue_foreach (priv->op_queue,
205 (GFunc)on_finalize_foreach,
206 MODEST_MAIL_OPERATION_QUEUE (obj));
209 g_queue_free (priv->op_queue);
212 g_mutex_unlock (priv->queue_lock);
213 g_mutex_free (priv->queue_lock);
215 G_OBJECT_CLASS(parent_class)->finalize (obj);
218 ModestMailOperationQueue *
219 modest_mail_operation_queue_new (void)
221 ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
223 return MODEST_MAIL_OPERATION_QUEUE (self);
227 on_operation_finished (ModestMailOperation *mail_op,
230 ModestMailOperationQueue *queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
232 /* Remove operation from queue when finished */
233 modest_mail_operation_queue_remove (queue, mail_op);
237 modest_mail_operation_queue_add (ModestMailOperationQueue *self,
238 ModestMailOperation *mail_op)
240 ModestMailOperationQueuePrivate *priv;
242 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
243 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
245 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
247 g_mutex_lock (priv->queue_lock);
248 g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
249 g_mutex_unlock (priv->queue_lock);
251 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "add"););
253 /* Get notified when the operation ends to remove it from the
254 queue. We connect it using the *after* because we want to
255 let the other handlers for the finish function happen
257 g_signal_connect_after (G_OBJECT (mail_op),
258 "operation-finished",
259 G_CALLBACK (on_operation_finished),
262 /* Notify observers */
263 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
264 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
268 modest_mail_operation_queue_remove (ModestMailOperationQueue *self,
269 ModestMailOperation *mail_op)
271 ModestMailOperationQueuePrivate *priv;
272 ModestMailOperationStatus status;
274 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
275 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
277 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
279 g_mutex_lock (priv->queue_lock);
280 g_queue_remove (priv->op_queue, mail_op);
281 g_mutex_unlock (priv->queue_lock);
283 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "remove"););
285 g_signal_handlers_disconnect_by_func (G_OBJECT (mail_op),
286 G_CALLBACK (on_operation_finished),
289 /* Notify observers */
290 g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
291 mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
294 status = modest_mail_operation_get_status (mail_op);
295 if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
296 /* This is a sanity check. Shouldn't be needed, but
297 prevent possible application crashes. It's useful
298 also for detecting mail operations with invalid
299 status and error handling */
300 if (modest_mail_operation_get_error (mail_op) != NULL) {
301 modest_mail_operation_execute_error_handler (mail_op);
303 if (status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
304 g_warning ("%s: operation canceled \n", __FUNCTION__);
306 g_warning ("%s: possible error in a mail operation " \
307 "implementation. The status is not successful " \
308 "but the mail operation does not have any " \
309 "error set\n", __FUNCTION__);
315 /* We do not own the last reference when this operation is deleted
316 * as response to a progress changed signal from the mail operation
317 * itself, in which case the glib signal system owns a reference
318 * until the signal emission is complete. armin. */
319 /* modest_runtime_verify_object_last_ref (mail_op, ""); */
320 g_object_unref (G_OBJECT (mail_op));
322 /* Emit the queue empty-signal */
323 g_signal_emit (self, signals[QUEUE_EMPTY_SIGNAL], 0);
327 modest_mail_operation_queue_num_elements (ModestMailOperationQueue *self)
329 ModestMailOperationQueuePrivate *priv;
332 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), 0);
334 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
336 g_mutex_lock (priv->queue_lock);
337 length = g_queue_get_length (priv->op_queue);
338 g_mutex_unlock (priv->queue_lock);
344 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self,
345 ModestMailOperation *mail_op)
347 ModestMailOperationQueuePrivate *priv;
349 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
350 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
352 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
354 MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel"););
356 /* This triggers a progess_changed signal in which we remove
357 * the operation from the queue. */
358 modest_mail_operation_cancel (mail_op);
362 on_cancel_all_foreach (gpointer op, gpointer list)
366 new_list = (GSList**) list;
367 *new_list = g_slist_prepend (*new_list, MODEST_MAIL_OPERATION (op));
371 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
373 ModestMailOperationQueuePrivate *priv;
374 GSList* operations_to_cancel = NULL;
377 g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
379 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
381 /* Remember which operations to cancel. This is the only thing that
382 * is done while holding the lock, so we do not need to call
383 * functions from other files while holding the lock, which could
384 * lead to a deadlock if such a call re-enters into this queue and
385 * tries to acquire another lock. */
386 g_mutex_lock (priv->queue_lock);
387 g_queue_foreach (priv->op_queue, (GFunc) on_cancel_all_foreach, &operations_to_cancel);
388 g_mutex_unlock (priv->queue_lock);
390 /* TODO: Reverse the list, to remove operations in order? */
392 for(cur = operations_to_cancel; cur != NULL; cur = cur->next) {
393 if (!MODEST_IS_MAIL_OPERATION(cur->data))
394 g_printerr ("modest: cur->data is not a valid mail operation\n");
396 modest_mail_operation_cancel (MODEST_MAIL_OPERATION (cur->data));
399 g_slist_free(operations_to_cancel);
409 on_find_by_source_foreach (gpointer op, gpointer data)
411 FindBySourceInfo *info = (FindBySourceInfo*) data;
413 if ( info->source == modest_mail_operation_get_source (MODEST_MAIL_OPERATION (op))) {
414 g_object_ref (G_OBJECT (op));
415 *(info->new_list) = g_slist_prepend (*(info->new_list), MODEST_MAIL_OPERATION (op));
420 modest_mail_operation_queue_get_by_source (
421 ModestMailOperationQueue *self,
424 ModestMailOperationQueuePrivate *priv;
425 GSList* found_operations= NULL;
426 FindBySourceInfo *info = g_new0 (FindBySourceInfo, 1);
428 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
429 g_return_val_if_fail (source != NULL, NULL);
431 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
433 info->new_list = &found_operations;
434 info->source = source;
436 g_mutex_lock (priv->queue_lock);
437 g_queue_foreach (priv->op_queue, (GFunc) on_find_by_source_foreach, info);
438 g_mutex_unlock (priv->queue_lock);
442 return found_operations;
446 accumulate_mail_op_strings (ModestMailOperation *op, gchar **str)
448 *str = g_strdup_printf ("%s\n%s", *str, modest_mail_operation_to_string (op));
453 modest_mail_operation_queue_to_string (ModestMailOperationQueue *self)
457 ModestMailOperationQueuePrivate *priv;
459 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self), NULL);
461 priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
463 len = g_queue_get_length (priv->op_queue);
464 str = g_strdup_printf ("mail operation queue (%02d)\n-------------------------", len);
466 str = g_strdup_printf ("%s\n%s", str, "<empty>");
468 g_mutex_lock (priv->queue_lock);
469 g_queue_foreach (priv->op_queue, (GFunc)accumulate_mail_op_strings, &str);
470 g_mutex_unlock (priv->queue_lock);