accounts -> ++

master
bqv 3 years ago
parent e6feccaf79
commit 7a47010b71
No known key found for this signature in database
GPG Key ID: 9E2FF3BDEBDFC910

@ -22,8 +22,7 @@
#include "channel.hh" #include "channel.hh"
#include "buffer.hh" #include "buffer.hh"
struct t_account *accounts = NULL; std::unordered_map<std::string, struct t_account *> accounts;
struct t_account *last_account = NULL;
char *account_options[ACCOUNT_NUM_OPTIONS][2] = char *account_options[ACCOUNT_NUM_OPTIONS][2] =
{ { (char*)"jid", (char*)"" }, { { (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 *account__search(const char *name)
{ {
struct t_account *ptr_account;
if (!name) if (!name)
return NULL; return NULL;
for (ptr_account = accounts; ptr_account; auto ptr_account = accounts.find(name);
ptr_account = ptr_account->next_account) if (ptr_account != accounts.end())
{ {
if (strcmp(ptr_account->name, name) == 0) return ptr_account->second;
return ptr_account;
} }
/* account not found */ /* account not found */
return NULL; 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) if (!name)
return NULL; return NULL;
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account->next_account)
{ {
if (weechat_strcasecmp (ptr_account->name, name) == 0) if (weechat_strcasecmp(ptr_account.second->name, name) == 0)
return ptr_account; return ptr_account.second;
} }
/* account not found */ /* account not found */
@ -416,14 +409,7 @@ struct t_account *account__alloc(const char *name)
return NULL; return NULL;
} }
/* add new account to queue */ accounts[name] = new_account;
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;
/* set name */ /* set name */
new_account->name = strdup(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) void account__free(struct t_account *account)
{ {
struct t_account *new_accounts;
if (!account) if (!account)
return; return;
@ -570,32 +554,17 @@ void account__free(struct t_account *account)
if (account->buffer) if (account->buffer)
weechat_buffer_close(account->buffer); weechat_buffer_close(account->buffer);
/* remove account from queue */ accounts.erase(account->name);
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;
account__free_data(account); account__free_data(account);
delete account; delete account;
account = nullptr;
accounts = new_accounts;
} }
void account__free_all() void account__free_all()
{ {
/* for each account in memory, remove it */ for (auto account : accounts)
while (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() void account__disconnect_all()
{ {
struct t_account *ptr_account; for (auto ptr_account : accounts)
for (ptr_account = accounts; ptr_account;
ptr_account = ptr_account->next_account)
{ {
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 //try
{ {
struct t_account *ptr_account; if (accounts.empty()) return WEECHAT_RC_ERROR;
if (!accounts) return WEECHAT_RC_ERROR;
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account ? ptr_account->next_account : NULL)
{ {
if (ptr_account->is_connected if (ptr_account.second->is_connected
&& (xmpp_conn_is_connecting(ptr_account->connection) && (xmpp_conn_is_connecting(ptr_account.second->connection)
|| xmpp_conn_is_connected(ptr_account->connection))) || xmpp_conn_is_connected(ptr_account.second->connection)))
connection__process(ptr_account->context, ptr_account->connection, 10); connection__process(ptr_account.second->context, ptr_account.second->connection, 10);
else if (ptr_account->disconnected); else if (ptr_account.second->disconnected);
else if (ptr_account->reconnect_start > 0 else if (ptr_account.second->reconnect_start > 0
&& ptr_account->reconnect_start < time(NULL)) && ptr_account.second->reconnect_start < time(NULL))
{ {
account__connect(ptr_account); account__connect(ptr_account.second);
} }
} }

@ -7,12 +7,12 @@
#include <ctime> #include <ctime>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <unordered_map>
#include <strophe.h> #include <strophe.h>
#include "omemo.hh" #include "omemo.hh"
extern struct t_account *accounts; extern std::unordered_map<std::string, struct t_account *> accounts;
extern struct t_account *last_account;
enum t_account_option enum t_account_option
{ {

@ -26,23 +26,22 @@ void buffer__get_account_and_channel(struct t_gui_buffer *buffer,
*channel = NULL; *channel = NULL;
/* look for a account or channel using this buffer */ /* look for a account or channel using this buffer */
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account->next_account)
{ {
if (ptr_account->buffer == buffer) if (ptr_account.second->buffer == buffer)
{ {
if (account) if (account)
*account = ptr_account; *account = ptr_account.second;
return; return;
} }
for (ptr_channel = ptr_account->channels; ptr_channel; for (ptr_channel = ptr_account.second->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel) ptr_channel = ptr_channel->next_channel)
{ {
if (ptr_channel->buffer == buffer) if (ptr_channel->buffer == buffer)
{ {
if (account) if (account)
*account = ptr_account; *account = ptr_account.second;
if (channel) if (channel)
*channel = ptr_channel; *channel = ptr_channel;
return; return;

@ -77,14 +77,13 @@ struct t_account *channel__account(struct t_channel *channel)
if (!channel) if (!channel)
return NULL; return NULL;
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account->next_account)
{ {
for (ptr_channel = ptr_account->channels; ptr_channel; for (ptr_channel = ptr_account.second->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel) ptr_channel = ptr_channel->next_channel)
{ {
if (ptr_channel == channel) if (ptr_channel == channel)
return ptr_account; return ptr_account.second;
} }
} }

@ -80,14 +80,13 @@ void command__account_list(int argc, char **argv)
} }
if (!account_name) if (!account_name)
{ {
if (accounts) if (!accounts.empty())
{ {
weechat_printf(NULL, ""); weechat_printf(NULL, "");
weechat_printf(NULL, _("All accounts:")); weechat_printf(NULL, _("All accounts:"));
for (ptr_account2 = accounts; ptr_account2; for (auto ptr_account2 : accounts)
ptr_account2 = ptr_account2->next_account)
{ {
command__display_account(ptr_account2); command__display_account(ptr_account2.second);
} }
} }
else else
@ -96,10 +95,9 @@ void command__account_list(int argc, char **argv)
else else
{ {
one_account_found = 0; one_account_found = 0;
for (ptr_account2 = accounts; ptr_account2; for (auto ptr_account2 : accounts)
ptr_account2 = ptr_account2->next_account)
{ {
if (weechat_strcasestr(ptr_account2->name, account_name)) if (weechat_strcasestr(ptr_account2.second->name, account_name))
{ {
if (!one_account_found) if (!one_account_found)
{ {
@ -109,7 +107,7 @@ void command__account_list(int argc, char **argv)
account_name); account_name);
} }
one_account_found = 1; one_account_found = 1;
command__display_account(ptr_account2); command__display_account(ptr_account2.second);
} }
} }
if (!one_account_found) if (!one_account_found)

@ -113,10 +113,9 @@ int completion__accounts_cb(const void *pointer, void *data,
(void) completion_item; (void) completion_item;
(void) buffer; (void) buffer;
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account->next_account)
{ {
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); 0, WEECHAT_LIST_POS_SORT);
} }

@ -361,13 +361,12 @@ int config__account_write_cb(const void *pointer, void *data,
if (!weechat_config_write_line(config_file, section_name, NULL)) if (!weechat_config_write_line(config_file, section_name, NULL))
return WEECHAT_CONFIG_WRITE_ERROR; return WEECHAT_CONFIG_WRITE_ERROR;
for (ptr_account = accounts; ptr_account; for (auto ptr_account : accounts)
ptr_account = ptr_account->next_account)
{ {
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++) for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
{ {
if (!weechat_config_write_option(config_file, if (!weechat_config_write_option(config_file,
ptr_account->options[i])) ptr_account.second->options[i]))
return WEECHAT_CONFIG_WRITE_ERROR; return WEECHAT_CONFIG_WRITE_ERROR;
} }
} }

Loading…
Cancel
Save