* src/modest-tny-attachment.h:
[modest] / src / modest-tny-platform-factory.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 /* modest-tny-platform-factory.c */
31
32 #include "modest-tny-platform-factory.h"
33 #include "modest-tny-account-store.h"
34
35 /* 'private'/'protected' functions */
36 static void modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass);
37 static void modest_tny_platform_factory_init       (ModestTnyPlatformFactory *obj);
38 static void modest_tny_platform_factory_finalize   (GObject *obj);
39 static GObject *modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
40                                                          GObjectConstructParam *construct_params);
41 static void tny_platform_factory_init (gpointer g, gpointer iface_data);
42 static TnyAccountStore *modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self);
43 static TnyDevice *modest_tny_platform_factory_new_device (TnyPlatformFactory *self);
44 static TnyMsgView *modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self);
45 /* list my signals  */
46 enum {
47         /* MY_SIGNAL_1, */
48         /* MY_SIGNAL_2, */
49         LAST_SIGNAL
50 };
51
52 typedef struct _ModestTnyPlatformFactoryPrivate ModestTnyPlatformFactoryPrivate;
53 struct _ModestTnyPlatformFactoryPrivate {
54         ModestTnyAccountStore *account_store;
55         ModestConf *conf;
56         ModestAccountMgr *account_mgr;
57 };
58 #define MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59                                                          MODEST_TYPE_TNY_PLATFORM_FACTORY, \
60                                                          ModestTnyPlatformFactoryPrivate))
61 /* globals */
62 static GObjectClass *parent_class = NULL;
63 static ModestTnyPlatformFactory *singleton = NULL;
64
65 /* uncomment the following if you have defined any signals */
66 /* static guint signals[LAST_SIGNAL] = {0}; */
67
68 GType
69 modest_tny_platform_factory_get_type (void)
70 {
71         static GType my_type = 0;
72         if (!my_type) {
73                 static const GTypeInfo my_info = {
74                         sizeof(ModestTnyPlatformFactoryClass),
75                         NULL,           /* base init */
76                         NULL,           /* base finalize */
77                         (GClassInitFunc) modest_tny_platform_factory_class_init,
78                         NULL,           /* class finalize */
79                         NULL,           /* class data */
80                         sizeof(ModestTnyPlatformFactory),
81                         1,              /* n_preallocs */
82                         (GInstanceInitFunc) modest_tny_platform_factory_init,
83                         NULL
84                 };
85
86                 static const GInterfaceInfo tny_platform_factory_info = {
87                         (GInterfaceInitFunc) tny_platform_factory_init, /* interface_init */
88                         NULL,         /* interface_finalize */
89                         NULL          /* interface_data */
90                 };
91
92                 my_type = g_type_register_static (G_TYPE_OBJECT,
93                                                   "ModestTnyPlatformFactory",
94                                                   &my_info, 0);
95
96                 g_type_add_interface_static (my_type, TNY_TYPE_PLATFORM_FACTORY, 
97                                              &tny_platform_factory_info);
98         }
99         return my_type;
100 }
101
102 static void
103 modest_tny_platform_factory_class_init (ModestTnyPlatformFactoryClass *klass)
104 {
105         GObjectClass *gobject_class;
106
107         gobject_class = (GObjectClass*) klass;
108         parent_class            = g_type_class_peek_parent (klass);
109
110         gobject_class->finalize = modest_tny_platform_factory_finalize;
111         gobject_class->constructor = modest_tny_platform_factory_constructor;
112
113         g_type_class_add_private (gobject_class, sizeof(ModestTnyPlatformFactoryPrivate));
114 }
115
116 static void
117 modest_tny_platform_factory_init (ModestTnyPlatformFactory *obj)
118 {
119         /* Empty implementation */
120 }
121
122 static GObject*
123 modest_tny_platform_factory_constructor (GType type, guint n_construct_params,
124                                          GObjectConstructParam *construct_params)
125 {
126         GObject *object;
127
128         if (!singleton) {
129                 object = G_OBJECT_CLASS (parent_class)->constructor (type,
130                                 n_construct_params, construct_params);
131
132                 singleton = MODEST_TNY_PLATFORM_FACTORY (object);
133         } else {
134                 object = G_OBJECT (singleton);
135                 g_object_freeze_notify (G_OBJECT (singleton));
136         }
137
138         return object;
139 }
140
141 static void
142 modest_tny_platform_factory_finalize (GObject *obj)
143 {
144         ModestTnyPlatformFactoryPrivate *priv;
145
146         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj);
147
148         if (priv->account_mgr) {
149                 g_object_unref (priv->account_mgr);
150                 priv->account_mgr = NULL;
151         }
152
153         if (priv->conf) {
154                 g_object_unref (priv->conf);
155                 priv->conf = NULL;
156         }
157
158         if (priv->account_store) {
159                 g_object_unref (priv->account_store);
160                 priv->account_store = NULL;
161         }
162         
163         G_OBJECT_CLASS(parent_class)->finalize (obj);
164 }
165
166 static void
167 tny_platform_factory_init (gpointer g, gpointer iface_data)
168 {
169         TnyPlatformFactoryIface *klass = (TnyPlatformFactoryIface *)g;
170
171         klass->new_account_store_func = modest_tny_platform_factory_new_account_store;
172         klass->new_device_func = modest_tny_platform_factory_new_device;
173         klass->new_msg_view_func = modest_tny_platform_factory_new_msg_view;
174
175         return;
176 }
177
178 TnyPlatformFactory *
179 modest_tny_platform_factory_get_instance (void)
180 {
181         ModestTnyPlatformFactory *self = g_object_new (MODEST_TYPE_TNY_PLATFORM_FACTORY, NULL);
182
183         return TNY_PLATFORM_FACTORY (self);
184 }
185
186 static TnyAccountStore *
187 modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self)
188 {
189         ModestTnyPlatformFactoryPrivate *priv;
190
191         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(self);
192
193         if (!priv->account_store) {
194                 if (!priv->account_mgr)
195                         modest_tny_platform_factory_get_modest_account_mgr_instance (self);
196
197                 priv->account_store = modest_tny_account_store_new (priv->account_mgr);
198         }
199
200         return TNY_ACCOUNT_STORE (priv->account_store);
201 }
202
203 static TnyDevice *
204 modest_tny_platform_factory_new_device (TnyPlatformFactory *self)
205 {
206         /* FIXME: do not use GNOME */
207         return TNY_DEVICE (tny_gnome_device_new ());
208 }
209
210 static TnyMsgView*
211 modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self)
212 {
213         /* TODO */
214         return NULL;
215 }
216
217 ModestAccountMgr *
218 modest_tny_platform_factory_get_modest_account_mgr_instance (TnyPlatformFactory *fact)
219 {
220         ModestTnyPlatformFactoryPrivate *priv;
221
222         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
223
224         if (!priv->account_mgr) {
225                 if (!priv->conf)
226                         modest_tny_platform_factory_get_modest_conf_instance (fact);
227
228                 priv->account_mgr = modest_account_mgr_new (priv->conf);
229         }
230
231         return priv->account_mgr;
232 }
233
234 ModestConf *
235 modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact)
236 {
237         ModestTnyPlatformFactoryPrivate *priv;
238
239         priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact);
240
241         if (!priv->conf)
242                 priv->conf = modest_conf_new ();
243
244         return priv->conf;
245 }