+/** The application should call this when the user changes should be saved.
+ * @account_name: Specify this again in case it was not previously known.
+ */
+gboolean
+modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self,
+ const gchar* account_name)
+{
+ ModestConnectionSpecificSmtpWindowPrivate *priv =
+ CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
+
+
+ /* Get the first iter in the list */
+ GtkTreeIter iter;
+ gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
+
+ /* Walk through the list, reading each row */
+ while (valid) {
+ gchar *id = NULL;
+ gchar *connection_name = NULL;
+ gchar *server_account_name = NULL;
+ ModestServerAccountData *data = NULL;
+
+ gtk_tree_model_get (priv->model, &iter,
+ MODEL_COL_ID, &id,
+ MODEL_COL_NAME, &connection_name,
+ MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
+ MODEL_COL_SERVER_ACCOUNT_DATA, &data,
+ -1);
+
+ gboolean success = TRUE;
+ if (id && data) { /* The presence of data suggests that there is something to save. */
+ if (!server_account_name) {
+ /* Add a new server account, building a (non-human-visible) name: */
+ gchar *name_start = g_strdup_printf("%s_specific_%s",
+ priv->account_name, connection_name);
+ server_account_name = modest_account_mgr_get_unused_account_name (
+ priv->account_manager, name_start, TRUE /* server account. */);
+ g_free (name_start);
+
+ success = modest_account_mgr_add_server_account (priv->account_manager,
+ server_account_name,
+ data->hostname,
+ data->username, data->password,
+ MODEST_PROTOCOL_TRANSPORT_SMTP,
+ data->security,
+ data->secure_auth);
+
+ /* associate the specific server account with this connection for this account: */
+ success = success && modest_account_mgr_set_connection_specific_smtp (
+ priv->account_manager, priv->account_name,
+ connection_name, server_account_name);
+
+ /* Save the new name in the treemodel, so it can be edited again later: */
+ gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
+ MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
+
+ } else {
+ /* Change an existing server account: */
+ success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
+ MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
+
+ success = success && modest_account_mgr_set_string (priv->account_manager, server_account_name,
+ MODEST_ACCOUNT_USERNAME, data->username, TRUE /* server account */);
+
+ success = success && modest_account_mgr_set_string (priv->account_manager, server_account_name,
+ MODEST_ACCOUNT_PASSWORD, data->password, TRUE /* server account */);
+
+ modest_server_account_set_secure_auth (priv->account_manager, server_account_name,
+ data->secure_auth);
+
+ modest_server_account_set_security (priv->account_manager, server_account_name,
+ data->security);
+
+ modest_account_mgr_set_int (priv->account_manager, server_account_name,
+ MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
+ }
+ }
+
+ g_free (connection_name);
+ g_free (id);
+ g_free (server_account_name);
+
+ if (!success)
+ return FALSE;
+
+ /* Get next row: */
+ valid = gtk_tree_model_iter_next (priv->model, &iter);
+ }
+
+ update_model_server_names (self);
+
+ return TRUE;
+}
+
+void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
+{
+ ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
+
+ GtkTreeIter iter;
+ gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
+ while (valid) {
+
+ gchar *server_account_name = NULL;
+ gtk_tree_model_get (priv->model, &iter,
+ MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
+ -1);
+
+ if (server_account_name) {
+ /* Get the server hostname and show it in the treemodel: */
+ gchar *hostname = modest_account_mgr_get_string (priv->account_manager,
+ server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
+ gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
+ MODEL_COL_SERVER_NAME, hostname,
+ -1);
+ g_free (hostname);
+ }
+
+ /* Get next row: */
+ valid = gtk_tree_model_iter_next (priv->model, &iter);
+ }
+}
+