2 This file is part of Telegram application for tizen
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.
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.
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
22 #include "telegramtizen.h"
23 #include "tg_splash_screen.h"
24 #include "tg_registration.h"
25 #include "tg_db_wrapper.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"
36 #include <notification.h>
38 #include "tg_settings_view.h"
39 #include "device_contacts_manager.h"
40 #include "tg_search_peer_view.h"
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)
50 static void popup_timeout_cb(void *data, Evas_Object *obj, void *event_info)
55 void show_toast(appdata_s *ad, char* value)
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);
64 char buf[512] = {'\0'};
65 snprintf(buf, sizeof(buf), "<font_size=30><color=#000000>%s</color></font_size>", value);
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);
75 static int _app_send_response(appdata_s *app, bundle *const msg)
77 int res = SVC_RES_FAIL;
78 res = service_client_send_message(app->service_client, msg);
83 void load_list_of_countries(appdata_s *ad)
88 int size_of_listed_countries = 0;
89 const char *file_name = ui_utils_get_resource(TG_LIST_OF_COUNTIRES);
106 fd = open(file_name, O_RDONLY);
110 file_size = lseek(fd, 0L, SEEK_END);
111 if ((int)file_size == -1) {
116 if (lseek(fd, 0L, SEEK_SET) < 0) {
119 ad->country_code_buffer = malloc(file_size);
120 if (!ad->country_code_buffer) {
124 if (read(fd, ad->country_code_buffer, file_size) != file_size) {
136 ptr = ad->country_code_buffer;
142 ptrToShortName = NULL;
143 ptrToFullName = NULL;
146 if (ptrToCode == NULL) {
150 } else if (*ptr == ';') {
152 state = STATE_SHORT_NAME;
156 case STATE_SHORT_NAME:
157 if (ptrToShortName == NULL) {
158 ptrToShortName = ptr;
159 } else if (*ptr == ';') {
161 state = STATE_FULL_NAME;
165 case STATE_FULL_NAME:
166 if (ptrToFullName == NULL) {
168 } else if (*ptr == '\r' || *ptr == '\n') {
170 dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] [%s] [%s]", ptrToCode, ptrToShortName, ptrToFullName);
171 ad->country_codes_list = eina_list_append(ad->country_codes_list, ptrToCode);
172 ad->country_names_list = eina_list_append(ad->country_names_list, ptrToFullName);
184 void load_registered_user_data(appdata_s *ad)
186 Eina_List *user_info = get_registered_user_info();
190 //show_toast(ad, "User info not loaded");
193 if (ad->current_user_data) {
194 free_user_data(ad->current_user_data);
195 ad->current_user_data = NULL;
198 ad->current_user_data = (user_data_s*)malloc(sizeof(user_data_s));
201 Eina_List *row_vals = NULL;
203 EINA_LIST_FREE(user_info, row_vals) {
207 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
209 int user_id = *temp_user_id;
212 ad->user_id.id = user_id;
213 ad->user_id.type = TGL_PEER_USER;
215 ad->current_user_data->user_id.id = user_id;
216 ad->current_user_data->user_id.type = TGL_PEER_USER;
219 ad->current_user_data->print_name = (char *)eina_list_nth(row_vals, 1);
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);
227 ad->current_user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
229 int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
231 ad->current_user_data->photo_id = *temp_photo_id;
235 ad->current_user_data->first_name = (char *)eina_list_nth(row_vals, 5);
237 ad->current_user_data->last_name = (char *)eina_list_nth(row_vals, 6);
239 char *phone_no = (char *)eina_list_nth(row_vals, 7);
240 ad->current_user_data->phone = phone_no;
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);
248 ad->current_user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
250 ad->current_user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
252 char *user_name = (char *)eina_list_nth(row_vals, 11);
253 ad->current_user_data->username = user_name;
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);
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);
268 EINA_LIST_FREE(row_vals, val) {
275 eina_list_free(user_info);
278 void load_peer_data(appdata_s *ad)
285 peer_with_pic_s* pic_item = NULL;
286 EINA_LIST_FREE(ad->peer_list, pic_item) {
289 tg_peer_info_s* item = pic_item->use_data;
291 if (item->print_name) {
292 free(item->print_name);
293 item->print_name = NULL;
295 if (item->photo_path) {
296 free(item->photo_path);
297 item->photo_path = NULL;
299 pic_item->contact_icon = NULL;
300 pic_item->msg_object = NULL;
301 pic_item->name_object = NULL;
307 ad->peer_list = NULL;
310 Eina_List *peer_details = get_all_peer_details(NULL);
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));
316 int *temp_peer_id = (int *)eina_list_nth(ts_msg, 0);
318 peer_info->peer_id = *temp_peer_id;
322 int *temp_peer_type = (int *)eina_list_nth(ts_msg, 1);
323 if (temp_peer_type) {
324 peer_info->peer_type = *temp_peer_type;
325 free(temp_peer_type);
328 int *temp_flags = (int *)eina_list_nth(ts_msg, 2);
330 peer_info->flags = *temp_flags;
334 char *temp_msg_id = (char *)eina_list_nth(ts_msg, 3);
336 peer_info->last_msg_id = atoll(temp_msg_id);
339 peer_info->last_msg_id = 0;
342 int *temp_last_msg_date = (int *)eina_list_nth(ts_msg, 4);
343 if (temp_last_msg_date) {
344 peer_info->last_msg_date = *temp_last_msg_date;
345 free(temp_last_msg_date);
348 peer_info->print_name = (char *)eina_list_nth(ts_msg, 5);
350 int *temp_struct_ver = (int *)eina_list_nth(ts_msg, 6);
351 if (temp_struct_ver) {
352 peer_info->struct_version = *temp_struct_ver;
353 free(temp_struct_ver);
356 int *temp_no_unread = (int *)eina_list_nth(ts_msg, 7);
357 if (temp_no_unread) {
358 peer_info->no_of_unread_msgs = *temp_no_unread;
359 free(temp_no_unread);
362 int *temp_last_seen = (int *)eina_list_nth(ts_msg, 8);
363 if (temp_last_seen) {
364 peer_info->last_seen_time = *temp_last_seen;
365 free(temp_last_seen);
368 peer_info->photo_path = (char *)eina_list_nth(ts_msg, 9);
370 int *temp_pic_id = (int *)eina_list_nth(ts_msg, 10);
372 peer_info->photo_id = *temp_pic_id;
375 int *temp_unknown = (int *)eina_list_nth(ts_msg, 11);
377 peer_info->is_unknown = *temp_unknown;
381 eina_list_free(ts_msg);
383 peer_with_pic_s *item = (peer_with_pic_s*) malloc(sizeof(*item));
384 item->use_data = peer_info;
385 item->contact_icon = NULL;
387 ad->peer_list = eina_list_append(ad->peer_list, item);
392 void load_main_list_data(appdata_s *ad)
394 if (!ad || !ad->peer_list)
397 tg_main_list_item_s* main_item = NULL;
398 EINA_LIST_FREE(ad->main_list, main_item) {
400 if (main_item->peer_print_name)
401 free(main_item->peer_print_name);
402 main_item->peer_print_name = NULL;
404 if (main_item->last_message)
405 free(main_item->last_message);
406 main_item->last_message = NULL;
408 if (main_item->profile_pic_path)
409 free(main_item->profile_pic_path);
410 main_item->profile_pic_path = NULL;
412 if (main_item->buddy_display_name)
413 free(main_item->buddy_display_name);
414 main_item->buddy_display_name = NULL;
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;
424 ad->main_list = NULL;
427 peer_with_pic_s *item = NULL;
428 EINA_LIST_FOREACH(ad->peer_list, l, item) {
430 tg_peer_info_s* peer_info = item->use_data;
434 if (peer_info->peer_type == TGL_PEER_CHAT &&
435 get_buddy_unknown_status(peer_info->peer_id))
438 //if (peer_info->last_msg_id > 0) {
440 // get message from message table.
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);
448 if (msg->message && strlen(msg->message) > 0) {
449 item->last_message = strdup(msg->message);
451 item->last_message = strdup(" ");
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));
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);
468 item->last_message = strdup("Document");
470 free_media_details(media_msg);
472 if (msg->message && strlen(msg->message) > 0) {
473 item->last_message = strdup(msg->message);
475 item->last_message = strdup("Document");
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");
490 item->last_message = strdup(" ");
494 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
495 main_list_item->peer_id = peer_info->peer_id;
496 main_list_item->peer_type = peer_info->peer_type;
498 set_peer_names(peer_info, main_list_item);
500 main_list_item->last_seen_time = msg->date;
501 main_list_item->profile_pic = NULL;
502 main_list_item->last_msg_id = msg->msg_id;
503 main_list_item->last_message = strdup(item->last_message);
504 main_list_item->last_msg_type = msg->media_type;
505 main_list_item->is_out_msg = msg->out;
506 main_list_item->last_msg_status = msg->msg_state;
507 main_list_item->last_msg_service = msg->service;
508 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
510 if (peer_info->photo_path)
511 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
513 main_list_item->profile_pic_path = NULL;
515 main_list_item->user_name_lbl = NULL;
516 main_list_item->status_lbl = NULL;
517 main_list_item->date_lbl = NULL;
518 main_list_item->msg_status_lbl = NULL;
519 main_list_item->main_item_layout = NULL;
520 ad->main_list = eina_list_append(ad->main_list, main_list_item);
522 // delete message object
529 msg->media_id = NULL;
535 item->last_message = strdup(" ");
536 if (peer_info->peer_type == TGL_PEER_CHAT) {
537 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
538 main_list_item->peer_id = peer_info->peer_id;
539 main_list_item->peer_type = peer_info->peer_type;
541 set_peer_names(peer_info, main_list_item);
543 main_list_item->last_seen_time = peer_info->last_seen_time;
544 main_list_item->profile_pic = NULL;
545 main_list_item->last_message = strdup(item->last_message);
546 main_list_item->last_msg_type = -1;
547 main_list_item->last_msg_service = 0;
548 main_list_item->is_out_msg = -1;
549 main_list_item->last_msg_id = -1;
550 main_list_item->last_msg_status = -1;
551 main_list_item->number_of_unread_msgs = 0;
553 if (peer_info->photo_path) {
554 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
556 main_list_item->profile_pic_path = NULL;
558 main_list_item->user_name_lbl = NULL;
559 main_list_item->status_lbl = NULL;
560 main_list_item->date_lbl = NULL;
561 main_list_item->msg_status_lbl = NULL;
562 main_list_item->main_item_layout = NULL;
563 ad->main_list = eina_list_append(ad->main_list, main_list_item);
574 void load_unknown_buddy_list_data(appdata_s *ad)
580 user_data_with_pic_s *item = NULL;
581 EINA_LIST_FREE(ad->unknown_buddy_list, item) {
582 user_data_s* user_data = item->use_data;
584 if (user_data->print_name)
585 free(user_data->print_name);
586 user_data->print_name = NULL;
588 if (user_data->photo_path)
589 free(user_data->photo_path);
590 user_data->photo_path = NULL;
592 if (user_data->first_name)
593 free(user_data->first_name);
594 user_data->first_name = NULL;
596 if (user_data->last_name)
597 free(user_data->last_name);
598 user_data->last_name = NULL;
600 if (user_data->phone)
601 free(user_data->phone);
602 user_data->phone = NULL;
604 if (user_data->real_first_name)
605 free(user_data->real_first_name);
606 user_data->real_first_name = NULL;
608 if (user_data->real_last_name)
609 free(user_data->real_last_name);
610 user_data->real_last_name = NULL;
612 if (user_data->username)
613 free(user_data->username);
614 user_data->username = NULL;
619 ad->unknown_buddy_list = NULL;
621 Eina_List *user_info = get_unknown_buddy_list_info();
624 //show_toast(ad, "User info not loaded");
628 Eina_List *row_vals = NULL;
629 EINA_LIST_FREE(user_info, row_vals) {
631 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
632 if (ad->user_id.id == (*temp_user_id)) {
634 EINA_LIST_FREE(row_vals, val) {
641 user_data_s *user_data = (user_data_s*)malloc(sizeof(*user_data));
642 user_data->is_selected = EINA_FALSE;
646 user_data->user_id.id = *temp_user_id;
647 user_data->user_id.type = TGL_PEER_USER;
651 user_data->print_name = (char *)eina_list_nth(row_vals, 1);
653 int *temp_struct_ver = (int *)eina_list_nth(row_vals, 2);
654 if (temp_struct_ver) {
655 user_data->structure_version = *temp_struct_ver;
656 free(temp_struct_ver);
659 user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
661 int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
663 user_data->photo_id = *temp_photo_id;
667 user_data->first_name = (char *)eina_list_nth(row_vals, 5);
669 user_data->last_name = (char *)eina_list_nth(row_vals, 6);
671 user_data->phone = (char *)eina_list_nth(row_vals, 7);
673 int *temp_access_hash = (int *)eina_list_nth(row_vals, 8);
674 if (temp_access_hash) {
675 user_data->access_hash = *temp_access_hash;
676 free(temp_access_hash);
679 user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
681 user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
683 user_data->username = (char *)eina_list_nth(row_vals, 11);
685 int *temp_online_status = (int *)eina_list_nth(row_vals, 12);
686 if (temp_online_status) {
687 user_data->online = *temp_online_status;
688 free(temp_online_status);
691 int *temp_last_seen = (int *)eina_list_nth(row_vals, 13);
692 if (temp_last_seen) {
693 user_data->last_seen = *temp_last_seen;
694 free(temp_last_seen);
697 int *temp_is_blocked = (int *)eina_list_nth(row_vals, 14);
698 if (temp_is_blocked) {
699 user_data->is_blocked = *temp_is_blocked;
700 free(temp_is_blocked);
703 int *temp_is_deleted = (int *)eina_list_nth(row_vals, 15);
704 if (temp_is_deleted) {
705 user_data->is_deleted = *temp_is_deleted;
706 free(temp_is_deleted);
709 int *temp_is_unknown = (int *)eina_list_nth(row_vals, 16);
710 if (temp_is_unknown) {
711 user_data->is_unknown = *temp_is_unknown;
712 free(temp_is_unknown);
715 user_data_with_pic_s *item = (user_data_with_pic_s *)malloc(sizeof(*item));
716 item->use_data = user_data;
717 item->contact_icon = NULL;
718 ad->unknown_buddy_list = eina_list_append(ad->unknown_buddy_list, item);
720 eina_list_free(row_vals);
724 void load_buddy_list_data(appdata_s *ad)
730 user_data_with_pic_s *item = NULL;
731 EINA_LIST_FREE(ad->buddy_list, item) {
732 user_data_s* user_data = item->use_data;
734 if (user_data->print_name)
735 free(user_data->print_name);
736 user_data->print_name = NULL;
738 if (user_data->photo_path)
739 free(user_data->photo_path);
740 user_data->photo_path = NULL;
742 if (user_data->first_name)
743 free(user_data->first_name);
744 user_data->first_name = NULL;
746 if (user_data->last_name)
747 free(user_data->last_name);
748 user_data->last_name = NULL;
750 if (user_data->phone)
751 free(user_data->phone);
752 user_data->phone = NULL;
754 if (user_data->real_first_name)
755 free(user_data->real_first_name);
756 user_data->real_first_name = NULL;
758 if (user_data->real_last_name)
759 free(user_data->real_last_name);
760 user_data->real_last_name = NULL;
762 if (user_data->username)
763 free(user_data->username);
764 user_data->username = NULL;
769 ad->buddy_list = NULL;
771 Eina_List *user_info = get_buddy_list_info();
775 //show_toast(ad, "User info not loaded");
779 Eina_List *row_vals = NULL;
780 EINA_LIST_FREE(user_info, row_vals) {
783 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
784 if (ad->user_id.id == (*temp_user_id)) {
786 EINA_LIST_FREE(row_vals, val) {
793 user_data_s *user_data = (user_data_s *)malloc(sizeof(*user_data));
794 user_data->is_selected = EINA_FALSE;
798 user_data->user_id.id = *temp_user_id;
799 user_data->user_id.type = TGL_PEER_USER;
803 user_data->print_name = (char *)eina_list_nth(row_vals, 1);
805 int *temp_struct_ver = (int *)eina_list_nth(row_vals, 2);
806 if (temp_struct_ver) {
807 user_data->structure_version = *temp_struct_ver;
808 free(temp_struct_ver);
811 user_data->photo_path = (char *)eina_list_nth(row_vals, 3);
813 int *temp_photo_id = (int *)eina_list_nth(row_vals, 4);
815 user_data->photo_id = *temp_photo_id;
819 user_data->first_name = (char *)eina_list_nth(row_vals, 5);
821 user_data->last_name = (char *)eina_list_nth(row_vals, 6);
823 user_data->phone = (char *)eina_list_nth(row_vals, 7);
825 int *temp_access_hash = (int *)eina_list_nth(row_vals, 8);
826 if (temp_access_hash) {
827 user_data->access_hash = *temp_access_hash;
828 free(temp_access_hash);
831 user_data->real_first_name = (char *)eina_list_nth(row_vals, 9);
833 user_data->real_last_name = (char *)eina_list_nth(row_vals, 10);
835 user_data->username = (char *)eina_list_nth(row_vals, 11);
837 int *temp_online_status = (int *)eina_list_nth(row_vals, 12);
838 if (temp_online_status) {
839 user_data->online = *temp_online_status;
840 free(temp_online_status);
843 int *temp_last_seen = (int *)eina_list_nth(row_vals, 13);
844 if (temp_last_seen) {
845 user_data->last_seen = *temp_last_seen;
846 free(temp_last_seen);
849 int *temp_is_blocked = (int *)eina_list_nth(row_vals, 14);
850 if (temp_is_blocked) {
851 user_data->is_blocked = *temp_is_blocked;
852 free(temp_is_blocked);
855 int *temp_is_deleted = (int *)eina_list_nth(row_vals, 15);
856 if (temp_is_deleted) {
857 user_data->is_deleted = *temp_is_deleted;
858 free(temp_is_deleted);
861 int *temp_is_unknown = (int *)eina_list_nth(row_vals, 16);
862 if (temp_is_unknown) {
863 user_data->is_unknown = *temp_is_unknown;
864 free(temp_is_unknown);
867 user_data_with_pic_s *item = (user_data_with_pic_s *) malloc(sizeof(*item));
868 item->use_data = user_data;
869 item->contact_icon = NULL;
870 ad->buddy_list = eina_list_append(ad->buddy_list, item);
871 eina_list_free(row_vals);
876 static int on_contacts_loaded(appdata_s *app, bundle *const rec_msg)
878 int result = SVC_RES_FAIL;
879 char* is_success_val = NULL;
880 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
881 if (strncmp("true", is_success_val, strlen("true")) == 0) {
885 load_registered_user_data(app);
887 if (app->current_app_state == TG_LOGIN_STATE || app->current_app_state == TG_REGISTRATION_STATE || app->current_app_state == TG_PROFILE_REGISTRATION_STATE) {
888 // Launch buddy list view
889 load_buddy_list_data(app);
890 load_unknown_buddy_list_data(app);
892 load_main_list_data(app);
893 elm_naviframe_item_pop(app->nf);
894 elm_naviframe_item_pop(app->nf);
895 //launch_buddy_list_cb(app);
897 //launch_user_main_view_cb(app);
898 launch_start_messaging_view(app);
900 } else if (app->current_app_state == TG_BUDDY_LIST_STATE) {
901 ////refresh_buddy_list(app);
908 show_toast(app, "Error: loading contacts");
910 hide_loading_popup(app);
915 static int on_contacts_and_chats_loaded(appdata_s *app, bundle *const rec_msg)
917 int result = SVC_RES_FAIL;
918 char* is_success_val = NULL;
919 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
920 if (strncmp("true", is_success_val, strlen("true")) == 0) {
923 //Eina_List *user_info = get_registered_user_info();
924 load_registered_user_data(app);
926 if (app->current_app_state == TG_LOGIN_STATE || app->current_app_state == TG_REGISTRATION_STATE || app->current_app_state == TG_PROFILE_REGISTRATION_STATE) {
927 // Launch buddy list view
928 load_buddy_list_data(app);
929 load_unknown_buddy_list_data(app);
931 load_main_list_data(app);
932 elm_naviframe_item_pop(app->nf);
933 elm_naviframe_item_pop(app->nf);
934 //launch_buddy_list_cb(app);
935 launch_user_main_view_cb(app);
936 } else if (app->current_app_state == TG_BUDDY_LIST_STATE) {
937 app->current_app_state = TG_BUDDY_LIST_STATE;
938 evas_object_show(app->panel);
939 //elm_panel_hidden_set(app->panel, EINA_FALSE);
940 ////refresh_buddy_list(app);
947 show_toast(app, "Error: loading contacts");
949 hide_loading_popup(app);
953 static int on_group_chat_updated(appdata_s *app, bundle *const rec_msg)
955 int result = SVC_RES_FAIL;
956 char *chat_id_str = NULL;
957 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
958 int chat_id = atoi(chat_id_str);
961 app->main_item_in_cahtting_data = NULL;
962 app->peer_in_cahtting_data = NULL;
965 tg_main_list_item_s *item = NULL;
966 EINA_LIST_FOREACH(app->main_list, l, item) {
969 if (item->peer_id == chat_id) {
970 app->main_item_in_cahtting_data = item;
976 peer_with_pic_s* pic_item = NULL;
978 EINA_LIST_FOREACH(app->peer_list, l, pic_item) {
983 tg_peer_info_s* item = pic_item->use_data;
984 if (item->peer_id == chat_id) {
985 app->peer_in_cahtting_data = pic_item;
992 launch_messaging_view_cb(app, buddy_id);
993 hide_loading_popup(app);
997 static int on_group_chat_info_changed_response(appdata_s *app, bundle *const rec_msg)
999 int result = SVC_RES_FAIL;
1000 char *chat_id_str = NULL;
1001 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1002 int chat_id = atoi(chat_id_str);
1004 char *type_of_change = NULL;
1005 result = bundle_get_str(rec_msg, "type_of_change", &type_of_change);
1008 peer_with_pic_s *peer_item = get_peer_info(chat_id);
1009 tg_main_list_item_s *latest_item = get_latest_item(app, peer_item);
1012 Eina_List *l = NULL;
1013 peer_with_pic_s *pitem = NULL;
1014 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1017 if (pitem->use_data->peer_id == chat_id) {
1018 app->peer_list = eina_list_remove(app->peer_list, pitem);
1024 tg_main_list_item_s *item = NULL;
1025 EINA_LIST_FOREACH(app->main_list, l, item) {
1029 if (item->peer_id == chat_id) {
1030 app->main_list = eina_list_remove(app->main_list, item);
1035 app->peer_list = eina_list_prepend(app->peer_list, peer_item);
1036 app->main_list = eina_list_prepend(app->main_list, latest_item);
1037 refresh_main_list_view(app, EINA_TRUE);
1039 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1040 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1042 app->peer_in_cahtting_data = peer_item;
1043 app->main_item_in_cahtting_data = latest_item;
1045 on_group_chat_info_changed(app, type_of_change);
1048 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1049 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1051 app->peer_in_cahtting_data = peer_item;
1052 app->main_item_in_cahtting_data = latest_item;
1054 on_group_chat_info_changed(app, type_of_change);
1056 on_group_chat_info_updated(app, type_of_change);
1059 hide_loading_popup(app);
1064 static int on_new_buddy_added_or_deleted_from_group_chat(appdata_s *app, bundle *const rec_msg, char *rec_key_val)
1066 int result = SVC_RES_FAIL;
1067 char* buddy_id_str = NULL;
1068 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1069 int buddy_id = atoi(buddy_id_str);
1071 Eina_Bool is_success = EINA_FALSE;
1072 char* is_success_val = NULL;
1073 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1074 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1075 is_success = EINA_TRUE;
1077 is_success = EINA_FALSE;
1080 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1081 && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1082 hide_loading_popup(app);
1083 if (strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) {
1084 show_toast(app, "Failed to add new buddy to chat.");
1085 } else if (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0) {
1086 show_toast(app, "Failed to delete buddy to chat.");
1089 hide_loading_popup(app);
1093 static int on_group_chat_name_changed(appdata_s *app, bundle *const rec_msg)
1095 int result = SVC_RES_FAIL;
1096 char* buddy_id_str = NULL;
1097 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1098 int buddy_id = atoi(buddy_id_str);
1100 Eina_Bool is_success = EINA_FALSE;
1101 char* is_success_val = NULL;
1102 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1103 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1104 is_success = EINA_TRUE;
1106 is_success = EINA_FALSE;
1109 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1110 && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1112 hide_loading_popup(app);
1113 show_toast(app, "Failed to rename the chat title.");
1115 hide_loading_popup(app);
1120 static int on_server_connection_status_changed(appdata_s *app, bundle *const rec_msg)
1122 int result = SVC_RES_FAIL;
1123 Eina_Bool is_success = EINA_FALSE;
1124 char* is_success_val = NULL;
1125 result = bundle_get_str(rec_msg, "connection_status", &is_success_val);
1126 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1127 is_success = EINA_TRUE;
1129 is_success = EINA_FALSE;
1132 app->is_server_ready = is_success;
1136 static int on_buddy_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1138 int result = SVC_RES_FAIL;
1139 char* user_id_str = NULL;
1140 result = bundle_get_str(rec_msg, "user_id", &user_id_str);
1141 int user_id = atoi(user_id_str);
1142 char* pic_file_path = NULL;
1143 result = bundle_get_str(rec_msg, "file_path", &pic_file_path);
1144 if (result == SVC_RES_FAIL || !pic_file_path || strlen(pic_file_path) <= 0 || access(pic_file_path, F_OK) == -1) {
1145 //pic_file_path = (char *)ui_utils_get_resource(DEFAULT_PROFILE_PIC);
1149 // update profile pic if buddy list view is present.
1150 if (user_id == app->user_id.id) {
1154 // update buddy list in appdata.
1156 Eina_List *l = NULL;
1157 user_data_with_pic_s *uitem = NULL;
1158 EINA_LIST_FOREACH(app->buddy_list, l, uitem) {
1161 if (uitem->use_data->user_id.id == user_id) {
1162 if (uitem->use_data->photo_path) {
1163 free(uitem->use_data->photo_path);
1164 uitem->use_data->photo_path = NULL;
1166 uitem->use_data->photo_path = strdup(pic_file_path);
1167 if (uitem->contact_icon) {
1168 elm_image_file_set(uitem->contact_icon, pic_file_path, NULL);
1169 evas_object_color_set(uitem->contact_icon, 0, 0, 0, 0);
1177 peer_with_pic_s *pitem = NULL;
1178 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1182 if (pitem->use_data->peer_id == user_id) {
1183 if (pitem->use_data->photo_path) {
1184 free(pitem->use_data->photo_path);
1185 pitem->use_data->photo_path = NULL;
1187 pitem->use_data->photo_path = strdup(pic_file_path);
1188 if (pitem->contact_icon) {
1189 elm_image_file_set(pitem->contact_icon, pic_file_path, NULL);
1190 evas_object_color_set(pitem->contact_icon, 0, 0, 0, 0);
1198 tg_main_list_item_s *item = NULL;
1199 EINA_LIST_FOREACH(app->main_list, l, item) {
1203 if (item->peer_id == user_id) {
1204 if (item->profile_pic_path) {
1205 free(item->profile_pic_path);
1206 item->profile_pic_path = NULL;
1208 item->profile_pic_path = strdup(pic_file_path);
1209 if (item->profile_pic) {
1210 elm_image_file_set(item->profile_pic, pic_file_path, NULL);
1211 evas_object_color_set(item->profile_pic, 0, 0, 0, 0);
1216 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data
1217 && app->peer_in_cahtting_data->use_data->peer_id == user_id) {
1218 on_chat_profile_pic_changed(app, pic_file_path);
1220 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data
1221 && app->peer_in_cahtting_data->use_data->peer_id == user_id) {
1222 //on_chat_profile_pic_changed(app, pic_file_path);
1223 //show_toast(app, "Chat profile picture updated successfully.");
1230 static int on_message_received_from_buddy(appdata_s *app, bundle *const rec_msg, Eina_Bool with_date)
1232 int result = SVC_RES_FAIL;
1235 if (!app->peer_in_cahtting_data || !app->peer_in_cahtting_data->use_data) {
1236 LOGE("Wrong status, previous chatting data are freed!!!");
1241 char* from_id_str = NULL;
1242 result = bundle_get_str(rec_msg, "from_id", &from_id_str);
1243 int from_id = atoi(from_id_str);
1245 char* to_id_str = NULL;
1246 result = bundle_get_str(rec_msg, "to_id", &to_id_str);
1247 int to_id = atoi(to_id_str);
1249 char* msg_id_str = NULL;
1250 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1251 long long message_id = atoll(msg_id_str);
1253 char* type_of_chat_str = NULL;
1254 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1255 int type_of_chat = atoi(type_of_chat_str);
1257 if (from_id == app->user_id.id) {
1258 // sent by the user. May be from another device.
1261 //load message from the received info.
1262 // get phone number using buddy id.
1264 Eina_Bool is_user_present_in_main_list = EINA_FALSE;
1266 if (type_of_chat == TGL_PEER_USER) {
1267 id_to_check = from_id;
1269 id_to_check = to_id;
1272 if (type_of_chat == TGL_PEER_USER) {
1273 if ((app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state == TG_SET_USER_INFO_STATE) &&
1274 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == from_id) {
1276 char* date_id_str = NULL;
1277 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1278 int date_id = atoi(date_id_str);
1279 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1282 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1287 } else if (type_of_chat == TGL_PEER_CHAT) {
1288 if ((app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state == TG_SET_CHAT_INFO_STATE) &&
1289 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
1290 char* tablename = get_table_name_from_number(to_id);
1291 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1295 char* date_id_str = NULL;
1296 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1297 int date_id = atoi(date_id_str);
1298 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1300 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1302 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1306 msg->message = NULL;
1309 if (msg->media_id) {
1310 free(msg->media_id);
1311 msg->media_id = NULL;
1320 Eina_List *l = NULL;
1321 tg_main_list_item_s *sel_item = NULL;
1322 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1324 if (sel_item->peer_id != id_to_check) {
1328 // update last message
1329 char* tablename = get_table_name_from_number(sel_item->peer_id);
1330 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1333 if (msg->service == 2) {
1336 msg->message = NULL;
1339 if (msg->media_id) {
1340 free(msg->media_id);
1341 msg->media_id = NULL;
1347 if (msg->service == 1) {
1348 if (msg->message && strlen(msg->message) > 0) {
1349 sel_item->last_message = strdup(msg->message);
1351 sel_item->last_message = strdup(" ");
1355 int media_type = msg->media_type;
1356 if (media_type == tgl_message_media_none) {
1357 if (msg->message && strlen(msg->message) > 0) {
1358 sel_item->last_message = strdup(msg->message);
1360 sel_item->last_message = strdup(" ");
1362 } else if (media_type == tgl_message_media_photo) {
1363 sel_item->last_message = strdup("Image");
1364 } else if (media_type == tgl_message_media_document) {
1367 if (msg->message && strlen(msg->message) > 0) {
1368 sel_item->last_message = strdup(msg->message);
1370 sel_item->last_message = strdup("Document");
1373 tgl_media_s *media_msg = NULL;
1374 media_msg = get_media_details_from_db(atoll(msg->media_id));
1375 sel_item->last_message = strdup(media_msg->doc_type);
1376 free_media_details(media_msg);
1378 } else if (media_type == tgl_message_media_geo) {
1379 sel_item->last_message = strdup("Geo location");
1380 } else if (media_type == tgl_message_media_contact) {
1381 sel_item->last_message = strdup("Contact");
1382 } else if (media_type == tgl_message_media_unsupported) {
1383 sel_item->last_message = strdup(" ");
1384 } else if (media_type == tgl_message_media_photo_encr) {
1385 sel_item->last_message = strdup("Image encrypted");
1386 } else if (media_type == tgl_message_media_document_encr) {
1387 sel_item->last_message = strdup("Document encrypted");
1389 sel_item->last_message = strdup(" ");
1392 sel_item->last_msg_id = msg->msg_id;
1393 sel_item->last_msg_type = msg->media_type;
1394 sel_item->is_out_msg = msg->out;
1395 sel_item->last_msg_status = msg->msg_state;
1396 sel_item->last_seen_time = msg->date;
1397 sel_item->number_of_unread_msgs = get_unread_message_count(tablename);
1399 if (sel_item->profile_pic) {
1403 if (sel_item->user_name_lbl) {
1407 if (sel_item->status_lbl) {
1408 char* org_msg = NULL;
1409 if (sel_item->last_message) {
1410 org_msg = sel_item->last_message;
1414 elm_object_text_set(sel_item->status_lbl, "");
1416 char status_buf[126] = {'\0'};
1419 if (msg->service == 1 || msg->service == 2) {
1420 sel_item->last_msg_service = 1;
1423 sel_item->last_msg_service = 0;
1427 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=%s align=left><font_size=26>%.*s</font_size></font>", color, 40, org_msg);
1428 elm_object_text_set(sel_item->status_lbl, status_buf);
1431 if (sel_item->date_lbl) {
1432 Eina_Bool is_today = compare_date_with_current_date(sel_item->last_seen_time);
1435 elm_object_text_set(sel_item->date_lbl, "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>Today</font_size></font>");
1439 char time_str[256] = { 0, };
1442 t = sel_item->last_seen_time;
1443 (void)localtime_r(&t, <);
1444 if (strftime(res, sizeof(res), "%d/%b/%Y", <) == 0) {
1445 strcpy(res, "unknown");
1448 snprintf(time_str, sizeof(time_str), "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>%s</font_size></font>", res);
1449 elm_object_text_set(sel_item->date_lbl, time_str);
1452 if (sel_item->msg_status_lbl) {
1453 Evas_Object* temp_lbl = elm_object_part_content_get(sel_item->msg_status_lbl, "swallow.status");
1455 evas_object_del(temp_lbl);
1458 Evas_Object* num_lbl = elm_label_add(app->nf);
1459 elm_object_style_set(num_lbl, "transparent");
1460 char unread_msgs[256];
1461 sprintf(unread_msgs, "<font=Tizen:style=Bold color=#ffffff align=center><font_size=25>%d</font_size></font>", sel_item->number_of_unread_msgs);
1462 elm_object_text_set(num_lbl, unread_msgs);
1463 evas_object_size_hint_weight_set(num_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1464 evas_object_size_hint_align_set(num_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
1465 evas_object_show(num_lbl);
1467 char edj_path[PATH_MAX] = {0, };
1468 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
1469 Evas_Object* msg_status = elm_layout_add(app->nf);
1470 elm_layout_file_set(msg_status, edj_path, "circle_item");
1471 evas_object_size_hint_weight_set(msg_status, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1472 evas_object_size_hint_align_set(msg_status, EVAS_HINT_FILL, EVAS_HINT_FILL);
1473 evas_object_show(msg_status);
1474 elm_object_part_content_set(msg_status, "content", num_lbl);
1476 elm_object_part_content_set(sel_item->msg_status_lbl, "swallow.status", msg_status);
1481 msg->message = NULL;
1484 if (msg->media_id) {
1485 free(msg->media_id);
1486 msg->media_id = NULL;
1492 is_user_present_in_main_list = EINA_TRUE;
1494 app->main_list = eina_list_remove(app->main_list, sel_item);
1495 app->main_list = eina_list_prepend(app->main_list, sel_item);
1496 refresh_main_list_view(app, EINA_FALSE);
1500 if (!is_user_present_in_main_list) {
1501 peer_with_pic_s *peer_item = get_peer_info(id_to_check);
1503 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
1505 app->main_list = eina_list_prepend(app->main_list, latest_item);
1506 refresh_main_list_view(app, EINA_TRUE);
1511 if (app->s_app_visible_state == APP_STATE_IN_BACKGROUND || app->current_app_state != TG_USER_MAIN_VIEW_STATE) {
1512 // show notification
1513 char *icon_path = (char *)ui_utils_get_resource(DEFAULT_TELEGRAM_ICON);
1514 char *title = "Telegram";
1517 int unread_msg_cnt = get_number_of_unread_messages();
1519 if (unread_msg_cnt <= 0) {
1520 badge_set_count(TELEGRAM_APP_ID, 0);
1524 sprintf(content, "%d new messages received.", unread_msg_cnt);
1526 char *sound_track = NULL;
1527 char *app_id = TELEGRAM_APP_ID;
1528 tg_notification_create(app, icon_path, title, content, sound_track, app_id);
1529 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
1530 if (BADGE_ERROR_NONE != err) {
1538 static int on_message_with_date_received_from_buddy(appdata_s *app, bundle *const rec_msg)
1540 return on_message_received_from_buddy(app, rec_msg, EINA_TRUE);
1543 static int on_new_contact_added(appdata_s *app, bundle *const rec_msg)
1545 int result = SVC_RES_FAIL;
1546 char* buddy_id_str = NULL;
1547 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1548 int buddy_id = atoi(buddy_id_str);
1550 Eina_Bool is_success = EINA_FALSE;
1551 char* is_success_val = NULL;
1552 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1553 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1554 is_success = EINA_TRUE;
1556 is_success = EINA_FALSE;
1559 load_buddy_list_data(app);
1560 load_peer_data(app);
1562 //show_toast(app, "Failed to add contact. Please try after sometime.");
1564 hide_loading_popup(app);
1565 if (app->current_app_state == TG_ADD_CONTACT_STATE) {
1566 on_new_contact_added_response_received(app, buddy_id, is_success);
1571 static int on_buddy_re_added_to_chat(appdata_s *app, bundle *const rec_msg)
1573 int result = SVC_RES_FAIL;
1574 char* buddy_id_str = NULL;
1575 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1576 int buddy_id = atoi(buddy_id_str);
1578 Eina_Bool is_success = EINA_FALSE;
1579 char* is_success_val = NULL;
1580 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1581 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1582 is_success = EINA_TRUE;
1584 is_success = EINA_FALSE;
1587 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1588 on_user_added_response_received(app, buddy_id, is_success);
1590 hide_loading_popup(app);
1594 static int on_buddy_deleted_from_friends_list(appdata_s *ad, bundle *const rec_msg)
1596 int result = SVC_RES_FAIL;
1597 char* buddy_id_str = NULL;
1598 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1599 int buddy_id = atoi(buddy_id_str);
1601 Eina_Bool is_success = EINA_FALSE;
1602 char* is_success_val = NULL;
1603 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1604 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1605 is_success = EINA_TRUE;
1607 is_success = EINA_FALSE;
1610 if (is_success && ad->current_app_state == TG_SET_USER_INFO_STATE) {
1611 //on_user_deleted_response_received(app, buddy_id, is_success);
1612 show_loading_popup(ad);
1613 elm_naviframe_item_pop(ad->nf);
1614 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
1615 ad->peer_in_cahtting_data = NULL;
1616 ad->main_item_in_cahtting_data = NULL;
1617 ad->buddy_in_cahtting_data = NULL;
1618 elm_naviframe_item_pop(ad->nf);
1619 load_registered_user_data(ad);
1620 load_buddy_list_data(ad);
1621 load_unknown_buddy_list_data(ad);
1623 load_main_list_data(ad);
1626 Eina_List *l = NULL;
1627 tg_main_list_item_s* main_list_item = NULL;
1628 EINA_LIST_FOREACH(ad->main_list, l, main_list_item) {
1629 if (main_list_item && main_list_item->peer_id == buddy_id) {
1630 ad->main_list = eina_list_remove(ad->main_list, main_list_item);
1631 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
1636 ecore_timer_add(1, on_load_main_view_requested, ad);
1638 hide_loading_popup(ad);
1643 static int on_buddy_blocked(appdata_s *app, bundle *const rec_msg)
1645 int result = SVC_RES_FAIL;
1647 char* buddy_id_str = NULL;
1648 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1649 int buddy_id = atoi(buddy_id_str);
1651 Eina_Bool is_success = EINA_FALSE;
1652 char* is_success_val = NULL;
1653 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1654 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1655 is_success = EINA_TRUE;
1657 is_success = EINA_FALSE;
1660 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1661 on_user_block_response_received(app, buddy_id, is_success);
1663 hide_loading_popup(app);
1668 static int on_buddy_unblocked(appdata_s *app, bundle *const rec_msg)
1670 int result = SVC_RES_FAIL;
1672 char* buddy_id_str = NULL;
1673 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1674 int buddy_id = atoi(buddy_id_str);
1676 Eina_Bool is_success = EINA_FALSE;
1677 char* is_success_val = NULL;
1678 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1679 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1680 is_success = EINA_TRUE;
1682 is_success = EINA_FALSE;
1685 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1686 on_user_unblock_response_received(app, buddy_id, is_success);
1688 hide_loading_popup(app);
1693 static int on_delete_selected_group_chats(appdata_s *ad, bundle *const rec_msg)
1695 int result = SVC_RES_OK;
1697 if (app->current_app_state == TG_USER_MAIN_VIEW_SELECTION_STATE) {
1698 elm_naviframe_item_pop(app->nf);
1699 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1700 show_floating_button(app);
1701 load_buddy_list_data(app);
1702 load_unknown_buddy_list_data(app);
1703 load_peer_data(app);
1704 load_main_list_data(app);
1705 refresh_main_list_view(app, EINA_FALSE);
1707 hide_loading_popup(app);
1709 load_registered_user_data(ad);
1710 load_buddy_list_data(ad);
1711 load_unknown_buddy_list_data(ad);
1713 load_main_list_data(ad);
1714 if (ad->main_item) {
1715 elm_naviframe_item_pop_to(ad->main_item);
1717 ecore_timer_add(1, on_load_main_view_requested, ad);
1722 static int on_group_chat_deleted_response(appdata_s *app, bundle *const rec_msg)
1724 int result = SVC_RES_FAIL;
1726 char* chat_id_str = NULL;
1727 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1728 int chat_id = atoi(chat_id_str);
1730 Eina_Bool is_success = EINA_FALSE;
1731 char* is_success_val = NULL;
1732 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1733 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1734 is_success = EINA_TRUE;
1736 is_success = EINA_FALSE;
1740 Eina_List *l = NULL;
1741 peer_with_pic_s *pitem = NULL;
1742 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1746 if (pitem->use_data->peer_id == chat_id) {
1747 app->peer_list = eina_list_remove(app->peer_list, pitem);
1753 tg_main_list_item_s *item = NULL;
1754 EINA_LIST_FOREACH(app->main_list, l, item) {
1755 if (item->peer_id == chat_id) {
1756 app->main_list = eina_list_remove(app->main_list, item);
1761 refresh_main_list_view(app, EINA_FALSE);
1763 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1764 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1766 app->peer_in_cahtting_data = NULL;
1767 app->main_item_in_cahtting_data = NULL;
1769 elm_naviframe_item_pop(app->nf);
1770 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1771 show_floating_button(app);
1774 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1775 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1776 on_group_chat_deleted(app, chat_id);
1780 show_toast(app, "Failed to delete chat");
1782 hide_loading_popup(app);
1787 static int on_message_sent_to_buddy(appdata_s *app, bundle *const rec_msg)
1789 int result = SVC_RES_FAIL;
1791 char* buddy_id_str = NULL;
1792 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1793 int buddy_id = atoi(buddy_id_str);
1795 char* msg_id_str = NULL;
1796 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1797 int message_id = atoi(msg_id_str);
1799 char* table_name = NULL;
1800 result = bundle_get_str(rec_msg, "table_name", &table_name);
1803 Eina_Bool is_success = EINA_FALSE;
1804 char* is_success_val = NULL;
1805 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1806 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1807 is_success = EINA_TRUE;
1809 is_success = EINA_FALSE;
1810 show_toast(app, "message sent failed");
1813 char* type_of_chat_str = NULL;
1814 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1815 int type_of_chat = atoi(type_of_chat_str);
1817 tg_message_s* msg = get_message_from_message_table(message_id, table_name);
1820 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1821 // update message to sent state
1822 on_text_message_state_changed(app, msg, type_of_chat);
1825 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
1826 Eina_List *l = NULL;
1827 tg_main_list_item_s *sel_item = NULL;
1828 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1829 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
1830 if (sel_item->msg_status_lbl) {
1831 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_SENT_ICON), NULL);
1840 msg->message = NULL;
1843 if (msg->media_id) {
1844 free(msg->media_id);
1845 msg->media_id = NULL;
1852 static int on_self_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1854 int result = SVC_RES_FAIL;
1855 Eina_Bool is_success = EINA_FALSE;
1856 char *is_success_val = NULL;
1857 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1858 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1859 is_success = EINA_TRUE;
1860 show_toast(app, "profile picture updated successfully.");
1862 is_success = EINA_FALSE;
1863 show_toast(app, "profile picture not updated.");
1867 char *file_path = NULL;
1868 result = bundle_get_str(rec_msg, "file_path", &file_path);
1870 if (app->current_user_data->photo_path) {
1871 free(app->current_user_data->photo_path);
1872 app->current_user_data->photo_path = NULL;
1875 app->current_user_data->photo_path = strdup(file_path);
1876 Evas_Object* profile_pic = evas_object_data_get(app->nf, "main_user_profile_pic");
1878 elm_image_file_set(profile_pic, file_path, NULL);
1881 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE) {
1882 Evas_Object* set_pro_pic = evas_object_data_get(app->nf, "settings_user_profile_pic");
1884 elm_image_file_set(set_pro_pic, file_path, NULL);
1888 hide_loading_popup(app);
1892 static int on_self_profile_name_updated(appdata_s *app, bundle *const rec_msg)
1894 int result = SVC_RES_FAIL;
1895 Eina_Bool is_success = EINA_FALSE;
1896 char *is_success_val = NULL;
1897 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1898 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1899 is_success = EINA_TRUE;
1900 show_toast(app, "Username updated successfully.");
1902 is_success = EINA_FALSE;
1903 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1908 char *first_name = NULL;
1909 result = bundle_get_str(rec_msg, "first_name", &first_name);
1914 char *last_name = NULL;
1915 result = bundle_get_str(rec_msg, "last_name", &last_name);
1920 if (app->current_user_data->first_name) {
1921 free(app->current_user_data->first_name);
1922 app->current_user_data->first_name = NULL;
1925 if (app->current_user_data->last_name) {
1926 free(app->current_user_data->last_name);
1927 app->current_user_data->last_name = NULL;
1931 app->current_user_data->first_name = strdup(first_name);
1935 app->current_user_data->last_name = strdup(last_name);
1938 if (app->current_app_state == TG_SETTINGS_EDIT_NAME_STATE) {
1939 elm_naviframe_item_pop(app->nf);
1940 app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1941 refresh_settings_screen(app);
1947 hide_loading_popup(app);
1951 static int on_self_username_updated(appdata_s *app, bundle *const rec_msg)
1953 int result = SVC_RES_FAIL;
1954 Eina_Bool is_success = EINA_FALSE;
1955 char *is_success_val = NULL;
1956 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1957 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1958 is_success = EINA_TRUE;
1959 show_toast(app, "Username updated successfully.");
1961 is_success = EINA_FALSE;
1962 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1966 char *username = NULL;
1967 result = bundle_get_str(rec_msg, "username", &username);
1969 if (app->current_user_data->username) {
1970 free(app->current_user_data->username);
1971 app->current_user_data->username = NULL;
1974 app->current_user_data->username = strdup(username);
1977 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE || app->current_app_state == TG_SET_USERNAME_STATE) {
1978 Evas_Object *name_lbl = evas_object_data_get(app->nf, "settings_user_name_label");
1980 char buf[512] = {'\0'};
1981 snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", app->current_user_data->username);
1982 elm_object_text_set(name_lbl, buf);
1985 if (app->current_app_state == TG_SET_USERNAME_STATE) {
1986 elm_naviframe_item_pop(app->nf);
1987 app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1990 hide_loading_popup(app);
1995 static int on_message_read_by_buddy(appdata_s *app, bundle *const rec_msg)
1997 int result = SVC_RES_FAIL;
1998 char* buddy_id_str = NULL;
1999 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2000 int buddy_id = atoi(buddy_id_str);
2002 char* msg_id_str = NULL;
2003 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
2004 int message_id = atoi(msg_id_str);
2006 char* table_name = NULL;
2007 result = bundle_get_str(rec_msg, "table_name", &table_name);
2009 char* phone_number = NULL;
2010 result = bundle_get_str(rec_msg, "phone_number", &phone_number);
2012 char* type_of_chat_str = NULL;
2013 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
2014 int type_of_chat = atoi(type_of_chat_str);
2016 tg_message_s* msg = get_message_from_message_table(message_id, table_name);
2019 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
2020 // update message to sent state
2021 on_text_message_state_changed(app, msg, type_of_chat);
2024 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2026 Eina_List *l = NULL;
2027 tg_main_list_item_s *sel_item = NULL;
2028 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2030 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
2032 if (sel_item->msg_status_lbl) {
2033 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_READ_ICON), NULL);
2043 msg->message = NULL;
2046 if (msg->media_id) {
2047 free(msg->media_id);
2048 msg->media_id = NULL;
2056 static int on_media_message_download_completed(appdata_s *app, bundle *const rec_msg)
2058 LOGE("Media download14");
2059 int result = SVC_RES_FAIL;
2060 char* buddy_id_str = NULL;
2061 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2062 int buddy_id = atoi(buddy_id_str);
2064 char* to_id_str = NULL;
2065 result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2066 int to_id = atoi(to_id_str);
2068 char* media_id_str = NULL;
2069 result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2070 long long media_id = atoll(media_id_str);
2072 char* file_name = NULL;
2073 result = bundle_get_str(rec_msg, "file_name", &file_name);
2075 //file_name == "failed_to_load"
2077 if (file_name && strstr(file_name, "failed_to_load") != NULL) {
2079 show_toast(app, "media download failed.");
2082 char* caption = NULL;
2083 result = bundle_get_str(rec_msg, "caption", &caption);
2086 if (file_name && app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2087 && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2088 // update media to sent state
2089 LOGE("Media download15");
2090 on_media_download_completed(app, buddy_id, media_id, file_name, caption);
2092 LOGE("Media download16");
2096 static int on_video_message_thumb_download_completed(appdata_s *app, bundle *const rec_msg)
2098 int result = SVC_RES_FAIL;
2099 char* buddy_id_str = NULL;
2100 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2101 int buddy_id = atoi(buddy_id_str);
2103 char* to_id_str = NULL;
2104 result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2105 int to_id = atoi(to_id_str);
2107 char* media_id_str = NULL;
2108 result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2109 long long media_id = atoll(media_id_str);
2111 char* file_name = NULL;
2112 result = bundle_get_str(rec_msg, "file_name", &file_name);
2114 char* caption = NULL;
2115 result = bundle_get_str(rec_msg, "caption", &caption);
2117 if (file_name && app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2118 && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2119 // update thumbnail icon
2120 on_video_thumb_download_completed(app, buddy_id, media_id, file_name, caption);
2125 static int on_new_buddy_added_to_contacts(appdata_s *app, bundle *const rec_msg)
2127 int result = SVC_RES_FAIL;
2128 char* buddy_id_str = NULL;
2129 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2131 int buddy_id = atoi(buddy_id_str);
2132 peer_with_pic_s *peer_item = get_peer_info(buddy_id);
2133 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2135 app->main_list = eina_list_prepend(app->main_list, latest_item);
2136 refresh_main_list_view(app, EINA_TRUE);
2138 hide_loading_popup(app);
2143 static int on_new_group_added(appdata_s *app, bundle *const rec_msg)
2145 int result = SVC_RES_FAIL;
2149 char* chat_id_str = NULL;
2150 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
2152 int chat_id = atoi(chat_id_str);
2153 peer_with_pic_s *peer_item = get_peer_info(chat_id);
2154 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2155 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2157 app->main_list = eina_list_prepend(app->main_list, latest_item);
2158 hide_loading_popup(app);
2159 refresh_main_list_view(app, EINA_FALSE);
2163 hide_loading_popup(app);
2164 if (app->main_item) {
2165 elm_naviframe_item_pop_to(app->main_item);
2166 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
2169 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2171 app->main_list = eina_list_prepend(app->main_list, latest_item);
2172 refresh_main_list_view(app, EINA_FALSE);
2176 app->peer_list = eina_list_prepend(app->peer_list, peer_item);
2178 load_registered_user_data(app);
2179 load_buddy_list_data(app);
2180 load_unknown_buddy_list_data(app);
2181 load_peer_data(app);
2182 load_main_list_data(app);
2184 if (app->main_item) {
2185 elm_naviframe_item_pop_to(app->main_item);
2188 ecore_timer_add(1, on_load_main_view_requested, app);
2193 static int on_buddy_contact_updated(appdata_s *app, bundle *const rec_msg)
2195 int result = SVC_RES_FAIL;
2196 char* buddy_id_str = NULL;
2197 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2198 //int buddy_id = atoi(buddy_id_str);
2200 char* update_msg = NULL;
2201 result = bundle_get_str(rec_msg, "update_message", &update_msg);
2208 static int on_buddy_status_updated(appdata_s *app, bundle *const rec_msg)
2210 int result = SVC_RES_FAIL;
2211 char* buddy_id_str = NULL;
2212 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2213 int buddy_id = atoi(buddy_id_str);
2214 // update to online or last seen, if current view is conversation.
2217 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2218 on_user_presence_state_changed(app, buddy_id);
2223 static int on_typing_status_updated(appdata_s *app, bundle *const rec_msg)
2225 int result = SVC_RES_FAIL;
2226 char* buddy_id_str = NULL;
2227 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2228 int buddy_id = atoi(buddy_id_str);
2230 char* buddy_name = NULL;
2231 result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2233 const char *type_status_str;
2235 result = bundle_get_str(rec_msg, "type_status", &type_status_str);
2236 enum tgl_typing_status type_status = atoi(type_status_str);
2239 char* type_status_message = NULL;
2241 switch (type_status) {
2242 case tgl_typing_none:
2243 type_status_message = "doing nothing";
2245 case tgl_typing_typing:
2246 type_status_message = "typing...";
2248 case tgl_typing_cancel:
2249 type_status_message = "deleting typed message";
2251 case tgl_typing_record_video:
2252 type_status_message = "recording video";
2254 case tgl_typing_upload_video:
2255 type_status_message = "uploading video";
2257 case tgl_typing_record_audio:
2258 type_status_message = "recording audio";
2260 case tgl_typing_upload_audio:
2261 type_status_message = "uploading audio";
2263 case tgl_typing_upload_photo:
2264 type_status_message = "uploading photo";
2266 case tgl_typing_upload_document:
2267 type_status_message = "uploading document";
2269 case tgl_typing_geo:
2270 type_status_message = "choosing location";
2272 case tgl_typing_choose_contact:
2273 type_status_message = "choosing contact";
2276 type_status_message = "";
2280 // update UI. may be contacts view or conversation view.
2281 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2282 Eina_List *l = NULL;
2283 tg_main_list_item_s *sel_item = NULL;
2284 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2285 if (sel_item->peer_id != buddy_id) {
2288 // update last message
2290 if (sel_item->profile_pic) {
2294 if (sel_item->user_name_lbl) {
2298 if (sel_item->status_lbl) {
2299 char status_buf[512] = {'\0'};
2300 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=#158CB0 align=left><font_size=30>%.*s</font_size></font>", 25, type_status_message);
2301 elm_object_text_set(sel_item->status_lbl, status_buf);
2304 if (sel_item->date_lbl) {
2307 if (sel_item->msg_status_lbl) {
2315 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2316 on_user_status_changed(app, type_status_message);
2323 static int on_user_status_updated(appdata_s *app, bundle *const rec_msg)
2325 int result = SVC_RES_FAIL;
2326 char* buddy_id_str = NULL;
2327 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2328 int buddy_id = atoi(buddy_id_str);
2330 char* buddy_name = NULL;
2331 result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2333 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2334 Eina_List *l = NULL;
2335 tg_main_list_item_s *sel_item = NULL;
2336 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2337 if (sel_item->peer_id != buddy_id) {
2340 // update last message
2342 if (sel_item->profile_pic) {
2346 if (sel_item->user_name_lbl) {
2350 if (sel_item->status_lbl) {
2351 char status_buf[126] = {'\0'};
2352 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Normal color=#A4A4A4 align=left><font_size=26>%.*s</font_size></font>", 29, sel_item->last_message ? sel_item->last_message : "");
2353 elm_object_text_set(sel_item->status_lbl, status_buf);
2356 if (sel_item->date_lbl) {
2359 if (sel_item->msg_status_lbl) {
2366 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2367 on_user_presence_state_changed(app, buddy_id);
2373 Eina_Bool on_logout_completed(void *data)
2375 appdata_s *ad = data;
2377 elm_naviframe_item_pop(ad->nf);
2378 ad->current_app_state = TG_REGISTRATION_STATE;
2379 launch_init_screen(ad);
2381 return ECORE_CALLBACK_CANCEL;
2384 Eina_Bool on_start_server_requested(void *data)
2386 appdata_s *ad = data;
2388 launch_tg_server(ad);
2390 return ECORE_CALLBACK_CANCEL;
2394 static int _on_service_client_msg_received_cb(void *data, bundle *const rec_msg)
2396 int result = SVC_RES_FAIL;
2397 RETVM_IF(!data, result, "Data is NULL");
2398 appdata_s *app = data;
2399 char *rec_key_val = NULL;
2402 result = bundle_get_str(rec_msg, "command", &rec_key_val);
2404 LOGI("MSG recv = %s", rec_key_val);
2406 if (strcmp(rec_key_val, "server_not_initialized") == 0) {
2407 hide_loading_popup(app);
2409 } else if (strcmp(rec_key_val, "server_restart_notification") == 0) {
2410 hide_loading_popup(app);
2411 if (!app->is_waiting_for_phone_num)
2412 show_toast(app, "Network error. Please try again");
2414 ecore_timer_add(1, on_start_server_requested, app);
2415 } else if (strcmp(rec_key_val, "registration_done") == 0) {
2417 char* is_success_val = NULL;
2418 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
2419 if (app->login_timer)
2420 ecore_timer_del(app->login_timer);
2421 app->login_timer = NULL;
2423 if (strncmp("true", is_success_val, strlen("true")) == 0) {
2424 //show_toast(app, is_success_val);
2425 // Launch login view
2426 //elm_naviframe_item_pop(app->nf);
2427 launch_login_cb(data);
2430 show_toast(app, "Error: registration");
2432 hide_loading_popup(app);
2434 } else if (strcmp(rec_key_val, "name_registration_request") == 0) {
2436 elm_naviframe_item_pop(app->nf);
2437 elm_naviframe_item_pop(app->nf);
2438 launch_first_registration_cb(app);
2439 hide_loading_popup(app);
2441 } else if (strcmp(rec_key_val, "request_reg_code_again") == 0) {
2442 show_toast(app, "Please enter a valid code.");
2443 hide_loading_popup(app);
2444 } else if (strcmp(rec_key_val, "request_phone_num_again") == 0) {
2445 show_toast(app, "Please enter a valid phone number.");
2446 hide_loading_popup(app);
2447 } else if (strcmp(rec_key_val, "request_phone_new_num") == 0) {
2448 app->is_waiting_for_phone_num = EINA_TRUE;
2449 } else if (strcmp(rec_key_val, "server_connection_failed") == 0) {
2450 show_toast(app, "Server connection failed. please check network connection");
2451 hide_loading_popup(app);
2452 } else if (strcmp(rec_key_val, "logout_completed") == 0) {
2454 free_app_data(app, EINA_TRUE);
2455 hide_loading_popup(app);
2456 app->phone_number = NULL;
2457 app->buddy_list = NULL;
2458 app->unknown_buddy_list = NULL;
2459 app->known_buddy_list = NULL;
2460 app->main_list = NULL;
2461 app->peer_list = NULL;
2462 app->search_peer_list = NULL;
2463 app->is_first_time_registration = EINA_FALSE;
2465 app->loaded_msg_list = NULL;
2466 app->loading_popup = NULL;
2467 app->current_user_data = NULL;
2468 app->is_tg_initilized = EINA_FALSE;
2469 app->chat_background = NULL;
2470 app->msg_popup = NULL;
2471 app->s_notififcation = NULL;
2473 app->is_server_ready = EINA_FALSE;
2474 app->is_waiting_for_phone_num = EINA_FALSE;
2475 app->login_timer = NULL;
2476 app->contact_list = NULL;
2478 char *chat_bg = NULL;
2479 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
2481 app->chat_background = strdup(chat_bg);
2484 if (!app->chat_background) {
2485 app->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
2486 preference_set_string(TG_CHAT_BG_PREFERENCE, app->chat_background);
2491 elm_naviframe_item_pop(app->nf);
2492 //ecore_timer_add(1, on_logout_completed, app);
2493 elm_naviframe_item_pop(app->nf);
2494 app->current_app_state = TG_REGISTRATION_STATE;
2495 launch_init_screen(app);
2497 } else if (strcmp(rec_key_val, "server_connection_status") == 0) {
2498 return on_server_connection_status_changed(data, rec_msg);
2501 if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2502 app->is_tg_initilized = EINA_TRUE;
2504 if (!app->is_tg_initilized) {
2508 if (strcmp(rec_key_val, "contacts_load_done") == 0) {
2510 return on_contacts_loaded(data, rec_msg);
2512 } else if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2514 return on_contacts_and_chats_loaded(data, rec_msg);
2516 } else if (strcmp(rec_key_val, "response_group_chat_updated") == 0) {
2518 return on_group_chat_updated(data, rec_msg);
2520 } else if (strcmp(rec_key_val, "group_chat_updated") == 0) {
2522 return on_group_chat_info_changed_response(data, rec_msg);
2524 } else if ((strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) || (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0)) {
2526 return on_new_buddy_added_or_deleted_from_group_chat(data, rec_msg, rec_key_val);
2528 } else if (strcmp(rec_key_val, "group_chat_rename_response") == 0) {
2530 return on_group_chat_name_changed(data, rec_msg);
2532 } else if (strcmp(rec_key_val, "buddy_profile_pic_updated") == 0) {
2534 return on_buddy_profile_pic_updated(data, rec_msg);
2536 } else if (strcmp(rec_key_val, "message_received") == 0) {
2538 return on_message_received_from_buddy(data, rec_msg, EINA_FALSE);
2540 } else if (strcmp(rec_key_val, "message_received_with_date") == 0) {
2542 return on_message_with_date_received_from_buddy(data, rec_msg);
2544 } else if (strcmp(rec_key_val, "buddy_readded") == 0) {
2546 return on_buddy_re_added_to_chat(data, rec_msg);
2548 } else if (strcmp(rec_key_val, "new_contact_added") == 0) {
2550 return on_new_contact_added(data, rec_msg);
2552 } else if (strcmp(rec_key_val, "buddy_deleted") == 0) {
2554 return on_buddy_deleted_from_friends_list(data, rec_msg);
2556 } else if (strcmp(rec_key_val, "buddy_blocked") == 0) {
2558 return on_buddy_blocked(data, rec_msg);
2560 } else if (strcmp(rec_key_val, "buddy_unblocked") == 0) {
2562 return on_buddy_unblocked(data, rec_msg);
2564 } else if (strcmp(rec_key_val, "selected_group_chats_deleted_response") == 0) {
2566 return on_delete_selected_group_chats(data, rec_msg);
2568 } else if (strcmp(rec_key_val, "group_chat_deleted_response") == 0) {
2570 return on_group_chat_deleted_response(data, rec_msg);
2572 } else if (strcmp(rec_key_val, "message_sent_to_buddy") == 0) {
2574 return on_message_sent_to_buddy(data, rec_msg);
2576 } else if (strcmp(rec_key_val, "self_profile_picture_updated") == 0) {
2578 return on_self_profile_pic_updated(data, rec_msg);
2580 } else if (strcmp(rec_key_val, "self_profile_name_updated") == 0) {
2582 return on_self_profile_name_updated(data, rec_msg);
2584 } else if (strcmp(rec_key_val, "self_username_updated") == 0) {
2586 return on_self_username_updated(data, rec_msg);
2588 } else if (strcmp(rec_key_val, "message_read_by_buddy") == 0) {
2590 return on_message_read_by_buddy(data, rec_msg);
2592 } else if (strcmp(rec_key_val, "media_download_completed") == 0) {
2594 return on_media_message_download_completed(data, rec_msg);
2596 } else if (strcmp(rec_key_val, "video_thumb_download_completed") == 0) {
2598 return on_video_message_thumb_download_completed(data, rec_msg);
2600 } else if (strcmp(rec_key_val, "add_contacts_request") == 0) {
2601 // Not to be handled.
2602 } else if (strcmp(rec_key_val, "new_buddy_added") == 0) {
2604 return on_new_buddy_added_to_contacts(data, rec_msg);
2606 } else if (strcmp(rec_key_val, "new_group_added") == 0) {
2608 return on_new_group_added(data, rec_msg);
2610 } else if (strcmp(rec_key_val, "contact_updated") == 0) {
2612 return on_buddy_contact_updated(data, rec_msg);
2614 } else if (strcmp(rec_key_val, "buddy_status_updated") == 0) {
2616 return on_buddy_status_updated(data, rec_msg);
2618 } else if (strcmp(rec_key_val, "type_status_updated") == 0) {
2620 return on_typing_status_updated(data, rec_msg);
2622 } else if (strcmp(rec_key_val, "user_status_updated") == 0) {
2624 return on_user_status_updated(data, rec_msg);
2633 int init_service(appdata_s *app)
2635 int result = SVC_RES_FAIL;
2636 RETVM_IF(!app, result, "Application data is NULL");
2638 app->service_client = service_client_create();
2639 RETVM_IF(!app->service_client, result, "Failed to create service client");
2641 result = service_client_register_port(app->service_client, TELEGRAM_CLIENT_PORT_NAME);
2642 if (result != SVC_RES_OK) {
2643 ERR("Failed to register service client port");
2644 service_client_destroy(app->service_client);
2645 app->service_client = NULL;
2649 result = _service_client_set_remote_data(app->service_client, TELEGRAM_SERVER_APP_NAME, TELEGRAM_SERVER_PORT_NAME);
2651 result = service_client_register_msg_receive_callback(app->service_client, _on_service_client_msg_received_cb, app);
2652 if (result != SVC_RES_OK) {
2653 ERR("Failed to register service client on message receive callback");
2654 service_client_destroy(app->service_client);
2655 app->service_client = NULL;
2661 void win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)
2667 tg_main_list_item_s* get_latest_item(appdata_s *ad, peer_with_pic_s *item)
2669 tg_main_list_item_s* main_list_item = NULL;
2673 tg_peer_info_s* peer_info = item->use_data;
2677 // get message from message table.
2679 char* tablename = get_table_name_from_number(peer_info->peer_id);
2680 tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2685 if (msg->message && strlen(msg->message) > 0) {
2686 item->last_message = strdup(msg->message);
2688 item->last_message = strdup(" ");
2692 int media_type = msg->media_type;
2693 if (media_type == tgl_message_media_none) {
2694 if (msg->message && strlen(msg->message) > 0) {
2695 item->last_message = strdup(msg->message);
2697 item->last_message = strdup(" ");
2699 } else if (media_type == tgl_message_media_photo) {
2700 item->last_message = strdup("Image");
2701 } else if (media_type == tgl_message_media_document) {
2702 tgl_media_s *media_msg = NULL;
2703 media_msg = get_media_details_from_db(atoll(msg->media_id));
2705 if (media_msg && media_msg->doc_type) {
2706 if (strlen(media_msg->doc_type) > 0) {
2707 item->last_message = strdup(media_msg->doc_type);
2709 item->last_message = strdup("Document");
2711 free_media_details(media_msg);
2713 if (msg->message && strlen(msg->message) > 0) {
2714 item->last_message = strdup(msg->message);
2716 item->last_message = strdup("Document");
2720 } else if (media_type == tgl_message_media_geo) {
2721 item->last_message = strdup("Geo location");
2722 } else if (media_type == tgl_message_media_contact) {
2723 item->last_message = strdup("Contact");
2724 } else if (media_type == tgl_message_media_unsupported) {
2725 item->last_message = strdup(" ");
2726 } else if (media_type == tgl_message_media_photo_encr) {
2727 item->last_message = strdup("Image encrypted");
2728 } else if (media_type == tgl_message_media_document_encr) {
2729 item->last_message = strdup("Document encrypted");
2731 item->last_message = strdup(" ");
2735 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2736 main_list_item->peer_id = peer_info->peer_id;
2737 main_list_item->peer_type = peer_info->peer_type;
2739 set_peer_names(peer_info, main_list_item);
2741 main_list_item->last_seen_time = msg->date;
2742 main_list_item->profile_pic = NULL;
2743 main_list_item->last_msg_id = msg->msg_id;
2744 main_list_item->last_message = strdup(item->last_message);
2745 main_list_item->last_msg_type = msg->media_type;
2746 main_list_item->is_out_msg = msg->out;
2747 main_list_item->last_msg_status = msg->msg_state;
2748 main_list_item->last_msg_service = msg->service;
2749 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2751 if (peer_info->photo_path) {
2752 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2754 main_list_item->profile_pic_path = NULL;
2756 main_list_item->user_name_lbl = NULL;
2757 main_list_item->status_lbl = NULL;
2758 main_list_item->date_lbl = NULL;
2759 main_list_item->msg_status_lbl = NULL;
2760 main_list_item->main_item_layout = NULL;
2762 // delete message object
2765 msg->message = NULL;
2768 if (msg->media_id) {
2769 free(msg->media_id);
2770 msg->media_id = NULL;
2777 item->last_message = strdup(" ");
2778 if (peer_info->peer_type == TGL_PEER_CHAT) {
2779 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2780 main_list_item->peer_id = peer_info->peer_id;
2781 main_list_item->peer_type = peer_info->peer_type;
2783 set_peer_names(peer_info, main_list_item);
2785 main_list_item->last_seen_time = peer_info->last_seen_time;
2786 main_list_item->profile_pic = NULL;
2787 main_list_item->last_message = strdup(item->last_message);
2788 main_list_item->last_msg_type = -1;
2789 main_list_item->last_msg_service = 0;
2790 main_list_item->is_out_msg = -1;
2791 main_list_item->last_msg_id = -1;
2792 main_list_item->last_msg_status = -1;
2793 main_list_item->number_of_unread_msgs = 0;
2795 if (peer_info->photo_path && strlen(peer_info->photo_path) > 0) {
2796 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2798 main_list_item->profile_pic_path = NULL;
2800 main_list_item->user_name_lbl = NULL;
2801 main_list_item->status_lbl = NULL;
2802 main_list_item->date_lbl = NULL;
2803 main_list_item->msg_status_lbl = NULL;
2804 main_list_item->main_item_layout = NULL;
2815 return main_list_item;
2818 void app_nf_back_cb(void *data, Evas_Object *obj, void *event_info)
2820 appdata_s *ad = data;
2821 if (ad->loading_popup) {
2822 show_toast(ad, "Please wait. request in progress");
2826 switch (ad->current_app_state) {
2827 case TG_INIT_SCREEN_STATE:
2828 elm_win_lower(ad->win);
2831 case TG_USER_MAIN_VIEW_STATE:
2832 if (!ad->menu_popup) {
2833 elm_win_lower(ad->win);
2837 case TG_CHAT_MESSAGING_VIEW_STATE:
2839 if (ad->main_item_in_cahtting_data) {
2840 int buddy_id = ad->main_item_in_cahtting_data->peer_id;
2841 char* tablename = get_table_name_from_number(buddy_id);
2843 Eina_Bool res = set_all_rows_read(tablename);
2849 tg_main_list_item_s* old_item = ad->main_item_in_cahtting_data;
2850 ad->main_list = eina_list_remove(ad->main_list, ad->main_item_in_cahtting_data);
2851 if (old_item->peer_print_name) {
2852 free(old_item->peer_print_name);
2853 old_item->peer_print_name = NULL;
2855 if (old_item->last_message) {
2856 free(old_item->last_message);
2857 old_item->last_message = NULL;
2859 if (old_item->profile_pic_path) {
2860 free(old_item->profile_pic_path);
2861 old_item->profile_pic_path = NULL;
2863 if (old_item->buddy_display_name) {
2864 free(old_item->buddy_display_name);
2865 old_item->buddy_display_name = NULL;
2868 old_item->date_lbl = NULL;
2869 old_item->msg_status_lbl = NULL;
2870 old_item->main_item_layout = NULL;
2871 old_item->profile_pic = NULL;
2872 old_item->profile_pic_path = NULL;
2873 old_item->status_lbl = NULL;
2874 old_item->user_name_lbl = NULL;
2875 ad->main_item_in_cahtting_data = NULL;
2876 if (ad->peer_in_cahtting_data) {
2877 peer_with_pic_s *item = ad->peer_in_cahtting_data;
2878 tg_main_list_item_s* latest_item = get_latest_item(ad, item);
2880 ad->main_list = eina_list_prepend(ad->main_list, latest_item);
2883 refresh_main_list_view(ad, EINA_FALSE);
2885 if (ad->peer_in_cahtting_data) {
2886 peer_with_pic_s *item = ad->peer_in_cahtting_data;
2888 int buddy_id = item->use_data->peer_id;
2889 char* tablename = get_table_name_from_number(buddy_id);
2890 Eina_Bool res = set_all_rows_read(tablename);
2896 tg_peer_info_s* peer_info = item->use_data;
2898 //if (peer_info->last_msg_id > 0) {
2900 // get message from message table.
2902 char* tablename = get_table_name_from_number(peer_info->peer_id);
2903 //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
2904 tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2909 if (msg->message && strlen(msg->message) > 0) {
2910 item->last_message = strdup(msg->message);
2912 item->last_message = strdup(" ");
2916 int media_type = msg->media_type;
2917 if (media_type == tgl_message_media_none) {
2918 if (msg->message && strlen(msg->message) > 0) {
2919 item->last_message = strdup(msg->message);
2921 item->last_message = strdup(" ");
2923 } else if (media_type == tgl_message_media_photo) {
2924 item->last_message = strdup("Image");
2925 } else if (media_type == tgl_message_media_document) {
2926 tgl_media_s *media_msg = NULL;
2927 media_msg = get_media_details_from_db(atoll(msg->media_id));
2929 if (media_msg && media_msg->doc_type) {
2930 if (strlen(media_msg->doc_type) > 0) {
2931 item->last_message = strdup(media_msg->doc_type);
2933 item->last_message = strdup("Document");
2935 free_media_details(media_msg);
2937 if (msg->message && strlen(msg->message) > 0) {
2938 item->last_message = strdup(msg->message);
2940 item->last_message = strdup("Document");
2944 } else if (media_type == tgl_message_media_geo) {
2945 item->last_message = strdup("Geo location");
2946 } else if (media_type == tgl_message_media_contact) {
2947 item->last_message = strdup("Contact");
2948 } else if (media_type == tgl_message_media_unsupported) {
2949 item->last_message = strdup(" ");
2950 } else if (media_type == tgl_message_media_photo_encr) {
2951 item->last_message = strdup("Image encrypted");
2952 } else if (media_type == tgl_message_media_document_encr) {
2953 item->last_message = strdup("Document encrypted");
2955 item->last_message = strdup(" ");
2959 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2960 main_list_item->peer_id = peer_info->peer_id;
2961 main_list_item->peer_type = peer_info->peer_type;
2963 set_peer_names(peer_info, main_list_item);
2965 main_list_item->last_seen_time = msg->date;
2966 main_list_item->profile_pic = NULL;
2967 main_list_item->last_msg_id = msg->msg_id;
2968 main_list_item->last_message = strdup(item->last_message);
2969 main_list_item->last_msg_type = msg->media_type;
2970 main_list_item->is_out_msg = msg->out;
2971 main_list_item->last_msg_status = msg->msg_state;
2972 main_list_item->last_msg_service = msg->service;
2973 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2975 if (peer_info->photo_path) {
2976 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2978 main_list_item->profile_pic_path = NULL;
2980 main_list_item->user_name_lbl = NULL;
2981 main_list_item->status_lbl = NULL;
2982 main_list_item->date_lbl = NULL;
2983 main_list_item->msg_status_lbl = NULL;
2984 main_list_item->main_item_layout = NULL;
2985 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
2987 // delete message object
2990 msg->message = NULL;
2993 if (msg->media_id) {
2994 free(msg->media_id);
2995 msg->media_id = NULL;
3002 item->last_message = strdup(" ");
3003 if (peer_info->peer_type == TGL_PEER_CHAT) {
3004 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
3005 main_list_item->peer_id = peer_info->peer_id;
3006 main_list_item->peer_type = peer_info->peer_type;
3008 set_peer_names(peer_info, main_list_item);
3010 main_list_item->last_seen_time = peer_info->last_seen_time;
3011 main_list_item->profile_pic = NULL;
3012 main_list_item->last_message = strdup(item->last_message);
3013 main_list_item->last_msg_type = -1;
3014 main_list_item->last_msg_service = 0;
3015 main_list_item->is_out_msg = -1;
3016 main_list_item->last_msg_id = -1;
3017 main_list_item->last_msg_status = -1;
3018 main_list_item->number_of_unread_msgs = 0;
3020 if (peer_info->photo_path) {
3021 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
3023 main_list_item->profile_pic_path = NULL;
3025 main_list_item->user_name_lbl = NULL;
3026 main_list_item->status_lbl = NULL;
3027 main_list_item->date_lbl = NULL;
3028 main_list_item->msg_status_lbl = NULL;
3029 main_list_item->main_item_layout = NULL;
3030 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
3041 refresh_main_list_view(ad, EINA_TRUE);
3045 elm_naviframe_item_pop(ad->nf);
3046 ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3047 show_floating_button(ad);
3049 case TG_SELECT_BUDDY_VIEW:
3050 elm_naviframe_item_pop(ad->nf);
3051 ad->current_app_state = TG_SET_CHAT_INFO_STATE;
3053 case TG_SET_USERNAME_STATE:
3054 elm_naviframe_item_pop(ad->nf);
3055 ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3058 case TG_ADD_CONTACT_STATE:
3059 elm_naviframe_item_pop(ad->nf);
3060 ad->current_app_state = TG_SET_USER_INFO_STATE;
3063 case TG_SET_CHAT_INFO_STATE:
3064 case TG_SET_USER_INFO_STATE:
3065 elm_naviframe_item_pop(ad->nf);
3066 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3068 case TG_USER_MAIN_VIEW_SELECTION_STATE:
3069 case TG_PEER_SEARCH_VIEW_STATE:
3070 case TG_START_MESSAGING_VIEW_STATE:
3071 case TG_SETTINGS_SCREEN_STATE:
3072 elm_naviframe_item_pop(ad->nf);
3073 ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3074 show_floating_button(ad);
3075 //evas_object_show(ad->panel);
3076 //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3078 case TG_SETTINGS_EDIT_NAME_STATE:
3079 elm_naviframe_item_pop(ad->nf);
3080 ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3081 delete_floating_button(ad);
3083 case TG_COUNTRY_SELECTION_VIEW:
3084 elm_naviframe_item_pop(ad->nf);
3085 ad->current_app_state = TG_REGISTRATION_STATE;
3087 case TG_REGISTRATION_STATE:
3088 elm_win_lower(ad->win);
3091 case TG_LOGIN_STATE:
3092 send_request_for_restart_server(ad, ad->service_client);
3093 if (ad->timer_value > 0) {
3094 Ecore_Timer* timer = evas_object_data_get(ad->nf, "code_timer");
3096 ecore_timer_del(timer);
3099 elm_naviframe_item_pop(ad->nf);
3100 ad->current_app_state = TG_REGISTRATION_STATE;
3102 case TG_PROFILE_REGISTRATION_STATE:
3103 /* ad->current_app_state = TG_REGISTRATION_STATE;
3104 elm_naviframe_item_pop(ad->nf);*/
3105 elm_win_lower(ad->win);
3108 case TG_BUDDY_LIST_STATE:
3109 evas_object_data_set(ad->nf, "buddy_list", NULL);
3110 elm_win_lower(ad->win);
3113 case TG_BUDDY_CHAT_CONV_STATE:
3114 ad->buddy_in_cahtting_data = NULL;
3115 if (ad->loaded_msg_list) {
3116 eina_list_free(ad->loaded_msg_list);
3117 ad->loaded_msg_list = NULL;
3119 elm_naviframe_item_pop(ad->nf);
3120 ad->current_app_state = TG_BUDDY_LIST_STATE;
3121 //evas_object_show(ad->panel);
3122 //refresh_buddy_list(ad);
3124 case TG_ADD_CONTACT_STATE:
3125 elm_naviframe_item_pop(ad->nf);
3126 if (ad->is_loading_from_profile_view) {
3127 ad->is_loading_from_profile_view = EINA_FALSE;
3128 ad->current_app_state = TG_SET_USER_INFO_STATE;
3129 } else if (ad->is_loading_from_msg_view) {
3130 ad->is_loading_from_msg_view = EINA_FALSE;
3131 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3133 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3134 show_floating_button(ad);
3137 case TG_BUDDY_LIST_SELECTION_STATE:
3139 Eina_List *l = NULL;
3140 user_data_with_pic_s *item = NULL;
3141 EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3144 user_data_s* user = item->use_data;
3145 user->is_selected = EINA_FALSE;
3147 elm_naviframe_item_pop(ad->nf);
3148 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3151 case TG_GROUP_CHAT_NAME_ENTRY_STATE:
3153 Eina_List *l = NULL;
3154 user_data_with_pic_s *item = NULL;
3155 EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3159 user_data_s* user = item->use_data;
3160 user->is_selected = EINA_FALSE;
3162 elm_naviframe_item_pop(ad->nf);
3163 ad->current_app_state = TG_BUDDY_LIST_SELECTION_STATE;
3164 //evas_object_show(ad->panel);
3165 //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3166 //refresh_buddy_list(ad);
3175 layout_back_cb(void *data, Evas_Object *obj, void *event_info)
3177 appdata_s *ad = data;
3178 /* Let window go to hide state. */
3179 elm_win_lower(ad->win);
3182 void _btn_clicked(void* data, Evas_Object* btn, void* ev)
3184 appdata_s *ad = data;
3185 bundle *msg = bundle_create();
3186 if (bundle_add_str(msg, "tizen_app", "Hello service") != 0) {
3187 ERR("Failed to add data by key to bundle");
3191 int result = _app_send_response(ad, msg);
3192 if (result != SVC_RES_OK) {
3198 Eina_Bool on_load_main_view_requested(void *data)
3200 appdata_s *ad = data;
3202 elm_naviframe_item_pop(ad->nf);
3203 hide_loading_popup(ad);
3204 ad->is_loading_from_msg_view = EINA_FALSE;
3205 ad->is_loading_from_profile_view = EINA_FALSE;
3206 launch_user_main_view_cb(ad);
3207 int unread_msg_cnt = get_number_of_unread_messages();
3208 if (unread_msg_cnt <= 0) {
3209 badge_set_count(TELEGRAM_APP_ID, 0);
3210 return ECORE_CALLBACK_CANCEL;
3213 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3214 if (BADGE_ERROR_NONE != err) {
3218 return ECORE_CALLBACK_CANCEL;
3221 Eina_Bool on_init_view_requested(void *data)
3223 appdata_s *ad = data;
3225 Eina_List *user_info = get_registered_user_info();
3227 elm_naviframe_item_pop(ad->nf);
3228 ad->current_app_state = TG_REGISTRATION_STATE;
3229 launch_init_screen(ad);
3231 //show_toast(ad, "user already registered");
3232 load_registered_user_data(ad);
3233 load_buddy_list_data(ad);
3234 load_unknown_buddy_list_data(ad);
3236 load_main_list_data(ad);
3237 eina_list_free(user_info);
3238 ecore_timer_add(2, on_load_main_view_requested, ad);
3241 return ECORE_CALLBACK_CANCEL;
3244 void on_tg_service_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
3246 if (result == APP_CONTROL_ERROR_NONE)
3247 LOGE("Server launched successfully.");
3249 LOGE("Server not launched.");
3252 void launch_tg_server(void *data)
3254 appdata_s *ad = data;
3255 app_control_h app_control = NULL;
3256 int ret = APP_CONTROL_ERROR_NONE;
3258 ret = app_control_create(&app_control);
3259 ret_if (APP_CONTROL_ERROR_NONE != ret);
3261 ret = app_control_set_app_id(app_control, TELEGRAM_SERVER_APP_NAME);
3262 goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3264 ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
3265 goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3267 ret = app_control_send_launch_request(app_control, &on_tg_service_result_cb, ad);
3268 goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3271 app_control_destroy(app_control);
3274 static void create_base_gui(appdata_s *ad)
3276 ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
3277 elm_win_conformant_set(ad->win, EINA_TRUE);
3278 elm_win_autodel_set(ad->win, EINA_TRUE);
3280 elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
3281 /* Telegram guideline 720 x 1280 */
3282 elm_app_base_scale_set(2.6);
3284 if (elm_win_wm_rotation_supported_get(ad->win)) {
3285 int rots[2] = {0, 180};
3286 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), sizeof(rots) / sizeof(int));
3288 ad->target_direction = tg_get_device_orientation();
3289 char edj_path[PATH_MAX] = {0, };
3290 app_get_resource(TELEGRAM_CUSTOM_WINSET_EDJ, edj_path, (int)PATH_MAX);
3292 Elm_Theme *theme = elm_theme_new();
3293 elm_theme_extension_add(NULL, edj_path);
3295 evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
3297 ad->conform = elm_conformant_add(ad->win);
3298 evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3299 elm_win_resize_object_add(ad->win, ad->conform);
3300 evas_object_show(ad->conform);
3302 ad->layout = elm_layout_add(ad->conform);
3303 evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3305 //elm_layout_theme_set(ad->layout, "layout", "drawer", "panel");
3306 elm_layout_theme_set(ad->layout, "layout", "application", "default");
3307 evas_object_show(ad->layout);
3309 elm_object_content_set(ad->conform, ad->layout);
3310 ad->nf = elm_naviframe_add(ad->layout);
3312 ad->current_app_state = TG_SPLASH_SCREEN_STATE;
3313 launch_splash_screen_cb(ad);
3314 elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
3315 eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, app_nf_back_cb, ad);
3317 evas_object_show(ad->win);
3319 // check for user info
3322 char *chat_bg = NULL;
3323 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
3325 ad->chat_background = strdup(chat_bg);
3328 if (!ad->chat_background) {
3329 ad->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
3330 preference_set_string(TG_CHAT_BG_PREFERENCE, ad->chat_background);
3332 launch_tg_server(ad);
3333 ecore_timer_add(5, on_init_view_requested, ad);
3338 char *build_a_path(const char *path, const char *filename)
3343 len = strlen(path) + strlen(filename) + 2;
3349 snprintf(ret, len, "%s/%s", path, filename);
3353 int remove_directory(const char *path)
3355 DIR *d = opendir(path);
3356 size_t path_len = strlen(path);
3365 while (!r && (p=readdir(d)))
3371 /* Skip the names "." and ".." as we don't want to recurse on them. */
3372 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
3377 len = path_len + strlen(p->d_name) + 2;
3382 struct stat statbuf;
3384 snprintf(buf, len, "%s/%s", path, p->d_name);
3386 if (!stat(buf, &statbuf))
3388 if (S_ISDIR(statbuf.st_mode))
3390 r2 = remove_directory(buf);
3415 Eina_Bool copy_file_another_dir(const char *source_file, const char *target_file)
3417 FILE *source, *target;
3419 source = fopen(source_file, "rb+");
3426 target = fopen(target_file, "wb+");
3434 char buffer[1024] = {0}; /*Buffer to store files content*/
3437 while((size = fread(buffer, 1, 1024, source)) != 0)
3439 fwrite(buffer, 1, size, target);
3447 void recursive_dir_copy(const char *source_dir, const char *target_dir)
3450 struct dirent *src_files;
3451 Eina_List *src_dir_stack;
3452 Eina_List *tar_dir_stack;
3458 src_dir_stack = NULL;
3459 tar_dir_stack = NULL;
3461 src_dir = strdup(source_dir);
3462 tar_dir = strdup(target_dir);
3465 open_src = opendir(src_dir);
3466 while((src_files = readdir(open_src))) {
3467 if (src_files->d_type == DT_DIR) {
3470 if (src_files->d_name[0] == '.' && (src_files->d_name[1] == '\0' || (src_files->d_name[1] == '.' && src_files->d_name[2] == '\0'))) {
3474 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3475 mkdir(temp_dest, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3477 char *temp_src = build_a_path(src_dir, src_files->d_name);
3479 src_dir_stack = eina_list_prepend(src_dir_stack, temp_src);
3480 tar_dir_stack = eina_list_prepend(tar_dir_stack, temp_dest);
3483 // copy file to target src_dir.
3484 char *temp_src = build_a_path(src_dir, src_files->d_name);
3485 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3486 //int ret = rename(temp_src, temp_dest);
3487 Eina_Bool ret = copy_file_another_dir(temp_src, temp_dest);
3489 // file copy success
3491 // file copy failed.
3504 src_dir = eina_list_nth(src_dir_stack, 0);
3506 src_dir_stack = eina_list_remove(src_dir_stack, src_dir);
3508 tar_dir = eina_list_nth(tar_dir_stack, 0);
3510 tar_dir_stack = eina_list_remove(tar_dir_stack, tar_dir);
3512 } while (src_dir != NULL);
3515 void move_downloaded_files()
3517 // create download directory.
3518 struct stat st = {0};
3519 char dest_download_dir[512] = {0,};
3520 sprintf(dest_download_dir, "%s/%s", app_get_shared_data_path(), "downloads");
3521 if (stat(dest_download_dir, &st) == -1) {
3522 mkdir(dest_download_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3525 LOGE("dest Download directory: %s", dest_download_dir);
3526 // create download directory.
3527 char target_download_dir[512] = {0,};
3528 sprintf(target_download_dir, "%s/%s/%s", app_get_data_path(), "telegram_tizen", "downloads");
3530 LOGE("target Download directory: %s", target_download_dir);
3531 recursive_dir_copy(target_download_dir, dest_download_dir);
3532 remove_directory(target_download_dir);
3536 Eina_Bool dirExists(const char *path)
3540 if (stat(path, &info) != 0)
3542 else if (info.st_mode & S_IFDIR)
3548 static void update_downloaded_video_thumb_paths(const char *table_name, const char *col_one, const char *column_two)
3550 LOGE("Update media info table thumb files");
3551 if (!table_name || !col_one || !column_two)
3554 Eina_List *col_types = NULL;
3555 col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER);
3556 col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT);
3558 Eina_List *col_names = NULL;
3559 col_names = eina_list_append(col_names, col_one);
3560 col_names = eina_list_append(col_names, column_two);
3562 Eina_List *vals = get_values_from_table_sync(table_name, col_names, col_types, NULL, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED);
3563 int val_size = eina_list_count(vals);
3564 if (vals && val_size > 0) {
3565 Eina_List *row_vals = NULL;
3566 EINA_LIST_FREE(vals, row_vals) {
3568 char *file_path = NULL;
3570 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
3572 user_id = *temp_user_id;
3576 file_path = (char *)eina_list_nth(row_vals, 1);
3579 eina_list_free(row_vals);
3583 if (strlen(file_path) > 0 && strncmp(file_path, "/opt/usr/media/telegram/telegram_tizen", strlen("/opt/usr/media/telegram/telegram_tizen")) == 0) {
3584 // update file name in db
3585 char *new_path = str_replace(new_path, "/opt/usr/media/telegram/telegram_tizen", app_get_shared_data_path());
3588 eina_list_free(row_vals);
3591 LOGE("Old file path: %s", file_path);
3592 LOGE("New file path: %s", new_path);
3595 Eina_List *new_col_types = NULL;
3596 new_col_types = eina_list_append(new_col_types, TG_DB_COLUMN_TEXT);
3598 Eina_List *new_col_names = NULL;
3599 new_col_names = eina_list_append(new_col_names, column_two);
3601 Eina_List *new_col_values = NULL;
3602 new_col_values = eina_list_append(new_col_values, new_path);
3604 char* where_clause = NULL;
3606 int str_size = strlen(col_one) + strlen(" = ") + 10 + 1;
3607 where_clause = (char *)malloc(str_size);
3608 snprintf(where_clause, str_size, "%s = %d", col_one, user_id);
3609 Eina_Bool ret = update_table(table_name, new_col_names, new_col_types, new_col_values, where_clause);
3611 LOGE("Media info table update failed.");
3613 LOGE("Media info table update success.");
3616 eina_list_free(new_col_types);
3617 eina_list_free(new_col_names);
3618 eina_list_free(new_col_values);
3623 eina_list_free(row_vals);
3626 eina_list_free(col_types);
3627 eina_list_free(col_names);
3630 void update_downloaded_video_thumb_paths_in_media_info_table()
3632 update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_DOCUMENT_THUMB_FILE);
3636 void update_downloaded_file_paths_in_media_info_table()
3638 update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_FILE_PATH);
3642 void update_downloaded_file_paths_in_chat_info_table()
3644 update_downloaded_video_thumb_paths(CHAT_INFO_TABLE_NAME, CHAT_INFO_TABLE_CHAT_ID, CHAT_INFO_TABLE_PHOTO_PATH);
3648 void update_downloaded_file_paths_in_buddy_info_table(const char *table_name)
3650 LOGE("Update %s", table_name);
3651 update_downloaded_video_thumb_paths(table_name, USER_INFO_TABLE_USER_ID, USER_INFO_TABLE_PHOTO_PATH);
3654 static bool app_create(void *data)
3657 Hook to take necessary actions before main event loop starts
3658 Initialize UI resources and application's data
3659 If this function returns true, the main loop of application starts
3660 If this function returns false, the application is terminated
3663 appdata_s *ad = data;
3664 if (dirExists(OLD_DIR)) {
3665 char *new_dir = app_get_data_path();
3666 recursive_dir_copy(OLD_DIR, new_dir);
3667 remove_directory(OLD_DIR);
3668 move_downloaded_files();
3669 // update downloaded image paths in DB
3670 update_downloaded_file_paths_in_buddy_info_table(BUDDY_INFO_TABLE_NAME);
3671 update_downloaded_file_paths_in_buddy_info_table(USER_INFO_TABLE_NAME);
3672 update_downloaded_file_paths_in_buddy_info_table(PEER_INFO_TABLE_NAME);
3673 update_downloaded_file_paths_in_chat_info_table();
3674 update_downloaded_file_paths_in_media_info_table();
3675 update_downloaded_video_thumb_paths_in_media_info_table();
3679 bindtextdomain(PACKAGE, "/opt/usr/apps/org.tizen.telegram_tizen_client/res/locale");
3681 ad->phone_number = NULL;
3682 ad->buddy_list = NULL;
3683 ad->unknown_buddy_list = NULL;
3684 ad->known_buddy_list = NULL;
3685 ad->main_list = NULL;
3686 ad->peer_list = NULL;
3687 ad->search_peer_list = NULL;
3688 ad->is_first_time_registration = EINA_FALSE;
3690 ad->loaded_msg_list = NULL;
3691 ad->loading_popup = NULL;
3692 ad->current_user_data = NULL;
3693 ad->is_tg_initilized = EINA_FALSE;
3694 ad->chat_background = NULL;
3695 ad->msg_popup = NULL;
3696 ad->s_notififcation = NULL;
3698 ad->is_server_ready = EINA_FALSE;
3699 ad->is_waiting_for_phone_num = EINA_FALSE;
3700 ad->login_timer = NULL;
3701 ad->contact_list = NULL;
3702 ad->main_item = NULL;
3703 ad->country_codes_list = NULL;
3704 ad->country_names_list = NULL;
3705 //ad->msg_count = 0;
3706 create_base_gui(ad);
3707 int err = badge_new(TELEGRAM_APP_ID);
3708 if (BADGE_ERROR_NONE != err) {
3716 app_control(app_control_h app_control, void *data)
3718 /* Handle the launch request. */
3722 app_pause(void *data)
3724 appdata_s *app_data = data;
3726 app_data->s_app_visible_state = APP_STATE_IN_BACKGROUND;
3727 int unread_msg_cnt = get_number_of_unread_messages();
3728 if (unread_msg_cnt <= 0) {
3729 badge_set_count(TELEGRAM_APP_ID, 0);
3732 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3733 if (BADGE_ERROR_NONE != err) {
3740 app_resume(void *data)
3742 appdata_s *app_data = data;
3744 app_data->s_app_visible_state = APP_STATE_IN_FOREGROUND;
3749 void free_app_data(appdata_s *app_data, Eina_Bool destroy_server)
3755 int unread_msg_cnt = get_number_of_unread_messages();
3756 if (unread_msg_cnt > 0) {
3757 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3758 if (BADGE_ERROR_NONE != err) {
3762 badge_set_count(TELEGRAM_APP_ID, 0);
3764 eina_list_free(app_data->country_codes_list);
3765 eina_list_free(app_data->country_names_list);
3766 free(app_data->country_code_buffer);
3768 if (app_data->panel) {
3769 Evas_Object *panel_list = evas_object_data_get(app_data->panel, "panel_list");
3771 evas_object_del(panel_list);
3773 evas_object_del(app_data->panel);
3774 app_data->panel = NULL;
3777 if (app_data->phone_number) {
3778 free(app_data->phone_number);
3779 app_data->phone_number = NULL;
3782 if (app_data->sms_code) {
3783 free(app_data->sms_code);
3784 app_data->sms_code = NULL;
3787 if (app_data->current_user_data) {
3788 free_user_data(app_data->current_user_data);
3790 if (app_data->chat_background) {
3791 app_data->chat_background = NULL;
3793 if (app_data && app_data->s_notififcation) {
3794 notification_delete_all(NOTIFICATION_TYPE_NOTI);
3795 app_data->s_notififcation = NULL;
3798 peer_with_pic_s* pic_item = NULL;
3799 EINA_LIST_FREE(app_data->peer_list, pic_item) {
3802 tg_peer_info_s* item = pic_item->use_data;
3804 if (item->print_name) {
3805 free(item->print_name);
3806 item->print_name = NULL;
3808 if (item->photo_path) {
3809 free(item->photo_path);
3810 item->photo_path = NULL;
3812 pic_item->contact_icon = NULL;
3813 pic_item->msg_object = NULL;
3814 pic_item->name_object = NULL;
3819 app_data->peer_list = NULL;
3821 tg_main_list_item_s* main_item = NULL;
3822 EINA_LIST_FREE(app_data->main_list, main_item) {
3824 if (main_item->peer_print_name) {
3825 free(main_item->peer_print_name);
3826 main_item->peer_print_name = NULL;
3828 if (main_item->last_message) {
3829 free(main_item->last_message);
3830 main_item->last_message = NULL;
3832 if (main_item->profile_pic_path) {
3833 free(main_item->profile_pic_path);
3834 main_item->profile_pic_path = NULL;
3836 if (main_item->buddy_display_name) {
3837 free(main_item->buddy_display_name);
3838 main_item->buddy_display_name = NULL;
3840 main_item->date_lbl = NULL;
3841 main_item->msg_status_lbl = NULL;
3842 main_item->main_item_layout = NULL;
3843 main_item->profile_pic = NULL;
3844 main_item->profile_pic_path = NULL;
3845 main_item->status_lbl = NULL;
3846 main_item->user_name_lbl = NULL;
3848 app_data->main_list = NULL;
3850 user_data_with_pic_s* item = NULL;
3851 EINA_LIST_FREE(app_data->buddy_list, item) {
3854 user_data_s* user_data = item->use_data;
3856 if (user_data->print_name) {
3857 free(user_data->print_name);
3858 user_data->print_name = NULL;
3861 if (user_data->photo_path) {
3862 free(user_data->photo_path);
3863 user_data->photo_path = NULL;
3866 if (user_data->first_name) {
3867 free(user_data->first_name);
3868 user_data->first_name = NULL;
3871 if (user_data->last_name) {
3872 free(user_data->last_name);
3873 user_data->last_name = NULL;
3876 if (user_data->phone) {
3877 free(user_data->phone);
3878 user_data->phone = NULL;
3880 if (user_data->real_first_name) {
3881 free(user_data->real_first_name);
3882 user_data->real_first_name = NULL;
3885 if (user_data->real_last_name) {
3886 free(user_data->real_last_name);
3887 user_data->real_last_name = NULL;
3890 if (user_data->username) {
3891 free(user_data->username);
3892 user_data->username = NULL;
3897 app_data->buddy_list = NULL;
3900 EINA_LIST_FREE(app_data->unknown_buddy_list, item) {
3905 user_data_s* user_data = item->use_data;
3907 if (user_data->print_name) {
3908 free(user_data->print_name);
3909 user_data->print_name = NULL;
3912 if (user_data->photo_path) {
3913 free(user_data->photo_path);
3914 user_data->photo_path = NULL;
3917 if (user_data->first_name) {
3918 free(user_data->first_name);
3919 user_data->first_name = NULL;
3922 if (user_data->last_name) {
3923 free(user_data->last_name);
3924 user_data->last_name = NULL;
3927 if (user_data->phone) {
3928 free(user_data->phone);
3929 user_data->phone = NULL;
3931 if (user_data->real_first_name) {
3932 free(user_data->real_first_name);
3933 user_data->real_first_name = NULL;
3936 if (user_data->real_last_name) {
3937 free(user_data->real_last_name);
3938 user_data->real_last_name = NULL;
3941 if (user_data->username) {
3942 free(user_data->username);
3943 user_data->username = NULL;
3948 app_data->unknown_buddy_list = NULL;
3950 EINA_LIST_FREE(app_data->known_buddy_list, item) {
3951 user_data_s* user_data = item->use_data;
3953 if (user_data->print_name)
3954 free(user_data->print_name);
3955 user_data->print_name = NULL;
3957 if (user_data->photo_path)
3958 free(user_data->photo_path);
3959 user_data->photo_path = NULL;
3961 if (user_data->first_name)
3962 free(user_data->first_name);
3963 user_data->first_name = NULL;
3965 if (user_data->last_name)
3966 free(user_data->last_name);
3967 user_data->last_name = NULL;
3969 if (user_data->phone)
3970 free(user_data->phone);
3971 user_data->phone = NULL;
3973 if (user_data->real_first_name)
3974 free(user_data->real_first_name);
3975 user_data->real_first_name = NULL;
3977 if (user_data->real_last_name)
3978 free(user_data->real_last_name);
3979 user_data->real_last_name = NULL;
3981 if (user_data->username)
3982 free(user_data->username);
3983 user_data->username = NULL;
3988 app_data->known_buddy_list = NULL;
3990 eina_list_free(app_data->loaded_msg_list);
3991 app_data->loaded_msg_list = NULL;
3993 if (app_data->service_client && destroy_server) {
3994 service_client_destroy(app_data->service_client);
3995 app_data->service_client = NULL;
3997 free_contact_list(app_data->contact_list);
4002 app_terminate(void *data)
4004 free_app_data(data, EINA_TRUE);
4008 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
4010 /*APP_EVENT_LANGUAGE_CHANGED*/
4011 char *locale = NULL;
4012 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
4014 setlocale(LC_ALL, locale);
4015 elm_language_set(locale);
4022 TelegramAppDirection tg_get_device_orientation()
4024 TelegramAppDirection target_direction;
4026 switch (app_get_device_orientation()) {
4027 case APP_DEVICE_ORIENTATION_90:
4028 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4030 case APP_DEVICE_ORIENTATION_180:
4031 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4033 case APP_DEVICE_ORIENTATION_270:
4034 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4036 case APP_DEVICE_ORIENTATION_0:
4038 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4042 return target_direction;
4045 void update_current_view(appdata_s *ad)
4050 switch (ad->current_app_state) {
4051 case TG_USER_MAIN_VIEW_STATE:
4052 refresh_main_list_view(ad, EINA_FALSE);
4054 case TG_CHAT_MESSAGING_VIEW_STATE:
4055 refresh_messaging_view(ad);
4064 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
4066 appdata_s *ad = user_data;
4070 TelegramAppDirection target_direction;
4071 app_device_orientation_e orientation;
4073 if (app_event_get_device_orientation(event_info, &orientation) != 0) {
4077 switch (orientation) {
4078 case APP_DEVICE_ORIENTATION_90:
4079 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4081 case APP_DEVICE_ORIENTATION_180:
4082 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4084 case APP_DEVICE_ORIENTATION_270:
4085 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4087 case APP_DEVICE_ORIENTATION_0:
4089 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4093 if (target_direction != ad->target_direction) {
4094 ad->target_direction = target_direction;
4095 update_current_view(ad);
4101 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
4105 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
4109 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
4113 int main(int argc, char *argv[])
4115 appdata_s ad = {0,};
4118 ui_app_lifecycle_callback_s event_callback = {0,};
4119 app_event_handler_h handlers[5] = {NULL, };
4121 event_callback.create = app_create;
4122 event_callback.terminate = app_terminate;
4123 event_callback.pause = app_pause;
4124 event_callback.resume = app_resume;
4125 event_callback.app_control = app_control;
4127 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
4128 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
4129 ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
4130 ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
4131 ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
4132 ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
4134 ret = ui_app_main(argc, argv, &event_callback, &ad);
4135 if (ret != APP_ERROR_NONE) {
4136 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main() is failed. err = %d", ret);