diff --git a/account.cpp b/account.cpp index 5782a4e..efe4804 100644 --- a/account.cpp +++ b/account.cpp @@ -22,8 +22,7 @@ #include "channel.hh" #include "buffer.hh" -struct t_account *accounts = NULL; -struct t_account *last_account = NULL; +std::unordered_map accounts; char *account_options[ACCOUNT_NUM_OPTIONS][2] = { { (char*)"jid", (char*)"" }, @@ -39,34 +38,28 @@ char *account_options[ACCOUNT_NUM_OPTIONS][2] = struct t_account *account__search(const char *name) { - struct t_account *ptr_account; - if (!name) return NULL; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + auto ptr_account = accounts.find(name); + if (ptr_account != accounts.end()) { - if (strcmp(ptr_account->name, name) == 0) - return ptr_account; + return ptr_account->second; } /* account not found */ return NULL; } -struct t_account *account__casesearch (const char *name) +struct t_account *account__casesearch(const char *name) { - struct t_account *ptr_account; - if (!name) return NULL; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { - if (weechat_strcasecmp (ptr_account->name, name) == 0) - return ptr_account; + if (weechat_strcasecmp(ptr_account.second->name, name) == 0) + return ptr_account.second; } /* account not found */ @@ -416,14 +409,7 @@ struct t_account *account__alloc(const char *name) return NULL; } - /* add new account to queue */ - new_account->prev_account = last_account; - new_account->next_account = NULL; - if (last_account) - last_account->next_account = new_account; - else - accounts = new_account; - last_account = new_account; + accounts[name] = new_account; /* set name */ new_account->name = strdup(name); @@ -557,8 +543,6 @@ void account__free_data(struct t_account *account) void account__free(struct t_account *account) { - struct t_account *new_accounts; - if (!account) return; @@ -570,32 +554,17 @@ void account__free(struct t_account *account) if (account->buffer) weechat_buffer_close(account->buffer); - /* remove account from queue */ - if (last_account == account) - last_account = account->prev_account; - if (account->prev_account) - { - (account->prev_account)->next_account = account->next_account; - new_accounts = accounts; - } - else - new_accounts = account->next_account; - - if (account->next_account) - (account->next_account)->prev_account = account->prev_account; + accounts.erase(account->name); account__free_data(account); delete account; - account = nullptr; - accounts = new_accounts; } void account__free_all() { - /* for each account in memory, remove it */ - while (accounts) + for (auto account : accounts) { - account__free(accounts); + account__free(account.second); } } @@ -672,12 +641,9 @@ void account__disconnect(struct t_account *account, int reconnect) void account__disconnect_all() { - struct t_account *ptr_account; - - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { - account__disconnect(ptr_account, 0); + account__disconnect(ptr_account.second, 0); } } @@ -755,22 +721,19 @@ int account__timer_cb(const void *pointer, void *data, int remaining_calls) //try { - struct t_account *ptr_account; - - if (!accounts) return WEECHAT_RC_ERROR; + if (accounts.empty()) return WEECHAT_RC_ERROR; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account ? ptr_account->next_account : NULL) + for (auto ptr_account : accounts) { - if (ptr_account->is_connected - && (xmpp_conn_is_connecting(ptr_account->connection) - || xmpp_conn_is_connected(ptr_account->connection))) - connection__process(ptr_account->context, ptr_account->connection, 10); - else if (ptr_account->disconnected); - else if (ptr_account->reconnect_start > 0 - && ptr_account->reconnect_start < time(NULL)) + if (ptr_account.second->is_connected + && (xmpp_conn_is_connecting(ptr_account.second->connection) + || xmpp_conn_is_connected(ptr_account.second->connection))) + connection__process(ptr_account.second->context, ptr_account.second->connection, 10); + else if (ptr_account.second->disconnected); + else if (ptr_account.second->reconnect_start > 0 + && ptr_account.second->reconnect_start < time(NULL)) { - account__connect(ptr_account); + account__connect(ptr_account.second); } } diff --git a/account.hh b/account.hh index d51b63a..5cb84c0 100644 --- a/account.hh +++ b/account.hh @@ -7,12 +7,12 @@ #include #include #include +#include #include #include "omemo.hh" -extern struct t_account *accounts; -extern struct t_account *last_account; +extern std::unordered_map accounts; enum t_account_option { diff --git a/buffer.cpp b/buffer.cpp index 7bd22b9..de64a1b 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -26,23 +26,22 @@ void buffer__get_account_and_channel(struct t_gui_buffer *buffer, *channel = NULL; /* look for a account or channel using this buffer */ - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { - if (ptr_account->buffer == buffer) + if (ptr_account.second->buffer == buffer) { if (account) - *account = ptr_account; + *account = ptr_account.second; return; } - for (ptr_channel = ptr_account->channels; ptr_channel; + for (ptr_channel = ptr_account.second->channels; ptr_channel; ptr_channel = ptr_channel->next_channel) { if (ptr_channel->buffer == buffer) { if (account) - *account = ptr_account; + *account = ptr_account.second; if (channel) *channel = ptr_channel; return; diff --git a/channel.cpp b/channel.cpp index eb2a058..7e840ee 100644 --- a/channel.cpp +++ b/channel.cpp @@ -77,14 +77,13 @@ struct t_account *channel__account(struct t_channel *channel) if (!channel) return NULL; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { - for (ptr_channel = ptr_account->channels; ptr_channel; + for (ptr_channel = ptr_account.second->channels; ptr_channel; ptr_channel = ptr_channel->next_channel) { if (ptr_channel == channel) - return ptr_account; + return ptr_account.second; } } diff --git a/command.cpp b/command.cpp index 34392fa..0b603ae 100644 --- a/command.cpp +++ b/command.cpp @@ -80,14 +80,13 @@ void command__account_list(int argc, char **argv) } if (!account_name) { - if (accounts) + if (!accounts.empty()) { weechat_printf(NULL, ""); weechat_printf(NULL, _("All accounts:")); - for (ptr_account2 = accounts; ptr_account2; - ptr_account2 = ptr_account2->next_account) + for (auto ptr_account2 : accounts) { - command__display_account(ptr_account2); + command__display_account(ptr_account2.second); } } else @@ -96,10 +95,9 @@ void command__account_list(int argc, char **argv) else { one_account_found = 0; - for (ptr_account2 = accounts; ptr_account2; - ptr_account2 = ptr_account2->next_account) + for (auto ptr_account2 : accounts) { - if (weechat_strcasestr(ptr_account2->name, account_name)) + if (weechat_strcasestr(ptr_account2.second->name, account_name)) { if (!one_account_found) { @@ -109,7 +107,7 @@ void command__account_list(int argc, char **argv) account_name); } one_account_found = 1; - command__display_account(ptr_account2); + command__display_account(ptr_account2.second); } } if (!one_account_found) diff --git a/completion.cpp b/completion.cpp index efa4898..2e78130 100644 --- a/completion.cpp +++ b/completion.cpp @@ -113,10 +113,9 @@ int completion__accounts_cb(const void *pointer, void *data, (void) completion_item; (void) buffer; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { - weechat_hook_completion_list_add(completion, account_jid(ptr_account), + weechat_hook_completion_list_add(completion, account_jid(ptr_account.second), 0, WEECHAT_LIST_POS_SORT); } diff --git a/config.cpp b/config.cpp index 642bffd..079cd2a 100644 --- a/config.cpp +++ b/config.cpp @@ -361,13 +361,12 @@ int config__account_write_cb(const void *pointer, void *data, if (!weechat_config_write_line(config_file, section_name, NULL)) return WEECHAT_CONFIG_WRITE_ERROR; - for (ptr_account = accounts; ptr_account; - ptr_account = ptr_account->next_account) + for (auto ptr_account : accounts) { for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++) { if (!weechat_config_write_option(config_file, - ptr_account->options[i])) + ptr_account.second->options[i])) return WEECHAT_CONFIG_WRITE_ERROR; } }