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