1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-09 21:30:42 +00:00

Refactored ProfAccount creation

This commit is contained in:
James Booth 2014-01-22 22:22:01 +00:00
parent bbdf2bea58
commit 21ab182151
13 changed files with 251 additions and 271 deletions

View File

@ -38,6 +38,7 @@ core_sources = \
src/tools/history.c src/tools/history.h \
src/tools/tinyurl.c src/tools/tinyurl.h \
src/config/accounts.c src/config/accounts.h \
src/config/account.c src/config/account.h \
src/config/preferences.c src/config/preferences.h \
src/config/theme.c src/config/theme.h
@ -58,6 +59,7 @@ test_sources = \
src/tools/history.c src/tools/history.h \
src/tools/tinyurl.c src/tools/tinyurl.h \
src/config/accounts.h \
src/config/account.c src/config/account.h \
src/config/preferences.c src/config/preferences.h \
src/config/theme.c src/config/theme.h \
src/ui/windows.c src/ui/windows.h \

View File

@ -29,6 +29,7 @@
#include "command/commands.h"
#include "common.h"
#include "config/accounts.h"
#include "config/account.h"
#include "config/preferences.h"
#include "config/theme.h"
#include "contact.h"
@ -149,13 +150,13 @@ cmd_connect(gchar **args, struct cmd_help_t help)
ProfAccount *account = accounts_get_account(lower);
if (account != NULL) {
jid = accounts_create_full_jid(account);
jid = account_create_full_jid(account);
if (account->password == NULL) {
account->password = ui_ask_password();
}
cons_show("Connecting with account %s as %s", account->name, jid);
conn_status = jabber_connect_with_account(account);
accounts_free_account(account);
account_free(account);
} else {
char *passwd = ui_ask_password();
jid = strdup(lower);
@ -189,7 +190,7 @@ cmd_account(gchar **args, struct cmd_help_t help)
} else {
ProfAccount *account = accounts_get_account(jabber_get_account_name());
cons_show_account(account);
accounts_free_account(account);
account_free(account);
}
} else if (strcmp(command, "list") == 0) {
gchar **accounts = accounts_get_list();
@ -206,7 +207,7 @@ cmd_account(gchar **args, struct cmd_help_t help)
cons_show("");
} else {
cons_show_account(account);
accounts_free_account(account);
account_free(account);
}
}
} else if (strcmp(command, "add") == 0) {
@ -1623,7 +1624,7 @@ cmd_join(gchar **args, struct cmd_help_t help)
jid_destroy(room_jid);
jid_destroy(my_jid);
g_string_free(room_str, TRUE);
accounts_free_account(account);
account_free(account);
return TRUE;
}

144
src/config/account.c Normal file
View File

