4f8270e94d3f65afb90514695775e4e2b68fbc45
[modest] / src / modest-mail-operation-queue.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include "config.h"
31 #include "modest-marshal.h"
32 #include "modest-mail-operation-queue.h"
33 #include "modest-runtime.h"
34
35 /* 'private'/'protected' functions */
36 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
37 static void modest_mail_operation_queue_init       (ModestMailOperationQueue *obj);
38 static void modest_mail_operation_queue_finalize   (GObject *obj);
39
40 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
41                                                                  ModestMailOperationQueue *op_queue);
42
43 static void modest_mail_operation_queue_cancel_no_block         (ModestMailOperationQueue *op_queue,
44                                                                  ModestMailOperation *mail_op);
45
46 /* list my signals  */
47 enum {
48         QUEUE_CHANGED_SIGNAL,
49         NUM_SIGNALS
50 };
51
52 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
53 struct _ModestMailOperationQueuePrivate {
54         GQueue *op_queue;
55         GMutex *queue_lock;
56 };
57 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
58                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
59                                                          ModestMailOperationQueuePrivate))
60 /* globals */
61 static GObjectClass *parent_class = NULL;
62
63 static guint signals[NUM_SIGNALS] = {0};
64
65 GType
66 modest_mail_operation_queue_get_type (void)
67 {
68         static GType my_type = 0;
69         if (!my_type) {
70                 static const GTypeInfo my_info = {
71                         sizeof(ModestMailOperationQueueClass),
72                         NULL,           /* base init */
73                         NULL,           /* base finalize */
74                         (GClassInitFunc) modest_mail_operation_queue_class_init,
75                         NULL,           /* class finalize */
76                         NULL,           /* class data */
77                         sizeof(ModestMailOperationQueue),
78                         1,              /* n_preallocs */
79                         (GInstanceInitFunc) modest_mail_operation_queue_init,
80                         NULL
81                 };
82
83                 my_type = g_type_register_static (G_TYPE_OBJECT,
84                                                   "ModestMailOperationQueue",
85                                                   &my_info, 0);
86         }
87         return my_type;
88 }
89
90 static void
91 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
92 {
93         GObjectClass *gobject_class;
94
95         gobject_class = (GObjectClass*) klass;
96         parent_class  = g_type_class_peek_parent (klass);
97
98         gobject_class->finalize    = modest_mail_operation_queue_finalize;
99
100         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
101
102         /**
103          * ModestMailOperationQueue::queue-changed
104          * @self: the #ModestMailOperationQueue that emits the signal
105          * @mail_op: the #ModestMailOperation affected
106          * @type: the type of change in the queue
107          * @user_data: user data set when the signal handler was connected
108          *
109          * Emitted whenever the contents of the queue change
110          */
111         signals[QUEUE_CHANGED_SIGNAL] =
112                 g_signal_new ("queue-changed",
113                               G_TYPE_FROM_CLASS (gobject_class),
114                               G_SIGNAL_RUN_FIRST,
115                               G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_changed),
116                               NULL, NULL,
117                               modest_marshal_VOID__POINTER_INT,
118                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);
119 }
120
121 static void
122 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
123 {
124         ModestMailOperationQueuePrivate *priv;
125
126         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
127
128         priv->op_queue   = g_queue_new ();
129         priv->queue_lock = g_mutex_new ();
130 }
131
132 static void
133 modest_mail_operation_queue_finalize (GObject *obj)
134 {
135         ModestMailOperationQueuePrivate *priv;
136
137         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
138
139         g_mutex_lock (priv->queue_lock);
140
141         if (priv->op_queue) {
142                 /* Cancel all */
143                 if (!g_queue_is_empty (priv->op_queue))
144                         modest_mail_operation_queue_cancel_all (MODEST_MAIL_OPERATION_QUEUE (obj));
145                 g_queue_free (priv->op_queue);
146         }
147
148         g_mutex_unlock (priv->queue_lock);
149         g_mutex_free (priv->queue_lock);
150         
151         G_OBJECT_CLASS(parent_class)->finalize (obj);
152 }
153
154 ModestMailOperationQueue *
155 modest_mail_operation_queue_new (void)
156 {
157         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
158
159         return MODEST_MAIL_OPERATION_QUEUE (self);
160 }
161
162 void 
163 modest_mail_operation_queue_add (ModestMailOperationQueue *self, 
164                                  ModestMailOperation *mail_op)
165 {
166         ModestMailOperationQueuePrivate *priv;
167
168         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
169         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
170         
171         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
172
173         g_mutex_lock (priv->queue_lock);
174         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
175         g_mutex_unlock (priv->queue_lock);
176
177         /* Notify observers */
178         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
179                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED);
180 }
181
182 void 
183 modest_mail_operation_queue_remove (ModestMailOperationQueue *self, 
184                                     ModestMailOperation *mail_op)
185 {
186         ModestMailOperationQueuePrivate *priv;
187
188         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
189         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
190
191         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
192
193         g_mutex_lock (priv->queue_lock);
194         g_queue_remove (priv->op_queue, mail_op);
195         g_mutex_unlock (priv->queue_lock);
196
197         /* HACK see the documentation of the function. Remove this
198            call when tinymail provides accurate progress values */
199         _modest_mail_operation_notify_end (mail_op);
200
201         /* Notify observers */
202         g_signal_emit (self, signals[QUEUE_CHANGED_SIGNAL], 0,
203                        mail_op, MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED);
204
205         /* Free object */
206         modest_runtime_verify_object_last_ref (mail_op, "");
207         g_object_unref (G_OBJECT (mail_op));
208 }
209
210
211 /* Utility function intended to be used with g_queue_foreach */
212 static void
213 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *self,
214                                                      ModestMailOperationQueue *op_queue)
215 {
216         modest_mail_operation_queue_cancel_no_block (op_queue, self);
217 }
218
219 static void 
220 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *self,
221                                              ModestMailOperation *mail_op)
222 {
223         if (modest_mail_operation_is_finished (mail_op))
224                 return;
225
226         /* TODO: the implementation is still empty */
227         modest_mail_operation_cancel (mail_op);
228
229         /* Remove from the queue */
230         modest_mail_operation_queue_remove (self, mail_op);
231 }
232
233 void 
234 modest_mail_operation_queue_cancel (ModestMailOperationQueue *self, 
235                                     ModestMailOperation *mail_op)
236 {
237         ModestMailOperationQueuePrivate *priv;
238
239         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
240         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
241
242         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
243
244         g_mutex_lock (priv->queue_lock);
245         modest_mail_operation_queue_cancel_no_block (self, mail_op);
246         g_mutex_unlock (priv->queue_lock);
247 }
248
249 void 
250 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *self)
251 {
252         ModestMailOperationQueuePrivate *priv;
253
254         g_return_if_fail (MODEST_IS_MAIL_OPERATION_QUEUE (self));
255
256         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(self);
257
258         g_mutex_lock (priv->queue_lock);
259         g_queue_foreach (priv->op_queue, 
260                          (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper, 
261                          self);
262         g_mutex_unlock (priv->queue_lock);
263 }