add telegram account, fix db insert error.
[apps/native/telegram-tizen.git] / TelegramTizen / src / tg_user_info_view.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 "tg_user_info_view.h"
20 #include "server_requests.h"
21 #include "tg_user_main_view.h"
22 #include "tg_add_contact.h"
23 #include "tg_db_wrapper.h"
24 #include "tg_search_peer_view.h"
25
26 static Evas_Object* get_image_from_path(const char* path, Evas_Object* parent)
27 {
28         if (!path || !parent)
29                 return NULL;
30
31         Evas_Object *media_image = elm_image_add(parent);
32         elm_image_aspect_fixed_set(media_image, EINA_FALSE);
33         elm_image_file_set(media_image, path, NULL);
34         evas_object_size_hint_weight_set(media_image, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
35         evas_object_size_hint_align_set(media_image, EVAS_HINT_FILL, EVAS_HINT_FILL);
36         evas_object_show(media_image);
37         return media_image;
38 }
39
40 void on_user_added_response_received(appdata_s *ad, int buddy_id, Eina_Bool is_success)
41 {
42         if (!is_success || !ad->buddy_in_cahtting_data || !ad->peer_in_cahtting_data || ad->peer_in_cahtting_data->use_data->peer_id != buddy_id) {
43                 show_toast(ad, "Failed to add user.");
44                 return;
45         }
46
47         ad->buddy_in_cahtting_data->use_data->is_deleted = 0;
48         load_buddy_list_data(ad);
49         load_unknown_buddy_list_data(ad);
50         load_peer_data(ad);
51         load_main_list_data(ad);
52         refresh_main_list_view(ad, EINA_FALSE);
53
54         Eina_List *l = NULL;
55         user_data_with_pic_s *item = NULL;
56         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
57                 user_data_s* user_data = item->use_data;
58                 if (user_data->user_id.id == buddy_id) {
59                         ad->buddy_in_cahtting_data = item;
60                         break;
61                 }
62         }
63
64         if (ad->buddy_in_cahtting_data == NULL) {
65                 l = NULL;
66                 user_data_with_pic_s *item = NULL;
67                 EINA_LIST_FOREACH(ad->unknown_buddy_list, l, item) {
68                         user_data_s* user_data = item->use_data;
69                         if (user_data->user_id.id == buddy_id) {
70                                 ad->buddy_in_cahtting_data = item;
71                                 break;
72                         }
73                 }
74         }
75         l = NULL;
76         peer_with_pic_s* pic_item = NULL;
77         int pos = 0;
78         EINA_LIST_FOREACH(ad->peer_list, l, pic_item) {
79                 tg_peer_info_s* item = pic_item->use_data;
80                 if (item->peer_id == buddy_id) {
81                         ad->peer_in_cahtting_data = pic_item;
82                         buddy_id = pos;
83                         break;
84                 }
85                 pos++;
86         }
87
88         show_toast(ad, "User added successfully");
89 }
90
91 void on_user_deleted_response_received(appdata_s *ad, int buddy_id, Eina_Bool is_success)
92 {
93         if (!is_success || !ad->buddy_in_cahtting_data || !ad->peer_in_cahtting_data || ad->peer_in_cahtting_data->use_data->peer_id != buddy_id) {
94                 show_toast(ad, "Failed to delete user.");
95                 return;
96         }
97
98         ad->buddy_in_cahtting_data->use_data->is_deleted = 1;
99         load_buddy_list_data(ad);
100         load_unknown_buddy_list_data(ad);
101         load_peer_data(ad);
102         load_main_list_data(ad);
103         refresh_main_list_view(ad, EINA_FALSE);
104
105         Eina_List *l = NULL;
106         user_data_with_pic_s *item = NULL;
107         EINA_LIST_FOREACH(ad->buddy_list, l, item) {
108                 user_data_s* user_data = item->use_data;
109                 if (user_data->user_id.id == buddy_id) {
110                         ad->buddy_in_cahtting_data = item;
111                         break;
112                 }
113         }
114
115         if (ad->buddy_in_cahtting_data == NULL) {
116                 l = NULL;
117                 user_data_with_pic_s *item = NULL;
118                 EINA_LIST_FOREACH(ad->unknown_buddy_list, l, item) {
119                         user_data_s* user_data = item->use_data;
120                         if (user_data->user_id.id == buddy_id) {
121                                 ad->buddy_in_cahtting_data = item;
122                                 break;
123                         }
124                 }
125         }
126
127         l = NULL;
128         peer_with_pic_s* pic_item = NULL;
129         int pos = 0;
130         EINA_LIST_FOREACH(ad->peer_list, l, pic_item) {
131                 tg_peer_info_s* item = pic_item->use_data;
132                 if (item->peer_id == buddy_id) {
133                         ad->peer_in_cahtting_data = pic_item;
134                         buddy_id = pos;
135                         break;
136                 }
137                 pos++;
138         }
139
140         show_toast(ad, "User deleted successfully");
141
142 }
143
144 void on_user_block_response_received(appdata_s *ad, int buddy_id, Eina_Bool is_success)
145 {
146         if (!is_success || !ad->buddy_in_cahtting_data || !ad->peer_in_cahtting_data || ad->peer_in_cahtting_data->use_data->peer_id != buddy_id) {
147                 show_toast(ad, "Failed to block user.");
148         }
149         ad->buddy_in_cahtting_data->use_data->is_blocked = 1;
150         show_toast(ad, "User blocked successfully");
151 }
152
153 void on_user_unblock_response_received(appdata_s *ad, int buddy_id, Eina_Bool is_success)
154 {
155         if (!is_success || !ad->buddy_in_cahtting_data || !ad->peer_in_cahtting_data || ad->peer_in_cahtting_data->use_data->peer_id != buddy_id) {
156                 show_toast(ad, "Failed to unblock user.");
157         }
158         ad->buddy_in_cahtting_data->use_data->is_blocked = 0;
159         show_toast(ad, "User unblocked successfully");
160 }
161
162
163 static void on_userinfo_menu_canceled(void *data, Evas_Object *obj, void *event_info)
164 {
165         //appdata_s *ad = evas_object_data_get(obj, "app_data");
166         Evas_Object *popup = evas_object_data_get(obj, "popup");
167         evas_object_del(popup);
168 }
169
170 static void on_user_block_ok_selected(void *data, Evas_Object *obj, void *event_info)
171 {
172         appdata_s *ad = evas_object_data_get(obj, "app_data");
173         Evas_Object *popup = evas_object_data_get(obj, "popup");
174
175         if (ad->peer_in_cahtting_data && ad->peer_in_cahtting_data->use_data) {
176                 show_loading_popup(ad);
177                 peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
178                 if (get_buddy_block_status(sel_item->use_data->peer_id) == 1) {
179                         send_unblock_buddy_request(ad, ad->service_client, ad->peer_in_cahtting_data->use_data->peer_id);
180                 } else {
181                         send_block_buddy_request(ad, ad->service_client, ad->peer_in_cahtting_data->use_data->peer_id);
182                 }
183         }
184         evas_object_del(popup);
185 }
186
187 static void on_block_selected_cb(appdata_s *ad)
188 {
189         Evas_Object *popup, *btn;
190
191         /* popup */
192         popup = elm_popup_add(ad->nf);
193         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
194         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
195         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
196
197         peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
198         if (get_buddy_block_status(sel_item->use_data->peer_id) == 1) {
199                 elm_object_text_set(popup, "Are you sure want to unblock this contact?");
200         } else {
201                 elm_object_text_set(popup, "Are you sure want to block this contact?");
202         }
203
204         /* ok button */
205         btn = elm_button_add(popup);
206         elm_object_style_set(btn, "popup");
207         elm_object_text_set(btn, "OK");
208         elm_object_part_content_set(popup, "button1", btn);
209         evas_object_smart_callback_add(btn, "clicked", on_user_block_ok_selected, ad);
210         evas_object_data_set(btn, "app_data", ad);
211         evas_object_data_set(btn, "popup", popup);
212
213         /* cancel button */
214         btn = elm_button_add(popup);
215         elm_object_style_set(btn, "popup");
216         elm_object_text_set(btn, "Cancel");
217         elm_object_part_content_set(popup, "button2", btn);
218         evas_object_smart_callback_add(btn, "clicked", on_userinfo_menu_canceled, ad);
219         evas_object_data_set(btn, "app_data", ad);
220         evas_object_data_set(btn, "popup", popup);
221
222         evas_object_show(popup);
223 }
224
225 /************************ delete user *****************************/
226
227 static void on_user_delete_ok_selected(void *data, Evas_Object *obj, void *event_info)
228 {
229         appdata_s *ad = evas_object_data_get(obj, "app_data");
230         Evas_Object *popup = evas_object_data_get(obj, "popup");
231
232         if (ad->peer_in_cahtting_data && ad->peer_in_cahtting_data->use_data) {
233                 peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
234                 if (get_buddy_delete_status(sel_item->use_data->peer_id) == 1 || get_buddy_unknown_status(sel_item->use_data->peer_id) == 1) {
235                         launch_add_contact_screen(ad);
236                         //send_add_buddy_request(ad->service_client, ad->peer_in_cahtting_data->use_data->peer_id);
237                 } else {
238                         show_loading_popup(ad);
239                         send_delete_buddy_request(ad, ad->service_client, ad->peer_in_cahtting_data->use_data->peer_id);
240                 }
241
242         }
243         evas_object_del(popup);
244 }
245
246 static void on_delete_selected_cb(appdata_s *ad)
247 {
248         Evas_Object *popup, *btn;
249
250         /* popup */
251         popup = elm_popup_add(ad->nf);
252         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
253         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
254         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
255         peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
256         if (get_buddy_delete_status(sel_item->use_data->peer_id) == 1 || get_buddy_unknown_status(sel_item->use_data->peer_id) == 1) {
257                 elm_object_text_set(popup, "Are you sure want to add this contact?");
258         } else {
259                 elm_object_text_set(popup, "Are you sure want to delete this contact?");
260         }
261
262         /* ok button */
263         btn = elm_button_add(popup);
264         elm_object_style_set(btn, "popup");
265         elm_object_text_set(btn, "OK");
266         elm_object_part_content_set(popup, "button1", btn);
267         evas_object_smart_callback_add(btn, "clicked", on_user_delete_ok_selected, ad);
268         evas_object_data_set(btn, "app_data", ad);
269         evas_object_data_set(btn, "popup", popup);
270
271         /* cancel button */
272         btn = elm_button_add(popup);
273         elm_object_style_set(btn, "popup");
274         elm_object_text_set(btn, "Cancel");
275         elm_object_part_content_set(popup, "button2", btn);
276         evas_object_smart_callback_add(btn, "clicked", on_userinfo_menu_canceled, ad);
277         evas_object_data_set(btn, "app_data", ad);
278         evas_object_data_set(btn, "popup", popup);
279
280         evas_object_show(popup);
281 }
282
283 /************************ delete user *****************************/
284
285 /************************ Menu Handler ********************/
286
287 void on_user_info_menu_option_selected_cb(void *data, Evas_Object *obj, void *event_info)
288 {
289         appdata_s *ad = data;
290         Elm_Object_Item *it = event_info;
291
292         if(!ad || !it)
293                 return;
294
295         elm_genlist_item_selected_set(it, EINA_FALSE);
296         int id = (int)elm_object_item_data_get(it);
297
298         peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
299         tg_peer_info_s *user_data = sel_item->use_data;
300
301         if (!sel_item || !user_data)
302                 return;
303
304
305         switch(id) {
306         case 0:
307                 if (user_data->peer_type == TGL_PEER_USER) {
308                         if (get_buddy_delete_status(sel_item->use_data->peer_id) == 1 || get_buddy_unknown_status(sel_item->use_data->peer_id) == 1) {
309                                 // To-Do
310                         } else {
311                                 on_delete_selected_cb(ad);
312                         }
313                 }
314                 break;
315         case 1:
316                 ad->is_loading_from_profile_view = EINA_TRUE;
317                 on_create_new_contact(ad);
318                 break;
319         case 2:
320                 show_toast(ad, i18n_get_text("IDS_TGRAM_OPT_SHARE"));
321                 break;
322         case 3:
323                 if (user_data->peer_type == TGL_PEER_USER) {
324                         on_block_selected_cb(ad);
325                 }
326                 break;
327         }
328         if (ad->msg_popup) {
329                 evas_object_del(ad->msg_popup);
330                 ad->msg_popup = NULL;
331         }
332 }
333
334 char* on_user_info_menu_text_get_cb(void *data, Evas_Object *obj, const char *part)
335 {
336         int id = (int) data;
337         appdata_s *ad = evas_object_data_get(obj, "app_data");
338         peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
339
340         if (!ad || !sel_item)
341                 return NULL;
342
343         switch(id) {
344         case 0:
345                 if (get_buddy_delete_status(sel_item->use_data->peer_id) == 1 ||
346                                 get_buddy_unknown_status(sel_item->use_data->peer_id) == 1)
347                         return strdup(i18n_get_text("IDS_TGRAM_OPT_ADD_TO_TELEGRAM"));
348                 else
349                         return strdup(i18n_get_text("IDS_TGRAM_OPT_DELETE"));
350
351         case 1:
352                 if(!is_telegram_account(sel_item->use_data->peer_id))
353                         return strdup(i18n_get_text("IDS_TGRAM_OPT_EDIT"));
354                 else
355                         return NULL;
356
357         case 2:
358                 return strdup(i18n_get_text("IDS_TGRAM_OPT_SHARE"));
359
360         case 3:
361                 if (get_buddy_block_status(sel_item->use_data->peer_id) == 1)
362                         return strdup(i18n_get_text("IDS_TGRAM_OPT_UNBLOCK"));
363                 else
364                         return strdup(i18n_get_text("IDS_TGRAM_OPT_BLOCK"));
365         }
366         return NULL;
367 }
368
369 void on_user_info_msg_popup_back_cb(void *data, Evas_Object *obj, void *event_info)
370 {
371         appdata_s *ad = data;
372         if (ad->msg_popup) {
373                 evas_object_del(ad->msg_popup);
374                 ad->msg_popup = NULL;
375         }
376 }
377
378 void on_user_info_menu_button_clicked(void *data, Evas_Object *obj, void *event_info)
379 {
380         appdata_s *ad = data;
381         if (ad->msg_popup || ad->current_app_state != TG_SET_USER_INFO_STATE)
382                 return;
383
384
385         static Elm_Genlist_Item_Class itc;
386         Evas_Object *genlist;
387         Evas_Object *win = ad->win;
388
389         ad->msg_popup = elm_popup_add(win);
390         elm_popup_align_set(ad->msg_popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
391         eext_object_event_callback_add(ad->msg_popup, EEXT_CALLBACK_BACK, on_user_info_msg_popup_back_cb, ad);
392         evas_object_size_hint_weight_set(ad->msg_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
393
394         evas_object_data_set(ad->msg_popup, "app_data", ad);
395
396         genlist = elm_genlist_add(ad->msg_popup);
397         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
398         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
399
400         evas_object_data_set(genlist, "app_data", ad);
401
402         itc.item_style = "default";
403         itc.func.text_get = on_user_info_menu_text_get_cb;
404         itc.func.content_get = NULL;
405         itc.func.state_get = NULL;
406         itc.func.del = NULL;
407
408         int number_of_menus = 4;
409         Eina_Bool is_need_continue = EINA_FALSE;
410         peer_with_pic_s  *sel_item = ad->peer_in_cahtting_data;
411         if(sel_item && is_telegram_account(sel_item->use_data->peer_id)) {
412                 number_of_menus = 3;
413                 is_need_continue = EINA_TRUE;
414         }
415
416         for (int i = 0; i < number_of_menus; i++) {
417                 if(is_need_continue && i == 1)
418                         continue;
419                 elm_genlist_item_append(genlist, &itc, (void *) i, NULL, ELM_GENLIST_ITEM_NONE, on_user_info_menu_option_selected_cb, ad);
420         }
421
422         evas_object_show(genlist);
423         elm_object_content_set(ad->msg_popup, genlist);
424         evas_object_show(ad->msg_popup);
425 }
426
427
428 /************************ Menu Handler ********************/
429
430 Evas_Object* on_buddy_info_requested(void *data, Evas_Object *obj, const char *part)
431 {
432         Evas_Object *eo = NULL;
433         appdata_s* ad = evas_object_data_get(obj, "app_data");
434         if (!strcmp(part, "elm.swallow.icon")) {
435                 Evas_Object *profile_pic = NULL;
436                 if (ad->peer_in_cahtting_data->use_data->photo_path && strlen(ad->peer_in_cahtting_data->use_data->photo_path) > 0 && strstr(ad->peer_in_cahtting_data->use_data->photo_path, "_null_") == NULL) {
437                         profile_pic = get_image_from_path(ad->peer_in_cahtting_data->use_data->photo_path, ad->nf);
438                 } else  {
439                         profile_pic = get_image_from_path(ui_utils_get_resource(DEFAULT_PROFILE_PIC), ad->nf);
440                 }
441                 char edj_path[PATH_MAX] = {0, };
442                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
443                 Evas_Object* user_pic_layout = elm_layout_add(ad->nf);
444                 elm_layout_file_set(user_pic_layout, edj_path, "circle_layout");
445                 evas_object_size_hint_weight_set(user_pic_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
446                 evas_object_size_hint_align_set(user_pic_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
447                 evas_object_show(user_pic_layout);
448                 elm_object_part_content_set(user_pic_layout, "content", profile_pic);
449
450                 evas_object_data_set(ad->nf, "user_profile_pic", profile_pic);
451
452                 eo = elm_layout_add(obj);
453                 elm_layout_theme_set(eo, "layout", "list/C/type.1", "default");
454                 elm_layout_content_set(eo, "elm.swallow.content", user_pic_layout);
455         } else if (!strcmp(part, "elm.swallow.icon.0")) {
456                 char edj_path[PATH_MAX] = {0, };
457                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
458                 Evas_Object* user_status_layout = elm_layout_add(ad->nf);
459                 elm_layout_file_set(user_status_layout, edj_path, "main_list_name_status_item");
460                 evas_object_size_hint_weight_set(user_status_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
461                 evas_object_size_hint_align_set(user_status_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
462                 evas_object_show(user_status_layout);
463
464                 char* user_name = replace(ad->peer_in_cahtting_data->use_data->print_name, '_', " ");
465                 char buf[512] = {'\0'};
466                 snprintf(buf, 512, "<font=Tizen:style=Bold color=#FFFFFF align=left><font_size=40>%s</font_size></font>", user_name);
467                 free(user_name);
468
469                 Evas_Object*  name_lbl = elm_label_add(ad->nf);
470                 elm_object_text_set(name_lbl, buf);
471                 evas_object_size_hint_weight_set(name_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
472                 evas_object_size_hint_align_set(name_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
473                 evas_object_show(name_lbl);
474
475                 elm_object_part_content_set(user_status_layout, "swallow.name", name_lbl);
476
477                 Evas_Object *profile_time = evas_object_data_get(ad->nf, "profile_time");
478                 char *online_status = "";
479                 if (profile_time)
480                         online_status = elm_entry_markup_to_utf8(elm_object_text_get(profile_time));
481
482                 char status_buf[126] = {'\0'};
483                 sprintf(status_buf, "<font=Tizen:style=Normal color=#FFFFFF align=left><font_size=35>%s</font_size></font>", online_status);
484
485                 Evas_Object*  status_lbl = elm_label_add(ad->nf);
486                 elm_object_text_set(status_lbl, status_buf);
487                 evas_object_size_hint_weight_set(status_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
488                 evas_object_size_hint_align_set(status_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
489                 evas_object_show(status_lbl);
490
491                 elm_object_part_content_set(user_status_layout, "swallow.status", status_lbl);
492                 return user_status_layout;
493         } else {
494                 // nothing to do
495         }
496         return eo;
497 }
498
499 Evas_Object* on_buddy_phone_info_requested(void *data, Evas_Object *obj, const char *part)
500 {
501         Evas_Object *eo = NULL;
502         if (strcmp(part, "elm.swallow.icon.0")) {
503                 return eo;
504         }
505         int id = (int) data;
506         appdata_s* ad = evas_object_data_get(obj, "app_data");
507
508         char edj_path[PATH_MAX] = {0, };
509         app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
510         Evas_Object* user_status_layout = elm_layout_add(ad->nf);
511         elm_layout_file_set(user_status_layout, edj_path, "main_list_name_status_item");
512         evas_object_size_hint_weight_set(user_status_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
513         evas_object_size_hint_align_set(user_status_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
514         evas_object_show(user_status_layout);
515
516         char* first_text = NULL;
517         char buf[512] = {'\0'};
518         if (id == 0) {
519                 if (ad->buddy_in_cahtting_data && ad->buddy_in_cahtting_data->use_data->phone) {
520                         first_text = ad->buddy_in_cahtting_data->use_data->phone;
521                 } else {
522                         char *phone_no = get_buddy_phone_num_from_id(ad->peer_in_cahtting_data->use_data->peer_id);
523                         if (phone_no) {
524                                 first_text = phone_no;
525                         } else {
526                                 first_text = "";
527                         }
528                 }
529         } else {
530                 if (ad->buddy_in_cahtting_data && ad->buddy_in_cahtting_data->use_data->username && strlen(ad->buddy_in_cahtting_data->use_data->username) > 0 && strstr(ad->buddy_in_cahtting_data->use_data->username, "_null_") == NULL) {
531                         first_text = ad->buddy_in_cahtting_data->use_data->username;
532                 } else {
533                         first_text = "None";
534                 }
535         }
536
537         snprintf(buf, sizeof(buf) - 1, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", first_text);
538
539         Evas_Object*  name_lbl = elm_label_add(ad->nf);
540         elm_object_text_set(name_lbl, buf);
541         evas_object_size_hint_weight_set(name_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
542         evas_object_size_hint_align_set(name_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
543         evas_object_show(name_lbl);
544
545         if (id == 1)
546                 evas_object_data_set(ad->nf, "settings_user_name_label", name_lbl);
547
548         elm_object_part_content_set(user_status_layout, "swallow.name", name_lbl);
549
550         char* second_text = NULL;
551         if (id == 0) {
552                 second_text = "Phone";
553         } else {
554                 second_text = "Username";
555         }
556         char status_buf[126] = {'\0'};
557         sprintf(status_buf, "<font=Tizen:style=Normal color=#808080 align=left><font_size=35>%s</font_size></font>", second_text);
558
559         Evas_Object*  status_lbl = elm_label_add(ad->nf);
560         elm_object_text_set(status_lbl, status_buf);
561         evas_object_size_hint_weight_set(status_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
562         evas_object_size_hint_align_set(status_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
563         evas_object_show(status_lbl);
564
565         elm_object_part_content_set(user_status_layout, "swallow.status", status_lbl);
566         return user_status_layout;
567
568 }
569
570 void on_message_button_clicked(void *data, Evas_Object *obj, void *event_info)
571 {
572         appdata_s *ad = data;
573         if (!ad)
574                 return;
575
576         elm_naviframe_item_pop(ad->nf);
577         ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
578 }
579
580 void on_userinfo_back_button_clicked(void *data, Evas_Object *obj, void *event_info)
581 {
582         appdata_s *ad = data;
583         if (!ad)
584                 return;
585
586         app_nf_back_cb(data, obj, event_info);
587 }
588
589
590 static void on_user_info_icon_pressed(void *data, Evas_Object *obj, void *event_info)
591 {
592         if (data)
593                 evas_object_color_set(data, 45, 165, 224, 178);
594 }
595
596 static void on_user_info_icon_unpressed(void *data, Evas_Object *obj, void *event_info)
597 {
598         if (data)
599                 evas_object_color_set(data, 45, 165, 224, 255);
600 }
601
602 static void on_user_info_msg_clicked(void *data, Evas_Object *obj, void *event_info)
603 {
604         appdata_s *ad = data;
605         if (!ad)
606                 return;
607         elm_naviframe_item_pop(ad->nf);
608         ad->current_app_state = TG_CHAT_MESSAGING_VIEW_STATE;
609 }
610
611 static void on_user_info_call_clicked(void *data, Evas_Object *obj, void *event_info)
612 {
613         appdata_s *ad = data;
614         if (!ad)
615                 return;
616
617         app_control_h app_control;
618         int ret = app_control_create(&app_control);
619         if (ret != APP_CONTROL_ERROR_NONE)
620                 return;
621
622         app_control_set_operation(app_control, APP_CONTROL_OPERATION_CALL);
623         char phone_num[512] = {0,};
624         strcpy(phone_num, "tel:");
625         strcat(phone_num, ad->buddy_in_cahtting_data->use_data->phone);
626         app_control_set_uri(app_control, phone_num);
627         if (app_control_send_launch_request(app_control, NULL, NULL) == APP_CONTROL_ERROR_NONE) {
628                 // sms view launched
629         }
630         app_control_destroy(app_control);
631 }
632
633 static void compare_with_current_date(int rtime, Elm_Object_Item *nf_it)
634 {
635         char time_str[256] = {0,};
636         time_t local_t = time(NULL);
637         int diff_sec = 0;
638         int diff_min = 0;
639         int diff_hour = 0;
640         int diff_day = 0;
641
642         diff_sec = local_t - rtime;
643         diff_min = diff_sec / 60;
644         diff_hour = diff_min / 60;
645         diff_day = diff_hour / 24;
646         LOGD("different : day : %d, hour = %d, min = %d, sec = %d", diff_day, diff_hour, diff_min, diff_sec);
647
648
649         if (diff_day > 30) {
650                 snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_LONG_TIME_AGO_ABB"));
651                 goto OUT;
652         }
653
654         if (diff_day > 7) {
655                 snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_WITHIN_A_MONTH_ABB"));
656                 goto OUT;
657         }
658         if (diff_day > 1) {
659                 snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_WITHIN_A_WEEK_ABB"));
660                 goto OUT;
661         }
662
663         if (diff_hour > 1) {
664                 snprintf(time_str, sizeof(time_str) - 1, i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_PD_HOURS_AGO_ABB"), diff_hour);
665                 goto OUT;
666         }
667
668         if (diff_hour == 1) {
669                 snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_1_HOUR_AGO_ABB"));
670                 goto OUT;
671         }
672         if (diff_min > 1) {
673                 snprintf(time_str, sizeof(time_str) - 1, i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_PD_MINUTES_AGO_ABB"), diff_min);
674                 goto OUT;
675         }
676         if (diff_min == 1) {
677                 snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_1_MINUTE_AGO_ABB"));
678                 goto OUT;
679         }
680         snprintf(time_str, sizeof(time_str) - 1, "%s", i18n_get_text("IDS_TGRAM_HEADER_LAST_SEEN_RECENTLY_ABB"));
681
682         OUT:
683         elm_object_item_part_text_set(nf_it, "subtitle", time_str);
684 }
685
686 void launch_user_info_screen(appdata_s* ad, int peer_id)
687 {
688         if (!ad)
689                 return;
690
691         if (ad->msg_popup) {
692                 evas_object_del(ad->msg_popup);
693                 ad->msg_popup = NULL;
694         }
695         ad->current_app_state = TG_SET_USER_INFO_STATE;
696         char edj_path[PATH_MAX] = {0, };
697         app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
698
699         Evas_Object* scroller = elm_scroller_add(ad->nf);
700         elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
701         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
702
703         Evas_Object* layout = elm_layout_add(ad->nf);
704         elm_layout_file_set(layout, edj_path, "buddy_info_screen");
705         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
706         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
707         evas_object_show(layout);
708         elm_object_content_set(scroller, layout);
709
710         char temp_name[512] = {'\0'};
711         char* user_name = replace(ad->peer_in_cahtting_data->use_data->print_name, '_', " ");
712         snprintf(temp_name, 512, "%s", user_name);
713         free(user_name);
714
715         Elm_Object_Item* navi_item = elm_naviframe_item_push(ad->nf, temp_name, NULL, NULL, scroller, NULL);
716         elm_object_item_part_text_set(navi_item, "subtitle", "Subtitle");
717
718         /*********************** set last seen ******************************/
719
720         Eina_List *buddy_details_array = get_buddy_info(ad->peer_in_cahtting_data->use_data->peer_id);
721         if (buddy_details_array && eina_list_count(buddy_details_array) > 0) {
722                 Eina_List *buddy_details = eina_list_nth(buddy_details_array, 0);
723                 if (buddy_details && eina_list_count(buddy_details) > 0) {
724                         int* temp_online = (int *)eina_list_nth(buddy_details, 12);
725                         int is_online = *temp_online;
726                         int* temp_last_seen = (int *)eina_list_nth(buddy_details, 13);
727                         int last_seen = *temp_last_seen;
728
729                         if (is_online > 0) {
730                                 elm_object_item_part_text_set(navi_item, "subtitle", i18n_get_text("IDS_TGRAM_SBODY_ONLINE"));
731                         } else {
732                                 compare_with_current_date(last_seen, navi_item);
733                         }
734
735                         void* val = NULL;
736                         EINA_LIST_FREE(buddy_details,val) {
737                                 if (val)
738                                         free(val);
739                         }
740                         eina_list_free(buddy_details);
741                 }
742                 eina_list_free(buddy_details_array);
743         }
744
745         Evas_Object *profile_pic = NULL;
746         Evas_Object *image_layout = NULL;
747
748         if (ad->peer_in_cahtting_data->use_data->photo_path && strlen(ad->peer_in_cahtting_data->use_data->photo_path) > 0 && strstr(ad->peer_in_cahtting_data->use_data->photo_path, "_null_") == NULL) {
749                 image_layout = elm_layout_add(ad->nf);
750                 elm_layout_file_set(image_layout, edj_path, "contact_image_masking");
751                 evas_object_size_hint_weight_set(image_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
752                 evas_object_size_hint_align_set(image_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
753                 evas_object_show(image_layout);
754                 profile_pic = get_image_from_path(ad->peer_in_cahtting_data->use_data->photo_path, ad->nf);
755                 elm_object_part_content_set(image_layout, "image", profile_pic);
756         } else  {
757                 image_layout = elm_layout_add(ad->nf);
758                 elm_layout_file_set(image_layout, edj_path, "contact_image_masking");
759                 evas_object_size_hint_weight_set(image_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
760                 evas_object_size_hint_align_set(image_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
761                 evas_object_show(image_layout);
762                 profile_pic = get_image_from_path(ui_utils_get_resource(TG_CALLER_ID_IMAGE), ad->nf);
763                 evas_object_color_set(profile_pic, 45, 165, 224, 225);
764                 elm_object_part_content_set(image_layout, "image", profile_pic);
765         }
766
767         elm_object_part_content_set(layout, "swallow.profile_pic", image_layout);
768
769         Evas_Object *info_layout = elm_layout_add(ad->nf);
770         elm_layout_file_set(info_layout, edj_path, "buddy_num_layout");
771         evas_object_size_hint_weight_set(info_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
772         evas_object_size_hint_align_set(info_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
773         evas_object_show(info_layout);
774         elm_object_part_content_set(layout, "swallow.profile_info", info_layout);
775
776         Evas_Object *phone_type = elm_label_add(ad->nf);
777         elm_object_style_set(phone_type, "transparent");
778         char phone_type_str[256];
779         sprintf(phone_type_str, "<font=Tizen:style=Normal color=#A9A9A9 align=left><font_size=35>&nbsp;&nbsp;%s</font_size></font>", i18n_get_text("IDS_TGRAM_BODY_MOBILE_ABB"));
780         elm_object_text_set(phone_type, phone_type_str);
781         evas_object_size_hint_weight_set(phone_type, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
782         evas_object_size_hint_align_set(phone_type, EVAS_HINT_FILL, EVAS_HINT_FILL);
783         evas_object_show(phone_type);
784
785         elm_object_part_content_set(info_layout, "swallow.phone_type", phone_type);
786
787         if (ad->buddy_in_cahtting_data) {
788                 Evas_Object* msg_btn = elm_button_add(ad->nf);
789                 elm_object_style_set(msg_btn, "transparent");
790                 evas_object_size_hint_align_set(msg_btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
791                 evas_object_size_hint_weight_set(msg_btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
792
793                 Evas_Object* msg_icon = elm_image_add(ad->nf);
794                 evas_object_size_hint_align_set(msg_icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
795                 evas_object_size_hint_weight_set(msg_icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
796                 elm_image_file_set(msg_icon, ui_utils_get_resource(TG_USER_INFO_MESSAGE), NULL);
797                 evas_object_show(msg_icon);
798                 evas_object_color_set(msg_icon, 45, 165, 224, 255);
799                 elm_object_content_set(msg_btn, msg_icon);
800
801                 evas_object_smart_callback_add(msg_btn, "clicked", on_user_info_msg_clicked, ad);
802                 evas_object_smart_callback_add(msg_btn, "pressed", on_user_info_icon_pressed, msg_icon);
803                 evas_object_smart_callback_add(msg_btn, "unpressed", on_user_info_icon_unpressed, msg_icon);
804                 elm_object_part_content_set(info_layout, "swallow.phone_msg", msg_btn);
805
806                 Evas_Object* call_btn = elm_button_add(ad->nf);
807                 elm_object_style_set(call_btn, "transparent");
808                 evas_object_size_hint_align_set(call_btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
809                 evas_object_size_hint_weight_set(call_btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
810
811                 Evas_Object* call_icon = elm_image_add(ad->nf);
812                 evas_object_size_hint_align_set(call_icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
813                 evas_object_size_hint_weight_set(call_icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
814                 elm_image_file_set(call_icon, ui_utils_get_resource(TG_USER_INFO_CALL), NULL);
815                 evas_object_show(call_icon);
816                 evas_object_color_set(call_icon, 45, 165, 224, 255);
817                 elm_object_content_set(call_btn, call_icon);
818
819                 evas_object_smart_callback_add(call_btn, "clicked", on_user_info_call_clicked, ad);
820                 evas_object_smart_callback_add(call_btn, "pressed", on_user_info_icon_pressed, call_icon);
821                 evas_object_smart_callback_add(call_btn, "unpressed", on_user_info_icon_unpressed, call_icon);
822                 elm_object_part_content_set(info_layout, "swallow.phone_call", call_btn);
823
824                 Evas_Object *phone_num = elm_label_add(ad->nf);
825                 elm_object_style_set(phone_num, "transparent");
826                 char phone_num_str[256];
827                 sprintf(phone_num_str, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>&nbsp;&nbsp;%s</font_size></font>", ad->buddy_in_cahtting_data->use_data->phone);
828                 elm_object_text_set(phone_num, phone_num_str);
829                 evas_object_size_hint_weight_set(phone_num, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
830                 evas_object_size_hint_align_set(phone_num, EVAS_HINT_FILL, EVAS_HINT_FILL);
831                 evas_object_show(phone_num);
832                 elm_object_part_content_set(info_layout, "swallow.phone_number", phone_num);
833         }
834         Evas_Object *back_btn = create_button(ad->nf, "naviframe/back_btn/default", NULL);
835         evas_object_smart_callback_add(back_btn, "clicked", on_userinfo_back_button_clicked, ad);
836         elm_object_item_part_content_set(navi_item,  "elm.swallow.prev_btn", back_btn);
837
838         eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_MORE, on_user_info_menu_button_clicked, ad);
839 }
840