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