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