Move more processing out of the delta event handling

This commit is contained in:
2019-04-11 22:14:54 +01:00
parent 212f75efb5
commit cd7a5c5d24

View File

@@ -150,46 +150,72 @@ err:
typedef struct { typedef struct {
DeltaConnectionData *conn; DeltaConnectionData *conn;
// Used by delta_process_incoming_message
uint32_t msg_id; uint32_t msg_id;
// Used by delta_process_connection_state
int connection_state;
} ProcessRequest; } ProcessRequest;
gboolean gboolean
delta_process(void *data) delta_process_incoming_message(void *data)
{ {
ProcessRequest *pr = (ProcessRequest *)data; ProcessRequest *pr = (ProcessRequest *)data;
g_assert(pr != NULL);
g_assert(pr->conn != NULL); g_assert(pr->conn != NULL);
delta_recv_im(pr->conn, pr->msg_id); delta_recv_im(pr->conn, pr->msg_id);
free(data); free(data);
return FALSE; return FALSE;
} }
void gboolean
delta_fresh_messages(DeltaConnectionData *conn, dc_context_t *mailbox) delta_process_connection_state(void *data)
{ {
g_assert(conn != NULL); ProcessRequest *pr = (ProcessRequest *)data;
g_assert(mailbox != NULL); g_assert(pr != NULL);
g_assert(pr->conn != NULL);
purple_connection_update_progress(
pr->conn->pc,
"Connecting...",
pr->connection_state,
MAX_DELTA_CONFIGURE
);
if (pr->connection_state == MAX_DELTA_CONFIGURE) {
purple_connection_set_state(pr->conn->pc, PURPLE_CONNECTED);
}
free(data);
return FALSE;
}
gboolean
delta_process_fresh_messages(void *data)
{
ProcessRequest *pr = (ProcessRequest *)data;
g_assert(pr != NULL);
g_assert(pr->conn != NULL);
g_assert(pr->conn->mailbox != NULL);
// Spot any messages received while offline // Spot any messages received while offline
dc_array_t *fresh_msgs = dc_get_fresh_msgs(mailbox); dc_array_t *fresh_msgs = dc_get_fresh_msgs(pr->conn->mailbox);
size_t fresh_count = dc_array_get_cnt(fresh_msgs); size_t fresh_count = dc_array_get_cnt(fresh_msgs);
printf("*** fresh_count: %zu\n", fresh_count); printf("*** fresh_count: %zu\n", fresh_count);
for(size_t i = 0; i < fresh_count; i++) { for(size_t i = 0; i < fresh_count; i++) {
ProcessRequest *pr = g_malloc(sizeof(ProcessRequest)); delta_recv_im(pr->conn, dc_array_get_id(fresh_msgs, i));
g_assert(pr != NULL);
pr->conn = conn;
pr->msg_id = dc_array_get_id(fresh_msgs, i);
purple_timeout_add(0, delta_process, pr);
} }
free(fresh_msgs); free(fresh_msgs);
return; return FALSE;
} }
// Do not call any libpurple functions in here, as it is not thread-safe and // Do not call any libpurple functions in here, as it is not thread-safe and
@@ -222,7 +248,11 @@ my_delta_handler(dc_context_t* mailbox, int event, uintptr_t data1, uintptr_t da
break; break;
case DC_EVENT_MSGS_CHANGED: case DC_EVENT_MSGS_CHANGED:
delta_fresh_messages(conn, mailbox); pr = g_malloc(sizeof(ProcessRequest));
g_assert(pr != NULL);
pr->conn = conn;
purple_timeout_add(0, delta_process_fresh_messages, pr);
break; break;
case DC_EVENT_INCOMING_MSG: case DC_EVENT_INCOMING_MSG:
@@ -230,9 +260,10 @@ my_delta_handler(dc_context_t* mailbox, int event, uintptr_t data1, uintptr_t da
// TODO: It may be needed for group chats // TODO: It may be needed for group chats
pr = g_malloc(sizeof(ProcessRequest)); pr = g_malloc(sizeof(ProcessRequest));
g_assert(pr != NULL); g_assert(pr != NULL);
pr->conn = conn; pr->conn = conn;
pr->msg_id = (uint32_t)data2; pr->msg_id = (uint32_t)data2;
purple_timeout_add(0, delta_process, pr); purple_timeout_add(0, delta_process_incoming_message, pr);
break; break;
// These are all to do with sending & receiving messages. The real meat of // These are all to do with sending & receiving messages. The real meat of
@@ -245,10 +276,12 @@ my_delta_handler(dc_context_t* mailbox, int event, uintptr_t data1, uintptr_t da
break; break;
case DC_EVENT_CONFIGURE_PROGRESS: case DC_EVENT_CONFIGURE_PROGRESS:
purple_connection_update_progress(conn->pc, "Connecting...", (int)data1, MAX_DELTA_CONFIGURE); pr = g_malloc(sizeof(ProcessRequest));
if ((int)data1 == MAX_DELTA_CONFIGURE) { g_assert(pr != NULL);
purple_connection_set_state(conn->pc, PURPLE_CONNECTED);
} pr->conn = conn;
pr->connection_state = (int)data1;
purple_timeout_add(0, delta_process_connection_state, pr);
break; break;
case DC_EVENT_HTTP_GET: case DC_EVENT_HTTP_GET:
printf("HTTP GET requested: %s\n", (char *)data1); printf("HTTP GET requested: %s\n", (char *)data1);
@@ -341,9 +374,6 @@ delta_connection_start_login(PurpleConnection *pc)
pthread_create(&conn->imap_thread, NULL, imap_thread_func, conn); pthread_create(&conn->imap_thread, NULL, imap_thread_func, conn);
pthread_create(&conn->smtp_thread, NULL, smtp_thread_func, conn); pthread_create(&conn->smtp_thread, NULL, smtp_thread_func, conn);
purple_connection_set_state(pc, PURPLE_CONNECTING);
purple_connection_update_progress(pc, "Connecting...", 1, MAX_DELTA_CONFIGURE);
dc_configure(mailbox); dc_configure(mailbox);
dc_maybe_network(mailbox); dc_maybe_network(mailbox);