add telegram account, fix db insert error.
[apps/native/telegram-tizen.git] / tg-engine-service / src / tg-engine-service.c
1 /*
2     This file is part of Telegram application for tizen
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <tizen.h>
20 #include <service_app.h>
21 #include "tg-engine-service.h"
22 #include "tg_engine.h"
23 #include "tg_server.h"
24 #include "logger.h"
25 #include "server_response.h"
26 #include <net_connection.h>
27 #include "tg_db_wrapper.h"
28 #include <badge.h>
29 #include "tg_common.h"
30
31 static void free_connection(tg_engine_data_s* tg_data);
32
33 static int _on_tg_server_msg_received_cb(void *data, bundle *const rec_msg)
34 {
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;
39
40         int res = bundle_get_str(rec_msg, "command", &cmd_key_val);
41
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);
47
48 #if 0
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);
51 #endif
52                 Eina_Bool th_sms = EINA_TRUE;
53
54                 if (strcmp(sms_key_val, "true") == 0)
55                         th_sms = EINA_TRUE;
56                 else
57                         th_sms = EINA_FALSE;
58
59                 process_registration_command(tg_data, ph_no_key_val, th_sms);
60                 return result;
61
62         }
63
64         if (strcmp(cmd_key_val, "code_validation") == 0) {
65
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);
69                 return result;
70         }
71
72         if (strcmp(cmd_key_val, "profile_registration") == 0) {
73
74                 char* first_name = NULL;
75                 char* last_name = NULL;
76
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);
80
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);
84
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);
87
88                 return result;
89         }
90
91         if (!tg_data->is_login_activated) {
92                 send_server_not_initialized_response(tg_data);
93                 return result;
94         }
95
96         if (strcmp(cmd_key_val, "logout_telegram") == 0) {
97
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;
104                 }
105
106                 if (tg_data->sms_code) {
107                         free(tg_data->sms_code);
108                         tg_data->sms_code = NULL;
109                 }
110
111                 if (tg_data->first_name) {
112                         free(tg_data->first_name);
113                         tg_data->first_name = NULL;
114                 }
115
116                 if (tg_data->last_name) {
117                         free(tg_data->last_name);
118                         tg_data->last_name = NULL;
119                 }
120
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;
124                 }
125
126                 if (tg_data->chat_list) {
127                         eina_list_free(tg_data->chat_list);
128                         tg_data->chat_list = NULL;
129                 }
130
131                 if (tg_data->buddy_list) {
132                         eina_list_free(tg_data->buddy_list);
133                         tg_data->buddy_list = NULL;
134                 }
135
136                 if (tg_data->peer_list) {
137                         eina_list_free(tg_data->peer_list);
138                         tg_data->peer_list = NULL;
139                 }
140
141                 tgl_engine_var_free();
142
143                 tg_db_fini();
144
145                 recursive_dir_delete(app_get_data_path());
146
147                 tgl_engine_var_init();
148                 tg_db_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;
159
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);
164
165         } else if (strcmp(cmd_key_val, "restart_server") == 0) {
166                 on_restart_service_requested(tg_data);
167
168         } else if (strcmp(cmd_key_val, "send_typing_status") == 0) {
169
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);
173
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);
177
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);
181
182                 process_typing_status_to_buddy_command(buddy_id, type_of_chat, typing_status);
183
184         } else if (strcmp(cmd_key_val, "message_forward") == 0) {
185
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);
189
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);
193
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);
197
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);
201
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);
205
206                 process_forward_message_command(to_id, type_of_chat, from_id, message_id, temp_message_id);
207
208         } else if (strcmp(cmd_key_val, "message_transport") == 0) {
209
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);
213
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);
217
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);
221
222                 char* msg_data_str = NULL;
223                 res = bundle_get_str(rec_msg, "message_data", &msg_data_str);
224
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);
228
229                 process_send_message_command(buddy_id, message_id, msg_type, msg_data_str, type_of_chat);
230
231         } else if (strcmp(cmd_key_val, "delete_all_messages_from_table") == 0) {
232
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);
236
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);
241
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);
246
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);
251
252         } else if (strcmp(cmd_key_val, "delete_group_chat_request") == 0) {
253
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);
257
258                 process_delete_group_chat_request(tg_data, chat_id);
259
260         } else if (strcmp(cmd_key_val, "delete_selected_group_chats_request") == 0) {
261
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;
266                 int chat_id = 0;
267
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);
271
272                         char* chat_id_str = NULL;
273                         res = bundle_get_str(rec_msg, chat_id_key, &chat_id_str);
274
275                         chat_id = atoi(chat_id_str);
276                         chat_id_list = eina_list_append(chat_id_list, &chat_id);
277                 }
278
279                 if (chat_id_list && eina_list_count(chat_id_list) > 0)
280                         process_delete_selected_group_chats_request(tg_data, chat_id_list);
281
282         } else if (strcmp(cmd_key_val, "add_buddy") == 0) {
283
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);
287
288                 char* first_name = NULL;
289                 res = bundle_get_str(rec_msg, "first_name", &first_name);
290
291                 char* last_name = NULL;
292                 res = bundle_get_str(rec_msg, "last_name", &last_name);
293
294                 char* phone_number = NULL;
295                 res = bundle_get_str(rec_msg, "phone_number", &phone_number);
296
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) {
299
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);
303
304                 process_update_chat_request(tg_data, chat_id);
305
306         } else if (strcmp(cmd_key_val, "start_secret_chat") == 0) {
307
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);
311
312                 process_send_secret_chat_request(tg_data, buddy_id);
313
314
315         } else if (strcmp(cmd_key_val, "delete_buddy") == 0) {
316
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);
320
321                 process_delete_user_request(tg_data, buddy_id);
322
323         } else if (strcmp(cmd_key_val, "delete_message") == 0) {
324
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);
328
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);
332
333                 process_delete_message_request(tg_data, buddy_id, message_id);
334
335         } else if (strcmp(cmd_key_val, "block_buddy") == 0) {
336
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);
340
341                 process_block_user_request(tg_data, buddy_id);
342
343         } else if (strcmp(cmd_key_val, "unblock_buddy") == 0) {
344
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);
348
349                 process_unblock_user_request(tg_data, buddy_id);
350
351         } else if (strcmp(cmd_key_val, "media_download_request") == 0) {
352
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);
356
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);
361
362         } else if (strcmp(cmd_key_val, "media_transport") == 0) {
363
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);
367
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);
371
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);
375
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);
379
380                 char* file_path_str = NULL;
381                 res = bundle_get_str(rec_msg, "file_path", &file_path_str);
382
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);
386
387                 process_send_media_command(buddy_id, message_id, media_id, msg_type, file_path_str, type_of_chat);
388
389         } else if (strcmp(cmd_key_val, "device_contact_list") == 0) {
390
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;
395
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;
401
402                         char first_name_key[20];
403                         snprintf(first_name_key, sizeof(first_name_key), "first_name_%d", count);
404
405                         char last_name_key[20];
406                         snprintf(first_name_key, sizeof(first_name_key), "last_name_%d", count);
407
408                         char phone_number_key[20];
409                         snprintf(first_name_key, sizeof(first_name_key), "phone_number_%d", count);
410
411                         char* first_name = NULL;
412                         res = bundle_get_str(rec_msg, first_name_key, &first_name);
413
414                         if (first_name)
415                                 contact->first_name = strdup(first_name);
416
417                         char* last_name = NULL;
418                         res = bundle_get_str(rec_msg, last_name_key, &last_name);
419
420                         if (last_name)
421                                 contact->last_name = strdup(last_name);
422
423                         char* phone_number = NULL;
424                         res = bundle_get_str(rec_msg, phone_number_key, &phone_number);
425
426                         if (phone_number)
427                                 contact->phone_number = strdup(phone_number);
428
429                         contact_list = eina_list_append(contact_list, contact);
430                 }
431
432                 process_add_contacts_command(tg_data, size, contact_list);
433
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;
439
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);
443
444                         char* buddy_id = NULL;
445                         res = bundle_get_str(rec_msg, buddy_id_key, &buddy_id);
446
447                         buddy_ids = eina_list_append(buddy_ids, buddy_id);
448                 }
449
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);
453
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);
457
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) {
460
461                 char *buddy_id_str = NULL;
462                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
463
464                 int buddy_id = 0;
465                 if (buddy_id_str)
466                         buddy_id = atoi(buddy_id_str);
467
468                 char *chat_id_str = NULL;
469                 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
470
471                 int chat_id = 0;
472                 if (chat_id_str)
473                         chat_id = atoi(chat_id_str);
474
475                 process_remove_buddy_from_chat_command(tg_data, buddy_id, chat_id);
476
477         } else if (strcmp(cmd_key_val, "add_new_buddy_to_chat_request") == 0) {
478
479                 char *buddy_id_str = NULL;
480                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
481
482                 int buddy_id = 0;
483                 if (buddy_id_str)
484                         buddy_id = atoi(buddy_id_str);
485
486                 char *chat_id_str = NULL;
487                 res = bundle_get_str(rec_msg, "chat_id", &chat_id_str);
488
489                 int chat_id = 0;
490                 if (chat_id_str)
491                         chat_id = atoi(chat_id_str);
492
493                 process_add_new_buddy_to_chat_command(tg_data, buddy_id, chat_id);
494
495         } else if (strcmp(cmd_key_val, "set_group_chat_new_title_request") == 0) {
496
497                 char *buddy_id_str = NULL;
498                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
499
500                 int buddy_id = 0;
501                 if (buddy_id_str)
502                         buddy_id = atoi(buddy_id_str);
503
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);
507
508         } else if (strcmp(cmd_key_val, "set_group_chat_profile_pic_request") == 0) {
509
510                 char *buddy_id_str = NULL;
511                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
512
513                 int buddy_id = 0;
514                 if (buddy_id_str)
515                         buddy_id = atoi(buddy_id_str);
516
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);
520
521         } else if (strcmp(cmd_key_val, "set_profile_pic_request") == 0) {
522
523                 char *buddy_id_str = NULL;
524                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
525
526                 int buddy_id = 0;
527                 if (buddy_id_str)
528                         buddy_id = atoi(buddy_id_str);
529
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);
533
534         } else if (strcmp(cmd_key_val, "update_display_name_request") == 0) {
535
536                 char *buddy_id_str = NULL;
537                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
538
539                 int buddy_id = 0;
540                 if (buddy_id_str)
541                         buddy_id = atoi(buddy_id_str);
542
543                 char *first_name = NULL;
544                 res = bundle_get_str(rec_msg, "first_name", &first_name);
545
546                 char *last_name = NULL;
547                 res = bundle_get_str(rec_msg, "last_name", &last_name);
548
549                 process_update_display_name_command(tg_data, buddy_id, first_name, last_name);
550
551         } else if (strcmp(cmd_key_val, "set_username_request") == 0) {
552
553                 char *buddy_id_str = NULL;
554                 res = bundle_get_str(rec_msg, "buddy_id", &buddy_id_str);
555
556                 int buddy_id = 0;
557                 if (buddy_id_str)
558                         buddy_id = atoi(buddy_id_str);
559
560                 char *username = NULL;
561                 res = bundle_get_str(rec_msg, "username", &username);
562                 process_set_username_command(tg_data, buddy_id, username);
563
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;
568                 }
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);
571         } else {
572
573         }
574
575         return result;
576 }
577 #if 0
578 Eina_Bool on_code_request_timer_expired(void *data)
579 {
580         tg_engine_data_s *tg_data = data;
581
582         if (tg_data->code_response_timer == NULL)
583                 return ECORE_CALLBACK_CANCEL;
584
585         if (tg_data->code_response_timer) {
586                 ecore_timer_del(tg_data->code_response_timer);
587                 tg_data->code_response_timer = NULL;
588         }
589         send_server_connection_failed_response(tg_data);
590         on_restart_service_requested(tg_data);
591         return ECORE_CALLBACK_CANCEL;
592 }
593 #endif
594
595 Eina_Bool event_idler_cb(void *data)
596 {
597         tg_engine_data_s *tg_data = data;
598
599         init_tl_engine(data);
600         tgl_login(tgl_engine_get_TLS());
601
602         tg_data->lazy_init_idler = NULL;
603         return ECORE_CALLBACK_CANCEL;
604 }
605
606 static Eina_Bool on_start_service_requested(void *data)
607 {
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());
613
614         tg_data->lazy_init_idler = NULL;
615         return ECORE_CALLBACK_CANCEL;
616 }
617
618 Eina_Bool on_restart_service_requested(void *data)
619 {
620         tg_engine_data_s *tg_data = data;
621         send_server_restart_notification(tg_data);
622         service_app_exit();
623         return ECORE_CALLBACK_CANCEL;
624 }
625
626 void on_connection_type_changed_cb(connection_type_e type, void *user_data)
627 {
628         tg_engine_data_s *tg_data = user_data;
629         int err_code;
630
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;
638                                 break;
639                         case CONNECTION_WIFI_STATE_DEACTIVATED:
640                         case CONNECTION_WIFI_STATE_DISCONNECTED:
641                         default:
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.
646                                  */
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);
651
652                                         free_connection(tg_data);
653                                         tgl_engine_var_init();
654                                         tg_db_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;
665                                         if (phone_num) {
666                                                 tg_data->phone_number = strdup(phone_num);
667                                                 free(phone_num);
668                                         }
669                                 }
670
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);
674                                 break;
675                         }
676                 }
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;
682
683         } else {
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);
688
689                         free_connection(tg_data);
690                         tgl_engine_var_init();
691                         tg_db_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;
702                         if (phone_num) {
703                                 tg_data->phone_number = strdup(phone_num);
704                                 free(phone_num);
705                         }
706                 }
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);
710         }
711
712         /**
713          * @note
714          * We should find a gorgeous way to initialize the Telegram Service.
715          */
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);
721                 } else {
722                         ecore_timer_add(5, on_start_service_requested, tg_data);
723                 }
724         }
725 }
726
727 bool service_app_create(void *data)
728 {
729         int result;
730
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();
734         tg_db_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");
753
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;
759                 tg_db_fini();
760                 return EINA_FALSE;
761         }
762
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);
765
766         badge_new(TELEGRAM_APP_ID);
767
768         result = connection_create(&(tg_data->connection));
769         if (result == CONNECTION_ERROR_NONE) {
770                 connection_type_e net_state;
771
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);
775
776                         result = connection_set_type_changed_cb(tg_data->connection, on_connection_type_changed_cb, tg_data);
777                         if (result != CONNECTION_ERROR_NONE) {
778                                 /**
779                                  * @note
780                                  * How can we handle this case?
781                                  */
782                         }
783                 }
784         } else {
785                 ERR("Failed to initialize connection object");
786                 tg_server_destroy(tg_data->tg_server);
787                 tg_data->tg_server = NULL;
788                 tg_db_fini();
789                 return false;
790         }
791
792         return true;
793 }
794
795 void free_connection(tg_engine_data_s* tg_data)
796 {
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;
802         }
803
804         if (tg_data->sms_code) {
805                 free(tg_data->sms_code);
806                 tg_data->sms_code = NULL;
807         }
808
809         if (tg_data->first_name) {
810                 free(tg_data->first_name);
811                 tg_data->first_name = NULL;
812         }
813
814         if (tg_data->last_name) {
815                 free(tg_data->last_name);
816                 tg_data->last_name = NULL;
817         }
818
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;
822         }
823
824         if (tg_data->chat_list) {
825                 eina_list_free(tg_data->chat_list);
826                 tg_data->chat_list = NULL;
827         }
828
829         if (tg_data->buddy_list) {
830                 eina_list_free(tg_data->buddy_list);
831                 tg_data->buddy_list = NULL;
832         }
833
834         if (tg_data->peer_list) {
835                 eina_list_free(tg_data->peer_list);
836                 tg_data->peer_list = NULL;
837         }
838
839         tgl_engine_var_free();
840
841         tg_db_fini();
842 }
843
844 void service_app_terminate(void *data)
845 {
846         tg_engine_data_s* tg_data = data;
847         if (tg_data->connection) {
848                 connection_destroy(tg_data->connection);
849                 tg_data->connection = NULL;
850         }
851         if (tg_data->tg_server) {
852                 tg_server_destroy(tg_data->tg_server);
853                 tg_data->tg_server = NULL;
854         }
855         if (tg_data && tg_data->s_notififcation) {
856                 notification_delete_all(NOTIFICATION_TYPE_NOTI);
857                 tg_data->s_notififcation = NULL;
858         }
859
860         free_connection(tg_data);
861         return;
862 }
863
864 void service_app_control(app_control_h app_control, void *data)
865 {
866         return;
867 }
868
869 static void
870 service_app_lang_changed(app_event_info_h event_info, void *user_data)
871 {
872         return;
873 }
874
875 static void
876 service_app_orient_changed(app_event_info_h event_info, void *user_data)
877 {
878         return;
879 }
880
881 static void
882 service_app_region_changed(app_event_info_h event_info, void *user_data)
883 {
884 }
885
886 static void
887 service_app_low_battery(app_event_info_h event_info, void *user_data)
888 {
889 }
890
891 static void
892 service_app_low_memory(app_event_info_h event_info, void *user_data)
893 {
894 }
895
896 int main(int argc, char* argv[])
897 {
898         /**
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.
902          */
903         static tg_engine_data_s tg_data;
904         service_app_lifecycle_callback_s event_callback;
905         app_event_handler_h handlers[5] = {NULL, };
906
907         event_callback.create = service_app_create;
908         event_callback.terminate = service_app_terminate;
909         event_callback.app_control = service_app_control;
910
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);
916
917         return service_app_main(argc, argv, &event_callback, &tg_data);
918 }