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
20 #include <service_app.h>
21 #include "tg-engine-service.h"
22 #include "tg_engine.h"
23 #include "tg_server.h"
25 #include "server_response.h"
26 #include <net_connection.h>
27 #include "tg_db_wrapper.h"
29 #include "tg_common.h"
31 static void free_connection(tg_engine_data_s* tg_data);
33 static int _on_tg_server_msg_received_cb(void *data, bundle *const rec_msg)
35 tg_engine_data_s *tg_data = data;
36 int result = SVC_RES_OK;
37 RETVM_IF(!tg_data, result, "Data is NULL");
38 char *cmd_key_val = NULL;
40 int res = bundle_get_str(rec_msg, "command", &cmd_key_val);
42 if (strcmp(cmd_key_val, "registration") == 0) {
43 char *ph_no_key_val = NULL;
44 char *sms_key_val = NULL;
45 res = bundle_get_str(rec_msg, "phone_number", &ph_no_key_val);
46 res = bundle_get_str(rec_msg, "through_sms", &sms_key_val);
49 if (tg_data->tg_state != TG_ENGINE_STATE_NONE && tg_data->tg_state != TG_ENGINE_STATE_REGISTRATION)
50 on_restart_service_requested(tg_data);
52 Eina_Bool th_sms = EINA_TRUE;
54 if (strcmp(sms_key_val, "true") == 0)
59 process_registration_command(tg_data, ph_no_key_val, th_sms);
64 if (strcmp(cmd_key_val, "code_validation") == 0) {
66 char* sms_code_val = NULL;
67 res = bundle_get_str(rec_msg, "sms_code", &sms_code_val);
68 process_validation_command(tg_data, sms_code_val);
72 if (strcmp(cmd_key_val, "profile_registration") == 0) {
74 char* first_name = NULL;
75 char* last_name = NULL;
77 res = bundle_get_str(rec_msg, "first_name", &first_name);
78 if (res == BUNDLE_ERROR_NONE && first_name)
79 tg_data->first_name = strdup(first_name);
81 res = bundle_get_str(rec_msg, "last_name", &last_name);
82 if (res == BUNDLE_ERROR_NONE && last_name)
83 tg_data->last_name = strdup(last_name);
85 if (tg_data->tg_state == TG_ENGINE_STATE_PROFILE_FIRST_NAME_REGISTRATION && tg_data->get_string)
86 tg_data->get_string(tgl_engine_get_TLS(), tg_data->first_name, tg_data->callback_arg);
91 if (!tg_data->is_login_activated) {
92 send_server_not_initialized_response(tg_data);
96 if (strcmp(cmd_key_val, "logout_telegram") == 0) {
98 /*process_logout_command(tg_data);*/
99 tg_data->is_login_activated = EINA_FALSE;
100 tg_data->is_network_connected = EINA_FALSE;
101 if (tg_data->phone_number) {
102 free(tg_data->phone_number);
103 tg_data->phone_number = NULL;
106 if (tg_data->sms_code) {
107 free(tg_data->sms_code);
108 tg_data->sms_code = NULL;
111 if (tg_data->first_name) {
112 free(tg_data->first_name);
113 tg_data->first_name = NULL;
116 if (tg_data->last_name) {
117 free(tg_data->last_name);
118 tg_data->last_name = NULL;
121 if (tg_data->contact_list_to_add) {
122 free_contact_data(tg_data->contact_list_to_add);
123 tg_data->contact_list_to_add = NULL;
126 if (tg_data->chat_list) {
127 eina_list_free(tg_data->chat_list);
128 tg_data->chat_list = NULL;
131 if (tg_data->buddy_list) {
132 eina_list_free(tg_data->buddy_list);
133 tg_data->buddy_list = NULL;
136 if (tg_data->peer_list) {
137 eina_list_free(tg_data->peer_list);
138 tg_data->peer_list = NULL;
141 tgl_engine_var_free();
145 recursive_dir_delete(app_get_data_path());
147 tgl_engine_var_init();
149 tg_data->tg_state = TG_ENGINE_STATE_NONE;
150 tg_data->first_name = NULL;
151 tg_data->last_name = NULL;
152 tg_data->phone_number = NULL;
153 tg_data->sms_code = NULL;
154 tg_data->contact_list_to_add = NULL;
155 tg_data->new_group_icon = NULL;
156 tg_data->mhash = NULL;
157 tg_data->lazy_init_idler = NULL;
158 tg_data->get_string = NULL;
160 init_tl_engine(data);
161 tgl_login(tgl_engine_get_TLS());
162 tg_data->lazy_init_idler = NULL;
163 send_response_for_logout(tg_data);
165 } else if (strcmp(cmd_key_val, "restart_server") == 0) {
166 on_restart_service_requested(tg_data);
168 } else if (strcmp(cmd_key_val, "send_typing_status") == 0) {
170 char* buddy_id_str = NULL;
171 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
172 int buddy_id = atoi(buddy_id_str);
174 char* type_of_chat_str = NULL;
175 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
176 int type_of_chat = atoi(type_of_chat_str);
178 char* typing_status_str = NULL;
179 res = bundle_get_str(rec_msg, "typing_status", &typing_status_str);
180 int typing_status = atoi(typing_status_str);
182 process_typing_status_to_buddy_command(buddy_id, type_of_chat, typing_status);
184 } else if (strcmp(cmd_key_val, "message_forward") == 0) {
186 char* from_id_str = NULL;
187 res = bundle_get_str(rec_msg, "from_id", &from_id_str);
188 int from_id = atoi(from_id_str);
190 char* to_id_str = NULL;
191 res = bundle_get_str(rec_msg, "to_id", &to_id_str);
192 int to_id = atoi(to_id_str);
194 char* type_of_chat_str = NULL;
195 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
196 int type_of_chat = atoi(type_of_chat_str);
198 char* message_id_str = NULL;
199 res = bundle_get_str(rec_msg, "message_id", &message_id_str);
200 int message_id = atoi(message_id_str);
202 char* temp_message_id_str = NULL;
203 res = bundle_get_str(rec_msg, "temp_message_id", &temp_message_id_str);
204 int temp_message_id = atoi(temp_message_id_str);
206 process_forward_message_command(to_id, type_of_chat, from_id, message_id, temp_message_id);
208 } else if (strcmp(cmd_key_val, "message_transport") == 0) {
210 char* buddy_id_str = NULL;
211 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
212 int buddy_id = atoi(buddy_id_str);
214 char* message_id_str = NULL;
215 res = bundle_get_str(rec_msg, "message_id", &message_id_str);
216 int message_id = atoi(message_id_str);
218 char* msg_type_str = NULL;
219 res = bundle_get_str(rec_msg, "message_type", &msg_type_str);
220 int msg_type = atoi(msg_type_str);
222 char* msg_data_str = NULL;
223 res = bundle_get_str(rec_msg, "message_data", &msg_data_str);
225 char* type_of_chat_str = NULL;
226 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
227 int type_of_chat = atoi(type_of_chat_str);
229 process_send_message_command(buddy_id, message_id, msg_type, msg_data_str, type_of_chat);
231 } else if (strcmp(cmd_key_val, "delete_all_messages_from_table") == 0) {
233 char* buddy_id_str = NULL;
234 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
235 int buddy_id = atoi(buddy_id_str);
237 char* type_of_chat_str = NULL;
238 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
239 int type_of_chat = atoi(type_of_chat_str);
240 process_delete_all_msgs_from_table_command(buddy_id, type_of_chat);
242 } else if (strcmp(cmd_key_val, "marked_as_read") == 0) {
243 char* buddy_id_str = NULL;
244 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
245 int buddy_id = atoi(buddy_id_str);
247 char* type_of_chat_str = NULL;
248 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
249 int type_of_chat = atoi(type_of_chat_str);
250 process_marked_as_read_command(buddy_id, type_of_chat);
252 } else if (strcmp(cmd_key_val, "delete_group_chat_request") == 0) {
254 char* chat_id_str = NULL;
255 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
256 int chat_id = atoi(chat_id_str);
258 process_delete_group_chat_request(tg_data, chat_id);
260 } else if (strcmp(cmd_key_val, "delete_selected_group_chats_request") == 0) {
262 char* count_str = NULL;
263 res = bundle_get_str(rec_msg, "list_size", &count_str);
264 int size = atoi(count_str);
265 Eina_List* chat_id_list = NULL;
268 for (int count = 0 ; count < size ; count++) {
269 char chat_id_key[20];
270 snprintf(chat_id_key, sizeof(chat_id_key), "chat_id_%d", count);
272 char* chat_id_str = NULL;
273 res = bundle_get_str(rec_msg, chat_id_key, &chat_id_str);
275 chat_id = atoi(chat_id_str);
276 chat_id_list = eina_list_append(chat_id_list, &chat_id);
279 if (chat_id_list && eina_list_count(chat_id_list) > 0)
280 process_delete_selected_group_chats_request(tg_data, chat_id_list);
282 } else if (strcmp(cmd_key_val, "add_buddy") == 0) {
284 char* buddy_id_str = NULL;
285 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
286 int buddy_id = atoi(buddy_id_str);
288 char* first_name = NULL;
289 res = bundle_get_str(rec_msg, "first_name", &first_name);
291 char* last_name = NULL;
292 res = bundle_get_str(rec_msg, "last_name", &last_name);
294 char* phone_number = NULL;
295 res = bundle_get_str(rec_msg, "phone_number", &phone_number);
297 process_add_user_request(tg_data, buddy_id, first_name, last_name, phone_number);
298 } else if (strcmp(cmd_key_val, "update_chat_info") == 0) {
300 char* chat_id_str = NULL;
301 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
302 int chat_id = atoi(chat_id_str);
304 process_update_chat_request(tg_data, chat_id);
306 } else if (strcmp(cmd_key_val, "start_secret_chat") == 0) {
308 char* buddy_id_str = NULL;
309 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
310 int buddy_id = atoi(buddy_id_str);
312 process_send_secret_chat_request(tg_data, buddy_id);
315 } else if (strcmp(cmd_key_val, "delete_buddy") == 0) {
317 char* buddy_id_str = NULL;
318 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
319 int buddy_id = atoi(buddy_id_str);
321 process_delete_user_request(tg_data, buddy_id);
323 } else if (strcmp(cmd_key_val, "delete_message") == 0) {
325 char* buddy_id_str = NULL;
326 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
327 int buddy_id = atoi(buddy_id_str);
329 char* message_id_str = NULL;
330 res = bundle_get_str(rec_msg, "message_id", &message_id_str);
331 int message_id = atoi(message_id_str);
333 process_delete_message_request(tg_data, buddy_id, message_id);
335 } else if (strcmp(cmd_key_val, "block_buddy") == 0) {
337 char* buddy_id_str = NULL;
338 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
339 int buddy_id = atoi(buddy_id_str);
341 process_block_user_request(tg_data, buddy_id);
343 } else if (strcmp(cmd_key_val, "unblock_buddy") == 0) {
345 char* buddy_id_str = NULL;
346 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
347 int buddy_id = atoi(buddy_id_str);
349 process_unblock_user_request(tg_data, buddy_id);
351 } else if (strcmp(cmd_key_val, "media_download_request") == 0) {
353 char* buddy_id_str = NULL;
354 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
355 int buddy_id = atoi(buddy_id_str);
357 char* media_id_str = NULL;
358 res = bundle_get_str(rec_msg, "media_id", &media_id_str);
359 long long media_id = atoll(media_id_str);
360 process_media_download_command(tg_data, buddy_id, media_id);
362 } else if (strcmp(cmd_key_val, "media_transport") == 0) {
364 char* buddy_id_str = NULL;
365 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
366 int buddy_id = atoi(buddy_id_str);
368 char* message_id_str = NULL;
369 res = bundle_get_str(rec_msg, "message_id", &message_id_str);
370 int message_id = atoi(message_id_str);
372 char* media_id_str = NULL;
373 res = bundle_get_str(rec_msg, "media_id", &media_id_str);
374 int media_id = atoi(media_id_str);
376 char* msg_type_str = NULL;
377 res = bundle_get_str(rec_msg, "message_type", &msg_type_str);
378 int msg_type = atoi(msg_type_str);
380 char* file_path_str = NULL;
381 res = bundle_get_str(rec_msg, "file_path", &file_path_str);
383 char* type_of_chat_str = NULL;
384 res = bundle_get_str(rec_msg, "type_of_chat", &type_of_chat_str);
385 int type_of_chat = atoi(type_of_chat_str);
387 process_send_media_command(buddy_id, message_id, media_id, msg_type, file_path_str, type_of_chat);
389 } else if (strcmp(cmd_key_val, "device_contact_list") == 0) {
391 char* count_str = NULL;
392 res = bundle_get_str(rec_msg, "count", &count_str);
393 int size = atoi(count_str);
394 Eina_List* contact_list = NULL;
396 for (int count = 0 ; count < size ; count++) {
397 contact_data_s* contact = (contact_data_s*)malloc(sizeof(contact_data_s));
398 contact->first_name = NULL;
399 contact->last_name = NULL;
400 contact->phone_number = NULL;
402 char first_name_key[20];
403 snprintf(first_name_key, sizeof(first_name_key), "first_name_%d", count);
405 char last_name_key[20];
406 snprintf(first_name_key, sizeof(first_name_key), "last_name_%d", count);
408 char phone_number_key[20];
409 snprintf(first_name_key, sizeof(first_name_key), "phone_number_%d", count);
411 char* first_name = NULL;
412 res = bundle_get_str(rec_msg, first_name_key, &first_name);
415 contact->first_name = strdup(first_name);
417 char* last_name = NULL;
418 res = bundle_get_str(rec_msg, last_name_key, &last_name);
421 contact->last_name = strdup(last_name);
423 char* phone_number = NULL;
424 res = bundle_get_str(rec_msg, phone_number_key, &phone_number);
427 contact->phone_number = strdup(phone_number);
429 contact_list = eina_list_append(contact_list, contact);
432 process_add_contacts_command(tg_data, size, contact_list);
434 } else if (strcmp(cmd_key_val, "group_creation_request") == 0) {
435 char* count_str = NULL;
436 res = bundle_get_str(rec_msg, "count", &count_str);
437 int size = atoi(count_str);
438 Eina_List* buddy_ids = NULL;
440 for (int count = 0 ; count < size ; count++) {
441 char buddy_id_key[20];
442 snprintf(buddy_id_key, sizeof(buddy_id_key), "buddy_id_%d", count);
444 char* buddy_id = NULL;
445 res = bundle_get_str(rec_msg, buddy_id_key, &buddy_id);
447 buddy_ids = eina_list_append(buddy_ids, buddy_id);
450 char* temp_group_name = NULL;
451 res = bundle_get_str(rec_msg, "group_name", &temp_group_name);
452 char* group_name = strdup(temp_group_name);
454 char* temp_group_image = NULL;
455 res = bundle_get_str(rec_msg, "group_image", &temp_group_image);
456 char* group_image = strdup(temp_group_image);
458 process_new_group_create_command(tg_data, buddy_ids, group_name, group_image);
459 } else if (strcmp(cmd_key_val, "remove_buddy_from_chat_request") == 0) {
461 char *buddy_id_str = NULL;
462 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
466 buddy_id = atoi(buddy_id_str);
468 char *chat_id_str = NULL;
469 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
473 chat_id = atoi(chat_id_str);
475 process_remove_buddy_from_chat_command(tg_data, buddy_id, chat_id);
477 } else if (strcmp(cmd_key_val, "add_new_buddy_to_chat_request") == 0) {
479 char *buddy_id_str = NULL;
480 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
484 buddy_id = atoi(buddy_id_str);
486 char *chat_id_str = NULL;
487 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
491 chat_id = atoi(chat_id_str);
493 process_add_new_buddy_to_chat_command(tg_data, buddy_id, chat_id);
495 } else if (strcmp(cmd_key_val, "set_group_chat_new_title_request") == 0) {
497 char *buddy_id_str = NULL;
498 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
502 buddy_id = atoi(buddy_id_str);
504 char *new_title = NULL;
505 res = bundle_get_str(rec_msg, "new_title", &new_title);
506 process_set_group_chat_new_title_command(tg_data, buddy_id, new_title);
508 } else if (strcmp(cmd_key_val, "set_group_chat_profile_pic_request") == 0) {
510 char *buddy_id_str = NULL;
511 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
515 buddy_id = atoi(buddy_id_str);
517 char *file_path = NULL;
518 res = bundle_get_str(rec_msg, "file_path", &file_path);
519 process_set_group_chat_profile_pic_command(tg_data, buddy_id, file_path);
521 } else if (strcmp(cmd_key_val, "set_profile_pic_request") == 0) {
523 char *buddy_id_str = NULL;
524 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
528 buddy_id = atoi(buddy_id_str);
530 char *file_path = NULL;
531 res = bundle_get_str(rec_msg, "file_path", &file_path);
532 process_set_profile_pic_command(tg_data, buddy_id, file_path);
534 } else if (strcmp(cmd_key_val, "update_display_name_request") == 0) {
536 char *buddy_id_str = NULL;
537 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
541 buddy_id = atoi(buddy_id_str);
543 char *first_name = NULL;
544 res = bundle_get_str(rec_msg, "first_name", &first_name);
546 char *last_name = NULL;
547 res = bundle_get_str(rec_msg, "last_name", &last_name);
549 process_update_display_name_command(tg_data, buddy_id, first_name, last_name);
551 } else if (strcmp(cmd_key_val, "set_username_request") == 0) {
553 char *buddy_id_str = NULL;
554 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
558 buddy_id = atoi(buddy_id_str);
560 char *username = NULL;
561 res = bundle_get_str(rec_msg, "username", &username);
562 process_set_username_command(tg_data, buddy_id, username);
564 } else if (strcmp(cmd_key_val, "delete_all_notifications") == 0) {
565 if (tg_data->s_notififcation) {
566 notification_delete_all(NOTIFICATION_TYPE_NOTI);
567 tg_data->s_notififcation = NULL;
569 } else if (strcmp(cmd_key_val, "server_connection_status") == 0) {
570 send_response_for_server_connection_status(tg_data, tg_data->is_login_activated);
578 Eina_Bool on_code_request_timer_expired(void *data)
580 tg_engine_data_s *tg_data = data;
582 if (tg_data->code_response_timer == NULL)
583 return ECORE_CALLBACK_CANCEL;
585 if (tg_data->code_response_timer) {
586 ecore_timer_del(tg_data->code_response_timer);
587 tg_data->code_response_timer = NULL;
589 send_server_connection_failed_response(tg_data);
590 on_restart_service_requested(tg_data);
591 return ECORE_CALLBACK_CANCEL;
595 Eina_Bool event_idler_cb(void *data)
597 tg_engine_data_s *tg_data = data;
599 init_tl_engine(data);
600 tgl_login(tgl_engine_get_TLS());
602 tg_data->lazy_init_idler = NULL;
603 return ECORE_CALLBACK_CANCEL;
606 static Eina_Bool on_start_service_requested(void *data)
608 tg_engine_data_s *tg_data = data;
609 tg_data->is_login_activated = EINA_TRUE;
610 send_response_for_server_connection_status(tg_data, tg_data->is_login_activated);
611 init_tl_engine(data);
612 tgl_login(tgl_engine_get_TLS());
614 tg_data->lazy_init_idler = NULL;
615 return ECORE_CALLBACK_CANCEL;
618 Eina_Bool on_restart_service_requested(void *data)
620 tg_engine_data_s *tg_data = data;
621 send_server_restart_notification(tg_data);
623 return ECORE_CALLBACK_CANCEL;
626 void on_connection_type_changed_cb(connection_type_e type, void *user_data)
628 tg_engine_data_s *tg_data = user_data;
631 if (type == CONNECTION_TYPE_WIFI) {
632 connection_wifi_state_e wifi_state;
633 err_code = connection_get_wifi_state(tg_data->connection, &wifi_state);
634 if (err_code == CONNECTION_ERROR_NONE) {
635 switch (wifi_state) {
636 case CONNECTION_WIFI_STATE_CONNECTED:
637 tg_data->is_network_connected = EINA_TRUE;
639 case CONNECTION_WIFI_STATE_DEACTIVATED:
640 case CONNECTION_WIFI_STATE_DISCONNECTED:
642 /*This case happens, when data connection toggle on/off at the time of registration.
643 1. preserving phone num.
644 2. restarting the connection.(clearing the existing values)
645 3. Again login with old number.
647 if (tg_data->is_login_activated) {
648 char *phone_num = NULL;
649 if (tg_data->phone_number)
650 phone_num = strdup(tg_data->phone_number);
652 free_connection(tg_data);
653 tgl_engine_var_init();
655 tg_data->tg_state = TG_ENGINE_STATE_NONE;
656 tg_data->tg_server = tg_server_create();
657 tg_data->first_name = NULL;
658 tg_data->last_name = NULL;
659 tg_data->phone_number = NULL;
660 tg_data->sms_code = NULL;
661 tg_data->contact_list_to_add = NULL;
662 tg_data->new_group_icon = NULL;
663 tg_data->mhash = NULL;
664 tg_data->lazy_init_idler = NULL;
666 tg_data->phone_number = strdup(phone_num);
671 tg_data->is_login_activated = EINA_FALSE;
672 tg_data->is_network_connected = EINA_FALSE;
673 send_response_for_server_connection_status(tg_data, tg_data->is_login_activated);
677 } else if (type == CONNECTION_TYPE_CELLULAR) {
678 connection_cellular_state_e cellular_state;
679 err_code = connection_get_cellular_state(tg_data->connection, &cellular_state);
680 if (err_code == CONNECTION_ERROR_NONE && cellular_state == CONNECTION_CELLULAR_STATE_CONNECTED)
681 tg_data->is_network_connected = EINA_TRUE;
684 if (tg_data->is_login_activated) {
685 char *phone_num = NULL;
686 if (tg_data->phone_number)
687 phone_num = strdup(tg_data->phone_number);
689 free_connection(tg_data);
690 tgl_engine_var_init();
692 tg_data->tg_state = TG_ENGINE_STATE_NONE;
693 tg_data->tg_server = tg_server_create();
694 tg_data->first_name = NULL;
695 tg_data->last_name = NULL;
696 tg_data->phone_number = NULL;
697 tg_data->sms_code = NULL;
698 tg_data->contact_list_to_add = NULL;
699 tg_data->new_group_icon = NULL;
700 tg_data->mhash = NULL;
701 tg_data->lazy_init_idler = NULL;
703 tg_data->phone_number = strdup(phone_num);
707 tg_data->is_login_activated = EINA_FALSE;
708 tg_data->is_network_connected = EINA_FALSE;
709 send_response_for_server_connection_status(tg_data, tg_data->is_login_activated);
714 * We should find a gorgeous way to initialize the Telegram Service.
716 if (tg_data->is_network_connected) {
717 if (tg_data->is_login_activated) {
718 tg_data->is_login_activated = EINA_FALSE;
719 free_connection(tg_data);
720 ecore_timer_add(5, on_restart_service_requested, tg_data);
722 ecore_timer_add(5, on_start_service_requested, tg_data);
727 bool service_app_create(void *data)
731 tg_engine_data_s *tg_data = data;
732 RETVM_IF(!tg_data, SVC_RES_FAIL, "Application data is NULL");
733 tgl_engine_var_init();
735 tg_data->tg_state = TG_ENGINE_STATE_NONE;
736 tg_data->tg_server = tg_server_create();
737 tg_data->first_name = NULL;
738 tg_data->last_name = NULL;
739 tg_data->phone_number = NULL;
740 tg_data->sms_code = NULL;
741 tg_data->contact_list_to_add = NULL;
742 tg_data->new_group_icon = NULL;
743 tg_data->mhash = NULL;
744 tg_data->lazy_init_idler = NULL;
745 tg_data->s_notififcation = NULL;
746 tg_data->chat_list = NULL;
747 tg_data->current_chat_index = 0;
748 tg_data->buddy_list = NULL;
749 tg_data->peer_list = NULL;
750 tg_data->current_buddy_index = 0;
751 tg_data->is_first_time_registration = EINA_FALSE;
752 RETVM_IF(!tg_data->tg_server, SVC_RES_FAIL, "Failed to create proxy client");
754 result = tg_server_register_port(tg_data->tg_server, TELEGRAM_SERVER_PORT_NAME);
755 if (result != SVC_RES_OK) {
756 ERR("Failed to register proxy client port");
757 tg_server_destroy(tg_data->tg_server);
758 tg_data->tg_server = NULL;
763 result = _tg_server_set_remote_data(tg_data->tg_server, TELEGRAM_CLIENT_APP_NAME, TELEGRAM_CLIENT_PORT_NAME);
764 result = tg_server_register_msg_receive_callback(tg_data->tg_server, _on_tg_server_msg_received_cb, tg_data);
766 badge_new(TELEGRAM_APP_ID);
768 result = connection_create(&(tg_data->connection));
769 if (result == CONNECTION_ERROR_NONE) {
770 connection_type_e net_state;
772 result = connection_get_type(tg_data->connection, &net_state);
773 if (result == CONNECTION_ERROR_NONE) {
774 on_connection_type_changed_cb(net_state, tg_data);
776 result = connection_set_type_changed_cb(tg_data->connection, on_connection_type_changed_cb, tg_data);
777 if (result != CONNECTION_ERROR_NONE) {
780 * How can we handle this case?
785 ERR("Failed to initialize connection object");
786 tg_server_destroy(tg_data->tg_server);
787 tg_data->tg_server = NULL;
795 void free_connection(tg_engine_data_s* tg_data)
797 tg_data->is_login_activated = EINA_FALSE;
798 tg_data->is_network_connected = EINA_FALSE;
799 if (tg_data->phone_number) {
800 free(tg_data->phone_number);
801 tg_data->phone_number = NULL;
804 if (tg_data->sms_code) {
805 free(tg_data->sms_code);
806 tg_data->sms_code = NULL;
809 if (tg_data->first_name) {
810 free(tg_data->first_name);
811 tg_data->first_name = NULL;
814 if (tg_data->last_name) {
815 free(tg_data->last_name);
816 tg_data->last_name = NULL;
819 if (tg_data->contact_list_to_add) {
820 free_contact_data(tg_data->contact_list_to_add);
821 tg_data->contact_list_to_add = NULL;
824 if (tg_data->chat_list) {
825 eina_list_free(tg_data->chat_list);
826 tg_data->chat_list = NULL;
829 if (tg_data->buddy_list) {
830 eina_list_free(tg_data->buddy_list);
831 tg_data->buddy_list = NULL;
834 if (tg_data->peer_list) {
835 eina_list_free(tg_data->peer_list);
836 tg_data->peer_list = NULL;
839 tgl_engine_var_free();
844 void service_app_terminate(void *data)
846 tg_engine_data_s* tg_data = data;
847 if (tg_data->connection) {
848 connection_destroy(tg_data->connection);
849 tg_data->connection = NULL;
851 if (tg_data->tg_server) {
852 tg_server_destroy(tg_data->tg_server);
853 tg_data->tg_server = NULL;
855 if (tg_data && tg_data->s_notififcation) {
856 notification_delete_all(NOTIFICATION_TYPE_NOTI);
857 tg_data->s_notififcation = NULL;
860 free_connection(tg_data);
864 void service_app_control(app_control_h app_control, void *data)
870 service_app_lang_changed(app_event_info_h event_info, void *user_data)
876 service_app_orient_changed(app_event_info_h event_info, void *user_data)
882 service_app_region_changed(app_event_info_h event_info, void *user_data)
887 service_app_low_battery(app_event_info_h event_info, void *user_data)
892 service_app_low_memory(app_event_info_h event_info, void *user_data)
896 int main(int argc, char* argv[])
899 * static type qualifier will initialize the tg_data.
900 * and the tg_data will be allocated in the DATA section.
901 * we can save more stack or heap area.
903 static tg_engine_data_s tg_data;
904 service_app_lifecycle_callback_s event_callback;
905 app_event_handler_h handlers[5] = {NULL, };
907 event_callback.create = service_app_create;
908 event_callback.terminate = service_app_terminate;
909 event_callback.app_control = service_app_control;
911 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &tg_data);
912 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &tg_data);
913 service_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, service_app_orient_changed, &tg_data);
914 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &tg_data);
915 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &tg_data);
917 return service_app_main(argc, argv, &event_callback, &tg_data);