add telegram account, fix db insert error.
[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 (!app->peer_in_cahtting_data || !app->peer_in_cahtting_data->use_data) {
1235                 LOGE("Wrong status, previous chatting data are freed!!!");
1236                 return result;
1237         }
1238
1239
1240         char* from_id_str = NULL;
1241         result = bundle_get_str(rec_msg, "from_id", &from_id_str);
1242         int from_id = atoi(from_id_str);
1243
1244         char* to_id_str = NULL;
1245         result = bundle_get_str(rec_msg, "to_id", &to_id_str);
1246         int to_id = atoi(to_id_str);
1247
1248         char* msg_id_str = NULL;
1249         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1250         long long message_id = atoll(msg_id_str);
1251
1252         char* type_of_chat_str = NULL;
1253         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1254         int type_of_chat = atoi(type_of_chat_str);
1255
1256         if (from_id == app->user_id.id) {
1257                 // sent by the user. May be from another device.
1258         }
1259
1260         //load message from the received info.
1261         // get phone number using buddy id.
1262
1263         Eina_Bool is_user_present_in_main_list = EINA_FALSE;
1264         int id_to_check;
1265         if (type_of_chat == TGL_PEER_USER) {
1266                 id_to_check = from_id;
1267         } else {
1268                 id_to_check = to_id;
1269         }
1270
1271         if (type_of_chat == TGL_PEER_USER) {
1272                 if ((app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state ==  TG_SET_USER_INFO_STATE) &&
1273                                 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == from_id) {
1274                         if (with_date) {
1275                                 char* date_id_str = NULL;
1276                                 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1277                                 int date_id = atoi(date_id_str);
1278                                 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1279                                 wait_for(1);
1280                         }
1281                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1282
1283                         return result;
1284                 }
1285
1286         } else if (type_of_chat == TGL_PEER_CHAT) {
1287                 if ((app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state ==  TG_SET_CHAT_INFO_STATE) &&
1288                                 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
1289                         char* tablename = get_table_name_from_number(to_id);
1290                         tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1291                         if (msg) {
1292                                 // To be handled.
1293                                 if (with_date) {
1294                                         char* date_id_str = NULL;
1295                                         result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1296                                         int date_id = atoi(date_id_str);
1297                                         on_text_message_received_from_buddy(app, date_id, type_of_chat);
1298                                         wait_for(1);
1299                                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1300                                 } else {
1301                                         on_text_message_received_from_buddy(app, message_id, type_of_chat);
1302                                 }
1303                                 if (msg->message) {
1304                                         free(msg->message);
1305                                         msg->message = NULL;
1306                                 }
1307
1308                                 if (msg->media_id) {
1309                                         free(msg->media_id);
1310                                         msg->media_id = NULL;
1311                                 }
1312                         }
1313                         free(msg);
1314                         free(tablename);
1315                         return result;
1316                 }
1317         }
1318
1319         Eina_List *l = NULL;
1320         tg_main_list_item_s *sel_item = NULL;
1321         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1322
1323                 if (sel_item->peer_id != id_to_check) {
1324                         continue;
1325                 }
1326
1327                 // update last message
1328                 char* tablename = get_table_name_from_number(sel_item->peer_id);
1329                 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1330
1331                 if (msg) {
1332                         if (msg->service == 2) {
1333                                 if (msg->message) {
1334                                         free(msg->message);
1335                                         msg->message = NULL;
1336                                 }
1337
1338                                 if (msg->media_id) {
1339                                         free(msg->media_id);
1340                                         msg->media_id = NULL;
1341                                 }
1342                                 free(msg);
1343                                 return SVC_RES_OK;
1344                         }
1345
1346                         if (msg->service == 1) {
1347                                 if (msg->message && strlen(msg->message) > 0) {
1348                                         sel_item->last_message = strdup(msg->message);
1349                                 } else {
1350                                         sel_item->last_message = strdup(" ");
1351                                 }
1352                         } else {
1353
1354                                 int media_type = msg->media_type;
1355                                 if (media_type == tgl_message_media_none) {
1356                                         if (msg->message && strlen(msg->message) > 0) {
1357                                                 sel_item->last_message = strdup(msg->message);
1358                                         } else {
1359                                                 sel_item->last_message = strdup(" ");
1360                                         }
1361                                 } else if (media_type == tgl_message_media_photo) {
1362                                         sel_item->last_message = strdup("Image");
1363                                 } else if (media_type == tgl_message_media_document) {
1364
1365 #if 0
1366                                         if (msg->message && strlen(msg->message) > 0) {
1367                                                 sel_item->last_message = strdup(msg->message);
1368                                         } else {
1369                                                 sel_item->last_message = strdup("Document");
1370                                         }
1371 #else
1372                                         tgl_media_s *media_msg = NULL;
1373                                         media_msg = get_media_details_from_db(atoll(msg->media_id));
1374                                         sel_item->last_message = strdup(media_msg->doc_type);
1375                                         free_media_details(media_msg);
1376 #endif
1377                                 } else if (media_type == tgl_message_media_geo) {
1378                                         sel_item->last_message = strdup("Geo location");
1379                                 } else if (media_type == tgl_message_media_contact) {
1380                                         sel_item->last_message = strdup("Contact");
1381                                 } else if (media_type == tgl_message_media_unsupported) {
1382                                         sel_item->last_message = strdup(" ");
1383                                 } else if (media_type == tgl_message_media_photo_encr) {
1384                                         sel_item->last_message = strdup("Image encrypted");
1385                                 } else if (media_type == tgl_message_media_document_encr) {
1386                                         sel_item->last_message = strdup("Document encrypted");
1387                                 } else  {
1388                                         sel_item->last_message = strdup(" ");
1389                                 }
1390                         }
1391                         sel_item->last_msg_id = msg->msg_id;
1392                         sel_item->last_msg_type = msg->media_type;
1393                         sel_item->is_out_msg = msg->out;
1394                         sel_item->last_msg_status = msg->msg_state;
1395                         sel_item->last_seen_time = msg->date;
1396                         sel_item->number_of_unread_msgs = get_unread_message_count(tablename);
1397
1398                         if (sel_item->profile_pic) {
1399
1400                         }
1401
1402                         if (sel_item->user_name_lbl) {
1403
1404                         }
1405
1406                         if (sel_item->status_lbl) {
1407                                 char* org_msg = NULL;
1408                                 if (sel_item->last_message) {
1409                                         org_msg = sel_item->last_message;
1410                                 } else {
1411                                         org_msg = "";
1412                                 }
1413                                 elm_object_text_set(sel_item->status_lbl, "");
1414
1415                                 char status_buf[126] = {'\0'};
1416                                 const char *color;
1417
1418                                 if (msg->service == 1 || msg->service == 2) {
1419                                         sel_item->last_msg_service = 1;
1420                                         color = "#158CB0";
1421                                 } else {
1422                                         sel_item->last_msg_service = 0;
1423                                         color = "#A4A4A4";
1424                                 }
1425
1426                                 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);
1427                                 elm_object_text_set(sel_item->status_lbl, status_buf);
1428                         }
1429
1430                         if (sel_item->date_lbl) {
1431                                 Eina_Bool is_today = compare_date_with_current_date(sel_item->last_seen_time);
1432
1433                                 if (is_today) {
1434                                         elm_object_text_set(sel_item->date_lbl, "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>Today</font_size></font>");
1435                                 } else {
1436                                         struct tm lt;
1437                                         char res[256];
1438                                         char time_str[256] = { 0, };
1439                                         time_t t;
1440
1441                                         t = sel_item->last_seen_time;
1442                                         (void)localtime_r(&t, &lt);
1443                                         if (strftime(res, sizeof(res), "%d/%b/%Y", &lt) == 0) {
1444                                                 strcpy(res, "unknown");
1445                                         }
1446
1447                                         snprintf(time_str, sizeof(time_str), "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>%s</font_size></font>", res);
1448                                         elm_object_text_set(sel_item->date_lbl, time_str);
1449                                 }
1450                         }
1451                         if (sel_item->msg_status_lbl) {
1452                                 Evas_Object* temp_lbl = elm_object_part_content_get(sel_item->msg_status_lbl, "swallow.status");
1453                                 if (temp_lbl) {
1454                                         evas_object_del(temp_lbl);
1455                                 }
1456
1457                                 Evas_Object* num_lbl = elm_label_add(app->nf);
1458                                 elm_object_style_set(num_lbl, "transparent");
1459                                 char unread_msgs[256];
1460                                 sprintf(unread_msgs, "<font=Tizen:style=Bold color=#ffffff align=center><font_size=25>%d</font_size></font>", sel_item->number_of_unread_msgs);
1461                                 elm_object_text_set(num_lbl, unread_msgs);
1462                                 evas_object_size_hint_weight_set(num_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1463                                 evas_object_size_hint_align_set(num_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
1464                                 evas_object_show(num_lbl);
1465
1466                                 char edj_path[PATH_MAX] = {0, };
1467                                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
1468                                 Evas_Object* msg_status = elm_layout_add(app->nf);
1469                                 elm_layout_file_set(msg_status, edj_path, "circle_item");
1470                                 evas_object_size_hint_weight_set(msg_status, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1471                                 evas_object_size_hint_align_set(msg_status, EVAS_HINT_FILL, EVAS_HINT_FILL);
1472                                 evas_object_show(msg_status);
1473                                 elm_object_part_content_set(msg_status, "content", num_lbl);
1474
1475                                 elm_object_part_content_set(sel_item->msg_status_lbl, "swallow.status", msg_status);
1476
1477                         }
1478                         if (msg->message) {
1479                                 free(msg->message);
1480                                 msg->message = NULL;
1481                         }
1482
1483                         if (msg->media_id) {
1484                                 free(msg->media_id);
1485                                 msg->media_id = NULL;
1486                         }
1487                         free(msg);
1488                 }
1489
1490                 free(tablename);
1491                 is_user_present_in_main_list = EINA_TRUE;
1492
1493                 app->main_list = eina_list_remove(app->main_list, sel_item);
1494                 app->main_list = eina_list_prepend(app->main_list, sel_item);
1495                 refresh_main_list_view(app, EINA_FALSE);
1496                 break;
1497         }
1498
1499         if (!is_user_present_in_main_list) {
1500                  peer_with_pic_s *peer_item = get_peer_info(id_to_check);
1501                  if (peer_item) {
1502                          tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
1503                          if (latest_item) {
1504                                  app->main_list = eina_list_prepend(app->main_list, latest_item);
1505                                  refresh_main_list_view(app, EINA_TRUE);
1506                          }
1507                  }
1508         }
1509
1510         if (app->s_app_visible_state == APP_STATE_IN_BACKGROUND || app->current_app_state !=  TG_USER_MAIN_VIEW_STATE) {
1511                 // show notification
1512                 char *icon_path = (char *)ui_utils_get_resource(DEFAULT_TELEGRAM_ICON);
1513                 char *title = "Telegram";
1514                 char content[512];
1515
1516                 int unread_msg_cnt = get_number_of_unread_messages();
1517
1518                 if (unread_msg_cnt <= 0) {
1519                         badge_set_count(TELEGRAM_APP_ID, 0);
1520                         return result;
1521                 }
1522
1523                 sprintf(content, "%d new messages received.", unread_msg_cnt);
1524
1525                 char *sound_track = NULL;
1526                 char *app_id = TELEGRAM_APP_ID;
1527                 tg_notification_create(app, icon_path, title, content, sound_track, app_id);
1528                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
1529                 if (BADGE_ERROR_NONE != err) {
1530
1531                 }
1532         }
1533
1534         return result;
1535 }
1536
1537 static int on_message_with_date_received_from_buddy(appdata_s *app, bundle *const rec_msg)
1538 {
1539         return on_message_received_from_buddy(app, rec_msg, EINA_TRUE);
1540 }
1541
1542 static int on_new_contact_added(appdata_s *app, bundle *const rec_msg)
1543 {
1544         int result = SVC_RES_FAIL;
1545         char* buddy_id_str = NULL;
1546         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1547         int buddy_id = atoi(buddy_id_str);
1548
1549         Eina_Bool is_success = EINA_FALSE;
1550         char* is_success_val = NULL;
1551         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1552         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1553                 is_success = EINA_TRUE;
1554         } else {
1555                 is_success = EINA_FALSE;
1556         }
1557         if (is_success) {
1558                 load_buddy_list_data(app);
1559                 load_peer_data(app);
1560         } else {
1561                 //show_toast(app, "Failed to add contact. Please try after sometime.");
1562         }
1563         hide_loading_popup(app);
1564         if (app->current_app_state == TG_ADD_CONTACT_STATE) {
1565                 on_new_contact_added_response_received(app, buddy_id, is_success);
1566         }
1567         return result;
1568 }
1569
1570 static int on_buddy_re_added_to_chat(appdata_s *app, bundle *const rec_msg)
1571 {
1572         int result = SVC_RES_FAIL;
1573         char* buddy_id_str = NULL;
1574         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1575         int buddy_id = atoi(buddy_id_str);
1576
1577         Eina_Bool is_success = EINA_FALSE;
1578         char* is_success_val = NULL;
1579         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1580         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1581                 is_success = EINA_TRUE;
1582         } else {
1583                 is_success = EINA_FALSE;
1584         }
1585
1586         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1587                 on_user_added_response_received(app, buddy_id, is_success);
1588         }
1589         hide_loading_popup(app);
1590         return result;
1591 }
1592
1593 static int on_buddy_deleted_from_friends_list(appdata_s *ad, bundle *const rec_msg)
1594 {
1595         int result = SVC_RES_FAIL;
1596         char* buddy_id_str = NULL;
1597         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1598         int buddy_id = atoi(buddy_id_str);
1599
1600         Eina_Bool is_success = EINA_FALSE;
1601         char* is_success_val = NULL;
1602         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1603         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1604                 is_success = EINA_TRUE;
1605         } else {
1606                 is_success = EINA_FALSE;
1607         }
1608
1609         if (is_success && ad->current_app_state == TG_SET_USER_INFO_STATE) {
1610                 //on_user_deleted_response_received(app, buddy_id, is_success);
1611                 show_loading_popup(ad);
1612                 elm_naviframe_item_pop(ad->nf);
1613                 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
1614                 ad->peer_in_cahtting_data = NULL;
1615                 ad->main_item_in_cahtting_data = NULL;
1616                 ad->buddy_in_cahtting_data = NULL;
1617                 elm_naviframe_item_pop(ad->nf);
1618                 load_registered_user_data(ad);
1619                 load_buddy_list_data(ad);
1620                 load_unknown_buddy_list_data(ad);
1621                 load_peer_data(ad);
1622                 load_main_list_data(ad);
1623
1624                 if (buddy_id > 0) {
1625                         Eina_List *l = NULL;
1626                         tg_main_list_item_s* main_list_item = NULL;
1627                         EINA_LIST_FOREACH(ad->main_list, l, main_list_item) {
1628                                 if (main_list_item && main_list_item->peer_id == buddy_id) {
1629                                         ad->main_list = eina_list_remove(ad->main_list, main_list_item);
1630                                         ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
1631                                         break;
1632                                 }
1633                         }
1634                 }
1635                 ecore_timer_add(1, on_load_main_view_requested, ad);
1636         } else {
1637                 hide_loading_popup(ad);
1638         }
1639         return result;
1640 }
1641
1642 static int on_buddy_blocked(appdata_s *app, bundle *const rec_msg)
1643 {
1644         int result = SVC_RES_FAIL;
1645
1646         char* buddy_id_str = NULL;
1647         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1648         int buddy_id = atoi(buddy_id_str);
1649
1650         Eina_Bool is_success = EINA_FALSE;
1651         char* is_success_val = NULL;
1652         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1653         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1654                 is_success = EINA_TRUE;
1655         } else {
1656                 is_success = EINA_FALSE;
1657         }
1658
1659         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1660                 on_user_block_response_received(app, buddy_id, is_success);
1661         }
1662         hide_loading_popup(app);
1663
1664         return result;
1665 }
1666
1667 static int on_buddy_unblocked(appdata_s *app, bundle *const rec_msg)
1668 {
1669         int result = SVC_RES_FAIL;
1670
1671         char* buddy_id_str = NULL;
1672         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1673         int buddy_id = atoi(buddy_id_str);
1674
1675         Eina_Bool is_success = EINA_FALSE;
1676         char* is_success_val = NULL;
1677         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1678         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1679                 is_success = EINA_TRUE;
1680         } else {
1681                 is_success = EINA_FALSE;
1682         }
1683
1684         if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1685                 on_user_unblock_response_received(app, buddy_id, is_success);
1686         }
1687         hide_loading_popup(app);
1688
1689         return result;
1690 }
1691
1692 static int on_delete_selected_group_chats(appdata_s *ad, bundle *const rec_msg)
1693 {
1694         int result = SVC_RES_OK;
1695 #if 0
1696         if (app->current_app_state == TG_USER_MAIN_VIEW_SELECTION_STATE) {
1697                 elm_naviframe_item_pop(app->nf);
1698                 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1699                 show_floating_button(app);
1700                 load_buddy_list_data(app);
1701                 load_unknown_buddy_list_data(app);
1702                 load_peer_data(app);
1703                 load_main_list_data(app);
1704                 refresh_main_list_view(app, EINA_FALSE);
1705         }
1706         hide_loading_popup(app);
1707 #else
1708         load_registered_user_data(ad);
1709         load_buddy_list_data(ad);
1710         load_unknown_buddy_list_data(ad);
1711         load_peer_data(ad);
1712         load_main_list_data(ad);
1713         if (ad->main_item) {
1714                 elm_naviframe_item_pop_to(ad->main_item);
1715         }
1716         ecore_timer_add(1, on_load_main_view_requested, ad);
1717 #endif
1718         return result;
1719 }
1720
1721 static int on_group_chat_deleted_response(appdata_s *app, bundle *const rec_msg)
1722 {
1723         int result = SVC_RES_FAIL;
1724
1725         char* chat_id_str = NULL;
1726         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1727         int chat_id = atoi(chat_id_str);
1728
1729         Eina_Bool is_success = EINA_FALSE;
1730         char* is_success_val = NULL;
1731         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1732         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1733                 is_success = EINA_TRUE;
1734         } else {
1735                 is_success = EINA_FALSE;
1736         }
1737
1738         if (is_success) {
1739
1740
1741                 Eina_List *l = NULL;
1742                 peer_with_pic_s *pitem = NULL;
1743                 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1744                         if (!pitem)
1745                                 continue;
1746
1747                         if (pitem->use_data->peer_id == chat_id) {
1748                                 app->peer_list = eina_list_remove(app->peer_list, pitem);
1749                                 break;
1750                         }
1751                 }
1752
1753                 l = NULL;
1754                 tg_main_list_item_s *item = NULL;
1755                 EINA_LIST_FOREACH(app->main_list, l, item) {
1756                         if (item->peer_id == chat_id) {
1757                                 app->main_list = eina_list_remove(app->main_list, item);
1758                                 break;
1759                         }
1760                 }
1761
1762                 refresh_main_list_view(app, EINA_FALSE);
1763
1764                 if (app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1765                                 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1766
1767                         app->peer_in_cahtting_data = NULL;
1768                         app->main_item_in_cahtting_data = NULL;
1769
1770                         elm_naviframe_item_pop(app->nf);
1771                         app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1772                         show_floating_button(app);
1773                 }
1774
1775                 if (app->current_app_state ==  TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1776                                                         && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1777                         on_group_chat_deleted(app, chat_id);
1778                 }
1779
1780         } else {
1781                 show_toast(app, "Failed to delete chat");
1782         }
1783         hide_loading_popup(app);
1784
1785         return result;
1786 }
1787
1788 static int on_message_sent_to_buddy(appdata_s *app, bundle *const rec_msg)
1789 {
1790         int result = SVC_RES_FAIL;
1791
1792         char* buddy_id_str = NULL;
1793         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1794         int buddy_id = atoi(buddy_id_str);
1795
1796         char* msg_id_str = NULL;
1797         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1798         int message_id = atoi(msg_id_str);
1799
1800         char* table_name = NULL;
1801         result = bundle_get_str(rec_msg, "table_name", &table_name);
1802
1803
1804         Eina_Bool is_success = EINA_FALSE;
1805         char* is_success_val = NULL;
1806         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1807         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1808                 is_success = EINA_TRUE;
1809         } else {
1810                 is_success = EINA_FALSE;
1811                 show_toast(app, "message sent failed");
1812         }
1813
1814         char* type_of_chat_str = NULL;
1815         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1816         int type_of_chat = atoi(type_of_chat_str);
1817
1818         tg_message_s* msg = get_message_from_message_table(message_id, table_name);
1819
1820         if (msg) {
1821                 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) {
1822                         // update message to sent state
1823                         on_text_message_state_changed(app, msg, type_of_chat);
1824                 }
1825
1826                 if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
1827                         Eina_List *l = NULL;
1828                         tg_main_list_item_s *sel_item = NULL;
1829                         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1830                                 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
1831                                         if (sel_item->msg_status_lbl) {
1832                                                 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_SENT_ICON), NULL);
1833                                         }
1834
1835                                         break;
1836                                 }
1837                         }
1838                 }
1839                 if (msg->message) {
1840                         free(msg->message);
1841                         msg->message = NULL;
1842                 }
1843
1844                 if (msg->media_id) {
1845                         free(msg->media_id);
1846                         msg->media_id = NULL;
1847                 }
1848                 free(msg);
1849         }
1850         return result;
1851 }
1852
1853 static int on_self_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1854 {
1855         int result = SVC_RES_FAIL;
1856         Eina_Bool is_success = EINA_FALSE;
1857         char *is_success_val = NULL;
1858         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1859         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1860                 is_success = EINA_TRUE;
1861                 show_toast(app, "profile picture updated successfully.");
1862         } else {
1863                 is_success = EINA_FALSE;
1864                 show_toast(app, "profile picture not updated.");
1865         }
1866
1867         if (is_success) {
1868                 char *file_path = NULL;
1869                 result = bundle_get_str(rec_msg, "file_path", &file_path);
1870
1871                 if (app->current_user_data->photo_path) {
1872                         free(app->current_user_data->photo_path);
1873                         app->current_user_data->photo_path = NULL;
1874                 }
1875
1876                 app->current_user_data->photo_path = strdup(file_path);
1877                 Evas_Object* profile_pic = evas_object_data_get(app->nf, "main_user_profile_pic");
1878                 if (profile_pic) {
1879                         elm_image_file_set(profile_pic, file_path, NULL);
1880                 }
1881
1882                 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE) {
1883                         Evas_Object* set_pro_pic = evas_object_data_get(app->nf, "settings_user_profile_pic");
1884                         if (set_pro_pic) {
1885                                 elm_image_file_set(set_pro_pic, file_path, NULL);
1886                         }
1887                 }
1888         }
1889         hide_loading_popup(app);
1890         return result;
1891 }
1892
1893 static int on_self_profile_name_updated(appdata_s *app, bundle *const rec_msg)
1894 {
1895         int result = SVC_RES_FAIL;
1896         Eina_Bool is_success = EINA_FALSE;
1897         char *is_success_val = NULL;
1898         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1899         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1900                 is_success = EINA_TRUE;
1901                 show_toast(app, "Username updated successfully.");
1902         } else {
1903                 is_success = EINA_FALSE;
1904                 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1905         }
1906
1907         if (is_success) {
1908
1909                 char *first_name = NULL;
1910                 result = bundle_get_str(rec_msg, "first_name", &first_name);
1911                 if (!first_name) {
1912                         first_name = "";
1913                 }
1914
1915                 char *last_name = NULL;
1916                 result = bundle_get_str(rec_msg, "last_name", &last_name);
1917                 if (!last_name) {
1918                         last_name = "";
1919                 }
1920
1921                 if (app->current_user_data->first_name) {
1922                         free(app->current_user_data->first_name);
1923                         app->current_user_data->first_name = NULL;
1924                 }
1925
1926                 if (app->current_user_data->last_name) {
1927                         free(app->current_user_data->last_name);
1928                         app->current_user_data->last_name = NULL;
1929                 }
1930
1931                 if (first_name) {
1932                         app->current_user_data->first_name = strdup(first_name);
1933                 }
1934
1935                 if (last_name) {
1936                         app->current_user_data->last_name = strdup(last_name);
1937                 }
1938
1939                 if (app->current_app_state == TG_SETTINGS_EDIT_NAME_STATE) {
1940                         elm_naviframe_item_pop(app->nf);
1941                         app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1942                         refresh_settings_screen(app);
1943                 }
1944
1945         } else {
1946
1947         }
1948         hide_loading_popup(app);
1949         return result;
1950 }
1951
1952 static int on_self_username_updated(appdata_s *app, bundle *const rec_msg)
1953 {
1954         int result = SVC_RES_FAIL;
1955         Eina_Bool is_success = EINA_FALSE;
1956         char *is_success_val = NULL;
1957         result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1958         if (strncmp("true", is_success_val, strlen("true")) == 0) {
1959                 is_success = EINA_TRUE;
1960                 show_toast(app, "Username updated successfully.");
1961         } else {
1962                 is_success = EINA_FALSE;
1963                 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1964         }
1965
1966         if (is_success) {
1967                 char *username = NULL;
1968                 result = bundle_get_str(rec_msg, "username", &username);
1969
1970                 if (app->current_user_data->username) {
1971                         free(app->current_user_data->username);
1972                         app->current_user_data->username = NULL;
1973                 }
1974
1975                 app->current_user_data->username = strdup(username);
1976
1977
1978                 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE || app->current_app_state == TG_SET_USERNAME_STATE) {
1979                         Evas_Object *name_lbl = evas_object_data_get(app->nf, "settings_user_name_label");
1980                         if (name_lbl) {
1981                                 char buf[512] = {'\0'};
1982                                 snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", app->current_user_data->username);
1983                                 elm_object_text_set(name_lbl, buf);
1984                         }
1985                 }
1986                 if (app->current_app_state == TG_SET_USERNAME_STATE) {
1987                         elm_naviframe_item_pop(app->nf);
1988                         app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1989                 }
1990         }
1991         hide_loading_popup(app);
1992
1993         return result;
1994 }
1995
1996 static int on_message_read_by_buddy(appdata_s *app, bundle *const rec_msg)
1997 {
1998         int result = SVC_RES_FAIL;
1999         char* buddy_id_str = NULL;
2000         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2001         int buddy_id = atoi(buddy_id_str);
2002
2003         char* msg_id_str = NULL;
2004         result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
2005         int message_id = atoi(msg_id_str);
2006
2007         char* table_name = NULL;
2008         result = bundle_get_str(rec_msg, "table_name", &table_name);
2009
2010         char* phone_number = NULL;
2011         result = bundle_get_str(rec_msg, "phone_number", &phone_number);
2012
2013         char* type_of_chat_str = NULL;
2014         result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
2015         int type_of_chat = atoi(type_of_chat_str);
2016
2017         tg_message_s* msg = get_message_from_message_table(message_id, table_name);
2018
2019         if (msg) {
2020                 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) {
2021                         // update message to sent state
2022                         on_text_message_state_changed(app, msg, type_of_chat);
2023                 }
2024
2025                 if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2026
2027                         Eina_List *l = NULL;
2028                         tg_main_list_item_s *sel_item = NULL;
2029                         EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2030
2031                                 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
2032
2033                                         if (sel_item->msg_status_lbl) {
2034                                                 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_READ_ICON), NULL);
2035                                         }
2036
2037                                         break;
2038                                 }
2039                         }
2040                 }
2041
2042                 if (msg->message) {
2043                         free(msg->message);
2044                         msg->message = NULL;
2045                 }
2046
2047                 if (msg->media_id) {
2048                         free(msg->media_id);
2049                         msg->media_id = NULL;
2050                 }
2051                 free(msg);
2052         }
2053
2054         return result;
2055 }
2056
2057 static int on_media_message_download_completed(appdata_s *app, bundle *const rec_msg)
2058 {
2059         LOGE("Media download14");
2060         int result = SVC_RES_FAIL;
2061         char* buddy_id_str = NULL;
2062         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2063         int buddy_id = atoi(buddy_id_str);
2064
2065         char* to_id_str = NULL;
2066         result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2067         int to_id = atoi(to_id_str);
2068
2069         char* media_id_str = NULL;
2070         result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2071         long long media_id = atoll(media_id_str);
2072
2073         char* file_name = NULL;
2074         result = bundle_get_str(rec_msg, "file_name", &file_name);
2075
2076         //file_name == "failed_to_load"
2077
2078         if (file_name && strstr(file_name, "failed_to_load") != NULL) {
2079                 // download failed.
2080                 show_toast(app, "media download failed.");
2081         }
2082
2083         char* caption = NULL;
2084         result = bundle_get_str(rec_msg, "caption", &caption);
2085
2086
2087         if (file_name && app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2088                         && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2089                 // update media to sent state
2090                 LOGE("Media download15");
2091                 on_media_download_completed(app, buddy_id, media_id, file_name, caption);
2092         }
2093         LOGE("Media download16");
2094         return result;
2095 }
2096
2097 static int on_video_message_thumb_download_completed(appdata_s *app, bundle *const rec_msg)
2098 {
2099         int result = SVC_RES_FAIL;
2100         char* buddy_id_str = NULL;
2101         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2102         int buddy_id = atoi(buddy_id_str);
2103
2104         char* to_id_str = NULL;
2105         result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2106         int to_id = atoi(to_id_str);
2107
2108         char* media_id_str = NULL;
2109         result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2110         long long media_id = atoll(media_id_str);
2111
2112         char* file_name = NULL;
2113         result = bundle_get_str(rec_msg, "file_name", &file_name);
2114
2115         char* caption = NULL;
2116         result = bundle_get_str(rec_msg, "caption", &caption);
2117
2118         if (file_name && app->current_app_state ==  TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2119                         && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2120                 // update thumbnail icon
2121                 on_video_thumb_download_completed(app, buddy_id, media_id, file_name, caption);
2122         }
2123         return result;
2124 }
2125
2126 static int on_new_buddy_added_to_contacts(appdata_s *app, bundle *const rec_msg)
2127 {
2128         int result = SVC_RES_FAIL;
2129         char* buddy_id_str = NULL;
2130         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2131
2132         int buddy_id = atoi(buddy_id_str);
2133         peer_with_pic_s *peer_item = get_peer_info(buddy_id);
2134         tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2135         if (latest_item) {
2136                 app->main_list = eina_list_prepend(app->main_list, latest_item);
2137                 refresh_main_list_view(app, EINA_TRUE);
2138         }
2139         hide_loading_popup(app);
2140
2141         return result;
2142 }
2143
2144 static int on_new_group_added(appdata_s *app, bundle *const rec_msg)
2145 {
2146         int result = SVC_RES_FAIL;
2147
2148
2149 #if 0
2150         char* chat_id_str = NULL;
2151         result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
2152
2153          int chat_id = atoi(chat_id_str);
2154          peer_with_pic_s *peer_item = get_peer_info(chat_id);
2155         if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2156                 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2157                 if (latest_item) {
2158                         app->main_list = eina_list_prepend(app->main_list, latest_item);
2159                         hide_loading_popup(app);
2160                         refresh_main_list_view(app, EINA_FALSE);
2161                 }
2162         } else {
2163
2164                 hide_loading_popup(app);
2165                 if (app->main_item) {
2166                         elm_naviframe_item_pop_to(app->main_item);
2167                         app->current_app_state = TG_USER_MAIN_VIEW_STATE;
2168                 }
2169
2170                 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2171                 if (latest_item) {
2172                         app->main_list = eina_list_prepend(app->main_list, latest_item);
2173                         refresh_main_list_view(app, EINA_FALSE);
2174                 }
2175
2176         }
2177         app->peer_list = eina_list_prepend(app->peer_list, peer_item);
2178 #else
2179         load_registered_user_data(app);
2180         load_buddy_list_data(app);
2181         load_unknown_buddy_list_data(app);
2182         load_peer_data(app);
2183         load_main_list_data(app);
2184
2185         if (app->main_item) {
2186                 elm_naviframe_item_pop_to(app->main_item);
2187         }
2188
2189         ecore_timer_add(1, on_load_main_view_requested, app);
2190 #endif
2191         return result;
2192 }
2193
2194 static int on_buddy_contact_updated(appdata_s *app, bundle *const rec_msg)
2195 {
2196         int result = SVC_RES_FAIL;
2197         char* buddy_id_str = NULL;
2198         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2199         //int buddy_id = atoi(buddy_id_str);
2200
2201         char* update_msg = NULL;
2202         result = bundle_get_str(rec_msg, "update_message", &update_msg);
2203
2204         // reload contact
2205
2206         return result;
2207 }
2208
2209 static int on_buddy_status_updated(appdata_s *app, bundle *const rec_msg)
2210 {
2211         int result = SVC_RES_FAIL;
2212         char* buddy_id_str = NULL;
2213         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2214         int buddy_id = atoi(buddy_id_str);
2215         // update to online or last seen, if current view is conversation.
2216
2217         // sandeep
2218         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) {
2219                 on_user_presence_state_changed(app, buddy_id);
2220         }
2221         return result;
2222 }
2223
2224 static int on_typing_status_updated(appdata_s *app, bundle *const rec_msg)
2225 {
2226         int result = SVC_RES_FAIL;
2227         char* buddy_id_str = NULL;
2228         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2229         int buddy_id = atoi(buddy_id_str);
2230
2231         char* buddy_name = NULL;
2232         result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2233
2234         const char *type_status_str;
2235
2236         result = bundle_get_str(rec_msg, "type_status", &type_status_str);
2237         enum tgl_typing_status type_status = atoi(type_status_str);
2238
2239         // update UI
2240         char* type_status_message = NULL;
2241
2242         switch (type_status) {
2243                 case tgl_typing_none:
2244                         type_status_message = "doing nothing";
2245                         break;
2246                 case tgl_typing_typing:
2247                         type_status_message = "typing...";
2248                         break;
2249                 case tgl_typing_cancel:
2250                         type_status_message = "deleting typed message";
2251                         break;
2252                 case tgl_typing_record_video:
2253                         type_status_message = "recording video";
2254                         break;
2255                 case tgl_typing_upload_video:
2256                         type_status_message = "uploading video";
2257                         break;
2258                 case tgl_typing_record_audio:
2259                         type_status_message = "recording audio";
2260                         break;
2261                 case tgl_typing_upload_audio:
2262                         type_status_message = "uploading audio";
2263                         break;
2264                 case tgl_typing_upload_photo:
2265                         type_status_message = "uploading photo";
2266                         break;
2267                 case tgl_typing_upload_document:
2268                         type_status_message = "uploading document";
2269                         break;
2270                 case tgl_typing_geo:
2271                         type_status_message = "choosing location";
2272                         break;
2273                 case tgl_typing_choose_contact:
2274                         type_status_message = "choosing contact";
2275                         break;
2276                 default:
2277                         type_status_message = "";
2278                         break;
2279         }
2280
2281         // update UI. may be contacts view or conversation view.
2282         if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2283                 Eina_List *l = NULL;
2284                 tg_main_list_item_s *sel_item = NULL;
2285                 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2286                         if (sel_item->peer_id != buddy_id) {
2287                                 continue;
2288                         }
2289                         // update last message
2290
2291                         if (sel_item->profile_pic) {
2292
2293                         }
2294
2295                         if (sel_item->user_name_lbl) {
2296
2297                         }
2298
2299                         if (sel_item->status_lbl) {
2300                                 char status_buf[512] = {'\0'};
2301                                 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);
2302                                 elm_object_text_set(sel_item->status_lbl, status_buf);
2303                         }
2304
2305                         if (sel_item->date_lbl) {
2306
2307                         }
2308                         if (sel_item->msg_status_lbl) {
2309
2310                         }
2311                         break;
2312                 }
2313
2314         }
2315
2316         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) {
2317                 on_user_status_changed(app, type_status_message);
2318         }
2319
2320         return result;
2321 }
2322
2323
2324 static int on_user_status_updated(appdata_s *app, bundle *const rec_msg)
2325 {
2326         int result = SVC_RES_FAIL;
2327         char* buddy_id_str = NULL;
2328         result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2329         int buddy_id = atoi(buddy_id_str);
2330
2331         char* buddy_name = NULL;
2332         result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2333
2334         if (app->current_app_state ==  TG_USER_MAIN_VIEW_STATE) {
2335                 Eina_List *l = NULL;
2336                 tg_main_list_item_s *sel_item = NULL;
2337                 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2338                         if (sel_item->peer_id != buddy_id) {
2339                                 continue;
2340                         }
2341                         // update last message
2342
2343                         if (sel_item->profile_pic) {
2344
2345                         }
2346
2347                         if (sel_item->user_name_lbl) {
2348
2349                         }
2350
2351                         if (sel_item->status_lbl) {
2352                                 char status_buf[126] = {'\0'};
2353                                 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 : "");
2354                                 elm_object_text_set(sel_item->status_lbl, status_buf);
2355                         }
2356
2357                         if (sel_item->date_lbl) {
2358
2359                         }
2360                         if (sel_item->msg_status_lbl) {
2361
2362                         }
2363
2364                         break;
2365                 }
2366         }
2367         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) {
2368                 on_user_presence_state_changed(app, buddy_id);
2369         }
2370
2371         return result;
2372 }
2373
2374 Eina_Bool on_logout_completed(void *data)
2375 {
2376         appdata_s *ad = data;
2377         if (ad) {
2378                 elm_naviframe_item_pop(ad->nf);
2379                 ad->current_app_state = TG_REGISTRATION_STATE;
2380                 launch_init_screen(ad);
2381         }
2382     return ECORE_CALLBACK_CANCEL;
2383 }
2384
2385 Eina_Bool on_start_server_requested(void *data)
2386 {
2387         appdata_s *ad = data;
2388         if (ad) {
2389                 launch_tg_server(ad);
2390         }
2391     return ECORE_CALLBACK_CANCEL;
2392 }
2393
2394
2395 static int _on_service_client_msg_received_cb(void *data, bundle *const rec_msg)
2396 {
2397         int result = SVC_RES_FAIL;
2398         RETVM_IF(!data, result, "Data is NULL");
2399         appdata_s *app = data;
2400         char *rec_key_val = NULL;
2401
2402
2403         result = bundle_get_str(rec_msg, "command", &rec_key_val);
2404
2405         LOGI("MSG recv = %s", rec_key_val);
2406
2407         if (strcmp(rec_key_val, "server_not_initialized") == 0) {
2408                 hide_loading_popup(app);
2409                 return result;
2410         } else if (strcmp(rec_key_val, "server_restart_notification") == 0) {
2411                 hide_loading_popup(app);
2412                 show_toast(app, i18n_get_text("IDS_NFC_POP_INITIALISING_PLEASE_WAIT_ING"));
2413                 ecore_timer_add(1, on_start_server_requested, app);
2414         } else if (strcmp(rec_key_val, "registration_done") == 0) {
2415
2416                 char* is_success_val = NULL;
2417                 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
2418
2419                 if (strncmp("true", is_success_val, strlen("true")) == 0) {
2420                 //show_toast(app, is_success_val);
2421                         // Launch login view
2422                         //elm_naviframe_item_pop(app->nf);
2423                         launch_login_cb(data);
2424                 } else {
2425                         // error handling
2426                         show_toast(app, "Error: registration");
2427                 }
2428                 hide_loading_popup(app);
2429                 return result;
2430         } else if (strcmp(rec_key_val, "name_registration_request") == 0) {
2431
2432                 elm_naviframe_item_pop(app->nf);
2433                 elm_naviframe_item_pop(app->nf);
2434                 launch_first_registration_cb(app);
2435                 hide_loading_popup(app);
2436                 return result;
2437         } else if (strcmp(rec_key_val, "request_reg_code_again") == 0) {
2438                 show_toast(app, "Please enter a valid code.");
2439                 hide_loading_popup(app);
2440         } else if (strcmp(rec_key_val, "request_phone_num_again") == 0) {
2441                 show_toast(app, "Please enter a valid phone number.");
2442                 hide_loading_popup(app);
2443         } else if (strcmp(rec_key_val, "server_connection_failed") == 0) {
2444                 show_toast(app, "Server connection failed. please check network connection");
2445                 hide_loading_popup(app);
2446         } else if (strcmp(rec_key_val, "logout_completed") == 0) {
2447
2448                 free_app_data(app, EINA_TRUE);
2449                 hide_loading_popup(app);
2450                 app->phone_number = NULL;
2451                 app->buddy_list = NULL;
2452                 app->unknown_buddy_list = NULL;
2453                 app->known_buddy_list = NULL;
2454                 app->main_list = NULL;
2455                 app->peer_list = NULL;
2456                 app->search_peer_list = NULL;
2457                 app->is_first_time_registration = EINA_FALSE;
2458                 app->panel = NULL;
2459                 app->loaded_msg_list = NULL;
2460                 app->loading_popup = NULL;
2461                 app->current_user_data = NULL;
2462                 app->is_tg_initilized = EINA_FALSE;
2463                 app->chat_background = NULL;
2464                 app->msg_popup = NULL;
2465                 app->s_notififcation = NULL;
2466                 app->panel = NULL;
2467                 app->is_server_ready = EINA_FALSE;
2468                 app->contact_list = NULL;
2469
2470                 char *chat_bg = NULL;
2471                 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
2472                 if (chat_bg) {
2473                         app->chat_background = strdup(chat_bg);
2474                 }
2475
2476                 if (!app->chat_background) {
2477                         app->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
2478                         preference_set_string(TG_CHAT_BG_PREFERENCE, app->chat_background);
2479                 }
2480
2481                 init_service(app);
2482
2483                 elm_naviframe_item_pop(app->nf);
2484                 //ecore_timer_add(1, on_logout_completed, app);
2485                 elm_naviframe_item_pop(app->nf);
2486                 app->current_app_state = TG_REGISTRATION_STATE;
2487                 launch_init_screen(app);
2488                 return result;
2489         } else if (strcmp(rec_key_val, "server_connection_status") == 0) {
2490                 return on_server_connection_status_changed(data, rec_msg);
2491         }
2492
2493         if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2494                 app->is_tg_initilized = EINA_TRUE;
2495         }
2496         if (!app->is_tg_initilized) {
2497                 return result;
2498         }
2499
2500         if (strcmp(rec_key_val, "contacts_load_done") == 0) {
2501
2502                 return on_contacts_loaded(data, rec_msg);
2503
2504         } else if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2505
2506                 return on_contacts_and_chats_loaded(data, rec_msg);
2507
2508         } else if (strcmp(rec_key_val, "response_group_chat_updated") == 0) {
2509
2510                 return on_group_chat_updated(data, rec_msg);
2511
2512         } else if (strcmp(rec_key_val, "group_chat_updated") == 0) {
2513
2514                 return on_group_chat_info_changed_response(data, rec_msg);
2515
2516         } else if ((strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) || (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0)) {
2517
2518                 return on_new_buddy_added_or_deleted_from_group_chat(data, rec_msg, rec_key_val);
2519
2520         } else if (strcmp(rec_key_val, "group_chat_rename_response") == 0) {
2521
2522                 return on_group_chat_name_changed(data, rec_msg);
2523
2524         } else if (strcmp(rec_key_val, "buddy_profile_pic_updated") == 0) {
2525
2526                 return on_buddy_profile_pic_updated(data, rec_msg);
2527
2528         } else if (strcmp(rec_key_val, "message_received") == 0) {
2529
2530                 return on_message_received_from_buddy(data, rec_msg, EINA_FALSE);
2531
2532         } else if (strcmp(rec_key_val, "message_received_with_date") == 0) {
2533
2534                 return on_message_with_date_received_from_buddy(data, rec_msg);
2535
2536         } else if (strcmp(rec_key_val, "buddy_readded") == 0) {
2537
2538                 return on_buddy_re_added_to_chat(data, rec_msg);
2539
2540         } else if (strcmp(rec_key_val, "new_contact_added") == 0) {
2541
2542                 return on_new_contact_added(data, rec_msg);
2543
2544         } else if (strcmp(rec_key_val, "buddy_deleted") == 0) {
2545
2546                 return on_buddy_deleted_from_friends_list(data, rec_msg);
2547
2548         } else if (strcmp(rec_key_val, "buddy_blocked") == 0) {
2549
2550                 return on_buddy_blocked(data, rec_msg);
2551
2552         } else if (strcmp(rec_key_val, "buddy_unblocked") == 0) {
2553
2554                 return on_buddy_unblocked(data, rec_msg);
2555
2556         } else if (strcmp(rec_key_val, "selected_group_chats_deleted_response") == 0) {
2557
2558                 return on_delete_selected_group_chats(data, rec_msg);
2559
2560         } else if (strcmp(rec_key_val, "group_chat_deleted_response") == 0) {
2561
2562                 return on_group_chat_deleted_response(data, rec_msg);
2563
2564         } else if (strcmp(rec_key_val, "message_sent_to_buddy") == 0) {
2565
2566                 return on_message_sent_to_buddy(data, rec_msg);
2567
2568         } else if (strcmp(rec_key_val, "self_profile_picture_updated") == 0) {
2569
2570                 return on_self_profile_pic_updated(data, rec_msg);
2571
2572         } else if (strcmp(rec_key_val, "self_profile_name_updated") == 0) {
2573
2574                 return on_self_profile_name_updated(data, rec_msg);
2575
2576         } else if (strcmp(rec_key_val, "self_username_updated") == 0) {
2577
2578                 return on_self_username_updated(data, rec_msg);
2579
2580         } else if (strcmp(rec_key_val, "message_read_by_buddy") == 0) {
2581
2582                 return on_message_read_by_buddy(data, rec_msg);
2583
2584         } else if (strcmp(rec_key_val, "media_download_completed") == 0) {
2585
2586                 return on_media_message_download_completed(data, rec_msg);
2587
2588         } else if (strcmp(rec_key_val, "video_thumb_download_completed") == 0) {
2589
2590                 return on_video_message_thumb_download_completed(data, rec_msg);
2591
2592         } else if (strcmp(rec_key_val, "add_contacts_request") == 0) {
2593                 // Not to be handled.
2594         } else if (strcmp(rec_key_val, "new_buddy_added") == 0) {
2595
2596                 return on_new_buddy_added_to_contacts(data, rec_msg);
2597
2598         } else if (strcmp(rec_key_val, "new_group_added") == 0) {
2599
2600                 return on_new_group_added(data, rec_msg);
2601
2602         } else if (strcmp(rec_key_val, "contact_updated") == 0) {
2603
2604                 return on_buddy_contact_updated(data, rec_msg);
2605
2606         } else if (strcmp(rec_key_val, "buddy_status_updated") == 0) {
2607
2608                 return on_buddy_status_updated(data, rec_msg);
2609
2610         } else if (strcmp(rec_key_val, "type_status_updated") == 0) {
2611
2612                 return on_typing_status_updated(data, rec_msg);
2613
2614         } else if (strcmp(rec_key_val, "user_status_updated") == 0) {
2615
2616                 return on_user_status_updated(data, rec_msg);
2617
2618         } else {
2619
2620         }
2621
2622         return result;
2623 }
2624
2625 int init_service(appdata_s *app)
2626 {
2627         int result = SVC_RES_FAIL;
2628         RETVM_IF(!app, result, "Application data is NULL");
2629
2630         app->service_client = service_client_create();
2631         RETVM_IF(!app->service_client, result, "Failed to create service client");
2632
2633         result = service_client_register_port(app->service_client, TELEGRAM_CLIENT_PORT_NAME);
2634         if (result != SVC_RES_OK) {
2635                 ERR("Failed to register service client port");
2636                 service_client_destroy(app->service_client);
2637                 app->service_client = NULL;
2638                 return result;
2639         }
2640
2641         result = _service_client_set_remote_data(app->service_client, TELEGRAM_SERVER_APP_NAME, TELEGRAM_SERVER_PORT_NAME);
2642
2643         result = service_client_register_msg_receive_callback(app->service_client, _on_service_client_msg_received_cb, app);
2644         if (result != SVC_RES_OK) {
2645                 ERR("Failed to register service client on message receive callback");
2646                 service_client_destroy(app->service_client);
2647                 app->service_client = NULL;
2648                 return result;
2649         }
2650         return result;
2651 }
2652
2653 void win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)
2654 {
2655         ui_app_exit();
2656 }
2657
2658
2659 tg_main_list_item_s* get_latest_item(appdata_s *ad,  peer_with_pic_s *item)
2660 {
2661         tg_main_list_item_s* main_list_item = NULL;
2662         if (!item)
2663                 return NULL;
2664
2665         tg_peer_info_s* peer_info = item->use_data;
2666         if (!peer_info)
2667                 return NULL;
2668
2669         // get message from message table.
2670
2671         char* tablename = get_table_name_from_number(peer_info->peer_id);
2672         tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2673
2674         if (msg) {
2675
2676                 if (msg->service) {
2677                         if (msg->message && strlen(msg->message) > 0) {
2678                                 item->last_message = strdup(msg->message);
2679                         } else {
2680                                 item->last_message = strdup(" ");
2681                         }
2682                 } else {
2683
2684                         int media_type = msg->media_type;
2685                         if (media_type == tgl_message_media_none) {
2686                                 if (msg->message && strlen(msg->message) > 0) {
2687                                         item->last_message = strdup(msg->message);
2688                                 } else {
2689                                         item->last_message = strdup(" ");
2690                                 }
2691                         } else if (media_type == tgl_message_media_photo) {
2692                                 item->last_message = strdup("Image");
2693                         } else if (media_type == tgl_message_media_document) {
2694                                 tgl_media_s *media_msg = NULL;
2695                                 media_msg = get_media_details_from_db(atoll(msg->media_id));
2696
2697                                 if (media_msg && media_msg->doc_type) {
2698                                         if (strlen(media_msg->doc_type) > 0) {
2699                                                 item->last_message = strdup(media_msg->doc_type);
2700                                         } else {
2701                                                 item->last_message = strdup("Document");
2702                                         }
2703                                         free_media_details(media_msg);
2704                                 } else {
2705                                         if (msg->message && strlen(msg->message) > 0) {
2706                                                 item->last_message = strdup(msg->message);
2707                                         } else {
2708                                                 item->last_message = strdup("Document");
2709                                         }
2710                                 }
2711
2712                         } else if (media_type == tgl_message_media_geo) {
2713                                 item->last_message = strdup("Geo location");
2714                         } else if (media_type == tgl_message_media_contact) {
2715                                 item->last_message = strdup("Contact");
2716                         } else if (media_type == tgl_message_media_unsupported) {
2717                                 item->last_message = strdup(" ");
2718                         } else if (media_type == tgl_message_media_photo_encr) {
2719                                 item->last_message = strdup("Image encrypted");
2720                         } else if (media_type == tgl_message_media_document_encr) {
2721                                 item->last_message = strdup("Document encrypted");
2722                         } else  {
2723                                 item->last_message = strdup(" ");
2724                         }
2725                 }
2726
2727                 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2728                 main_list_item->peer_id = peer_info->peer_id;
2729                 main_list_item->peer_type = peer_info->peer_type;
2730
2731                 set_peer_names(peer_info, main_list_item);
2732
2733                 main_list_item->last_seen_time = msg->date;
2734                 main_list_item->profile_pic = NULL;
2735                 main_list_item->last_msg_id = msg->msg_id;
2736                 main_list_item->last_message = strdup(item->last_message);
2737                 main_list_item->last_msg_type = msg->media_type;
2738                 main_list_item->is_out_msg = msg->out;
2739                 main_list_item->last_msg_status = msg->msg_state;
2740                 main_list_item->last_msg_service = msg->service;
2741                 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2742
2743                 if (peer_info->photo_path) {
2744                         main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2745                 } else {
2746                         main_list_item->profile_pic_path = NULL;
2747                 }
2748                 main_list_item->user_name_lbl = NULL;
2749                 main_list_item->status_lbl = NULL;
2750                 main_list_item->date_lbl = NULL;
2751                 main_list_item->msg_status_lbl = NULL;
2752                 main_list_item->main_item_layout = NULL;
2753
2754                 // delete message object
2755                 if (msg->message) {
2756                         free(msg->message);
2757                         msg->message = NULL;
2758                 }
2759
2760                 if (msg->media_id) {
2761                         free(msg->media_id);
2762                         msg->media_id = NULL;
2763                 }
2764
2765                 free(msg);
2766                 msg = NULL;
2767
2768         } else {
2769                 item->last_message = strdup(" ");
2770                 if (peer_info->peer_type == TGL_PEER_CHAT) {
2771                         main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2772                         main_list_item->peer_id = peer_info->peer_id;
2773                         main_list_item->peer_type = peer_info->peer_type;
2774
2775                         set_peer_names(peer_info, main_list_item);
2776
2777                         main_list_item->last_seen_time = peer_info->last_seen_time;
2778                         main_list_item->profile_pic = NULL;
2779                         main_list_item->last_message = strdup(item->last_message);
2780                         main_list_item->last_msg_type = -1;
2781                         main_list_item->last_msg_service = 0;
2782                         main_list_item->is_out_msg = -1;
2783                         main_list_item->last_msg_id = -1;
2784                         main_list_item->last_msg_status = -1;
2785                         main_list_item->number_of_unread_msgs = 0;
2786
2787                         if (peer_info->photo_path && strlen(peer_info->photo_path) > 0) {
2788                                 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2789                         } else {
2790                                 main_list_item->profile_pic_path = NULL;
2791                         }
2792                         main_list_item->user_name_lbl = NULL;
2793                         main_list_item->status_lbl = NULL;
2794                         main_list_item->date_lbl = NULL;
2795                         main_list_item->msg_status_lbl = NULL;
2796                         main_list_item->main_item_layout = NULL;
2797
2798                 }
2799         }
2800
2801         if (tablename) {
2802                 free(tablename);
2803                 tablename = NULL;
2804         }
2805
2806
2807         return main_list_item;
2808 }
2809
2810 void app_nf_back_cb(void *data, Evas_Object *obj, void *event_info)
2811 {
2812         appdata_s *ad = data;
2813         if (ad->loading_popup) {
2814                 show_toast(ad, "Please wait. request in progress");
2815                 return;
2816         }
2817
2818         switch (ad->current_app_state) {
2819                 case TG_INIT_SCREEN_STATE:
2820                         elm_win_lower(ad->win);
2821                         elm_exit();
2822                         break;
2823                 case TG_USER_MAIN_VIEW_STATE:
2824                         if (!ad->menu_popup) {
2825                                 elm_win_lower(ad->win);
2826                                 elm_exit();
2827                         }
2828                         break;
2829                 case TG_CHAT_MESSAGING_VIEW_STATE:
2830             // to be handled
2831                         if (ad->main_item_in_cahtting_data) {
2832                                 int buddy_id = ad->main_item_in_cahtting_data->peer_id;
2833                                 char* tablename = get_table_name_from_number(buddy_id);
2834
2835                                 Eina_Bool res = set_all_rows_read(tablename);
2836                                 if (!res) {
2837                                         //failed.
2838                                 }
2839                                 free(tablename);
2840
2841                                 tg_main_list_item_s* old_item = ad->main_item_in_cahtting_data;
2842                                 ad->main_list = eina_list_remove(ad->main_list,  ad->main_item_in_cahtting_data);
2843                                 if (old_item->peer_print_name) {
2844                                         free(old_item->peer_print_name);
2845                                         old_item->peer_print_name = NULL;
2846                                 }
2847                                 if (old_item->last_message) {
2848                                         free(old_item->last_message);
2849                                         old_item->last_message = NULL;
2850                                 }
2851                                 if (old_item->profile_pic_path) {
2852                                         free(old_item->profile_pic_path);
2853                                         old_item->profile_pic_path = NULL;
2854                                 }
2855                                 if (old_item->buddy_display_name) {
2856                                         free(old_item->buddy_display_name);
2857                                         old_item->buddy_display_name = NULL;
2858                                 }
2859
2860                                 old_item->date_lbl = NULL;
2861                                 old_item->msg_status_lbl = NULL;
2862                                 old_item->main_item_layout = NULL;
2863                                 old_item->profile_pic = NULL;
2864                                 old_item->profile_pic_path = NULL;
2865                                 old_item->status_lbl = NULL;
2866                                 old_item->user_name_lbl = NULL;
2867                                 ad->main_item_in_cahtting_data = NULL;
2868                                 if (ad->peer_in_cahtting_data) {
2869                                         peer_with_pic_s *item = ad->peer_in_cahtting_data;
2870                                         tg_main_list_item_s* latest_item = get_latest_item(ad, item);
2871                                         if (latest_item) {
2872                                                 ad->main_list = eina_list_prepend(ad->main_list, latest_item);
2873                                         }
2874                                 }
2875                                 refresh_main_list_view(ad, EINA_FALSE);
2876                         } else {
2877                                 if (ad->peer_in_cahtting_data) {
2878                                         peer_with_pic_s *item = ad->peer_in_cahtting_data;
2879                                         if (item) {
2880                                                 int buddy_id = item->use_data->peer_id;
2881                                                 char* tablename = get_table_name_from_number(buddy_id);
2882                                                 Eina_Bool res = set_all_rows_read(tablename);
2883                                                 if (!res) {
2884                                                         //failed.
2885                                                 }
2886                                                 free(tablename);
2887
2888                                                 tg_peer_info_s* peer_info = item->use_data;
2889                                                 if (peer_info) {
2890                                                         //if (peer_info->last_msg_id > 0) {
2891
2892                                                         // get message from message table.
2893
2894                                                         char* tablename = get_table_name_from_number(peer_info->peer_id);
2895                                                         //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
2896                                                         tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2897
2898                                                         if (msg) {
2899
2900                                                                 if (msg->service) {
2901                                                                         if (msg->message && strlen(msg->message) > 0) {
2902                                                                                 item->last_message = strdup(msg->message);
2903                                                                         } else {
2904                                                                                 item->last_message = strdup(" ");
2905                                                                         }
2906                                                                 } else {
2907
2908                                                                         int media_type = msg->media_type;
2909                                                                         if (media_type == tgl_message_media_none) {
2910                                                                                 if (msg->message && strlen(msg->message) > 0) {
2911                                                                                         item->last_message = strdup(msg->message);
2912                                                                                 } else {
2913                                                                                         item->last_message = strdup(" ");
2914                                                                                 }
2915                                                                         } else if (media_type == tgl_message_media_photo) {
2916                                                                                 item->last_message = strdup("Image");
2917                                                                         } else if (media_type == tgl_message_media_document) {
2918                                                                                 tgl_media_s *media_msg = NULL;
2919                                                                                 media_msg = get_media_details_from_db(atoll(msg->media_id));
2920
2921                                                                                 if (media_msg && media_msg->doc_type) {
2922                                                                                         if (strlen(media_msg->doc_type) > 0) {
2923                                                                                                 item->last_message = strdup(media_msg->doc_type);
2924                                                                                         } else {
2925                                                                                                 item->last_message = strdup("Document");
2926                                                                                         }
2927                                                                                         free_media_details(media_msg);
2928                                                                                 } else {
2929                                                                                         if (msg->message && strlen(msg->message) > 0) {
2930                                                                                                 item->last_message = strdup(msg->message);
2931                                                                                         } else {
2932                                                                                                 item->last_message = strdup("Document");
2933                                                                                         }
2934                                                                                 }
2935
2936                                                                         } else if (media_type == tgl_message_media_geo) {
2937                                                                                 item->last_message = strdup("Geo location");
2938                                                                         } else if (media_type == tgl_message_media_contact) {
2939                                                                                 item->last_message = strdup("Contact");
2940                                                                         } else if (media_type == tgl_message_media_unsupported) {
2941                                                                                 item->last_message = strdup(" ");
2942                                                                         } else if (media_type == tgl_message_media_photo_encr) {
2943                                                                                 item->last_message = strdup("Image encrypted");
2944                                                                         } else if (media_type == tgl_message_media_document_encr) {
2945                                                                                 item->last_message = strdup("Document encrypted");
2946                                                                         } else  {
2947                                                                                 item->last_message = strdup(" ");
2948                                                                         }
2949                                                                 }
2950
2951                                                                 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2952                                                                 main_list_item->peer_id = peer_info->peer_id;
2953                                                                 main_list_item->peer_type = peer_info->peer_type;
2954
2955                                                                 set_peer_names(peer_info, main_list_item);
2956
2957                                                                 main_list_item->last_seen_time = msg->date;
2958                                                                 main_list_item->profile_pic = NULL;
2959                                                                 main_list_item->last_msg_id = msg->msg_id;
2960                                                                 main_list_item->last_message = strdup(item->last_message);
2961                                                                 main_list_item->last_msg_type = msg->media_type;
2962                                                                 main_list_item->is_out_msg = msg->out;
2963                                                                 main_list_item->last_msg_status = msg->msg_state;
2964                                                                 main_list_item->last_msg_service = msg->service;
2965                                                                 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2966
2967                                                                 if (peer_info->photo_path) {
2968                                                                         main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2969                                                                 } else {
2970                                                                         main_list_item->profile_pic_path = NULL;
2971                                                                 }
2972                                                                 main_list_item->user_name_lbl = NULL;
2973                                                                 main_list_item->status_lbl = NULL;
2974                                                                 main_list_item->date_lbl = NULL;
2975                                                                 main_list_item->msg_status_lbl = NULL;
2976                                                                 main_list_item->main_item_layout = NULL;
2977                                                                 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
2978
2979                                                                 // delete message object
2980                                                                 if (msg->message) {
2981                                                                         free(msg->message);
2982                                                                         msg->message = NULL;
2983                                                                 }
2984
2985                                                                 if (msg->media_id) {
2986                                                                         free(msg->media_id);
2987                                                                         msg->media_id = NULL;
2988                                                                 }
2989
2990                                                                 free(msg);
2991                                                                 msg = NULL;
2992
2993                                                         } else {
2994                                                                 item->last_message = strdup(" ");
2995                                                                 if (peer_info->peer_type == TGL_PEER_CHAT) {
2996                                                                         tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2997                                                                         main_list_item->peer_id = peer_info->peer_id;
2998                                                                         main_list_item->peer_type = peer_info->peer_type;
2999
3000                                                                         set_peer_names(peer_info, main_list_item);
3001
3002                                                                         main_list_item->last_seen_time = peer_info->last_seen_time;
3003                                                                         main_list_item->profile_pic = NULL;
3004                                                                         main_list_item->last_message = strdup(item->last_message);
3005                                                                         main_list_item->last_msg_type = -1;
3006                                                                         main_list_item->last_msg_service = 0;
3007                                                                         main_list_item->is_out_msg = -1;
3008                                                                         main_list_item->last_msg_id = -1;
3009                                                                         main_list_item->last_msg_status = -1;
3010                                                                         main_list_item->number_of_unread_msgs = 0;
3011
3012                                                                         if (peer_info->photo_path) {
3013                                                                                 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
3014                                                                         } else {
3015                                                                                 main_list_item->profile_pic_path = NULL;
3016                                                                         }
3017                                                                         main_list_item->user_name_lbl = NULL;
3018                                                                         main_list_item->status_lbl = NULL;
3019                                                                         main_list_item->date_lbl = NULL;
3020                                                                         main_list_item->msg_status_lbl = NULL;
3021                                                                         main_list_item->main_item_layout = NULL;
3022                                                                         ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
3023                                                                 }
3024                                                         }
3025
3026                                                         if (tablename) {
3027                                                                 free(tablename);
3028                                                                 tablename = NULL;
3029                                                         }
3030                                                         //}
3031                                                 }
3032                                         }
3033                                         refresh_main_list_view(ad, EINA_TRUE);
3034                                 }
3035                         }
3036
3037                         elm_naviframe_item_pop(ad->nf);
3038                         ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3039                         show_floating_button(ad);
3040                         break;
3041                 case TG_SELECT_BUDDY_VIEW:
3042                         elm_naviframe_item_pop(ad->nf);
3043                         ad->current_app_state = TG_SET_CHAT_INFO_STATE;
3044                         break;
3045                 case TG_SET_USERNAME_STATE:
3046                         elm_naviframe_item_pop(ad->nf);
3047                         ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3048                         break;
3049 #if 0
3050                 case TG_ADD_CONTACT_STATE:
3051                         elm_naviframe_item_pop(ad->nf);
3052                         ad->current_app_state = TG_SET_USER_INFO_STATE;
3053                         break;
3054 #endif
3055                 case TG_SET_CHAT_INFO_STATE:
3056                 case TG_SET_USER_INFO_STATE:
3057                         elm_naviframe_item_pop(ad->nf);
3058                         ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3059                         break;
3060                 case TG_USER_MAIN_VIEW_SELECTION_STATE:
3061                 case TG_PEER_SEARCH_VIEW_STATE:
3062                 case TG_START_MESSAGING_VIEW_STATE:
3063                 case TG_SETTINGS_SCREEN_STATE:
3064                         elm_naviframe_item_pop(ad->nf);
3065                         ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3066                         show_floating_button(ad);
3067                         //evas_object_show(ad->panel);
3068                         //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3069                         break;
3070                 case TG_SETTINGS_EDIT_NAME_STATE:
3071                         elm_naviframe_item_pop(ad->nf);
3072                         ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3073                         delete_floating_button(ad);
3074                         break;
3075                 case TG_COUNTRY_SELECTION_VIEW:
3076                         elm_naviframe_item_pop(ad->nf);
3077                         ad->current_app_state = TG_REGISTRATION_STATE;
3078                         break;
3079                 case TG_REGISTRATION_STATE:
3080                         elm_win_lower(ad->win);
3081                         elm_exit();
3082                         break;
3083                 case TG_LOGIN_STATE:
3084                         send_request_for_restart_server(ad, ad->service_client);
3085                         if (ad->timer_value > 0) {
3086                                 Ecore_Timer* timer = evas_object_data_get(ad->nf, "code_timer");
3087                                 if (timer)
3088                                         ecore_timer_del(timer);
3089                         }
3090
3091                         elm_naviframe_item_pop(ad->nf);
3092                         ad->current_app_state = TG_REGISTRATION_STATE;
3093                         break;
3094                 case TG_PROFILE_REGISTRATION_STATE:
3095                         /*                      ad->current_app_state = TG_REGISTRATION_STATE;
3096                                                 elm_naviframe_item_pop(ad->nf);*/
3097                         elm_win_lower(ad->win);
3098                         elm_exit();
3099                         break;
3100                 case TG_BUDDY_LIST_STATE:
3101                         evas_object_data_set(ad->nf, "buddy_list", NULL);
3102                         elm_win_lower(ad->win);
3103                         elm_exit();
3104                         break;
3105                 case TG_BUDDY_CHAT_CONV_STATE:
3106                         ad->buddy_in_cahtting_data = NULL;
3107                         if (ad->loaded_msg_list) {
3108                                 eina_list_free(ad->loaded_msg_list);
3109                                 ad->loaded_msg_list = NULL;
3110                         }
3111                         elm_naviframe_item_pop(ad->nf);
3112                         ad->current_app_state = TG_BUDDY_LIST_STATE;
3113                         //evas_object_show(ad->panel);
3114                         //refresh_buddy_list(ad);
3115                         break;
3116                 case TG_ADD_CONTACT_STATE:
3117                         elm_naviframe_item_pop(ad->nf);
3118                         if (ad->is_loading_from_profile_view) {
3119                                 ad->is_loading_from_profile_view = EINA_FALSE;
3120                                 ad->current_app_state = TG_SET_USER_INFO_STATE;
3121                         } else if (ad->is_loading_from_msg_view) {
3122                                 ad->is_loading_from_msg_view = EINA_FALSE;
3123                                 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3124                         } else {
3125                                 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3126                                 show_floating_button(ad);
3127                         }
3128                         break;
3129                 case TG_BUDDY_LIST_SELECTION_STATE:
3130                 {
3131                         Eina_List *l = NULL;
3132                         user_data_with_pic_s *item = NULL;
3133                         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3134                                 if (!item)
3135                                         continue;
3136                                 user_data_s* user = item->use_data;
3137                                 user->is_selected = EINA_FALSE;
3138                         }
3139                         elm_naviframe_item_pop(ad->nf);
3140                         ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3141                 }
3142                         break;
3143                 case TG_GROUP_CHAT_NAME_ENTRY_STATE:
3144                 {
3145                         Eina_List *l = NULL;
3146                         user_data_with_pic_s *item = NULL;
3147                         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3148                                 if (!item)
3149                                         continue;
3150
3151                                 user_data_s* user = item->use_data;
3152                                 user->is_selected = EINA_FALSE;
3153                         }
3154                         elm_naviframe_item_pop(ad->nf);
3155                         ad->current_app_state = TG_BUDDY_LIST_SELECTION_STATE;
3156                         //evas_object_show(ad->panel);
3157                         //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3158                         //refresh_buddy_list(ad);
3159                 }
3160                         break;
3161                 default:
3162                         break;
3163         }
3164 }
3165
3166 void
3167 layout_back_cb(void *data, Evas_Object *obj, void *event_info)
3168 {
3169         appdata_s *ad = data;
3170         /* Let window go to hide state. */
3171         elm_win_lower(ad->win);
3172 }
3173
3174 void _btn_clicked(void* data, Evas_Object* btn, void* ev)
3175 {
3176         appdata_s *ad = data;
3177         bundle *msg = bundle_create();
3178         if (bundle_add_str(msg, "tizen_app", "Hello service") != 0) {
3179                 ERR("Failed to add data by key to bundle");
3180                 bundle_free(msg);
3181         }
3182
3183         int result = _app_send_response(ad, msg);
3184         if (result != SVC_RES_OK) {
3185                 bundle_free(msg);
3186         }
3187         bundle_free(msg);
3188 }
3189
3190 Eina_Bool on_load_main_view_requested(void *data)
3191 {
3192         appdata_s *ad = data;
3193         if (ad) {
3194                 elm_naviframe_item_pop(ad->nf);
3195                 hide_loading_popup(ad);
3196                 ad->is_loading_from_msg_view = EINA_FALSE;
3197                 ad->is_loading_from_profile_view = EINA_FALSE;
3198                 launch_user_main_view_cb(ad);
3199                 int unread_msg_cnt = get_number_of_unread_messages();
3200                 if (unread_msg_cnt <= 0) {
3201                         badge_set_count(TELEGRAM_APP_ID, 0);
3202                         return ECORE_CALLBACK_CANCEL;
3203                 }
3204
3205                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3206                 if (BADGE_ERROR_NONE != err) {
3207
3208                 }
3209         }
3210     return ECORE_CALLBACK_CANCEL;
3211 }
3212
3213 Eina_Bool on_init_view_requested(void *data)
3214 {
3215         appdata_s *ad = data;
3216         if (ad) {
3217                 Eina_List *user_info = get_registered_user_info();
3218                 if (!user_info) {
3219                         elm_naviframe_item_pop(ad->nf);
3220                         ad->current_app_state = TG_REGISTRATION_STATE;
3221                         launch_init_screen(ad);
3222                 } else {
3223                         //show_toast(ad, "user already registered");
3224                         load_registered_user_data(ad);
3225                         load_buddy_list_data(ad);
3226                         load_unknown_buddy_list_data(ad);
3227                         load_peer_data(ad);
3228                         load_main_list_data(ad);
3229                         eina_list_free(user_info);
3230                         ecore_timer_add(2, on_load_main_view_requested, ad);
3231                 }
3232         }
3233     return ECORE_CALLBACK_CANCEL;
3234 }
3235
3236 void on_tg_service_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
3237 {
3238         appdata_s *ad = user_data;
3239         if (result == APP_CONTROL_RESULT_SUCCEEDED) {
3240                 if (ad) {
3241                         show_toast(ad, "Server launched successfully.");
3242                 } else {
3243                         show_toast(ad, "Server not launched.");
3244                 }
3245         }
3246 }
3247
3248 void launch_tg_server(void *data)
3249 {
3250         appdata_s *ad = data;
3251         app_control_h app_control = NULL;
3252         int ret = APP_CONTROL_ERROR_NONE;
3253
3254         ret = app_control_create(&app_control);
3255         ret_if (APP_CONTROL_ERROR_NONE != ret);
3256
3257         ret = app_control_set_app_id(app_control, TELEGRAM_SERVER_APP_NAME);
3258         goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3259
3260         ret = app_control_send_launch_request(app_control, &on_tg_service_result_cb, ad);
3261         goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3262
3263 out:
3264         app_control_destroy(app_control);
3265 }
3266
3267 static void create_base_gui(appdata_s *ad)
3268 {
3269         ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
3270         elm_win_conformant_set(ad->win, EINA_TRUE);
3271         elm_win_autodel_set(ad->win, EINA_TRUE);
3272
3273         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
3274         /* Telegram guideline 720 x 1280 */
3275         elm_app_base_scale_set(2.6);
3276
3277         if (elm_win_wm_rotation_supported_get(ad->win)) {
3278                 int rots[2] = {0, 180};
3279                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), sizeof(rots) / sizeof(int));
3280         }
3281         ad->target_direction = tg_get_device_orientation();
3282         char edj_path[PATH_MAX] = {0, };
3283         app_get_resource(TELEGRAM_CUSTOM_WINSET_EDJ, edj_path, (int)PATH_MAX);
3284
3285         Elm_Theme *theme = elm_theme_new();
3286         elm_theme_extension_add(NULL, edj_path);
3287         ad->theme = theme;
3288         evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
3289
3290         ad->conform = elm_conformant_add(ad->win);
3291         evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3292         elm_win_resize_object_add(ad->win, ad->conform);
3293         evas_object_show(ad->conform);
3294
3295         ad->layout = elm_layout_add(ad->conform);
3296         evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3297
3298         //elm_layout_theme_set(ad->layout, "layout", "drawer", "panel");
3299         elm_layout_theme_set(ad->layout, "layout", "application", "default");
3300         evas_object_show(ad->layout);
3301
3302         elm_object_content_set(ad->conform, ad->layout);
3303         ad->nf = elm_naviframe_add(ad->layout);
3304
3305         ad->current_app_state = TG_SPLASH_SCREEN_STATE;
3306         launch_splash_screen_cb(ad);
3307         elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
3308         eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, app_nf_back_cb, ad);
3309
3310         evas_object_show(ad->win);
3311
3312         // check for user info
3313
3314         //
3315         char *chat_bg = NULL;
3316         preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
3317         if (chat_bg) {
3318                 ad->chat_background = strdup(chat_bg);
3319         }
3320
3321         if (!ad->chat_background) {
3322                 ad->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
3323                 preference_set_string(TG_CHAT_BG_PREFERENCE, ad->chat_background);
3324         }
3325         launch_tg_server(ad);
3326         ecore_timer_add(5, on_init_view_requested, ad);
3327         ucol_init();
3328 }
3329
3330
3331 char *build_a_path(const char *path, const char *filename)
3332 {
3333         char *ret;
3334         int len;
3335
3336         len = strlen(path) + strlen(filename) + 2;
3337         ret = malloc(len);
3338         if (!ret) {
3339                 return NULL;
3340         }
3341
3342         snprintf(ret, len, "%s/%s", path, filename);
3343         return ret;
3344 }
3345
3346 int remove_directory(const char *path)
3347 {
3348         DIR *d = opendir(path);
3349         size_t path_len = strlen(path);
3350         int r = -1;
3351
3352         if (d)
3353         {
3354                 struct dirent *p;
3355
3356                 r = 0;
3357
3358                 while (!r && (p=readdir(d)))
3359                 {
3360                         int r2 = -1;
3361                         char *buf;
3362                         size_t len;
3363
3364                         /* Skip the names "." and ".." as we don't want to recurse on them. */
3365                         if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
3366                         {
3367                                 continue;
3368                         }
3369
3370                         len = path_len + strlen(p->d_name) + 2;
3371                         buf = malloc(len);
3372
3373                         if (buf)
3374                         {
3375                                 struct stat statbuf;
3376
3377                                 snprintf(buf, len, "%s/%s", path, p->d_name);
3378
3379                                 if (!stat(buf, &statbuf))
3380                                 {
3381                                         if (S_ISDIR(statbuf.st_mode))
3382                                         {
3383                                                 r2 = remove_directory(buf);
3384                                         }
3385                                         else
3386                                         {
3387                                                 r2 = unlink(buf);
3388                                         }
3389                                 }
3390
3391                                 free(buf);
3392                         }
3393
3394                         r = r2;
3395                 }
3396
3397                 closedir(d);
3398         }
3399
3400         if (!r)
3401         {
3402                 r = rmdir(path);
3403         }
3404
3405         return r;
3406 }
3407
3408 Eina_Bool copy_file_another_dir(const char *source_file, const char *target_file)
3409 {
3410         FILE *source, *target;
3411
3412         source = fopen(source_file, "rb+");
3413
3414         if (source == NULL)
3415         {
3416                 return EINA_FALSE;
3417         }
3418
3419         target = fopen(target_file, "wb+");
3420
3421         if (target == NULL)
3422         {
3423                 fclose(source);
3424                 return EINA_FALSE;
3425         }
3426
3427         char buffer[1024] = {0};    /*Buffer to store files content*/
3428
3429         int size = 0;
3430         while((size = fread(buffer, 1, 1024, source)) != 0)
3431         {
3432                 fwrite(buffer, 1, size, target);
3433         }
3434
3435         fclose(source);
3436         fclose(target);
3437         return EINA_TRUE;
3438 }
3439
3440 void recursive_dir_copy(const char *source_dir, const char *target_dir)
3441 {
3442         DIR *open_src;
3443         struct dirent *src_files;
3444         Eina_List *src_dir_stack;
3445         Eina_List *tar_dir_stack;
3446
3447         char *src_dir;
3448         char *tar_dir;
3449
3450
3451         src_dir_stack = NULL;
3452         tar_dir_stack = NULL;
3453
3454         src_dir = strdup(source_dir);
3455         tar_dir = strdup(target_dir);
3456
3457         do {
3458                 open_src = opendir(src_dir);
3459                 while((src_files = readdir(open_src))) {
3460                         if (src_files->d_type == DT_DIR) {
3461                                 // create directory
3462                                 // push to stack
3463                                 if (src_files->d_name[0] == '.' && (src_files->d_name[1] == '\0' || (src_files->d_name[1] == '.' && src_files->d_name[2] == '\0'))) {
3464                                         continue;
3465                                 }
3466
3467                                 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3468                                 mkdir(temp_dest, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3469
3470                                 char *temp_src = build_a_path(src_dir, src_files->d_name);
3471
3472                                 src_dir_stack = eina_list_prepend(src_dir_stack, temp_src);
3473                                 tar_dir_stack = eina_list_prepend(tar_dir_stack, temp_dest);
3474
3475                         } else {
3476                                 // copy file to target src_dir.
3477                                 char *temp_src = build_a_path(src_dir, src_files->d_name);
3478                                 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3479                                 //int ret = rename(temp_src, temp_dest);
3480                                 Eina_Bool ret = copy_file_another_dir(temp_src, temp_dest);
3481                                 if (ret) {
3482                                         // file copy success
3483                                 } else {
3484                                         // file copy failed.
3485                                 }
3486                                 free(temp_src);
3487                                 free(temp_dest);
3488                         }
3489                 }
3490
3491                 if (open_src) {
3492                         closedir(open_src);
3493                 }
3494
3495                 free(src_dir);
3496                 free(tar_dir);
3497                 src_dir = eina_list_nth(src_dir_stack, 0);
3498                 if (src_dir) {
3499                         src_dir_stack = eina_list_remove(src_dir_stack, src_dir);
3500                 }
3501                 tar_dir = eina_list_nth(tar_dir_stack, 0);
3502                 if (tar_dir) {
3503                         tar_dir_stack = eina_list_remove(tar_dir_stack, tar_dir);
3504                 }
3505         } while (src_dir != NULL);
3506 }
3507
3508 void move_downloaded_files()
3509 {
3510         // create download directory.
3511         struct stat st = {0};
3512         char dest_download_dir[512] = {0,};
3513         sprintf(dest_download_dir, "%s/%s", app_get_shared_data_path(), "downloads");
3514         if (stat(dest_download_dir, &st) == -1) {
3515                 mkdir(dest_download_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3516         }
3517
3518         LOGE("dest Download directory: %s", dest_download_dir);
3519         // create download directory.
3520         char target_download_dir[512] = {0,};
3521         sprintf(target_download_dir, "%s/%s/%s", app_get_data_path(), "telegram_tizen", "downloads");
3522
3523         LOGE("target Download directory: %s", target_download_dir);
3524         recursive_dir_copy(target_download_dir, dest_download_dir);
3525         remove_directory(target_download_dir);
3526 }
3527
3528
3529 Eina_Bool dirExists(const char *path)
3530 {
3531     struct stat info;
3532
3533     if (stat(path, &info) != 0)
3534         return EINA_FALSE;
3535     else if (info.st_mode & S_IFDIR)
3536         return EINA_TRUE;
3537     else
3538         return EINA_FALSE;
3539 }
3540
3541 static void update_downloaded_video_thumb_paths(const char *table_name, const char *col_one, const char *column_two)
3542 {
3543         LOGE("Update media info table thumb files");
3544         if (!table_name || !col_one || !column_two)
3545                 return;
3546
3547         Eina_List *col_types = NULL;
3548         col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER);
3549         col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT);
3550
3551         Eina_List *col_names = NULL;
3552         col_names = eina_list_append(col_names, col_one);
3553         col_names = eina_list_append(col_names, column_two);
3554
3555         Eina_List *vals = get_values_from_table_sync(table_name, col_names, col_types, NULL, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED);
3556         int val_size = eina_list_count(vals);
3557         if (vals && val_size > 0) {
3558                 Eina_List *row_vals = NULL;
3559                 EINA_LIST_FREE(vals, row_vals) {
3560                         int user_id = 0;
3561                         char *file_path = NULL;
3562
3563                         int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
3564                         if (temp_user_id) {
3565                                 user_id  = *temp_user_id;
3566                                 free(temp_user_id);
3567                         }
3568
3569                         file_path = (char *)eina_list_nth(row_vals, 1);
3570
3571                         if (!file_path) {
3572                                 eina_list_free(row_vals);
3573                                 continue;
3574                         }
3575
3576                         if (strlen(file_path) > 0 && strncmp(file_path, "/opt/usr/media/telegram/telegram_tizen", strlen("/opt/usr/media/telegram/telegram_tizen")) == 0) {
3577                                 // update file name in db
3578                                 char *new_path = str_replace(new_path, "/opt/usr/media/telegram/telegram_tizen", app_get_shared_data_path());
3579                                 if (!new_path) {
3580                                         free(file_path);
3581                                         eina_list_free(row_vals);
3582                                         continue;
3583                                 }
3584                                 LOGE("Old file path: %s", file_path);
3585                                 LOGE("New file path: %s", new_path);
3586
3587                                 //update database.
3588                                 Eina_List *new_col_types = NULL;
3589                                 new_col_types = eina_list_append(new_col_types, TG_DB_COLUMN_TEXT);
3590
3591                                 Eina_List *new_col_names = NULL;
3592                                 new_col_names = eina_list_append(new_col_names, column_two);
3593
3594                                 Eina_List *new_col_values = NULL;
3595                                 new_col_values = eina_list_append(new_col_values, new_path);
3596
3597                                 char* where_clause = NULL;
3598
3599                                 int str_size = strlen(col_one) + strlen(" = ") + 10 + 1;
3600                                 where_clause = (char *)malloc(str_size);
3601                                 snprintf(where_clause, str_size, "%s = %d", col_one, user_id);
3602                                 Eina_Bool ret = update_table(table_name, new_col_names, new_col_types, new_col_values, where_clause);
3603                                 if (!ret) {
3604                                         LOGE("Media info table update failed.");
3605                                 } else {
3606                                         LOGE("Media info table update success.");
3607                                 }
3608                                 free(where_clause);
3609                                 eina_list_free(new_col_types);
3610                                 eina_list_free(new_col_names);
3611                                 eina_list_free(new_col_values);
3612                         }
3613
3614                         free(file_path);
3615
3616                         eina_list_free(row_vals);
3617                 }
3618         }
3619         eina_list_free(col_types);
3620         eina_list_free(col_names);
3621 }
3622
3623 void update_downloaded_video_thumb_paths_in_media_info_table()
3624 {
3625         update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_DOCUMENT_THUMB_FILE);
3626 }
3627
3628
3629 void update_downloaded_file_paths_in_media_info_table()
3630 {
3631         update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_FILE_PATH);
3632 }
3633
3634
3635 void update_downloaded_file_paths_in_chat_info_table()
3636 {
3637         update_downloaded_video_thumb_paths(CHAT_INFO_TABLE_NAME, CHAT_INFO_TABLE_CHAT_ID, CHAT_INFO_TABLE_PHOTO_PATH);
3638
3639 }
3640
3641 void update_downloaded_file_paths_in_buddy_info_table(const char *table_name)
3642 {
3643         LOGE("Update %s", table_name);
3644         update_downloaded_video_thumb_paths(table_name, USER_INFO_TABLE_USER_ID, USER_INFO_TABLE_PHOTO_PATH);
3645 }
3646
3647 static bool app_create(void *data)
3648 {
3649         /*
3650            Hook to take necessary actions before main event loop starts
3651            Initialize UI resources and application's data
3652            If this function returns true, the main loop of application starts
3653            If this function returns false, the application is terminated
3654          */
3655
3656         appdata_s *ad = data;
3657         if (dirExists(OLD_DIR)) {
3658                 char *new_dir = app_get_data_path();
3659                 recursive_dir_copy(OLD_DIR, new_dir);
3660                 remove_directory(OLD_DIR);
3661                 move_downloaded_files();
3662                 // update downloaded image paths in DB
3663                 update_downloaded_file_paths_in_buddy_info_table(BUDDY_INFO_TABLE_NAME);
3664                 update_downloaded_file_paths_in_buddy_info_table(USER_INFO_TABLE_NAME);
3665                 update_downloaded_file_paths_in_buddy_info_table(PEER_INFO_TABLE_NAME);
3666                 update_downloaded_file_paths_in_chat_info_table();
3667                 update_downloaded_file_paths_in_media_info_table();
3668                 update_downloaded_video_thumb_paths_in_media_info_table();
3669         }
3670
3671         tg_db_init();
3672         bindtextdomain(PACKAGE, "/opt/usr/apps/org.tizen.telegram_tizen/res/locale");
3673
3674         ad->phone_number = NULL;
3675         ad->buddy_list = NULL;
3676         ad->unknown_buddy_list = NULL;
3677         ad->known_buddy_list = NULL;
3678         ad->main_list = NULL;
3679         ad->peer_list = NULL;
3680         ad->search_peer_list = NULL;
3681         ad->is_first_time_registration = EINA_FALSE;
3682         ad->panel = NULL;
3683         ad->loaded_msg_list = NULL;
3684         ad->loading_popup = NULL;
3685         ad->current_user_data = NULL;
3686         ad->is_tg_initilized = EINA_FALSE;
3687         ad->chat_background = NULL;
3688         ad->msg_popup = NULL;
3689         ad->s_notififcation = NULL;
3690         ad->panel = NULL;
3691         ad->is_server_ready = EINA_FALSE;
3692         ad->contact_list = NULL;
3693         ad->main_item = NULL;
3694         ad->country_codes_list = NULL;
3695         ad->country_names_list = NULL;
3696         //ad->msg_count = 0;
3697         create_base_gui(ad);
3698         int err = badge_new(TELEGRAM_APP_ID);
3699         if (BADGE_ERROR_NONE != err) {
3700
3701         }
3702         init_service(ad);
3703         return true;
3704 }
3705
3706 static void
3707 app_control(app_control_h app_control, void *data)
3708 {
3709         /* Handle the launch request. */
3710 }
3711
3712 static void
3713 app_pause(void *data)
3714 {
3715         appdata_s *app_data = data;
3716         if (app_data) {
3717                 app_data->s_app_visible_state = APP_STATE_IN_BACKGROUND;
3718                 int unread_msg_cnt = get_number_of_unread_messages();
3719                 if (unread_msg_cnt <= 0) {
3720                         badge_set_count(TELEGRAM_APP_ID, 0);
3721                         return;
3722                 }
3723                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3724                 if (BADGE_ERROR_NONE != err) {
3725
3726                 }
3727         }
3728 }
3729
3730 static void
3731 app_resume(void *data)
3732 {
3733         appdata_s *app_data = data;
3734         if (app_data) {
3735                 app_data->s_app_visible_state = APP_STATE_IN_FOREGROUND;
3736         }
3737 }
3738
3739
3740 void free_app_data(appdata_s *app_data, Eina_Bool destroy_server)
3741 {
3742         if (!app_data) {
3743                 return;
3744         }
3745
3746         int unread_msg_cnt = get_number_of_unread_messages();
3747         if (unread_msg_cnt > 0) {
3748                 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3749                 if (BADGE_ERROR_NONE != err) {
3750
3751                 }
3752         } else {
3753                 badge_set_count(TELEGRAM_APP_ID, 0);
3754         }
3755         eina_list_free(app_data->country_codes_list);
3756         eina_list_free(app_data->country_names_list);
3757         free(app_data->country_code_buffer);
3758
3759         if (app_data->panel) {
3760                 Evas_Object *panel_list = evas_object_data_get(app_data->panel, "panel_list");
3761                 if (panel_list) {
3762                         evas_object_del(panel_list);
3763                 }
3764                 evas_object_del(app_data->panel);
3765                 app_data->panel = NULL;
3766         }
3767
3768         if (app_data->phone_number) {
3769                 free(app_data->phone_number);
3770                 app_data->phone_number = NULL;
3771         }
3772
3773         if (app_data->sms_code) {
3774                 free(app_data->sms_code);
3775                 app_data->sms_code = NULL;
3776         }
3777
3778         if (app_data->current_user_data) {
3779                 free_user_data(app_data->current_user_data);
3780         }
3781         if (app_data->chat_background) {
3782                 app_data->chat_background = NULL;
3783         }
3784         if (app_data && app_data->s_notififcation) {
3785                 notification_delete_all(NOTIFICATION_TYPE_NOTI);
3786                 app_data->s_notififcation = NULL;
3787         }
3788
3789         peer_with_pic_s* pic_item = NULL;
3790         EINA_LIST_FREE(app_data->peer_list, pic_item) {
3791                 if (!pic_item)
3792                         continue;
3793                 tg_peer_info_s* item = pic_item->use_data;
3794                 if (item) {
3795                         if (item->print_name) {
3796                                 free(item->print_name);
3797                                 item->print_name = NULL;
3798                         }
3799                         if (item->photo_path) {
3800                                 free(item->photo_path);
3801                                 item->photo_path = NULL;
3802                         }
3803                         pic_item->contact_icon = NULL;
3804                         pic_item->msg_object = NULL;
3805                         pic_item->name_object = NULL;
3806                         free(item);
3807                 }
3808                 free(pic_item);
3809         }
3810         app_data->peer_list = NULL;
3811
3812         tg_main_list_item_s* main_item = NULL;
3813         EINA_LIST_FREE(app_data->main_list, main_item) {
3814
3815                 if (main_item->peer_print_name) {
3816                         free(main_item->peer_print_name);
3817                         main_item->peer_print_name = NULL;
3818                 }
3819                 if (main_item->last_message) {
3820                         free(main_item->last_message);
3821                         main_item->last_message = NULL;
3822                 }
3823                 if (main_item->profile_pic_path) {
3824                         free(main_item->profile_pic_path);
3825                         main_item->profile_pic_path = NULL;
3826                 }
3827                 if (main_item->buddy_display_name) {
3828                         free(main_item->buddy_display_name);
3829                         main_item->buddy_display_name = NULL;
3830                 }
3831                 main_item->date_lbl = NULL;
3832                 main_item->msg_status_lbl = NULL;
3833                 main_item->main_item_layout = NULL;
3834                 main_item->profile_pic = NULL;
3835                 main_item->profile_pic_path = NULL;
3836                 main_item->status_lbl = NULL;
3837                 main_item->user_name_lbl = NULL;
3838         }
3839         app_data->main_list = NULL;
3840
3841         user_data_with_pic_s* item = NULL;
3842         EINA_LIST_FREE(app_data->buddy_list, item) {
3843                 if (!item)
3844                         continue;
3845                 user_data_s* user_data = item->use_data;
3846
3847                 if (user_data->print_name) {
3848                         free(user_data->print_name);
3849                         user_data->print_name = NULL;
3850                 }
3851
3852                 if (user_data->photo_path) {
3853                         free(user_data->photo_path);
3854                         user_data->photo_path = NULL;
3855                 }
3856
3857                 if (user_data->first_name) {
3858                         free(user_data->first_name);
3859                         user_data->first_name = NULL;
3860                 }
3861
3862                 if (user_data->last_name) {
3863                         free(user_data->last_name);
3864                         user_data->last_name = NULL;
3865                 }
3866
3867                 if (user_data->phone) {
3868                         free(user_data->phone);
3869                         user_data->phone = NULL;
3870                 }
3871                 if (user_data->real_first_name) {
3872                         free(user_data->real_first_name);
3873                         user_data->real_first_name = NULL;
3874                 }
3875
3876                 if (user_data->real_last_name) {
3877                         free(user_data->real_last_name);
3878                         user_data->real_last_name = NULL;
3879                 }
3880
3881                 if (user_data->username) {
3882                         free(user_data->username);
3883                         user_data->username = NULL;
3884                 }
3885                 free(user_data);
3886                 free(item);
3887         }
3888         app_data->buddy_list = NULL;
3889
3890
3891         EINA_LIST_FREE(app_data->unknown_buddy_list, item) {
3892
3893                 if (!item)
3894                         continue;
3895
3896                 user_data_s* user_data = item->use_data;
3897
3898                 if (user_data->print_name) {
3899                         free(user_data->print_name);
3900                         user_data->print_name = NULL;
3901                 }
3902
3903                 if (user_data->photo_path) {
3904                         free(user_data->photo_path);
3905                         user_data->photo_path = NULL;
3906                 }
3907
3908                 if (user_data->first_name) {
3909                         free(user_data->first_name);
3910                         user_data->first_name = NULL;
3911                 }
3912
3913                 if (user_data->last_name) {
3914                         free(user_data->last_name);
3915                         user_data->last_name = NULL;
3916                 }
3917
3918                 if (user_data->phone) {
3919                         free(user_data->phone);
3920                         user_data->phone = NULL;
3921                 }
3922                 if (user_data->real_first_name) {
3923                         free(user_data->real_first_name);
3924                         user_data->real_first_name = NULL;
3925                 }
3926
3927                 if (user_data->real_last_name) {
3928                         free(user_data->real_last_name);
3929                         user_data->real_last_name = NULL;
3930                 }
3931
3932                 if (user_data->username) {
3933                         free(user_data->username);
3934                         user_data->username = NULL;
3935                 }
3936                 free(user_data);
3937                 free(item);
3938         }
3939         app_data->unknown_buddy_list = NULL;
3940
3941         EINA_LIST_FREE(app_data->known_buddy_list, item) {
3942                 user_data_s* user_data = item->use_data;
3943
3944                 if (user_data->print_name)
3945                         free(user_data->print_name);
3946                         user_data->print_name = NULL;
3947
3948                 if (user_data->photo_path)
3949                         free(user_data->photo_path);
3950                         user_data->photo_path = NULL;
3951
3952                 if (user_data->first_name)
3953                         free(user_data->first_name);
3954                         user_data->first_name = NULL;
3955
3956                 if (user_data->last_name)
3957                         free(user_data->last_name);
3958                         user_data->last_name = NULL;
3959
3960                 if (user_data->phone)
3961                         free(user_data->phone);
3962                         user_data->phone = NULL;
3963
3964                 if (user_data->real_first_name)
3965                         free(user_data->real_first_name);
3966                         user_data->real_first_name = NULL;
3967
3968                 if (user_data->real_last_name)
3969                         free(user_data->real_last_name);
3970                         user_data->real_last_name = NULL;
3971
3972                 if (user_data->username)
3973                         free(user_data->username);
3974                         user_data->username = NULL;
3975
3976                 free(user_data);
3977                 free(item);
3978         }
3979         app_data->known_buddy_list = NULL;
3980
3981         eina_list_free(app_data->loaded_msg_list);
3982         app_data->loaded_msg_list = NULL;
3983
3984         if (app_data->service_client && destroy_server) {
3985                 service_client_destroy(app_data->service_client);
3986                 app_data->service_client = NULL;
3987         }
3988         free_contact_list(app_data->contact_list);
3989         tg_db_fini();
3990 }
3991
3992 static void
3993 app_terminate(void *data)
3994 {
3995         free_app_data(data, EINA_TRUE);
3996 }
3997
3998 static void
3999 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
4000 {
4001         /*APP_EVENT_LANGUAGE_CHANGED*/
4002         char *locale = NULL;
4003         system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
4004         if (locale) {
4005                 setlocale(LC_ALL, locale);
4006                 elm_language_set(locale);
4007         }
4008         free(locale);
4009         return;
4010 }
4011
4012
4013 TelegramAppDirection tg_get_device_orientation()
4014 {
4015         TelegramAppDirection target_direction;
4016
4017         switch (app_get_device_orientation()) {
4018         case APP_DEVICE_ORIENTATION_90:
4019                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4020                 break;
4021         case APP_DEVICE_ORIENTATION_180:
4022                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4023                 break;
4024         case APP_DEVICE_ORIENTATION_270:
4025                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4026                 break;
4027         case APP_DEVICE_ORIENTATION_0:
4028         default:
4029                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4030                 break;
4031         }
4032
4033         return target_direction;
4034 }
4035
4036 void update_current_view(appdata_s *ad)
4037 {
4038         if (!ad) {
4039                 return;
4040         }
4041         switch (ad->current_app_state) {
4042         case TG_USER_MAIN_VIEW_STATE:
4043                 refresh_main_list_view(ad, EINA_FALSE);
4044                 break;
4045         case TG_CHAT_MESSAGING_VIEW_STATE:
4046                 refresh_messaging_view(ad);
4047                 break;
4048         default:
4049                 break;
4050         }
4051
4052 }
4053
4054
4055 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
4056 {
4057         appdata_s *ad = user_data;
4058         if (!ad) {
4059                 return;
4060         }
4061         TelegramAppDirection target_direction;
4062         app_device_orientation_e orientation;
4063
4064         if (app_event_get_device_orientation(event_info, &orientation) != 0) {
4065                 return;
4066         }
4067
4068         switch (orientation) {
4069         case APP_DEVICE_ORIENTATION_90:
4070                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4071                 break;
4072         case APP_DEVICE_ORIENTATION_180:
4073                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4074                 break;
4075         case APP_DEVICE_ORIENTATION_270:
4076                 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4077                 break;
4078         case APP_DEVICE_ORIENTATION_0:
4079         default:
4080                 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4081                 break;
4082         }
4083
4084         if (target_direction != ad->target_direction) {
4085                 ad->target_direction = target_direction;
4086                 update_current_view(ad);
4087         }
4088
4089         return;
4090 }
4091
4092 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
4093 {
4094 }
4095
4096 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
4097 {
4098 }
4099
4100 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
4101 {
4102 }
4103
4104 int main(int argc, char *argv[])
4105 {
4106         appdata_s ad = {0,};
4107         int ret = 0;
4108
4109         ui_app_lifecycle_callback_s event_callback = {0,};
4110         app_event_handler_h handlers[5] = {NULL, };
4111
4112         event_callback.create = app_create;
4113         event_callback.terminate = app_terminate;
4114         event_callback.pause = app_pause;
4115         event_callback.resume = app_resume;
4116         event_callback.app_control = app_control;
4117
4118         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
4119         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
4120         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
4121         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
4122         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
4123         ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
4124
4125         ret = ui_app_main(argc, argv, &event_callback, &ad);
4126         if (ret != APP_ERROR_NONE) {
4127                 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main() is failed. err = %d", ret);
4128         }
4129
4130         return ret;
4131 }