* modest-header-view-*
[modest] / src / modest-account-mgr.c
index 7132df1..77deee5 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-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 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 {
@@ -50,7 +49,6 @@ enum {
 typedef struct _ModestAccountMgrPrivate ModestAccountMgrPrivate;
 struct _ModestAccountMgrPrivate {
        ModestConf        *modest_conf;
-       ModestProtocolMgr *proto_mgr;
 };
 
 #define MODEST_ACCOUNT_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -58,10 +56,8 @@ struct _ModestAccountMgrPrivate {
                                                 ModestAccountMgrPrivate))
 /* globals */
 static GObjectClass *parent_class = NULL;
-
 static guint signals[LAST_SIGNAL] = {0};
 
-
 static gchar*
 account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
 {
@@ -70,7 +66,7 @@ account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_serve
        gchar *cursor;
        gchar *account = NULL;
 
-       /* determine if it's an account or a server account,
+       /* determine whether it's an account or a server account,
         * based on the prefix */
        if (g_str_has_prefix (key, account_ns)) {
 
@@ -99,7 +95,7 @@ account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_serve
        /* put a NULL where the first slash was */
        if (cursor)
                *cursor = '\0';
-               
+
        return account;
 }
 
@@ -215,7 +211,6 @@ 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
@@ -229,11 +224,6 @@ modest_account_mgr_finalize (GObject * obj)
                priv->modest_conf = NULL;
        }
 
-       if (priv->proto_mgr) {
-               g_object_unref (G_OBJECT(priv->proto_mgr));
-               priv->proto_mgr = NULL;
-       }
-
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -298,22 +288,23 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
 
        g_return_val_if_fail (self, FALSE);
        g_return_val_if_fail (name, FALSE);
-
-       if (modest_account_mgr_account_exists (self, name, FALSE, err)) {
-               g_printerr ("modest: account already exists\n");
-               return FALSE;
-       }
+       g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
+       
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        
        /*
         * we create the account by adding an account 'dir', with the name <name>,
         * and in that the 'display_name' string key
         */
-       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       
        key = get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
+       if (modest_account_mgr_account_exists (self, key, FALSE, err)) {
+               g_printerr ("modest: account already exists\n");
+               g_free (key);
+               return FALSE;
+       }
+       
        ok = modest_conf_set_string (priv->modest_conf, key, name, err);
        g_free (key);
-
        if (!ok) {
                g_printerr ("modest: cannot set display name\n");
                return FALSE;
@@ -351,28 +342,32 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
 
 gboolean
 modest_account_mgr_add_server_account (ModestAccountMgr * self,
-                                      const gchar * name, const gchar * hostname,
+                                      const gchar * name, const gchar *hostname,
                                       const gchar * username, const gchar * password,
-                                      const gchar * proto)
+                                      ModestProtocol proto)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
-
+       ModestProtocolType proto_type;
+       
        g_return_val_if_fail (self, FALSE);
        g_return_val_if_fail (name, FALSE);
+       g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
 
+       proto_type = modest_protocol_info_get_protocol_type (proto);
+       g_return_val_if_fail (proto_type == MODEST_PROTOCOL_TYPE_TRANSPORT ||
+                             proto_type == MODEST_PROTOCOL_TYPE_STORE, FALSE);
+                             
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-
-       key = get_account_keyname (name, NULL, TRUE);
+       
+       /* hostname */
+       key = get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
        if (modest_conf_key_exists (priv->modest_conf, key, NULL)) {
                g_printerr ("modest: server account '%s' already exists", name);
                g_free (key);
                return FALSE;
        }
-       g_free (key);
        
-       /* hostname */
-       key = get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
        modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), NULL);
        g_free (key);
 
@@ -388,7 +383,9 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
 
        /* proto */
        key = get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
-       modest_conf_set_string (priv->modest_conf, key, null_means_empty (proto), NULL);
+       modest_conf_set_string (priv->modest_conf, key,
+                               modest_protocol_info_get_protocol_name(proto),
+                               NULL);
        g_free (key);
        
        return TRUE;
@@ -425,30 +422,27 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
 
 
 
-/* strip the first /n/ character from each element */
-/* caller must make sure all elements are strings with
+/* strip the first /n/ character from each element
+ * caller must make sure all elements are strings with
  * length >= n, and also that data can be freed.
+ * change is in-place
  */