@ -0,0 +1,144 @@
/*
* account.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "jid.h"
#include "config/account.h"
ProfAccount*
account_new(const gchar * const name, const gchar * const jid,
const gchar * const password, gboolean enabled, const gchar * const server,
int port, const gchar * const resource, const gchar * const last_presence,
const gchar * const login_presence, int priority_online, int priority_chat,
int priority_away, int priority_xa, int priority_dnd,
const gchar * const muc_service, const gchar * const muc_nick)
{
ProfAccount *new_account = malloc(sizeof(ProfAccount));
new_account->name = strdup(name);
if (jid != NULL) {
new_account->jid = strdup(jid);
} else {
new_account->jid = strdup(name);
}
if (password != NULL) {
new_account->password = strdup(password);
} else {
new_account->password = NULL;
}
new_account->enabled = enabled;
if (server != NULL) {
new_account->server = strdup(server);
} else {
new_account->server = NULL;
}
if (resource != NULL) {
new_account->resource = strdup(resource);
} else {
new_account->resource = NULL;
}
new_account->port = port;
if (last_presence == NULL || !valid_resource_presence_string(last_presence)) {
new_account->last_presence = strdup("online");
} else {
new_account->last_presence = strdup(last_presence);
}
if (login_presence == NULL) {
new_account->login_presence = strdup("online");
} else if (strcmp(login_presence, "last") == 0) {
new_account->login_presence = strdup(login_presence);
} else if (!valid_resource_presence_string(login_presence)) {
new_account->login_presence = strdup("online");
} else {
new_account->login_presence = strdup(login_presence);
}
new_account->priority_online = priority_online;
new_account->priority_chat = priority_chat;
new_account->priority_away = priority_away;
new_account->priority_xa = priority_xa;
new_account->priority_dnd = priority_dnd;
if (muc_service == NULL) {
GString *g_muc_service = g_string_new("conference.");
Jid *jidp = jid_create(new_account->jid);
g_string_append(g_muc_service, jidp->domainpart);
new_account->muc_service = g_muc_service->str;
g_string_free(g_muc_service, FALSE);
jid_destroy(jidp);
} else {
new_account->muc_service = strdup(muc_service);
}
if (muc_nick == NULL) {
Jid *jidp = jid_create(new_account->jid);
new_account->muc_nick = strdup(jidp->domainpart);
jid_destroy(jidp);
} else {
new_account->muc_nick = strdup(muc_nick);
}
return new_account;
}
char *
account_create_full_jid(ProfAccount *account)
{
if (account->resource != NULL) {
return create_fulljid(account->jid, account->resource);
} else {
return strdup(account->jid);
}
}
void
account_free(ProfAccount *account)
{
if (account != NULL) {
free(account->name);
free(account->jid);
free(account->password);
free(account->resource);
free(account->server);
free(account->last_presence);
free(account->login_presence);
free(account->muc_service);
free(account->muc_nick);
free(account);
account = NULL;
}
}

58
src/config/account.h Normal file
View File

@ -0,0 +1,58 @@
/*
* account.h
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "common.h"
typedef struct prof_account_t {
gchar *name;
gchar *jid;
gchar *password;
gchar *resource;
gchar *server;
int port;
gchar *last_presence;
gchar *login_presence;
gint priority_online;
gint priority_chat;
gint priority_away;
gint priority_xa;
gint priority_dnd;
gchar *muc_service;
gchar *muc_nick;
gboolean enabled;
} ProfAccount;
ProfAccount* account_new(const gchar * const name, const gchar * const jid,
const gchar * const passord, gboolean enabled, const gchar * const server,
int port, const gchar * const resource, const gchar * const last_presence,
const gchar * const login_presence, int priority_online, int priority_chat,
int priority_away, int priority_xa, int priority_dnd,
const gchar * const muc_service, const gchar * const muc_nick);
char* account_create_full_jid(ProfAccount *account);
void account_free(ProfAccount *account);
#endif

View File

@ -28,6 +28,7 @@
#include "accounts.h"
#include "common.h"
#include "config/account.h"
#include "jid.h"
#include "log.h"
#include "tools/autocomplete.h"
@ -184,145 +185,48 @@ _accounts_get_account(const char * const name)
if (!g_key_file_has_group(accounts, name)) {
return NULL;
} else {
ProfAccount *account = malloc(sizeof(ProfAccount));
account->name = strdup(name);
gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL);
if (jid != NULL) {
account->jid = strdup(jid);
g_free(jid);
} else {
account->jid = strdup(name);
// fix accounts that have no jid property by setting to name
if (jid == NULL) {
g_key_file_set_string(accounts, name, "jid", name);
_save_accounts();
}
gchar *password = g_key_file_get_string(accounts, name, "password", NULL);
if (password != NULL) {
account->password = strdup(password);
g_free(password);
} else {
account->password = NULL;
}
account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);
gboolean enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);
gchar *server = g_key_file_get_string(accounts, name, "server", NULL);
if (server != NULL) {
account->server = strdup(server);
g_free(server);
} else {
account->server = NULL;
}
int port = g_key_file_get_integer(accounts, name, "port", NULL);
account->port = port;
gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL);
if (resource != NULL) {
account->resource = strdup(resource);
g_free(resource);
} else {
account->resource = NULL;
}
int port = g_key_file_get_integer(accounts, name, "port", NULL);
gchar *presence = g_key_file_get_string(accounts, name, "presence.last", NULL);
if (presence == NULL || (!valid_resource_presence_string(presence))) {
account->last_presence = strdup("online");
} else {
account->last_presence = strdup(presence);
}
gchar *last_presence = g_key_file_get_string(accounts, name, "presence.last", NULL);
gchar *login_presence = g_key_file_get_string(accounts, name, "presence.login", NULL);
if (presence != NULL) {
g_free(presence);
}
presence = g_key_file_get_string(accounts, name, "presence.login", NULL);
if (presence == NULL) {
account->login_presence = strdup("online");
} else if (strcmp(presence, "last") == 0) {
account->login_presence = strdup("last");
} else if (!valid_resource_presence_string(presence)) {
account->login_presence = strdup("online");
} else {
account->login_presence = strdup(presence);
}
if (presence != NULL) {
g_free(presence);
}
account->priority_online = g_key_file_get_integer(accounts, name, "priority.online", NULL);
account->priority_chat = g_key_file_get_integer(accounts, name, "priority.chat", NULL);
account->priority_away = g_key_file_get_integer(accounts, name, "priority.away", NULL);
account->priority_xa = g_key_file_get_integer(accounts, name, "priority.xa", NULL);
account->priority_dnd = g_key_file_get_integer(accounts, name, "priority.dnd", NULL);
int priority_online = g_key_file_get_integer(accounts, name, "priority.online", NULL);
int priority_chat = g_key_file_get_integer(accounts, name, "priority.chat", NULL);
int priority_away = g_key_file_get_integer(accounts, name, "priority.away", NULL);
int priority_xa = g_key_file_get_integer(accounts, name, "priority.xa", NULL);
int priority_dnd = g_key_file_get_integer(accounts, name, "priority.dnd", NULL);
gchar *muc_service = g_key_file_get_string(accounts, name, "muc.service", NULL);
if (muc_service == NULL) {
GString *g_muc_service = g_string_new("conference.");
Jid *jidp = jid_create(account->jid);
g_string_append(g_muc_service, jidp->domainpart);
account->muc_service = strdup(g_muc_service->str);
g_string_free(g_muc_service, TRUE);
jid_destroy(jidp);
} else {
account->muc_service = muc_service;
}
gchar *muc_nick = g_key_file_get_string(accounts, name, "muc.nick", NULL);
if (muc_nick == NULL) {
Jid *jidp = jid_create(account->jid);
account->muc_nick = strdup(jidp->localpart);
jid_destroy(jidp);
} else {
account->muc_nick = muc_nick;
}
// get room history
account->room_history = NULL;
gsize history_size = 0;
gchar **room_history_values = g_key_file_get_string_list(accounts, name,
"rooms.history", &history_size, NULL);
ProfAccount *new_account = account_new(name, jid, password, enabled,
server, port, resource, last_presence, login_presence,
priority_online, priority_chat, priority_away, priority_xa,
priority_dnd, muc_service, muc_nick);
if (room_history_values != NULL) {
int i = 0;
for (i = 0; i < history_size; i++) {
account->room_history = g_slist_append(account->room_history,
strdup(room_history_values[i]));
}
g_free(jid);
g_free(password);
g_free(server);
g_free(resource);
g_free(last_presence);
g_free(login_presence);
g_free(muc_service);
g_free(muc_nick);
g_strfreev(room_history_values);
}
return account;
}
}
static char *
_accounts_create_full_jid(ProfAccount *account)
{
if (account->resource != NULL) {
return create_fulljid(account->jid, account->resource);
} else {
return strdup(account->jid);
}
}
static void
_accounts_free_account(ProfAccount *account)
{
if (account != NULL) {
free(account->name);
free(account->jid);
free(account->password);
free(account->resource);
free(account->server);
free(account->last_presence);
free(account->login_presence);
free(account->muc_service);
free(account->muc_nick);
free(account);
return new_account;
}
}
@ -745,7 +649,6 @@ accounts_init_module(void)
accounts_add = _accounts_add;
accounts_get_list = _accounts_get_list;
accounts_get_account = _accounts_get_account;
accounts_free_account = _accounts_free_account;
accounts_enable = _accounts_enable;
accounts_disable = _accounts_disable;
accounts_rename = _accounts_rename;
@ -769,6 +672,5 @@ accounts_init_module(void)
accounts_set_priority_all = _accounts_set_priority_all;
accounts_get_priority_for_presence_type = _accounts_get_priority_for_presence_type;
accounts_clear_password = _accounts_clear_password;
accounts_create_full_jid = _accounts_create_full_jid;
}

View File

@ -26,26 +26,7 @@
#define MAX_PASSWORD_SIZE 64
#include "common.h"
typedef struct prof_account_t {
gchar *name;
gchar *jid;
gchar *password;
gchar *resource;
gchar *server;
int port;
gchar *last_presence;
gchar *login_presence;
gint priority_online;
gint priority_chat;
gint priority_away;
gint priority_xa;
gint priority_dnd;
gchar *muc_service;
gchar *muc_nick;
gboolean enabled;
GSList *room_history;
} ProfAccount;
#include "config/account.h"
void accounts_init_module(void);
@ -59,7 +40,6 @@ void (*accounts_reset_enabled_search)(void);
void (*accounts_add)(const char *jid, const char *altdomain, const int port);
gchar** (*accounts_get_list)(void);
ProfAccount* (*accounts_get_account)(const char * const name);
void (*accounts_free_account)(ProfAccount *account);
gboolean (*accounts_enable)(const char * const name);
gboolean (*accounts_disable)(const char * const name);
gboolean (*accounts_rename)(const char * const account_name,
@ -85,6 +65,5 @@ void (*accounts_set_priority_all)(const char * const account_name, const gint va
gint (*accounts_get_priority_for_presence_type)(const char * const account_name,
resource_presence_t presence_type);
void (*accounts_clear_password)(const char * const account_name);
char * (*accounts_create_full_jid)(ProfAccount *account);
#endif

View File

@ -27,6 +27,7 @@
#include "log.h"
#include "muc.h"
#include "config/preferences.h"
#include "config/account.h"
#include "roster_list.h"
#include "ui/ui.h"
@ -67,7 +68,7 @@ handle_login_account_success(char *account_name)
status_bar_print_message(account->jid);
status_bar_refresh();
accounts_free_account(account);
account_free(account);
}
void

View File

@ -36,25 +36,6 @@ _mock_accounts_get_account(const char * const name)
return (ProfAccount *)mock();
}
static char *
_mock_accounts_create_full_jid(ProfAccount *account)
{
check_expected(account);
return (char *)mock();
}
void
_stub_accounts_free_account(ProfAccount *account)
{
// do nothing
}
void
_mock_accounts_free_account(ProfAccount *account)
{
check_expected(account);
}
gchar **
_mock_accounts_get_list(void)
{
@ -274,24 +255,6 @@ mock_accounts_get_account(void)
accounts_get_account = _mock_accounts_get_account;
}
void
mock_accounts_create_full_jid(void)
{
accounts_create_full_jid = _mock_accounts_create_full_jid;
}
void
stub_accounts_free_account(void)
{
accounts_free_account = _stub_accounts_free_account;
}
void
mock_accounts_free_account(void)
{
accounts_free_account = _mock_accounts_free_account;
}
void
mock_accounts_get_list(void)
{
@ -460,23 +423,6 @@ accounts_get_account_return(ProfAccount *account)
will_return(_mock_accounts_get_account, account);
}
void
accounts_create_full_jid_return(char *fulljid)
{
expect_any(_mock_accounts_create_full_jid, account);
if (fulljid != NULL) {
will_return(_mock_accounts_create_full_jid, strdup(fulljid));
} else {
will_return(_mock_accounts_create_full_jid, NULL);
}
}
void
accounts_free_account_expect(ProfAccount *account)
{
expect_memory(_mock_accounts_free_account, account, account, sizeof(ProfAccount));
}
void
accounts_get_list_return(gchar **accounts)
{

View File

@ -24,13 +24,6 @@ void mock_accounts_get_account(void);
void accounts_get_account_expect_and_return(const char * const name, ProfAccount *account);
void accounts_get_account_return(ProfAccount *account);
void mock_accounts_create_full_jid(void);
void accounts_create_full_jid_return(char *fulljid);
void mock_accounts_free_account(void);
void stub_accounts_free_account(void);
void accounts_free_account_expect(ProfAccount *account);
void mock_accounts_get_list(void);
void accounts_get_list_return(gchar **accounts);

View File

@ -38,9 +38,9 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state)
{
mock_cons_show_account();
mock_accounts_get_account();
stub_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
ProfAccount *account = malloc(sizeof(ProfAccount));
ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL,
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL);
gchar *args[] = { NULL };
mock_connection_status(JABBER_CONNECTED);
@ -54,7 +54,6 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state)
assert_true(result);
free(help);
free(account);
}
void cmd_account_list_shows_accounts(void **state)
@ -117,10 +116,10 @@ void cmd_account_show_shows_account_when_exists(void **state)
{
mock_cons_show_account();
mock_accounts_get_account();
stub_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "show", "account_name", NULL };
ProfAccount *account = malloc(sizeof(ProfAccount));
ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL,
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL);
accounts_get_account_return(account);

View File

@ -418,21 +418,19 @@ void cmd_connect_lowercases_argument(void **state)
void cmd_connect_asks_password_when_not_in_account(void **state)
{
stub_cons_show();
stub_ui_ask_password();
mock_ui_ask_password();
mock_accounts_get_account();
mock_accounts_create_full_jid();
mock_jabber_connect_with_account();
stub_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = malloc(sizeof(ProfAccount));
account->password = NULL;
ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL,
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
accounts_get_account_return(account);
accounts_create_full_jid_return("user@jabber.org");
mock_ui_ask_password_returns("password");
jabber_connect_with_account_return(JABBER_CONNECTING);
@ -440,28 +438,22 @@ void cmd_connect_asks_password_when_not_in_account(void **state)
assert_true(result);
free(help);
free(account);
}
void cmd_connect_shows_message_when_connecting_with_account(void **state)
{
mock_cons_show();
mock_accounts_get_account();
mock_accounts_create_full_jid();
mock_jabber_connect_with_account();
stub_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = malloc(sizeof(ProfAccount));
account->password = "password";
account->name = "jabber_org";
ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password",
TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
accounts_get_account_return(account);
accounts_create_full_jid_return("user@jabber.org/laptop");
expect_cons_show("Connecting with account jabber_org as user@jabber.org/laptop");
jabber_connect_with_account_return(JABBER_CONNECTING);
@ -470,61 +462,26 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state)
assert_true(result);
free(help);
free(account);
}
void cmd_connect_connects_with_account(void **state)
{
stub_cons_show();
mock_accounts_get_account();
mock_accounts_create_full_jid();
mock_jabber_connect_with_account();
stub_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = malloc(sizeof(ProfAccount));
account->password = "password";
account->name = "jabber_org";
ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password",
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
accounts_get_account_return(account);
accounts_create_full_jid_return("user@jabber.org/laptop");
jabber_connect_with_account_expect_and_return(account, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
free(help);
free(account);
}
void cmd_connect_frees_account_after_connecting(void **state)
{
stub_cons_show();
mock_accounts_get_account();
mock_accounts_create_full_jid();
mock_jabber_connect_with_account();
mock_accounts_free_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = malloc(sizeof(ProfAccount));
mock_connection_status(JABBER_DISCONNECTED);
accounts_get_account_return(account);
accounts_create_full_jid_return("user@jabber.org/laptop");
jabber_connect_with_account_return(JABBER_CONNECTING);
accounts_free_account_expect(account);
gboolean result = cmd_connect(args, *help);
assert_true(result);
free(help);
free(account);
}

View File

@ -9,7 +9,6 @@ void cmd_connect_lowercases_argument(void **state);
void cmd_connect_asks_password_when_not_in_account(void **state);
void cmd_connect_shows_message_when_connecting_with_account(void **state);
void cmd_connect_connects_with_account(void **state);
void cmd_connect_frees_account_after_connecting(void **state);
void cmd_connect_shows_usage_when_no_server_value(void **state);
void cmd_connect_shows_usage_when_server_no_port_value(void **state);
void cmd_connect_shows_usage_when_no_port_value(void **state);

View File

@ -201,7 +201,6 @@ int main(int argc, char* argv[]) {
unit_test(cmd_connect_asks_password_when_not_in_account),
unit_test(cmd_connect_shows_message_when_connecting_with_account),
unit_test(cmd_connect_connects_with_account),
unit_test(cmd_connect_frees_account_after_connecting),
unit_test(cmd_connect_shows_usage_when_no_server_value),
unit_test(cmd_connect_shows_usage_when_server_no_port_value),
unit_test(cmd_connect_shows_usage_when_no_port_value),