Commit 7fc88c7f authored by Emmanuel Milou's avatar Emmanuel Milou

[#1215] Redesign current account computing

parent 008b1169
......@@ -23,7 +23,6 @@
#include <string.h>
GQueue * accountQueue;
gchar* __CURRENT_ACCOUNT_ID = NULL;
/* GCompareFunc to compare a accountID (gchar* and a account_t) */
gint
......@@ -129,23 +128,35 @@ account_list_get_nth ( guint n )
account_t*
account_list_get_current( )
{
if( __CURRENT_ACCOUNT_ID != NULL )
return account_list_get_by_id( __CURRENT_ACCOUNT_ID );
else
return NULL;
}
void
account_list_set_current_id(const gchar * accountID)
{
DEBUG("set current id = %s", accountID);
__CURRENT_ACCOUNT_ID = g_strdup(accountID);
account_t *current;
// No account registered
if (account_list_get_registered_accounts () == 0)
return NULL;
// if we are here, it means that we have at least one registered account in the list
// So we get the first one
current = account_list_get_by_state (ACCOUNT_STATE_REGISTERED);
if (!current)
return NULL;
return current;
}
void
account_list_set_current_pos( guint n)
void account_list_set_current (account_t *current)
{
__CURRENT_ACCOUNT_ID = account_list_get_nth(n)->accountID;
gpointer acc;
guint pos;
// 2 steps:
// 1 - retrieve the index of the current account in the Queue
// 2 - then set it as first
pos = account_list_get_position (current);
if (pos > 0)
{
acc = g_queue_pop_nth(accountQueue, pos);
g_queue_push_nth(accountQueue, acc, 0);
}
}
......@@ -198,12 +209,13 @@ account_list_clear ( )
void
account_list_move_up(guint index)
{
DEBUG ("index = %i\n", index);
if(index != 0)
{
gpointer acc = g_queue_pop_nth(accountQueue, index);
g_queue_push_nth(accountQueue, acc, index-1);
}
account_list_set_current_pos( 0 );
}
void
......@@ -214,7 +226,6 @@ account_list_move_down(guint index)
gpointer acc = g_queue_pop_nth(accountQueue, index);
g_queue_push_nth(accountQueue, acc, index+1);
}
account_list_set_current_pos( 0 );
}
guint
......@@ -232,10 +243,14 @@ account_list_get_registered_accounts( void )
}
gchar* account_list_get_current_id( void ){
if( __CURRENT_ACCOUNT_ID == NULL )
return "";
else
return __CURRENT_ACCOUNT_ID;
account_t *current;
current = account_list_get_current ();
if (current)
return current->accountID;
else
return "";
}
int account_list_get_sip_account_number( void ){
......@@ -283,3 +298,22 @@ gchar * account_list_get_ordered_list (void) {
}
return order;
}
guint account_list_get_position (account_t *account)
{
guint size, i;
account_t *tmp;
size = account_list_get_size ();
for (i=0; i<size; i++)
{
tmp = account_list_get_nth (i);
if (g_strcasecmp (tmp->accountID, account->accountID) == 0)
{
return i;
}
}
// Not found
return -1;
}
......@@ -125,15 +125,9 @@ account_t * account_list_get_current( );
/**
* This function sets an account as the current one
* @param accountID The ID of the current account
* @param current the account you want to set as current
*/
void account_list_set_current_id(const gchar * accountID);
/**
* This function sets an account as the current one
* @param n the position of the account you want to use
*/
void account_list_set_current_pos( guint n );
void account_list_set_current (account_t *current);
/**
* This function maps account_state_t enums to a description.
......@@ -184,5 +178,6 @@ int account_list_get_iax_account_number( void );
gchar * account_list_get_ordered_list (void);
guint account_list_get_position (account_t *account);
#endif
......@@ -81,9 +81,13 @@ status_bar_display_account ()
msg = g_markup_printf_escaped(_("Registered to %s (%s)") ,
(gchar*)g_hash_table_lookup( acc->properties , ACCOUNT_ALIAS),
(gchar*)g_hash_table_lookup( acc->properties , ACCOUNT_TYPE));
statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT);
g_free(msg);
}
else
{
msg = g_markup_printf_escaped(_("No registered account"));
}
statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT);
g_free(msg);
}
......@@ -235,7 +239,6 @@ gboolean sflphone_init()
codec_list_init();
sflphone_fill_account_list(FALSE);
sflphone_fill_codec_list();
sflphone_set_current_account();
return TRUE;
}
}
......@@ -680,7 +683,6 @@ sflphone_keypad( guint keyval, gchar * key)
sflphone_place_call ( call_t * c )
{
if (c->state == CALL_STATE_DIALING && g_str_has_prefix (c->to, "ip:"))
{
dbus_place_call (c);
......@@ -736,7 +738,6 @@ sflphone_place_call ( call_t * c )
c -> accountID = current -> accountID;
dbus_place_call(c);
notify_current_account( current );
account_list_set_current_id( c-> accountID );
}
}
else
......@@ -749,7 +750,6 @@ sflphone_place_call ( call_t * c )
c -> accountID = current -> accountID;
dbus_place_call(c);
notify_current_account( current );
account_list_set_current_id( c-> accountID );
}
}
// Update history
......@@ -819,15 +819,6 @@ sflphone_rec_call()
// DEBUG("sflphone_get_current_codec_name: %s",codname);
}
/* Internal to action - set the __CURRENT_ACCOUNT variable */
void
sflphone_set_current_account()
{
if( account_list_get_size() > 0 )
account_list_set_current_pos( 0 );
}
/* Internal to action - get the codec list */
void
sflphone_fill_codec_list()
......
......@@ -104,7 +104,6 @@ static void sip_apply_callback( void ) {
g_hash_table_insert(current->properties, g_strdup(ACCOUNT_SIP_STUN_SERVER), g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(wiz->addr))));
dbus_add_account( current );
account_list_set_current_id( current->accountID );
sprintf(message, MESSAGE_SUMMARY,
gtk_entry_get_text (GTK_ENTRY(wiz->sip_alias)),
gtk_entry_get_text (GTK_ENTRY(wiz->sip_server)),
......@@ -129,7 +128,6 @@ static void iax_apply_callback( void ) {
g_hash_table_insert(current->properties, g_strdup(ACCOUNT_HOSTNAME), g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(wiz->iax_server))));
g_hash_table_insert(current->properties, g_strdup(ACCOUNT_PASSWORD), g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(wiz->iax_password))));
dbus_add_account( current );
account_list_set_current_id( current->accountID );
sprintf(message, MESSAGE_SUMMARY,
gtk_entry_get_text (GTK_ENTRY(wiz->iax_alias)),
gtk_entry_get_text (GTK_ENTRY(wiz->iax_server)),
......
......@@ -309,11 +309,9 @@ show_account_window (account_t * a)
/** @todo Verify if it's the best condition to check */
if (currentAccount->accountID == NULL) {
dbus_add_account(currentAccount);
account_list_set_current_id( currentAccount->accountID );
}
else {
dbus_set_account_details(currentAccount);
account_list_set_current_id( currentAccount->accountID);
}
}
gtk_widget_destroy (GTK_WIDGET(dialog));
......
......@@ -858,8 +858,6 @@ show_accounts_window( void )
accDialogOpen=FALSE;
gtk_widget_destroy(GTK_WIDGET(dialog));
if( account_list_get_size() >0 && account_list_get_current()==NULL )
account_list_set_current_pos(0);
toolbar_update_buttons();
}
......
......@@ -41,6 +41,13 @@ addressbook_is_ready();
gboolean
addressbook_is_enabled();
/**
* Return if at least one addressbook is active
*/
gboolean
addressbook_is_active();
/**
* Perform a search in addressbook
*/
......
This diff is collapsed.
......@@ -24,22 +24,27 @@
#include "../src/accountlist.h"
#include "../src/sflphone_const.h"
account_t* create_test_account ()
account_t* create_test_account (gchar *alias)
{
account_t *test;
gchar *id;
srand(time(NULL));
test = g_new0 (account_t, 1);
test->accountID = "test";
id = g_new0(gchar, 30);
g_sprintf(id, "%s-%d", alias, rand());
test->accountID = g_strdup (id);
test->state = ACCOUNT_STATE_REGISTERED;
test->properties = g_hash_table_new(NULL, g_str_equal);
// Populate the properties
g_hash_table_replace (test->properties, ACCOUNT_ENABLED, "1");
g_hash_table_replace (test->properties, ACCOUNT_ALIAS, "test account");
g_hash_table_replace (test->properties, ACCOUNT_ALIAS, alias);
g_hash_table_replace (test->properties, ACCOUNT_TYPE, "SIP");
g_hash_table_replace (test->properties, ACCOUNT_HOSTNAME, "192.168.1.1");
g_hash_table_replace (test->properties, ACCOUNT_USERNAME, "test");
g_hash_table_replace (test->properties, ACCOUNT_PASSWORD, "my-password");
g_hash_table_replace (test->properties, ACCOUNT_HOSTNAME, "sflphone.org");
g_hash_table_replace (test->properties, ACCOUNT_USERNAME, "1260");
g_hash_table_replace (test->properties, ACCOUNT_PASSWORD, "NIPAgmLo");
g_hash_table_replace (test->properties, ACCOUNT_MAILBOX, "888");
g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_SERVER, "");
g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_ENABLED, "0");
......@@ -50,7 +55,7 @@ account_t* create_test_account ()
START_TEST (test_add_account)
{
account_t *test = create_test_account ();
account_t *test = create_test_account ("test");
account_list_init ();
account_list_add (test);
......@@ -62,18 +67,21 @@ END_TEST
START_TEST (test_ordered_list)
{
account_t *test = create_test_account ();
gchar *list;
account_t *test = create_test_account ("test");
list = g_new0(gchar, 30);
g_sprintf(list, "%s/%s/", test->accountID, test->accountID);
account_list_init ();
account_list_add (test);
account_list_add (test);
fail_unless (g_strcasecmp (account_list_get_ordered_list (), "test/test/") == 0, "ERROR - BAD ACCOUNT LIST SERIALIZING");
fail_unless (g_strcasecmp (account_list_get_ordered_list (), list) == 0, "ERROR - BAD ACCOUNT LIST SERIALIZING");
}
END_TEST
START_TEST (test_get_by_id)
{
account_t *test = create_test_account ();
account_t *test = create_test_account ("test");
account_t *tmp;
account_list_init ();
......@@ -85,7 +93,7 @@ END_TEST
START_TEST (test_sip_account)
{
account_t *test = create_test_account ();
account_t *test = create_test_account ("test");
account_list_init ();
account_list_add (test);
......@@ -93,14 +101,63 @@ START_TEST (test_sip_account)
}
END_TEST
START_TEST (test_set_current_account)
START_TEST (test_get_account_position)
{
guint pos, pos1;
account_t *test = create_test_account ("test");
account_t *test2 = create_test_account ("test2");
account_list_init ();
account_list_add (test);
account_list_add (test2);
pos = account_list_get_position (test);
pos1 = account_list_get_position (test2);
fail_if (pos == -1, "ERROR - bad account position");
fail_unless (pos == 0, "ERROR - bad account position");
fail_if (pos1 == -1, "ERROR - bad account position");
fail_unless (pos1 == 1, "ERROR - bad account position");
account_list_set_current (test);
pos = account_list_get_position (test);
pos1 = account_list_get_position (test2);
fail_if (pos == -1, "ERROR - bad account position");
fail_unless (pos == 0, "ERROR - bad account position");
fail_unless (pos1 == 1, "ERROR - bad account position");
}
END_TEST
START_TEST (test_get_current_account)
{
account_t *test = create_test_account ();
account_t *test = create_test_account ("test");
account_t *test2 = create_test_account ("test2");
account_t *current;
account_list_init ();
account_list_add (test);
account_list_set_current_id (test->accountID);
fail_unless (account_list_get_sip_account_number () == 1, "ERROR - BAD CURRENT ACCOUNT");
account_list_add (test2);
current = account_list_get_current ();
fail_unless (current != NULL, "ERROR - current account NULL");
// The current account must be the first we add
if (current)
{
fail_unless (g_strcasecmp (g_hash_table_lookup(current->properties, ACCOUNT_ALIAS) ,
g_hash_table_lookup(test->properties, ACCOUNT_ALIAS)) == 0,
"ERROR - BAD CURRENT ACCOUNT");
}
// Then we try to change the current account
account_list_set_current (test2);
current = account_list_get_current ();
fail_unless (current != NULL, "ERROR - current account NULL");
// The current account must be the first we add
if (current)
{
fail_unless (g_strcasecmp (g_hash_table_lookup(current->properties, ACCOUNT_ALIAS) ,
g_hash_table_lookup(test2->properties, ACCOUNT_ALIAS)) == 0,
"ERROR - BAD CURRENT ACCOUNT");
}
}
END_TEST
......@@ -115,6 +172,8 @@ global_suite (void)
tcase_add_test (tc_cases, test_ordered_list);
tcase_add_test (tc_cases, test_sip_account);
tcase_add_test (tc_cases, test_get_by_id);
tcase_add_test (tc_cases, test_get_account_position);
tcase_add_test (tc_cases, test_get_current_account);
suite_add_tcase (s, tc_cases);
return s;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment