From 7801e1eea5bfe75ca41949f6296f4fefced0e155 Mon Sep 17 00:00:00 2001 From: Kyuho Jo Date: Thu, 28 Jan 2016 19:29:29 +0900 Subject: [PATCH] Fetching messages at once Change-Id: I8c8950db20f5fdef8f5607596d3be2467b297f89 Signed-off-by: Kyuho Jo --- TelegramTizen/inc/tg_db_wrapper.h | 2 + TelegramTizen/src/tg_db_wrapper.c | 158 ++++++++++++++++++++++++++++++++++ TelegramTizen/src/tg_messaging_view.c | 93 ++++---------------- TelegramTizen/tg_utils/tg_common.c | 20 +++++ TelegramTizen/tg_utils/tg_common.h | 1 + 5 files changed, 196 insertions(+), 78 deletions(-) diff --git a/TelegramTizen/inc/tg_db_wrapper.h b/TelegramTizen/inc/tg_db_wrapper.h index 9edb986..1884b42 100644 --- a/TelegramTizen/inc/tg_db_wrapper.h +++ b/TelegramTizen/inc/tg_db_wrapper.h @@ -221,6 +221,8 @@ char* get_profile_pic_path(int buddy_id); tg_message_s* get_message_from_message_table(long long msg_id, char* table_name); +Eina_List *get_messages_from_message_table_order_by(char* table_name, const char *order_column, Eina_Bool is_asc, unsigned int limit, unsigned int offset); + char* get_video_thumb_path_from_db(long long media_id); char* get_media_path_from_db(long long media_id); diff --git a/TelegramTizen/src/tg_db_wrapper.c b/TelegramTizen/src/tg_db_wrapper.c index 1cfa2f9..821d091 100644 --- a/TelegramTizen/src/tg_db_wrapper.c +++ b/TelegramTizen/src/tg_db_wrapper.c @@ -2214,6 +2214,164 @@ tg_message_s* get_message_from_message_table(long long msg_id, char* table_name) return message; } +Eina_List *get_messages_from_message_table_order_by(char* table_name, const char *order_column, Eina_Bool is_asc, unsigned int limit, unsigned int offset) +{ + tg_message_s *message = NULL; + Eina_List *message_list = NULL; + Eina_List *message_details = NULL; + Eina_List *col_names = NULL; + Eina_List *col_types = NULL; + int message_count = 0; + + /* Column names */ + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MESSAGE_ID); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_FLAGS); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_FWD_FROM_ID); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_FWD_DATE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_FROM_ID); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_TO_ID); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_OUT_MSG); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_UNREAD); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_DATE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_SERVICE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MESSAGE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MESSAGE_STATE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MESSAGE_LENGTH); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MEDIA_TYPE); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MEDIA_ID); + col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_UNIQUE_ID); + + /* Column types */ + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER_PRIMARY_KEY); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT); + col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); + + message_details = get_values_from_table_sync_order_by(table_name, col_names, col_types, order_column, is_asc, NULL, limit, offset); + + eina_list_free(col_names); + eina_list_free(col_types); + + if (message_details) + message_count = eina_list_count(message_details); + + for (int i = 0; i < message_count; i++) { + Eina_List* ts_msg = eina_list_nth(message_details, i); + + message = (tg_message_s*)malloc(sizeof(tg_message_s)); + message_list = eina_list_append(message_list, message); + + int *temp_msg_id = (int*)eina_list_nth(ts_msg, 0); + if (temp_msg_id) { + message->msg_id = *temp_msg_id; + free(temp_msg_id); + } + + int *temp_flags = (int*)eina_list_nth(ts_msg, 1); + if (temp_flags) { + message->flags = *temp_flags; + free(temp_flags); + } + + int *temp_fwd_from_id = (int*)eina_list_nth(ts_msg, 2); + if (temp_fwd_from_id) { + message->fwd_from_id = *temp_fwd_from_id; + free(temp_fwd_from_id); + } + + int *temp_fwd_date = (int*)eina_list_nth(ts_msg, 3); + if (temp_fwd_date) { + message->fwd_date = *temp_fwd_date; + free(temp_fwd_date); + } + + int *temp_from_id = (int*)eina_list_nth(ts_msg, 4); + if (temp_from_id) { + message->from_id = *temp_from_id; + free(temp_from_id); + } + + int *temp_to_id = (int*)eina_list_nth(ts_msg, 5); + if (temp_to_id) { + message->to_id = *temp_to_id; + free(temp_to_id); + } + + int *temp_out = (int*)eina_list_nth(ts_msg, 6); + if (temp_out) { + message->out = *temp_out; + free(temp_out); + } + + int *temp_unread = (int*)eina_list_nth(ts_msg, 7); + if (temp_unread) { + message->unread = *temp_unread; + free(temp_unread); + } + + int *temp_date = (int*)eina_list_nth(ts_msg, 8); + if (temp_date) { + message->date = *temp_date; + free(temp_date); + } + + int *temp_service = (int*)eina_list_nth(ts_msg, 9); + if (temp_service) { + message->service = *temp_service; + free(temp_service); + } + + char *temp_msg = (char*)eina_list_nth(ts_msg, 10); + if (temp_msg) { + message->message = strdup(temp_msg); + free(temp_msg); + } + + int *temp_message_state = (int*)eina_list_nth(ts_msg, 11); + if (temp_message_state) { + message->msg_state = *temp_message_state; + free(temp_message_state); + } + + int *temp_message_len = (int*)eina_list_nth(ts_msg, 12); + if (temp_message_len) { + message->message_len = *temp_message_len; + free(temp_message_len); + } + + int *temp_media_type = (int*)eina_list_nth(ts_msg, 13); + if (temp_media_type) { + message->media_type = *temp_media_type; + free(temp_media_type); + } + + char *temp_media_id = (char*)eina_list_nth(ts_msg, 14); + if (temp_media_id) { + message->media_id = strdup(temp_media_id); + free(temp_media_id); + } + + int *temp_unique_id = (int*)eina_list_nth(ts_msg, 15); + if (temp_unique_id) { + message->unique_id = *temp_unique_id; + free(temp_unique_id); + } + } + return message_list; +} + void insert_or_update_peer_into_database(tg_peer_info_s* UC) diff --git a/TelegramTizen/src/tg_messaging_view.c b/TelegramTizen/src/tg_messaging_view.c index 22a063f..d9494e2 100644 --- a/TelegramTizen/src/tg_messaging_view.c +++ b/TelegramTizen/src/tg_messaging_view.c @@ -28,39 +28,6 @@ #include #include "tg_search_peer_view.h" -#define PROFILE_BEGIN(pfid) \ - unsigned int __prf_l1_##pfid = __LINE__;\ - struct timeval __prf_1_##pfid;\ - struct timeval __prf_2_##pfid;\ - do {\ - gettimeofday(&__prf_1_##pfid, 0);\ - DBG("**PROFILE BEGIN** [TG: %s() :%s %u ~ ] " #pfid \ - " -> Start Time: %u.%06u seconds\n",\ - __FUNCTION__,\ - rindex(__FILE__,'/')+1, \ - __prf_l1_##pfid,\ - (unsigned int)__prf_1_##pfid.tv_sec,\ - (unsigned int)__prf_1_##pfid.tv_usec );\ - } while (0) - - -#define PROFILE_END(pfid) \ - unsigned int __prf_l2_##pfid = __LINE__;\ - do { \ - gettimeofday(&__prf_2_##pfid, 0);\ - long __ds = __prf_2_##pfid.tv_sec - __prf_1_##pfid.tv_sec;\ - long __dm = __prf_2_##pfid.tv_usec - __prf_1_##pfid.tv_usec;\ - if ( __dm < 0 ) { __ds--; __dm = 1000000 + __dm; } \ - DBG("**PROFILE END** [TG: %s() :%s %u ~ %u] " #pfid \ - " -> Elapsed Time: %u.%06u seconds\n",\ - __FUNCTION__,\ - rindex(__FILE__, '/')+1,\ - __prf_l1_##pfid,\ - __prf_l2_##pfid,\ - (unsigned int)(__ds),\ - (unsigned int)(__dm));\ - } while (0) - static int scroller_show_bottom_edge(Evas_Object *scroller) { if (!scroller) { @@ -1101,6 +1068,7 @@ static Evas_Object * item_provider(void *data, Evas_Object *entry, const char *i char* tablename = get_table_name_from_number(buddy_id); tg_message_s* msg = get_message_from_message_table(message_id, tablename); free(tablename); + if (!msg) { return NULL; } @@ -1415,7 +1383,7 @@ Evas_Object *on_message_item_content_get_cb(void *data, Evas_Object *obj, const if (!strcmp(part, "elm.icon.entry")) { - int message_id = (int)data; + tg_message_s *msg = (tg_message_s*)data; Evas_Object *chat_scroller = obj; appdata_s* ad = evas_object_data_get(chat_scroller, "app_data"); @@ -1425,14 +1393,14 @@ Evas_Object *on_message_item_content_get_cb(void *data, Evas_Object *obj, const } int user_id = (int)evas_object_data_get(chat_scroller, "user_id"); - evas_object_data_set(chat_scroller, "message_id", data); + evas_object_data_set(chat_scroller, "message_id", msg->msg_id); peer_with_pic_s *sel_item = eina_list_nth(ad->peer_list, user_id); int buddy_id = sel_item->use_data->peer_id; char* tablename = get_table_name_from_number(buddy_id); - tg_message_s* msg = get_message_from_message_table(message_id, tablename); + //Eina_Bool is_blur_image = EINA_FALSE; if (msg) { @@ -1490,7 +1458,7 @@ Evas_Object *on_message_item_content_get_cb(void *data, Evas_Object *obj, const return layout; } evas_object_data_set(entry, "chat_list", (void*)chat_scroller); - evas_object_data_set(entry, "message_id", (void*)message_id); + evas_object_data_set(entry, "message_id", (void*)msg->msg_id); if (msg->media_type != tgl_message_media_none) { entry = elm_entry_add(obj); @@ -1551,9 +1519,13 @@ Evas_Object *on_message_item_content_get_cb(void *data, Evas_Object *obj, const } else if (msg->media_type == tgl_message_media_photo || msg->media_type == tgl_message_media_document) { elm_entry_item_provider_append(entry, item_provider, chat_scroller); + LOGD("media id[%s]", msg->media_id); tgl_media_s *media_msg = get_media_details_from_db(atoll(msg->media_id)); + if (media_msg) + LOGD("file path[%s]", media_msg->file_path); if (media_msg && media_msg->caption && strlen(media_msg->caption) > 0) { caption = strdup(media_msg->caption); + LOGD("caption[%s]", caption); } if (msg->out) { if (media_msg) { @@ -1688,17 +1660,6 @@ Evas_Object *on_message_item_content_get_cb(void *data, Evas_Object *obj, const elm_object_part_content_set(entry, "status_icon", status_obj); evas_object_show(status_obj); free(tablename); - if (msg->message) { - free(msg->message); - msg->message = NULL; - } - - if (msg->media_id) { - free(msg->media_id); - msg->media_id = NULL; - } - - free(msg); } #if 0 if (chat_scroller) { @@ -2878,48 +2839,24 @@ Eina_Bool load_chat_history(Evas_Object *chat_scroller) peer_with_pic_s *sel_item = eina_list_nth(ad->peer_list, user_id); int buddy_id = sel_item->use_data->peer_id; - char* tablename = get_table_name_from_number(buddy_id); + Eina_List *vals = get_messages_from_message_table_order_by(tablename, MESSAGE_INFO_TABLE_DATE, EINA_TRUE, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED); + tg_message_s *message_item = NULL; - Eina_List* col_types = NULL; - col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER); - - Eina_List* col_names = NULL; - col_names = eina_list_append(col_names, MESSAGE_INFO_TABLE_MESSAGE_ID); - - char unknown_str[50]; - int unknown = 0; - sprintf(unknown_str, "%d", unknown); - - char* where_clause = (char*)malloc(strlen(MESSAGE_INFO_TABLE_MARKED_FOR_DELETE) + strlen(" = ") + strlen(unknown_str) + 1); - strcpy(where_clause, MESSAGE_INFO_TABLE_MARKED_FOR_DELETE); - strcat(where_clause, " = "); - strcat(where_clause, unknown_str); - - Eina_List* vals = get_values_from_table_sync_order_by(tablename, col_names, col_types, MESSAGE_INFO_TABLE_DATE, EINA_TRUE, where_clause, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED); if (!vals) { // set no messages yet return EINA_FALSE; } else { int row_count = eina_list_count(vals); - if (row_count <= 0) { - // set no messages yet - return EINA_FALSE; - } - for (int i = 0 ; i < row_count ; i++) { - Eina_List* row_vals = eina_list_nth(vals, i); - int* temp_message_id = (int*)eina_list_nth(row_vals, 0); - int message_id = *temp_message_id; Evas_Object *message = NULL; - message = on_message_item_content_get_cb((void *)message_id, chat_scroller, "elm.icon.entry"); - elm_object_signal_callback_add(message, "clicked", "item", on_list_media_item_clicked, (void*)message_id); - + message_item = eina_list_nth(vals, i); + message = on_message_item_content_get_cb((void *)message_item, chat_scroller, "elm.icon.entry"); + elm_object_signal_callback_add(message, "clicked", "item", on_list_media_item_clicked, (void*)message_item->msg_id); scroller_push_item(chat_scroller, message); - - eina_list_free(row_vals); + free_message(&message_item); } eina_list_free(vals); } diff --git a/TelegramTizen/tg_utils/tg_common.c b/TelegramTizen/tg_utils/tg_common.c index 12c37c7..b42ce9e 100644 --- a/TelegramTizen/tg_utils/tg_common.c +++ b/TelegramTizen/tg_utils/tg_common.c @@ -328,6 +328,26 @@ void launch_app_control(appdata_s *ad, char *media_type, char *url) } +extern void free_message(tg_message_s **message) +{ + if (message == NULL || *message == NULL) { + return; + } + + if ((*message)->message) { + free((*message)->message); + (*message)->message = NULL; + } + + if ((*message)->media_id) { + free((*message)->media_id); + (*message)->media_id = NULL; + } + + free(*message); + *message = NULL; +} + void free_user_data(user_data_s *user_data) { if (!user_data) { diff --git a/TelegramTizen/tg_utils/tg_common.h b/TelegramTizen/tg_utils/tg_common.h index 7ccce07..3a103d7 100644 --- a/TelegramTizen/tg_utils/tg_common.h +++ b/TelegramTizen/tg_utils/tg_common.h @@ -610,6 +610,7 @@ extern void load_peer_data(appdata_s *ad); extern void load_registered_user_data(appdata_s *ad); extern void load_main_list_data(appdata_s *ad); extern void launch_app_control(appdata_s *ad, char *media_type, char *url); +extern void free_message(tg_message_s **message); #if 0 static char *trim(char *s) { -- 2.7.4