diff --git a/src/database.c b/src/database.c index 966f6ca4..d5f12ae9 100644 --- a/src/database.c +++ b/src/database.c @@ -248,16 +248,16 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t gchar* sort = !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); - gchar* start_date_fmt = start_time ? start_time : NULL; - 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` DESC LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_date_fmt, start_date_fmt, 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, sort, MESSAGES_TO_RETRIEVE, sort); + + g_date_time_unref(now); + g_free(end_date_fmt); + if (!query) { log_error("log_database_get_previous_chat(): SQL query. could not allocate memory"); return NULL; } - g_date_time_unref(now); - g_free(end_date_fmt); - jid_destroy(myjid); int rc = sqlite3_prepare_v2(g_chatlog_database, query, -1, &stmt, NULL); diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index ec7f3f7a..0b2724d3 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -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); } diff --git a/src/ui/ui.h b/src/ui/ui.h index 1b87783a..e396b18a 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -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); diff --git a/src/ui/window.c b/src/ui/window.c index 3bbdad01..e56b14fa 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -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; diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index e5d6b13a..a1784b45 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -2583,7 +2583,7 @@ _mam_buffer_commit_handler(xmpp_stanza_t* const stanza, void* const userdata) ProfChatWin* chatwin = (ProfChatWin*)userdata; // Remove the "Loading messages ..." message buffer_remove_entry(((ProfWin*)chatwin)->layout->buffer, 0); - chatwin_old_history(chatwin, NULL); + chatwin_db_history(chatwin, NULL, NULL, TRUE); return 0; } @@ -2685,10 +2685,10 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) buffer_remove_entry(window->layout->buffer, 0); if (is_complete || data->end_datestr) { - chatwin_old_history(data->win, is_complete ? NULL : data->start_datestr); + chatwin_db_history(data->win, is_complete ? NULL : data->start_datestr, NULL, TRUE); return 0; } - chatwin_old_history(data->win, data->start_datestr); + chatwin_db_history(data->win, data->start_datestr, NULL, TRUE); xmpp_stanza_t* set = xmpp_stanza_get_child_by_name_and_ns(fin, STANZA_TYPE_SET, STANZA_NS_RSM); if (set) {