2007-04-29 rray Cumming <murrayc@murrayc.com>
[modest] / src / modest-account-mgr.c
index 84e6e0f..89a3d5b 100644 (file)
@@ -307,12 +307,14 @@ gboolean
 modest_account_mgr_add_server_account (ModestAccountMgr * self,
                                       const gchar * name, const gchar *hostname,
                                       const gchar * username, const gchar * password,
-                                      ModestProtocol proto)
+                                      ModestProtocol proto,
+                                      ModestProtocol security,
+                                      ModestProtocol auth)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
        ModestProtocolType proto_type;
-       gboolean ok;
+       gboolean ok = TRUE;
        GError *err = NULL;
        
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
@@ -328,11 +330,10 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        /* hostname */
        key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
        if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
-               g_printerr ("modest: server account '%s' already exists", name);
+               g_printerr ("modest: server account '%s' already exists\n", name);
                g_free (key);
                ok =  FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
        
@@ -381,6 +382,67 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                ok = FALSE;
        }
        g_free (key);
+       if (!ok)
+               goto cleanup;
+
+       /* auth mechanism */
+       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
+       ok = modest_conf_set_string (priv->modest_conf, key,
+                                    modest_protocol_info_get_protocol_name (auth), &err);
+       if (err) {
+               g_printerr ("modest: failed to set %s: %s\n", key, err->message);
+               g_error_free (err);
+               ok = FALSE;
+       }
+       g_free (key);
+       if (!ok)
+               goto cleanup;
+
+       if (proto_type == MODEST_PROTOCOL_TYPE_STORE) {
+
+               GSList *option_list = NULL;
+
+               /* Connection options. Some options are only valid for IMAP
+                  accounts but it's OK for just now since POP is still not
+                  supported */
+               key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_OPTIONS, TRUE);
+               /* Enable subscriptions and check the mails in all folders */
+               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_USE_LSUB);
+               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_CHECK_ALL);
+
+               /* TODO: Remove this hack. These are hard-coded camel options to make the connection work.
+                * The regular connection options (set later here) should be interpreted instead 
+                * because in future these camel options will not be in gconf. murrayc.
+                */
+               /* Security options */
+               switch (security) {
+               case MODEST_PROTOCOL_SECURITY_NONE:
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER);
+                       break;
+               case MODEST_PROTOCOL_SECURITY_SSL:
+               case MODEST_PROTOCOL_SECURITY_TLS:
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS);
+                       break;
+               case MODEST_PROTOCOL_SECURITY_TLS_OP:
+                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE);
+                       break;
+               default:
+                       g_warning ("Invalid security option");
+               }
+               ok = modest_conf_set_list (priv->modest_conf, key, 
+                                          option_list, MODEST_CONF_VALUE_STRING, &err);
+               if (err) {
+                       g_printerr ("modest: failed to set %s: %s\n", key, err->message);
+                       g_error_free (err);
+                       ok = FALSE;
+               }
+               g_slist_free (option_list);
+               g_free (key);
+               
+               
+               /* Add the security settings: */
+               modest_server_account_set_security (self, name, security);
+       }
 
 cleanup:
        if (!ok) {
@@ -451,6 +513,31 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                return FALSE;
        }
 
+       /* in case we're not deleting an account, also delete the dependent store and transport account */
+       if (!server_account) {
+               gchar *server_account_name;
+               
+               server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                                   FALSE);
+               if (server_account_name) {
+                       if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
+                               g_printerr ("modest: failed to remove store account '%s' (%s)\n",
+                                           server_account_name, name);
+                       g_free (server_account_name);
+               } else
+                       g_printerr ("modest: could not find the store account for %s\n", name);
+               
+               server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                                                   FALSE);
+               if (server_account_name) {
+                       if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
+                               g_printerr ("modest: failed to remove transport account '%s' (%s)\n",
+                                           server_account_name, name);
+                       g_free (server_account_name);
+               } else
+                       g_printerr ("modest: could not find the transport account for %s\n", name);
+       }                       
+                       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        key = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
        
@@ -857,6 +944,40 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
        return retval;
 }
 
+gboolean       modest_account_mgr_account_with_display_name_exists       (ModestAccountMgr *self,
+                                                          const gchar *display_name)
+{
+       GSList *account_names = NULL;
+       GSList *cursor = NULL;
+       
+       cursor = account_names = modest_account_mgr_account_names (self);
+
+       gboolean found = FALSE;
+       
+       /* Look at each non-server account to check their display names; */
+       while (cursor) {
+               const gchar * account_name = (gchar*)cursor->data;
+               
+               ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
+               if (!account_data) {
+                       g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       continue;
+               }
+
+               if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
+                       found = TRUE;
+                       break;
+               }
+
+               modest_account_mgr_free_account_data (self, account_data);
+               cursor = cursor->next;
+       }
+       g_slist_free (account_names);
+       
+       return found;
+}
+
+
 
 
 gboolean