More support for multiple identities.
This commit is contained in:
parent
76d07ed4f6
commit
b60ac98a3e
14 changed files with 529 additions and 344 deletions
|
@ -5,13 +5,11 @@
|
|||
#include <tox/tox.h>
|
||||
|
||||
#include "tox-weechat.h"
|
||||
#include "tox-weechat-tox.h"
|
||||
#include "tox-weechat-utils.h"
|
||||
|
||||
#include "tox-weechat-chats.h"
|
||||
|
||||
struct t_tox_chat *tox_weechat_chats_head = NULL;
|
||||
struct t_tox_chat *tox_weechat_chats_tail = NULL;
|
||||
|
||||
int tox_weechat_buffer_input_callback(void *data,
|
||||
struct t_gui_buffer *buffer,
|
||||
const char *input_data);
|
||||
|
@ -20,44 +18,45 @@ int tox_weechat_buffer_close_callback(void *data,
|
|||
struct t_gui_buffer *buffer);
|
||||
|
||||
void
|
||||
tox_weechat_chat_add(struct t_tox_chat *chat)
|
||||
tox_weechat_chat_add(struct t_tox_weechat_identity *identity,
|
||||
struct t_tox_weechat_chat *chat)
|
||||
{
|
||||
if (tox_weechat_chats_head == NULL)
|
||||
{
|
||||
tox_weechat_chats_head = tox_weechat_chats_tail = chat;
|
||||
chat->prev = chat->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
tox_weechat_chats_tail->next = chat;
|
||||
chat->prev = tox_weechat_chats_tail;
|
||||
chat->next = NULL;
|
||||
chat->identity = identity;
|
||||
|
||||
tox_weechat_chats_tail = chat;
|
||||
}
|
||||
chat->prev_chat = identity->last_chat;
|
||||
chat->next_chat = NULL;
|
||||
|
||||
if (identity->chats == NULL)
|
||||
identity->chats = chat;
|
||||
else
|
||||
identity->last_chat->next_chat = chat;
|
||||
|
||||
identity->last_chat = chat;
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_chat_remove(struct t_tox_chat *chat)
|
||||
tox_weechat_chat_remove(struct t_tox_weechat_chat *chat)
|
||||
{
|
||||
if (chat->prev)
|
||||
chat->prev->next = chat->next;
|
||||
if (chat->next)
|
||||
chat->next->prev = chat->prev;
|
||||
if (chat->prev_chat)
|
||||
chat->prev_chat->next_chat = chat->next_chat;
|
||||
if (chat->next_chat)
|
||||
chat->next_chat->prev_chat = chat->prev_chat;
|
||||
|
||||
if (chat == tox_weechat_chats_head)
|
||||
tox_weechat_chats_head = chat->next;
|
||||
if (chat == tox_weechat_chats_tail)
|
||||
tox_weechat_chats_tail = chat->prev;
|
||||
if (chat == chat->identity->chats)
|
||||
chat->identity->chats = chat->next_chat;
|
||||
if (chat == chat->identity->last_chat)
|
||||
chat->identity->last_chat = chat->prev_chat;
|
||||
|
||||
free(chat);
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_chat_refresh(struct t_tox_chat *chat)
|
||||
tox_weechat_chat_refresh(struct t_tox_weechat_chat *chat)
|
||||
{
|
||||
char *name = tox_weechat_get_name_nt(chat->friend_number);
|
||||
char *status_message = tox_weechat_get_status_message_nt(chat->friend_number);
|
||||
char *name = tox_weechat_get_name_nt(chat->identity->tox,
|
||||
chat->friend_number);
|
||||
char *status_message = tox_weechat_get_status_message_nt(chat->identity->tox,
|
||||
chat->friend_number);
|
||||
|
||||
weechat_buffer_set(chat->buffer, "short_name", name);
|
||||
weechat_buffer_set(chat->buffer, "title", status_message);
|
||||
|
@ -66,40 +65,38 @@ tox_weechat_chat_refresh(struct t_tox_chat *chat)
|
|||
free(status_message);
|
||||
}
|
||||
|
||||
struct t_tox_chat *
|
||||
tox_weechat_friend_chat_new(int32_t friend_number)
|
||||
struct t_tox_weechat_chat *
|
||||
tox_weechat_friend_chat_new(struct t_tox_weechat_identity *identity,
|
||||
int32_t friend_number)
|
||||
{
|
||||
struct t_tox_chat *chat = malloc(sizeof(*chat));
|
||||
struct t_tox_weechat_chat *chat = malloc(sizeof(*chat));
|
||||
chat->friend_number = friend_number;
|
||||
|
||||
uint8_t client_id[TOX_CLIENT_ID_SIZE];
|
||||
tox_get_client_id(tox, friend_number, client_id);
|
||||
tox_get_client_id(identity->tox, friend_number, client_id);
|
||||
|
||||
char *buffer_name = tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE);
|
||||
// TODO: prepend identity name
|
||||
char *buffer_name = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
|
||||
tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name);
|
||||
|
||||
chat->buffer = weechat_buffer_new(buffer_name,
|
||||
tox_weechat_buffer_input_callback, chat,
|
||||
tox_weechat_buffer_close_callback, chat);
|
||||
tox_weechat_chat_refresh(chat);
|
||||
tox_weechat_chat_add(chat);
|
||||
tox_weechat_chat_add(identity, chat);
|
||||
|
||||
free(buffer_name);
|
||||
|
||||
return chat;
|
||||
}
|
||||
|
||||
struct t_tox_chat *
|
||||
tox_weechat_get_first_chat()
|
||||
struct t_tox_weechat_chat *
|
||||
tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity,
|
||||
int32_t friend_number)
|
||||
{
|
||||
return tox_weechat_chats_head;
|
||||
}
|
||||
|
||||
struct t_tox_chat *
|
||||
tox_weechat_get_existing_friend_chat(int32_t friend_number)
|
||||
{
|
||||
for (struct t_tox_chat *chat = tox_weechat_chats_head;
|
||||
for (struct t_tox_weechat_chat *chat = identity->chats;
|
||||
chat;
|
||||
chat = chat->next)
|
||||
chat = chat->next_chat)
|
||||
{
|
||||
if (chat->friend_number == friend_number)
|
||||
return chat;
|
||||
|
@ -108,33 +105,39 @@ tox_weechat_get_existing_friend_chat(int32_t friend_number)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
struct t_tox_chat *
|
||||
tox_weechat_get_friend_chat(int32_t friend_number)
|
||||
struct t_tox_weechat_chat *
|
||||
tox_weechat_get_friend_chat(struct t_tox_weechat_identity *identity,
|
||||
int32_t friend_number)
|
||||
{
|
||||
struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number);
|
||||
struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity, friend_number);
|
||||
|
||||
if (chat)
|
||||
return chat;
|
||||
else
|
||||
return tox_weechat_friend_chat_new(friend_number);
|
||||
return tox_weechat_friend_chat_new(identity, friend_number);
|
||||
}
|
||||
|
||||
struct t_tox_chat *
|
||||
struct t_tox_weechat_chat *
|
||||
tox_weechat_get_chat_for_buffer(struct t_gui_buffer *buffer)
|
||||
{
|
||||
for (struct t_tox_chat *chat = tox_weechat_chats_head;
|
||||
chat;
|
||||
chat = chat->next)
|
||||
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
|
||||
identity;
|
||||
identity = identity->next_identity)
|
||||
{
|
||||
if (chat->buffer == buffer)
|
||||
return chat;
|
||||
for (struct t_tox_weechat_chat *chat = identity->chats;
|
||||
chat;
|
||||
chat = chat->next_chat)
|
||||
{
|
||||
if (chat->buffer == buffer)
|
||||
return chat;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
tox_weechat_chat_print_message(struct t_tox_chat *chat,
|
||||
tox_weechat_chat_print_message(struct t_tox_weechat_chat *chat,
|
||||
const char *sender,
|
||||
const char *message)
|
||||
{
|
||||
|
@ -142,7 +145,7 @@ tox_weechat_chat_print_message(struct t_tox_chat *chat,
|
|||
}
|
||||
|
||||
void
|
||||
tox_weechat_chat_print_action(struct t_tox_chat *chat,
|
||||
tox_weechat_chat_print_action(struct t_tox_weechat_chat *chat,
|
||||
const char *sender,
|
||||
const char *message)
|
||||
{
|
||||
|
@ -157,14 +160,14 @@ tox_weechat_buffer_input_callback(void *data,
|
|||
struct t_gui_buffer *weechat_buffer,
|
||||
const char *input_data)
|
||||
{
|
||||
struct t_tox_chat *chat = data;
|
||||
struct t_tox_weechat_chat *chat = data;
|
||||
|
||||
tox_send_message(tox,
|
||||
tox_send_message(chat->identity->tox,
|
||||
chat->friend_number,
|
||||
(uint8_t *)input_data,
|
||||
strlen(input_data));
|
||||
|
||||
char *name = tox_weechat_get_self_name_nt();
|
||||
char *name = tox_weechat_get_self_name_nt(chat->identity->tox);
|
||||
|
||||
tox_weechat_chat_print_message(chat, name, input_data);
|
||||
|
||||
|
@ -177,7 +180,7 @@ int
|
|||
tox_weechat_buffer_close_callback(void *data,
|
||||
struct t_gui_buffer *weechat_buffer)
|
||||
{
|
||||
struct t_tox_chat *chat = data;
|
||||
struct t_tox_weechat_chat *chat = data;
|
||||
tox_weechat_chat_remove(chat);
|
||||
|
||||
return WEECHAT_RC_OK;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue