Add account preset object draft.
[modest] / experimental / modest / src / modest-tny-account-store.c
1 /* modest-tny-account-store.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <string.h>
6
7 #include <tny-account-store-iface.h>
8 #include <tny-account-iface.h>
9 #include <tny-account-store-iface.h>
10
11 #include <tny-account-iface.h>
12 #include <tny-store-account-iface.h>
13 #include <tny-transport-account-iface.h>
14
15 #include <tny-store-account.h>
16 #include <tny-transport-account.h>
17
18 #include "modest-account-mgr.h"
19 #include "modest-tny-account-store.h"
20
21 /* 'private'/'protected' functions */
22 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
23 static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
24 static void modest_tny_account_store_finalize     (GObject *obj);
25
26 /* implementations for tny-account-store-iface */
27 static void          modest_tny_account_store_iface_init              (gpointer g_iface, gpointer iface_data);
28
29 static gboolean      modest_tny_account_store_add_store_account       (TnyAccountStoreIface *self,
30                                                                        TnyStoreAccountIface *account);
31 static gboolean      modest_tny_account_store_add_transport_account   (TnyAccountStoreIface *self,
32                                                                        TnyTransportAccountIface *account);
33 static const GList*  modest_tny_account_store_get_store_accounts      (TnyAccountStoreIface *iface);
34 static const GList*  modest_tny_account_store_get_transport_accounts  (TnyAccountStoreIface *iface);
35
36 static gboolean destroy_all_accounts (ModestTnyAccountStore *self);
37 static gboolean get_all_accounts     (ModestTnyAccountStore *self);
38
39 /* list my signals */
40 enum {
41         /* MY_SIGNAL_1, */
42         /* MY_SIGNAL_2, */
43         LAST_SIGNAL
44 };
45
46 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
47 struct _ModestTnyAccountStorePrivate {
48         ModestAccountMgr *modest_acc_mgr;       
49         GList       *tny_transport_accounts;
50         GList       *tny_store_accounts;
51 };
52 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
54                                                       ModestTnyAccountStorePrivate))
55 /* globals */
56 static GObjectClass *parent_class = NULL;
57
58 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
60
61 GType
62 modest_tny_account_store_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestTnyAccountStoreClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_tny_account_store_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestTnyAccountStore),
74                         1,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_tny_account_store_init,
76                 };
77                 
78                 static const GInterfaceInfo iface_info = {
79                         (GInterfaceInitFunc) modest_tny_account_store_iface_init, 
80                         NULL,         /* interface_finalize */
81                         NULL          /* interface_data */
82                 };
83
84                 my_type = g_type_register_static (G_TYPE_OBJECT,
85                                                   "ModestTnyAccountStore", &my_info, 0);
86                 
87                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
88                                              &iface_info);
89
90
91                 
92         }
93         return my_type;
94 }
95
96 static void
97 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
98 {
99         GObjectClass *gobject_class;
100         gobject_class = (GObjectClass*) klass;
101
102         parent_class            = g_type_class_peek_parent (klass);
103         gobject_class->finalize = modest_tny_account_store_finalize;
104
105         g_type_class_add_private (gobject_class, sizeof(ModestTnyAccountStorePrivate));
106
107         /* signal definitions go here, e.g.: */
108 /*      signals[MY_SIGNAL_1] = */
109 /*              g_signal_new ("my_signal_1",....); */
110 /*      signals[MY_SIGNAL_2] = */
111 /*              g_signal_new ("my_signal_2",....); */
112 /*      etc. */
113 }
114
115 static void
116 modest_tny_account_store_init (ModestTnyAccountStore *obj)
117 {
118         ModestTnyAccountStorePrivate *priv =
119                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
120         
121         priv->modest_acc_mgr         = NULL;
122         priv->tny_transport_accounts = NULL;
123         priv->tny_store_accounts     = NULL;
124 }
125
126 static void
127 modest_tny_account_store_finalize (GObject *obj)
128 {
129         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
130         ModestTnyAccountStorePrivate *priv =
131                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
132         
133         if (priv->modest_acc_mgr) {
134                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
135                 priv->modest_acc_mgr = NULL;
136         }
137         
138         destroy_all_accounts (self);
139 }
140
141 GObject*
142 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
143 {
144         GObject *obj;
145         ModestTnyAccountStorePrivate *priv;
146
147         g_return_val_if_fail (modest_acc_mgr, NULL);
148         
149         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
150         
151         if (!get_all_accounts (MODEST_TNY_ACCOUNT_STORE(obj))) {
152                 g_warning ("could get accounts");
153                 g_object_unref (obj);
154                 return NULL;
155         }
156         
157         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
158         g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
159
160         return obj;
161 }
162
163
164 static void
165 destroy_account (gpointer account)
166 {
167         g_object_unref (G_OBJECT(account));
168 }
169
170
171 static gboolean
172 destroy_all_accounts (ModestTnyAccountStore *self)
173 {
174         ModestTnyAccountStorePrivate *priv;
175
176         g_return_val_if_fail (self, FALSE);
177
178         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
179         
180         /* clear up old stuff first */
181         if (priv->tny_transport_accounts) {
182                 g_list_foreach (priv->tny_transport_accounts,
183                                 (GFunc)destroy_account, NULL);
184                 g_list_free (priv->tny_transport_accounts);
185                 priv->tny_transport_accounts = NULL;
186
187         }
188         
189         if (priv->tny_store_accounts) {
190                 g_list_foreach (priv->tny_store_accounts,
191                                 (GFunc)destroy_account, NULL);
192                 g_list_free (priv->tny_store_accounts);
193                 priv->tny_store_accounts = NULL;
194         }
195
196         return TRUE;
197 }
198
199
200 /* FIXME: tinymail needs to change here */
201 /* a gpointer arg to get_password should be enough */
202 static gchar*
203 get_password (TnyAccountIface *account, const gchar *prompt)
204 {
205         /* don't want to create all these, but there's no other way right now */
206         ModestConf       *modest_conf;
207         ModestAccountMgr *modest_acc_mgr;
208         gchar            *pw = NULL;
209         const gchar      *account_name;
210
211         g_return_val_if_fail (account, NULL);
212
213         modest_conf    = MODEST_CONF(modest_conf_new ());
214         if (!modest_conf) {
215                 g_warning ("could not create conf");
216                 return NULL;
217         }
218         
219         modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
220         if (!modest_acc_mgr) {
221                 g_object_unref (modest_conf);
222                 g_warning ("could not create acc mgr");
223                 return NULL;
224         }
225
226         account_name = tny_account_iface_get_id(account);
227         if (!account_name) {
228                 g_object_unref (modest_acc_mgr);
229                 g_object_unref (modest_conf);
230                 g_warning ("could not retrieve account name");
231                 return NULL;
232         }
233         
234         pw = modest_account_mgr_get_account_string (modest_acc_mgr, account_name,
235                                                     MODEST_ACCOUNT_PASSWORD, NULL);
236                                      
237         g_object_unref (G_OBJECT(modest_conf));
238         g_object_unref (G_OBJECT(modest_acc_mgr));
239         
240         return pw;
241 }
242
243
244 static void
245 forget_password (TnyAccountIface *account)
246 {
247         g_warning (__FUNCTION__);
248 }
249
250
251
252 static gboolean
253 add_tny_account_from_account (ModestTnyAccountStore *self, const gchar* account_name)
254 {
255         gchar *type, *val;
256         TnyAccountIface *account_iface = NULL; 
257         ModestTnyAccountStorePrivate *priv;
258
259         g_return_val_if_fail (self, FALSE);
260         g_return_val_if_fail (account_name, FALSE);
261         
262         priv =  MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
263         
264         type = modest_account_mgr_get_account_string (priv->modest_acc_mgr,
265                                                       account_name,
266                                                       MODEST_ACCOUNT_TYPE, NULL);
267         if (!type) {
268                 g_warning ("error in account %s: type not defined", account_name);
269                 return FALSE;
270         }
271
272         if (strcmp (type, MODEST_ACCOUNT_TYPE_STORE) == 0) {
273                 account_iface = TNY_ACCOUNT_IFACE (tny_store_account_new ());
274                 priv->tny_store_accounts = g_list_append (priv->tny_store_accounts,
275                                                           account_iface);
276         
277         } else if (strcmp (type, MODEST_ACCOUNT_TYPE_TRANSPORT) == 0) {
278                 account_iface = TNY_ACCOUNT_IFACE (tny_transport_account_new ());
279                 priv->tny_transport_accounts = g_list_append (priv->tny_transport_accounts,
280                                                               account_iface);
281         } else {
282                 g_warning ("invalid account '%s': type: '%s'", account_name, type);
283                 g_free (type);
284                 return FALSE;
285         }
286
287         g_free (type);
288         tny_account_iface_set_id(account_iface, account_name);  
289         
290         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
291                                                      MODEST_ACCOUNT_PROTO,NULL);
292         tny_account_iface_set_proto(account_iface, val);
293         g_free (val);
294         
295         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
296                                                      MODEST_ACCOUNT_SERVER,NULL);
297         tny_account_iface_set_hostname(account_iface, val);
298         g_free (val);
299
300         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
301                                                      MODEST_ACCOUNT_USER,NULL);
302         tny_account_iface_set_user(account_iface, val); 
303         g_free (val);
304
305         tny_account_iface_set_pass_func(account_iface, get_password);   
306         tny_account_iface_set_forget_pass_func(account_iface, forget_password); 
307
308         return TRUE;
309 }
310
311
312
313 static gboolean
314 get_all_accounts (ModestTnyAccountStore *self)
315 {
316         ModestTnyAccountStorePrivate *priv;
317         GSList *account_names, *cursor;
318
319         g_return_if_fail (self);
320         
321         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
322         
323         destroy_all_accounts (self);    
324         
325         cursor = account_names = modest_account_mgr_account_names (priv->modest_acc_mgr,
326                                                                    NULL);
327         while (cursor) {
328                 const gchar *account_name = (const gchar*) cursor->data;
329                 if (!add_tny_account_from_account (self, account_name)) {
330                         g_warning ("cannot add iface for account %s",
331                                    account_name);
332                         return FALSE;
333                 }
334                 cursor = cursor->next;
335         }
336         return TRUE;
337 }
338
339
340
341 static gboolean
342 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
343                                              TnyStoreAccountIface *account)
344 {
345         TnyAccountIface       *account_iface;
346         ModestTnyAccountStore *account_store; 
347         ModestTnyAccountStorePrivate *priv;
348
349         const gchar* account_name; 
350         const gchar *hostname, *user, *proto;
351         gboolean check;
352         
353         g_return_if_fail (self);
354         g_return_if_fail (account);
355         
356         account_iface  = TNY_ACCOUNT_IFACE(account);
357         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
358         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
359
360         account_name   = tny_account_iface_get_id(account_iface);
361         if (!account_name) {
362                 g_warning ("failed to retrieve account name");
363                 return FALSE;
364         }
365
366         if (!modest_account_mgr_add_account (priv->modest_acc_mgr, account_name, NULL)) {
367                 g_warning ("failed to add account %s", account_name);
368                 return FALSE;
369         }
370
371         hostname =  tny_account_iface_get_hostname(account_iface);
372         user     =  tny_account_iface_get_user(account_iface);
373         proto    =  tny_account_iface_get_proto(account_iface);
374
375         if (!hostname || !user || !proto) {
376                 g_warning ("error in account data: hostname:%s; user:%s; proto:%s",
377                            hostname ? hostname : "<none>",
378                            user     ? user     : "<none>",
379                            proto    ? proto    : "<none>");
380                 return FALSE;
381         }
382         
383         check = modest_account_mgr_set_account_string (priv->modest_acc_mgr,
384                                                        account_name,MODEST_ACCOUNT_SERVER,
385                                                        hostname, NULL);
386         check = check && modest_account_mgr_set_account_string (priv->modest_acc_mgr,
387                                                                 account_name,MODEST_ACCOUNT_USER,
388                                                                 user, NULL);
389         check = check && modest_account_mgr_set_account_string (priv->modest_acc_mgr,
390                                                                 account_name, MODEST_ACCOUNT_PROTO,
391                                                                 proto, NULL);
392         if (!check)
393                 g_warning ("failed to set some account data");
394
395         return check;
396 }
397
398
399 static const GList*
400 modest_tny_account_store_get_store_accounts  (TnyAccountStoreIface *iface)
401 {
402         ModestTnyAccountStore *self;
403         ModestTnyAccountStorePrivate *priv;
404
405         g_return_val_if_fail (iface, NULL);
406         
407         self = MODEST_TNY_ACCOUNT_STORE(iface);
408         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
409         
410         if (!priv->tny_store_accounts)
411                 get_all_accounts (self);
412                 
413         return priv->tny_store_accounts;
414 }
415         
416
417 static gboolean
418 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
419                                                  TnyTransportAccountIface *account)
420 {               
421         g_return_if_fail (self);
422         g_return_if_fail (account);
423         
424         return modest_tny_account_store_add_transport_account (self, account);
425 }
426
427         
428 static const GList*
429 modest_tny_account_store_get_transport_accounts  (TnyAccountStoreIface *iface)
430 {
431         ModestTnyAccountStore        *self;
432         ModestTnyAccountStorePrivate *priv;
433
434         g_return_if_fail (iface);
435         
436         self = MODEST_TNY_ACCOUNT_STORE(iface);
437         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
438
439         if (!priv->tny_transport_accounts)
440                 get_all_accounts (self);
441         
442         return priv->tny_transport_accounts;
443 }
444
445
446 static void
447 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
448 {
449         TnyAccountStoreIfaceClass *klass;
450
451         g_return_if_fail (g_iface);
452         
453         klass = (TnyAccountStoreIfaceClass *)g_iface;
454
455         klass->add_store_account_func      = modest_tny_account_store_add_store_account;
456         klass->get_store_accounts_func     = modest_tny_account_store_get_store_accounts;
457         klass->add_transport_account_func  = modest_tny_account_store_add_transport_account;
458         klass->get_transport_accounts_func = modest_tny_account_store_get_transport_accounts;
459 }
460