* Added ModestMailOperationQueue
[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-mail-operation-queue.h"
32
33 /* 'private'/'protected' functions */
34 static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass);
35 static void modest_mail_operation_queue_init       (ModestMailOperationQueue *obj);
36 static void modest_mail_operation_queue_finalize   (GObject *obj);
37
38 static GObject *modest_mail_operation_queue_constructor (GType type, guint n_construct_params,
39                                                          GObjectConstructParam *construct_params);
40
41 static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
42                                                                  ModestMailOperationQueue *op_queue);
43
44 static void modest_mail_operation_queue_cancel_no_block         (ModestMailOperationQueue *op_queue,
45                                                                  ModestMailOperation *mail_op);
46
47 /* list my signals  */
48 enum {
49         /* MY_SIGNAL_1, */
50         /* MY_SIGNAL_2, */
51         LAST_SIGNAL
52 };
53
54 typedef struct _ModestMailOperationQueuePrivate ModestMailOperationQueuePrivate;
55 struct _ModestMailOperationQueuePrivate {
56         GQueue *op_queue;
57         GMutex *queue_lock;
58 };
59 #define MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60                                                          MODEST_TYPE_MAIL_OPERATION_QUEUE, \
61                                                          ModestMailOperationQueuePrivate))
62 /* globals */
63 static GObjectClass *parent_class = NULL;
64 static ModestMailOperationQueue *singleton = NULL;
65
66 /* uncomment the following if you have defined any signals */
67 /* static guint signals[LAST_SIGNAL] = {0}; */
68
69 GType
70 modest_mail_operation_queue_get_type (void)
71 {
72         static GType my_type = 0;
73         if (!my_type) {
74                 static const GTypeInfo my_info = {
75                         sizeof(ModestMailOperationQueueClass),
76                         NULL,           /* base init */
77                         NULL,           /* base finalize */
78                         (GClassInitFunc) modest_mail_operation_queue_class_init,
79                         NULL,           /* class finalize */
80                         NULL,           /* class data */
81                         sizeof(ModestMailOperationQueue),
82                         1,              /* n_preallocs */
83                         (GInstanceInitFunc) modest_mail_operation_queue_init,
84                         NULL
85                 };
86
87                 my_type = g_type_register_static (G_TYPE_OBJECT,
88                                                   "ModestMailOperationQueue",
89                                                   &my_info, 0);
90         }
91         return my_type;
92 }
93
94 static void
95 modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass)
96 {
97         GObjectClass *gobject_class;
98
99         gobject_class = (GObjectClass*) klass;
100         parent_class  = g_type_class_peek_parent (klass);
101
102         gobject_class->finalize    = modest_mail_operation_queue_finalize;
103         gobject_class->constructor = modest_mail_operation_queue_constructor;
104
105         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate));
106 }
107
108 static void
109 modest_mail_operation_queue_init (ModestMailOperationQueue *obj)
110 {
111         ModestMailOperationQueuePrivate *priv;
112
113         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
114
115         priv->op_queue   = g_queue_new ();
116         priv->queue_lock = g_mutex_new ();
117 }
118
119 static GObject*
120 modest_mail_operation_queue_constructor (GType type, guint n_construct_params,
121                                          GObjectConstructParam *construct_params)
122 {
123         GObject *object;
124
125         if (!singleton) {
126                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
127                                 n_construct_params, construct_params);
128
129                 singleton = MODEST_MAIL_OPERATION_QUEUE (object);
130         } else {
131                 object = G_OBJECT (singleton);
132                 g_object_freeze_notify (G_OBJECT (singleton));
133         }
134
135         return object;
136 }
137
138 static void
139 modest_mail_operation_queue_finalize (GObject *obj)
140 {
141         ModestMailOperationQueuePrivate *priv;
142
143         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(obj);
144
145         g_mutex_lock (priv->queue_lock);
146
147         if (priv->op_queue) {
148                 if (!g_queue_is_empty (priv->op_queue))
149                         g_queue_foreach (priv->op_queue, (GFunc) g_object_unref, NULL);
150                 g_queue_free (priv->op_queue);
151         }
152
153         g_mutex_unlock (priv->queue_lock);
154         g_mutex_free (priv->queue_lock);
155         
156         G_OBJECT_CLASS(parent_class)->finalize (obj);
157 }
158
159 ModestMailOperationQueue *
160 modest_mail_operation_queue_get_instance (void)
161 {
162         ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL);
163
164         return MODEST_MAIL_OPERATION_QUEUE (self);
165 }
166
167 void 
168 modest_mail_operation_queue_add (ModestMailOperationQueue *op_queue, 
169                                  ModestMailOperation *mail_op)
170 {
171         ModestMailOperationQueuePrivate *priv;
172
173         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
174             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
175                 g_warning ("%s: bad parametters", G_GNUC_FUNCTION);
176                 return;
177         }
178
179         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
180
181         g_mutex_lock (priv->queue_lock);
182         g_queue_push_tail (priv->op_queue, g_object_ref (mail_op));
183         g_mutex_unlock (priv->queue_lock);
184 }
185
186 void 
187 modest_mail_operation_queue_remove (ModestMailOperationQueue *op_queue, 
188                                     ModestMailOperation *mail_op)
189 {
190         ModestMailOperationQueuePrivate *priv;
191
192         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
193             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
194                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
195                 return;
196         }
197
198         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
199
200         g_mutex_lock (priv->queue_lock);
201         g_queue_remove (priv->op_queue, mail_op);
202         g_mutex_unlock (priv->queue_lock);
203
204         g_object_unref (G_OBJECT (mail_op));
205 }
206
207
208 /* Utility function intended to be used with g_queue_foreach */
209 static void
210 modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op,
211                                                      ModestMailOperationQueue *op_queue)
212 {
213         modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
214 }
215
216 static void 
217 modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue,
218                                              ModestMailOperation *mail_op)
219 {
220         /* TODO: place here the cancel code */
221 }
222
223 void 
224 modest_mail_operation_queue_cancel (ModestMailOperationQueue *op_queue, 
225                                     ModestMailOperation *mail_op)
226 {
227         ModestMailOperationQueuePrivate *priv;
228         GList *iter;
229
230         if (!MODEST_IS_MAIL_OPERATION (mail_op) ||
231             !MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
232                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
233                 return;
234         }
235
236         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
237
238         g_mutex_lock (priv->queue_lock);
239         modest_mail_operation_queue_cancel_no_block (op_queue, mail_op);
240         g_mutex_unlock (priv->queue_lock);
241 }
242
243 void 
244 modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *op_queue)
245 {
246         ModestMailOperationQueuePrivate *priv;
247
248         if (!MODEST_IS_MAIL_OPERATION_QUEUE (op_queue)) {
249                 g_warning ("%s: invalid paramette", G_GNUC_FUNCTION);
250                 return;
251         }
252
253         priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE(op_queue);
254
255         g_mutex_lock (priv->queue_lock);
256         g_queue_foreach (priv->op_queue, 
257                          (GFunc) modest_mail_operation_queue_cancel_no_block_wrapper, 
258                          op_queue);
259         g_mutex_unlock (priv->queue_lock);
260 }