fixed nextwork failed issue while login.
[apps/native/telegram-tizen.git] / TelegramTizen / src / telegramtizen.c
1 /*
2     This file is part of Telegram application for tizen
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include "telegramtizen.h"
23 #include "tg_splash_screen.h"
24 #include "tg_registration.h"
25 #include "tg_db_wrapper.h"
26 #include "tg_login.h"
27 #include "tg_first_registration.h"
28 #include "server_requests.h"
29 #include "tg_init_screen.h"
30 #include "tg_user_main_view.h"
31 #include "tg_messaging_view.h"
32 #include "tg_start_messaging_view.h"
33 #include "tg_user_info_view.h"
34 #include "tg_chat_info_view.h"
35 #include "ucol.h"
36 #include <notification.h>
37 #include <badge.h>
38 #include "tg_settings_view.h"
39 #include "device_contacts_manager.h"
40 #include "tg_search_peer_view.h"
41 #include <sys/stat.h>
42
43 static void free_app_data(appdata_s *app_data, Eina_Bool destroy_server);
44 static int init_service(appdata_s *app);
45 static void popup_block_clicked_cb(void *data, Evas_Object *obj, void *event_info)
46 {
47         evas_object_del(obj);
48 }
49
50 static void popup_timeout_cb(void *data, Evas_Object *obj, void *event_info)
51 {
52         evas_object_del(obj);
53 }
54
55 void show_toast(appdata_s *ad, char* value)
56 {
57         Evas_Object *popup;
58         Evas_Object *win = ad->win;
59         popup = elm_popup_add(win);
60         elm_object_style_set(popup, "toast");
61         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
62         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
63
64         char buf[512] = {'\0'};
65         snprintf(buf, sizeof(buf), "<font_size=30><color=#000000>%s</color></font_size>", value);
66
67         //elm_object_part_text_set(popup,"elm.text", buf);
68         elm_object_text_set(popup, buf);
69         evas_object_smart_callback_add(popup, "block,clicked", popup_block_clicked_cb, win);
70         elm_popup_timeout_set(popup, 2.0);
71         evas_object_smart_callback_add(popup, "timeout", popup_timeout_cb, NULL);
72         evas_object_show(popup);
73 }
74
75 static int _app_send_response(appdata_s *app, bundle *const msg)
76 {
77         int res = SVC_RES_FAIL;
78         res = service_client_send_message(app->service_client, msg);
79         return res;
80 }
81
82
83 void load_list_of_countries(appdata_s *ad)
84 {
85         if (!ad)
86                 return;
87
88         int size_of_listed_countries = 0;
89         const char *file_name = ui_utils_get_resource(TG_LIST_OF_COUNTIRES);
90         if (!file_name)
91                 return;
92
93         int fd;
94         char *ptr;
95         char *ptrToCode;
96         char *ptrToShortName;
97         char *ptrToFullName;
98         enum parse_state {
99                 STATE_BEGIN,
100                 STATE_CODE,
101                 STATE_SHORT_NAME,
102                 STATE_FULL_NAME,
103                 STATE_ERROR
104         } state;
105
106         fd = open(file_name, O_RDONLY);
107         if (fd > 0) {
108                 off_t file_size;
109
110                 file_size = lseek(fd, 0L, SEEK_END);
111                 if ((int)file_size == -1) {
112                         if (close(fd) < 0) {
113
114                         }
115                 } else {
116                         if (lseek(fd, 0L, SEEK_SET) < 0) {
117                                 // TODO: Error
118                         }
119                         ad->country_code_buffer = malloc(file_size);
120                         if (!ad->country_code_buffer) {
121                                 // TODO: Error
122                         }
123
124                         if (read(fd, ad->country_code_buffer, file_size) != file_size) {
125                                 // TODO: Error
126                         }
127
128                         if (close(fd) < 0) {
129
130                         }
131                 }
132         } else {
133                 // TODO: Error
134         }
135
136         ptr = ad->country_code_buffer;
137         state = STATE_BEGIN;
138         while (*ptr) {
139                 switch (state) {
140                 case STATE_BEGIN:
141                         ptrToCode = NULL;
142                         ptrToShortName = NULL;
143                         ptrToFullName = NULL;
144                         state = STATE_CODE;
145                 case STATE_CODE:
146                         if (ptrToCode == NULL) {
147                                 if (isdigit(*ptr)) {
148                                         ptrToCode = ptr;
149                                 }
150                         } else if (*ptr == ';') {
151                                 *ptr = '\0';
152                                 state = STATE_SHORT_NAME;
153                         }
154                         ptr++;
155                         break;
156                 case STATE_SHORT_NAME:
157                         if (ptrToShortName == NULL) {
158                                 ptrToShortName = ptr;
159                         } else if (*ptr == ';') {
160                                 *ptr = '\0';
161                                 state = STATE_FULL_NAME;
162                         }
163                         ptr++;
164                         break;
165                 case STATE_FULL_NAME:
166                         if (ptrToFullName == NULL) {
167                                 ptrToFullName = ptr;
168                         } else if (*ptr == '\r' || *ptr == '\n') {
169                                 *ptr = '\0';
170                                 dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] [%s] [%s]", ptrToCode, ptrToShortName, ptrToFullName);
171                                 ad->country_codes_list = eina_list_append(ad->country_codes_list, ptrToCode);
172                                 ad->country_names_list = eina_list_append(ad->country_names_list, ptrToFullName);
173                                 state = STATE_BEGIN;
174                         }
175                         ptr++;
176                         break;
177                 case STATE_ERROR:
178                 default:
179                         break;
180                 }
181         }
182 }
183
184 void load_registered_user_data(appdata_s *ad)
185 {
186         Eina_List *user_info = get_registered_user_info();
187
188         if (!user_info) {
189                 //error
190                 //show_toast(ad, "User info not loaded");
191         }
192
193         if (ad->current_user_data) {
194                 free_user_data(ad->current_user_data);
195                 ad->current_user_data = NULL;
196         }
197
198         ad->current_user_data = (user_data_s*)malloc(sizeof(user_data_s));
199
200
201         Eina_List *row_vals = NULL;
202         int pos = 0;
203         EINA_LIST_FREE(user_info, row_vals) {
204                 if (!row_vals)
205                         continue;
206                 if (pos == 0) {
207                         int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
208                         if (temp_user_id) {
209                                 int user_id = *temp_user_id;
210                                 free(temp_user_id);
211
212                                 ad->user_id.id = user_id;
213                                 ad->user_id.type = TGL_PEER_USER;
214
215                                 ad->current_user_data->user_id.id = user_id;
216                                 ad->current_user_data->user_id.type = TGL_PEER_USER;
217                         }
218
219                         ad->current_user_data->print_name = (char *)eina_list_nth(row_vals, 1);
220
221                         int *temp_struct_ver = (int *)eina_list_nth(row_vals, 2);
222                         if (temp_struct_ver) {
223                                 ad->current_user_data->structure_version = *temp_struct_ver;
224                                 free(temp_struct_ver);
225                         }
226
227                         ad->current_user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
228
229                         int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
230                         if (temp_photo_id) {
231                                 ad->current_user_data->photo_id = *temp_photo_id;
232                                 free(temp_photo_id);
233                         }
234
235                         ad->current_user_data->first_name = (char *)eina_list_nth(row_vals, 5);
236
237                         ad->current_user_data->last_name = (char *)eina_list_nth(row_vals, 6);
238
239                         char *phone_no = (char *)eina_list_nth(row_vals, 7);
240                         ad->current_user_data->phone = phone_no;
241
242                         int *temp_access_hash = (int *)eina_list_nth(row_vals, 8);
243                         if (temp_access_hash) {
244                                 ad->current_user_data->access_hash = *temp_access_hash;
245                                 free(temp_access_hash);
246                         }
247
248                         ad->current_user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
249
250                         ad->current_user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
251
252                         char *user_name = (char *)eina_list_nth(row_vals, 11);
253                         ad->current_user_data->username = user_name;
254
255                         int *temp_online_status = (int *)eina_list_nth(row_vals, 12);
256                         if (temp_online_status) {
257                                 ad->current_user_data->online = *temp_online_status;
258                                 free(temp_online_status);
259                         }
260
261                         int *temp_last_seen = (int *)eina_list_nth(row_vals, 13);
262                         if (temp_last_seen) {
263                                 ad->current_user_data->last_seen = *temp_last_seen;
264                                 free(temp_last_seen);
265                         }
266                 } else {
267                         void* val = NULL;
268                         EINA_LIST_FREE(row_vals, val) {
269                                 if (val)
270                                         free(val);
271                         }
272                 }
273                 pos++;
274         }
275         eina_list_free(user_info);
276 }
277
278 void load_peer_data(appdata_s *ad)
279 {
280         if (!ad) {
281                 return;
282         }
283
284
285         peer_with_pic_s* pic_item = NULL;
286         EINA_LIST_FREE(ad->peer_list, pic_item) {
287                 if (!pic_item)
288                         continue;
289                 tg_peer_info_s* item = pic_item->use_data;
290                 if (item) {
291                         if (item->print_name) {
292                                 free(item->print_name);
293                                 item->print_name = NULL;
294                         }
295                         if (item->photo_path) {
296                                 free(item->photo_path);
297                                 item->photo_path = NULL;
298                         }
299                         pic_item->contact_icon = NULL;
300                         pic_item->msg_object = NULL;
301                         pic_item->name_object = NULL;
302                         free(item);
303                 }
304                 free(pic_item);
305         }
306
307         ad->peer_list = NULL;
308
309
310         Eina_List *peer_details = get_all_peer_details(NULL);
311
312         Eina_List *ts_msg = NULL;
313         EINA_LIST_FREE(peer_details, ts_msg) {
314                 tg_peer_info_s* peer_info = (tg_peer_info_s*)malloc(sizeof(*peer_info));
315
316                 int *temp_peer_id = (int *)eina_list_nth(ts_msg, 0);
317                 if (temp_peer_id) {
318                         peer_info->peer_id  = *temp_peer_id;
319                         free(temp_peer_id);
320                 }
321
322                 int *temp_peer_type = (int *)eina_list_nth(ts_msg, 1);
323                 if (temp_peer_type) {
324                         peer_info->peer_type  = *temp_peer_type;
325                         free(temp_peer_type);
326                 }
327
328                 int *temp_flags = (int *)eina_list_nth(ts_msg, 2);
329                 if (temp_flags) {
330                         peer_info->flags = *temp_flags;
331                         free(temp_flags);
332                 }
333
334                 char *temp_msg_id = (char *)eina_list_nth(ts_msg, 3);
335                 if (temp_msg_id) {
336                         peer_info->last_msg_id  = atoll(temp_msg_id);
337                         free(temp_msg_id);
338                 } else {
339                         peer_info->last_msg_id = 0;
340                 }
341
342                 int *temp_last_msg_date = (int *)eina_list_nth(ts_msg, 4);
343                 if (temp_last_msg_date) {
344                         peer_info->last_msg_date = *temp_last_msg_date;
345                         free(temp_last_msg_date);
346                 }
347
348                 peer_info->print_name = (char *)eina_list_nth(ts_msg, 5);
349
350                 int *temp_struct_ver = (int *)eina_list_nth(ts_msg, 6);
351                 if (temp_struct_ver) {
352                         peer_info->struct_version = *temp_struct_ver;
353                         free(temp_struct_ver);
354                 }
355
356                 int *temp_no_unread = (int *)eina_list_nth(ts_msg, 7);
357                 if (temp_no_unread) {
358                         peer_info->no_of_unread_msgs = *temp_no_unread;
359                         free(temp_no_unread);
360                 }
361
362                 int *temp_last_seen = (int *)eina_list_nth(ts_msg, 8);
363                 if (temp_last_seen) {
364                         peer_info->last_seen_time = *temp_last_seen;
365                         free(temp_last_seen);
366                 }
367
368                 peer_info->photo_path = (char *)eina_list_nth(ts_msg, 9);
369
370                 int *temp_pic_id = (int *)eina_list_nth(ts_msg, 10);
371                 if (temp_pic_id) {
372                         peer_info->photo_id = *temp_pic_id;
373                         free(temp_pic_id);
374                 }
375                 int *temp_unknown = (int *)eina_list_nth(ts_msg, 11);
376                 if (temp_unknown) {
377                         peer_info->is_unknown = *temp_unknown;
378                         free(temp_unknown);
379                 }
380
381                 eina_list_free(ts_msg);
382
383                 peer_with_pic_s *item = (peer_with_pic_s*) malloc(sizeof(*item));
384                 item->use_data = peer_info;
385                 item->contact_icon = NULL;
386
387                 ad->peer_list = eina_list_append(ad->peer_list, item);
388
389         }
390 }
391
392 void load_main_list_data(appdata_s *ad)
393 {
394         if (!ad || !ad->peer_list)
395                 return;
396
397         tg_main_list_item_s* main_item = NULL;
398         EINA_LIST_FREE(ad->main_list, main_item) {
399
400                 if (main_item->peer_print_name)
401                         free(main_item->peer_print_name);
402                 main_item->peer_print_name = NULL;
403
404                 if (main_item->last_message)
405                         free(main_item->last_message);
406                 main_item->last_message = NULL;
407
408                 if (main_item->profile_pic_path)
409                         free(main_item->profile_pic_path);
410                 main_item->profile_pic_path = NULL;
411
412                 if (main_item->buddy_display_name)
413                         free(main_item->buddy_display_name);
414                 main_item->buddy_display_name = NULL;
415
416                 main_item->date_lbl = NULL;
417                 main_item->msg_status_lbl = NULL;
418                 main_item->profile_pic = NULL;
419                 main_item->profile_pic_path = NULL;
420                 main_item->status_lbl = NULL;
421                 main_item->user_name_lbl = NULL;
422                 main_item->main_item_layout = NULL;
423         }
424         ad->main_list = NULL;
425
426         Eina_List *l = NULL;
427         peer_with_pic_s *item = NULL;
428         EINA_LIST_FOREACH(ad->peer_list, l, item) {
429
430                 tg_peer_info_s* peer_info = item->use_data;
431                 if (!peer_info)
432                         continue;
433
434                 if (peer_info->peer_type == TGL_PEER_CHAT &&
435                                 get_buddy_unknown_status(peer_info->peer_id))
436                         continue;
437
438                 //if (peer_info->last_msg_id > 0) {
439
440                 // get message from message table.
441
442                 char* tablename = get_table_name_from_number(peer_info->peer_id);
443                 tg_message_s *msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
444
445                 if (msg) {
446
447                         if (msg->service) {
448                                 if (msg->message && strlen(msg->message) > 0) {
449                                         item->last_message = strdup(msg->message);
450                                 } else {
451                                         item->last_message = strdup(" ");
452                                 }
453                         } else {
454
455                                 int media_type = msg->media_type;
456                                 if (media_type == tgl_message_media_none) {
457                                         item->last_message = strdup(msg->message ? msg->message : " ");
458                                 } else if (media_type == tgl_message_media_photo) {
459                                         item->last_message = strdup("Image");
460                                 } else if (media_type == tgl_message_media_document) {
461                                         tgl_media_s *media_msg = NULL;
462                                         media_msg = get_media_details_from_db(atoll(msg->media_id));
463
464                                         if (media_msg && media_msg->doc_type) {
465                                                 if (strlen(media_msg->doc_type) > 0) {
466                                                         item->last_message = strdup(media_msg->doc_type);
467                                                 } else {
468                                                         item->last_message = strdup("Document");
469                                                 }
470                                                 free_media_details(media_msg);
471                                         } else {
472                                                 if (msg->message && strlen(msg->message) > 0) {
473                                                         item->last_message = strdup(msg->message);
474                                                 } else {
475                                                         item->last_message = strdup("Document");
476                                                 }
477                                         }
478
479                                 } else if (media_type == tgl_message_media_geo) {
480                                         item->last_message = strdup("Geo location");
481                                 } else if (media_type == tgl_message_media_contact) {
482                                         item->last_message = strdup("Contact");
483                                 } else if (media_type == tgl_message_media_unsupported) {
484                                         item->last_message = strdup(" ");
485                                 } else if (media_type == tgl_message_media_photo_encr) {
486                                         item->last_message = strdup("Image encrypted");
487                                 } else if (media_type == tgl_message_media_document_encr) {
488                                         item->last_message = strdup("Document encrypted");
489                                 } else  {
490                                         item->last_message = strdup(" ");
491                                 }
492                         }
493
494                         tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
495                         main_list_item->peer_id = peer_info->peer_id;
496                         main_list_item->peer_type = peer_info->peer_type;
497
498                         set_peer_names(peer_info, main_list_item);
499
500                         main_list_item->last_seen_time = msg->date;
501                         main_list_item->profile_pic = NULL;
502                         main_list_item->last_msg_id = msg->msg_id;
503                         main_list_item->last_message = strdup(item->last_message);
504                         main_list_item->last_msg_type = msg->media_type;
505                         main_list_item->is_out_msg = msg->out;
506                         main_list_item->last_msg_status = msg->msg_state;
507                         main_list_item->last_msg_service = msg->service;
508                         main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
509
510                         if (peer_info->photo_path)
511                                 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
512                         else
513                                 main_list_item->profile_pic_path = NULL;
514
515                         main_list_item->user_name_lbl = NULL;
516                         main_list_item->status_lbl = NULL;
517                         main_list_item->date_lbl = NULL;
518                         main_list_item->msg_status_lbl = NULL;
519                         main_list_item->main_item_layout = NULL;
520                         ad->main_list = eina_list_append(ad->main_list, main_list_item);
521
522                         // delete message object
523                         if (msg->message)
524                                 free(msg->message);
525                         msg->message = NULL;
526
527                         if (msg->media_id)
528                                 free(msg->media_id);
529                         msg->media_id = NULL;
530
531                         free(msg);
532                         msg = NULL;
533
534                 } else {
535                         item->last_message = strdup(" ");
536                         if (peer_info->peer_type == TGL_PEER_CHAT) {
537                                 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
538                                 main_list_item->peer_id = peer_info->peer_id;
539                                 main_list_item->peer_type = peer_info->peer_type;
540
541                                 set_peer_names(peer_info, main_list_item);
542
543                                 main_list_item->last_seen_time = peer_info->last_seen_time;
544                                 main_list_item->profile_pic = NULL;
545                                 main_list_item->last_message = strdup(item->last_message);
546                                 main_list_item->last_msg_type = -1;
547                                 main_list_item->last_msg_service = 0;
548                                 main_list_item->is_out_msg = -1;
549                                 main_list_item->last_msg_id = -1;
550                                 main_list_item->last_msg_status = -1;
551                                 main_list_item->number_of_unread_msgs = 0;
552
553                                 if (peer_info->photo_path) {
554                                         main_list_item->profile_pic_path = strdup(peer_info->photo_path);
555                                 } else {
556                                         main_list_item->profile_pic_path = NULL;
557                                 }
558                                 main_list_item->user_name_lbl = NULL;
559                                 main_list_item->status_lbl = NULL;
560                                 main_list_item->date_lbl = NULL;
561                                 main_list_item->msg_status_lbl = NULL;
562                                 main_list_item->main_item_layout = NULL;
563                                 ad->main_list = eina_list_append(ad->main_list, main_list_item);
564                         }
565                 }
566
567                 if (tablename) {
568                         free(tablename);
569                         tablename = NULL;
570                 }
571         }
572 }
573
574 void load_unknown_buddy_list_data(appdata_s *ad)
575 {
576
577         if (!ad)
578                 return;
579
580         user_data_with_pic_s *item = NULL;
581         EINA_LIST_FREE(ad->unknown_buddy_list, item) {
582                 user_data_s* user_data = item->use_data;
583
584                 if (user_data->print_name)
585                         free(user_data->print_name);
586                 user_data->print_name = NULL;
587
588                 if (user_data->photo_path)
589                         free(user_data->photo_path);
590                 user_data->photo_path = NULL;
591
592                 if (user_data->first_name)
593                         free(user_data->first_name);
594                 user_data->first_name = NULL;
595
596                 if (user_data->last_name)
597                         free(user_data->last_name);
598                 user_data->last_name = NULL;
599
600                 if (user_data->phone)
601                         free(user_data->phone);
602                 user_data->phone = NULL;
603
604                 if (user_data->real_first_name)
605                         free(user_data->real_first_name);
606                 user_data->real_first_name = NULL;
607
608                 if (user_data->real_last_name)
609                         free(user_data->real_last_name);
610                 user_data->real_last_name = NULL;
611
612                 if (user_data->username)
613                         free(user_data->username);
614                 user_data->username = NULL;
615
616                 free(user_data);
617                 free(item);
618         }
619         ad->unknown_buddy_list = NULL;
620
621         Eina_List *user_info = get_unknown_buddy_list_info();
622         if (!user_info) {
623                 //error
624                 //show_toast(ad, "User info not loaded");
625                 return;
626         }
627
628         Eina_List *row_vals = NULL;
629         EINA_LIST_FREE(user_info, row_vals) {
630
631                 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
632                 if (ad->user_id.id == (*temp_user_id)) {
633                         void* val = NULL;
634                         EINA_LIST_FREE(row_vals, val) {
635                                 if (val)
636                                         free(val);
637                         }
638                         continue;
639                 }
640
641                 user_data_s *user_data = (user_data_s*)malloc(sizeof(*user_data));
642                 user_data->is_selected = EINA_FALSE;
643
644
645                 if (temp_user_id) {
646                         user_data->user_id.id = *temp_user_id;
647                         user_data->user_id.type = TGL_PEER_USER;
648                         free(temp_user_id);
649                 }
650
651                 user_data->print_name = (char *)eina_list_nth(row_vals, 1);
652
653                 int *temp_struct_ver = (int *)eina_list_nth(row_vals, 2);
654                 if (temp_struct_ver) {
655                         user_data->structure_version = *temp_struct_ver;
656                         free(temp_struct_ver);
657                 }
658
659                 user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
660
661                 int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
662                 if (temp_photo_id) {
663                         user_data->photo_id = *temp_photo_id;
664                         free(temp_photo_id);
665                 }
666
667                 user_data->first_name = (char *)eina_list_nth(row_vals, 5);
668
669                 user_data->last_name = (char *)eina_list_nth(row_vals, 6);
670
671                 user_data->phone = (char *)eina_list_nth(row_vals, 7);
672
673                 int *temp_access_hash = (int *)eina_list_nth(row_vals, 8);
674                 if (temp_access_hash) {
675                         user_data->access_hash = *temp_access_hash;
676                         free(temp_access_hash);
677                 }
678
679                 user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
680
681                 user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
682
683                 user_data->username = (char *)eina_list_nth(row_vals, 11);
684
685                 int *temp_online_status = (int *)eina_list_nth(row_vals, 12);
686                 if (temp_online_status) {
687                         user_data->online = *temp_online_status;
688                         free(temp_online_status);
689                 }
690
691                 int *temp_last_seen = (int *)eina_list_nth(row_vals, 13);
692                 if (temp_last_seen) {
693                         user_data->last_seen = *temp_last_seen;
694                         free(temp_last_seen);
695                 }
696
697                 int *temp_is_blocked = (int *)eina_list_nth(row_vals, 14);
698                 if (temp_is_blocked) {
699                         user_data->is_blocked = *temp_is_blocked;
700                         free(temp_is_blocked);
701                 }
702
703                 int *temp_is_deleted = (int *)eina_list_nth(row_vals, 15);
704                 if (temp_is_deleted) {
705                         user_data->is_deleted = *temp_is_deleted;
706                         free(temp_is_deleted);
707                 }
708
709                 int *temp_is_unknown = (int *)eina_list_nth(row_vals, 16);
710                 if (temp_is_unknown) {
711                         user_data->is_unknown = *temp_is_unknown;
712                         free(temp_is_unknown);
713                 }
714
715                 user_data_with_pic_s *item = (user_data_with_pic_s *)malloc(sizeof(*item));
716                 item->use_data = user_data;
717                 item->contact_icon = NULL;
718                 ad->unknown_buddy_list = eina_list_append(ad->unknown_buddy_list, item);
719
720                 eina_list_free(row_vals);
721         }
722 }
723
724 void load_buddy_list_data(appdata_s *ad)
725 {
726         if (!ad) {
727                 return;
728         }
729
730         user_data_with_pic_s *item = NULL;
731         EINA_LIST_FREE(ad->buddy_list, item) {
732                 user_data_s* user_data = item->use_data;
733
734                 if (user_data->print_name)
735                         free(user_data->print_name);
736                         user_data->print_name = NULL;
737
738                 if (user_data->photo_path)
739                         free(user_data->photo_path);
740                         user_data->photo_path = NULL;
741
742                 if (user_data->first_name)
743                         free(user_data->first_name);
744                         user_data->first_name = NULL;
745
746                 if (user_data->last_name)
747                         free(user_data->last_name);
748                         user_data->last_name = NULL;
749
750                 if (user_data->phone)
751                         free(user_data->phone);
752                         user_data->phone = NULL;
753
754                 if (user_data->real_first_name)
755                         free(user_data->real_first_name);
756                         user_data->real_first_name = NULL;
757
758                 if (user_data->real_last_name)
759                         free(user_data->real_last_name);
760                         user_data->real_last_name = NULL;
761
762                 if (user_data->username)
763                         free(user_data->username);
764                         user_data->username = NULL;
765
766                 free(user_data);
767                 free(item);
768         }
769         ad->buddy_list = NULL;
770
771         Eina_List *user_info = get_buddy_list_info();
772
773         if (!user_info) {
774                 //error
775                 //show_toast(ad, "User info not loaded");
776                 return;
777         }
778
779         Eina_List *row_vals = NULL;
780         EINA_LIST_FREE(user_info, row_vals) {
781                 if (!row_vals)
782                         continue;
783                 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
784                 if (ad->user_id.id == (*temp_user_id)) {
785                         void* val = NULL;
786                         EINA_LIST_FREE(row_vals, val) {
787                                 if (val)
788                                         free(val);
789                         }
790                         continue;
791                 }
792
793                 user_data_s *user_data = (user_data_s *)malloc(sizeof(*user_data));
794                 user_data->is_selected = EINA_FALSE;
795
796
797                 if (temp_user_id) {
798                         user_data->user_id.id = *temp_user_id;
799                         user_data->user_id.type = TGL_PEER_USER;
800                         free(temp_user_id);
801                 }
802
803                 user_data->print_name = (char *)eina_list_nth(row_vals, 1);
804
805                 int *temp_struct_ver = (int *)eina_list_nth(row_vals, 2);
806                 if (temp_struct_ver) {
807                         user_data->structure_version = *temp_struct_ver;
808                         free(temp_struct_ver);
809                 }
810
811                 user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
812
813                 int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
814                 if (temp_photo_id) {
815                         user_data->photo_id = *temp_photo_id;
816                         free(temp_photo_id);
817                 }
818
819                 user_data->first_name = (char *)eina_list_nth(row_vals, 5);
820
821                 user_data->last_name = (char *)eina_list_nth(row_vals, 6);
822
823                 user_data->phone = (char *)eina_list_nth(row_vals, 7);
824
825                 int *temp_access_hash = (int *)eina_list_nth(row_vals, 8);
826                 if (temp_access_hash) {
827                         user_data->access_hash = *temp_access_hash;
828                         free(temp_access_hash);
829                 }
830
831                 user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
832
833                 user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
834
835                 user_data->username = (char *)eina_list_nth(row_vals, 11);
836
837                 int *temp_online_status = (int *)eina_list_nth(row_vals, 12);
838                 if (temp_online_status) {
839                         user_data->online = *temp_online_status;
840                         free(temp_online_status);
841                 }
842
843                 int *temp_last_seen = (int *)eina_list_nth(row_vals, 13);
844                 if (temp_last_seen) {
845                         user_data->last_seen = *temp_last_seen;
846                         free(temp_last_seen);
847                 }
848
849                 int *temp_is_blocked = (int *)eina_list_nth(row_vals, 14);
850                 if (temp_is_blocked) {
851                         user_data->is_blocked = *temp_is_blocked;
852                         free(temp_is_blocked);
853                 }
854
855                 int *temp_is_deleted = (int *)eina_list_nth(row_vals, 15);
856                 if (temp_is_deleted) {
857                         user_data->is_deleted = *temp_is_deleted;
858                         free(temp_is_deleted);
859                 }
860
861                 int *temp_is_unknown = (int *)eina_list_nth(row_vals, 16);
862                 if (temp_is_unknown) {
863                         user_data->is_unknown = *temp_is_unknown;
864                         free(temp_is_unknown);
865                 }
866
867                 user_data_with_pic_s *item = (user_data_with_pic_s *) malloc(sizeof(*item));
868                 item->use_data = user_data;
869                 item->contact_icon = NULL;
870                 ad->buddy_list = eina_list_append(ad->buddy_list, item);
871                 eina_list_free(row_vals);
872         }
873 }
874
875
876 static int on_contacts_loaded(appdata_s *app, bundle *const rec_msg)
877 {
878         int result = SVC_RES_FAIL;
879         char* is_success_val = NULL;
880         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
881         if (strncmp("true", is_success_val, strlen("true")) == 0) {
882
883                 // Load user data
884
885                 load_registered_user_data(app);
886
887                 if (app->current_app_state == TG_LOGIN_STATE || app->current_app_state == TG_REGISTRATION_STATE || app->current_app_state == TG_PROFILE_REGISTRATION_STATE) {
888                         // Launch buddy list view
889                         load_buddy_list_data(app);
890                         load_unknown_buddy_list_data(app);
891                         load_peer_data(app);
892                         load_main_list_data(app);
893                         elm_naviframe_item_pop(app->nf);
894                         elm_naviframe_item_pop(app->nf);
895                         //launch_buddy_list_cb(app);
896
897                         //launch_user_main_view_cb(app);
898                         launch_start_messaging_view(app);
899
900                 } else if (app->current_app_state == TG_BUDDY_LIST_STATE) {
901                         ////refresh_buddy_list(app);
902                 } else {
903
904                 }
905
906         } else {
907                 // error handling
908                 show_toast(app, "Error: loading contacts");
909         }
910         hide_loading_popup(app);
911         return result;
912 }
913
914
915 static int on_contacts_and_chats_loaded(appdata_s *app, bundle *const rec_msg)
916 {
917         int result = SVC_RES_FAIL;
918         char* is_success_val = NULL;
919         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
920         if (strncmp("true", is_success_val, strlen("true")) == 0) {
921
922                 // Load user data
923                 //Eina_List *user_info = get_registered_user_info();
924                 load_registered_user_data(app);
925
926                 if (app->current_app_state == TG_LOGIN_STATE || app->current_app_state == TG_REGISTRATION_STATE || app->current_app_state == TG_PROFILE_REGISTRATION_STATE) {
927                         // Launch buddy list view
928                         load_buddy_list_data(app);
929                         load_unknown_buddy_list_data(app);
930                         load_peer_data(app);
931                         load_main_list_data(app);
932                         elm_naviframe_item_pop(app->nf);
933                         elm_naviframe_item_pop(app->nf);
934                         //launch_buddy_list_cb(app);
935                         launch_user_main_view_cb(app);
936                 } else if (app->current_app_state == TG_BUDDY_LIST_STATE) {
937                         app->current_app_state = TG_BUDDY_LIST_STATE;
938                         evas_object_show(app->panel);
939                         //elm_panel_hidden_set(app->panel, EINA_FALSE);
940                         ////refresh_buddy_list(app);
941                 } else {
942
943                 }
944
945         } else {
946                 // error handling
947                 show_toast(app, "Error: loading contacts");
948         }
949         hide_loading_popup(app);
950         return result;
951 }
952
953 static int on_group_chat_updated(appdata_s *app, bundle *const rec_msg)
954 {
955         int result = SVC_RES_FAIL;
956         char *chat_id_str = NULL;
957         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
958         int chat_id = atoi(chat_id_str);
959
960         int buddy_id = -1;
961         app->main_item_in_cahtting_data = NULL;
962         app->peer_in_cahtting_data = NULL;
963
964         Eina_List *l = NULL;
965         tg_main_list_item_s *item = NULL;
966         EINA_LIST_FOREACH(app->main_list, l, item) {
967                 if (!item)
968                         continue;
969                 if (item->peer_id == chat_id) {
970                         app->main_item_in_cahtting_data = item;
971                         break;
972                 }
973         }
974
975         l = NULL;
976         peer_with_pic_s* pic_item = NULL;
977         int i = 0;
978         EINA_LIST_FOREACH(app->peer_list, l, pic_item) {
979                 if (!pic_item) {
980                         i++;
981                         continue;
982                 }
983                 tg_peer_info_s* item = pic_item->use_data;
984                 if (item->peer_id == chat_id) {
985                         app->peer_in_cahtting_data = pic_item;
986                         buddy_id = i;
987                         break;
988                 }
989                 i++;
990         }
991
992         launch_messaging_view_cb(app, buddy_id);
993         hide_loading_popup(app);
994         return result;
995 }
996
997 static int on_group_chat_info_changed_response(appdata_s *app, bundle *const rec_msg)
998 {
999         int result = SVC_RES_FAIL;
1000         char *chat_id_str = NULL;
1001         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1002         int chat_id = atoi(chat_id_str);
1003
1004         char *type_of_change = NULL;
1005         result = bundle_get_str(rec_msg, "type_of_change", &type_of_change);
1006
1007
1008         peer_with_pic_s *peer_item = get_peer_info(chat_id);
1009         tg_main_list_item_s *latest_item = get_latest_item(app, peer_item);
1010
1011
1012         Eina_List *l = NULL;
1013         peer_with_pic_s *pitem = NULL;
1014         EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1015                 if (!pitem)
1016                         continue;
1017                 if (pitem->use_data->peer_id == chat_id) {
1018                         app->peer_list = eina_list_remove(app->peer_list, pitem);
1019                         break;
1020                 }
1021         }
1022
1023         l = NULL;
1024         tg_main_list_item_s *item = NULL;
1025         EINA_LIST_FOREACH(app->main_list, l, item) {
1026                 if (!item)
1027                         continue;
1028
1029                 if (item->peer_id == chat_id) {
1030                         app->main_list = eina_list_remove(app->main_list, item);
1031                         break;
1032                 }
1033         }
1034
1035         app->peer_list = eina_list_prepend(app->peer_list, peer_item);
1036         app->main_list = eina_list_prepend(app->main_list, latest_item);
1037         refresh_main_list_view(app, EINA_TRUE);
1038
1039         if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1040                         && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1041
1042                 app->peer_in_cahtting_data = peer_item;
1043                 app->main_item_in_cahtting_data = latest_item;
1044
1045                 on_group_chat_info_changed(app, type_of_change);
1046         }
1047
1048         if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1049                         && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1050
1051                 app->peer_in_cahtting_data = peer_item;
1052                 app->main_item_in_cahtting_data = latest_item;
1053
1054                 on_group_chat_info_changed(app, type_of_change);
1055
1056                 on_group_chat_info_updated(app, type_of_change);
1057
1058         }
1059         hide_loading_popup(app);
1060
1061         return result;
1062 }
1063
1064 static int on_new_buddy_added_or_deleted_from_group_chat(appdata_s *app, bundle *const rec_msg, char *rec_key_val)
1065 {
1066         int result = SVC_RES_FAIL;
1067         char* buddy_id_str = NULL;
1068         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1069         int buddy_id = atoi(buddy_id_str);
1070
1071         Eina_Bool is_success = EINA_FALSE;
1072         char* is_success_val = NULL;
1073         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1074         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1075                 is_success = EINA_TRUE;
1076         } else {
1077                 is_success = EINA_FALSE;
1078         }
1079
1080         if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1081                         && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1082                 hide_loading_popup(app);
1083                 if (strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) {
1084                         show_toast(app, "Failed to add new buddy to chat.");
1085                 } else if (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0) {
1086                         show_toast(app, "Failed to delete buddy to chat.");
1087                 }
1088         }
1089         hide_loading_popup(app);
1090         return result;
1091 }
1092
1093 static int on_group_chat_name_changed(appdata_s *app, bundle *const rec_msg)
1094 {
1095         int result = SVC_RES_FAIL;
1096         char* buddy_id_str = NULL;
1097         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1098         int buddy_id = atoi(buddy_id_str);
1099
1100         Eina_Bool is_success = EINA_FALSE;
1101         char* is_success_val = NULL;
1102         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1103         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1104                 is_success = EINA_TRUE;
1105         } else {
1106                 is_success = EINA_FALSE;
1107         }
1108
1109         if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1110                         && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1111
1112                 hide_loading_popup(app);
1113                 show_toast(app, "Failed to rename the chat title.");
1114         }
1115         hide_loading_popup(app);
1116
1117         return result;
1118 }
1119
1120 static int on_server_connection_status_changed(appdata_s *app, bundle *const rec_msg)
1121 {
1122         int result = SVC_RES_FAIL;
1123         Eina_Bool is_success = EINA_FALSE;
1124         char* is_success_val = NULL;
1125         result = bundle_get_str(rec_msg, "connection_status", &is_success_val);
1126         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1127                 is_success = EINA_TRUE;
1128         } else {
1129                 is_success = EINA_FALSE;
1130         }
1131
1132         app->is_server_ready = is_success;
1133         return result;
1134 }
1135
1136 static int on_buddy_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1137 {
1138         int result = SVC_RES_FAIL;
1139         char* user_id_str = NULL;
1140         result = bundle_get_str(rec_msg, "user_id", &user_id_str);
1141         int user_id = atoi(user_id_str);
1142         char* pic_file_path = NULL;
1143         result = bundle_get_str(rec_msg, "file_path", &pic_file_path);
1144         if (result == SVC_RES_FAIL || !pic_file_path || strlen(pic_file_path) <= 0 || access(pic_file_path, F_OK) == -1) {
1145                 //pic_file_path = (char *)ui_utils_get_resource(DEFAULT_PROFILE_PIC);
1146                 return result;
1147         }
1148
1149         // update profile pic if buddy list view is present.
1150         if (user_id == app->user_id.id) {
1151                 // registerd user.
1152                 // to be handled
1153         } else {
1154                 // update buddy list in appdata.
1155
1156                 Eina_List *l = NULL;
1157                 user_data_with_pic_s *uitem = NULL;
1158                 EINA_LIST_FOREACH(app->buddy_list, l, uitem) {
1159                         if (!uitem)
1160                                 continue;
1161                         if (uitem->use_data->user_id.id == user_id) {
1162                                 if (uitem->use_data->photo_path) {
1163                                         free(uitem->use_data->photo_path);
1164                                         uitem->use_data->photo_path = NULL;
1165                                 }
1166                                 uitem->use_data->photo_path = strdup(pic_file_path);
1167                                 if (uitem->contact_icon) {
1168                                         elm_image_file_set(uitem->contact_icon, pic_file_path, NULL);
1169                                         evas_object_color_set(uitem->contact_icon, 0, 0, 0, 0);
1170                                 }
1171                                 break;
1172                         }
1173                 }
1174
1175
1176                 l = NULL;
1177                 peer_with_pic_s *pitem = NULL;
1178                 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1179                         if (!pitem)
1180                                 continue;
1181
1182                         if (pitem->use_data->peer_id == user_id) {
1183                                 if (pitem->use_data->photo_path) {
1184                                         free(pitem->use_data->photo_path);
1185                                         pitem->use_data->photo_path = NULL;
1186                                 }
1187                                 pitem->use_data->photo_path = strdup(pic_file_path);
1188                                 if (pitem->contact_icon) {
1189                                         elm_image_file_set(pitem->contact_icon, pic_file_path, NULL);
1190                                         evas_object_color_set(pitem->contact_icon, 0, 0, 0, 0);
1191                                 }
1192                                 break;
1193                         }
1194                 }
1195
1196
1197                 l = NULL;
1198                 tg_main_list_item_s *item = NULL;
1199                 EINA_LIST_FOREACH(app->main_list, l, item) {
1200                         if (!item)
1201                                 continue;
1202
1203                         if (item->peer_id == user_id) {
1204                                 if (item->profile_pic_path) {
1205                                         free(item->profile_pic_path);
1206                                         item->profile_pic_path = NULL;
1207                                 }
1208                                 item->profile_pic_path = strdup(pic_file_path);
1209                                 if (item->profile_pic) {
1210                                         elm_image_file_set(item->profile_pic, pic_file_path, NULL);
1211                                         evas_object_color_set(item->profile_pic, 0, 0, 0, 0);
1212                                 }
1213                                 break;
1214                         }
1215                 }
1216                 if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data
1217                                 && app->peer_in_cahtting_data->use_data->peer_id == user_id) {
1218                         on_chat_profile_pic_changed(app, pic_file_path);
1219                 }
1220                 if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data
1221                                 && app->peer_in_cahtting_data->use_data->peer_id == user_id) {
1222                         //on_chat_profile_pic_changed(app, pic_file_path);
1223                         //show_toast(app, "Chat profile picture updated successfully.");
1224                 }
1225         }
1226         return result;
1227 }
1228
1229
1230 static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg, Eina_Bool with_date)
1231 {
1232         int result = SVC_RES_FAIL;
1233
1234 #if 0
1235         if (!app->peer_in_cahtting_data || !app->peer_in_cahtting_data->use_data) {
1236                 LOGE("Wrong status, previous chatting data are freed!!!");
1237                 return result;
1238         }
1239 #endif
1240
1241         char* from_id_str = NULL;
1242         result = bundle_get_str(rec_msg, "from_id", &from_id_str);
1243         int from_id = atoi(from_id_str);
1244
1245         char* to_id_str = NULL;
1246         result = bundle_get_str(rec_msg, "to_id", &to_id_str);
1247         int to_id = atoi(to_id_str);
1248
1249         char* msg_id_str = NULL;
1250         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1251         long long message_id = atoll(msg_id_str);
1252
1253         char* type_of_chat_str = NULL;
1254         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1255         int type_of_chat = atoi(type_of_chat_str);
1256
1257         if (from_id == app->user_id.id) {
1258                 // sent by the user. May be from another device.
1259         }
1260
1261         //load message from the received info.
1262         // get phone number using buddy id.
1263
1264         Eina_Bool is_user_present_in_main_list = EINA_FALSE;
1265         int id_to_check;
1266         if (type_of_chat == TGL_PEER_USER) {
1267                 id_to_check = from_id;
1268         } else {
1269                 id_to_check = to_id;
1270         }
1271
1272         if (type_of_chat == TGL_PEER_USER) {
1273                 if ((app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state ==  TG_SET_USER_INFO_STATE) &&
1274                                 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == from_id) {
1275                         if (with_date) {
1276                                 char* date_id_str = NULL;
1277                                 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1278                                 int date_id = atoi(date_id_str);
1279                                 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1280                                 wait_for(1);
1281                         }
1282                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1283
1284                         return result;
1285                 }
1286
1287         } else if (type_of_chat == TGL_PEER_CHAT) {
1288                 if ((app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state ==  TG_SET_CHAT_INFO_STATE) &&
1289                                 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
1290                         char* tablename = get_table_name_from_number(to_id);
1291                         tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1292                         if (msg) {
1293                                 // To be handled.
1294                                 if (with_date) {
1295                                         char* date_id_str = NULL;
1296                                         result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1297                                         int date_id = atoi(date_id_str);
1298                                         on_text_message_received_from_buddy(app, date_id, type_of_chat);
1299                                         wait_for(1);
1300                                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1301                                 } else {
1302                                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1303                                 }
1304                                 if (msg->message) {
1305                                         free(msg->message);
1306                                         msg->message = NULL;
1307                                 }
1308
1309                                 if (msg->media_id) {
1310                                         free(msg->media_id);
1311                                         msg->media_id = NULL;
1312                                 }
1313                         }
1314                         free(msg);
1315                         free(tablename);
1316                         return result;
1317                 }
1318         }
1319
1320         Eina_List *l = NULL;
1321         tg_main_list_item_s *sel_item = NULL;
1322         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1323
1324                 if (sel_item->peer_id != id_to_check) {
1325                         continue;
1326                 }
1327
1328                 // update last message
1329                 char* tablename = get_table_name_from_number(sel_item->peer_id);
1330                 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1331
1332                 if (msg) {
1333                         if (msg->service == 2) {
1334                                 if (msg->message) {
1335                                         free(msg->message);
1336                                         msg->message = NULL;
1337                                 }
1338
1339                                 if (msg->media_id) {
1340                                         free(msg->media_id);
1341                                         msg->media_id = NULL;
1342                                 }
1343                                 free(msg);
1344                                 return SVC_RES_OK;
1345                         }
1346
1347                         if (msg->service == 1) {
1348                                 if (msg->message && strlen(msg->message) > 0) {
1349                                         sel_item->last_message = strdup(msg->message);
1350                                 } else {
1351                                         sel_item->last_message = strdup(" ");
1352                                 }
1353                         } else {
1354
1355                                 int media_type = msg->media_type;
1356                                 if (media_type == tgl_message_media_none) {
1357                                         if (msg->message && strlen(msg->message) > 0) {
1358                                                 sel_item->last_message = strdup(msg->message);
1359                                         } else {
1360                                                 sel_item->last_message = strdup(" ");
1361                                         }
1362                                 } else if (media_type == tgl_message_media_photo) {
1363                                         sel_item->last_message = strdup("Image");
1364                                 } else if (media_type == tgl_message_media_document) {
1365
1366 #if 0
1367                                         if (msg->message && strlen(msg->message) > 0) {
1368                                                 sel_item->last_message = strdup(msg->message);
1369                                         } else {
1370                                                 sel_item->last_message = strdup("Document");
1371                                         }
1372 #else
1373                                         tgl_media_s *media_msg = NULL;
1374                                         media_msg = get_media_details_from_db(atoll(msg->media_id));
1375                                         sel_item->last_message = strdup(media_msg->doc_type);
1376                                         free_media_details(media_msg);
1377 #endif
1378                                 } else if (media_type == tgl_message_media_geo) {
1379                                         sel_item->last_message = strdup("Geo location");
1380                                 } else if (media_type == tgl_message_media_contact) {
1381                                         sel_item->last_message = strdup("Contact");
1382                                 } else if (media_type == tgl_message_media_unsupported) {
1383                                         sel_item->last_message = strdup(" ");
1384                                 } else if (media_type == tgl_message_media_photo_encr) {
1385                                         sel_item->last_message = strdup("Image encrypted");
1386                                 } else if (media_type == tgl_message_media_document_encr) {
1387                                         sel_item->last_message = strdup("Document encrypted");
1388                                 } else  {
1389                                         sel_item->last_message = strdup(" ");
1390                                 }
1391                         }
1392                         sel_item->last_msg_id = msg->msg_id;
1393                         sel_item->last_msg_type = msg->media_type;
1394                         sel_item->is_out_msg = msg->out;
1395                         sel_item->last_msg_status = msg->msg_state;
1396                         sel_item->last_seen_time = msg->date;
1397                         sel_item->number_of_unread_msgs = get_unread_message_count(tablename);
1398
1399                         if (sel_item->profile_pic) {
1400
1401                         }
1402
1403                         if (sel_item->user_name_lbl) {
1404
1405                         }
1406
1407                         if (sel_item->status_lbl) {
1408                                 char* org_msg = NULL;
1409                                 if (sel_item->last_message) {
1410                                         org_msg = sel_item->last_message;
1411                                 } else {
1412                                         org_msg = "";
1413                                 }
1414                                 elm_object_text_set(sel_item->status_lbl, "");
1415
1416                                 char status_buf[126] = {'\0'};
1417                                 const char *color;
1418
1419                                 if (msg->service == 1 || msg->service == 2) {
1420                                         sel_item->last_msg_service = 1;
1421                                         color = "#158CB0";
1422                                 } else {
1423                                         sel_item->last_msg_service = 0;
1424                                         color = "#A4A4A4";
1425                                 }
1426
1427                                 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=%s align=left><font_size=26>%.*s</font_size></font>", color, 40, org_msg);
1428                                 elm_object_text_set(sel_item->status_lbl, status_buf);
1429                         }
1430
1431                         if (sel_item->date_lbl) {
1432                                 Eina_Bool is_today = compare_date_with_current_date(sel_item->last_seen_time);
1433
1434                                 if (is_today) {
1435                                         elm_object_text_set(sel_item->date_lbl, "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>Today</font_size></font>");
1436                                 } else {
1437                                         struct tm lt;
1438                                         char res[256];
1439                                         char time_str[256] = { 0, };
1440                                         time_t t;
1441
1442                                         t = sel_item->last_seen_time;
1443                                         (void)localtime_r(&t, &lt);
1444                                         if (strftime(res, sizeof(res), "%d/%b/%Y", &lt) == 0) {
1445                                                 strcpy(res, "unknown");
1446                                         }
1447
1448                                         snprintf(time_str, sizeof(time_str), "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>%s</font_size></font>", res);
1449                                         elm_object_text_set(sel_item->date_lbl, time_str);
1450                                 }
1451                         }
1452                         if (sel_item->msg_status_lbl) {
1453                                 Evas_Object* temp_lbl = elm_object_part_content_get(sel_item->msg_status_lbl, "swallow.status");
1454                                 if (temp_lbl) {
1455                                         evas_object_del(temp_lbl);
1456                                 }
1457
1458                                 Evas_Object* num_lbl = elm_label_add(app->nf);
1459                                 elm_object_style_set(num_lbl, "transparent");
1460                                 char unread_msgs[256];
1461                                 sprintf(unread_msgs, "<font=Tizen:style=Bold color=#ffffff align=center><font_size=25>%d</font_size></font>", sel_item->number_of_unread_msgs);
1462                                 elm_object_text_set(num_lbl, unread_msgs);
1463                                 evas_object_size_hint_weight_set(num_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1464                                 evas_object_size_hint_align_set(num_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
1465                                 evas_object_show(num_lbl);
1466
1467                                 char edj_path[PATH_MAX] = {0, };
1468                                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
1469                                 Evas_Object* msg_status = elm_layout_add(app->nf);
1470                                 elm_layout_file_set(msg_status, edj_path, "circle_item");
1471                                 evas_object_size_hint_weight_set(msg_status, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1472                                 evas_object_size_hint_align_set(msg_status, EVAS_HINT_FILL, EVAS_HINT_FILL);
1473                                 evas_object_show(msg_status);
1474                                 elm_object_part_content_set(msg_status, "content", num_lbl);
1475
1476                                 elm_object_part_content_set(sel_item->msg_status_lbl, "swallow.status", msg_status);
1477
1478                         }
1479                         if (msg->message) {
1480                                 free(msg->message);
1481                                 msg->message = NULL;
1482                         }
1483
1484                         if (msg->media_id) {
1485                                 free(msg->media_id);
1486                                 msg->media_id = NULL;
1487                         }
1488                         free(msg);
1489                 }
1490
1491                 free(tablename);
1492                 is_user_present_in_main_list = EINA_TRUE;
1493
1494                 app->main_list = eina_list_remove(app->main_list, sel_item);
1495                 app->main_list = eina_list_prepend(app->main_list, sel_item);
1496                 refresh_main_list_view(app, EINA_FALSE);
1497                 break;
1498         }
1499
1500         if (!is_user_present_in_main_list) {
1501                  peer_with_pic_s *peer_item = get_peer_info(id_to_check);
1502                  if (peer_item) {
1503                          tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
1504                          if (latest_item) {
1505                                  app->main_list = eina_list_prepend(app->main_list, latest_item);
1506                                  refresh_main_list_view(app, EINA_TRUE);
1507                          }
1508                  }
1509         }
1510
1511         if (app->s_app_visible_state == APP_STATE_IN_BACKGROUND || app->current_app_state !=  TG_USER_MAIN_VIEW_STATE) {
1512                 // show notification
1513                 char *icon_path = (char *)ui_utils_get_resource(DEFAULT_TELEGRAM_ICON);
1514                 char *title = "Telegram";
1515                 char content[512];
1516
1517                 int unread_msg_cnt = get_number_of_unread_messages();
1518
1519                 if (unread_msg_cnt <= 0) {
1520                         badge_set_count(TELEGRAM_APP_ID, 0);
1521                         return result;
1522                 }
1523
1524                 sprintf(content, "%d new messages received.", unread_msg_cnt);
1525
1526                 char *sound_track = NULL;
1527                 char *app_id = TELEGRAM_APP_ID;
1528                 tg_notification_create(app, icon_path, title, content, sound_track, app_id);
1529                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
1530                 if (BADGE_ERROR_NONE != err) {
1531
1532                 }
1533         }
1534
1535         return result;
1536 }
1537
1538 static int on_message_with_date_received_from_buddy(appdata_s *app, bundle *const rec_msg)
1539 {
1540         return on_message_received_from_buddy(app, rec_msg, EINA_TRUE);
1541 }
1542
1543 static int on_new_contact_added(appdata_s *app, bundle *const rec_msg)
1544 {
1545         int result = SVC_RES_FAIL;
1546         char* buddy_id_str = NULL;
1547         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1548         int buddy_id = atoi(buddy_id_str);
1549
1550         Eina_Bool is_success = EINA_FALSE;
1551         char* is_success_val = NULL;
1552         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1553         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1554                 is_success = EINA_TRUE;
1555         } else {
1556                 is_success = EINA_FALSE;
1557         }
1558         if (is_success) {
1559                 load_buddy_list_data(app);
1560                 load_peer_data(app);
1561         } else {
1562                 //show_toast(app, "Failed to add contact. Please try after sometime.");
1563         }
1564         hide_loading_popup(app);
1565         if (app->current_app_state == TG_ADD_CONTACT_STATE) {
1566                 on_new_contact_added_response_received(app, buddy_id, is_success);
1567         }
1568         return result;
1569 }
1570
1571 static int on_buddy_re_added_to_chat(appdata_s *app, bundle *const rec_msg)
1572 {
1573         int result = SVC_RES_FAIL;
1574         char* buddy_id_str = NULL;
1575         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1576         int buddy_id = atoi(buddy_id_str);
1577
1578         Eina_Bool is_success = EINA_FALSE;
1579         char* is_success_val = NULL;
1580         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1581         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1582                 is_success = EINA_TRUE;
1583         } else {
1584                 is_success = EINA_FALSE;
1585         }
1586
1587         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1588                 on_user_added_response_received(app, buddy_id, is_success);
1589         }
1590         hide_loading_popup(app);
1591         return result;
1592 }
1593
1594 static int on_buddy_deleted_from_friends_list(appdata_s *ad, bundle *const rec_msg)
1595 {
1596         int result = SVC_RES_FAIL;
1597         char* buddy_id_str = NULL;
1598         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1599         int buddy_id = atoi(buddy_id_str);
1600
1601         Eina_Bool is_success = EINA_FALSE;
1602         char* is_success_val = NULL;
1603         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1604         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1605                 is_success = EINA_TRUE;
1606         } else {
1607                 is_success = EINA_FALSE;
1608         }
1609
1610         if (is_success && ad->current_app_state == TG_SET_USER_INFO_STATE) {
1611                 //on_user_deleted_response_received(app, buddy_id, is_success);
1612                 show_loading_popup(ad);
1613                 elm_naviframe_item_pop(ad->nf);
1614                 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
1615                 ad->peer_in_cahtting_data = NULL;
1616                 ad->main_item_in_cahtting_data = NULL;
1617                 ad->buddy_in_cahtting_data = NULL;
1618                 elm_naviframe_item_pop(ad->nf);
1619                 load_registered_user_data(ad);
1620                 load_buddy_list_data(ad);
1621                 load_unknown_buddy_list_data(ad);
1622                 load_peer_data(ad);
1623                 load_main_list_data(ad);
1624
1625                 if (buddy_id > 0) {
1626                         Eina_List *l = NULL;
1627                         tg_main_list_item_s* main_list_item = NULL;
1628                         EINA_LIST_FOREACH(ad->main_list, l, main_list_item) {
1629                                 if (main_list_item && main_list_item->peer_id == buddy_id) {
1630                                         ad->main_list = eina_list_remove(ad->main_list, main_list_item);
1631                                         ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
1632                                         break;
1633                                 }
1634                         }
1635                 }
1636                 ecore_timer_add(1, on_load_main_view_requested, ad);
1637         } else {
1638                 hide_loading_popup(ad);
1639         }
1640         return result;
1641 }
1642
1643 static int on_buddy_blocked(appdata_s *app, bundle *const rec_msg)
1644 {
1645         int result = SVC_RES_FAIL;
1646
1647         char* buddy_id_str = NULL;
1648         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1649         int buddy_id = atoi(buddy_id_str);
1650
1651         Eina_Bool is_success = EINA_FALSE;
1652         char* is_success_val = NULL;
1653         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1654         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1655                 is_success = EINA_TRUE;
1656         } else {
1657                 is_success = EINA_FALSE;
1658         }
1659
1660         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1661                 on_user_block_response_received(app, buddy_id, is_success);
1662         }
1663         hide_loading_popup(app);
1664
1665         return result;
1666 }
1667
1668 static int on_buddy_unblocked(appdata_s *app, bundle *const rec_msg)
1669 {
1670         int result = SVC_RES_FAIL;
1671
1672         char* buddy_id_str = NULL;
1673         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1674         int buddy_id = atoi(buddy_id_str);
1675
1676         Eina_Bool is_success = EINA_FALSE;
1677         char* is_success_val = NULL;
1678         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1679         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1680                 is_success = EINA_TRUE;
1681         } else {
1682                 is_success = EINA_FALSE;
1683         }
1684
1685         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1686                 on_user_unblock_response_received(app, buddy_id, is_success);
1687         }
1688         hide_loading_popup(app);
1689
1690         return result;
1691 }
1692
1693 static int on_delete_selected_group_chats(appdata_s *ad, bundle *const rec_msg)
1694 {
1695         int result = SVC_RES_OK;
1696 #if 0
1697         if (app->current_app_state == TG_USER_MAIN_VIEW_SELECTION_STATE) {
1698                 elm_naviframe_item_pop(app->nf);
1699                 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1700                 show_floating_button(app);
1701                 load_buddy_list_data(app);
1702                 load_unknown_buddy_list_data(app);
1703                 load_peer_data(app);
1704                 load_main_list_data(app);
1705                 refresh_main_list_view(app, EINA_FALSE);
1706         }
1707         hide_loading_popup(app);
1708 #else
1709         load_registered_user_data(ad);
1710         load_buddy_list_data(ad);
1711         load_unknown_buddy_list_data(ad);
1712         load_peer_data(ad);
1713         load_main_list_data(ad);
1714         if (ad->main_item) {
1715                 elm_naviframe_item_pop_to(ad->main_item);
1716         }
1717         ecore_timer_add(1, on_load_main_view_requested, ad);
1718 #endif
1719         return result;
1720 }
1721
1722 static int on_group_chat_deleted_response(appdata_s *app, bundle *const rec_msg)
1723 {
1724         int result = SVC_RES_FAIL;
1725
1726         char* chat_id_str = NULL;
1727         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1728         int chat_id = atoi(chat_id_str);
1729
1730         Eina_Bool is_success = EINA_FALSE;
1731         char* is_success_val = NULL;
1732         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1733         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1734                 is_success = EINA_TRUE;
1735         } else {
1736                 is_success = EINA_FALSE;
1737         }
1738
1739         if (is_success) {
1740                 Eina_List *l = NULL;
1741                 peer_with_pic_s *pitem = NULL;
1742                 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1743                         if (!pitem)
1744                                 continue;
1745
1746                         if (pitem->use_data->peer_id == chat_id) {
1747                                 app->peer_list = eina_list_remove(app->peer_list, pitem);
1748                                 break;
1749                         }
1750                 }
1751
1752                 l = NULL;
1753                 tg_main_list_item_s *item = NULL;
1754                 EINA_LIST_FOREACH(app->main_list, l, item) {
1755                         if (item->peer_id == chat_id) {
1756                                 app->main_list = eina_list_remove(app->main_list, item);
1757                                 break;
1758                         }
1759                 }
1760
1761                 refresh_main_list_view(app, EINA_FALSE);
1762
1763                 if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1764                                 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1765
1766                         app->peer_in_cahtting_data = NULL;
1767                         app->main_item_in_cahtting_data = NULL;
1768
1769                         elm_naviframe_item_pop(app->nf);
1770                         app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1771                         show_floating_button(app);
1772                 }
1773
1774                 if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1775                                                         && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1776                         on_group_chat_deleted(app, chat_id);
1777                 }
1778
1779         } else {
1780                 show_toast(app, "Failed to delete chat");
1781         }
1782         hide_loading_popup(app);
1783
1784         return result;
1785 }
1786
1787 static int on_message_sent_to_buddy(appdata_s *app, bundle *const rec_msg)
1788 {
1789         int result = SVC_RES_FAIL;
1790
1791         char* buddy_id_str = NULL;
1792         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1793         int buddy_id = atoi(buddy_id_str);
1794
1795         char* msg_id_str = NULL;
1796         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1797         int message_id = atoi(msg_id_str);
1798
1799         char* table_name = NULL;
1800         result = bundle_get_str(rec_msg, "table_name", &table_name);
1801
1802
1803         Eina_Bool is_success = EINA_FALSE;
1804         char* is_success_val = NULL;
1805         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1806         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1807                 is_success = EINA_TRUE;
1808         } else {
1809                 is_success = EINA_FALSE;
1810                 show_toast(app, "message sent failed");
1811         }
1812
1813         char* type_of_chat_str = NULL;
1814         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1815         int type_of_chat = atoi(type_of_chat_str);
1816
1817         tg_message_s* msg = get_message_from_message_table(message_id, table_name);
1818
1819         if (msg) {
1820                 if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1821                         // update message to sent state
1822                         on_text_message_state_changed(app, msg, type_of_chat);
1823                 }
1824
1825                 if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
1826                         Eina_List *l = NULL;
1827                         tg_main_list_item_s *sel_item = NULL;
1828                         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1829                                 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
1830                                         if (sel_item->msg_status_lbl) {
1831                                                 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_SENT_ICON), NULL);
1832                                         }
1833
1834                                         break;
1835                                 }
1836                         }
1837                 }
1838                 if (msg->message) {
1839                         free(msg->message);
1840                         msg->message = NULL;
1841                 }
1842
1843                 if (msg->media_id) {
1844                         free(msg->media_id);
1845                         msg->media_id = NULL;
1846                 }
1847                 free(msg);
1848         }
1849         return result;
1850 }
1851
1852 static int on_self_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1853 {
1854         int result = SVC_RES_FAIL;
1855         Eina_Bool is_success = EINA_FALSE;
1856         char *is_success_val = NULL;
1857         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1858         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1859                 is_success = EINA_TRUE;
1860                 show_toast(app, "profile picture updated successfully.");
1861         } else {
1862                 is_success = EINA_FALSE;
1863                 show_toast(app, "profile picture not updated.");
1864         }
1865
1866         if (is_success) {
1867                 char *file_path = NULL;
1868                 result = bundle_get_str(rec_msg, "file_path", &file_path);
1869
1870                 if (app->current_user_data->photo_path) {
1871                         free(app->current_user_data->photo_path);
1872                         app->current_user_data->photo_path = NULL;
1873                 }
1874
1875                 app->current_user_data->photo_path = strdup(file_path);
1876                 Evas_Object* profile_pic = evas_object_data_get(app->nf, "main_user_profile_pic");
1877                 if (profile_pic) {
1878                         elm_image_file_set(profile_pic, file_path, NULL);
1879                 }
1880
1881                 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE) {
1882                         Evas_Object* set_pro_pic = evas_object_data_get(app->nf, "settings_user_profile_pic");
1883                         if (set_pro_pic) {
1884                                 elm_image_file_set(set_pro_pic, file_path, NULL);
1885                         }
1886                 }
1887         }
1888         hide_loading_popup(app);
1889         return result;
1890 }
1891
1892 static int on_self_profile_name_updated(appdata_s *app, bundle *const rec_msg)
1893 {
1894         int result = SVC_RES_FAIL;
1895         Eina_Bool is_success = EINA_FALSE;
1896         char *is_success_val = NULL;
1897         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1898         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1899                 is_success = EINA_TRUE;
1900                 show_toast(app, "Username updated successfully.");
1901         } else {
1902                 is_success = EINA_FALSE;
1903                 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1904         }
1905
1906         if (is_success) {
1907
1908                 char *first_name = NULL;
1909                 result = bundle_get_str(rec_msg, "first_name", &first_name);
1910                 if (!first_name) {
1911                         first_name = "";
1912                 }
1913
1914                 char *last_name = NULL;
1915                 result = bundle_get_str(rec_msg, "last_name", &last_name);
1916                 if (!last_name) {
1917                         last_name = "";
1918                 }
1919
1920                 if (app->current_user_data->first_name) {
1921                         free(app->current_user_data->first_name);
1922                         app->current_user_data->first_name = NULL;
1923                 }
1924
1925                 if (app->current_user_data->last_name) {
1926                         free(app->current_user_data->last_name);
1927                         app->current_user_data->last_name = NULL;
1928                 }
1929
1930                 if (first_name) {
1931                         app->current_user_data->first_name = strdup(first_name);
1932                 }
1933
1934                 if (last_name) {
1935                         app->current_user_data->last_name = strdup(last_name);
1936                 }
1937
1938                 if (app->current_app_state == TG_SETTINGS_EDIT_NAME_STATE) {
1939                         elm_naviframe_item_pop(app->nf);
1940                         app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1941                         refresh_settings_screen(app);
1942                 }
1943
1944         } else {
1945
1946         }
1947         hide_loading_popup(app);
1948         return result;
1949 }
1950
1951 static int on_self_username_updated(appdata_s *app, bundle *const rec_msg)
1952 {
1953         int result = SVC_RES_FAIL;
1954         Eina_Bool is_success = EINA_FALSE;
1955         char *is_success_val = NULL;
1956         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1957         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1958                 is_success = EINA_TRUE;
1959                 show_toast(app, "Username updated successfully.");
1960         } else {
1961                 is_success = EINA_FALSE;
1962                 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1963         }
1964
1965         if (is_success) {
1966                 char *username = NULL;
1967                 result = bundle_get_str(rec_msg, "username", &username);
1968
1969                 if (app->current_user_data->username) {
1970                         free(app->current_user_data->username);
1971                         app->current_user_data->username = NULL;
1972                 }
1973
1974                 app->current_user_data->username = strdup(username);
1975
1976
1977                 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE || app->current_app_state == TG_SET_USERNAME_STATE) {
1978                         Evas_Object *name_lbl = evas_object_data_get(app->nf, "settings_user_name_label");
1979                         if (name_lbl) {
1980                                 char buf[512] = {'\0'};
1981                                 snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", app->current_user_data->username);
1982                                 elm_object_text_set(name_lbl, buf);
1983                         }
1984                 }
1985                 if (app->current_app_state == TG_SET_USERNAME_STATE) {
1986                         elm_naviframe_item_pop(app->nf);
1987                         app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1988                 }
1989         }
1990         hide_loading_popup(app);
1991
1992         return result;
1993 }
1994
1995 static int on_message_read_by_buddy(appdata_s *app, bundle *const rec_msg)
1996 {
1997         int result = SVC_RES_FAIL;
1998         char* buddy_id_str = NULL;
1999         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2000         int buddy_id = atoi(buddy_id_str);
2001
2002         char* msg_id_str = NULL;
2003         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
2004         int message_id = atoi(msg_id_str);
2005
2006         char* table_name = NULL;
2007         result = bundle_get_str(rec_msg, "table_name", &table_name);
2008
2009         char* phone_number = NULL;
2010         result = bundle_get_str(rec_msg, "phone_number", &phone_number);
2011
2012         char* type_of_chat_str = NULL;
2013         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
2014         int type_of_chat = atoi(type_of_chat_str);
2015
2016         tg_message_s* msg = get_message_from_message_table(message_id, table_name);
2017
2018         if (msg) {
2019                 if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
2020                         // update message to sent state
2021                         on_text_message_state_changed(app, msg, type_of_chat);
2022                 }
2023
2024                 if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2025
2026                         Eina_List *l = NULL;
2027                         tg_main_list_item_s *sel_item = NULL;
2028                         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2029
2030                                 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
2031
2032                                         if (sel_item->msg_status_lbl) {
2033                                                 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_READ_ICON), NULL);
2034                                         }
2035
2036                                         break;
2037                                 }
2038                         }
2039                 }
2040
2041                 if (msg->message) {
2042                         free(msg->message);
2043                         msg->message = NULL;
2044                 }
2045
2046                 if (msg->media_id) {
2047                         free(msg->media_id);
2048                         msg->media_id = NULL;
2049                 }
2050                 free(msg);
2051         }
2052
2053         return result;
2054 }
2055
2056 static int on_media_message_download_completed(appdata_s *app, bundle *const rec_msg)
2057 {
2058         LOGE("Media download14");
2059         int result = SVC_RES_FAIL;
2060         char* buddy_id_str = NULL;
2061         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2062         int buddy_id = atoi(buddy_id_str);
2063
2064         char* to_id_str = NULL;
2065         result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2066         int to_id = atoi(to_id_str);
2067
2068         char* media_id_str = NULL;
2069         result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2070         long long media_id = atoll(media_id_str);
2071
2072         char* file_name = NULL;
2073         result = bundle_get_str(rec_msg, "file_name", &file_name);
2074
2075         //file_name == "failed_to_load"
2076
2077         if (file_name && strstr(file_name, "failed_to_load") != NULL) {
2078                 // download failed.
2079                 show_toast(app, "media download failed.");
2080         }
2081
2082         char* caption = NULL;
2083         result = bundle_get_str(rec_msg, "caption", &caption);
2084
2085
2086         if (file_name && app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2087                         && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2088                 // update media to sent state
2089                 LOGE("Media download15");
2090                 on_media_download_completed(app, buddy_id, media_id, file_name, caption);
2091         }
2092         LOGE("Media download16");
2093         return result;
2094 }
2095
2096 static int on_video_message_thumb_download_completed(appdata_s *app, bundle *const rec_msg)
2097 {
2098         int result = SVC_RES_FAIL;
2099         char* buddy_id_str = NULL;
2100         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2101         int buddy_id = atoi(buddy_id_str);
2102
2103         char* to_id_str = NULL;
2104         result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2105         int to_id = atoi(to_id_str);
2106
2107         char* media_id_str = NULL;
2108         result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2109         long long media_id = atoll(media_id_str);
2110
2111         char* file_name = NULL;
2112         result = bundle_get_str(rec_msg, "file_name", &file_name);
2113
2114         char* caption = NULL;
2115         result = bundle_get_str(rec_msg, "caption", &caption);
2116
2117         if (file_name && app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2118                         && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2119                 // update thumbnail icon
2120                 on_video_thumb_download_completed(app, buddy_id, media_id, file_name, caption);
2121         }
2122         return result;
2123 }
2124
2125 static int on_new_buddy_added_to_contacts(appdata_s *app, bundle *const rec_msg)
2126 {
2127         int result = SVC_RES_FAIL;
2128         char* buddy_id_str = NULL;
2129         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2130
2131         int buddy_id = atoi(buddy_id_str);
2132         peer_with_pic_s *peer_item = get_peer_info(buddy_id);
2133         tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2134         if (latest_item) {
2135                 app->main_list = eina_list_prepend(app->main_list, latest_item);
2136                 refresh_main_list_view(app, EINA_TRUE);
2137         }
2138         hide_loading_popup(app);
2139
2140         return result;
2141 }
2142
2143 static int on_new_group_added(appdata_s *app, bundle *const rec_msg)
2144 {
2145         int result = SVC_RES_FAIL;
2146
2147
2148 #if 0
2149         char* chat_id_str = NULL;
2150         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
2151
2152          int chat_id = atoi(chat_id_str);
2153          peer_with_pic_s *peer_item = get_peer_info(chat_id);
2154         if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2155                 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2156                 if (latest_item) {
2157                         app->main_list = eina_list_prepend(app->main_list, latest_item);
2158                         hide_loading_popup(app);
2159                         refresh_main_list_view(app, EINA_FALSE);
2160                 }
2161         } else {
2162
2163                 hide_loading_popup(app);
2164                 if (app->main_item) {
2165                         elm_naviframe_item_pop_to(app->main_item);
2166                         app->current_app_state = TG_USER_MAIN_VIEW_STATE;
2167                 }
2168
2169                 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2170                 if (latest_item) {
2171                         app->main_list = eina_list_prepend(app->main_list, latest_item);
2172                         refresh_main_list_view(app, EINA_FALSE);
2173                 }
2174
2175         }
2176         app->peer_list = eina_list_prepend(app->peer_list, peer_item);
2177 #else
2178         load_registered_user_data(app);
2179         load_buddy_list_data(app);
2180         load_unknown_buddy_list_data(app);
2181         load_peer_data(app);
2182         load_main_list_data(app);
2183
2184         if (app->main_item) {
2185                 elm_naviframe_item_pop_to(app->main_item);
2186         }
2187
2188         ecore_timer_add(1, on_load_main_view_requested, app);
2189 #endif
2190         return result;
2191 }
2192
2193 static int on_buddy_contact_updated(appdata_s *app, bundle *const rec_msg)
2194 {
2195         int result = SVC_RES_FAIL;
2196         char* buddy_id_str = NULL;
2197         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2198         //int buddy_id = atoi(buddy_id_str);
2199
2200         char* update_msg = NULL;
2201         result = bundle_get_str(rec_msg, "update_message", &update_msg);
2202
2203         // reload contact
2204
2205         return result;
2206 }
2207
2208 static int on_buddy_status_updated(appdata_s *app, bundle *const rec_msg)
2209 {
2210         int result = SVC_RES_FAIL;
2211         char* buddy_id_str = NULL;
2212         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2213         int buddy_id = atoi(buddy_id_str);
2214         // update to online or last seen, if current view is conversation.
2215
2216         // sandeep
2217         if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2218                 on_user_presence_state_changed(app, buddy_id);
2219         }
2220         return result;
2221 }
2222
2223 static int on_typing_status_updated(appdata_s *app, bundle *const rec_msg)
2224 {
2225         int result = SVC_RES_FAIL;
2226         char* buddy_id_str = NULL;
2227         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2228         int buddy_id = atoi(buddy_id_str);
2229
2230         char* buddy_name = NULL;
2231         result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2232
2233         const char *type_status_str;
2234
2235         result = bundle_get_str(rec_msg, "type_status", &type_status_str);
2236         enum tgl_typing_status type_status = atoi(type_status_str);
2237
2238         // update UI
2239         char* type_status_message = NULL;
2240
2241         switch (type_status) {
2242                 case tgl_typing_none:
2243                         type_status_message = "doing nothing";
2244                         break;
2245                 case tgl_typing_typing:
2246                         type_status_message = "typing...";
2247                         break;
2248                 case tgl_typing_cancel:
2249                         type_status_message = "deleting typed message";
2250                         break;
2251                 case tgl_typing_record_video:
2252                         type_status_message = "recording video";
2253                         break;
2254                 case tgl_typing_upload_video:
2255                         type_status_message = "uploading video";
2256                         break;
2257                 case tgl_typing_record_audio:
2258                         type_status_message = "recording audio";
2259                         break;
2260                 case tgl_typing_upload_audio:
2261                         type_status_message = "uploading audio";
2262                         break;
2263                 case tgl_typing_upload_photo:
2264                         type_status_message = "uploading photo";
2265                         break;
2266                 case tgl_typing_upload_document:
2267                         type_status_message = "uploading document";
2268                         break;
2269                 case tgl_typing_geo:
2270                         type_status_message = "choosing location";
2271                         break;
2272                 case tgl_typing_choose_contact:
2273                         type_status_message = "choosing contact";
2274                         break;
2275                 default:
2276                         type_status_message = "";
2277                         break;
2278         }
2279
2280         // update UI. may be contacts view or conversation view.
2281         if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2282                 Eina_List *l = NULL;
2283                 tg_main_list_item_s *sel_item = NULL;
2284                 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2285                         if (sel_item->peer_id != buddy_id) {
2286                                 continue;
2287                         }
2288                         // update last message
2289
2290                         if (sel_item->profile_pic) {
2291
2292                         }
2293
2294                         if (sel_item->user_name_lbl) {
2295
2296                         }
2297
2298                         if (sel_item->status_lbl) {
2299                                 char status_buf[512] = {'\0'};
2300                                 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=#158CB0 align=left><font_size=30>%.*s</font_size></font>", 25, type_status_message);
2301                                 elm_object_text_set(sel_item->status_lbl, status_buf);
2302                         }
2303
2304                         if (sel_item->date_lbl) {
2305
2306                         }
2307                         if (sel_item->msg_status_lbl) {
2308
2309                         }
2310                         break;
2311                 }
2312
2313         }
2314
2315         if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2316                 on_user_status_changed(app, type_status_message);
2317         }
2318
2319         return result;
2320 }
2321
2322
2323 static int on_user_status_updated(appdata_s *app, bundle *const rec_msg)
2324 {
2325         int result = SVC_RES_FAIL;
2326         char* buddy_id_str = NULL;
2327         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2328         int buddy_id = atoi(buddy_id_str);
2329
2330         char* buddy_name = NULL;
2331         result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2332
2333         if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2334                 Eina_List *l = NULL;
2335                 tg_main_list_item_s *sel_item = NULL;
2336                 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2337                         if (sel_item->peer_id != buddy_id) {
2338                                 continue;
2339                         }
2340                         // update last message
2341
2342                         if (sel_item->profile_pic) {
2343
2344                         }
2345
2346                         if (sel_item->user_name_lbl) {
2347
2348                         }
2349
2350                         if (sel_item->status_lbl) {
2351                                 char status_buf[126] = {'\0'};
2352                                 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Normal color=#A4A4A4 align=left><font_size=26>%.*s</font_size></font>", 29, sel_item->last_message ? sel_item->last_message : "");
2353                                 elm_object_text_set(sel_item->status_lbl, status_buf);
2354                         }
2355
2356                         if (sel_item->date_lbl) {
2357
2358                         }
2359                         if (sel_item->msg_status_lbl) {
2360
2361                         }
2362
2363                         break;
2364                 }
2365         }
2366         if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2367                 on_user_presence_state_changed(app, buddy_id);
2368         }
2369
2370         return result;
2371 }
2372
2373 Eina_Bool on_logout_completed(void *data)
2374 {
2375         appdata_s *ad = data;
2376         if (ad) {
2377                 elm_naviframe_item_pop(ad->nf);
2378                 ad->current_app_state = TG_REGISTRATION_STATE;
2379                 launch_init_screen(ad);
2380         }
2381     return ECORE_CALLBACK_CANCEL;
2382 }
2383
2384 Eina_Bool on_start_server_requested(void *data)
2385 {
2386         appdata_s *ad = data;
2387         if (ad) {
2388                 launch_tg_server(ad);
2389         }
2390     return ECORE_CALLBACK_CANCEL;
2391 }
2392
2393
2394 static int _on_service_client_msg_received_cb(void *data, bundle *const rec_msg)
2395 {
2396         int result = SVC_RES_FAIL;
2397         RETVM_IF(!data, result, "Data is NULL");
2398         appdata_s *app = data;
2399         char *rec_key_val = NULL;
2400
2401
2402         result = bundle_get_str(rec_msg, "command", &rec_key_val);
2403
2404         LOGI("MSG recv = %s", rec_key_val);
2405
2406         if (strcmp(rec_key_val, "server_not_initialized") == 0) {
2407                 hide_loading_popup(app);
2408                 return result;
2409         } else if (strcmp(rec_key_val, "server_restart_notification") == 0) {
2410                 hide_loading_popup(app);
2411                 if (!app->is_waiting_for_phone_num)
2412                         show_toast(app, "Network error. Please try again");
2413
2414                 ecore_timer_add(1, on_start_server_requested, app);
2415         } else if (strcmp(rec_key_val, "registration_done") == 0) {
2416
2417                 char* is_success_val = NULL;
2418                 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
2419                 if (app->login_timer)
2420                         ecore_timer_del(app->login_timer);
2421                 app->login_timer = NULL;
2422                 
2423                 if (strncmp("true", is_success_val, strlen("true")) == 0) {
2424                 //show_toast(app, is_success_val);
2425                         // Launch login view
2426                         //elm_naviframe_item_pop(app->nf);
2427                         launch_login_cb(data);
2428                 } else {
2429                         // error handling
2430                         show_toast(app, "Error: registration");
2431                 }
2432                 hide_loading_popup(app);
2433                 return result;
2434         } else if (strcmp(rec_key_val, "name_registration_request") == 0) {
2435
2436                 elm_naviframe_item_pop(app->nf);
2437                 elm_naviframe_item_pop(app->nf);
2438                 launch_first_registration_cb(app);
2439                 hide_loading_popup(app);
2440                 return result;
2441         } else if (strcmp(rec_key_val, "request_reg_code_again") == 0) {
2442                 show_toast(app, "Please enter a valid code.");
2443                 hide_loading_popup(app);
2444         } else if (strcmp(rec_key_val, "request_phone_num_again") == 0) {
2445                 show_toast(app, "Please enter a valid phone number.");
2446                 hide_loading_popup(app);
2447         } else if (strcmp(rec_key_val, "request_phone_new_num") == 0) {
2448                 app->is_waiting_for_phone_num = EINA_TRUE;
2449         } else if (strcmp(rec_key_val, "server_connection_failed") == 0) {
2450                 show_toast(app, "Server connection failed. please check network connection");
2451                 hide_loading_popup(app);
2452         } else if (strcmp(rec_key_val, "logout_completed") == 0) {
2453
2454                 free_app_data(app, EINA_TRUE);
2455                 hide_loading_popup(app);
2456                 app->phone_number = NULL;
2457                 app->buddy_list = NULL;
2458                 app->unknown_buddy_list = NULL;
2459                 app->known_buddy_list = NULL;
2460                 app->main_list = NULL;
2461                 app->peer_list = NULL;
2462                 app->search_peer_list = NULL;
2463                 app->is_first_time_registration = EINA_FALSE;
2464                 app->panel = NULL;
2465                 app->loaded_msg_list = NULL;
2466                 app->loading_popup = NULL;
2467                 app->current_user_data = NULL;
2468                 app->is_tg_initilized = EINA_FALSE;
2469                 app->chat_background = NULL;
2470                 app->msg_popup = NULL;
2471                 app->s_notififcation = NULL;
2472                 app->panel = NULL;
2473                 app->is_server_ready = EINA_FALSE;
2474                 app->is_waiting_for_phone_num = EINA_FALSE;
2475                 app->login_timer = NULL;
2476                 app->contact_list = NULL;
2477
2478                 char *chat_bg = NULL;
2479                 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
2480                 if (chat_bg) {
2481                         app->chat_background = strdup(chat_bg);
2482                 }
2483
2484                 if (!app->chat_background) {
2485                         app->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
2486                         preference_set_string(TG_CHAT_BG_PREFERENCE, app->chat_background);
2487                 }
2488
2489                 init_service(app);
2490
2491                 elm_naviframe_item_pop(app->nf);
2492                 //ecore_timer_add(1, on_logout_completed, app);
2493                 elm_naviframe_item_pop(app->nf);
2494                 app->current_app_state = TG_REGISTRATION_STATE;
2495                 launch_init_screen(app);
2496                 return result;
2497         } else if (strcmp(rec_key_val, "server_connection_status") == 0) {
2498                 return on_server_connection_status_changed(data, rec_msg);
2499         }
2500
2501         if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2502                 app->is_tg_initilized = EINA_TRUE;
2503         }
2504         if (!app->is_tg_initilized) {
2505                 return result;
2506         }
2507
2508         if (strcmp(rec_key_val, "contacts_load_done") == 0) {
2509
2510                 return on_contacts_loaded(data, rec_msg);
2511
2512         } else if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2513
2514                 return on_contacts_and_chats_loaded(data, rec_msg);
2515
2516         } else if (strcmp(rec_key_val, "response_group_chat_updated") == 0) {
2517
2518                 return on_group_chat_updated(data, rec_msg);
2519
2520         } else if (strcmp(rec_key_val, "group_chat_updated") == 0) {
2521
2522                 return on_group_chat_info_changed_response(data, rec_msg);
2523
2524         } else if ((strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) || (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0)) {
2525
2526                 return on_new_buddy_added_or_deleted_from_group_chat(data, rec_msg, rec_key_val);
2527
2528         } else if (strcmp(rec_key_val, "group_chat_rename_response") == 0) {
2529
2530                 return on_group_chat_name_changed(data, rec_msg);
2531
2532         } else if (strcmp(rec_key_val, "buddy_profile_pic_updated") == 0) {
2533
2534                 return on_buddy_profile_pic_updated(data, rec_msg);
2535
2536         } else if (strcmp(rec_key_val, "message_received") == 0) {
2537
2538                 return on_message_received_from_buddy(data, rec_msg, EINA_FALSE);
2539
2540         } else if (strcmp(rec_key_val, "message_received_with_date") == 0) {
2541
2542                 return on_message_with_date_received_from_buddy(data, rec_msg);
2543
2544         } else if (strcmp(rec_key_val, "buddy_readded") == 0) {
2545
2546                 return on_buddy_re_added_to_chat(data, rec_msg);
2547
2548         } else if (strcmp(rec_key_val, "new_contact_added") == 0) {
2549
2550                 return on_new_contact_added(data, rec_msg);
2551
2552         } else if (strcmp(rec_key_val, "buddy_deleted") == 0) {
2553
2554                 return on_buddy_deleted_from_friends_list(data, rec_msg);
2555
2556         } else if (strcmp(rec_key_val, "buddy_blocked") == 0) {
2557
2558                 return on_buddy_blocked(data, rec_msg);
2559
2560         } else if (strcmp(rec_key_val, "buddy_unblocked") == 0) {
2561
2562                 return on_buddy_unblocked(data, rec_msg);
2563
2564         } else if (strcmp(rec_key_val, "selected_group_chats_deleted_response") == 0) {
2565
2566                 return on_delete_selected_group_chats(data, rec_msg);
2567
2568         } else if (strcmp(rec_key_val, "group_chat_deleted_response") == 0) {
2569
2570                 return on_group_chat_deleted_response(data, rec_msg);
2571
2572         } else if (strcmp(rec_key_val, "message_sent_to_buddy") == 0) {
2573
2574                 return on_message_sent_to_buddy(data, rec_msg);
2575
2576         } else if (strcmp(rec_key_val, "self_profile_picture_updated") == 0) {
2577
2578                 return on_self_profile_pic_updated(data, rec_msg);
2579
2580         } else if (strcmp(rec_key_val, "self_profile_name_updated") == 0) {
2581
2582                 return on_self_profile_name_updated(data, rec_msg);
2583
2584         } else if (strcmp(rec_key_val, "self_username_updated") == 0) {
2585
2586                 return on_self_username_updated(data, rec_msg);
2587
2588         } else if (strcmp(rec_key_val, "message_read_by_buddy") == 0) {
2589
2590                 return on_message_read_by_buddy(data, rec_msg);
2591
2592         } else if (strcmp(rec_key_val, "media_download_completed") == 0) {
2593
2594                 return on_media_message_download_completed(data, rec_msg);
2595
2596         } else if (strcmp(rec_key_val, "video_thumb_download_completed") == 0) {
2597
2598                 return on_video_message_thumb_download_completed(data, rec_msg);
2599
2600         } else if (strcmp(rec_key_val, "add_contacts_request") == 0) {
2601                 // Not to be handled.
2602         } else if (strcmp(rec_key_val, "new_buddy_added") == 0) {
2603
2604                 return on_new_buddy_added_to_contacts(data, rec_msg);
2605
2606         } else if (strcmp(rec_key_val, "new_group_added") == 0) {
2607
2608                 return on_new_group_added(data, rec_msg);
2609
2610         } else if (strcmp(rec_key_val, "contact_updated") == 0) {
2611
2612                 return on_buddy_contact_updated(data, rec_msg);
2613
2614         } else if (strcmp(rec_key_val, "buddy_status_updated") == 0) {
2615
2616                 return on_buddy_status_updated(data, rec_msg);
2617
2618         } else if (strcmp(rec_key_val, "type_status_updated") == 0) {
2619
2620                 return on_typing_status_updated(data, rec_msg);
2621
2622         } else if (strcmp(rec_key_val, "user_status_updated") == 0) {
2623
2624                 return on_user_status_updated(data, rec_msg);
2625
2626         } else {
2627
2628         }
2629
2630         return result;
2631 }
2632
2633 int init_service(appdata_s *app)
2634 {
2635         int result = SVC_RES_FAIL;
2636         RETVM_IF(!app, result, "Application data is NULL");
2637
2638         app->service_client = service_client_create();
2639         RETVM_IF(!app->service_client, result, "Failed to create service client");
2640
2641         result = service_client_register_port(app->service_client, TELEGRAM_CLIENT_PORT_NAME);
2642         if (result != SVC_RES_OK) {
2643                 ERR("Failed to register service client port");
2644                 service_client_destroy(app->service_client);
2645                 app->service_client = NULL;
2646                 return result;
2647         }
2648
2649         result = _service_client_set_remote_data(app->service_client, TELEGRAM_SERVER_APP_NAME, TELEGRAM_SERVER_PORT_NAME);
2650
2651         result = service_client_register_msg_receive_callback(app->service_client, _on_service_client_msg_received_cb, app);
2652         if (result != SVC_RES_OK) {
2653                 ERR("Failed to register service client on message receive callback");
2654                 service_client_destroy(app->service_client);
2655                 app->service_client = NULL;
2656                 return result;
2657         }
2658         return result;
2659 }
2660
2661 void win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)
2662 {
2663         ui_app_exit();
2664 }
2665
2666
2667 tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
2668 {
2669         tg_main_list_item_s* main_list_item = NULL;
2670         if (!item)
2671                 return NULL;
2672
2673         tg_peer_info_s* peer_info = item->use_data;
2674         if (!peer_info)
2675                 return NULL;
2676
2677         // get message from message table.
2678
2679         char* tablename = get_table_name_from_number(peer_info->peer_id);
2680         tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2681
2682         if (msg) {
2683
2684                 if (msg->service) {
2685                         if (msg->message && strlen(msg->message) > 0) {
2686                                 item->last_message = strdup(msg->message);
2687                         } else {
2688                                 item->last_message = strdup(" ");
2689                         }
2690                 } else {
2691
2692                         int media_type = msg->media_type;
2693                         if (media_type == tgl_message_media_none) {
2694                                 if (msg->message && strlen(msg->message) > 0) {
2695                                         item->last_message = strdup(msg->message);
2696                                 } else {
2697                                         item->last_message = strdup(" ");
2698                                 }
2699                         } else if (media_type == tgl_message_media_photo) {
2700                                 item->last_message = strdup("Image");
2701                         } else if (media_type == tgl_message_media_document) {
2702                                 tgl_media_s *media_msg = NULL;
2703                                 media_msg = get_media_details_from_db(atoll(msg->media_id));
2704
2705                                 if (media_msg && media_msg->doc_type) {
2706                                         if (strlen(media_msg->doc_type) > 0) {
2707                                                 item->last_message = strdup(media_msg->doc_type);
2708                                         } else {
2709                                                 item->last_message = strdup("Document");
2710                                         }
2711                                         free_media_details(media_msg);
2712                                 } else {
2713                                         if (msg->message && strlen(msg->message) > 0) {
2714                                                 item->last_message = strdup(msg->message);
2715                                         } else {
2716                                                 item->last_message = strdup("Document");
2717                                         }
2718                                 }
2719
2720                         } else if (media_type == tgl_message_media_geo) {
2721                                 item->last_message = strdup("Geo location");
2722                         } else if (media_type == tgl_message_media_contact) {
2723                                 item->last_message = strdup("Contact");
2724                         } else if (media_type == tgl_message_media_unsupported) {
2725                                 item->last_message = strdup(" ");
2726                         } else if (media_type == tgl_message_media_photo_encr) {
2727                                 item->last_message = strdup("Image encrypted");
2728                         } else if (media_type == tgl_message_media_document_encr) {
2729                                 item->last_message = strdup("Document encrypted");
2730                         } else  {
2731                                 item->last_message = strdup(" ");
2732                         }
2733                 }
2734
2735                 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2736                 main_list_item->peer_id = peer_info->peer_id;
2737                 main_list_item->peer_type = peer_info->peer_type;
2738
2739                 set_peer_names(peer_info, main_list_item);
2740
2741                 main_list_item->last_seen_time = msg->date;
2742                 main_list_item->profile_pic = NULL;
2743                 main_list_item->last_msg_id = msg->msg_id;
2744                 main_list_item->last_message = strdup(item->last_message);
2745                 main_list_item->last_msg_type = msg->media_type;
2746                 main_list_item->is_out_msg = msg->out;
2747                 main_list_item->last_msg_status = msg->msg_state;
2748                 main_list_item->last_msg_service = msg->service;
2749                 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2750
2751                 if (peer_info->photo_path) {
2752                         main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2753                 } else {
2754                         main_list_item->profile_pic_path = NULL;
2755                 }
2756                 main_list_item->user_name_lbl = NULL;
2757                 main_list_item->status_lbl = NULL;
2758                 main_list_item->date_lbl = NULL;
2759                 main_list_item->msg_status_lbl = NULL;
2760                 main_list_item->main_item_layout = NULL;
2761
2762                 // delete message object
2763                 if (msg->message) {
2764                         free(msg->message);
2765                         msg->message = NULL;
2766                 }
2767
2768                 if (msg->media_id) {
2769                         free(msg->media_id);
2770                         msg->media_id = NULL;
2771                 }
2772
2773                 free(msg);
2774                 msg = NULL;
2775
2776         } else {
2777                 item->last_message = strdup(" ");
2778                 if (peer_info->peer_type == TGL_PEER_CHAT) {
2779                         main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2780                         main_list_item->peer_id = peer_info->peer_id;
2781                         main_list_item->peer_type = peer_info->peer_type;
2782
2783                         set_peer_names(peer_info, main_list_item);
2784
2785                         main_list_item->last_seen_time = peer_info->last_seen_time;
2786                         main_list_item->profile_pic = NULL;
2787                         main_list_item->last_message = strdup(item->last_message);
2788                         main_list_item->last_msg_type = -1;
2789                         main_list_item->last_msg_service = 0;
2790                         main_list_item->is_out_msg = -1;
2791                         main_list_item->last_msg_id = -1;
2792                         main_list_item->last_msg_status = -1;
2793                         main_list_item->number_of_unread_msgs = 0;
2794
2795                         if (peer_info->photo_path && strlen(peer_info->photo_path) > 0) {
2796                                 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2797                         } else {
2798                                 main_list_item->profile_pic_path = NULL;
2799                         }
2800                         main_list_item->user_name_lbl = NULL;
2801                         main_list_item->status_lbl = NULL;
2802                         main_list_item->date_lbl = NULL;
2803                         main_list_item->msg_status_lbl = NULL;
2804                         main_list_item->main_item_layout = NULL;
2805
2806                 }
2807         }
2808
2809         if (tablename) {
2810                 free(tablename);
2811                 tablename = NULL;
2812         }
2813
2814
2815         return main_list_item;
2816 }
2817
2818 void app_nf_back_cb(void *data, Evas_Object *obj, void *event_info)
2819 {
2820         appdata_s *ad = data;
2821         if (ad->loading_popup) {
2822                 show_toast(ad, "Please wait. request in progress");
2823                 return;
2824         }
2825
2826         switch (ad->current_app_state) {
2827                 case TG_INIT_SCREEN_STATE:
2828                         elm_win_lower(ad->win);
2829                         elm_exit();
2830                         break;
2831                 case TG_USER_MAIN_VIEW_STATE:
2832                         if (!ad->menu_popup) {
2833                                 elm_win_lower(ad->win);
2834                                 elm_exit();
2835                         }
2836                         break;
2837                 case TG_CHAT_MESSAGING_VIEW_STATE:
2838             // to be handled
2839                         if (ad->main_item_in_cahtting_data) {
2840                                 int buddy_id = ad->main_item_in_cahtting_data->peer_id;
2841                                 char* tablename = get_table_name_from_number(buddy_id);
2842
2843                                 Eina_Bool res = set_all_rows_read(tablename);
2844                                 if (!res) {
2845                                         //failed.
2846                                 }
2847                                 free(tablename);
2848
2849                                 tg_main_list_item_s* old_item = ad->main_item_in_cahtting_data;
2850                                 ad->main_list = eina_list_remove(ad->main_list,  ad->main_item_in_cahtting_data);
2851                                 if (old_item->peer_print_name) {
2852                                         free(old_item->peer_print_name);
2853                                         old_item->peer_print_name = NULL;
2854                                 }
2855                                 if (old_item->last_message) {
2856                                         free(old_item->last_message);
2857                                         old_item->last_message = NULL;
2858                                 }
2859                                 if (old_item->profile_pic_path) {
2860                                         free(old_item->profile_pic_path);
2861                                         old_item->profile_pic_path = NULL;
2862                                 }
2863                                 if (old_item->buddy_display_name) {
2864                                         free(old_item->buddy_display_name);
2865                                         old_item->buddy_display_name = NULL;
2866                                 }
2867
2868                                 old_item->date_lbl = NULL;
2869                                 old_item->msg_status_lbl = NULL;
2870                                 old_item->main_item_layout = NULL;
2871                                 old_item->profile_pic = NULL;
2872                                 old_item->profile_pic_path = NULL;
2873                                 old_item->status_lbl = NULL;
2874                                 old_item->user_name_lbl = NULL;
2875                                 ad->main_item_in_cahtting_data = NULL;
2876                                 if (ad->peer_in_cahtting_data) {
2877                                         peer_with_pic_s *item = ad->peer_in_cahtting_data;
2878                                         tg_main_list_item_s* latest_item = get_latest_item(ad, item);
2879                                         if (latest_item) {
2880                                                 ad->main_list = eina_list_prepend(ad->main_list, latest_item);
2881                                         }
2882                                 }
2883                                 refresh_main_list_view(ad, EINA_FALSE);
2884                         } else {
2885                                 if (ad->peer_in_cahtting_data) {
2886                                         peer_with_pic_s *item = ad->peer_in_cahtting_data;
2887                                         if (item) {
2888                                                 int buddy_id = item->use_data->peer_id;
2889                                                 char* tablename = get_table_name_from_number(buddy_id);
2890                                                 Eina_Bool res = set_all_rows_read(tablename);
2891                                                 if (!res) {
2892                                                         //failed.
2893                                                 }
2894                                                 free(tablename);
2895
2896                                                 tg_peer_info_s* peer_info = item->use_data;
2897                                                 if (peer_info) {
2898                                                         //if (peer_info->last_msg_id > 0) {
2899
2900                                                         // get message from message table.
2901
2902                                                         char* tablename = get_table_name_from_number(peer_info->peer_id);
2903                                                         //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
2904                                                         tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2905
2906                                                         if (msg) {
2907
2908                                                                 if (msg->service) {
2909                                                                         if (msg->message && strlen(msg->message) > 0) {
2910                                                                                 item->last_message = strdup(msg->message);
2911                                                                         } else {
2912                                                                                 item->last_message = strdup(" ");
2913                                                                         }
2914                                                                 } else {
2915
2916                                                                         int media_type = msg->media_type;
2917                                                                         if (media_type == tgl_message_media_none) {
2918                                                                                 if (msg->message && strlen(msg->message) > 0) {
2919                                                                                         item->last_message = strdup(msg->message);
2920                                                                                 } else {
2921                                                                                         item->last_message = strdup(" ");
2922                                                                                 }
2923                                                                         } else if (media_type == tgl_message_media_photo) {
2924                                                                                 item->last_message = strdup("Image");
2925                                                                         } else if (media_type == tgl_message_media_document) {
2926                                                                                 tgl_media_s *media_msg = NULL;
2927                                                                                 media_msg = get_media_details_from_db(atoll(msg->media_id));
2928
2929                                                                                 if (media_msg && media_msg->doc_type) {
2930                                                                                         if (strlen(media_msg->doc_type) > 0) {
2931                                                                                                 item->last_message = strdup(media_msg->doc_type);
2932                                                                                         } else {
2933                                                                                                 item->last_message = strdup("Document");
2934                                                                                         }
2935                                                                                         free_media_details(media_msg);
2936                                                                                 } else {
2937                                                                                         if (msg->message && strlen(msg->message) > 0) {
2938                                                                                                 item->last_message = strdup(msg->message);
2939                                                                                         } else {
2940                                                                                                 item->last_message = strdup("Document");
2941                                                                                         }
2942                                                                                 }
2943
2944                                                                         } else if (media_type == tgl_message_media_geo) {
2945                                                                                 item->last_message = strdup("Geo location");
2946                                                                         } else if (media_type == tgl_message_media_contact) {
2947                                                                                 item->last_message = strdup("Contact");
2948                                                                         } else if (media_type == tgl_message_media_unsupported) {
2949                                                                                 item->last_message = strdup(" ");
2950                                                                         } else if (media_type == tgl_message_media_photo_encr) {
2951                                                                                 item->last_message = strdup("Image encrypted");
2952                                                                         } else if (media_type == tgl_message_media_document_encr) {
2953                                                                                 item->last_message = strdup("Document encrypted");
2954                                                                         } else  {
2955                                                                                 item->last_message = strdup(" ");
2956                                                                         }
2957                                                                 }
2958
2959                                                                 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2960                                                                 main_list_item->peer_id = peer_info->peer_id;
2961                                                                 main_list_item->peer_type = peer_info->peer_type;
2962
2963                                                                 set_peer_names(peer_info, main_list_item);
2964
2965                                                                 main_list_item->last_seen_time = msg->date;
2966                                                                 main_list_item->profile_pic = NULL;
2967                                                                 main_list_item->last_msg_id = msg->msg_id;
2968                                                                 main_list_item->last_message = strdup(item->last_message);
2969                                                                 main_list_item->last_msg_type = msg->media_type;
2970                                                                 main_list_item->is_out_msg = msg->out;
2971                                                                 main_list_item->last_msg_status = msg->msg_state;
2972                                                                 main_list_item->last_msg_service = msg->service;
2973                                                                 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2974
2975                                                                 if (peer_info->photo_path) {
2976                                                                         main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2977                                                                 } else {
2978                                                                         main_list_item->profile_pic_path = NULL;
2979                                                                 }
2980                                                                 main_list_item->user_name_lbl = NULL;
2981                                                                 main_list_item->status_lbl = NULL;
2982                                                                 main_list_item->date_lbl = NULL;
2983                                                                 main_list_item->msg_status_lbl = NULL;
2984                                                                 main_list_item->main_item_layout = NULL;
2985                                                                 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
2986
2987                                                                 // delete message object
2988                                                                 if (msg->message) {
2989                                                                         free(msg->message);
2990                                                                         msg->message = NULL;
2991                                                                 }
2992
2993                                                                 if (msg->media_id) {
2994                                                                         free(msg->media_id);
2995                                                                         msg->media_id = NULL;
2996                                                                 }
2997
2998                                                                 free(msg);
2999                                                                 msg = NULL;
3000
3001                                                         } else {
3002                                                                 item->last_message = strdup(" ");
3003                                                                 if (peer_info->peer_type == TGL_PEER_CHAT) {
3004                                                                         tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
3005                                                                         main_list_item->peer_id = peer_info->peer_id;
3006                                                                         main_list_item->peer_type = peer_info->peer_type;
3007
3008                                                                         set_peer_names(peer_info, main_list_item);
3009
3010                                                                         main_list_item->last_seen_time = peer_info->last_seen_time;
3011                                                                         main_list_item->profile_pic = NULL;
3012                                                                         main_list_item->last_message = strdup(item->last_message);
3013                                                                         main_list_item->last_msg_type = -1;
3014                                                                         main_list_item->last_msg_service = 0;
3015                                                                         main_list_item->is_out_msg = -1;
3016                                                                         main_list_item->last_msg_id = -1;
3017                                                                         main_list_item->last_msg_status = -1;
3018                                                                         main_list_item->number_of_unread_msgs = 0;
3019
3020                                                                         if (peer_info->photo_path) {
3021                                                                                 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
3022                                                                         } else {
3023                                                                                 main_list_item->profile_pic_path = NULL;
3024                                                                         }
3025                                                                         main_list_item->user_name_lbl = NULL;
3026                                                                         main_list_item->status_lbl = NULL;
3027                                                                         main_list_item->date_lbl = NULL;
3028                                                                         main_list_item->msg_status_lbl = NULL;
3029                                                                         main_list_item->main_item_layout = NULL;
3030                                                                         ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
3031                                                                 }
3032                                                         }
3033
3034                                                         if (tablename) {
3035                                                                 free(tablename);
3036                                                                 tablename = NULL;
3037                                                         }
3038                                                         //}
3039                                                 }
3040                                         }
3041                                         refresh_main_list_view(ad, EINA_TRUE);
3042                                 }
3043                         }
3044
3045                         elm_naviframe_item_pop(ad->nf);
3046                         ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3047                         show_floating_button(ad);
3048                         break;
3049                 case TG_SELECT_BUDDY_VIEW:
3050                         elm_naviframe_item_pop(ad->nf);
3051                         ad->current_app_state = TG_SET_CHAT_INFO_STATE;
3052                         break;
3053                 case TG_SET_USERNAME_STATE:
3054                         elm_naviframe_item_pop(ad->nf);
3055                         ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3056                         break;
3057 #if 0
3058                 case TG_ADD_CONTACT_STATE:
3059                         elm_naviframe_item_pop(ad->nf);
3060                         ad->current_app_state = TG_SET_USER_INFO_STATE;
3061                         break;
3062 #endif
3063                 case TG_SET_CHAT_INFO_STATE:
3064                 case TG_SET_USER_INFO_STATE:
3065                         elm_naviframe_item_pop(ad->nf);
3066                         ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3067                         break;
3068                 case TG_USER_MAIN_VIEW_SELECTION_STATE:
3069                 case TG_PEER_SEARCH_VIEW_STATE:
3070                 case TG_START_MESSAGING_VIEW_STATE:
3071                 case TG_SETTINGS_SCREEN_STATE:
3072                         elm_naviframe_item_pop(ad->nf);
3073                         ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3074                         show_floating_button(ad);
3075                         //evas_object_show(ad->panel);
3076                         //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3077                         break;
3078                 case TG_SETTINGS_EDIT_NAME_STATE:
3079                         elm_naviframe_item_pop(ad->nf);
3080                         ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3081                         delete_floating_button(ad);
3082                         break;
3083                 case TG_COUNTRY_SELECTION_VIEW:
3084                         elm_naviframe_item_pop(ad->nf);
3085                         ad->current_app_state = TG_REGISTRATION_STATE;
3086                         break;
3087                 case TG_REGISTRATION_STATE:
3088                         elm_win_lower(ad->win);
3089                         elm_exit();
3090                         break;
3091                 case TG_LOGIN_STATE:
3092                         send_request_for_restart_server(ad, ad->service_client);
3093                         if (ad->timer_value > 0) {
3094                                 Ecore_Timer* timer = evas_object_data_get(ad->nf, "code_timer");
3095                                 if (timer)
3096                                         ecore_timer_del(timer);
3097                         }
3098
3099                         elm_naviframe_item_pop(ad->nf);
3100                         ad->current_app_state = TG_REGISTRATION_STATE;
3101                         break;
3102                 case TG_PROFILE_REGISTRATION_STATE:
3103                         /*                      ad->current_app_state = TG_REGISTRATION_STATE;
3104                                                 elm_naviframe_item_pop(ad->nf);*/
3105                         elm_win_lower(ad->win);
3106                         elm_exit();
3107                         break;
3108                 case TG_BUDDY_LIST_STATE:
3109                         evas_object_data_set(ad->nf, "buddy_list", NULL);
3110                         elm_win_lower(ad->win);
3111                         elm_exit();
3112                         break;
3113                 case TG_BUDDY_CHAT_CONV_STATE:
3114                         ad->buddy_in_cahtting_data = NULL;
3115                         if (ad->loaded_msg_list) {
3116                                 eina_list_free(ad->loaded_msg_list);
3117                                 ad->loaded_msg_list = NULL;
3118                         }
3119                         elm_naviframe_item_pop(ad->nf);
3120                         ad->current_app_state = TG_BUDDY_LIST_STATE;
3121                         //evas_object_show(ad->panel);
3122                         //refresh_buddy_list(ad);
3123                         break;
3124                 case TG_ADD_CONTACT_STATE:
3125                         elm_naviframe_item_pop(ad->nf);
3126                         if (ad->is_loading_from_profile_view) {
3127                                 ad->is_loading_from_profile_view = EINA_FALSE;
3128                                 ad->current_app_state = TG_SET_USER_INFO_STATE;
3129                         } else if (ad->is_loading_from_msg_view) {
3130                                 ad->is_loading_from_msg_view = EINA_FALSE;
3131                                 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3132                         } else {
3133                                 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3134                                 show_floating_button(ad);
3135                         }
3136                         break;
3137                 case TG_BUDDY_LIST_SELECTION_STATE:
3138                 {
3139                         Eina_List *l = NULL;
3140                         user_data_with_pic_s *item = NULL;
3141                         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3142                                 if (!item)
3143                                         continue;
3144                                 user_data_s* user = item->use_data;
3145                                 user->is_selected = EINA_FALSE;
3146                         }
3147                         elm_naviframe_item_pop(ad->nf);
3148                         ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3149                 }
3150                         break;
3151                 case TG_GROUP_CHAT_NAME_ENTRY_STATE:
3152                 {
3153                         Eina_List *l = NULL;
3154                         user_data_with_pic_s *item = NULL;
3155                         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3156                                 if (!item)
3157                                         continue;
3158
3159                                 user_data_s* user = item->use_data;
3160                                 user->is_selected = EINA_FALSE;
3161                         }
3162                         elm_naviframe_item_pop(ad->nf);
3163                         ad->current_app_state = TG_BUDDY_LIST_SELECTION_STATE;
3164                         //evas_object_show(ad->panel);
3165                         //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3166                         //refresh_buddy_list(ad);
3167                 }
3168                         break;
3169                 default:
3170                         break;
3171         }
3172 }
3173
3174 void
3175 layout_back_cb(void *data, Evas_Object *obj, void *event_info)
3176 {
3177         appdata_s *ad = data;
3178         /* Let window go to hide state. */
3179         elm_win_lower(ad->win);
3180 }
3181
3182 void _btn_clicked(void* data, Evas_Object* btn, void* ev)
3183 {
3184         appdata_s *ad = data;
3185         bundle *msg = bundle_create();
3186         if (bundle_add_str(msg, "tizen_app", "Hello service") != 0) {
3187                 ERR("Failed to add data by key to bundle");
3188                 bundle_free(msg);
3189         }
3190
3191         int result = _app_send_response(ad, msg);
3192         if (result != SVC_RES_OK) {
3193                 bundle_free(msg);
3194         }
3195         bundle_free(msg);
3196 }
3197
3198 Eina_Bool on_load_main_view_requested(void *data)
3199 {
3200         appdata_s *ad = data;
3201         if (ad) {
3202                 elm_naviframe_item_pop(ad->nf);
3203                 hide_loading_popup(ad);
3204                 ad->is_loading_from_msg_view = EINA_FALSE;
3205                 ad->is_loading_from_profile_view = EINA_FALSE;
3206                 launch_user_main_view_cb(ad);
3207                 int unread_msg_cnt = get_number_of_unread_messages();
3208                 if (unread_msg_cnt <= 0) {
3209                         badge_set_count(TELEGRAM_APP_ID, 0);
3210                         return ECORE_CALLBACK_CANCEL;
3211                 }
3212
3213                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3214                 if (BADGE_ERROR_NONE != err) {
3215
3216                 }
3217         }
3218     return ECORE_CALLBACK_CANCEL;
3219 }
3220
3221 Eina_Bool on_init_view_requested(void *data)
3222 {
3223         appdata_s *ad = data;
3224         if (ad) {
3225                 Eina_List *user_info = get_registered_user_info();
3226                 if (!user_info) {
3227                         elm_naviframe_item_pop(ad->nf);
3228                         ad->current_app_state = TG_REGISTRATION_STATE;
3229                         launch_init_screen(ad);
3230                 } else {
3231                         //show_toast(ad, "user already registered");
3232                         load_registered_user_data(ad);
3233                         load_buddy_list_data(ad);
3234                         load_unknown_buddy_list_data(ad);
3235                         load_peer_data(ad);
3236                         load_main_list_data(ad);
3237                         eina_list_free(user_info);
3238                         ecore_timer_add(2, on_load_main_view_requested, ad);
3239                 }
3240         }
3241     return ECORE_CALLBACK_CANCEL;
3242 }
3243
3244 void on_tg_service_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
3245 {
3246         if (result == APP_CONTROL_ERROR_NONE)
3247                 LOGE("Server launched successfully.");
3248         else
3249                 LOGE("Server not launched.");
3250 }
3251
3252 void launch_tg_server(void *data)
3253 {
3254         appdata_s *ad = data;
3255         app_control_h app_control = NULL;
3256         int ret = APP_CONTROL_ERROR_NONE;
3257
3258         ret = app_control_create(&app_control);
3259         ret_if (APP_CONTROL_ERROR_NONE != ret);
3260
3261         ret = app_control_set_app_id(app_control, TELEGRAM_SERVER_APP_NAME);
3262         goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3263
3264         ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
3265         goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3266
3267         ret = app_control_send_launch_request(app_control, &on_tg_service_result_cb, ad);
3268         goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3269
3270 out:
3271         app_control_destroy(app_control);
3272 }
3273
3274 static void create_base_gui(appdata_s *ad)
3275 {
3276         ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
3277         elm_win_conformant_set(ad->win, EINA_TRUE);
3278         elm_win_autodel_set(ad->win, EINA_TRUE);
3279
3280         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
3281         /* Telegram guideline 720 x 1280 */
3282         elm_app_base_scale_set(2.6);
3283
3284         if (elm_win_wm_rotation_supported_get(ad->win)) {
3285                 int rots[2] = {0, 180};
3286                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), sizeof(rots) / sizeof(int));
3287         }
3288         ad->target_direction = tg_get_device_orientation();
3289         char edj_path[PATH_MAX] = {0, };
3290         app_get_resource(TELEGRAM_CUSTOM_WINSET_EDJ, edj_path, (int)PATH_MAX);
3291
3292         Elm_Theme *theme = elm_theme_new();
3293         elm_theme_extension_add(NULL, edj_path);
3294         ad->theme = theme;
3295         evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
3296
3297         ad->conform = elm_conformant_add(ad->win);
3298         evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3299         elm_win_resize_object_add(ad->win, ad->conform);
3300         evas_object_show(ad->conform);
3301
3302         ad->layout = elm_layout_add(ad->conform);
3303         evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3304
3305         //elm_layout_theme_set(ad->layout, "layout", "drawer", "panel");
3306         elm_layout_theme_set(ad->layout, "layout", "application", "default");
3307         evas_object_show(ad->layout);
3308
3309         elm_object_content_set(ad->conform, ad->layout);
3310         ad->nf = elm_naviframe_add(ad->layout);
3311
3312         ad->current_app_state = TG_SPLASH_SCREEN_STATE;
3313         launch_splash_screen_cb(ad);
3314         elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
3315         eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, app_nf_back_cb, ad);
3316
3317         evas_object_show(ad->win);
3318
3319         // check for user info
3320
3321         //
3322         char *chat_bg = NULL;
3323         preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
3324         if (chat_bg) {
3325                 ad->chat_background = strdup(chat_bg);
3326         }
3327
3328         if (!ad->chat_background) {
3329                 ad->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
3330                 preference_set_string(TG_CHAT_BG_PREFERENCE, ad->chat_background);
3331         }
3332         launch_tg_server(ad);
3333         ecore_timer_add(5, on_init_view_requested, ad);
3334         ucol_init();
3335 }
3336
3337
3338 char *build_a_path(const char *path, const char *filename)
3339 {
3340         char *ret;
3341         int len;
3342
3343         len = strlen(path) + strlen(filename) + 2;
3344         ret = malloc(len);
3345         if (!ret) {
3346                 return NULL;
3347         }
3348
3349         snprintf(ret, len, "%s/%s", path, filename);
3350         return ret;
3351 }
3352
3353 int remove_directory(const char *path)
3354 {
3355         DIR *d = opendir(path);
3356         size_t path_len = strlen(path);
3357         int r = -1;
3358
3359         if (d)
3360         {
3361                 struct dirent *p;
3362
3363                 r = 0;
3364
3365                 while (!r && (p=readdir(d)))
3366                 {
3367                         int r2 = -1;
3368                         char *buf;
3369                         size_t len;
3370
3371                         /* Skip the names "." and ".." as we don't want to recurse on them. */
3372                         if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
3373                         {
3374                                 continue;
3375                         }
3376
3377                         len = path_len + strlen(p->d_name) + 2;
3378                         buf = malloc(len);
3379
3380                         if (buf)
3381                         {
3382                                 struct stat statbuf;
3383
3384                                 snprintf(buf, len, "%s/%s", path, p->d_name);
3385
3386                                 if (!stat(buf, &statbuf))
3387                                 {
3388                                         if (S_ISDIR(statbuf.st_mode))
3389                                         {
3390                                                 r2 = remove_directory(buf);
3391                                         }
3392                                         else
3393                                         {
3394                                                 r2 = unlink(buf);
3395                                         }
3396                                 }
3397
3398                                 free(buf);
3399                         }
3400
3401                         r = r2;
3402                 }
3403
3404                 closedir(d);
3405         }
3406
3407         if (!r)
3408         {
3409                 r = rmdir(path);
3410         }
3411
3412         return r;
3413 }
3414
3415 Eina_Bool copy_file_another_dir(const char *source_file, const char *target_file)
3416 {
3417         FILE *source, *target;
3418
3419         source = fopen(source_file, "rb+");
3420
3421         if (source == NULL)
3422         {
3423                 return EINA_FALSE;
3424         }
3425
3426         target = fopen(target_file, "wb+");
3427
3428         if (target == NULL)
3429         {
3430                 fclose(source);
3431                 return EINA_FALSE;
3432         }
3433
3434         char buffer[1024] = {0};    /*Buffer to store files content*/
3435
3436         int size = 0;
3437         while((size = fread(buffer, 1, 1024, source)) != 0)
3438         {
3439                 fwrite(buffer, 1, size, target);
3440         }
3441
3442         fclose(source);
3443         fclose(target);
3444         return EINA_TRUE;
3445 }
3446
3447 void recursive_dir_copy(const char *source_dir, const char *target_dir)
3448 {
3449         DIR *open_src;
3450         struct dirent *src_files;
3451         Eina_List *src_dir_stack;
3452         Eina_List *tar_dir_stack;
3453
3454         char *src_dir;
3455         char *tar_dir;
3456
3457
3458         src_dir_stack = NULL;
3459         tar_dir_stack = NULL;
3460
3461         src_dir = strdup(source_dir);
3462         tar_dir = strdup(target_dir);
3463
3464         do {
3465                 open_src = opendir(src_dir);
3466                 while((src_files = readdir(open_src))) {
3467                         if (src_files->d_type == DT_DIR) {
3468                                 // create directory
3469                                 // push to stack
3470                                 if (src_files->d_name[0] == '.' && (src_files->d_name[1] == '\0' || (src_files->d_name[1] == '.' && src_files->d_name[2] == '\0'))) {
3471                                         continue;
3472                                 }
3473
3474                                 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3475                                 mkdir(temp_dest, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3476
3477                                 char *temp_src = build_a_path(src_dir, src_files->d_name);
3478
3479                                 src_dir_stack = eina_list_prepend(src_dir_stack, temp_src);
3480                                 tar_dir_stack = eina_list_prepend(tar_dir_stack, temp_dest);
3481
3482                         } else {
3483                                 // copy file to target src_dir.
3484                                 char *temp_src = build_a_path(src_dir, src_files->d_name);
3485                                 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3486                                 //int ret = rename(temp_src, temp_dest);
3487                                 Eina_Bool ret = copy_file_another_dir(temp_src, temp_dest);
3488                                 if (ret) {
3489                                         // file copy success
3490                                 } else {
3491                                         // file copy failed.
3492                                 }
3493                                 free(temp_src);
3494                                 free(temp_dest);
3495                         }
3496                 }
3497
3498                 if (open_src) {
3499                         closedir(open_src);
3500                 }
3501
3502                 free(src_dir);
3503                 free(tar_dir);
3504                 src_dir = eina_list_nth(src_dir_stack, 0);
3505                 if (src_dir) {
3506                         src_dir_stack = eina_list_remove(src_dir_stack, src_dir);
3507                 }
3508                 tar_dir = eina_list_nth(tar_dir_stack, 0);
3509                 if (tar_dir) {
3510                         tar_dir_stack = eina_list_remove(tar_dir_stack, tar_dir);
3511                 }
3512         } while (src_dir != NULL);
3513 }
3514
3515 void move_downloaded_files()
3516 {
3517         // create download directory.
3518         struct stat st = {0};
3519         char dest_download_dir[512] = {0,};
3520         sprintf(dest_download_dir, "%s/%s", app_get_shared_data_path(), "downloads");
3521         if (stat(dest_download_dir, &st) == -1) {
3522                 mkdir(dest_download_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3523         }
3524
3525         LOGE("dest Download directory: %s", dest_download_dir);
3526         // create download directory.
3527         char target_download_dir[512] = {0,};
3528         sprintf(target_download_dir, "%s/%s/%s", app_get_data_path(), "telegram_tizen", "downloads");
3529
3530         LOGE("target Download directory: %s", target_download_dir);
3531         recursive_dir_copy(target_download_dir, dest_download_dir);
3532         remove_directory(target_download_dir);
3533 }
3534
3535
3536 Eina_Bool dirExists(const char *path)
3537 {
3538     struct stat info;
3539
3540     if (stat(path, &info) != 0)
3541         return EINA_FALSE;
3542     else if (info.st_mode & S_IFDIR)
3543         return EINA_TRUE;
3544     else
3545         return EINA_FALSE;
3546 }
3547
3548 static void update_downloaded_video_thumb_paths(const char *table_name, const char *col_one, const char *column_two)
3549 {
3550         LOGE("Update media info table thumb files");
3551         if (!table_name || !col_one || !column_two)
3552                 return;
3553
3554         Eina_List *col_types = NULL;
3555         col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER);
3556         col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT);
3557
3558         Eina_List *col_names = NULL;
3559         col_names = eina_list_append(col_names, col_one);
3560         col_names = eina_list_append(col_names, column_two);
3561
3562         Eina_List *vals = get_values_from_table_sync(table_name, col_names, col_types, NULL, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED);
3563         int val_size = eina_list_count(vals);
3564         if (vals && val_size > 0) {
3565                 Eina_List *row_vals = NULL;
3566                 EINA_LIST_FREE(vals, row_vals) {
3567                         int user_id = 0;
3568                         char *file_path = NULL;
3569
3570                         int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
3571                         if (temp_user_id) {
3572                                 user_id  = *temp_user_id;
3573                                 free(temp_user_id);
3574                         }
3575
3576                         file_path = (char *)eina_list_nth(row_vals, 1);
3577
3578                         if (!file_path) {
3579                                 eina_list_free(row_vals);
3580                                 continue;
3581                         }
3582
3583                         if (strlen(file_path) > 0 && strncmp(file_path, "/opt/usr/media/telegram/telegram_tizen", strlen("/opt/usr/media/telegram/telegram_tizen")) == 0) {
3584                                 // update file name in db
3585                                 char *new_path = str_replace(new_path, "/opt/usr/media/telegram/telegram_tizen", app_get_shared_data_path());
3586                                 if (!new_path) {
3587                                         free(file_path);
3588                                         eina_list_free(row_vals);
3589                                         continue;
3590                                 }
3591                                 LOGE("Old file path: %s", file_path);
3592                                 LOGE("New file path: %s", new_path);
3593
3594                                 //update database.
3595                                 Eina_List *new_col_types = NULL;
3596                                 new_col_types = eina_list_append(new_col_types, TG_DB_COLUMN_TEXT);
3597
3598                                 Eina_List *new_col_names = NULL;
3599                                 new_col_names = eina_list_append(new_col_names, column_two);
3600
3601                                 Eina_List *new_col_values = NULL;
3602                                 new_col_values = eina_list_append(new_col_values, new_path);
3603
3604                                 char* where_clause = NULL;
3605
3606                                 int str_size = strlen(col_one) + strlen(" = ") + 10 + 1;
3607                                 where_clause = (char *)malloc(str_size);
3608                                 snprintf(where_clause, str_size, "%s = %d", col_one, user_id);
3609                                 Eina_Bool ret = update_table(table_name, new_col_names, new_col_types, new_col_values, where_clause);
3610                                 if (!ret) {
3611                                         LOGE("Media info table update failed.");
3612                                 } else {
3613                                         LOGE("Media info table update success.");
3614                                 }
3615                                 free(where_clause);
3616                                 eina_list_free(new_col_types);
3617                                 eina_list_free(new_col_names);
3618                                 eina_list_free(new_col_values);
3619                         }
3620
3621                         free(file_path);
3622
3623                         eina_list_free(row_vals);
3624                 }
3625         }
3626         eina_list_free(col_types);
3627         eina_list_free(col_names);
3628 }
3629
3630 void update_downloaded_video_thumb_paths_in_media_info_table()
3631 {
3632         update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_DOCUMENT_THUMB_FILE);
3633 }
3634
3635
3636 void update_downloaded_file_paths_in_media_info_table()
3637 {
3638         update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_FILE_PATH);
3639 }
3640
3641
3642 void update_downloaded_file_paths_in_chat_info_table()
3643 {
3644         update_downloaded_video_thumb_paths(CHAT_INFO_TABLE_NAME, CHAT_INFO_TABLE_CHAT_ID, CHAT_INFO_TABLE_PHOTO_PATH);
3645
3646 }
3647
3648 void update_downloaded_file_paths_in_buddy_info_table(const char *table_name)
3649 {
3650         LOGE("Update %s", table_name);
3651         update_downloaded_video_thumb_paths(table_name, USER_INFO_TABLE_USER_ID, USER_INFO_TABLE_PHOTO_PATH);
3652 }
3653
3654 static bool app_create(void *data)
3655 {
3656         /*
3657            Hook to take necessary actions before main event loop starts
3658            Initialize UI resources and application's data
3659            If this function returns true, the main loop of application starts
3660            If this function returns false, the application is terminated
3661          */
3662
3663         appdata_s *ad = data;
3664         if (dirExists(OLD_DIR)) {
3665                 char *new_dir = app_get_data_path();
3666                 recursive_dir_copy(OLD_DIR, new_dir);
3667                 remove_directory(OLD_DIR);
3668                 move_downloaded_files();
3669                 // update downloaded image paths in DB
3670                 update_downloaded_file_paths_in_buddy_info_table(BUDDY_INFO_TABLE_NAME);
3671                 update_downloaded_file_paths_in_buddy_info_table(USER_INFO_TABLE_NAME);
3672                 update_downloaded_file_paths_in_buddy_info_table(PEER_INFO_TABLE_NAME);
3673                 update_downloaded_file_paths_in_chat_info_table();
3674                 update_downloaded_file_paths_in_media_info_table();
3675                 update_downloaded_video_thumb_paths_in_media_info_table();
3676         }
3677
3678         tg_db_init();
3679         bindtextdomain(PACKAGE, "/opt/usr/apps/org.tizen.telegram_tizen_client/res/locale");
3680
3681         ad->phone_number = NULL;
3682         ad->buddy_list = NULL;
3683         ad->unknown_buddy_list = NULL;
3684         ad->known_buddy_list = NULL;
3685         ad->main_list = NULL;
3686         ad->peer_list = NULL;
3687         ad->search_peer_list = NULL;
3688         ad->is_first_time_registration = EINA_FALSE;
3689         ad->panel = NULL;
3690         ad->loaded_msg_list = NULL;
3691         ad->loading_popup = NULL;
3692         ad->current_user_data = NULL;
3693         ad->is_tg_initilized = EINA_FALSE;
3694         ad->chat_background = NULL;
3695         ad->msg_popup = NULL;
3696         ad->s_notififcation = NULL;
3697         ad->panel = NULL;
3698         ad->is_server_ready = EINA_FALSE;
3699         ad->is_waiting_for_phone_num = EINA_FALSE;
3700         ad->login_timer = NULL;
3701         ad->contact_list = NULL;
3702         ad->main_item = NULL;
3703         ad->country_codes_list = NULL;
3704         ad->country_names_list = NULL;
3705         //ad->msg_count = 0;
3706         create_base_gui(ad);
3707         int err = badge_new(TELEGRAM_APP_ID);
3708         if (BADGE_ERROR_NONE != err) {
3709
3710         }
3711         init_service(ad);
3712         return true;
3713 }
3714
3715 static void
3716 app_control(app_control_h app_control, void *data)
3717 {
3718         /* Handle the launch request. */
3719 }
3720
3721 static void
3722 app_pause(void *data)
3723 {
3724         appdata_s *app_data = data;
3725         if (app_data) {
3726                 app_data->s_app_visible_state = APP_STATE_IN_BACKGROUND;
3727                 int unread_msg_cnt = get_number_of_unread_messages();
3728                 if (unread_msg_cnt <= 0) {
3729                         badge_set_count(TELEGRAM_APP_ID, 0);
3730                         return;
3731                 }
3732                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3733                 if (BADGE_ERROR_NONE != err) {
3734
3735                 }
3736         }
3737 }
3738
3739 static void
3740 app_resume(void *data)
3741 {
3742         appdata_s *app_data = data;
3743         if (app_data) {
3744                 app_data->s_app_visible_state = APP_STATE_IN_FOREGROUND;
3745         }
3746 }
3747
3748
3749 void free_app_data(appdata_s *app_data, Eina_Bool destroy_server)
3750 {
3751         if (!app_data) {
3752                 return;
3753         }
3754
3755         int unread_msg_cnt = get_number_of_unread_messages();
3756         if (unread_msg_cnt > 0) {
3757                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3758                 if (BADGE_ERROR_NONE != err) {
3759
3760                 }
3761         } else {
3762                 badge_set_count(TELEGRAM_APP_ID, 0);
3763         }
3764         eina_list_free(app_data->country_codes_list);
3765         eina_list_free(app_data->country_names_list);
3766         free(app_data->country_code_buffer);
3767
3768         if (app_data->panel) {
3769                 Evas_Object *panel_list = evas_object_data_get(app_data->panel, "panel_list");
3770                 if (panel_list) {
3771                         evas_object_del(panel_list);
3772                 }
3773                 evas_object_del(app_data->panel);
3774                 app_data->panel = NULL;
3775         }
3776
3777         if (app_data->phone_number) {
3778                 free(app_data->phone_number);
3779                 app_data->phone_number = NULL;
3780         }
3781
3782         if (app_data->sms_code) {
3783                 free(app_data->sms_code);
3784                 app_data->sms_code = NULL;
3785         }
3786
3787         if (app_data->current_user_data) {
3788                 free_user_data(app_data->current_user_data);
3789         }
3790         if (app_data->chat_background) {
3791                 app_data->chat_background = NULL;
3792         }
3793         if (app_data && app_data->s_notififcation) {
3794                 notification_delete_all(NOTIFICATION_TYPE_NOTI);
3795                 app_data->s_notififcation = NULL;
3796         }
3797
3798         peer_with_pic_s* pic_item = NULL;
3799         EINA_LIST_FREE(app_data->peer_list, pic_item) {
3800                 if (!pic_item)
3801                         continue;
3802                 tg_peer_info_s* item = pic_item->use_data;
3803                 if (item) {
3804                         if (item->print_name) {
3805                                 free(item->print_name);
3806                                 item->print_name = NULL;
3807                         }
3808                         if (item->photo_path) {
3809                                 free(item->photo_path);
3810                                 item->photo_path = NULL;
3811                         }
3812                         pic_item->contact_icon = NULL;
3813                         pic_item->msg_object = NULL;
3814                         pic_item->name_object = NULL;
3815                         free(item);
3816                 }
3817                 free(pic_item);
3818         }
3819         app_data->peer_list = NULL;
3820
3821         tg_main_list_item_s* main_item = NULL;
3822         EINA_LIST_FREE(app_data->main_list, main_item) {
3823
3824                 if (main_item->peer_print_name) {
3825                         free(main_item->peer_print_name);
3826                         main_item->peer_print_name = NULL;
3827                 }
3828                 if (main_item->last_message) {
3829                         free(main_item->last_message);
3830                         main_item->last_message = NULL;
3831                 }
3832                 if (main_item->profile_pic_path) {
3833                         free(main_item->profile_pic_path);
3834                         main_item->profile_pic_path = NULL;
3835                 }
3836                 if (main_item->buddy_display_name) {
3837                         free(main_item->buddy_display_name);
3838                         main_item->buddy_display_name = NULL;
3839                 }
3840                 main_item->date_lbl = NULL;
3841                 main_item->msg_status_lbl = NULL;
3842                 main_item->main_item_layout = NULL;
3843                 main_item->profile_pic = NULL;
3844                 main_item->profile_pic_path = NULL;
3845                 main_item->status_lbl = NULL;
3846                 main_item->user_name_lbl = NULL;
3847         }
3848         app_data->main_list = NULL;
3849
3850         user_data_with_pic_s* item = NULL;
3851         EINA_LIST_FREE(app_data->buddy_list, item) {
3852                 if (!item)
3853                         continue;
3854                 user_data_s* user_data = item->use_data;
3855
3856                 if (user_data->print_name) {
3857                         free(user_data->print_name);
3858                         user_data->print_name = NULL;
3859                 }
3860
3861                 if (user_data->photo_path) {
3862                         free(user_data->photo_path);
3863                         user_data->photo_path = NULL;
3864                 }
3865
3866                 if (user_data->first_name) {
3867                         free(user_data->first_name);
3868                         user_data->first_name = NULL;
3869                 }
3870
3871                 if (user_data->last_name) {
3872                         free(user_data->last_name);
3873                         user_data->last_name = NULL;
3874                 }
3875
3876                 if (user_data->phone) {
3877                         free(user_data->phone);
3878                         user_data->phone = NULL;
3879                 }
3880                 if (user_data->real_first_name) {
3881                         free(user_data->real_first_name);
3882                         user_data->real_first_name = NULL;
3883                 }
3884
3885                 if (user_data->real_last_name) {
3886                         free(user_data->real_last_name);
3887                         user_data->real_last_name = NULL;
3888                 }
3889
3890                 if (user_data->username) {
3891                         free(user_data->username);
3892                         user_data->username = NULL;
3893                 }
3894                 free(user_data);
3895                 free(item);
3896         }
3897         app_data->buddy_list = NULL;
3898
3899
3900         EINA_LIST_FREE(app_data->unknown_buddy_list, item) {
3901
3902                 if (!item)
3903                         continue;
3904
3905                 user_data_s* user_data = item->use_data;
3906
3907                 if (user_data->print_name) {
3908                         free(user_data->print_name);
3909                         user_data->print_name = NULL;
3910                 }
3911
3912                 if (user_data->photo_path) {
3913                         free(user_data->photo_path);
3914                         user_data->photo_path = NULL;
3915                 }
3916
3917                 if (user_data->first_name) {
3918                         free(user_data->first_name);
3919                         user_data->first_name = NULL;
3920                 }
3921
3922                 if (user_data->last_name) {
3923                         free(user_data->last_name);
3924                         user_data->last_name = NULL;
3925                 }
3926
3927                 if (user_data->phone) {
3928                         free(user_data->phone);
3929                         user_data->phone = NULL;
3930                 }
3931                 if (user_data->real_first_name) {
3932                         free(user_data->real_first_name);
3933                         user_data->real_first_name = NULL;
3934                 }
3935
3936                 if (user_data->real_last_name) {
3937                         free(user_data->real_last_name);
3938                         user_data->real_last_name = NULL;
3939                 }
3940
3941                 if (user_data->username) {
3942                         free(user_data->username);
3943                         user_data->username = NULL;
3944                 }
3945                 free(user_data);
3946                 free(item);
3947         }
3948         app_data->unknown_buddy_list = NULL;
3949
3950         EINA_LIST_FREE(app_data->known_buddy_list, item) {
3951                 user_data_s* user_data = item->use_data;
3952
3953                 if (user_data->print_name)
3954                         free(user_data->print_name);
3955                         user_data->print_name = NULL;
3956
3957                 if (user_data->photo_path)
3958                         free(user_data->photo_path);
3959                         user_data->photo_path = NULL;
3960
3961                 if (user_data->first_name)
3962                         free(user_data->first_name);
3963                         user_data->first_name = NULL;
3964
3965                 if (user_data->last_name)
3966                         free(user_data->last_name);
3967                         user_data->last_name = NULL;
3968
3969                 if (user_data->phone)
3970                         free(user_data->phone);
3971                         user_data->phone = NULL;
3972
3973                 if (user_data->real_first_name)
3974                         free(user_data->real_first_name);
3975                         user_data->real_first_name = NULL;
3976
3977                 if (user_data->real_last_name)
3978                         free(user_data->real_last_name);
3979                         user_data->real_last_name = NULL;
3980
3981                 if (user_data->username)
3982                         free(user_data->username);
3983                         user_data->username = NULL;
3984
3985                 free(user_data);
3986                 free(item);
3987         }
3988         app_data->known_buddy_list = NULL;
3989
3990         eina_list_free(app_data->loaded_msg_list);
3991         app_data->loaded_msg_list = NULL;
3992
3993         if (app_data->service_client && destroy_server) {
3994                 service_client_destroy(app_data->service_client);
3995                 app_data->service_client = NULL;
3996         }
3997         free_contact_list(app_data->contact_list);
3998         tg_db_fini();
3999 }
4000
4001 static void
4002 app_terminate(void *data)
4003 {
4004         free_app_data(data, EINA_TRUE);
4005 }
4006
4007 static void
4008 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
4009 {
4010         /*APP_EVENT_LANGUAGE_CHANGED*/
4011         char *locale = NULL;
4012         system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
4013         if (locale) {
4014                 setlocale(LC_ALL, locale);
4015                 elm_language_set(locale);
4016         }
4017         free(locale);
4018         return;
4019 }
4020
4021
4022 TelegramAppDirection tg_get_device_orientation()
4023 {
4024         TelegramAppDirection target_direction;
4025
4026         switch (app_get_device_orientation()) {
4027         case APP_DEVICE_ORIENTATION_90:
4028                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4029                 break;
4030         case APP_DEVICE_ORIENTATION_180:
4031                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4032                 break;
4033         case APP_DEVICE_ORIENTATION_270:
4034                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4035                 break;
4036         case APP_DEVICE_ORIENTATION_0:
4037         default:
4038                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4039                 break;
4040         }
4041
4042         return target_direction;
4043 }
4044
4045 void update_current_view(appdata_s *ad)
4046 {
4047         if (!ad) {
4048                 return;
4049         }
4050         switch (ad->current_app_state) {
4051         case TG_USER_MAIN_VIEW_STATE:
4052                 refresh_main_list_view(ad, EINA_FALSE);
4053                 break;
4054         case TG_CHAT_MESSAGING_VIEW_STATE:
4055                 refresh_messaging_view(ad);
4056                 break;
4057         default:
4058                 break;
4059         }
4060
4061 }
4062
4063
4064 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
4065 {
4066         appdata_s *ad = user_data;
4067         if (!ad) {
4068                 return;
4069         }
4070         TelegramAppDirection target_direction;
4071         app_device_orientation_e orientation;
4072
4073         if (app_event_get_device_orientation(event_info, &orientation) != 0) {
4074                 return;
4075         }
4076
4077         switch (orientation) {
4078         case APP_DEVICE_ORIENTATION_90:
4079                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4080                 break;
4081         case APP_DEVICE_ORIENTATION_180:
4082                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4083                 break;
4084         case APP_DEVICE_ORIENTATION_270:
4085                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4086                 break;
4087         case APP_DEVICE_ORIENTATION_0:
4088         default:
4089                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4090                 break;
4091         }
4092
4093         if (target_direction != ad->target_direction) {
4094                 ad->target_direction = target_direction;
4095                 update_current_view(ad);
4096         }
4097
4098         return;
4099 }
4100
4101 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
4102 {
4103 }
4104
4105 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
4106 {
4107 }
4108
4109 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
4110 {
4111 }
4112
4113 int main(int argc, char *argv[])
4114 {
4115         appdata_s ad = {0,};
4116         int ret = 0;
4117
4118         ui_app_lifecycle_callback_s event_callback = {0,};
4119         app_event_handler_h handlers[5] = {NULL, };
4120
4121         event_callback.create = app_create;
4122         event_callback.terminate = app_terminate;
4123         event_callback.pause = app_pause;
4124         event_callback.resume = app_resume;
4125         event_callback.app_control = app_control;
4126
4127         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
4128         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
4129         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
4130         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
4131         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
4132         ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
4133
4134         ret = ui_app_main(argc, argv, &event_callback, &ad);
4135         if (ret != APP_ERROR_NONE) {
4136                 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main() is failed. err = %d", ret);
4137         }
4138
4139         return ret;
4140 }