Skip to content
Snippets Groups Projects
Commit 7fc88c7f authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

[#1215] Redesign current account computing

parent 008b1169
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include <string.h> #include <string.h>
GQueue * accountQueue; GQueue * accountQueue;
gchar* __CURRENT_ACCOUNT_ID = NULL;
/* GCompareFunc to compare a accountID (gchar* and a account_t) */ /* GCompareFunc to compare a accountID (gchar* and a account_t) */
gint gint
...@@ -129,23 +128,35 @@ account_list_get_nth ( guint n ) ...@@ -129,23 +128,35 @@ account_list_get_nth ( guint n )
account_t* account_t*
account_list_get_current( ) account_list_get_current( )
{ {
if( __CURRENT_ACCOUNT_ID != NULL ) account_t *current;
return account_list_get_by_id( __CURRENT_ACCOUNT_ID );
else // No account registered
if (account_list_get_registered_accounts () == 0)
return NULL; 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 void account_list_set_current (account_t *current)
account_list_set_current_id(const gchar * accountID)
{ {
DEBUG("set current id = %s", accountID); gpointer acc;
__CURRENT_ACCOUNT_ID = g_strdup(accountID); guint pos;
}
void // 2 steps:
account_list_set_current_pos( guint n) // 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)
{ {
__CURRENT_ACCOUNT_ID = account_list_get_nth(n)->accountID; acc = g_queue_pop_nth(accountQueue, pos);
g_queue_push_nth(accountQueue, acc, 0);
}
} }
...@@ -198,12 +209,13 @@ account_list_clear ( ) ...@@ -198,12 +209,13 @@ account_list_clear ( )
void void
account_list_move_up(guint index) account_list_move_up(guint index)
{ {
DEBUG ("index = %i\n", index);
if(index != 0) if(index != 0)
{ {
gpointer acc = g_queue_pop_nth(accountQueue, index); gpointer acc = g_queue_pop_nth(accountQueue, index);
g_queue_push_nth(accountQueue, acc, index-1); g_queue_push_nth(accountQueue, acc, index-1);
} }
account_list_set_current_pos( 0 );
} }
void void
...@@ -214,7 +226,6 @@ account_list_move_down(guint index) ...@@ -214,7 +226,6 @@ account_list_move_down(guint index)
gpointer acc = g_queue_pop_nth(accountQueue, index); gpointer acc = g_queue_pop_nth(accountQueue, index);
g_queue_push_nth(accountQueue, acc, index+1); g_queue_push_nth(accountQueue, acc, index+1);
} }
account_list_set_current_pos( 0 );
} }
guint guint
...@@ -232,10 +243,14 @@ account_list_get_registered_accounts( void ) ...@@ -232,10 +243,14 @@ account_list_get_registered_accounts( void )
} }
gchar* account_list_get_current_id( void ){ gchar* account_list_get_current_id( void ){
if( __CURRENT_ACCOUNT_ID == NULL )
return ""; account_t *current;
current = account_list_get_current ();
if (current)
return current->accountID;
else else
return __CURRENT_ACCOUNT_ID; return "";
} }
int account_list_get_sip_account_number( void ){ int account_list_get_sip_account_number( void ){
...@@ -283,3 +298,22 @@ gchar * account_list_get_ordered_list (void) { ...@@ -283,3 +298,22 @@ gchar * account_list_get_ordered_list (void) {
} }
return order; 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( ); ...@@ -125,15 +125,9 @@ account_t * account_list_get_current( );
/** /**
* This function sets an account as the current one * 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); void account_list_set_current (account_t *current);
/**
* 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 );
/** /**
* This function maps account_state_t enums to a description. * This function maps account_state_t enums to a description.
...@@ -184,5 +178,6 @@ int account_list_get_iax_account_number( void ); ...@@ -184,5 +178,6 @@ int account_list_get_iax_account_number( void );
gchar * account_list_get_ordered_list (void); gchar * account_list_get_ordered_list (void);
guint account_list_get_position (account_t *account);
#endif #endif
...@@ -81,10 +81,14 @@ status_bar_display_account () ...@@ -81,10 +81,14 @@ status_bar_display_account ()
msg = g_markup_printf_escaped(_("Registered to %s (%s)") , 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_ALIAS),
(gchar*)g_hash_table_lookup( acc->properties , ACCOUNT_TYPE)); (gchar*)g_hash_table_lookup( acc->properties , ACCOUNT_TYPE));
}
else
{
msg = g_markup_printf_escaped(_("No registered account"));
}
statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT); statusbar_push_message( msg , __MSG_ACCOUNT_DEFAULT);
g_free(msg); g_free(msg);
} }
}
gboolean gboolean
...@@ -235,7 +239,6 @@ gboolean sflphone_init() ...@@ -235,7 +239,6 @@ gboolean sflphone_init()
codec_list_init(); codec_list_init();
sflphone_fill_account_list(FALSE); sflphone_fill_account_list(FALSE);
sflphone_fill_codec_list(); sflphone_fill_codec_list();
sflphone_set_current_account();
return TRUE; return TRUE;
} }
} }
...@@ -680,7 +683,6 @@ sflphone_keypad( guint keyval, gchar * key) ...@@ -680,7 +683,6 @@ sflphone_keypad( guint keyval, gchar * key)
sflphone_place_call ( call_t * c ) sflphone_place_call ( call_t * c )
{ {
if (c->state == CALL_STATE_DIALING && g_str_has_prefix (c->to, "ip:")) if (c->state == CALL_STATE_DIALING && g_str_has_prefix (c->to, "ip:"))
{ {
dbus_place_call (c); dbus_place_call (c);
...@@ -736,7 +738,6 @@ sflphone_place_call ( call_t * c ) ...@@ -736,7 +738,6 @@ sflphone_place_call ( call_t * c )
c -> accountID = current -> accountID; c -> accountID = current -> accountID;
dbus_place_call(c); dbus_place_call(c);
notify_current_account( current ); notify_current_account( current );
account_list_set_current_id( c-> accountID );
} }
} }
else else
...@@ -749,7 +750,6 @@ sflphone_place_call ( call_t * c ) ...@@ -749,7 +750,6 @@ sflphone_place_call ( call_t * c )
c -> accountID = current -> accountID; c -> accountID = current -> accountID;
dbus_place_call(c); dbus_place_call(c);
notify_current_account( current ); notify_current_account( current );
account_list_set_current_id( c-> accountID );
} }
} }
// Update history // Update history
...@@ -819,15 +819,6 @@ sflphone_rec_call() ...@@ -819,15 +819,6 @@ sflphone_rec_call()
// DEBUG("sflphone_get_current_codec_name: %s",codname); // 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 */ /* Internal to action - get the codec list */
void void
sflphone_fill_codec_list() sflphone_fill_codec_list()
......
...@@ -104,7 +104,6 @@ static void sip_apply_callback( void ) { ...@@ -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)))); 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 ); dbus_add_account( current );
account_list_set_current_id( current->accountID );
sprintf(message, MESSAGE_SUMMARY, sprintf(message, MESSAGE_SUMMARY,
gtk_entry_get_text (GTK_ENTRY(wiz->sip_alias)), gtk_entry_get_text (GTK_ENTRY(wiz->sip_alias)),
gtk_entry_get_text (GTK_ENTRY(wiz->sip_server)), gtk_entry_get_text (GTK_ENTRY(wiz->sip_server)),
...@@ -129,7 +128,6 @@ static void iax_apply_callback( void ) { ...@@ -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_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)))); 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 ); dbus_add_account( current );
account_list_set_current_id( current->accountID );
sprintf(message, MESSAGE_SUMMARY, sprintf(message, MESSAGE_SUMMARY,
gtk_entry_get_text (GTK_ENTRY(wiz->iax_alias)), gtk_entry_get_text (GTK_ENTRY(wiz->iax_alias)),
gtk_entry_get_text (GTK_ENTRY(wiz->iax_server)), gtk_entry_get_text (GTK_ENTRY(wiz->iax_server)),
......
...@@ -309,11 +309,9 @@ show_account_window (account_t * a) ...@@ -309,11 +309,9 @@ show_account_window (account_t * a)
/** @todo Verify if it's the best condition to check */ /** @todo Verify if it's the best condition to check */
if (currentAccount->accountID == NULL) { if (currentAccount->accountID == NULL) {
dbus_add_account(currentAccount); dbus_add_account(currentAccount);
account_list_set_current_id( currentAccount->accountID );
} }
else { else {
dbus_set_account_details(currentAccount); dbus_set_account_details(currentAccount);
account_list_set_current_id( currentAccount->accountID);
} }
} }
gtk_widget_destroy (GTK_WIDGET(dialog)); gtk_widget_destroy (GTK_WIDGET(dialog));
......
...@@ -858,8 +858,6 @@ show_accounts_window( void ) ...@@ -858,8 +858,6 @@ show_accounts_window( void )
accDialogOpen=FALSE; accDialogOpen=FALSE;
gtk_widget_destroy(GTK_WIDGET(dialog)); 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(); toolbar_update_buttons();
} }
......
...@@ -41,6 +41,13 @@ addressbook_is_ready(); ...@@ -41,6 +41,13 @@ addressbook_is_ready();
gboolean gboolean
addressbook_is_enabled(); addressbook_is_enabled();
/**
* Return if at least one addressbook is active
*/
gboolean
addressbook_is_active();
/** /**
* Perform a search in addressbook * Perform a search in addressbook
*/ */
......
...@@ -203,7 +203,7 @@ switch_account( GtkWidget* item , gpointer data UNUSED) ...@@ -203,7 +203,7 @@ switch_account( GtkWidget* item , gpointer data UNUSED)
{ {
account_t* acc = g_object_get_data( G_OBJECT(item) , "account" ); account_t* acc = g_object_get_data( G_OBJECT(item) , "account" );
DEBUG("%s" , acc->accountID); DEBUG("%s" , acc->accountID);
account_list_set_current_id( acc->accountID ); account_list_set_current (acc);
status_bar_display_account (); status_bar_display_account ();
} }
...@@ -1009,7 +1009,7 @@ void add_registered_accounts_to_menu (GtkWidget *menu) { ...@@ -1009,7 +1009,7 @@ void add_registered_accounts_to_menu (GtkWidget *menu) {
GtkWidget *menu_items; GtkWidget *menu_items;
unsigned int i; unsigned int i;
account_t* acc; account_t* acc, *current;
gchar* alias; gchar* alias;
menu_items = gtk_separator_menu_item_new (); menu_items = gtk_separator_menu_item_new ();
...@@ -1025,9 +1025,10 @@ void add_registered_accounts_to_menu (GtkWidget *menu) { ...@@ -1025,9 +1025,10 @@ void add_registered_accounts_to_menu (GtkWidget *menu) {
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_items); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_items);
g_object_set_data( G_OBJECT( menu_items ) , "account" , acc ); g_object_set_data( G_OBJECT( menu_items ) , "account" , acc );
g_free( alias ); g_free( alias );
if( account_list_get_current() != NULL ){ current = account_list_get_current();
if(current){
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_items), gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_items),
(g_strcasecmp( acc->accountID , account_list_get_current()->accountID) == 0)? TRUE : FALSE); (g_strcasecmp( acc->accountID , current->accountID) == 0)? TRUE : FALSE);
} }
g_signal_connect (G_OBJECT (menu_items), "activate", g_signal_connect (G_OBJECT (menu_items), "activate",
G_CALLBACK (switch_account), G_CALLBACK (switch_account),
......
...@@ -24,22 +24,27 @@ ...@@ -24,22 +24,27 @@
#include "../src/accountlist.h" #include "../src/accountlist.h"
#include "../src/sflphone_const.h" #include "../src/sflphone_const.h"
account_t* create_test_account () account_t* create_test_account (gchar *alias)
{ {
account_t *test; account_t *test;
gchar *id;
srand(time(NULL));
test = g_new0 (account_t, 1); 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->state = ACCOUNT_STATE_REGISTERED;
test->properties = g_hash_table_new(NULL, g_str_equal); test->properties = g_hash_table_new(NULL, g_str_equal);
// Populate the properties // Populate the properties
g_hash_table_replace (test->properties, ACCOUNT_ENABLED, "1"); 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_TYPE, "SIP");
g_hash_table_replace (test->properties, ACCOUNT_HOSTNAME, "192.168.1.1"); g_hash_table_replace (test->properties, ACCOUNT_HOSTNAME, "sflphone.org");
g_hash_table_replace (test->properties, ACCOUNT_USERNAME, "test"); g_hash_table_replace (test->properties, ACCOUNT_USERNAME, "1260");
g_hash_table_replace (test->properties, ACCOUNT_PASSWORD, "my-password"); 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_MAILBOX, "888");
g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_SERVER, ""); g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_SERVER, "");
g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_ENABLED, "0"); g_hash_table_replace (test->properties, ACCOUNT_SIP_STUN_ENABLED, "0");
...@@ -50,7 +55,7 @@ account_t* create_test_account () ...@@ -50,7 +55,7 @@ account_t* create_test_account ()
START_TEST (test_add_account) START_TEST (test_add_account)
{ {
account_t *test = create_test_account (); account_t *test = create_test_account ("test");
account_list_init (); account_list_init ();
account_list_add (test); account_list_add (test);
...@@ -62,18 +67,21 @@ END_TEST ...@@ -62,18 +67,21 @@ END_TEST
START_TEST (test_ordered_list) 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_init ();
account_list_add (test); account_list_add (test);
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 END_TEST
START_TEST (test_get_by_id) START_TEST (test_get_by_id)
{ {
account_t *test = create_test_account (); account_t *test = create_test_account ("test");
account_t *tmp; account_t *tmp;
account_list_init (); account_list_init ();
...@@ -85,7 +93,7 @@ END_TEST ...@@ -85,7 +93,7 @@ END_TEST
START_TEST (test_sip_account) START_TEST (test_sip_account)
{ {
account_t *test = create_test_account (); account_t *test = create_test_account ("test");
account_list_init (); account_list_init ();
account_list_add (test); account_list_add (test);
...@@ -93,14 +101,63 @@ START_TEST (test_sip_account) ...@@ -93,14 +101,63 @@ START_TEST (test_sip_account)
} }
END_TEST END_TEST
START_TEST (test_set_current_account) START_TEST (test_get_account_position)
{ {
account_t *test = create_test_account (); guint pos, pos1;
account_t *test = create_test_account ("test");
account_t *test2 = create_test_account ("test2");
account_list_init (); account_list_init ();
account_list_add (test); account_list_add (test);
account_list_set_current_id (test->accountID); account_list_add (test2);
fail_unless (account_list_get_sip_account_number () == 1, "ERROR - BAD CURRENT ACCOUNT");
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 ("test");
account_t *test2 = create_test_account ("test2");
account_t *current;
account_list_init ();
account_list_add (test);
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 END_TEST
...@@ -115,6 +172,8 @@ global_suite (void) ...@@ -115,6 +172,8 @@ global_suite (void)
tcase_add_test (tc_cases, test_ordered_list); tcase_add_test (tc_cases, test_ordered_list);
tcase_add_test (tc_cases, test_sip_account); tcase_add_test (tc_cases, test_sip_account);
tcase_add_test (tc_cases, test_get_by_id); 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); suite_add_tcase (s, tc_cases);
return s; return s;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment