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