modified Time stamp entry with uniqe id. 07/55807/1
authorpolu.sandeep <polu.sandeep@samsung.com>
Tue, 29 Dec 2015 10:16:38 +0000 (19:16 +0900)
committerpolu.sandeep <polu.sandeep@samsung.com>
Tue, 29 Dec 2015 10:16:38 +0000 (19:16 +0900)
Change-Id: Ic406ac5c58141a4047ddedcbb78b650c68b3bf0b

TelegramTizen/inc/tg_db_wrapper.h
TelegramTizen/src/telegramtizen.c
TelegramTizen/src/tg_db_wrapper.c
TelegramTizen/src/tg_messaging_view.c
TelegramTizen/tg_utils/tg_common.h
tg-engine-service/inc/server_response.h
tg-engine-service/src/server_response.c
tg-engine-service/src/tg_db_wrapper.c
tg-engine-service/tg_engine/tg_engine.c
tg-engine-service/tg_engine_utils/tg_common.h

index eaa9657..c4d127e 100644 (file)
@@ -230,7 +230,7 @@ void insert_media_info_to_db(tg_message_s *M, char *file_path, int width, int he
 Eina_List* get_image_details_from_db(long long media_id);
 Eina_List* get_image_sizes_from_db(long long media_id);
 
-tg_message_s* get_latest_message_from_message_table(char* table_name);
+tg_message_s* get_latest_message_from_message_table(char* table_name, Eina_Bool is_for_date);
 int get_unread_message_count(char* table_name);
 
 extern void update_msg_into_db(tg_message_s *M, char* table_name);
index 33eeee4..4628c87 100644 (file)
@@ -370,7 +370,7 @@ void load_main_list_data(appdata_s *ad)
 
                                        char* tablename = get_table_name_from_number(peer_info->peer_id);
                                        //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
-                                       tg_message_s* msg = get_latest_message_from_message_table(tablename);
+                                       tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
 
                                        if (msg) {
 
@@ -1504,7 +1504,7 @@ static int on_buddy_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
 }
 
 
-static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg)
+static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg, Eina_Bool with_date)
 {
        int result = SVC_RES_FAIL;
 
@@ -1747,7 +1747,16 @@ static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg)
        if (type_of_chat == TGL_PEER_USER) {
                if ((app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state ==  TG_SET_USER_INFO_STATE) && app->peer_in_cahtting_data
                                && app->peer_in_cahtting_data->use_data->peer_id == from_id) {
-                       on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                       if (with_date) {
+                               char* date_id_str = NULL;
+                               result = bundle_get_str(rec_msg, "date_id", &date_id_str);
+                               int date_id = atoi(date_id_str);
+                               on_text_message_received_from_buddy(app, date_id, type_of_chat);
+                               wait_for(1);
+                               on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                       } else {
+                               on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                       }
                        return result;
                }
 
@@ -1758,7 +1767,16 @@ static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg)
                        tg_message_s* msg = get_message_from_message_table(message_id, tablename);
                        if (msg) {
                                // To be handled.
-                               on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                               if (with_date) {
+                                       char* date_id_str = NULL;
+                                       result = bundle_get_str(rec_msg, "date_id", &date_id_str);
+                                       int date_id = atoi(date_id_str);
+                                       on_text_message_received_from_buddy(app, date_id, type_of_chat);
+                                       wait_for(1);
+                                       on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                               } else {
+                                       on_text_message_received_from_buddy(app, message_id, type_of_chat);
+                               }
                                if(msg->message) {
                                        free(msg->message);
                                        msg->message = NULL;
@@ -1796,6 +1814,11 @@ static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg)
        return result;
 }
 
+static int on_message_with_date_received_from_buddy(appdata_s *app, bundle *const rec_msg)
+{
+       return on_message_received_from_buddy(app, rec_msg, EINA_TRUE);
+}
+
 static int on_new_contact_added(appdata_s *app, bundle *const rec_msg)
 {
        int result = SVC_RES_FAIL;
@@ -2787,7 +2810,11 @@ static int _on_service_client_msg_received_cb(void *data, bundle *const rec_msg)
 
        } else if (strcmp(rec_key_val, "message_received") == 0) {
 
-               return on_message_received_from_buddy(data, rec_msg);
+               return on_message_received_from_buddy(data, rec_msg, EINA_FALSE);
+
+       } else if (strcmp(rec_key_val, "message_received_with_date") == 0) {
+
+               return on_message_with_date_received_from_buddy(data, rec_msg);
 
        } else if (strcmp(rec_key_val, "buddy_readded") == 0) {
 
@@ -2924,7 +2951,7 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                // get message from message table.
 
                                char* tablename = get_table_name_from_number(peer_info->peer_id);
-                               tg_message_s* msg = get_latest_message_from_message_table(tablename);
+                               tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
 
                                if (msg) {
 
@@ -2984,9 +3011,18 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                        main_list_item->peer_type = peer_info->peer_type;
                                        if ((peer_info->peer_type == TGL_PEER_USER) && get_buddy_unknown_status(peer_info->peer_id)) {
                                                //set phone number
-                                               main_list_item->peer_print_name = strdup(get_buddy_phone_num_from_id(peer_info->peer_id));
+                                               char *p_num = get_buddy_phone_num_from_id(peer_info->peer_id);
+                                               if (p_num && strlen(p_num) > 0) {
+                                                       main_list_item->peer_print_name = strdup(p_num);
+                                               } else {
+                                                       main_list_item->peer_print_name = strdup("");
+                                               }
                                        } else {
-                                               main_list_item->peer_print_name = strdup(peer_info->print_name);
+                                               if (peer_info->print_name && strlen(peer_info->print_name) > 0) {
+                                                       main_list_item->peer_print_name = strdup(peer_info->print_name);
+                                               } else {
+                                                       main_list_item->peer_print_name = strdup("");
+                                               }
                                        }
                                        main_list_item->last_seen_time = msg->date;
                                        main_list_item->profile_pic = NULL;
@@ -3017,6 +3053,10 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                                                first_name = phone_num;
                                                        }
 
+                                                       if (!first_name) {
+                                                               first_name = "";
+                                                       }
+
                                                        if (!last_name || strstr(last_name ,"null") != 0) {
                                                                last_name = "";
                                                        }
@@ -3032,7 +3072,6 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                                main_list_item->buddy_display_name = strdup("");
                                        }
 
