From 85aaf40432d50a99c50ae8d1baa7c02ff5ae43ba Mon Sep 17 00:00:00 2001 From: MarcoPolo-PasTonMolo Date: Sun, 10 Jul 2022 10:44:06 +0300 Subject: Have ability to scroll through history even without MAM --- src/database.c | 11 +++++++---- src/database.h | 2 +- src/ui/chatwin.c | 4 ++-- src/ui/window.c | 6 +++--- 4 files changed, 13 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/database.c b/src/database.c index d5f12ae9..80effb71 100644 --- a/src/database.c +++ b/src/database.c @@ -233,9 +233,11 @@ log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_las return msg; } -// Query previous chat +// Query previous chats, constraints start_time and end_time. If end_time is +// null the current time is used. from_start gets first few messages if true +// otherwise the last ones. Flip flips the order of the results GSList* -log_database_get_previous_chat(const gchar* const contact_barejid, char* start_time, char* end_time, gboolean flip) +log_database_get_previous_chat(const gchar* const contact_barejid, char* start_time, char* end_time, gboolean from_start, gboolean flip) { sqlite3_stmt* stmt = NULL; gchar* query; @@ -245,10 +247,11 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t return NULL; // Flip order when querying older pages - gchar* sort = !flip ? "ASC" : "DESC"; + gchar* sort1 = from_start ? "ASC" : "DESC"; + gchar* sort2 = !flip ? "ASC" : "DESC"; GDateTime* now = g_date_time_new_now_local(); gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(now); - query = sqlite3_mprintf("SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE ((`from_jid` = '%q' AND `to_jid` = '%q') OR (`from_jid` = '%q' AND `to_jid` = '%q')) AND `timestamp` < '%q' AND (%Q IS NULL OR `timestamp` > %Q) ORDER BY `timestamp` %s LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_time, start_time, sort, MESSAGES_TO_RETRIEVE, sort); + query = sqlite3_mprintf("SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE ((`from_jid` = '%q' AND `to_jid` = '%q') OR (`from_jid` = '%q' AND `to_jid` = '%q')) AND `timestamp` < '%q' AND (%Q IS NULL OR `timestamp` > %Q) ORDER BY `timestamp` %s LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_time, start_time, sort1, MESSAGES_TO_RETRIEVE, sort2); g_date_time_unref(now); g_free(end_date_fmt); diff --git a/src/database.h b/src/database.h index d575d62e..4913aae0 100644 --- a/src/database.h +++ b/src/database.h @@ -47,7 +47,7 @@ void log_database_add_incoming(ProfMessage* message); void log_database_add_outgoing_chat(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc); void log_database_add_outgoing_muc(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc); void log_database_add_outgoing_muc_pm(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc); -GSList* log_database_get_previous_chat(const gchar* const contact_barejid, char* start_time, char* end_time, gboolean flip); +GSList* log_database_get_previous_chat(const gchar* const contact_barejid, char* start_time, char* end_time, gboolean from_start, gboolean flip); ProfMessage* log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_last); void log_database_close(void); diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 0b2724d3..530739ae 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -519,7 +519,7 @@ static void _chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid) { if (!chatwin->history_shown) { - GSList* history = log_database_get_previous_chat(contact_barejid, NULL, NULL, FALSE); + GSList* history = log_database_get_previous_chat(contact_barejid, NULL, NULL, FALSE, FALSE); GSList* curr = history; while (curr) { @@ -547,7 +547,7 @@ chatwin_db_history(ProfChatWin* chatwin, char* start_time, char* end_time, gbool end_time = buffer_size(((ProfWin*)chatwin)->layout->buffer) == 0 ? NULL : g_date_time_format_iso8601(buffer_get_entry(((ProfWin*)chatwin)->layout->buffer, 0)->time); } - GSList* history = log_database_get_previous_chat(chatwin->barejid, start_time, end_time, flip); + GSList* history = log_database_get_previous_chat(chatwin->barejid, start_time, end_time, !flip, flip); gboolean has_items = g_slist_length(history) != 0; GSList* curr = history; diff --git a/src/ui/window.c b/src/ui/window.c index e56b14fa..4ac45f9c 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -601,13 +601,13 @@ win_page_up(ProfWin* window) *page_start -= page_space; - if (*page_start == -page_space && prefs_get_boolean(PREF_MAM) && window->type == WIN_CHAT) { + if (*page_start == -page_space && window->type == WIN_CHAT) { ProfChatWin* chatwin = (ProfChatWin*) window; ProfBuffEntry* first_entry = buffer_size(window->layout->buffer) != 0 ? buffer_get_entry(window->layout->buffer, 0) : NULL; // Don't do anything if still fetching mam messages if (first_entry && !(first_entry->theme_item == THEME_ROOMINFO && g_strcmp0(first_entry->message, LOADING_MESSAGE) == 0)) { - if (!chatwin_db_history(chatwin, NULL, NULL, TRUE)) { + if (!chatwin_db_history(chatwin, NULL, NULL, TRUE) && prefs_get_boolean(PREF_MAM)) { win_print_loading_history(window); iq_mam_request_older(chatwin); } @@ -638,7 +638,7 @@ win_page_down(ProfWin* window) *page_start += page_space; // Scrolled down after reaching the bottom of the page - if ((*page_start == y || (*page_start == page_space && *page_start >= y)) && prefs_get_boolean(PREF_MAM) && window->type == WIN_CHAT) { + if ((*page_start == y || (*page_start == page_space && *page_start >= y)) && window->type == WIN_CHAT) { int bf_size = buffer_size(window->layout->buffer); if (bf_size > 0) { char* start = g_date_time_format_iso8601(buffer_get_entry(window->layout->buffer, bf_size - 1)->time); -- cgit 1.4.1-2-gfad0