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;
1234 if (!app->peer_in_cahtting_data || !app->peer_in_cahtting_data->use_data) {
1235 LOGE("Wrong status, previous chatting data are freed!!!");
1240 char* from_id_str = NULL;
1241 result = bundle_get_str(rec_msg, "from_id", &from_id_str);
1242 int from_id = atoi(from_id_str);
1244 char* to_id_str = NULL;
1245 result = bundle_get_str(rec_msg, "to_id", &to_id_str);
1246 int to_id = atoi(to_id_str);
1248 char* msg_id_str = NULL;
1249 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1250 long long message_id = atoll(msg_id_str);
1252 char* type_of_chat_str = NULL;
1253 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1254 int type_of_chat = atoi(type_of_chat_str);
1256 if (from_id == app->user_id.id) {
1257 // sent by the user. May be from another device.
1260 //load message from the received info.
1261 // get phone number using buddy id.
1263 Eina_Bool is_user_present_in_main_list = EINA_FALSE;
1265 if (type_of_chat == TGL_PEER_USER) {
1266 id_to_check = from_id;
1268 id_to_check = to_id;
1271 if (type_of_chat == TGL_PEER_USER) {
1272 if ((app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state == TG_SET_USER_INFO_STATE) &&
1273 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == from_id) {
1275 char* date_id_str = NULL;
1276 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1277 int date_id = atoi(date_id_str);
1278 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1281 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1286 } else if (type_of_chat == TGL_PEER_CHAT) {
1287 if ((app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE || app->current_app_state == TG_SET_CHAT_INFO_STATE) &&
1288 app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
1289 char* tablename = get_table_name_from_number(to_id);
1290 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1294 char* date_id_str = NULL;
1295 result = bundle_get_str(rec_msg, "date_id", &date_id_str);
1296 int date_id = atoi(date_id_str);
1297 on_text_message_received_from_buddy(app, date_id, type_of_chat);
1299 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1301 on_text_message_received_from_buddy(app, message_id, type_of_chat);
1305 msg->message = NULL;
1308 if (msg->media_id) {
1309 free(msg->media_id);
1310 msg->media_id = NULL;
1319 Eina_List *l = NULL;
1320 tg_main_list_item_s *sel_item = NULL;
1321 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1323 if (sel_item->peer_id != id_to_check) {
1327 // update last message
1328 char* tablename = get_table_name_from_number(sel_item->peer_id);
1329 tg_message_s* msg = get_message_from_message_table(message_id, tablename);
1332 if (msg->service == 2) {
1335 msg->message = NULL;
1338 if (msg->media_id) {
1339 free(msg->media_id);
1340 msg->media_id = NULL;
1346 if (msg->service == 1) {
1347 if (msg->message && strlen(msg->message) > 0) {
1348 sel_item->last_message = strdup(msg->message);
1350 sel_item->last_message = strdup(" ");
1354 int media_type = msg->media_type;
1355 if (media_type == tgl_message_media_none) {
1356 if (msg->message && strlen(msg->message) > 0) {
1357 sel_item->last_message = strdup(msg->message);
1359 sel_item->last_message = strdup(" ");
1361 } else if (media_type == tgl_message_media_photo) {
1362 sel_item->last_message = strdup("Image");
1363 } else if (media_type == tgl_message_media_document) {
1366 if (msg->message && strlen(msg->message) > 0) {
1367 sel_item->last_message = strdup(msg->message);
1369 sel_item->last_message = strdup("Document");
1372 tgl_media_s *media_msg = NULL;
1373 media_msg = get_media_details_from_db(atoll(msg->media_id));
1374 sel_item->last_message = strdup(media_msg->doc_type);
1375 free_media_details(media_msg);
1377 } else if (media_type == tgl_message_media_geo) {
1378 sel_item->last_message = strdup("Geo location");
1379 } else if (media_type == tgl_message_media_contact) {
1380 sel_item->last_message = strdup("Contact");
1381 } else if (media_type == tgl_message_media_unsupported) {
1382 sel_item->last_message = strdup(" ");
1383 } else if (media_type == tgl_message_media_photo_encr) {
1384 sel_item->last_message = strdup("Image encrypted");
1385 } else if (media_type == tgl_message_media_document_encr) {
1386 sel_item->last_message = strdup("Document encrypted");
1388 sel_item->last_message = strdup(" ");
1391 sel_item->last_msg_id = msg->msg_id;
1392 sel_item->last_msg_type = msg->media_type;
1393 sel_item->is_out_msg = msg->out;
1394 sel_item->last_msg_status = msg->msg_state;
1395 sel_item->last_seen_time = msg->date;
1396 sel_item->number_of_unread_msgs = get_unread_message_count(tablename);
1398 if (sel_item->profile_pic) {
1402 if (sel_item->user_name_lbl) {
1406 if (sel_item->status_lbl) {
1407 char* org_msg = NULL;
1408 if (sel_item->last_message) {
1409 org_msg = sel_item->last_message;
1413 elm_object_text_set(sel_item->status_lbl, "");
1415 char status_buf[126] = {'\0'};
1418 if (msg->service == 1 || msg->service == 2) {
1419 sel_item->last_msg_service = 1;
1422 sel_item->last_msg_service = 0;
1426 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=%s align=left><font_size=26>%.*s</font_size></font>", color, 40, org_msg);
1427 elm_object_text_set(sel_item->status_lbl, status_buf);
1430 if (sel_item->date_lbl) {
1431 Eina_Bool is_today = compare_date_with_current_date(sel_item->last_seen_time);
1434 elm_object_text_set(sel_item->date_lbl, "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>Today</font_size></font>");
1438 char time_str[256] = { 0, };
1441 t = sel_item->last_seen_time;
1442 (void)localtime_r(&t, <);
1443 if (strftime(res, sizeof(res), "%d/%b/%Y", <) == 0) {
1444 strcpy(res, "unknown");
1447 snprintf(time_str, sizeof(time_str), "<font=Tizen:style=Normal color=#000000 align=center><font_size=25>%s</font_size></font>", res);
1448 elm_object_text_set(sel_item->date_lbl, time_str);
1451 if (sel_item->msg_status_lbl) {
1452 Evas_Object* temp_lbl = elm_object_part_content_get(sel_item->msg_status_lbl, "swallow.status");
1454 evas_object_del(temp_lbl);
1457 Evas_Object* num_lbl = elm_label_add(app->nf);
1458 elm_object_style_set(num_lbl, "transparent");
1459 char unread_msgs[256];
1460 sprintf(unread_msgs, "<font=Tizen:style=Bold color=#ffffff align=center><font_size=25>%d</font_size></font>", sel_item->number_of_unread_msgs);
1461 elm_object_text_set(num_lbl, unread_msgs);
1462 evas_object_size_hint_weight_set(num_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1463 evas_object_size_hint_align_set(num_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
1464 evas_object_show(num_lbl);
1466 char edj_path[PATH_MAX] = {0, };
1467 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
1468 Evas_Object* msg_status = elm_layout_add(app->nf);
1469 elm_layout_file_set(msg_status, edj_path, "circle_item");
1470 evas_object_size_hint_weight_set(msg_status, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1471 evas_object_size_hint_align_set(msg_status, EVAS_HINT_FILL, EVAS_HINT_FILL);
1472 evas_object_show(msg_status);
1473 elm_object_part_content_set(msg_status, "content", num_lbl);
1475 elm_object_part_content_set(sel_item->msg_status_lbl, "swallow.status", msg_status);
1480 msg->message = NULL;
1483 if (msg->media_id) {
1484 free(msg->media_id);
1485 msg->media_id = NULL;
1491 is_user_present_in_main_list = EINA_TRUE;
1493 app->main_list = eina_list_remove(app->main_list, sel_item);
1494 app->main_list = eina_list_prepend(app->main_list, sel_item);
1495 refresh_main_list_view(app, EINA_FALSE);
1499 if (!is_user_present_in_main_list) {
1500 peer_with_pic_s *peer_item = get_peer_info(id_to_check);
1502 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
1504 app->main_list = eina_list_prepend(app->main_list, latest_item);
1505 refresh_main_list_view(app, EINA_TRUE);
1510 if (app->s_app_visible_state == APP_STATE_IN_BACKGROUND || app->current_app_state != TG_USER_MAIN_VIEW_STATE) {
1511 // show notification
1512 char *icon_path = (char *)ui_utils_get_resource(DEFAULT_TELEGRAM_ICON);
1513 char *title = "Telegram";
1516 int unread_msg_cnt = get_number_of_unread_messages();
1518 if (unread_msg_cnt <= 0) {
1519 badge_set_count(TELEGRAM_APP_ID, 0);
1523 sprintf(content, "%d new messages received.", unread_msg_cnt);
1525 char *sound_track = NULL;
1526 char *app_id = TELEGRAM_APP_ID;
1527 tg_notification_create(app, icon_path, title, content, sound_track, app_id);
1528 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
1529 if (BADGE_ERROR_NONE != err) {
1537 static int on_message_with_date_received_from_buddy(appdata_s *app, bundle *const rec_msg)
1539 return on_message_received_from_buddy(app, rec_msg, EINA_TRUE);
1542 static int on_new_contact_added(appdata_s *app, bundle *const rec_msg)
1544 int result = SVC_RES_FAIL;
1545 char* buddy_id_str = NULL;
1546 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1547 int buddy_id = atoi(buddy_id_str);
1549 Eina_Bool is_success = EINA_FALSE;
1550 char* is_success_val = NULL;
1551 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1552 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1553 is_success = EINA_TRUE;
1555 is_success = EINA_FALSE;
1558 load_buddy_list_data(app);
1559 load_peer_data(app);
1561 //show_toast(app, "Failed to add contact. Please try after sometime.");
1563 hide_loading_popup(app);
1564 if (app->current_app_state == TG_ADD_CONTACT_STATE) {
1565 on_new_contact_added_response_received(app, buddy_id, is_success);
1570 static int on_buddy_re_added_to_chat(appdata_s *app, bundle *const rec_msg)
1572 int result = SVC_RES_FAIL;
1573 char* buddy_id_str = NULL;
1574 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1575 int buddy_id = atoi(buddy_id_str);
1577 Eina_Bool is_success = EINA_FALSE;
1578 char* is_success_val = NULL;
1579 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1580 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1581 is_success = EINA_TRUE;
1583 is_success = EINA_FALSE;
1586 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1587 on_user_added_response_received(app, buddy_id, is_success);
1589 hide_loading_popup(app);
1593 static int on_buddy_deleted_from_friends_list(appdata_s *ad, bundle *const rec_msg)
1595 int result = SVC_RES_FAIL;
1596 char* buddy_id_str = NULL;
1597 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1598 int buddy_id = atoi(buddy_id_str);
1600 Eina_Bool is_success = EINA_FALSE;
1601 char* is_success_val = NULL;
1602 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1603 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1604 is_success = EINA_TRUE;
1606 is_success = EINA_FALSE;
1609 if (is_success && ad->current_app_state == TG_SET_USER_INFO_STATE) {
1610 //on_user_deleted_response_received(app, buddy_id, is_success);
1611 show_loading_popup(ad);
1612 elm_naviframe_item_pop(ad->nf);
1613 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
1614 ad->peer_in_cahtting_data = NULL;
1615 ad->main_item_in_cahtting_data = NULL;
1616 ad->buddy_in_cahtting_data = NULL;
1617 elm_naviframe_item_pop(ad->nf);
1618 load_registered_user_data(ad);
1619 load_buddy_list_data(ad);
1620 load_unknown_buddy_list_data(ad);
1622 load_main_list_data(ad);
1625 Eina_List *l = NULL;
1626 tg_main_list_item_s* main_list_item = NULL;
1627 EINA_LIST_FOREACH(ad->main_list, l, main_list_item) {
1628 if (main_list_item && main_list_item->peer_id == buddy_id) {
1629 ad->main_list = eina_list_remove(ad->main_list, main_list_item);
1630 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
1635 ecore_timer_add(1, on_load_main_view_requested, ad);
1637 hide_loading_popup(ad);
1642 static int on_buddy_blocked(appdata_s *app, bundle *const rec_msg)
1644 int result = SVC_RES_FAIL;
1646 char* buddy_id_str = NULL;
1647 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1648 int buddy_id = atoi(buddy_id_str);
1650 Eina_Bool is_success = EINA_FALSE;
1651 char* is_success_val = NULL;
1652 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1653 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1654 is_success = EINA_TRUE;
1656 is_success = EINA_FALSE;
1659 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1660 on_user_block_response_received(app, buddy_id, is_success);
1662 hide_loading_popup(app);
1667 static int on_buddy_unblocked(appdata_s *app, bundle *const rec_msg)
1669 int result = SVC_RES_FAIL;
1671 char* buddy_id_str = NULL;
1672 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1673 int buddy_id = atoi(buddy_id_str);
1675 Eina_Bool is_success = EINA_FALSE;
1676 char* is_success_val = NULL;
1677 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1678 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1679 is_success = EINA_TRUE;
1681 is_success = EINA_FALSE;
1684 if (app->current_app_state == TG_SET_USER_INFO_STATE) {
1685 on_user_unblock_response_received(app, buddy_id, is_success);
1687 hide_loading_popup(app);
1692 static int on_delete_selected_group_chats(appdata_s *ad, bundle *const rec_msg)
1694 int result = SVC_RES_OK;
1696 if (app->current_app_state == TG_USER_MAIN_VIEW_SELECTION_STATE) {
1697 elm_naviframe_item_pop(app->nf);
1698 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1699 show_floating_button(app);
1700 load_buddy_list_data(app);
1701 load_unknown_buddy_list_data(app);
1702 load_peer_data(app);
1703 load_main_list_data(app);
1704 refresh_main_list_view(app, EINA_FALSE);
1706 hide_loading_popup(app);
1708 load_registered_user_data(ad);
1709 load_buddy_list_data(ad);
1710 load_unknown_buddy_list_data(ad);
1712 load_main_list_data(ad);
1713 if (ad->main_item) {
1714 elm_naviframe_item_pop_to(ad->main_item);
1716 ecore_timer_add(1, on_load_main_view_requested, ad);
1721 static int on_group_chat_deleted_response(appdata_s *app, bundle *const rec_msg)
1723 int result = SVC_RES_FAIL;
1725 char* chat_id_str = NULL;
1726 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
1727 int chat_id = atoi(chat_id_str);
1729 Eina_Bool is_success = EINA_FALSE;
1730 char* is_success_val = NULL;
1731 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1732 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1733 is_success = EINA_TRUE;
1735 is_success = EINA_FALSE;
1741 Eina_List *l = NULL;
1742 peer_with_pic_s *pitem = NULL;
1743 EINA_LIST_FOREACH(app->peer_list, l, pitem) {
1747 if (pitem->use_data->peer_id == chat_id) {
1748 app->peer_list = eina_list_remove(app->peer_list, pitem);
1754 tg_main_list_item_s *item = NULL;
1755 EINA_LIST_FOREACH(app->main_list, l, item) {
1756 if (item->peer_id == chat_id) {
1757 app->main_list = eina_list_remove(app->main_list, item);
1762 refresh_main_list_view(app, EINA_FALSE);
1764 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
1765 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1767 app->peer_in_cahtting_data = NULL;
1768 app->main_item_in_cahtting_data = NULL;
1770 elm_naviframe_item_pop(app->nf);
1771 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
1772 show_floating_button(app);
1775 if (app->current_app_state == TG_SET_CHAT_INFO_STATE && app->peer_in_cahtting_data
1776 && app->peer_in_cahtting_data->use_data->peer_id == chat_id) {
1777 on_group_chat_deleted(app, chat_id);
1781 show_toast(app, "Failed to delete chat");
1783 hide_loading_popup(app);
1788 static int on_message_sent_to_buddy(appdata_s *app, bundle *const rec_msg)
1790 int result = SVC_RES_FAIL;
1792 char* buddy_id_str = NULL;
1793 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
1794 int buddy_id = atoi(buddy_id_str);
1796 char* msg_id_str = NULL;
1797 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
1798 int message_id = atoi(msg_id_str);
1800 char* table_name = NULL;
1801 result = bundle_get_str(rec_msg, "table_name", &table_name);
1804 Eina_Bool is_success = EINA_FALSE;
1805 char* is_success_val = NULL;
1806 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1807 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1808 is_success = EINA_TRUE;
1810 is_success = EINA_FALSE;
1811 show_toast(app, "message sent failed");
1814 char* type_of_chat_str = NULL;
1815 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
1816 int type_of_chat = atoi(type_of_chat_str);
1818 tg_message_s* msg = get_message_from_message_table(message_id, table_name);
1821 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
1822 // update message to sent state
1823 on_text_message_state_changed(app, msg, type_of_chat);
1826 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
1827 Eina_List *l = NULL;
1828 tg_main_list_item_s *sel_item = NULL;
1829 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
1830 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
1831 if (sel_item->msg_status_lbl) {
1832 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_SENT_ICON), NULL);
1841 msg->message = NULL;
1844 if (msg->media_id) {
1845 free(msg->media_id);
1846 msg->media_id = NULL;
1853 static int on_self_profile_pic_updated(appdata_s *app, bundle *const rec_msg)
1855 int result = SVC_RES_FAIL;
1856 Eina_Bool is_success = EINA_FALSE;
1857 char *is_success_val = NULL;
1858 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1859 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1860 is_success = EINA_TRUE;
1861 show_toast(app, "profile picture updated successfully.");
1863 is_success = EINA_FALSE;
1864 show_toast(app, "profile picture not updated.");
1868 char *file_path = NULL;
1869 result = bundle_get_str(rec_msg, "file_path", &file_path);
1871 if (app->current_user_data->photo_path) {
1872 free(app->current_user_data->photo_path);
1873 app->current_user_data->photo_path = NULL;
1876 app->current_user_data->photo_path = strdup(file_path);
1877 Evas_Object* profile_pic = evas_object_data_get(app->nf, "main_user_profile_pic");
1879 elm_image_file_set(profile_pic, file_path, NULL);
1882 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE) {
1883 Evas_Object* set_pro_pic = evas_object_data_get(app->nf, "settings_user_profile_pic");
1885 elm_image_file_set(set_pro_pic, file_path, NULL);
1889 hide_loading_popup(app);
1893 static int on_self_profile_name_updated(appdata_s *app, bundle *const rec_msg)
1895 int result = SVC_RES_FAIL;
1896 Eina_Bool is_success = EINA_FALSE;
1897 char *is_success_val = NULL;
1898 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1899 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1900 is_success = EINA_TRUE;
1901 show_toast(app, "Username updated successfully.");
1903 is_success = EINA_FALSE;
1904 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1909 char *first_name = NULL;
1910 result = bundle_get_str(rec_msg, "first_name", &first_name);
1915 char *last_name = NULL;
1916 result = bundle_get_str(rec_msg, "last_name", &last_name);
1921 if (app->current_user_data->first_name) {
1922 free(app->current_user_data->first_name);
1923 app->current_user_data->first_name = NULL;
1926 if (app->current_user_data->last_name) {
1927 free(app->current_user_data->last_name);
1928 app->current_user_data->last_name = NULL;
1932 app->current_user_data->first_name = strdup(first_name);
1936 app->current_user_data->last_name = strdup(last_name);
1939 if (app->current_app_state == TG_SETTINGS_EDIT_NAME_STATE) {
1940 elm_naviframe_item_pop(app->nf);
1941 app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1942 refresh_settings_screen(app);
1948 hide_loading_popup(app);
1952 static int on_self_username_updated(appdata_s *app, bundle *const rec_msg)
1954 int result = SVC_RES_FAIL;
1955 Eina_Bool is_success = EINA_FALSE;
1956 char *is_success_val = NULL;
1957 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
1958 if (strncmp("true", is_success_val, strlen("true")) == 0) {
1959 is_success = EINA_TRUE;
1960 show_toast(app, "Username updated successfully.");
1962 is_success = EINA_FALSE;
1963 show_toast(app, "Username not updated. Username already exist or please check your network connection.");
1967 char *username = NULL;
1968 result = bundle_get_str(rec_msg, "username", &username);
1970 if (app->current_user_data->username) {
1971 free(app->current_user_data->username);
1972 app->current_user_data->username = NULL;
1975 app->current_user_data->username = strdup(username);
1978 if (app->current_app_state == TG_SETTINGS_SCREEN_STATE || app->current_app_state == TG_SET_USERNAME_STATE) {
1979 Evas_Object *name_lbl = evas_object_data_get(app->nf, "settings_user_name_label");
1981 char buf[512] = {'\0'};
1982 snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", app->current_user_data->username);
1983 elm_object_text_set(name_lbl, buf);
1986 if (app->current_app_state == TG_SET_USERNAME_STATE) {
1987 elm_naviframe_item_pop(app->nf);
1988 app->current_app_state = TG_SETTINGS_SCREEN_STATE;
1991 hide_loading_popup(app);
1996 static int on_message_read_by_buddy(appdata_s *app, bundle *const rec_msg)
1998 int result = SVC_RES_FAIL;
1999 char* buddy_id_str = NULL;
2000 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2001 int buddy_id = atoi(buddy_id_str);
2003 char* msg_id_str = NULL;
2004 result = bundle_get_str(rec_msg, "message_id", &msg_id_str);
2005 int message_id = atoi(msg_id_str);
2007 char* table_name = NULL;
2008 result = bundle_get_str(rec_msg, "table_name", &table_name);
2010 char* phone_number = NULL;
2011 result = bundle_get_str(rec_msg, "phone_number", &phone_number);
2013 char* type_of_chat_str = NULL;
2014 result = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
2015 int type_of_chat = atoi(type_of_chat_str);
2017 tg_message_s* msg = get_message_from_message_table(message_id, table_name);
2020 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data && app->peer_in_cahtting_data->use_data->peer_id == buddy_id) {
2021 // update message to sent state
2022 on_text_message_state_changed(app, msg, type_of_chat);
2025 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2027 Eina_List *l = NULL;
2028 tg_main_list_item_s *sel_item = NULL;
2029 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2031 if (sel_item->peer_id == buddy_id && sel_item->last_msg_id == message_id) {
2033 if (sel_item->msg_status_lbl) {
2034 elm_image_file_set(sel_item->msg_status_lbl, ui_utils_get_resource(MESSAGE_READ_ICON), NULL);
2044 msg->message = NULL;
2047 if (msg->media_id) {
2048 free(msg->media_id);
2049 msg->media_id = NULL;
2057 static int on_media_message_download_completed(appdata_s *app, bundle *const rec_msg)
2059 LOGE("Media download14");
2060 int result = SVC_RES_FAIL;
2061 char* buddy_id_str = NULL;
2062 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2063 int buddy_id = atoi(buddy_id_str);
2065 char* to_id_str = NULL;
2066 result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2067 int to_id = atoi(to_id_str);
2069 char* media_id_str = NULL;
2070 result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2071 long long media_id = atoll(media_id_str);
2073 char* file_name = NULL;
2074 result = bundle_get_str(rec_msg, "file_name", &file_name);
2076 //file_name == "failed_to_load"
2078 if (file_name && strstr(file_name, "failed_to_load") != NULL) {
2080 show_toast(app, "media download failed.");
2083 char* caption = NULL;
2084 result = bundle_get_str(rec_msg, "caption", &caption);
2087 if (file_name && app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2088 && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2089 // update media to sent state
2090 LOGE("Media download15");
2091 on_media_download_completed(app, buddy_id, media_id, file_name, caption);
2093 LOGE("Media download16");
2097 static int on_video_message_thumb_download_completed(appdata_s *app, bundle *const rec_msg)
2099 int result = SVC_RES_FAIL;
2100 char* buddy_id_str = NULL;
2101 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2102 int buddy_id = atoi(buddy_id_str);
2104 char* to_id_str = NULL;
2105 result = bundle_get_str(rec_msg, "to_peer_id", &to_id_str);
2106 int to_id = atoi(to_id_str);
2108 char* media_id_str = NULL;
2109 result = bundle_get_str(rec_msg, "media_id", &media_id_str);
2110 long long media_id = atoll(media_id_str);
2112 char* file_name = NULL;
2113 result = bundle_get_str(rec_msg, "file_name", &file_name);
2115 char* caption = NULL;
2116 result = bundle_get_str(rec_msg, "caption", &caption);
2118 if (file_name && app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->peer_in_cahtting_data
2119 && app->peer_in_cahtting_data->use_data->peer_id == to_id) {
2120 // update thumbnail icon
2121 on_video_thumb_download_completed(app, buddy_id, media_id, file_name, caption);
2126 static int on_new_buddy_added_to_contacts(appdata_s *app, bundle *const rec_msg)
2128 int result = SVC_RES_FAIL;
2129 char* buddy_id_str = NULL;
2130 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2132 int buddy_id = atoi(buddy_id_str);
2133 peer_with_pic_s *peer_item = get_peer_info(buddy_id);
2134 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2136 app->main_list = eina_list_prepend(app->main_list, latest_item);
2137 refresh_main_list_view(app, EINA_TRUE);
2139 hide_loading_popup(app);
2144 static int on_new_group_added(appdata_s *app, bundle *const rec_msg)
2146 int result = SVC_RES_FAIL;
2150 char* chat_id_str = NULL;
2151 result = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
2153 int chat_id = atoi(chat_id_str);
2154 peer_with_pic_s *peer_item = get_peer_info(chat_id);
2155 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2156 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2158 app->main_list = eina_list_prepend(app->main_list, latest_item);
2159 hide_loading_popup(app);
2160 refresh_main_list_view(app, EINA_FALSE);
2164 hide_loading_popup(app);
2165 if (app->main_item) {
2166 elm_naviframe_item_pop_to(app->main_item);
2167 app->current_app_state = TG_USER_MAIN_VIEW_STATE;
2170 tg_main_list_item_s* latest_item = get_latest_item(app, peer_item);
2172 app->main_list = eina_list_prepend(app->main_list, latest_item);
2173 refresh_main_list_view(app, EINA_FALSE);
2177 app->peer_list = eina_list_prepend(app->peer_list, peer_item);
2179 load_registered_user_data(app);
2180 load_buddy_list_data(app);
2181 load_unknown_buddy_list_data(app);
2182 load_peer_data(app);
2183 load_main_list_data(app);
2185 if (app->main_item) {
2186 elm_naviframe_item_pop_to(app->main_item);
2189 ecore_timer_add(1, on_load_main_view_requested, app);
2194 static int on_buddy_contact_updated(appdata_s *app, bundle *const rec_msg)
2196 int result = SVC_RES_FAIL;
2197 char* buddy_id_str = NULL;
2198 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2199 //int buddy_id = atoi(buddy_id_str);
2201 char* update_msg = NULL;
2202 result = bundle_get_str(rec_msg, "update_message", &update_msg);
2209 static int on_buddy_status_updated(appdata_s *app, bundle *const rec_msg)
2211 int result = SVC_RES_FAIL;
2212 char* buddy_id_str = NULL;
2213 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2214 int buddy_id = atoi(buddy_id_str);
2215 // update to online or last seen, if current view is conversation.
2218 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2219 on_user_presence_state_changed(app, buddy_id);
2224 static int on_typing_status_updated(appdata_s *app, bundle *const rec_msg)
2226 int result = SVC_RES_FAIL;
2227 char* buddy_id_str = NULL;
2228 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2229 int buddy_id = atoi(buddy_id_str);
2231 char* buddy_name = NULL;
2232 result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2234 const char *type_status_str;
2236 result = bundle_get_str(rec_msg, "type_status", &type_status_str);
2237 enum tgl_typing_status type_status = atoi(type_status_str);
2240 char* type_status_message = NULL;
2242 switch (type_status) {
2243 case tgl_typing_none:
2244 type_status_message = "doing nothing";
2246 case tgl_typing_typing:
2247 type_status_message = "typing...";
2249 case tgl_typing_cancel:
2250 type_status_message = "deleting typed message";
2252 case tgl_typing_record_video:
2253 type_status_message = "recording video";
2255 case tgl_typing_upload_video:
2256 type_status_message = "uploading video";
2258 case tgl_typing_record_audio:
2259 type_status_message = "recording audio";
2261 case tgl_typing_upload_audio:
2262 type_status_message = "uploading audio";
2264 case tgl_typing_upload_photo:
2265 type_status_message = "uploading photo";
2267 case tgl_typing_upload_document:
2268 type_status_message = "uploading document";
2270 case tgl_typing_geo:
2271 type_status_message = "choosing location";
2273 case tgl_typing_choose_contact:
2274 type_status_message = "choosing contact";
2277 type_status_message = "";
2281 // update UI. may be contacts view or conversation view.
2282 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2283 Eina_List *l = NULL;
2284 tg_main_list_item_s *sel_item = NULL;
2285 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2286 if (sel_item->peer_id != buddy_id) {
2289 // update last message
2291 if (sel_item->profile_pic) {
2295 if (sel_item->user_name_lbl) {
2299 if (sel_item->status_lbl) {
2300 char status_buf[512] = {'\0'};
2301 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Bold color=#158CB0 align=left><font_size=30>%.*s</font_size></font>", 25, type_status_message);
2302 elm_object_text_set(sel_item->status_lbl, status_buf);
2305 if (sel_item->date_lbl) {
2308 if (sel_item->msg_status_lbl) {
2316 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2317 on_user_status_changed(app, type_status_message);
2324 static int on_user_status_updated(appdata_s *app, bundle *const rec_msg)
2326 int result = SVC_RES_FAIL;
2327 char* buddy_id_str = NULL;
2328 result = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
2329 int buddy_id = atoi(buddy_id_str);
2331 char* buddy_name = NULL;
2332 result = bundle_get_str(rec_msg, "buddy_name", &buddy_name);
2334 if (app->current_app_state == TG_USER_MAIN_VIEW_STATE) {
2335 Eina_List *l = NULL;
2336 tg_main_list_item_s *sel_item = NULL;
2337 EINA_LIST_FOREACH(app->main_list, l, sel_item) {
2338 if (sel_item->peer_id != buddy_id) {
2341 // update last message
2343 if (sel_item->profile_pic) {
2347 if (sel_item->user_name_lbl) {
2351 if (sel_item->status_lbl) {
2352 char status_buf[126] = {'\0'};
2353 snprintf(status_buf, sizeof(status_buf), "<font=Tizen:style=Normal color=#A4A4A4 align=left><font_size=26>%.*s</font_size></font>", 29, sel_item->last_message ? sel_item->last_message : "");
2354 elm_object_text_set(sel_item->status_lbl, status_buf);
2357 if (sel_item->date_lbl) {
2360 if (sel_item->msg_status_lbl) {
2367 if (app->current_app_state == TG_CHAT_MESSAGING_VIEW_STATE && app->buddy_in_cahtting_data && app->buddy_in_cahtting_data->use_data->user_id.id == buddy_id) {
2368 on_user_presence_state_changed(app, buddy_id);
2374 Eina_Bool on_logout_completed(void *data)
2376 appdata_s *ad = data;
2378 elm_naviframe_item_pop(ad->nf);
2379 ad->current_app_state = TG_REGISTRATION_STATE;
2380 launch_init_screen(ad);
2382 return ECORE_CALLBACK_CANCEL;
2385 Eina_Bool on_start_server_requested(void *data)
2387 appdata_s *ad = data;
2389 launch_tg_server(ad);
2391 return ECORE_CALLBACK_CANCEL;
2395 static int _on_service_client_msg_received_cb(void *data, bundle *const rec_msg)
2397 int result = SVC_RES_FAIL;
2398 RETVM_IF(!data, result, "Data is NULL");
2399 appdata_s *app = data;
2400 char *rec_key_val = NULL;
2403 result = bundle_get_str(rec_msg, "command", &rec_key_val);
2405 LOGI("MSG recv = %s", rec_key_val);
2407 if (strcmp(rec_key_val, "server_not_initialized") == 0) {
2408 hide_loading_popup(app);
2410 } else if (strcmp(rec_key_val, "server_restart_notification") == 0) {
2411 hide_loading_popup(app);
2412 show_toast(app, i18n_get_text("IDS_NFC_POP_INITIALISING_PLEASE_WAIT_ING"));
2413 ecore_timer_add(1, on_start_server_requested, app);
2414 } else if (strcmp(rec_key_val, "registration_done") == 0) {
2416 char* is_success_val = NULL;
2417 result = bundle_get_str(rec_msg, "is_success", &is_success_val);
2419 if (strncmp("true", is_success_val, strlen("true")) == 0) {
2420 //show_toast(app, is_success_val);
2421 // Launch login view
2422 //elm_naviframe_item_pop(app->nf);
2423 launch_login_cb(data);
2426 show_toast(app, "Error: registration");
2428 hide_loading_popup(app);
2430 } else if (strcmp(rec_key_val, "name_registration_request") == 0) {
2432 elm_naviframe_item_pop(app->nf);
2433 elm_naviframe_item_pop(app->nf);
2434 launch_first_registration_cb(app);
2435 hide_loading_popup(app);
2437 } else if (strcmp(rec_key_val, "request_reg_code_again") == 0) {
2438 show_toast(app, "Please enter a valid code.");
2439 hide_loading_popup(app);
2440 } else if (strcmp(rec_key_val, "request_phone_num_again") == 0) {
2441 show_toast(app, "Please enter a valid phone number.");
2442 hide_loading_popup(app);
2443 } else if (strcmp(rec_key_val, "server_connection_failed") == 0) {
2444 show_toast(app, "Server connection failed. please check network connection");
2445 hide_loading_popup(app);
2446 } else if (strcmp(rec_key_val, "logout_completed") == 0) {
2448 free_app_data(app, EINA_TRUE);
2449 hide_loading_popup(app);
2450 app->phone_number = NULL;
2451 app->buddy_list = NULL;
2452 app->unknown_buddy_list = NULL;
2453 app->known_buddy_list = NULL;
2454 app->main_list = NULL;
2455 app->peer_list = NULL;
2456 app->search_peer_list = NULL;
2457 app->is_first_time_registration = EINA_FALSE;
2459 app->loaded_msg_list = NULL;
2460 app->loading_popup = NULL;
2461 app->current_user_data = NULL;
2462 app->is_tg_initilized = EINA_FALSE;
2463 app->chat_background = NULL;
2464 app->msg_popup = NULL;
2465 app->s_notififcation = NULL;
2467 app->is_server_ready = EINA_FALSE;
2468 app->contact_list = NULL;
2470 char *chat_bg = NULL;
2471 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
2473 app->chat_background = strdup(chat_bg);
2476 if (!app->chat_background) {
2477 app->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
2478 preference_set_string(TG_CHAT_BG_PREFERENCE, app->chat_background);
2483 elm_naviframe_item_pop(app->nf);
2484 //ecore_timer_add(1, on_logout_completed, app);
2485 elm_naviframe_item_pop(app->nf);
2486 app->current_app_state = TG_REGISTRATION_STATE;
2487 launch_init_screen(app);
2489 } else if (strcmp(rec_key_val, "server_connection_status") == 0) {
2490 return on_server_connection_status_changed(data, rec_msg);
2493 if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2494 app->is_tg_initilized = EINA_TRUE;
2496 if (!app->is_tg_initilized) {
2500 if (strcmp(rec_key_val, "contacts_load_done") == 0) {
2502 return on_contacts_loaded(data, rec_msg);
2504 } else if (strcmp(rec_key_val, "contacts_and_chats_load_done") == 0) {
2506 return on_contacts_and_chats_loaded(data, rec_msg);
2508 } else if (strcmp(rec_key_val, "response_group_chat_updated") == 0) {
2510 return on_group_chat_updated(data, rec_msg);
2512 } else if (strcmp(rec_key_val, "group_chat_updated") == 0) {
2514 return on_group_chat_info_changed_response(data, rec_msg);
2516 } else if ((strcmp(rec_key_val, "group_chat_new_buddy_added_response") == 0) || (strcmp(rec_key_val, "group_chat_buddy_deleted_response") == 0)) {
2518 return on_new_buddy_added_or_deleted_from_group_chat(data, rec_msg, rec_key_val);
2520 } else if (strcmp(rec_key_val, "group_chat_rename_response") == 0) {
2522 return on_group_chat_name_changed(data, rec_msg);
2524 } else if (strcmp(rec_key_val, "buddy_profile_pic_updated") == 0) {
2526 return on_buddy_profile_pic_updated(data, rec_msg);
2528 } else if (strcmp(rec_key_val, "message_received") == 0) {
2530 return on_message_received_from_buddy(data, rec_msg, EINA_FALSE);
2532 } else if (strcmp(rec_key_val, "message_received_with_date") == 0) {
2534 return on_message_with_date_received_from_buddy(data, rec_msg);
2536 } else if (strcmp(rec_key_val, "buddy_readded") == 0) {
2538 return on_buddy_re_added_to_chat(data, rec_msg);
2540 } else if (strcmp(rec_key_val, "new_contact_added") == 0) {
2542 return on_new_contact_added(data, rec_msg);
2544 } else if (strcmp(rec_key_val, "buddy_deleted") == 0) {
2546 return on_buddy_deleted_from_friends_list(data, rec_msg);
2548 } else if (strcmp(rec_key_val, "buddy_blocked") == 0) {
2550 return on_buddy_blocked(data, rec_msg);
2552 } else if (strcmp(rec_key_val, "buddy_unblocked") == 0) {
2554 return on_buddy_unblocked(data, rec_msg);
2556 } else if (strcmp(rec_key_val, "selected_group_chats_deleted_response") == 0) {
2558 return on_delete_selected_group_chats(data, rec_msg);
2560 } else if (strcmp(rec_key_val, "group_chat_deleted_response") == 0) {
2562 return on_group_chat_deleted_response(data, rec_msg);
2564 } else if (strcmp(rec_key_val, "message_sent_to_buddy") == 0) {
2566 return on_message_sent_to_buddy(data, rec_msg);
2568 } else if (strcmp(rec_key_val, "self_profile_picture_updated") == 0) {
2570 return on_self_profile_pic_updated(data, rec_msg);
2572 } else if (strcmp(rec_key_val, "self_profile_name_updated") == 0) {
2574 return on_self_profile_name_updated(data, rec_msg);
2576 } else if (strcmp(rec_key_val, "self_username_updated") == 0) {
2578 return on_self_username_updated(data, rec_msg);
2580 } else if (strcmp(rec_key_val, "message_read_by_buddy") == 0) {
2582 return on_message_read_by_buddy(data, rec_msg);
2584 } else if (strcmp(rec_key_val, "media_download_completed") == 0) {
2586 return on_media_message_download_completed(data, rec_msg);
2588 } else if (strcmp(rec_key_val, "video_thumb_download_completed") == 0) {
2590 return on_video_message_thumb_download_completed(data, rec_msg);
2592 } else if (strcmp(rec_key_val, "add_contacts_request") == 0) {
2593 // Not to be handled.
2594 } else if (strcmp(rec_key_val, "new_buddy_added") == 0) {
2596 return on_new_buddy_added_to_contacts(data, rec_msg);
2598 } else if (strcmp(rec_key_val, "new_group_added") == 0) {
2600 return on_new_group_added(data, rec_msg);
2602 } else if (strcmp(rec_key_val, "contact_updated") == 0) {
2604 return on_buddy_contact_updated(data, rec_msg);
2606 } else if (strcmp(rec_key_val, "buddy_status_updated") == 0) {
2608 return on_buddy_status_updated(data, rec_msg);
2610 } else if (strcmp(rec_key_val, "type_status_updated") == 0) {
2612 return on_typing_status_updated(data, rec_msg);
2614 } else if (strcmp(rec_key_val, "user_status_updated") == 0) {
2616 return on_user_status_updated(data, rec_msg);
2625 int init_service(appdata_s *app)
2627 int result = SVC_RES_FAIL;
2628 RETVM_IF(!app, result, "Application data is NULL");
2630 app->service_client = service_client_create();
2631 RETVM_IF(!app->service_client, result, "Failed to create service client");
2633 result = service_client_register_port(app->service_client, TELEGRAM_CLIENT_PORT_NAME);
2634 if (result != SVC_RES_OK) {
2635 ERR("Failed to register service client port");
2636 service_client_destroy(app->service_client);
2637 app->service_client = NULL;
2641 result = _service_client_set_remote_data(app->service_client, TELEGRAM_SERVER_APP_NAME, TELEGRAM_SERVER_PORT_NAME);
2643 result = service_client_register_msg_receive_callback(app->service_client, _on_service_client_msg_received_cb, app);
2644 if (result != SVC_RES_OK) {
2645 ERR("Failed to register service client on message receive callback");
2646 service_client_destroy(app->service_client);
2647 app->service_client = NULL;
2653 void win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)
2659 tg_main_list_item_s* get_latest_item(appdata_s *ad, peer_with_pic_s *item)
2661 tg_main_list_item_s* main_list_item = NULL;
2665 tg_peer_info_s* peer_info = item->use_data;
2669 // get message from message table.
2671 char* tablename = get_table_name_from_number(peer_info->peer_id);
2672 tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2677 if (msg->message && strlen(msg->message) > 0) {
2678 item->last_message = strdup(msg->message);
2680 item->last_message = strdup(" ");
2684 int media_type = msg->media_type;
2685 if (media_type == tgl_message_media_none) {
2686 if (msg->message && strlen(msg->message) > 0) {
2687 item->last_message = strdup(msg->message);
2689 item->last_message = strdup(" ");
2691 } else if (media_type == tgl_message_media_photo) {
2692 item->last_message = strdup("Image");
2693 } else if (media_type == tgl_message_media_document) {
2694 tgl_media_s *media_msg = NULL;
2695 media_msg = get_media_details_from_db(atoll(msg->media_id));
2697 if (media_msg && media_msg->doc_type) {
2698 if (strlen(media_msg->doc_type) > 0) {
2699 item->last_message = strdup(media_msg->doc_type);
2701 item->last_message = strdup("Document");
2703 free_media_details(media_msg);
2705 if (msg->message && strlen(msg->message) > 0) {
2706 item->last_message = strdup(msg->message);
2708 item->last_message = strdup("Document");
2712 } else if (media_type == tgl_message_media_geo) {
2713 item->last_message = strdup("Geo location");
2714 } else if (media_type == tgl_message_media_contact) {
2715 item->last_message = strdup("Contact");
2716 } else if (media_type == tgl_message_media_unsupported) {
2717 item->last_message = strdup(" ");
2718 } else if (media_type == tgl_message_media_photo_encr) {
2719 item->last_message = strdup("Image encrypted");
2720 } else if (media_type == tgl_message_media_document_encr) {
2721 item->last_message = strdup("Document encrypted");
2723 item->last_message = strdup(" ");
2727 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2728 main_list_item->peer_id = peer_info->peer_id;
2729 main_list_item->peer_type = peer_info->peer_type;
2731 set_peer_names(peer_info, main_list_item);
2733 main_list_item->last_seen_time = msg->date;
2734 main_list_item->profile_pic = NULL;
2735 main_list_item->last_msg_id = msg->msg_id;
2736 main_list_item->last_message = strdup(item->last_message);
2737 main_list_item->last_msg_type = msg->media_type;
2738 main_list_item->is_out_msg = msg->out;
2739 main_list_item->last_msg_status = msg->msg_state;
2740 main_list_item->last_msg_service = msg->service;
2741 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2743 if (peer_info->photo_path) {
2744 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2746 main_list_item->profile_pic_path = NULL;
2748 main_list_item->user_name_lbl = NULL;
2749 main_list_item->status_lbl = NULL;
2750 main_list_item->date_lbl = NULL;
2751 main_list_item->msg_status_lbl = NULL;
2752 main_list_item->main_item_layout = NULL;
2754 // delete message object
2757 msg->message = NULL;
2760 if (msg->media_id) {
2761 free(msg->media_id);
2762 msg->media_id = NULL;
2769 item->last_message = strdup(" ");
2770 if (peer_info->peer_type == TGL_PEER_CHAT) {
2771 main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2772 main_list_item->peer_id = peer_info->peer_id;
2773 main_list_item->peer_type = peer_info->peer_type;
2775 set_peer_names(peer_info, main_list_item);
2777 main_list_item->last_seen_time = peer_info->last_seen_time;
2778 main_list_item->profile_pic = NULL;
2779 main_list_item->last_message = strdup(item->last_message);
2780 main_list_item->last_msg_type = -1;
2781 main_list_item->last_msg_service = 0;
2782 main_list_item->is_out_msg = -1;
2783 main_list_item->last_msg_id = -1;
2784 main_list_item->last_msg_status = -1;
2785 main_list_item->number_of_unread_msgs = 0;
2787 if (peer_info->photo_path && strlen(peer_info->photo_path) > 0) {
2788 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2790 main_list_item->profile_pic_path = NULL;
2792 main_list_item->user_name_lbl = NULL;
2793 main_list_item->status_lbl = NULL;
2794 main_list_item->date_lbl = NULL;
2795 main_list_item->msg_status_lbl = NULL;
2796 main_list_item->main_item_layout = NULL;
2807 return main_list_item;
2810 void app_nf_back_cb(void *data, Evas_Object *obj, void *event_info)
2812 appdata_s *ad = data;
2813 if (ad->loading_popup) {
2814 show_toast(ad, "Please wait. request in progress");
2818 switch (ad->current_app_state) {
2819 case TG_INIT_SCREEN_STATE:
2820 elm_win_lower(ad->win);
2823 case TG_USER_MAIN_VIEW_STATE:
2824 if (!ad->menu_popup) {
2825 elm_win_lower(ad->win);
2829 case TG_CHAT_MESSAGING_VIEW_STATE:
2831 if (ad->main_item_in_cahtting_data) {
2832 int buddy_id = ad->main_item_in_cahtting_data->peer_id;
2833 char* tablename = get_table_name_from_number(buddy_id);
2835 Eina_Bool res = set_all_rows_read(tablename);
2841 tg_main_list_item_s* old_item = ad->main_item_in_cahtting_data;
2842 ad->main_list = eina_list_remove(ad->main_list, ad->main_item_in_cahtting_data);
2843 if (old_item->peer_print_name) {
2844 free(old_item->peer_print_name);
2845 old_item->peer_print_name = NULL;
2847 if (old_item->last_message) {
2848 free(old_item->last_message);
2849 old_item->last_message = NULL;
2851 if (old_item->profile_pic_path) {
2852 free(old_item->profile_pic_path);
2853 old_item->profile_pic_path = NULL;
2855 if (old_item->buddy_display_name) {
2856 free(old_item->buddy_display_name);
2857 old_item->buddy_display_name = NULL;
2860 old_item->date_lbl = NULL;
2861 old_item->msg_status_lbl = NULL;
2862 old_item->main_item_layout = NULL;
2863 old_item->profile_pic = NULL;
2864 old_item->profile_pic_path = NULL;
2865 old_item->status_lbl = NULL;
2866 old_item->user_name_lbl = NULL;
2867 ad->main_item_in_cahtting_data = NULL;
2868 if (ad->peer_in_cahtting_data) {
2869 peer_with_pic_s *item = ad->peer_in_cahtting_data;
2870 tg_main_list_item_s* latest_item = get_latest_item(ad, item);
2872 ad->main_list = eina_list_prepend(ad->main_list, latest_item);
2875 refresh_main_list_view(ad, EINA_FALSE);
2877 if (ad->peer_in_cahtting_data) {
2878 peer_with_pic_s *item = ad->peer_in_cahtting_data;
2880 int buddy_id = item->use_data->peer_id;
2881 char* tablename = get_table_name_from_number(buddy_id);
2882 Eina_Bool res = set_all_rows_read(tablename);
2888 tg_peer_info_s* peer_info = item->use_data;
2890 //if (peer_info->last_msg_id > 0) {
2892 // get message from message table.
2894 char* tablename = get_table_name_from_number(peer_info->peer_id);
2895 //tg_message_s* msg = get_message_from_message_table(peer_info->last_msg_id, tablename);
2896 tg_message_s* msg = get_latest_message_from_message_table(tablename, EINA_FALSE);
2901 if (msg->message && strlen(msg->message) > 0) {
2902 item->last_message = strdup(msg->message);
2904 item->last_message = strdup(" ");
2908 int media_type = msg->media_type;
2909 if (media_type == tgl_message_media_none) {
2910 if (msg->message && strlen(msg->message) > 0) {
2911 item->last_message = strdup(msg->message);
2913 item->last_message = strdup(" ");
2915 } else if (media_type == tgl_message_media_photo) {
2916 item->last_message = strdup("Image");
2917 } else if (media_type == tgl_message_media_document) {
2918 tgl_media_s *media_msg = NULL;
2919 media_msg = get_media_details_from_db(atoll(msg->media_id));
2921 if (media_msg && media_msg->doc_type) {
2922 if (strlen(media_msg->doc_type) > 0) {
2923 item->last_message = strdup(media_msg->doc_type);
2925 item->last_message = strdup("Document");
2927 free_media_details(media_msg);
2929 if (msg->message && strlen(msg->message) > 0) {
2930 item->last_message = strdup(msg->message);
2932 item->last_message = strdup("Document");
2936 } else if (media_type == tgl_message_media_geo) {
2937 item->last_message = strdup("Geo location");
2938 } else if (media_type == tgl_message_media_contact) {
2939 item->last_message = strdup("Contact");
2940 } else if (media_type == tgl_message_media_unsupported) {
2941 item->last_message = strdup(" ");
2942 } else if (media_type == tgl_message_media_photo_encr) {
2943 item->last_message = strdup("Image encrypted");
2944 } else if (media_type == tgl_message_media_document_encr) {
2945 item->last_message = strdup("Document encrypted");
2947 item->last_message = strdup(" ");
2951 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2952 main_list_item->peer_id = peer_info->peer_id;
2953 main_list_item->peer_type = peer_info->peer_type;
2955 set_peer_names(peer_info, main_list_item);
2957 main_list_item->last_seen_time = msg->date;
2958 main_list_item->profile_pic = NULL;
2959 main_list_item->last_msg_id = msg->msg_id;
2960 main_list_item->last_message = strdup(item->last_message);
2961 main_list_item->last_msg_type = msg->media_type;
2962 main_list_item->is_out_msg = msg->out;
2963 main_list_item->last_msg_status = msg->msg_state;
2964 main_list_item->last_msg_service = msg->service;
2965 main_list_item->number_of_unread_msgs = get_unread_message_count(tablename);
2967 if (peer_info->photo_path) {
2968 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
2970 main_list_item->profile_pic_path = NULL;
2972 main_list_item->user_name_lbl = NULL;
2973 main_list_item->status_lbl = NULL;
2974 main_list_item->date_lbl = NULL;
2975 main_list_item->msg_status_lbl = NULL;
2976 main_list_item->main_item_layout = NULL;
2977 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
2979 // delete message object
2982 msg->message = NULL;
2985 if (msg->media_id) {
2986 free(msg->media_id);
2987 msg->media_id = NULL;
2994 item->last_message = strdup(" ");
2995 if (peer_info->peer_type == TGL_PEER_CHAT) {
2996 tg_main_list_item_s* main_list_item = (tg_main_list_item_s*)malloc(sizeof(tg_main_list_item_s));
2997 main_list_item->peer_id = peer_info->peer_id;
2998 main_list_item->peer_type = peer_info->peer_type;
3000 set_peer_names(peer_info, main_list_item);
3002 main_list_item->last_seen_time = peer_info->last_seen_time;
3003 main_list_item->profile_pic = NULL;
3004 main_list_item->last_message = strdup(item->last_message);
3005 main_list_item->last_msg_type = -1;
3006 main_list_item->last_msg_service = 0;
3007 main_list_item->is_out_msg = -1;
3008 main_list_item->last_msg_id = -1;
3009 main_list_item->last_msg_status = -1;
3010 main_list_item->number_of_unread_msgs = 0;
3012 if (peer_info->photo_path) {
3013 main_list_item->profile_pic_path = strdup(peer_info->photo_path);
3015 main_list_item->profile_pic_path = NULL;
3017 main_list_item->user_name_lbl = NULL;
3018 main_list_item->status_lbl = NULL;
3019 main_list_item->date_lbl = NULL;
3020 main_list_item->msg_status_lbl = NULL;
3021 main_list_item->main_item_layout = NULL;
3022 ad->main_list = eina_list_prepend(ad->main_list, main_list_item);
3033 refresh_main_list_view(ad, EINA_TRUE);
3037 elm_naviframe_item_pop(ad->nf);
3038 ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3039 show_floating_button(ad);
3041 case TG_SELECT_BUDDY_VIEW:
3042 elm_naviframe_item_pop(ad->nf);
3043 ad->current_app_state = TG_SET_CHAT_INFO_STATE;
3045 case TG_SET_USERNAME_STATE:
3046 elm_naviframe_item_pop(ad->nf);
3047 ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3050 case TG_ADD_CONTACT_STATE:
3051 elm_naviframe_item_pop(ad->nf);
3052 ad->current_app_state = TG_SET_USER_INFO_STATE;
3055 case TG_SET_CHAT_INFO_STATE:
3056 case TG_SET_USER_INFO_STATE:
3057 elm_naviframe_item_pop(ad->nf);
3058 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3060 case TG_USER_MAIN_VIEW_SELECTION_STATE:
3061 case TG_PEER_SEARCH_VIEW_STATE:
3062 case TG_START_MESSAGING_VIEW_STATE:
3063 case TG_SETTINGS_SCREEN_STATE:
3064 elm_naviframe_item_pop(ad->nf);
3065 ad->current_app_state = TG_USER_MAIN_VIEW_STATE;
3066 show_floating_button(ad);
3067 //evas_object_show(ad->panel);
3068 //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3070 case TG_SETTINGS_EDIT_NAME_STATE:
3071 elm_naviframe_item_pop(ad->nf);
3072 ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
3073 delete_floating_button(ad);
3075 case TG_COUNTRY_SELECTION_VIEW:
3076 elm_naviframe_item_pop(ad->nf);
3077 ad->current_app_state = TG_REGISTRATION_STATE;
3079 case TG_REGISTRATION_STATE:
3080 elm_win_lower(ad->win);
3083 case TG_LOGIN_STATE:
3084 send_request_for_restart_server(ad, ad->service_client);
3085 if (ad->timer_value > 0) {
3086 Ecore_Timer* timer = evas_object_data_get(ad->nf, "code_timer");
3088 ecore_timer_del(timer);
3091 elm_naviframe_item_pop(ad->nf);
3092 ad->current_app_state = TG_REGISTRATION_STATE;
3094 case TG_PROFILE_REGISTRATION_STATE:
3095 /* ad->current_app_state = TG_REGISTRATION_STATE;
3096 elm_naviframe_item_pop(ad->nf);*/
3097 elm_win_lower(ad->win);
3100 case TG_BUDDY_LIST_STATE:
3101 evas_object_data_set(ad->nf, "buddy_list", NULL);
3102 elm_win_lower(ad->win);
3105 case TG_BUDDY_CHAT_CONV_STATE:
3106 ad->buddy_in_cahtting_data = NULL;
3107 if (ad->loaded_msg_list) {
3108 eina_list_free(ad->loaded_msg_list);
3109 ad->loaded_msg_list = NULL;
3111 elm_naviframe_item_pop(ad->nf);
3112 ad->current_app_state = TG_BUDDY_LIST_STATE;
3113 //evas_object_show(ad->panel);
3114 //refresh_buddy_list(ad);
3116 case TG_ADD_CONTACT_STATE:
3117 elm_naviframe_item_pop(ad->nf);
3118 if (ad->is_loading_from_profile_view) {
3119 ad->is_loading_from_profile_view = EINA_FALSE;
3120 ad->current_app_state = TG_SET_USER_INFO_STATE;
3121 } else if (ad->is_loading_from_msg_view) {
3122 ad->is_loading_from_msg_view = EINA_FALSE;
3123 ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
3125 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3126 show_floating_button(ad);
3129 case TG_BUDDY_LIST_SELECTION_STATE:
3131 Eina_List *l = NULL;
3132 user_data_with_pic_s *item = NULL;
3133 EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3136 user_data_s* user = item->use_data;
3137 user->is_selected = EINA_FALSE;
3139 elm_naviframe_item_pop(ad->nf);
3140 ad->current_app_state = TG_PEER_SEARCH_VIEW_STATE;
3143 case TG_GROUP_CHAT_NAME_ENTRY_STATE:
3145 Eina_List *l = NULL;
3146 user_data_with_pic_s *item = NULL;
3147 EINA_LIST_FOREACH(ad->buddy_list, l, item) {
3151 user_data_s* user = item->use_data;
3152 user->is_selected = EINA_FALSE;
3154 elm_naviframe_item_pop(ad->nf);
3155 ad->current_app_state = TG_BUDDY_LIST_SELECTION_STATE;
3156 //evas_object_show(ad->panel);
3157 //elm_panel_hidden_set(ad->panel, EINA_FALSE);
3158 //refresh_buddy_list(ad);
3167 layout_back_cb(void *data, Evas_Object *obj, void *event_info)
3169 appdata_s *ad = data;
3170 /* Let window go to hide state. */
3171 elm_win_lower(ad->win);
3174 void _btn_clicked(void* data, Evas_Object* btn, void* ev)
3176 appdata_s *ad = data;
3177 bundle *msg = bundle_create();
3178 if (bundle_add_str(msg, "tizen_app", "Hello service") != 0) {
3179 ERR("Failed to add data by key to bundle");
3183 int result = _app_send_response(ad, msg);
3184 if (result != SVC_RES_OK) {
3190 Eina_Bool on_load_main_view_requested(void *data)
3192 appdata_s *ad = data;
3194 elm_naviframe_item_pop(ad->nf);
3195 hide_loading_popup(ad);
3196 ad->is_loading_from_msg_view = EINA_FALSE;
3197 ad->is_loading_from_profile_view = EINA_FALSE;
3198 launch_user_main_view_cb(ad);
3199 int unread_msg_cnt = get_number_of_unread_messages();
3200 if (unread_msg_cnt <= 0) {
3201 badge_set_count(TELEGRAM_APP_ID, 0);
3202 return ECORE_CALLBACK_CANCEL;
3205 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3206 if (BADGE_ERROR_NONE != err) {
3210 return ECORE_CALLBACK_CANCEL;
3213 Eina_Bool on_init_view_requested(void *data)
3215 appdata_s *ad = data;
3217 Eina_List *user_info = get_registered_user_info();
3219 elm_naviframe_item_pop(ad->nf);
3220 ad->current_app_state = TG_REGISTRATION_STATE;
3221 launch_init_screen(ad);
3223 //show_toast(ad, "user already registered");
3224 load_registered_user_data(ad);
3225 load_buddy_list_data(ad);
3226 load_unknown_buddy_list_data(ad);
3228 load_main_list_data(ad);
3229 eina_list_free(user_info);
3230 ecore_timer_add(2, on_load_main_view_requested, ad);
3233 return ECORE_CALLBACK_CANCEL;
3236 void on_tg_service_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
3238 appdata_s *ad = user_data;
3239 if (result == APP_CONTROL_RESULT_SUCCEEDED) {
3241 show_toast(ad, "Server launched successfully.");
3243 show_toast(ad, "Server not launched.");
3248 void launch_tg_server(void *data)
3250 appdata_s *ad = data;
3251 app_control_h app_control = NULL;
3252 int ret = APP_CONTROL_ERROR_NONE;
3254 ret = app_control_create(&app_control);
3255 ret_if (APP_CONTROL_ERROR_NONE != ret);
3257 ret = app_control_set_app_id(app_control, TELEGRAM_SERVER_APP_NAME);
3258 goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3260 ret = app_control_send_launch_request(app_control, &on_tg_service_result_cb, ad);
3261 goto_if (APP_CONTROL_ERROR_NONE != ret, out);
3264 app_control_destroy(app_control);
3267 static void create_base_gui(appdata_s *ad)
3269 ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
3270 elm_win_conformant_set(ad->win, EINA_TRUE);
3271 elm_win_autodel_set(ad->win, EINA_TRUE);
3273 elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
3274 /* Telegram guideline 720 x 1280 */
3275 elm_app_base_scale_set(2.6);
3277 if (elm_win_wm_rotation_supported_get(ad->win)) {
3278 int rots[2] = {0, 180};
3279 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), sizeof(rots) / sizeof(int));
3281 ad->target_direction = tg_get_device_orientation();
3282 char edj_path[PATH_MAX] = {0, };
3283 app_get_resource(TELEGRAM_CUSTOM_WINSET_EDJ, edj_path, (int)PATH_MAX);
3285 Elm_Theme *theme = elm_theme_new();
3286 elm_theme_extension_add(NULL, edj_path);
3288 evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
3290 ad->conform = elm_conformant_add(ad->win);
3291 evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3292 elm_win_resize_object_add(ad->win, ad->conform);
3293 evas_object_show(ad->conform);
3295 ad->layout = elm_layout_add(ad->conform);
3296 evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3298 //elm_layout_theme_set(ad->layout, "layout", "drawer", "panel");
3299 elm_layout_theme_set(ad->layout, "layout", "application", "default");
3300 evas_object_show(ad->layout);
3302 elm_object_content_set(ad->conform, ad->layout);
3303 ad->nf = elm_naviframe_add(ad->layout);
3305 ad->current_app_state = TG_SPLASH_SCREEN_STATE;
3306 launch_splash_screen_cb(ad);
3307 elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
3308 eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, app_nf_back_cb, ad);
3310 evas_object_show(ad->win);
3312 // check for user info
3315 char *chat_bg = NULL;
3316 preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
3318 ad->chat_background = strdup(chat_bg);
3321 if (!ad->chat_background) {
3322 ad->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
3323 preference_set_string(TG_CHAT_BG_PREFERENCE, ad->chat_background);
3325 launch_tg_server(ad);
3326 ecore_timer_add(5, on_init_view_requested, ad);
3331 char *build_a_path(const char *path, const char *filename)
3336 len = strlen(path) + strlen(filename) + 2;
3342 snprintf(ret, len, "%s/%s", path, filename);
3346 int remove_directory(const char *path)
3348 DIR *d = opendir(path);
3349 size_t path_len = strlen(path);
3358 while (!r && (p=readdir(d)))
3364 /* Skip the names "." and ".." as we don't want to recurse on them. */
3365 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
3370 len = path_len + strlen(p->d_name) + 2;
3375 struct stat statbuf;
3377 snprintf(buf, len, "%s/%s", path, p->d_name);
3379 if (!stat(buf, &statbuf))
3381 if (S_ISDIR(statbuf.st_mode))
3383 r2 = remove_directory(buf);
3408 Eina_Bool copy_file_another_dir(const char *source_file, const char *target_file)
3410 FILE *source, *target;
3412 source = fopen(source_file, "rb+");
3419 target = fopen(target_file, "wb+");
3427 char buffer[1024] = {0}; /*Buffer to store files content*/
3430 while((size = fread(buffer, 1, 1024, source)) != 0)
3432 fwrite(buffer, 1, size, target);
3440 void recursive_dir_copy(const char *source_dir, const char *target_dir)
3443 struct dirent *src_files;
3444 Eina_List *src_dir_stack;
3445 Eina_List *tar_dir_stack;
3451 src_dir_stack = NULL;
3452 tar_dir_stack = NULL;
3454 src_dir = strdup(source_dir);
3455 tar_dir = strdup(target_dir);
3458 open_src = opendir(src_dir);
3459 while((src_files = readdir(open_src))) {
3460 if (src_files->d_type == DT_DIR) {
3463 if (src_files->d_name[0] == '.' && (src_files->d_name[1] == '\0' || (src_files->d_name[1] == '.' && src_files->d_name[2] == '\0'))) {
3467 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3468 mkdir(temp_dest, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3470 char *temp_src = build_a_path(src_dir, src_files->d_name);
3472 src_dir_stack = eina_list_prepend(src_dir_stack, temp_src);
3473 tar_dir_stack = eina_list_prepend(tar_dir_stack, temp_dest);
3476 // copy file to target src_dir.
3477 char *temp_src = build_a_path(src_dir, src_files->d_name);
3478 char *temp_dest = build_a_path(tar_dir, src_files->d_name);
3479 //int ret = rename(temp_src, temp_dest);
3480 Eina_Bool ret = copy_file_another_dir(temp_src, temp_dest);
3482 // file copy success
3484 // file copy failed.
3497 src_dir = eina_list_nth(src_dir_stack, 0);
3499 src_dir_stack = eina_list_remove(src_dir_stack, src_dir);
3501 tar_dir = eina_list_nth(tar_dir_stack, 0);
3503 tar_dir_stack = eina_list_remove(tar_dir_stack, tar_dir);
3505 } while (src_dir != NULL);
3508 void move_downloaded_files()
3510 // create download directory.
3511 struct stat st = {0};
3512 char dest_download_dir[512] = {0,};
3513 sprintf(dest_download_dir, "%s/%s", app_get_shared_data_path(), "downloads");
3514 if (stat(dest_download_dir, &st) == -1) {
3515 mkdir(dest_download_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
3518 LOGE("dest Download directory: %s", dest_download_dir);
3519 // create download directory.
3520 char target_download_dir[512] = {0,};
3521 sprintf(target_download_dir, "%s/%s/%s", app_get_data_path(), "telegram_tizen", "downloads");
3523 LOGE("target Download directory: %s", target_download_dir);
3524 recursive_dir_copy(target_download_dir, dest_download_dir);
3525 remove_directory(target_download_dir);
3529 Eina_Bool dirExists(const char *path)
3533 if (stat(path, &info) != 0)
3535 else if (info.st_mode & S_IFDIR)
3541 static void update_downloaded_video_thumb_paths(const char *table_name, const char *col_one, const char *column_two)
3543 LOGE("Update media info table thumb files");
3544 if (!table_name || !col_one || !column_two)
3547 Eina_List *col_types = NULL;
3548 col_types = eina_list_append(col_types, TG_DB_COLUMN_INTEGER);
3549 col_types = eina_list_append(col_types, TG_DB_COLUMN_TEXT);
3551 Eina_List *col_names = NULL;
3552 col_names = eina_list_append(col_names, col_one);
3553 col_names = eina_list_append(col_names, column_two);
3555 Eina_List *vals = get_values_from_table_sync(table_name, col_names, col_types, NULL, TG_DBMGR_NOLIMITED, TG_DBMGR_NOLIMITED);
3556 int val_size = eina_list_count(vals);
3557 if (vals && val_size > 0) {
3558 Eina_List *row_vals = NULL;
3559 EINA_LIST_FREE(vals, row_vals) {
3561 char *file_path = NULL;
3563 int *temp_user_id = (int *)eina_list_nth(row_vals, 0);
3565 user_id = *temp_user_id;
3569 file_path = (char *)eina_list_nth(row_vals, 1);
3572 eina_list_free(row_vals);
3576 if (strlen(file_path) > 0 && strncmp(file_path, "/opt/usr/media/telegram/telegram_tizen", strlen("/opt/usr/media/telegram/telegram_tizen")) == 0) {
3577 // update file name in db
3578 char *new_path = str_replace(new_path, "/opt/usr/media/telegram/telegram_tizen", app_get_shared_data_path());
3581 eina_list_free(row_vals);
3584 LOGE("Old file path: %s", file_path);
3585 LOGE("New file path: %s", new_path);
3588 Eina_List *new_col_types = NULL;
3589 new_col_types = eina_list_append(new_col_types, TG_DB_COLUMN_TEXT);
3591 Eina_List *new_col_names = NULL;
3592 new_col_names = eina_list_append(new_col_names, column_two);
3594 Eina_List *new_col_values = NULL;
3595 new_col_values = eina_list_append(new_col_values, new_path);
3597 char* where_clause = NULL;
3599 int str_size = strlen(col_one) + strlen(" = ") + 10 + 1;
3600 where_clause = (char *)malloc(str_size);
3601 snprintf(where_clause, str_size, "%s = %d", col_one, user_id);
3602 Eina_Bool ret = update_table(table_name, new_col_names, new_col_types, new_col_values, where_clause);
3604 LOGE("Media info table update failed.");
3606 LOGE("Media info table update success.");
3609 eina_list_free(new_col_types);
3610 eina_list_free(new_col_names);
3611 eina_list_free(new_col_values);
3616 eina_list_free(row_vals);
3619 eina_list_free(col_types);
3620 eina_list_free(col_names);
3623 void update_downloaded_video_thumb_paths_in_media_info_table()
3625 update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_DOCUMENT_THUMB_FILE);
3629 void update_downloaded_file_paths_in_media_info_table()
3631 update_downloaded_video_thumb_paths(MEDIA_INFO_TABLE_NAME, MEDIA_INFO_TABLE_MEDIA_ID, MEDIA_INFO_TABLE_FILE_PATH);
3635 void update_downloaded_file_paths_in_chat_info_table()
3637 update_downloaded_video_thumb_paths(CHAT_INFO_TABLE_NAME, CHAT_INFO_TABLE_CHAT_ID, CHAT_INFO_TABLE_PHOTO_PATH);
3641 void update_downloaded_file_paths_in_buddy_info_table(const char *table_name)
3643 LOGE("Update %s", table_name);
3644 update_downloaded_video_thumb_paths(table_name, USER_INFO_TABLE_USER_ID, USER_INFO_TABLE_PHOTO_PATH);
3647 static bool app_create(void *data)
3650 Hook to take necessary actions before main event loop starts
3651 Initialize UI resources and application's data
3652 If this function returns true, the main loop of application starts
3653 If this function returns false, the application is terminated
3656 appdata_s *ad = data;
3657 if (dirExists(OLD_DIR)) {
3658 char *new_dir = app_get_data_path();
3659 recursive_dir_copy(OLD_DIR, new_dir);
3660 remove_directory(OLD_DIR);
3661 move_downloaded_files();
3662 // update downloaded image paths in DB
3663 update_downloaded_file_paths_in_buddy_info_table(BUDDY_INFO_TABLE_NAME);
3664 update_downloaded_file_paths_in_buddy_info_table(USER_INFO_TABLE_NAME);
3665 update_downloaded_file_paths_in_buddy_info_table(PEER_INFO_TABLE_NAME);
3666 update_downloaded_file_paths_in_chat_info_table();
3667 update_downloaded_file_paths_in_media_info_table();
3668 update_downloaded_video_thumb_paths_in_media_info_table();
3672 bindtextdomain(PACKAGE, "/opt/usr/apps/org.tizen.telegram_tizen/res/locale");
3674 ad->phone_number = NULL;
3675 ad->buddy_list = NULL;
3676 ad->unknown_buddy_list = NULL;
3677 ad->known_buddy_list = NULL;
3678 ad->main_list = NULL;
3679 ad->peer_list = NULL;
3680 ad->search_peer_list = NULL;
3681 ad->is_first_time_registration = EINA_FALSE;
3683 ad->loaded_msg_list = NULL;
3684 ad->loading_popup = NULL;
3685 ad->current_user_data = NULL;
3686 ad->is_tg_initilized = EINA_FALSE;
3687 ad->chat_background = NULL;
3688 ad->msg_popup = NULL;
3689 ad->s_notififcation = NULL;
3691 ad->is_server_ready = EINA_FALSE;
3692 ad->contact_list = NULL;
3693 ad->main_item = NULL;
3694 ad->country_codes_list = NULL;
3695 ad->country_names_list = NULL;
3696 //ad->msg_count = 0;
3697 create_base_gui(ad);
3698 int err = badge_new(TELEGRAM_APP_ID);
3699 if (BADGE_ERROR_NONE != err) {
3707 app_control(app_control_h app_control, void *data)
3709 /* Handle the launch request. */
3713 app_pause(void *data)
3715 appdata_s *app_data = data;
3717 app_data->s_app_visible_state = APP_STATE_IN_BACKGROUND;
3718 int unread_msg_cnt = get_number_of_unread_messages();
3719 if (unread_msg_cnt <= 0) {
3720 badge_set_count(TELEGRAM_APP_ID, 0);
3723 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3724 if (BADGE_ERROR_NONE != err) {
3731 app_resume(void *data)
3733 appdata_s *app_data = data;
3735 app_data->s_app_visible_state = APP_STATE_IN_FOREGROUND;
3740 void free_app_data(appdata_s *app_data, Eina_Bool destroy_server)
3746 int unread_msg_cnt = get_number_of_unread_messages();
3747 if (unread_msg_cnt > 0) {
3748 int err = badge_set_count(TELEGRAM_APP_ID, unread_msg_cnt);
3749 if (BADGE_ERROR_NONE != err) {
3753 badge_set_count(TELEGRAM_APP_ID, 0);
3755 eina_list_free(app_data->country_codes_list);
3756 eina_list_free(app_data->country_names_list);
3757 free(app_data->country_code_buffer);
3759 if (app_data->panel) {
3760 Evas_Object *panel_list = evas_object_data_get(app_data->panel, "panel_list");
3762 evas_object_del(panel_list);
3764 evas_object_del(app_data->panel);
3765 app_data->panel = NULL;
3768 if (app_data->phone_number) {
3769 free(app_data->phone_number);
3770 app_data->phone_number = NULL;
3773 if (app_data->sms_code) {
3774 free(app_data->sms_code);
3775 app_data->sms_code = NULL;
3778 if (app_data->current_user_data) {
3779 free_user_data(app_data->current_user_data);
3781 if (app_data->chat_background) {
3782 app_data->chat_background = NULL;
3784 if (app_data && app_data->s_notififcation) {
3785 notification_delete_all(NOTIFICATION_TYPE_NOTI);
3786 app_data->s_notififcation = NULL;
3789 peer_with_pic_s* pic_item = NULL;
3790 EINA_LIST_FREE(app_data->peer_list, pic_item) {
3793 tg_peer_info_s* item = pic_item->use_data;
3795 if (item->print_name) {
3796 free(item->print_name);
3797 item->print_name = NULL;
3799 if (item->photo_path) {
3800 free(item->photo_path);
3801 item->photo_path = NULL;
3803 pic_item->contact_icon = NULL;
3804 pic_item->msg_object = NULL;
3805 pic_item->name_object = NULL;
3810 app_data->peer_list = NULL;
3812 tg_main_list_item_s* main_item = NULL;
3813 EINA_LIST_FREE(app_data->main_list, main_item) {
3815 if (main_item->peer_print_name) {
3816 free(main_item->peer_print_name);
3817 main_item->peer_print_name = NULL;
3819 if (main_item->last_message) {
3820 free(main_item->last_message);
3821 main_item->last_message = NULL;
3823 if (main_item->profile_pic_path) {
3824 free(main_item->profile_pic_path);
3825 main_item->profile_pic_path = NULL;
3827 if (main_item->buddy_display_name) {
3828 free(main_item->buddy_display_name);
3829 main_item->buddy_display_name = NULL;
3831 main_item->date_lbl = NULL;
3832 main_item->msg_status_lbl = NULL;
3833 main_item->main_item_layout = NULL;
3834 main_item->profile_pic = NULL;
3835 main_item->profile_pic_path = NULL;
3836 main_item->status_lbl = NULL;
3837 main_item->user_name_lbl = NULL;
3839 app_data->main_list = NULL;
3841 user_data_with_pic_s* item = NULL;
3842 EINA_LIST_FREE(app_data->buddy_list, item) {
3845 user_data_s* user_data = item->use_data;
3847 if (user_data->print_name) {
3848 free(user_data->print_name);
3849 user_data->print_name = NULL;
3852 if (user_data->photo_path) {
3853 free(user_data->photo_path);
3854 user_data->photo_path = NULL;
3857 if (user_data->first_name) {
3858 free(user_data->first_name);
3859 user_data->first_name = NULL;
3862 if (user_data->last_name) {
3863 free(user_data->last_name);
3864 user_data->last_name = NULL;
3867 if (user_data->phone) {
3868 free(user_data->phone);
3869 user_data->phone = NULL;
3871 if (user_data->real_first_name) {
3872 free(user_data->real_first_name);
3873 user_data->real_first_name = NULL;
3876 if (user_data->real_last_name) {
3877 free(user_data->real_last_name);
3878 user_data->real_last_name = NULL;
3881 if (user_data->username) {
3882 free(user_data->username);
3883 user_data->username = NULL;
3888 app_data->buddy_list = NULL;
3891 EINA_LIST_FREE(app_data->unknown_buddy_list, item) {
3896 user_data_s* user_data = item->use_data;
3898 if (user_data->print_name) {
3899 free(user_data->print_name);
3900 user_data->print_name = NULL;
3903 if (user_data->photo_path) {
3904 free(user_data->photo_path);
3905 user_data->photo_path = NULL;
3908 if (user_data->first_name) {
3909 free(user_data->first_name);
3910 user_data->first_name = NULL;
3913 if (user_data->last_name) {
3914 free(user_data->last_name);
3915 user_data->last_name = NULL;
3918 if (user_data->phone) {
3919 free(user_data->phone);
3920 user_data->phone = NULL;
3922 if (user_data->real_first_name) {
3923 free(user_data->real_first_name);
3924 user_data->real_first_name = NULL;
3927 if (user_data->real_last_name) {
3928 free(user_data->real_last_name);
3929 user_data->real_last_name = NULL;
3932 if (user_data->username) {
3933 free(user_data->username);
3934 user_data->username = NULL;
3939 app_data->unknown_buddy_list = NULL;
3941 EINA_LIST_FREE(app_data->known_buddy_list, item) {
3942 user_data_s* user_data = item->use_data;
3944 if (user_data->print_name)
3945 free(user_data->print_name);
3946 user_data->print_name = NULL;
3948 if (user_data->photo_path)
3949 free(user_data->photo_path);
3950 user_data->photo_path = NULL;
3952 if (user_data->first_name)
3953 free(user_data->first_name);
3954 user_data->first_name = NULL;
3956 if (user_data->last_name)
3957 free(user_data->last_name);
3958 user_data->last_name = NULL;
3960 if (user_data->phone)
3961 free(user_data->phone);
3962 user_data->phone = NULL;
3964 if (user_data->real_first_name)
3965 free(user_data->real_first_name);
3966 user_data->real_first_name = NULL;
3968 if (user_data->real_last_name)
3969 free(user_data->real_last_name);
3970 user_data->real_last_name = NULL;
3972 if (user_data->username)
3973 free(user_data->username);
3974 user_data->username = NULL;
3979 app_data->known_buddy_list = NULL;
3981 eina_list_free(app_data->loaded_msg_list);
3982 app_data->loaded_msg_list = NULL;
3984 if (app_data->service_client && destroy_server) {
3985 service_client_destroy(app_data->service_client);
3986 app_data->service_client = NULL;
3988 free_contact_list(app_data->contact_list);
3993 app_terminate(void *data)
3995 free_app_data(data, EINA_TRUE);
3999 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
4001 /*APP_EVENT_LANGUAGE_CHANGED*/
4002 char *locale = NULL;
4003 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
4005 setlocale(LC_ALL, locale);
4006 elm_language_set(locale);
4013 TelegramAppDirection tg_get_device_orientation()
4015 TelegramAppDirection target_direction;
4017 switch (app_get_device_orientation()) {
4018 case APP_DEVICE_ORIENTATION_90:
4019 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4021 case APP_DEVICE_ORIENTATION_180:
4022 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4024 case APP_DEVICE_ORIENTATION_270:
4025 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4027 case APP_DEVICE_ORIENTATION_0:
4029 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4033 return target_direction;
4036 void update_current_view(appdata_s *ad)
4041 switch (ad->current_app_state) {
4042 case TG_USER_MAIN_VIEW_STATE:
4043 refresh_main_list_view(ad, EINA_FALSE);
4045 case TG_CHAT_MESSAGING_VIEW_STATE:
4046 refresh_messaging_view(ad);
4055 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
4057 appdata_s *ad = user_data;
4061 TelegramAppDirection target_direction;
4062 app_device_orientation_e orientation;
4064 if (app_event_get_device_orientation(event_info, &orientation) != 0) {
4068 switch (orientation) {
4069 case APP_DEVICE_ORIENTATION_90:
4070 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
4072 case APP_DEVICE_ORIENTATION_180:
4073 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
4075 case APP_DEVICE_ORIENTATION_270:
4076 target_direction = TELEGRAM_TARGET_DIRECTION_LANDSCAPE;
4078 case APP_DEVICE_ORIENTATION_0:
4080 target_direction = TELEGRAM_TARGET_DIRECTION_PORTRAIT;
4084 if (target_direction != ad->target_direction) {
4085 ad->target_direction = target_direction;
4086 update_current_view(ad);
4092 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
4096 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
4100 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
4104 int main(int argc, char *argv[])
4106 appdata_s ad = {0,};
4109 ui_app_lifecycle_callback_s event_callback = {0,};
4110 app_event_handler_h handlers[5] = {NULL, };
4112 event_callback.create = app_create;
4113 event_callback.terminate = app_terminate;
4114 event_callback.pause = app_pause;
4115 event_callback.resume = app_resume;
4116 event_callback.app_control = app_control;
4118 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
4119 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
4120 ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
4121 ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
4122 ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
4123 ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
4125 ret = ui_app_main(argc, argv, &event_callback, &ad);
4126 if (ret != APP_ERROR_NONE) {
4127 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main() is failed. err = %d", ret);