* all:
[modest] / src / modest-singletons.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 "modest-singletons.h"
31
32 /* 'private'/'protected' functions */
33 static void modest_singletons_class_init (ModestSingletonsClass *klass);
34 static void modest_singletons_init       (ModestSingletons *obj);
35 static void modest_singletons_finalize   (GObject *obj);
36
37 typedef struct _ModestSingletonsPrivate ModestSingletonsPrivate;
38 struct _ModestSingletonsPrivate {
39         ModestConf                *conf;
40         ModestAccountMgr          *account_mgr;
41         ModestTnyAccountStore     *account_store;
42         ModestCacheMgr            *cache_mgr;   
43         ModestMailOperationQueue  *mail_op_queue;
44         ModestWidgetFactory       *widget_factory;
45 };
46 #define MODEST_SINGLETONS_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
47                                                MODEST_TYPE_SINGLETONS, \
48                                                ModestSingletonsPrivate))
49 /* globals */
50 static GObjectClass *parent_class = NULL;
51
52 GType
53 modest_singletons_get_type (void)
54 {
55         static GType my_type = 0;
56         if (!my_type) {
57                 static const GTypeInfo my_info = {
58                         sizeof(ModestSingletonsClass),
59                         NULL,           /* base init */
60                         NULL,           /* base finalize */
61                         (GClassInitFunc) modest_singletons_class_init,
62                         NULL,           /* class finalize */
63                         NULL,           /* class data */
64                         sizeof(ModestSingletons),
65                         1,              /* n_preallocs */
66                         (GInstanceInitFunc) modest_singletons_init,
67                         NULL
68                 };
69                 my_type = g_type_register_static (G_TYPE_OBJECT,
70                                                   "ModestSingletons",
71                                                   &my_info, 0);
72         }
73         return my_type;
74 }
75
76 static void
77 modest_singletons_class_init (ModestSingletonsClass *klass)
78 {
79         GObjectClass *gobject_class;
80         gobject_class = (GObjectClass*) klass;
81
82         parent_class            = g_type_class_peek_parent (klass);
83         gobject_class->finalize = modest_singletons_finalize;
84
85         g_type_class_add_private (gobject_class, sizeof(ModestSingletonsPrivate));
86 }
87
88 static void
89 modest_singletons_init (ModestSingletons *obj)
90 {
91         ModestSingletonsPrivate *priv;
92         priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
93
94         priv->conf           = NULL;
95         priv->account_mgr    = NULL;
96         priv->account_store  = NULL;
97         priv->cache_mgr      = NULL;
98         priv->mail_op_queue  = NULL;
99         priv->widget_factory = NULL;
100         
101         priv->conf           = modest_conf_new ();
102         if (!priv->conf) {
103                 g_printerr ("modest: cannot create modest conf instance\n");
104                 return;
105         }
106
107         priv->account_mgr    = modest_account_mgr_new (priv->conf);
108         if (!priv->account_mgr) {
109                 g_printerr ("modest: cannot create modest account mgr instance\n");
110                 return;
111         }
112
113         priv->account_store  = modest_tny_account_store_new (priv->account_mgr);
114         if (!priv->account_store) {
115                 g_printerr ("modest: cannot create modest tny account store instance\n");
116                 return;
117         }
118
119         priv->cache_mgr     = modest_cache_mgr_new ();
120         if (!priv->cache_mgr) {
121                 g_printerr ("modest: cannot create modest cache mgr instance\n");
122                 return;
123         }
124
125         priv->mail_op_queue  = modest_mail_operation_queue_new ();
126         if (!priv->mail_op_queue) {
127                 g_printerr ("modest: cannot create modest mail operation queue instance\n");
128                 return;
129         }
130
131         /* don't initialize widget_factory here, but do it lazily, so we can
132          * instaniatie modest-singletons before gtk_init
133          */
134 }
135
136 static void
137 modest_singletons_finalize (GObject *obj)
138 {
139         ModestSingletonsPrivate *priv;
140         priv = MODEST_SINGLETONS_GET_PRIVATE(obj);
141
142         if (priv->conf) {
143                 g_object_unref (G_OBJECT(priv->conf));
144                 priv->conf = NULL;
145         }
146         
147         if (priv->account_mgr) {
148                 g_object_unref (G_OBJECT(priv->account_mgr));
149                 priv->account_mgr = NULL;
150         }
151         if (priv->account_store) {
152                 g_object_unref (G_OBJECT(priv->account_store));
153                 priv->account_store = NULL;
154         }
155
156         if (priv->cache_mgr) {
157                 g_object_unref (G_OBJECT(priv->cache_mgr));
158                 priv->cache_mgr = NULL;
159         }
160
161         if (priv->widget_factory) {
162                 g_object_unref (G_OBJECT(priv->widget_factory));
163                 priv->widget_factory = NULL;
164         }
165         
166         G_OBJECT_CLASS(parent_class)->finalize (obj);
167 }
168
169 ModestSingletons*
170 modest_singletons_new (void)
171 {
172         ModestSingletonsPrivate *priv;
173         ModestSingletons *self;
174         
175         self = MODEST_SINGLETONS(g_object_new(MODEST_TYPE_SINGLETONS, NULL));
176         priv = MODEST_SINGLETONS_GET_PRIVATE(self);
177
178         /* widget_factory will still be NULL, as it is initialized lazily */
179         if (!(priv->conf && priv->account_mgr && priv->account_store &&
180               priv->cache_mgr && priv->mail_op_queue)) {
181                 g_printerr ("modest: failed to create singletons instance\n");
182                 g_object_unref (G_OBJECT(self));
183                 self = NULL;
184         }
185         
186         return self;
187 }
188
189
190 ModestConf*
191 modest_singletons_get_conf (ModestSingletons *self)
192 {
193         g_return_val_if_fail (self, NULL);
194         return MODEST_SINGLETONS_GET_PRIVATE(self)->conf;
195 }
196
197 ModestAccountMgr*
198 modest_singletons_get_account_mgr (ModestSingletons *self)
199 {
200         g_return_val_if_fail (self, NULL);
201         return MODEST_SINGLETONS_GET_PRIVATE(self)->account_mgr;
202 }
203
204 ModestTnyAccountStore*
205 modest_singletons_get_account_store (ModestSingletons *self)
206 {
207         g_return_val_if_fail (self, NULL);
208         return MODEST_SINGLETONS_GET_PRIVATE(self)->account_store;
209 }
210
211 ModestCacheMgr*
212 modest_singletons_get_cache_mgr (ModestSingletons *self)
213 {
214         g_return_val_if_fail (self, NULL);
215         return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr;
216 }
217
218
219 ModestMailOperationQueue*
220 modest_singletons_get_mail_operation_queue (ModestSingletons *self)
221 {
222         g_return_val_if_fail (self, NULL);
223         return MODEST_SINGLETONS_GET_PRIVATE(self)->mail_op_queue;
224 }
225
226
227 ModestWidgetFactory*
228 modest_singletons_get_widget_factory (ModestSingletons *self)
229 {
230         ModestSingletonsPrivate *priv;
231
232         g_return_val_if_fail (self, NULL);
233
234         priv = MODEST_SINGLETONS_GET_PRIVATE(self);
235
236         if (G_UNLIKELY(!priv->widget_factory))  
237                 priv->widget_factory = modest_widget_factory_new (priv->account_store);
238         if (G_UNLIKELY(!priv->widget_factory)) {
239                 g_printerr ("modest: cannot create modest widget factory instance\n");
240                 return NULL;
241         }
242         return priv->widget_factory;
243 }