From 47b6bed5e41055dcffdbfdb9c989b49bf6500293 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 19 Dec 2006 09:41:59 +0000 Subject: [PATCH] * all: - allow also non-ASCII account names - all but NULL and '/' in names is allowed pmo-trunk-r564 --- src/modest-account-mgr.c | 123 +++++++++++++++++++++++++-------------------- src/modest-account-mgr.h | 4 +- src/modest-conf.c | 7 +++ src/modest-conf.h | 17 ++++++- tests/check_account-mgr.c | 23 ++++----- 5 files changed, 104 insertions(+), 70 deletions(-) diff --git a/src/modest-account-mgr.c b/src/modest-account-mgr.c index 7132df1..9cca65e 100644 --- a/src/modest-account-mgr.c +++ b/src/modest-account-mgr.c @@ -34,11 +34,11 @@ /* '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 { @@ -58,10 +58,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 +68,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 +97,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; } @@ -298,19 +296,21 @@ 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 , * 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); @@ -351,7 +351,7 @@ 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) { @@ -360,19 +360,18 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self, g_return_val_if_fail (self, FALSE); g_return_val_if_fail (name, FALSE); + g_return_val_if_fail (strchr(name, '/') == NULL, 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); @@ -425,22 +424,19 @@ 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* +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 + n, lst->data, + strlen(lst->data) + 1); + lst = lst->next; } - return lst; } @@ -457,7 +453,7 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self, GError *err = NULL; g_return_val_if_fail (self, NULL); - + key = get_account_keyname (account_name, NULL, TRUE); priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self); @@ -470,9 +466,11 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self, } /* 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 */ + if (type == MODEST_PROTOCOL_TYPE_ANY && !proto) { + strip_prefix_from_elements (accounts, strlen(key)+1); + return accounts; + /* +1 because we must remove the ending '/' as well */ + } /* otherwise, filter out the none-matching ones */ cursor = accounts; @@ -499,8 +497,11 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self, 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; + } @@ -518,7 +519,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; } @@ -568,8 +570,7 @@ modest_account_mgr_free_server_account_data (ModestAccountMgr *self, 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; @@ -590,9 +591,9 @@ modest_account_mgr_get_account_data (ModestAccountMgr *self, 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,8 +617,7 @@ 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); @@ -635,7 +635,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) { @@ -858,24 +857,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); + + /* 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); + g_free (account_name_esc); + g_free (name_esc); + } return retval; } diff --git a/src/modest-account-mgr.h b/src/modest-account-mgr.h index 7cdfbc0..4930893 100644 --- a/src/modest-account-mgr.h +++ b/src/modest-account-mgr.h @@ -112,7 +112,7 @@ ModestAccountMgr* modest_account_mgr_new (ModestConf *modest_c /** * modest_account_mgr_add_account: * @self: a ModestAccountMgr instance - * @name: the name of the account to create + * @name: name (id) of the account, which is a valid UTF8 string that does not contain '/' * @store_name: the store account (ie. POP/IMAP) * @transport_name: the transport account (ie. sendmail/SMTP) * @err: a GError ptr, or NULL to ignore. @@ -132,7 +132,7 @@ gboolean modest_account_mgr_add_account (ModestAccountMgr *self, /** * modest_account_mgr_add_server_account: * @self: a ModestAccountMgr instance - * @name: name (id) of the account + * @name: name (id) of the account, which is a valid UTF8 string that does not contain '/' * @hostname: the hostname * @username: the username * @password: the password diff --git a/src/modest-conf.c b/src/modest-conf.c index d5db022..3fc3509 100644 --- a/src/modest-conf.c +++ b/src/modest-conf.c @@ -391,6 +391,13 @@ modest_conf_key_unescape (const gchar* key) return gconf_unescape_key (key, strlen(key)); } +gboolean +modest_conf_key_is_valid (const gchar* key) +{ + return gconf_valid_key (key, NULL); +} + + static void diff --git a/src/modest-conf.h b/src/modest-conf.h index e6d33b8..80e34b4 100644 --- a/src/modest-conf.h +++ b/src/modest-conf.h @@ -255,13 +255,27 @@ gboolean modest_conf_remove_key (ModestConf* self, const gchar* key, GError gboolean modest_conf_key_exists (ModestConf* self, const gchar* key, GError **err); + +/** + * modest_conf_key_valid: + * @str: some key + * + * check whether @str is a valid key in the config system + * This is a *class* function, and therefore does not require a ModestConf + * instance + * + * Returns: TRUE if it is valid, FALSE otherwise + */ +gboolean modest_conf_key_is_valid (const gchar* str); + + /** * modest_conf_key_escape: * @str: a string to escape * * returns an escaped version of @str, ie. something suitable as a key * This is a *class* function, and therefore does not require a ModestConf - * instance + * instance. Note: this for is invidual elements in a key * * Returns: a newly allocated string with the escaped version */ @@ -274,6 +288,7 @@ gchar* modest_conf_key_escape (const gchar* str); * * returns an unescaped version of @str. This is a *class* function, and * therefore does not require a ModestConf instance + * Note: this for is invidual elements in a key * * Returns: a newly allocated string with the unescaped version */ diff --git a/tests/check_account-mgr.c b/tests/check_account-mgr.c index c35317a..c745acc 100644 --- a/tests/check_account-mgr.c +++ b/tests/check_account-mgr.c @@ -34,7 +34,7 @@ /* ----------------------- Defines ---------------------- */ -#define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-account" +#define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-àccount" /* ------------------ Global variables ------------------ */ @@ -45,7 +45,6 @@ static ModestAccountMgr *account_mgr = NULL; static void fx_setup_default_account_mgr () { - int argc = 0; ModestConf *conf = NULL; g_type_init (); @@ -244,7 +243,7 @@ START_TEST (test_add_exists_remove_account_invalid) /* Test 3*/ result = modest_account_mgr_add_account (account_mgr, - "ïnválid_accountñ_nÄméç", + "ïnválid//accountñ//nÄméç", "store_account", "transport_account", NULL); @@ -275,15 +274,15 @@ START_TEST (test_add_exists_remove_account_invalid) "FALSE when passing a NULL account name"); /* Test 6 */ - result = modest_account_mgr_add_server_account (account_mgr, - "ïnválid_accountñ_nÄméç", - "hostname", - "username", - "password", - "proto"); - fail_unless (!result, - "modest_account_mgr_add_server_account does not return " \ - "FALSE when passing an invalid account name"); + result = modest_account_mgr_add_server_account (account_mgr, + "ïnválid//accountñ//nÄméç", + "hostname", + "username", + "password", + "proto"); + fail_unless (!result, + "modest_account_mgr_add_server_account does not return " \ + "FALSE when passing an invalid account name"); /* Test 7 */ result = modest_account_mgr_remove_account (account_mgr, -- 1.7.9.5