-
                                        if (peer_info->photo_path) {
                                                main_list_item->profile_pic_path = strdup(peer_info->photo_path);
                                        } else {
@@ -3066,9 +3105,18 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                                main_list_item->peer_type = peer_info->peer_type;
                                                if ((peer_info->peer_type == TGL_PEER_USER) && get_buddy_unknown_status(peer_info->peer_id)) {
                                                        //set phone number
-                                                       main_list_item->peer_print_name = strdup(get_buddy_phone_num_from_id(peer_info->peer_id));
+                                                       char *p_num = get_buddy_phone_num_from_id(peer_info->peer_id);
+                                                       if (p_num && strlen(p_num) > 0) {
+                                                               main_list_item->peer_print_name = strdup(p_num);
+                                                       } else {
+                                                               main_list_item->peer_print_name = strdup("");
+                                                       }
                                                } else {
-                                                       main_list_item->peer_print_name = strdup(peer_info->print_name);
+                                                       if (peer_info->print_name && strlen(peer_info->print_name) > 0) {
+                                                               main_list_item->peer_print_name = strdup(peer_info->print_name);
+                                                       } else {
+                                                               main_list_item->peer_print_name = strdup("");
+                                                       }
                                                }
                                                main_list_item->last_seen_time = peer_info->last_seen_time;
                                                main_list_item->profile_pic = NULL;
@@ -3092,8 +3140,10 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
 
                                                        if (!first_name && !last_name && phone_num) {
                                                                first_name = phone_num;
+                                                       } if (!last_name)
+                                                       if (!first_name) {
+                                                               first_name = "";
                                                        }
-
                                                        if (!last_name || strstr(last_name ,"null") != 0) {
                                                                last_name = "";
                                                        }
@@ -3103,12 +3153,21 @@ tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
                                                        strcat(user_name, last_name);
                                                        main_list_item->buddy_display_name = user_name;
                                                } else if (peer_info->peer_type == TGL_PEER_CHAT) {
-                                                       main_list_item->buddy_display_name = replace(peer_info->print_name, '_', " ");
+                                                       if (peer_info->print_name) {
+                                                               char *temp_name = replace(peer_info->print_name, '_', " ");
+                                                               if (temp_name && strlen(temp_name) > 0) {
+                                                                       main_list_item->buddy_display_name = temp_name;
+                                                               } else {
+                                                                       main_list_item->buddy_display_name = strdup("");
+                                                               }
+                                                       } else {
+                                                               main_list_item->buddy_display_name = strdup("");
+                                                       }
                                                } else {
                                                        main_list_item->buddy_display_name = strdup("");
                                                }
 