-static GSList*
+static void
 strip_prefix_from_elements (GSList * lst, guint n)
 {
-       GSList *cursor = lst;
-
-       while (cursor) {
-               gchar *str = (gchar *) cursor->data;
-               cursor->data = g_strdup (str + n);
-               g_free (str);
-               cursor = cursor->next;
+       while (lst) {
+               memmove (lst->data, lst->data + n,
+                        strlen(lst->data) - n + 1);
+               lst = lst->next;
        }
-       return lst;
 }
 
 
-GSList *
+GSList*
 modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
                                           const gchar * account_name,
                                           ModestProtocolType type,
-                                          const gchar *proto)
+                                          ModestProtocol proto)
 {
        GSList *accounts;
        GSList *cursor;
@@ -457,6 +451,13 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
        GError *err = NULL;
        
        g_return_val_if_fail (self, NULL);
+
+       if (proto != MODEST_PROTOCOL_UNKNOWN) {
+               ModestProtocolType proto_type;
+               proto_type = modest_protocol_info_get_protocol_type (proto);
+               g_return_val_if_fail (proto_type == MODEST_PROTOCOL_TYPE_TRANSPORT ||
+                                     proto_type == MODEST_PROTOCOL_TYPE_STORE, NULL);
+       }
        
        key      = get_account_keyname (account_name, NULL, TRUE);
        priv     = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
@@ -469,42 +470,36 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
                return NULL;
        }
        
-       /* no restrictions, return everything */
-       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 */
-       
-       /* otherwise, filter out the none-matching ones */
+       /* filter out the ones with the wrong protocol */
+       /* we could optimize for unknown proto / unknown type, but it will only
+        * make the code more complex */
        cursor = accounts;
-       while (cursor) {
-               gchar *account;
-               gchar *acc_proto;
-               
-               account = account_from_key ((gchar*)cursor->data, NULL, NULL);
-               acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,
-                                                          TRUE, NULL);
-               if ((!acc_proto) ||                                /* proto not defined? */
-                   (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 */
+       while (cursor) { 
+               gchar *account   = account_from_key ((gchar*)cursor->data, NULL, NULL);
+               gchar *acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,
+                                                                 TRUE, NULL);
+               ModestProtocol     this_proto = modest_protocol_info_get_protocol (acc_proto);
+               ModestProtocolType this_type  = modest_protocol_info_get_protocol_type (this_proto);
+
+               if ((this_type  != MODEST_PROTOCOL_TYPE_UNKNOWN && this_type  != type) ||
+                   (this_proto != MODEST_PROTOCOL_UNKNOWN      && this_proto != proto)) {
                        GSList *nxt = cursor->next;
                        accounts = g_slist_delete_link (accounts, cursor);
                        cursor = nxt;
                } else
                        cursor = cursor->next;
-
+               
                g_free (account);
                g_free (acc_proto);
        }
-
-       return strip_prefix_from_elements (accounts, strlen(key)+1);
+       
        /* +1 because we must remove the ending '/' as well */
+       strip_prefix_from_elements (accounts, strlen(key)+1);
+       return accounts;        
 }
 
 
-GSList *
+GSList*
 modest_account_mgr_account_names (ModestAccountMgr * self, GError ** err)
 {
        GSList *accounts;
@@ -518,7 +513,8 @@ modest_account_mgr_account_names (ModestAccountMgr * self, GError ** err)
        accounts = modest_conf_list_subkeys (priv->modest_conf,
                                              MODEST_ACCOUNT_NAMESPACE, err);
        
-       return strip_prefix_from_elements (accounts, prefix_len);
+       strip_prefix_from_elements (accounts, prefix_len);
+       return accounts;
 }
 
 
@@ -526,6 +522,7 @@ static ModestServerAccountData*
 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
 {
        ModestServerAccountData *data;
+       gchar *proto;
        
        g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
                                                                 TRUE, NULL), NULL);    
@@ -538,9 +535,12 @@ modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar*
        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);
+       
+       proto        = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO,
+                                                     TRUE, NULL);
+       data->proto  = modest_protocol_info_get_protocol (proto);
+       g_free (proto);
+       
        data->password     = modest_account_mgr_get_string (self, name,
                                                            MODEST_ACCOUNT_PASSWORD,
                                                            TRUE, NULL);
