* fixed get_account_keyname(), not adding MODEST_ACCOUNT_NAMESPACE to keys anymore
[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-iface.h>
8 #include <tny-account-store-iface.h>
9 #include <tny-store-account-iface.h>
10 #include <tny-transport-account-iface.h>
11
12 #include <tny-store-account.h>
13 #include <tny-transport-account.h>
14
15 #include "modest-account-mgr.h"
16 #include "modest-tny-account-store.h"
17
18 /* 'private'/'protected' functions */
19 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
20 static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
21 static void modest_tny_account_store_finalize     (GObject *obj);
22
23 /* implementations for tny-account-store-iface */
24 static void    modest_tny_account_store_iface_init              (gpointer g_iface, gpointer iface_data);
25
26 static void    modest_tny_account_store_add_store_account       (TnyAccountStoreIface *self,
27                                                                TnyStoreAccountIface *account);
28 static void    modest_tny_account_store_add_transport_account   (TnyAccountStoreIface *self,
29                                                                        TnyTransportAccountIface *account);
30 static const GList*  modest_tny_account_store_get_store_accounts      (TnyAccountStoreIface *iface);
31 static const GList*  modest_tny_account_store_get_transport_accounts  (TnyAccountStoreIface *iface);
32
33 /* list my signals */
34 enum {
35         /* MY_SIGNAL_1, */
36         /* MY_SIGNAL_2, */
37         LAST_SIGNAL
38 };
39
40 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
41 struct _ModestTnyAccountStorePrivate {
42         ModestAccountMgr *modest_acc_mgr;
43 };
44 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
45                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
46                                                       ModestTnyAccountStorePrivate))
47 /* globals */
48 static GObjectClass *parent_class = NULL;
49
50 /* uncomment the following if you have defined any signals */
51 /* static guint signals[LAST_SIGNAL] = {0}; */
52
53 GType
54 modest_tny_account_store_get_type (void)
55 {
56         static GType my_type = 0;
57         if (!my_type) {
58                 static const GTypeInfo my_info = {
59                         sizeof(ModestTnyAccountStoreClass),
60                         NULL,           /* base init */
61                         NULL,           /* base finalize */
62                         (GClassInitFunc) modest_tny_account_store_class_init,
63                         NULL,           /* class finalize */
64                         NULL,           /* class data */
65                         sizeof(ModestTnyAccountStore),
66                         1,              /* n_preallocs */
67                         (GInstanceInitFunc) modest_tny_account_store_init,
68                 };
69
70                 static const GInterfaceInfo iface_info = {
71                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
72                         NULL,         /* interface_finalize */
73                         NULL          /* interface_data */
74                 };
75
76                 my_type = g_type_register_static (G_TYPE_OBJECT,
77                                                   "ModestTnyAccountStore", &my_info, 0);
78
79                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
80                                              &iface_info);
81
82         }
83         return my_type;
84 }
85
86 static void
87 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
88 {
89         GObjectClass *gobject_class;
90         gobject_class = (GObjectClass*) klass;
91
92         parent_class            = g_type_class_peek_parent (klass);
93         gobject_class->finalize = modest_tny_account_store_finalize;
94
95         g_type_class_add_private (gobject_class, sizeof(ModestTnyAccountStorePrivate));
96
97         /* signal definitions go here, e.g.: */
98 /*      signals[MY_SIGNAL_1] = */
99 /*              g_signal_new ("my_signal_1",....); */
100 /*      signals[MY_SIGNAL_2] = */
101 /*              g_signal_new ("my_signal_2",....); */
102 /*      etc. */
103 }
104
105 static void
106 modest_tny_account_store_init (ModestTnyAccountStore *obj)
107 {
108         ModestTnyAccountStorePrivate *priv =
109                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
110
111         priv->modest_acc_mgr         = NULL;
112 }
113
114 static void
115 modest_tny_account_store_finalize (GObject *obj)
116 {
117         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
118         ModestTnyAccountStorePrivate *priv =
119                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
120
121         if (priv->modest_acc_mgr) {
122                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
123                 priv->modest_acc_mgr = NULL;
124         }
125 }
126
127 GObject*
128 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
129 {
130         GObject *obj;
131         ModestTnyAccountStorePrivate *priv;
132
133         g_return_val_if_fail (modest_acc_mgr, NULL);
134
135         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
136
137         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
138         g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
139
140         return obj;
141 }
142
143
144
145 /* FIXME: tinymail needs to change here */
146 /* a gpointer arg to get_password should be enough */
147 static gchar*
148 get_password (TnyAccountIface *account, const gchar *prompt)
149 {
150         const gchar *key = tny_account_iface_get_id (account);
151         const TnyAccountStoreIface *AccountStore = tny_account_iface_get_account_store(account);
152         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(AccountStore);
153         gchar *val;
154
155         /* hostname */
156         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
157                                                      MODEST_ACCOUNT_PASSWORD, NULL);
158         /* SMF:
159          * FIXME: if no password avail. in modest-conf, then we need to get the pw from
160          * somewhere else.
161          */
162
163         /* g_warning (val); */
164
165         return val;
166 }
167
168
169 static void
170 forget_password (TnyAccountIface *account)
171 {
172         g_warning (__FUNCTION__);
173 }
174
175
176
177 static gboolean
178 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account)
179 {
180         TnyAccountIface       *account_iface;
181         ModestTnyAccountStore *account_store;
182         ModestTnyAccountStorePrivate *priv;
183
184         const gchar* account_name;
185         const gchar *hostname, *username, *proto;
186
187         g_warning (__FUNCTION__);
188
189         g_return_val_if_fail (self, FALSE);
190         g_return_val_if_fail (account, FALSE);
191
192         account_iface  = TNY_ACCOUNT_IFACE(account);
193         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
194         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
195
196         account_name   = tny_account_iface_get_id(account_iface);
197         if (!account_name) {
198                 g_warning ("failed to retrieve account name");
199                 return FALSE;
200         }
201
202         hostname =  tny_account_iface_get_hostname(account_iface);
203         username =  tny_account_iface_get_user(account_iface);
204         proto    =  tny_account_iface_get_proto(account_iface);
205
206         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
207                                                       account_name,
208                                                       hostname, username, NULL,
209                                                       proto);
210 }
211
212
213
214 static void
215 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
216                                              TnyStoreAccountIface *account)
217 {
218         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
219                 g_warning ("failed to add store account");
220 }
221
222
223
224 static void
225 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
226                                                  TnyTransportAccountIface *account)
227 {
228         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
229                 g_warning ("failed to add transport account");
230 }
231
232
233
234
235 static TnyAccountIface*
236 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
237                       gboolean is_store)
238 {
239         TnyAccountIface *tny_account;
240         ModestTnyAccountStorePrivate *priv;
241         gchar *val;
242
243         g_return_val_if_fail (self, NULL);
244         g_return_val_if_fail (key, NULL);
245
246         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
247
248         /* is it a store or a transport? */
249         if (is_store)
250                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
251         else
252                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
253
254         tny_account_iface_set_account_store (tny_account,
255                                              TNY_ACCOUNT_STORE_IFACE(self));
256         /* id */
257         tny_account_iface_set_id (tny_account, key);
258
259         /* hostname */
260         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
261                                                      MODEST_ACCOUNT_HOSTNAME, NULL);
262         g_warning (val);
263         tny_account_iface_set_hostname (tny_account, val);
264         g_free (val);
265
266         /* username */
267         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
268                                                      MODEST_ACCOUNT_USERNAME, NULL);
269         g_warning (val);
270         tny_account_iface_set_user (tny_account, val);
271         g_free (val);
272
273         /* proto */
274         val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
275                                                      MODEST_ACCOUNT_PROTO, NULL);
276         g_warning (val);
277         tny_account_iface_set_proto (tny_account, val);
278         g_free (val);
279
280         g_warning ("set_pass");
281         tny_account_iface_set_pass_func (tny_account, get_password);
282         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
283
284         return tny_account;
285 }
286
287
288 static GList*
289 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
290                                    gboolean is_store)
291 {
292         GSList *cursor = accounts;
293         GList *tny_accounts = NULL;
294
295         g_return_val_if_fail (self, NULL);
296
297         while (cursor) {
298                 TnyAccountIface *tny_account;
299                 tny_account = tny_account_from_key (self, (gchar*)cursor->data, is_store);
300                 if (!tny_account) {
301                         g_warning ("could not create tnyaccount for %s",
302                                    (gchar*)cursor->data);
303                 } else {
304                         g_warning ("added %s",(gchar*)cursor->data);
305                         tny_accounts =
306                                 g_list_append (tny_accounts, tny_account);
307                 }
308                 cursor = cursor->next;
309         }
310
311         return tny_accounts;
312 }
313
314
315
316
317 static const GList*
318 modest_tny_account_store_get_store_accounts  (TnyAccountStoreIface *iface)
319 {
320         ModestTnyAccountStore        *self;
321         ModestTnyAccountStorePrivate *priv;
322         GSList                       *accounts, *cursor;
323         GList                        *tny_accounts;
324
325         g_return_val_if_fail (iface, NULL);
326
327         /* g_warning ("i'm being called: %s", __FUNCTION__); */
328
329         self = MODEST_TNY_ACCOUNT_STORE(iface);
330         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
331
332         accounts =
333                 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
334                                                          NULL,
335                                                          MODEST_PROTO_TYPE_STORE,
336                                                          NULL, FALSE);
337
338         g_warning ("accounts: %d", g_slist_length (accounts));
339         tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
340         g_slist_free (accounts);
341         g_warning ("store accounts: %d", g_list_length (tny_accounts));
342
343         return tny_accounts; /* FIXME: who will free this? */
344 }
345
346
347
348 static const GList*
349 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
350 {
351         ModestTnyAccountStore        *self;
352         ModestTnyAccountStorePrivate *priv;
353         GSList                       *accounts, *cursor;
354         GList                        *tny_accounts;
355
356         g_return_val_if_fail (iface, NULL);
357
358         self = MODEST_TNY_ACCOUNT_STORE(iface);
359         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
360
361         accounts =
362                 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
363                                                          NULL,
364                                                          MODEST_PROTO_TYPE_TRANSPORT,
365                                                          NULL, FALSE);
366         tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
367         g_warning ("transport accounts: %d", g_list_length (tny_accounts));
368
369         g_slist_free (accounts);
370
371         return tny_accounts; /* FIXME: who will free this? */
372 }
373
374
375 /**
376  * modest_tny_account_store_get_cache_dir:
377  * @self: self a TnyAccountStoreIface instance
378  *
379  * returns the pathname of the cache directory
380  *
381  * Returns: a newly allocated string with the value of the pathname
382  * to the cache directory or NULL if the environment variable $HOME is
383  * not set,
384  * pointer has to be freed by caller
385  */
386 static const gchar*
387 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
388 {
389 gchar *cache_dir;
390
391         if (g_getenv("HOME") != NULL)
392                 cache_dir = g_strconcat(g_getenv("HOME"), "/.modest/cache/", NULL);
393         else
394                 cache_dir = NULL;
395
396         return cache_dir;
397 }
398
399
400
401 static void
402 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
403 {
404         TnyAccountStoreIfaceClass *klass;
405
406         g_return_if_fail (g_iface);
407
408         klass = (TnyAccountStoreIfaceClass *)g_iface;
409
410         klass->add_store_account_func      =
411                 modest_tny_account_store_add_store_account;
412         klass->get_store_accounts_func     =
413                 modest_tny_account_store_get_store_accounts;
414         klass->add_transport_account_func  =
415                 modest_tny_account_store_add_transport_account;
416         klass->get_transport_accounts_func =
417                 modest_tny_account_store_get_transport_accounts;
418         klass->get_cache_dir_func =
419                 modest_tny_account_store_get_cache_dir;
420 }
421