From f42f856d37335dc33ce1faf4b1e3e51537df9af2 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 8 Apr 2020 12:50:23 +0200 Subject: [PATCH] Retrieve message type from database So we don't have to check for MUC another way. --- src/database.c | 17 ++++++++++++++++- src/ui/chatwin.c | 2 +- src/ui/mucwin.c | 2 +- src/ui/window.c | 5 ++--- src/ui/window.h | 2 +- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/database.c b/src/database.c index 76f85a82..c02392be 100644 --- a/src/database.c +++ b/src/database.c @@ -50,6 +50,7 @@ static sqlite3 *g_chatlog_database; static void _add_to_db(ProfMessage *message, char *type, const Jid * const from_jid, const Jid * const to_jid); static char* _get_db_filename(ProfAccount *account); +static prof_msg_type_t _get_message_type_type(const char *const type); static char* _get_db_filename(ProfAccount *account) @@ -215,7 +216,7 @@ log_database_get_previous_chat(const gchar *const contact_barejid) sqlite3_stmt *stmt = NULL; char *query; - if (asprintf(&query, "SELECT * FROM (SELECT `message`, `timestamp`, `from_jid` from `ChatLogs` WHERE `from_jid` = '%s' OR `to_jid` = '%s' ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, contact_barejid) == -1) { + if (asprintf(&query, "SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE `from_jid` = '%s' OR `to_jid` = '%s' ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, contact_barejid) == -1) { log_error("log_database_get_previous_chat(): SQL query. could not allocate memory"); return NULL; } @@ -232,11 +233,13 @@ log_database_get_previous_chat(const gchar *const contact_barejid) char *message = (char*)sqlite3_column_text(stmt, 0); char *date = (char*)sqlite3_column_text(stmt, 1); char *from = (char*)sqlite3_column_text(stmt, 2); + char *type = (char*)sqlite3_column_text(stmt, 3); ProfMessage *msg = message_init(); msg->jid = jid_create(from); msg->plain = strdup(message); msg->timestamp = g_date_time_new_from_iso8601(date, NULL); + msg->type = _get_message_type_type(type); // TODO: later we can get more fields like 'enc'. then we can display the history like regular chats with all info the user enabled. history = g_slist_append(history, msg); @@ -261,6 +264,18 @@ static const char* _get_message_type_str(prof_msg_type_t type) { return NULL; } +static prof_msg_type_t _get_message_type_type(const char *const type) { + if (g_strcmp0(type, "chat") == 0) { + return PROF_MSG_TYPE_CHAT; + } else if (g_strcmp0(type, "muc") == 0) { + return PROF_MSG_TYPE_MUC; + } else if (g_strcmp0(type, "mucpm") == 0) { + return PROF_MSG_TYPE_MUCPM; + } else { + return PROF_MSG_TYPE_UNINITIALIZED; + } +} + static const char* _get_message_enc_str(prof_enc_t enc) { switch (enc) { case PROF_MSG_ENC_PGP: diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 5e2ac8c2..975c6b2a 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -486,7 +486,7 @@ _chatwin_history(ProfChatWin *chatwin, const char *const contact_barejid) while (curr) { ProfMessage *msg = curr->data; - win_print_history((ProfWin*)chatwin, msg, FALSE); + win_print_history((ProfWin*)chatwin, msg); curr = g_slist_next(curr); } chatwin->history_shown = TRUE; diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index 620ac704..a47b82f1 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -373,7 +373,7 @@ mucwin_history(ProfMucWin *mucwin, const ProfMessage *const message) char *muc_history_color = prefs_get_string(PREF_HISTORY_COLOR_MUC); if (g_strcmp0(muc_history_color, "unanimous") == 0) { - win_print_history(window, message, TRUE); + win_print_history(window, message); } else { char *mynick = muc_nick(mucwin->roomjid); GSList *mentions = get_mentions(prefs_get_boolean(PREF_NOTIFY_MENTION_WHOLE_WORD), prefs_get_boolean(PREF_NOTIFY_MENTION_CASE_SENSITIVE), message->plain, mynick); diff --git a/src/ui/window.c b/src/ui/window.c index 5b0aec8b..a44e2e4c 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1218,15 +1218,14 @@ win_print_outgoing(ProfWin *window, const char *show_char, const char *const id, } void -win_print_history(ProfWin *window, const ProfMessage *const message, gboolean is_muc) +win_print_history(ProfWin *window, const ProfMessage *const message) { g_date_time_ref(message->timestamp); int flags = 0; - // TODO: ProfMessage needs a 'type' field like we have in sql db. then we can know whether each message is a chat, muc, mucpm char *display_name; - if (is_muc) { + if (message->type == PROF_MSG_TYPE_MUC) { display_name = strdup(message->jid->resourcepart); char *muc_history_color = prefs_get_string(PREF_HISTORY_COLOR_MUC); diff --git a/src/ui/window.h b/src/ui/window.h index 378dae24..4dc8e7d2 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -68,7 +68,7 @@ void win_print_outgoing(ProfWin *window, const char *show_char, const char *cons void win_print_outgoing_with_receipt(ProfWin *window, const char *show_char, const char *const from, const char *const message, char *id, const char *const replace_id); void win_println_incoming_muc_msg(ProfWin *window, char *show_char, int flags, const ProfMessage *const message); void win_print_outgoing_muc_msg(ProfWin *window, char *show_char, const char *const me, const char *const id, const char *const replace_id, const char *const message); -void win_print_history(ProfWin *window, const ProfMessage *const message, gboolean is_muc); +void win_print_history(ProfWin *window, const ProfMessage *const message); void win_print_http_upload(ProfWin *window, const char *const message, char *url);