@@ -558,18 +558,22 @@ modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
                return; /* not an error */
 
        g_free (data->account_name);
+       data->account_name = NULL;
+       
        g_free (data->hostname);
+       data->hostname = NULL;
+       
        g_free (data->username);
-       g_free (data->proto);
-       g_free (data->password);
+       data->username = NULL;
 
+       g_free (data->password);
+       data->password = NULL;
+       
        g_free (data);
 }
 
-
 ModestAccountData*
-modest_account_mgr_get_account_data     (ModestAccountMgr *self,
-                                        const gchar* name)
+modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
 {
        ModestAccountData *data;
        gchar *server_account;
@@ -581,18 +585,22 @@ modest_account_mgr_get_account_data     (ModestAccountMgr *self,
        data = g_new0 (ModestAccountData, 1);
 
        data->account_name = g_strdup (name);
-       data->full_name    = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_FULLNAME,
+
+       data->display_name = modest_account_mgr_get_string (self, name,
+                                                           MODEST_ACCOUNT_DISPLAY_NAME,
                                                            FALSE, NULL);
+       data->fullname      = 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);
+       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,
@@ -616,16 +624,16 @@ modest_account_mgr_get_account_data     (ModestAccountMgr *self,
 
 
 void
-modest_account_mgr_free_account_data     (ModestAccountMgr *self,
-                                         ModestAccountData *data)
+modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
 {
        g_return_if_fail (self);
 
-       if (!data)
+       if (!data) /* not an error */ 
                return;
 
        g_free (data->account_name);
-       g_free (data->full_name);
+       g_free (data->display_name);
+       g_free (data->fullname);
        g_free (data->email);
 
        modest_account_mgr_free_server_account_data (self, data->store_account);
@@ -635,7 +643,6 @@ modest_account_mgr_free_account_data     (ModestAccountMgr *self,
 }
 
 
-
 gchar *
 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
                               const gchar *key, gboolean server_account, GError **err) {
@@ -783,6 +790,29 @@ modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
        return retval;
 }
 
+void 
+modest_account_mgr_set_list (ModestAccountMgr *self,
+                            const gchar *name,
+                            const gchar *key,
+                            GSList *val,
+                            ModestConfValueType list_type,
+                            gboolean server_account,
+                            GError **err)
+{
+       ModestAccountMgrPrivate *priv;
+       gchar *keyname;
+       
+       g_return_if_fail (self);
+       g_return_if_fail (name);
+       g_return_if_fail (key);
+       g_return_if_fail (val);
+
+       keyname = get_account_keyname (name, key, server_account);
+       
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       modest_conf_set_list (priv->modest_conf, keyname, val, list_type, err);
+       g_free (keyname);
+}
 
 gboolean
 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
@@ -858,24 +888,38 @@ modest_account_mgr_unset (ModestAccountMgr *self,
        return retval;
 }
 
+
+
 /* must be freed by caller */
 static gchar *
-get_account_keyname (const gchar * accname, const gchar * name, gboolean server_account)
+get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
 {
-       gchar *namespace, *account_name, *retval;
+       gchar *namespace;
+       gchar *retval;
        
        namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
-
-       if (!accname)
+       
+       if (!account_name)
                return g_strdup (namespace);
-
-       account_name = modest_conf_key_escape (NULL, accname);
        
        if (name)
-               retval = g_strconcat (namespace, "/", accname, "/", name, NULL);
+               retval = g_strconcat (namespace, "/", account_name, "/", name, NULL);
        else
-               retval = g_strconcat (namespace, "/", accname, NULL);
+               retval = g_strconcat (namespace, "/", account_name, NULL);
 
-       g_free (account_name);
+       /* special case: the key has some weird characters */
+       if (!modest_conf_key_is_valid (retval)) {
+
+               gchar *account_name_esc, *name_esc;
+               g_free (retval);
+               
+               account_name_esc = account_name ? modest_conf_key_escape (account_name) : NULL;
+               name_esc         = name ? modest_conf_key_escape (name) : NULL;
+               
+               retval =  get_account_keyname (account_name_esc, name_esc, server_account);
+
+               g_free (account_name_esc);
+               g_free (name_esc);
+       }
        return retval;
 }