Handle scrolling down when buffer fills up

This commit is contained in:
MarcoPolo-PasTonMolo
2022-07-05 13:09:16 +03:00
parent b03c3bda98
commit 47b3e528e2
5 changed files with 37 additions and 15 deletions

View File

@@ -538,12 +538,16 @@ _chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid)
}
}
// Print history starting from start_time to end_time if end_time is null the
// first entrie's timestamp in the buffer is used. Flip true to prepend to buffer.
gboolean
chatwin_old_history(ProfChatWin* chatwin, char* start_time)
chatwin_db_history(ProfChatWin* chatwin, char* start_time, char* end_time, gboolean flip)
{
// TODO: not correct location but check whether notifications get screwed
char* 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, TRUE);
if (!end_time) {
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);
gboolean has_items = g_slist_length(history) != 0;
GSList* curr = history;
@@ -554,7 +558,11 @@ chatwin_old_history(ProfChatWin* chatwin, char* start_time)
// This is dirty workaround for memory leak. We reassign msg->plain above so have to free previous object
// TODO: Make a better solution, for example, pass msg object to the function and it will replace msg->plain properly if needed.
free(msg_plain);
win_print_old_history((ProfWin*)chatwin, msg);
if (flip) {
win_print_old_history((ProfWin*)chatwin, msg);
} else {
win_print_history((ProfWin*)chatwin, msg);
}
curr = g_slist_next(curr);
}

View File

@@ -145,7 +145,7 @@ void chatwin_set_incoming_char(ProfChatWin* chatwin, const char* const ch);
void chatwin_unset_incoming_char(ProfChatWin* chatwin);
void chatwin_set_outgoing_char(ProfChatWin* chatwin, const char* const ch);
void chatwin_unset_outgoing_char(ProfChatWin* chatwin);
gboolean chatwin_old_history(ProfChatWin* chatwin, char* start_date);
gboolean chatwin_db_history(ProfChatWin* chatwin, char* start_time, char* end_time, gboolean flip);
// MUC window
ProfMucWin* mucwin_new(const char* const barejid);

View File

@@ -607,7 +607,7 @@ win_page_up(ProfWin* window)
// 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_old_history(chatwin, NULL)) {
if (!chatwin_db_history(chatwin, NULL, NULL, TRUE)) {
win_print_loading_history(window);
iq_mam_request_older(chatwin);
}
@@ -637,6 +637,20 @@ 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) {
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);
GDateTime* now = g_date_time_new_now_local();
char* end = g_date_time_format_iso8601(now);
chatwin_db_history((ProfChatWin*)window, start, end, FALSE);
g_free(start);
g_date_time_unref(now);
}
}
// only got half a screen, show full screen
if ((y - (*page_start)) < page_space)
*page_start = y - page_space;