* make it compile again with latest Tinymail; does not work yet though.
[modest] / src / modest-account-mgr.c
index 4fee9f3..8a05785 100644 (file)
  */
 
 #include <string.h>
-#include "modest-marshal.h"
-#include "modest-account-keys.h"
-#include "modest-account-mgr.h"
+#include <modest-marshal.h>
+#include <modest-account-keys.h>
+#include <modest-account-mgr.h>
 
 /* 'private'/'protected' functions */
 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
 static void modest_account_mgr_init (ModestAccountMgr * obj);
 static void modest_account_mgr_finalize (GObject * obj);
 
-static gchar *get_account_keyname (const gchar * accname, const gchar * name, gboolean server_account);
+static gchar *get_account_keyname (const gchar * accname, const gchar * name,
+                                  gboolean server_account);
 
 /* list my signals */
 enum {
@@ -48,8 +49,8 @@ enum {
 
 typedef struct _ModestAccountMgrPrivate ModestAccountMgrPrivate;
 struct _ModestAccountMgrPrivate {
-       ModestConf *modest_conf;
-       GSList *current_accounts;
+       ModestConf        *modest_conf;
+       ModestProtocolMgr *proto_mgr;
 };
 
 #define MODEST_ACCOUNT_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -165,6 +166,7 @@ modest_account_mgr_get_type (void)
                        sizeof (ModestAccountMgr),
                        1,      /* n_preallocs */
                        (GInstanceInitFunc) modest_account_mgr_init,
+                       NULL
                };
 
                my_type = g_type_register_static (G_TYPE_OBJECT,
@@ -213,6 +215,7 @@ modest_account_mgr_init (ModestAccountMgr * obj)
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
        priv->modest_conf = NULL;
+       priv->proto_mgr   = modest_protocol_mgr_new ();
 }
 
 static void
@@ -225,8 +228,12 @@ modest_account_mgr_finalize (GObject * obj)
                g_object_unref (G_OBJECT(priv->modest_conf));
                priv->modest_conf = NULL;
        }
-}
 
+       if (priv->proto_mgr) {
+               g_object_unref (G_OBJECT(priv->proto_mgr));
+               priv->proto_mgr = NULL;
+       }
+}
 
 ModestAccountMgr *
 modest_account_mgr_new (ModestConf * conf)
@@ -342,10 +349,8 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
 
 gboolean
 modest_account_mgr_add_server_account (ModestAccountMgr * self,
-                                      const gchar * name,
-                                      const gchar * hostname,
-                                      const gchar * username,
-                                      const gchar * password,
+                                      const gchar * name, const gchar * hostname,
+                                      const gchar * username, const gchar * password,
                                       const gchar * proto)
 {
        ModestAccountMgrPrivate *priv;
@@ -440,7 +445,7 @@ strip_prefix_from_elements (GSList * lst, guint n)
 GSList *
 modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
                                           const gchar * account_name,
-                                          ModestProtoType type,
+                                          ModestProtocolType type,
                                           const gchar *proto)
 {
        GSList *accounts;
@@ -463,7 +468,7 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
        }
        
        /* no restrictions, return everything */
-       if (type == MODEST_PROTO_TYPE_ANY && !proto)
+       if (type == MODEST_PROTOCOL_TYPE_ANY && !proto)
                return strip_prefix_from_elements (accounts, strlen(key)+1);
        /* +1 because we must remove the ending '/' as well */
        
@@ -477,8 +482,9 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
                acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,
                                                           TRUE, NULL);
                if ((!acc_proto) ||                                /* proto not defined? */
-                   (type != MODEST_PROTO_TYPE_ANY &&              /* proto type ...     */
-                    modest_proto_type (acc_proto) != type) ||     /* ... matches?       */
+                   (type != MODEST_PROTOCOL_TYPE_ANY &&                   /* proto type ...     */
+                    !modest_protocol_mgr_protocol_is_valid (priv->proto_mgr,
+                                                            acc_proto,type)) ||           /* ... matches?       */
                    (proto && strcmp (proto, acc_proto) != 0)) {  /* proto matches?     */
                        /* match! remove from the list */
                        GSList *nxt = cursor->next;
@@ -514,6 +520,120 @@ modest_account_mgr_account_names (ModestAccountMgr * self, GError ** err)
 }
 
 
+static ModestServerAccountData*
+modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
+{
+       ModestServerAccountData *data;
+       
+       g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
+                                                                TRUE, NULL), NULL);    
+       data = g_new0 (ModestServerAccountData, 1);
+
+       data->account_name = g_strdup (name);
+       data->hostname     = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_HOSTNAME,
+                                                           TRUE, NULL);
+       data->username     = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_USERNAME,
+                                                           TRUE, NULL);
+       data->proto        = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_PROTO,
+                                                           TRUE, NULL);
+       data->password     = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_PASSWORD,
+                                                           TRUE, NULL);
+       return data;
+}
+
+
+static void
+modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
+                                            ModestServerAccountData* data)
+{
+       g_return_if_fail (self);
+
+       if (!data)
+               return; /* not an error */
+
+       g_free (data->account_name);
+       g_free (data->hostname);
+       g_free (data->username);
+       g_free (data->proto);
+       g_free (data->password);
+
+       g_free (data);
+}
+
+
+ModestAccountData*
+modest_account_mgr_get_account_data     (ModestAccountMgr *self,
+                                        const gchar* name)
+{
+       ModestAccountData *data;
+       gchar *server_account;
+       
+       g_return_val_if_fail (self, NULL);
+       g_return_val_if_fail (name, NULL);
+       g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
+                                                                FALSE, NULL), NULL);   
+       data = g_new0 (ModestAccountData, 1);
+
+       data->account_name = g_strdup (name);
+       data->full_name    = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_FULLNAME,
+                                                           FALSE, NULL);
+       data->email        = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_EMAIL,
+                                                           FALSE, NULL);
+       data->enabled      = modest_account_mgr_account_get_enabled (self, name);
+
+       /* store */
+       server_account = modest_account_mgr_get_string (self, name,
+                                                       MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                       FALSE, NULL);
+       if (server_account) {
+               data->store_account =
+                       modest_account_mgr_get_server_account_data (self,
+                                                                   server_account);
+               g_free (server_account);
+       }
+
+       /* transport */
+       server_account = modest_account_mgr_get_string (self, name,
+                                                       MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                                       FALSE, NULL);
+       if (server_account) {
+               data->transport_account =
+                       modest_account_mgr_get_server_account_data (self,
+                                                                   server_account);
+               g_free (server_account);
+       }
+
+       return data;
+}
+
+
+void
+modest_account_mgr_free_account_data     (ModestAccountMgr *self,
+                                         ModestAccountData *data)
+{
+       g_return_if_fail (self);
+
+       if (!data)
+               return;
+
+       g_free (data->account_name);
+       g_free (data->full_name);
+       g_free (data->email);
+
+       modest_account_mgr_free_server_account_data (self, data->store_account);
+       modest_account_mgr_free_server_account_data (self, data->transport_account);
+       
+       g_free (data);
+}
+
+
+
 gchar *
 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
                               const gchar *key, gboolean server_account, GError **err) {
@@ -703,6 +823,5 @@ get_account_keyname (const gchar * accname, const gchar * name, gboolean server_
                retval = g_strconcat (namespace, "/", accname, NULL);
 
        g_free (account_name);
-
        return retval;
 }