-                                               if (peer_info->photo_path) {
+                                               if (peer_info->photo_path && strlen(peer_info->photo_path) > 0) {
                                                        main_list_item->profile_pic_path = strdup(peer_info->photo_path);
                                                } else {
                                                        main_list_item->profile_pic_path = NULL;
@@ -3220,7 +3279,7 @@ void app_nf_back_cb(void *data, Evas_Object *obj, void *event_info)
 
                                                                char* tablename = get_table_name_from_number(peer_info->peer_id);
                                                                //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
-                                                               tg_message_s* msg = get_latest_message_from_message_table(tablename);
+                                                               tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
 
                                                                if (msg) {
 
index 6d6d019..fea7870 100644 (file)
@@ -1657,7 +1657,7 @@ int get_unread_message_count(char* table_name)
 }
 
 
-tg_message_s* get_latest_message_from_message_table(char* table_name)
+tg_message_s* get_latest_message_from_message_table(char* table_name, Eina_Bool is_for_date)
 {
        tg_message_s* message = NULL;
 
@@ -1706,18 +1706,23 @@ tg_message_s* get_latest_message_from_message_table(char* table_name)
 
        char marked_del_str[50];
        sprintf(marked_del_str, "%d", 1);
-
-       char* where_clause = (char*)malloc(strlen(MESSAGE_INFO_TABLE_SERVICE) + strlen(" <> ") + strlen(service_str) + strlen(" AND ") + strlen(MESSAGE_INFO_TABLE_MARKED_FOR_DELETE)+ strlen(" <> ")+ strlen(marked_del_str) + 1);
-       strcpy(where_clause, MESSAGE_INFO_TABLE_SERVICE);
-       strcat(where_clause, " <> ");
-       strcat(where_clause, service_str);
-       strcat(where_clause, " AND ");
-       strcpy(where_clause, MESSAGE_INFO_TABLE_MARKED_FOR_DELETE);
-       strcat(where_clause, " <> ");
-       strcat(where_clause, marked_del_str);
+       char* where_clause = NULL;
+       if (!is_for_date) {
+               where_clause = (char*)malloc(strlen(MESSAGE_INFO_TABLE_SERVICE) + strlen(" != ") + strlen(service_str) + strlen(" AND ") + strlen(MESSAGE_INFO_TABLE_MARKED_FOR_DELETE)+ strlen(" != ")+ strlen(marked_del_str) + 1);
+               strcpy(where_clause, MESSAGE_INFO_TABLE_SERVICE);
+               strcat(where_clause, " != ");
+               strcat(where_clause, service_str);
+               strcat(where_clause, " AND ");
+               strcat(where_clause, MESSAGE_INFO_TABLE_MARKED_FOR_DELETE);
+               strcat(where_clause, " != ");
+               strcat(where_clause, marked_del_str);
+       }
 
        message_details = get_values_from_table_sync_order_by(table_name, col_names, col_types, MESSAGE_INFO_TABLE_DATE, EINA_FALSE, where_clause);
-       free(where_clause);
+       if (where_clause) {
+               free(where_clause);
+               where_clause = NULL;
+       }
 
        eina_list_free(col_names);
        eina_list_free(col_types);
@@ -1959,7 +1964,7 @@ void update_msg_into_db(tg_message_s *M, char* table_name)
 
 int insert_current_date_to_table(char* tb_name)
 {
-       tg_message_s* last_msg = get_latest_message_from_message_table(tb_name);
+       tg_message_s* last_msg = get_latest_message_from_message_table(tb_name, EINA_TRUE);
        if (last_msg) {
                int old_date = last_msg->date;
                time_t old_t = old_date;
@@ -1967,8 +1972,7 @@ int insert_current_date_to_table(char* tb_name)
                struct tm old_lt;
                (void) localtime_r(&old_t, &old_lt);
 
-               int cur_time = time(NULL);
-               time_t new_t = cur_time;
+               time_t new_t = time(NULL);
 
                struct tm new_lt;
                (void) localtime_r(&new_t, &new_lt);
@@ -1977,33 +1981,16 @@ int insert_current_date_to_table(char* tb_name)
                        // no need of new date
                        return -1;
                } else {
-                       int cur_time = time(NULL);
-#if 0
-                       time_t t = cur_time;
-
-                       char *format = NULL;
-                       format = "%a, %e %b %Y";
-
-                       struct tm lt;
-                       char res[256];
-                       (void) localtime_r(&t, &lt);
+                       tg_message_s date_msg;
+                       date_msg.msg_id = get_time_stamp_in_macro();
+                       date_msg.media_type = tgl_message_media_none;
 
-                       if (strftime(res, sizeof(res), format, &lt) == 0) {
-                               (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                               "date/time into buffer of size %u "
-                                               "using: '%s'\n",
-                                               sizeof(res), format);
-                       }
-#else
+                       int cur_time = time(NULL);
+                       cur_time = cur_time - 10;
                        // convert time to string
                        char res[256];
                        sprintf(res, "%d", cur_time);
-#endif
-                       srand(time(NULL));
-                       int r = rand();
-                       tg_message_s date_msg;
-                       date_msg.msg_id = 2*r;
-                       date_msg.media_type = tgl_message_media_none;
+
                        date_msg.date = cur_time;
                        date_msg.message = res;
                        date_msg.message_len = strlen(res);
@@ -2017,33 +2004,15 @@ int insert_current_date_to_table(char* tb_name)
                }
 
        } else {
-               int cur_time = time(NULL);
-#if 0
-               time_t t = cur_time;
-
-               char *format = NULL;
-               format = "%a, %e %b %Y";
-
-               struct tm lt;
-               char res[256];
-               (void) localtime_r(&t, &lt);
+               tg_message_s date_msg;
+               date_msg.msg_id = get_time_stamp_in_macro();
+               date_msg.media_type = tgl_message_media_none;
 
-               if (strftime(res, sizeof(res), format, &lt) == 0) {
-                       (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                       "date/time into buffer of size %u "
-                                       "using: '%s'\n",
-                                       sizeof(res), format);
-               }
-#else
+               int cur_time = time(NULL);
+               cur_time = cur_time - 10;
                // convert time to string
                char res[256];
                sprintf(res, "%d", cur_time);
-#endif
-               srand(time(NULL));
-               int r = rand();
-               tg_message_s date_msg;
-               date_msg.msg_id = 2*r;
-               date_msg.media_type = tgl_message_media_none;
                date_msg.date = cur_time;
                date_msg.message = res;
                date_msg.message_len = strlen(res);
index 8a66dd2..d5e2543 100644 (file)
@@ -2057,7 +2057,7 @@ static Eina_Bool on_new_text_message_send_cb(void *data)
        if (!text_to_send || (strlen(text_to_send) == 0))
                return ECORE_CALLBACK_CANCEL;
 
-       int unique_id = time(NULL);
+       int unique_id = get_time_stamp_in_macro();
        tg_message_s msg;
        msg.msg_id = unique_id;
        msg.from_id = ad->current_user_data->user_id.id;
@@ -2120,11 +2120,13 @@ static void on_text_message_send_clicked(void *data, Evas_Object *obj, const cha
        if (!text_to_send || (strlen(text_to_send) == 0))
                return;
 
-       if(add_date_item_to_chat(data)) {
+       if (add_date_item_to_chat(data)) {
                //ecore_timer_add(2, on_new_text_message_send_cb, chat_scroller);
                //return;
+               wait_for(1);
        }
-       int unique_id = time(NULL);
+
+       int unique_id = get_time_stamp_in_macro();
        tg_message_s msg;
        msg.msg_id = unique_id;
        msg.from_id = ad->current_user_data->user_id.id;
@@ -2510,18 +2512,19 @@ void send_contact_message_to_buddy(void *data, char *first_name, char *last_name
        appdata_s* ad = evas_object_data_get(chat_scroller, "app_data");
        int user_id = (int)evas_object_data_get(chat_scroller, "user_id");
 
-       if(add_date_item_to_chat(data)) {
+       if (add_date_item_to_chat(data)) {
 /*             evas_object_data_set(chat_scroller, "contact_first_name", strdup(first_name));
                evas_object_data_set(chat_scroller, "contact_last_name", strdup(last_name));
                evas_object_data_set(chat_scroller, "contact_phone_number", strdup(phone_number));
 
                ecore_timer_add(2, on_new_contact_message_send_cb, chat_scroller);
                return;*/
+               wait_for(1);
        }
 
        peer_with_pic_s *sel_item =  eina_list_nth(ad->peer_list, user_id);
 
-       int unique_id = time(NULL);
+       int unique_id = get_time_stamp_in_macro();
 
        char unique_id_str[50];
        sprintf(unique_id_str, "%d", unique_id);
@@ -2633,16 +2636,17 @@ void send_location_message_to_buddy(void *data, char *latitude, char *longitude)
        appdata_s* ad = evas_object_data_get(chat_scroller, "app_data");
        int user_id = (int)evas_object_data_get(chat_scroller, "user_id");
 
-       if(add_date_item_to_chat(data)) {
+       if (add_date_item_to_chat(data)) {
 /*             evas_object_data_set(chat_scroller, "contact_latitude", strdup(latitude));
                evas_object_data_set(chat_scroller, "contact_longitude", strdup(longitude));
                ecore_timer_add(2, on_new_location_message_send_cb, chat_scroller);
                return;*/
+               wait_for(1);
        }
 
        peer_with_pic_s *sel_item =  eina_list_nth(ad->peer_list, user_id);
 
-       int unique_id = time(NULL);
+       int unique_id = get_time_stamp_in_macro();
 
        char unique_id_str[50];
        sprintf(unique_id_str, "%d", unique_id);
@@ -2764,7 +2768,7 @@ void send_media_message_to_buddy(void *data, const char* file_path, enum tgl_mes
        Evas_Object *chat_scroller = data;
        appdata_s* ad = evas_object_data_get(chat_scroller, "app_data");
        int user_id = (int)evas_object_data_get(chat_scroller, "user_id");
-       if(add_date_item_to_chat(data)) {
+       if (add_date_item_to_chat(data)) {
 /*             int temp_file_type = file_type;
                char file_type_char[10]= {0, };
                sprintf(file_type_char, "%d", temp_file_type);
@@ -2773,10 +2777,11 @@ void send_media_message_to_buddy(void *data, const char* file_path, enum tgl_mes
 
                ecore_timer_add(2, on_new_media_message_send_cb, chat_scroller);
                return;*/
+               wait_for(1);
        }
        peer_with_pic_s *sel_item =  eina_list_nth(ad->peer_list, user_id);
 
-       int unique_id = time(NULL);
+       int unique_id = get_time_stamp_in_macro();
 
        char unique_id_str[50];
        sprintf(unique_id_str, "%d", unique_id);
index 830e318..b11d653 100644 (file)
@@ -1050,6 +1050,19 @@ static inline TELEGRAM_APP_FILE_TYPE_E telegram_common_get_file_type(const char
        return file_type;
 }
 
+static uint64_t get_time_stamp_in_macro()
+{
+    struct timeval tv;
+    gettimeofday(&tv,NULL);
+    return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
+}
+
+static void wait_for(unsigned int secs)
+{
+    int retTime = time(0) + secs;
+    while (time(0) < retTime);
+}
+
 typedef struct _telegram_Time
 {
        int hours;
index c2b3b02..7e442d7 100644 (file)
@@ -52,6 +52,7 @@ extern void send_self_user_name_updated_response(tg_engine_data_s *tg_data, char
 extern void send_contacts_and_chats_load_done_response(tg_engine_data_s *tg_data, Eina_Bool is_success);
 extern void send_buddy_profile_pic_updated_response(tg_engine_data_s *tg_data, int buddy_id, char* file_path);
 extern void send_message_received_response(tg_engine_data_s *tg_data, int from_id, int to_id, long long message_id, int type_of_chat);
+extern void send_message_with_date_received_response(tg_engine_data_s *tg_data, int from_id, int to_id, long long message_id, int date_id, int type_of_chat);
 extern void send_message_sent_to_buddy_response(tg_engine_data_s *tg_data, int buddy_id, int message_id, char* table_name, Eina_Bool is_success, int type_of_chat);
 extern void send_group_chat_deleted_response(tg_engine_data_s *tg_data, int chat_id, Eina_Bool is_success);
 extern void send_selected_group_chats_deleted_response(tg_engine_data_s *tg_data);
index 02f1fe2..5dcd77b 100644 (file)
@@ -800,6 +800,86 @@ void send_buddy_profile_pic_updated_response(tg_engine_data_s *tg_data, int budd
        bundle_free(msg);
 }
 
+
+void send_message_with_date_received_response(tg_engine_data_s *tg_data, int from_id, int to_id, long long message_id, int date_id, int type_of_chat)
+{
+       bundle *msg = bundle_create();
+       if (bundle_add_str(msg, "app_name", "Tizen Telegram") != 0)     {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       if (bundle_add_str(msg, "command", "message_received_with_date") != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       char from_id_str[50];
+       sprintf(from_id_str,"%d",from_id);
+
+       if (bundle_add_str(msg, "from_id", from_id_str) != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       char to_id_str[50];
+       sprintf(to_id_str,"%d",to_id);
+
+       if (bundle_add_str(msg, "to_id", to_id_str) != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       char msg_id_str[50];
+       sprintf(msg_id_str,"%lld",message_id);
+
+       if (bundle_add_str(msg, "message_id", msg_id_str) != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       char date_id_str[50];
+       sprintf(date_id_str,"%d",date_id);
+
+       if (bundle_add_str(msg, "date_id", date_id_str) != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       char type_of_chat_str[50];
+       sprintf(type_of_chat_str,"%d",type_of_chat);
+
+       if (bundle_add_str(msg, "type_of_chat", type_of_chat_str) != 0) {
+               ERR("Failed to add data by key to bundle");
+               bundle_free(msg);
+       }
+
+       int result = SVC_RES_FAIL;
+       result = tg_server_send_message(tg_data->tg_server, msg);
+
+       if(result != SVC_RES_OK) {
+               // error: cient not ready
+               // send notification
+               char *icon_path = ui_utils_get_resource(DEFAULT_TELEGRAM_ICON);
+               char *title = "Telegram";
+               char content[512];
+
+               int unread_msg_cnt = get_number_of_unread_messages();
+               sprintf(content, "%d new messages received.", unread_msg_cnt);
+
+               char *sound_track = NULL;
+               char *app_id = TELEGRAM_APP_ID;
+               tg_notification_create(tg_data, icon_path, title, content, sound_track, app_id);
+
+               int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
+               if (BADGE_ERROR_NONE != err) {
+
+               }
+       }
+       bundle_free(msg);
+}
+
+
 void send_message_received_response(tg_engine_data_s *tg_data, int from_id, int to_id, long long message_id, int type_of_chat)
 {
        bundle *msg = bundle_create();
index 3be073c..fa8decc 100644 (file)
@@ -697,6 +697,7 @@ void create_buddy_msg_table(const char* table_name)
 int set_date_item_to_table(char* tb_name, int recent_msg_date)
 {
        struct tgl_message* last_msg = get_latest_message_from_message_table(tb_name);
+       recent_msg_date = recent_msg_date - 1;
        if (last_msg) {
                int old_date = last_msg->date;
                time_t old_t = old_date;
@@ -724,26 +725,11 @@ int set_date_item_to_table(char* tb_name, int recent_msg_date)
                        time_t t = cur_time;
                        char res[256];
                        sprintf(res, "%d", cur_time);
-#if 0
-                       char *format = NULL;
-                       format = "%a, %d%b. %Y";
-                       struct tm lt;
-                       char res[256];
-                       (void) localtime_r(&t, &lt);
 
-                       if (strftime(res, sizeof(res), format, &lt) == 0) {
-                               (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                               "date/time into buffer of size %u "
-                                               "using: '%s'\n",
-                                               sizeof(res), format);
-                       }
-#endif
-                       srand(time(NULL));
-                       int r = rand();
                        struct tgl_message date_msg;
-                       date_msg.id = r;
+                       date_msg.id = recent_msg_date;
                        date_msg.media.type = tgl_message_media_none;
-                       date_msg.date = recent_msg_date - 1;
+                       date_msg.date = recent_msg_date;
                        date_msg.message = res;
                        date_msg.message_len = strlen(res);
                        date_msg.service = 2;
@@ -766,27 +752,10 @@ int set_date_item_to_table(char* tb_name, int recent_msg_date)
                char res[256];
                sprintf(res, "%d", cur_time);
 
-#if 0
-               char *format = NULL;
-               format = "%a, %d%b. %Y";
-
-               struct tm lt;
-               char res[256];
-               (void) localtime_r(&t, &lt);
-
-               if (strftime(res, sizeof(res), format, &lt) == 0) {
-                       (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                       "date/time into buffer of size %u "
-                                       "using: '%s'\n",
-                                       sizeof(res), format);
-               }
-#endif
-               srand(time(NULL));
-               int r = rand();
                struct tgl_message date_msg;
-               date_msg.id = r;
+               date_msg.id = recent_msg_date;
                date_msg.media.type = tgl_message_media_none;
-               date_msg.date = recent_msg_date - 1;
+               date_msg.date = recent_msg_date;
                date_msg.message = res;
                date_msg.message_len = strlen(res);
                date_msg.service = 2;
@@ -803,6 +772,7 @@ int set_date_item_to_table(char* tb_name, int recent_msg_date)
 int update_current_date_to_table(char* tb_name, int recent_msg_date)
 {
        struct tgl_message* last_msg = get_latest_message_from_message_table(tb_name);
+       recent_msg_date = recent_msg_date -1;
        if (last_msg) {
                int old_date = last_msg->date;
                time_t old_t = old_date;
@@ -810,9 +780,7 @@ int update_current_date_to_table(char* tb_name, int recent_msg_date)
                struct tm old_lt;
                (void) localtime_r(&old_t, &old_lt);
 
-
-               int cur_time = time(NULL);
-               time_t new_t = cur_time;
+               time_t new_t = recent_msg_date;
 
                struct tm new_lt;
                (void) localtime_r(&new_t, &new_lt);
@@ -826,31 +794,15 @@ int update_current_date_to_table(char* tb_name, int recent_msg_date)
 
                        return -1;
                } else {
-                       int cur_time = time(NULL);
-                       time_t t = cur_time;
-                       char res[256];
-                       sprintf(res, "%d", cur_time);
-#if 0
-                       char *format = NULL;
-                       format = "%a, %d%b. %Y";
 
-                       struct tm lt;
+                       time_t t = recent_msg_date;
                        char res[256];
-                       (void) localtime_r(&t, &lt);
+                       sprintf(res, "%d", recent_msg_date);
 
-                       if (strftime(res, sizeof(res), format, &lt) == 0) {
-                               (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                               "date/time into buffer of size %u "strftime
-                                               "using: '%s'\n",
-                                               sizeof(res), format);
-                       }
-#endif
-                       srand(time(NULL));
-                       int r = rand();
                        struct tgl_message date_msg;
-                       date_msg.id = r;
+                       date_msg.id = recent_msg_date;
                        date_msg.media.type = tgl_message_media_none;
-                       date_msg.date = recent_msg_date - 1;
+                       date_msg.date = recent_msg_date;
                        date_msg.message = res;
                        date_msg.message_len = strlen(res);
                        date_msg.service = 2;
@@ -867,32 +819,15 @@ int update_current_date_to_table(char* tb_name, int recent_msg_date)
                }
 
        } else {
-               int cur_time = time(NULL);
-               time_t t = cur_time;
-               char res[256];
-               sprintf(res, "%d", cur_time);
-
-#if 0
-               char *format = NULL;
-               format = "%a, %d%b. %Y";
 
-               struct tm lt;
+               time_t t = recent_msg_date;
                char res[256];
-               (void) localtime_r(&t, &lt);
+               sprintf(res, "%d", recent_msg_date);
 
-               if (strftime(res, sizeof(res), format, &lt) == 0) {
-                       (void) fprintf(stderr,  "strftime(3): cannot format supplied "
-                                       "date/time into buffer of size %u "
-                                       "using: '%s'\n",
-                                       sizeof(res), format);
-               }
-#endif
-               srand(time(NULL));
-               int r = rand();
                struct tgl_message date_msg;
-               date_msg.id = r;
+               date_msg.id = recent_msg_date;
                date_msg.media.type = tgl_message_media_none;
-               date_msg.date = recent_msg_date - 1;
+               date_msg.date = recent_msg_date;
                date_msg.message = res;
                date_msg.message_len = strlen(res);
                date_msg.service = 2;
@@ -954,10 +889,8 @@ int insert_current_date_to_table(char* tb_name)
                        char res[256];
                        sprintf(res, "%d", cur_time);
 #endif
-                       srand(time(NULL));
-                       int r = rand();
                        struct tgl_message date_msg;
-                       date_msg.id = r;
+                       date_msg.id = get_time_stamp_in_macro();
                        date_msg.media.type = tgl_message_media_none;
                        date_msg.date = cur_time;
                        date_msg.message = res;
@@ -997,10 +930,8 @@ int insert_current_date_to_table(char* tb_name)
                char res[256];
                sprintf(res, "%d", cur_time);
 #endif
-               srand(time(NULL));
-               int r = rand();
                struct tgl_message date_msg;
-               date_msg.id = r;
+               date_msg.id = get_time_stamp_in_macro();
                date_msg.media.type = tgl_message_media_none;
                date_msg.date = cur_time;
                date_msg.message = res;
index 136f63d..0c20f31 100644 (file)
@@ -1030,7 +1030,7 @@ void on_new_chat_info_received(struct tgl_state *TLS, void *callback_extra, int
        strcat(creator_name, " created the group");
 
        int cur_time = time(NULL);
-       M->id = cur_time;
+       M->id = chat_info->id.id;
        M->message = creator_name;
        M->message_len = msg_len;
        M->unread = 1;
@@ -1241,7 +1241,7 @@ void on_requested_update_chat_received(struct tgl_state *TLS, void *callback_ext
                        int cur_time = chat_info->date;
                        msg.to_id = chat_info->id;
                        msg.from_id = admin_id;
-                       msg.id = cur_time;
+                       msg.id = chat_info->id.id;
                        msg.message = creator_name;
                        msg.message_len = msg_len;
                        msg.unread = 0;
@@ -1503,32 +1503,30 @@ void tg_msg_receive(struct tgl_state *TLS, struct tgl_message *M)
 #endif
                                int msg_id = update_current_date_to_table(tb_name, M->date);
                                free(tb_name);
-                               if (msg_id > 0) {
-                                       send_message_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, msg_id, tgl_get_peer_type(M->to_id));
 
-                                       struct tg_temp_msg_data *msg_data = (struct tg_temp_msg_data*)malloc(sizeof(struct tg_temp_msg_data));
-                                       msg_data->M = M;
-                                       msg_data->TLS = TLS;
-                                       msg_data->send_timer = ecore_timer_add(6, on_msg_received_cb, msg_data);
-                               } else {
-                                       if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
-                                               M->message = strdup("Audio");
-                                               M->message_len = strlen("Audio");
-                                       } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
-                                               M->message = strdup("Video");
-                                               M->message_len = strlen("Video");
-                                       }
-                                       insert_buddy_msg_to_db(M);
-                                       if(M->media.type != tgl_message_media_none) {
-                                               insert_media_info_to_db(M, "");
-                                               if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
-                                                       tgl_do_load_document_thumb(TLS, &(M->media.document), on_video_thumb_loaded, M);
-                                                       return;
-                                               } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
 
-                                               }
+                               if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
+                                       M->message = strdup("Audio");
+                                       M->message_len = strlen("Audio");
+                               } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
+                                       M->message = strdup("Video");
+                                       M->message_len = strlen("Video");
+                               }
+                               insert_buddy_msg_to_db(M);
+                               if(M->media.type != tgl_message_media_none) {
+                                       insert_media_info_to_db(M, "");
+                                       if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
+                                               tgl_do_load_document_thumb(TLS, &(M->media.document), on_video_thumb_loaded, M);
+                                               return;
+                                       } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
+
                                        }
-                                       // inform to application
+                               }
+                               // inform to application
+
+                               if (msg_id > 0) {
+                                       send_message_with_date_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, M->id, msg_id, tgl_get_peer_type(M->to_id));
+                               } else {
                                        send_message_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, M->id, tgl_get_peer_type(M->to_id));
                                }
 
@@ -1553,13 +1551,13 @@ void tg_msg_receive(struct tgl_state *TLS, struct tgl_message *M)
                                }
 
                                // check whether user is present or not
-
+#if 0
                                Eina_Bool is_present_in_peer_db = is_user_present_buddy_table(user_id);
-
                                if (!is_present_in_peer_db) {
                                        tgl_do_get_chat_info(TLS, M->to_id, 0, &on_requested_chat_info_received, M);
                                        return;
                                }
+#endif
 
                                Eina_Bool is_present_in_chat_db = is_user_present_chat_table(user_id);
                                if (!is_present_in_chat_db) {
@@ -1571,29 +1569,27 @@ void tg_msg_receive(struct tgl_state *TLS, struct tgl_message *M)
                                char* tb_name = get_table_name_from_number(user_id);
                                int msg_id = update_current_date_to_table(tb_name, M->date);
                                free(tb_name);
+
+                               if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
+                                       M->message = strdup("Audio");
+                                       M->message_len = strlen("Audio");
+                               } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
+                                       M->message = strdup("Video");
+                                       M->message_len = strlen("Video");
+                               }
+                               insert_buddy_msg_to_db(M);
+                               if(M->media.type != tgl_message_media_none) {
+                                       insert_media_info_to_db(M, "");
+                                       if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
+                                               tgl_do_load_document_thumb(TLS, &(M->media.document), on_video_thumb_loaded, M);
+                                               return;
+                                       }
+                               }
+                               // inform to application
+
                                if (msg_id > 0) {
-                                       send_message_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, msg_id, tgl_get_peer_type(M->to_id));
-                                       struct tg_temp_msg_data *msg_data = (struct tg_temp_msg_data*)malloc(sizeof(struct tg_temp_msg_data));
-                                       msg_data->M = M;
-                                       msg_data->TLS = TLS;
-                                       msg_data->send_timer = ecore_timer_add(6, on_msg_received_cb, msg_data);
+                                       send_message_with_date_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, M->id, msg_id, tgl_get_peer_type(M->to_id));
                                } else {
-                                       if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_AUDIO)) {
-                                               M->message = strdup("Audio");
-                                               M->message_len = strlen("Audio");
-                                       } else if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
-                                               M->message = strdup("Video");
-                                               M->message_len = strlen("Video");
-                                       }
-                                       insert_buddy_msg_to_db(M);
-                                       if(M->media.type != tgl_message_media_none) {
-                                               insert_media_info_to_db(M, "");
-                                               if (M->media.type != tgl_message_media_none && (M->media.document.flags & FLAG_DOCUMENT_VIDEO)) {
-                                                       tgl_do_load_document_thumb(TLS, &(M->media.document), on_video_thumb_loaded, M);
-                                                       return;
-                                               }
-                                       }
-                                       // inform to application
                                        send_message_received_response(TLS->callback_data, M->from_id.id, M->to_id.id, M->id, tgl_get_peer_type(M->to_id));
                                }
                        }
@@ -1804,7 +1800,7 @@ void on_chat_info_received(struct tgl_state *TLS, void *callback_extra, int succ
                        int cur_time = chat_info->date;
                        msg.to_id = chat_info->id;
                        msg.from_id = admin_id;
-                       msg.id = cur_time;
+                       msg.id = chat_info->id.id;
                        msg.message = creator_name;
                        msg.message_len = msg_len;
                        msg.unread = 0;
@@ -1969,15 +1965,18 @@ static Eina_Bool send_chat_loading_is_done_response(void *data)
 void on_offline_chat_received(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[])
 {
        tg_engine_data_s *tg_data = TLS->callback_data;
-       for (int i = 0; i < size; i++) {
+       for (int i = size - 1; i >= 0; i--) {
                struct tgl_message* message = list[i];
                if (message->service || message->from_id.id == tg_data->id.id) {
                        continue;
                }
+               /*
                Eina_Bool ret = insert_buddy_msg_to_db(message);
                if (ret) {
                        tg_msg_receive(s_info.TLS, message);
                }
+               */
+               tg_msg_receive(s_info.TLS, message);
        }
 }
 
@@ -2070,14 +2069,14 @@ void on_peer_chat_info_received(struct tgl_state *TLS, void *callback_extra, int
                strcpy(creator_name, UC->user.first_name);
                strcat(creator_name, " created the group");
                struct tgl_message msg;
-               int cur_time = chat_info->date;
+
                msg.to_id = chat_info->id;
                msg.from_id = admin_id;
-               msg.id = cur_time;
+               msg.id = chat_info->id.id;
                msg.message = creator_name;
                msg.message_len = msg_len;
                msg.unread = 0;
-               msg.date = cur_time;
+               msg.date = chat_info->date;
                msg.media.type = tgl_message_media_none;
                msg.service = 1;
                msg.out = 0;
@@ -2114,7 +2113,7 @@ void on_peer_chat_info_received(struct tgl_state *TLS, void *callback_extra, int
        }
 
 end:
-       ecore_timer_add(0.5, on_async_peer_info_requested, TLS);
+       ecore_timer_add(1, on_async_peer_info_requested, TLS);
        return;
 }
 
@@ -2209,6 +2208,7 @@ void on_contacts_and_chats_loaded(struct tgl_state *TLS, void *callback_extra, i
 
        if ((tg_data->chat_list == NULL) || (eina_list_count(tg_data->chat_list) <= 0)) {
                send_contacts_and_chats_load_done_response(TLS->callback_data, EINA_TRUE);
+               ecore_timer_add(5, on_send_unsent_messages_requested, TLS);
        } else {
                // load chat info one by one.
                tg_data->current_chat_index = 0;
@@ -2560,7 +2560,7 @@ void on_new_group_created(struct tgl_state *TLS, void *callback_extra, int succe
 
 
                                int cur_time = time(NULL);
-                               M->id = cur_time;
+                               M->id = M->to_id.id;
                                M->message = creator_name;
                                M->message_len = msg_len;
                                M->unread = 1;
index 2c5c998..0dbd6a8 100644 (file)
@@ -37,6 +37,13 @@ typedef struct msg_list_container {
        int current_index;
 } msg_list_container_s;
 
+static uint64_t get_time_stamp_in_macro()
+{
+    struct timeval tv;
+    gettimeofday(&tv,NULL);
+    return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
+}
+
 extern char *ui_utils_get_resource(const char *res_name);
 
 extern char *get_table_name_from_number(const int id);