add telegram account, fix db insert error.
[apps/native/telegram-tizen.git] / TelegramTizen / src / tg_settings_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
20 #include "tg_settings_view.h"
21 #include "server_requests.h"
22 #include "tg_set_username_view.h"
23 #include "tg_edit_name_view.h"
24 #include "tg_db_wrapper.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         Evas_Object *media_image = elm_image_add(parent);
31         elm_image_aspect_fixed_set(media_image, EINA_FALSE);
32         elm_image_file_set(media_image, path, NULL);
33         evas_object_size_hint_weight_set(media_image, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
34         evas_object_size_hint_align_set(media_image, EVAS_HINT_FILL, EVAS_HINT_FILL);
35         evas_object_show(media_image);
36         return media_image;
37 }
38
39 static void on_chat_bg_clicked(void *data, Evas_Object *obj, void *event_info)
40 {
41         appdata_s *ad = data;
42         if (ad && ad->chat_background) {
43 #if 0
44                 if (strstr(ad->chat_background, ui_utils_get_resource(TG_CHAT_DEFAULT_BG)) != NULL) {
45                         show_toast(ad, "Default background");
46                 } else {
47                         launch_app_control(ad, "image", ad->chat_background);
48                 }
49 #endif
50         }
51 }
52
53 Evas_Object* on_settings_info_requested(void *data, Evas_Object *obj, const char *part)
54 {
55         Evas_Object *eo = NULL;
56         int id = (int) data;
57         appdata_s* ad = evas_object_data_get(obj, "app_data");
58         if (!ad)
59                 return NULL;
60         if (!strcmp(part, "elm.swallow.icon.0")) {
61                 char edj_path[PATH_MAX] = {0, };
62                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
63                 Evas_Object* user_status_layout = elm_layout_add(ad->nf);
64                 elm_layout_file_set(user_status_layout, edj_path, "main_list_name_status_item");
65                 evas_object_size_hint_weight_set(user_status_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
66                 evas_object_size_hint_align_set(user_status_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
67                 evas_object_show(user_status_layout);
68
69                 char* first_text = NULL;
70                 char buf[512] = {'\0'};
71                 if (id == 0) {
72                         first_text = "Chat Background";
73                 } else {
74                         first_text = "";
75                 }
76                 snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", first_text);
77                 Evas_Object*  name_lbl = elm_label_add(ad->nf);
78                 elm_object_text_set(name_lbl, buf);
79                 evas_object_size_hint_weight_set(name_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
80                 evas_object_size_hint_align_set(name_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
81                 evas_object_show(name_lbl);
82                 elm_object_part_content_set(user_status_layout, "swallow.name", name_lbl);
83                 return user_status_layout;
84         } else if (!strcmp(part, "elm.swallow.end")) {
85                 if (!ad->chat_background) {
86                         return eo;
87                 }
88                 char *doc_thumb = NULL;
89                 Evas_Object *chat_bg = NULL;
90
91                 get_thumbnail_from_video_url(ad->chat_background, &doc_thumb);
92                 if (doc_thumb) {
93                         chat_bg = get_image_from_path(doc_thumb, ad->nf);
94                 } else {
95                         chat_bg = get_image_from_path(ad->chat_background, ad->nf);
96                 }
97
98                 evas_object_data_set(ad->nf, "settings_chat_bg", chat_bg);
99                 eo = elm_layout_add(obj);
100                 elm_layout_theme_set(eo, "layout", "list/C/type.1", "default");
101                 elm_layout_content_set(eo, "elm.swallow.content", chat_bg);
102                 evas_object_smart_callback_add(chat_bg, "clicked", on_chat_bg_clicked, ad);
103         }
104         return eo;
105 }
106
107
108 Evas_Object* on_user_phone_info_requested(void *data, Evas_Object *obj, const char *part)
109 {
110         Evas_Object *eo = NULL;
111         int id = (int) data;
112         appdata_s* ad = evas_object_data_get(obj, "app_data");
113         if (strcmp(part, "elm.swallow.icon.0")) {
114                 return eo;
115         }
116         char edj_path[PATH_MAX] = {0, };
117         app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
118         Evas_Object* user_status_layout = elm_layout_add(ad->nf);
119         elm_layout_file_set(user_status_layout, edj_path, "main_list_name_status_item");
120         evas_object_size_hint_weight_set(user_status_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
121         evas_object_size_hint_align_set(user_status_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
122         evas_object_show(user_status_layout);
123
124         char* first_text = NULL;
125         char buf[512] = {'\0'};
126         if (id == 0) {
127                 if (ad->current_user_data->phone) {
128                         first_text = ad->current_user_data->phone;
129                 } else {
130                         first_text = "";
131                 }
132         } else {
133                 if (ad->current_user_data->username && strlen(ad->current_user_data->username) > 0 && strstr(ad->current_user_data->username, "_null_") == NULL) {
134                         first_text = ad->current_user_data->username;
135                 } else {
136                         first_text = "None";
137                 }
138         }
139
140         snprintf(buf, 512, "<font=Tizen:style=Bold color=#000000 align=left><font_size=40>%s</font_size></font>", first_text);
141         Evas_Object*  name_lbl = elm_label_add(ad->nf);
142         elm_object_text_set(name_lbl, buf);
143         evas_object_size_hint_weight_set(name_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
144         evas_object_size_hint_align_set(name_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
145         evas_object_show(name_lbl);
146
147         if (id == 1) {
148                 evas_object_data_set(ad->nf, "settings_user_name_label", name_lbl);
149         }
150         elm_object_part_content_set(user_status_layout, "swallow.name", name_lbl);
151
152         char* second_text = NULL;
153         if (id == 0) {
154                 second_text = "Phone";
155         } else {
156                 second_text = "Username";
157         }
158         char status_buf[126] = {'\0'};
159         sprintf(status_buf, "<font=Tizen:style=Normal color=#808080 align=left><font_size=35>%s</font_size></font>", second_text);
160
161         Evas_Object*  status_lbl = elm_label_add(ad->nf);
162         elm_object_text_set(status_lbl, status_buf);
163         evas_object_size_hint_weight_set(status_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
164         evas_object_size_hint_align_set(status_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
165         evas_object_show(status_lbl);
166
167         elm_object_part_content_set(user_status_layout, "swallow.status", status_lbl);
168         return user_status_layout;
169 }
170
171 Evas_Object* on_user_info_requested(void *data, Evas_Object *obj, const char *part)
172 {
173         Evas_Object *eo = NULL;
174         appdata_s* ad = evas_object_data_get(obj, "app_data");
175
176         if (!ad)
177                 return NULL;
178
179         if (!strcmp(part, "elm.swallow.icon")) {
180                 Evas_Object *profile_pic = NULL;
181                 if (ad->current_user_data->photo_path && strlen(ad->current_user_data->photo_path) > 0 && strstr(ad->current_user_data->photo_path, "_null_") == NULL) {
182                         profile_pic = get_image_from_path(ad->current_user_data->photo_path, ad->nf);
183                 } else  {
184                         profile_pic = get_image_from_path(ui_utils_get_resource(DEFAULT_PROFILE_PIC), ad->nf);
185                 }
186                 char edj_path[PATH_MAX] = {0, };
187                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
188                 Evas_Object* user_pic_layout = elm_layout_add(ad->nf);
189                 elm_layout_file_set(user_pic_layout, edj_path, "circle_layout");
190                 evas_object_size_hint_weight_set(user_pic_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
191                 evas_object_size_hint_align_set(user_pic_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
192                 evas_object_show(user_pic_layout);
193                 elm_object_part_content_set(user_pic_layout, "content", profile_pic);
194                 evas_object_data_set(ad->nf, "user_profile_pic", profile_pic);
195                 eo = elm_layout_add(obj);
196                 elm_layout_theme_set(eo, "layout", "list/C/type.1", "default");
197                 elm_layout_content_set(eo, "elm.swallow.content", user_pic_layout);
198         } else if (!strcmp(part, "elm.swallow.icon.0")) {
199                 char edj_path[PATH_MAX] = {0, };
200                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
201                 Evas_Object* user_status_layout = elm_layout_add(ad->nf);
202                 elm_layout_file_set(user_status_layout, edj_path, "main_list_name_status_item");
203                 evas_object_size_hint_weight_set(user_status_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
204                 evas_object_size_hint_align_set(user_status_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
205                 evas_object_show(user_status_layout);
206
207                 char* user_name = replace(ad->current_user_data->print_name, '_', " ");
208                 char buf[512] = {'\0'};
209                 snprintf(buf, 512, "<font=Tizen:style=Bold color=#FFFFFF align=left><font_size=40>%s</font_size></font>", user_name);
210                 free(user_name);
211
212                 Evas_Object*  name_lbl = elm_label_add(ad->nf);
213                 elm_object_text_set(name_lbl, buf);
214                 evas_object_size_hint_weight_set(name_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
215                 evas_object_size_hint_align_set(name_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
216                 evas_object_show(name_lbl);
217
218                 elm_object_part_content_set(user_status_layout, "swallow.name", name_lbl);
219
220                 char* user_status = i18n_get_text("IDS_TGRAM_SBODY_ONLINE");
221                 char status_buf[126] = {'\0'};
222                 sprintf(status_buf, "<font=Tizen:style=Normal color=#FFFFFF align=left><font_size=35>%s</font_size></font>", user_status);
223
224                 Evas_Object*  status_lbl = elm_label_add(ad->nf);
225                 elm_object_text_set(status_lbl, status_buf);
226                 evas_object_size_hint_weight_set(status_lbl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
227                 evas_object_size_hint_align_set(status_lbl, EVAS_HINT_FILL, EVAS_HINT_FILL);
228                 evas_object_show(status_lbl);
229
230                 elm_object_part_content_set(user_status_layout, "swallow.status", status_lbl);
231                 return user_status_layout;
232         } else {
233
234         }
235         return eo;
236 }
237
238 void on_user_info_item_clicked(void *data, Evas_Object *obj, void *event_info)
239 {
240         Elm_Object_Item *it = event_info;
241         elm_genlist_item_selected_set(it, EINA_FALSE);
242 }
243
244 void on_phone_info_item_clicked(void *data, Evas_Object *obj, void *event_info)
245 {
246         Elm_Object_Item *it = event_info;
247         elm_genlist_item_selected_set(it, EINA_FALSE);
248         int id = (int)data;
249         appdata_s* ad = evas_object_data_get(obj, "app_data");
250         if (id == 1) {
251                 launch_set_username_cb(ad);
252         }
253 }
254
255 void on_image_select_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
256 {
257         if (result != APP_CONTROL_RESULT_SUCCEEDED || !user_data) {
258                 return;
259         }
260         char** path_arryay = NULL;
261         int array_length = 0;
262         appdata_s *ad = (appdata_s*)user_data;
263         app_control_get_extra_data_array(reply, APP_CONTROL_DATA_SELECTED, &path_arryay,  &array_length);
264         for (int i = 0 ; i < array_length ; i++) {
265                 if (path_arryay[i]) {
266                         show_loading_popup(ad);
267                         send_set_profile_pic_request(ad, ad->service_client, ad->current_user_data->user_id.id, path_arryay[i]);
268                         break;
269                 }
270         }
271 }
272
273 void on_image_request_option_selected_cb(void *data, Evas_Object *obj, void *event_info)
274 {
275         Evas_Object *popup = data;
276         int id = 0;
277         Elm_Object_Item *item = event_info;
278         app_control_h app_control;
279
280         id = (int)elm_object_item_data_get(item);
281         int ret = app_control_create(&app_control);
282         appdata_s *ad = evas_object_data_get(popup, "app_data");
283
284         if (!ad)
285                 return;
286
287         if (ret != APP_CONTROL_ERROR_NONE) {
288                 show_toast(ad, "Error: Can not load image picker");
289                 return;
290         }
291
292         if (id == 1) {
293                 app_control_set_operation(app_control, APP_CONTROL_OPERATION_CREATE_CONTENT);
294                 app_control_set_mime(app_control, "image/jpg");
295                 app_control_send_launch_request(app_control, &on_image_select_result_cb, ad);
296         } else {
297                 app_control_set_operation(app_control, APP_CONTROL_OPERATION_PICK);
298                 app_control_set_mime(app_control, "image/*");
299                 app_control_send_launch_request(app_control, &on_image_select_result_cb, ad);
300         }
301         app_control_destroy(app_control);
302         evas_object_del(popup);
303 }
304
305 char* on_camera_load_text_get_cb(void *data, Evas_Object *obj, const char *part)
306 {
307         int id = (int) data;
308         if (id == 0) {
309                 return strdup(_("IDS_TGRAM_OPT_FROM_GALLERY_ABB2"));
310         } else {
311                 return strdup(_("IDS_TGRAM_OPT_TAKE_PICTURE_ABB"));
312         }
313 }
314
315 void on_camera_button_clicked(void *data, Evas_Object *obj, void *event_info)
316 {
317         appdata_s* ad = data;
318         if (!ad)
319                 return;
320
321         static Elm_Genlist_Item_Class itc;
322         Evas_Object *popup;
323         Evas_Object *genlist;
324         int i;
325
326         Evas_Object *win = ad->win;
327         Elm_Object_Item *it = event_info;
328         elm_genlist_item_selected_set(it, EINA_FALSE);
329
330         popup = elm_popup_add(win);
331         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
332         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
333         elm_object_part_text_set(popup, "title,text", i18n_get_text("IDS_TGRAM_HEADER_SELECT_IMAGE_ABB2"));
334         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
335         evas_object_data_set(popup, "app_data", ad);
336         /* genlist */
337         genlist = elm_genlist_add(popup);
338         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
339         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
340
341         itc.item_style = "default";
342         itc.func.text_get = on_camera_load_text_get_cb;
343         itc.func.content_get = NULL;
344         itc.func.state_get = NULL;
345         itc.func.del = NULL;
346
347         for (i = 0; i < 2; i++) {
348                 elm_genlist_item_append(genlist, &itc, (void *) i, NULL, ELM_GENLIST_ITEM_NONE, on_image_request_option_selected_cb, popup);
349         }
350         evas_object_show(genlist);
351         elm_object_content_set(popup, genlist);
352         evas_object_show(popup);
353 }
354
355
356 /**************************************************************************/
357
358 void on_chat_bg_select_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
359 {
360         if (result != APP_CONTROL_RESULT_SUCCEEDED || !user_data) {
361                 return;
362         }
363         char* file_path = NULL;
364         char** path_arryay = NULL;
365         int array_length = 0;
366         char *chat_bg = NULL;
367         appdata_s *ad = user_data;
368         app_control_get_extra_data_array(reply, APP_CONTROL_DATA_SELECTED, &path_arryay,  &array_length);
369
370         for (int i = 0 ; i < array_length ; i++) {
371                 file_path = path_arryay[i];
372                 chat_bg = NULL;
373                 if (file_path) {
374                         preference_set_string(TG_CHAT_BG_PREFERENCE, file_path);
375                         preference_get_string(TG_CHAT_BG_PREFERENCE, &chat_bg);
376
377                         if (ad->chat_background) {
378                                 free(ad->chat_background);
379                                 ad->chat_background = NULL;
380                         }
381
382                         if (chat_bg)
383                                 ad->chat_background = strdup(chat_bg);
384
385                         if (!ad->chat_background) {
386                                 ad->chat_background = strdup(ui_utils_get_resource(TG_CHAT_DEFAULT_BG));
387                                 preference_set_string(TG_CHAT_BG_PREFERENCE, ad->chat_background);
388                         }
389                         break;
390                 }
391         }
392 }
393
394 void on_chat_bg_change_option_selected_cb(void *data, Evas_Object *obj, void *event_info)
395 {
396         Evas_Object *popup = data;
397         int id = 0;
398         app_control_h app_control;
399
400         Elm_Object_Item *item = event_info;
401         id = (int)elm_object_item_data_get(item);
402         int ret = app_control_create(&app_control);
403
404         appdata_s *ad = evas_object_data_get(popup, "app_data");
405
406         if (ret != APP_CONTROL_ERROR_NONE) {
407                 show_toast(ad, "Error: Can not load image picker");
408                 return;
409         }
410
411         if (id == 1) {
412                 app_control_set_operation(app_control, APP_CONTROL_OPERATION_CREATE_CONTENT);
413                 app_control_set_mime(app_control, "image/jpg");
414                 app_control_send_launch_request(app_control, &on_chat_bg_select_result_cb, ad);
415         } else {
416                 app_control_set_operation(app_control, APP_CONTROL_OPERATION_PICK);
417                 app_control_set_mime(app_control, "image/*");
418                 app_control_send_launch_request(app_control, &on_chat_bg_select_result_cb, ad);
419         }
420
421         app_control_destroy(app_control);
422         evas_object_del(popup);
423 }
424
425 char* on_chat_bg_load_text_get_cb(void *data, Evas_Object *obj, const char *part)
426 {
427         int id = (int) data;
428         if (id == 0) {
429                 return strdup(_("IDS_TGRAM_OPT_FROM_GALLERY_ABB2"));
430         } else {
431                 return strdup(_("IDS_TGRAM_OPT_TAKE_PICTURE_ABB"));
432         }
433 }
434
435 void on_settings_info_item_clicked(void *data, Evas_Object *obj, void *event_info)
436 {
437         Elm_Object_Item *it = event_info;
438         elm_genlist_item_selected_set(it, EINA_FALSE);
439
440         /*Evas_Object* cam_icon =  data;*/
441         appdata_s* ad = evas_object_data_get(obj, "app_data");
442         static Elm_Genlist_Item_Class itc;
443         Evas_Object *popup;
444         Evas_Object *box;
445         Evas_Object *genlist;
446         int i;
447         Evas_Object *win = ad->win;
448
449         popup = elm_popup_add(win);
450         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
451         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
452         elm_object_part_text_set(popup, "title,text", i18n_get_text("IDS_TGRAM_HEADER_SELECT_IMAGE_ABB2"));
453         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
454
455         evas_object_data_set(popup, "app_data", ad);
456         /* box */
457         box = elm_box_add(popup);
458         elm_box_horizontal_set(box, EINA_FALSE);
459         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
460         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
461
462         /* genlist */
463         genlist = elm_genlist_add(box);
464         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
465         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
466         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
467         elm_scroller_content_min_limit(genlist, EINA_TRUE, EINA_TRUE);
468
469         itc.item_style = "default";
470         itc.func.text_get = on_chat_bg_load_text_get_cb;
471         itc.func.content_get = NULL;
472         itc.func.state_get = NULL;
473         itc.func.del = NULL;
474
475         for (i = 0; i < 2; i++) {
476                 elm_genlist_item_append(genlist, &itc, (void *) i, NULL, ELM_GENLIST_ITEM_NONE, on_chat_bg_change_option_selected_cb, popup);
477         }
478         evas_object_show(genlist);
479         elm_box_pack_end(box, genlist);
480         elm_object_content_set(popup, box);
481         evas_object_show(popup);
482 }
483
484 char* _text_requested_cb(void *data, Evas_Object *obj, const char *part)
485 {
486         int id = (int) data;
487         appdata_s* ad = evas_object_data_get(obj, "app_data");
488         char *user_name = NULL;
489         char *first_name = NULL;
490         char *last_name = NULL;
491         if (!strcmp(part, "elm.text.main.left.top") || !strcmp(part, "elm.text")) {
492                 switch (id) {
493                         case 0:
494                                 //return replace(ad->current_user_data->print_name, '_', " ");
495                                 first_name = ad->current_user_data->first_name;
496                                 last_name = ad->current_user_data->last_name;
497
498
499                                 if (!first_name || (first_name && strstr(first_name ,"null") != 0)) {
500                                         first_name = NULL;
501                                 }
502
503                                 if (!first_name && !last_name) {
504                                         first_name = "";
505                                 }
506
507                                 if (!last_name || (last_name && strstr(last_name ,"null") != 0)) {
508                                         last_name = "";
509                                 }
510                                 int str_len = strlen(first_name) + strlen(" ") + strlen(last_name) + 1;
511                                 user_name = (char *)malloc(str_len);
512                                 snprintf(user_name, sizeof(user_name), "%s %s", first_name, last_name);
513                                 return user_name;
514                         case 1:
515                                 return strdup(i18n_get_text("IDS_TGRAM_OPT_SET_BACKGROUND_IMAGE_ABB"));
516                         default:
517                                 break;
518                 }
519         } else if (!strcmp(part, "elm.text.sub.left.bottom") || !strcmp(part, "elm.text.sub")) {
520                 switch (id) {
521                         case 0:
522                         if (ad->current_user_data->online) {
523                                 return strdup(i18n_get_text("IDS_TGRAM_SBODY_ONLINE"));
524                         } else {
525                                 return strdup("offine");
526                         }
527                         break;
528                 }
529         }
530         return NULL;
531 }
532
533 Evas_Object* _content_requested_cb(void *data, Evas_Object *obj, const char *part)
534 {
535         Evas_Object *eo = NULL;
536         if (!strcmp(part, "elm.icon.left") || !strcmp(part, "elm.icon.1") || !strcmp(part, "elm.swallow.icon")) {
537                 appdata_s* ad = evas_object_data_get(obj, "app_data");
538
539                 int id = (int) data;
540                 if (id != 0)
541                         return eo;
542
543                 Evas_Object *profile_pic = NULL;
544
545                 if (ad->current_user_data->photo_path && strlen(ad->current_user_data->photo_path) > 0 && strstr(ad->current_user_data->photo_path, "_null_") == NULL) {
546                         profile_pic = get_image_from_path(ad->current_user_data->photo_path, ad->nf);
547                 } else  {
548                         profile_pic = get_image_from_path(ui_utils_get_resource(DEFAULT_LIST_THUMB_SINGLE_PIC), ad->nf);
549                         evas_object_color_set(profile_pic, 45, 165, 224, 255);
550                 }
551
552                 char edj_path[PATH_MAX] = {0, };
553                 app_get_resource(TELEGRAM_INIT_VIEW_EDJ, edj_path, (int)PATH_MAX);
554                 Evas_Object* user_pic_layout = elm_layout_add(ad->nf);
555                 elm_layout_file_set(user_pic_layout, edj_path, "search_circle_layout");
556                 evas_object_size_hint_weight_set(user_pic_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
557                 evas_object_size_hint_align_set(user_pic_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
558                 evas_object_show(user_pic_layout);
559                 elm_object_part_content_set(user_pic_layout, "content", profile_pic);
560                 evas_object_data_set(ad->nf, "settings_user_profile_pic", profile_pic);
561
562                 eo = elm_layout_add(obj);
563                 elm_layout_theme_set(eo, "layout", "list/B/type.1", "default");
564                 elm_layout_content_set(eo, "elm.swallow.content", user_pic_layout);
565         }
566         return eo;
567 }
568
569 static void _cancel_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
570 {
571         Evas_Object *popup = data;
572         evas_object_del(popup);
573 }
574
575 static void _logout_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
576 {
577         appdata_s *ad = data;
578         Evas_Object *popup = NULL;
579
580         popup = evas_object_data_del(obj, "popup");
581         if (popup)
582                 evas_object_del(popup);
583
584         send_request_for_logout(ad, ad->service_client);
585         show_loading_popup(ad);
586 }
587
588 static void _create_logout_popup(appdata_s *ad)
589 {
590         Evas_Object *popup;
591         Evas_Object *logout_btn;
592         Evas_Object *btn;
593
594         /* popup */
595         popup = elm_popup_add(ad->win);
596         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
597         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
598         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
599         elm_object_translatable_text_set(popup, "IDS_TGRAM_POP_USE_TELEGRAM_SEAMLESSLY_ACROSS_ANY_NUMBER_OF_DEVICES_MSG");
600         elm_object_part_text_set(popup, "title,text", i18n_get_text("IDS_TGRAM_OPT_LOG_OUT"));
601
602         /* logout button */
603         logout_btn = elm_button_add(popup);
604         elm_object_style_set(logout_btn, "popup");
605         elm_object_translatable_text_set(logout_btn, "IDS_TGRAM_BUTTON_LOG_OUT_ABB2");
606         elm_object_part_content_set(popup, "button2", logout_btn);
607         evas_object_smart_callback_add(logout_btn, "clicked", _logout_btn_clicked_cb, ad);
608         evas_object_data_set(logout_btn, "popup", popup);
609
610         /* cancel button */
611         btn = elm_button_add(popup);
612         elm_object_style_set(btn, "popup");
613         elm_object_translatable_text_set(btn, "IDS_TGRAM_BUTTON_CANCEL_ABB5");
614         elm_object_part_content_set(popup, "button1", btn);
615         evas_object_smart_callback_add(btn, "clicked", _cancel_btn_clicked_cb, popup);
616
617         evas_object_show(popup);
618 }
619
620 static void _more_popup_rotate(void *data, Evas_Object *obj, void *event_info)
621 {
622         int pos;
623         Evas_Coord w, h;
624         appdata_s *ad = data;
625
626         elm_win_screen_size_get(ad->win, NULL, NULL, &w, &h);
627         pos = elm_win_rotation_get(ad->win);
628
629         switch (pos) {
630         case 90:
631         case 270:
632                 evas_object_move(ad->menu_popup, 0, w);
633                 break;
634         case 180:
635         default:
636                 evas_object_move(ad->menu_popup, 0, h);
637                 break;
638     }
639 }
640
641 static void _ctxpopup_dismiss_cb(void *data, Evas_Object *obj, void *event_info)
642 {
643         appdata_s *ad = data;
644         evas_object_del(ad->menu_popup);
645         ad->menu_popup = NULL;
646 }
647
648 static void _ctxpopup_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
649 {
650         appdata_s *ad = data;
651         if (!ad || !ad->win)
652                 return;
653
654         evas_object_smart_callback_del(ad->win, "rotation,changed", _more_popup_rotate);
655         if (ad->menu_popup) {
656                 evas_object_smart_callback_del(ad->menu_popup, "dismissed", _ctxpopup_dismiss_cb);
657                 evas_object_del(ad->menu_popup);
658                 ad->menu_popup = NULL;
659         }
660 }
661
662 static void ctxpopup_profile_select_cb(void *data, Evas_Object *obj, void *event_info)
663 {
664         appdata_s *ad = data;
665         if (!ad)
666                 return;
667
668         _ctxpopup_dismiss_cb(ad, NULL, NULL);
669         on_camera_button_clicked(ad, obj, event_info);
670 }
671 /*
672 static void ctxpopup_edit_name_select_cb(void *data, Evas_Object *obj, void *event_info)
673 {
674         appdata_s *ad = data;
675         if (!ad)
676                 return;
677
678         _ctxpopup_dismiss_cb(ad, NULL, NULL);
679         launch_editname_screen(ad);
680 }
681 */
682 static void ctxpopup_logout_select_cb(void *data, Evas_Object *obj, void *event_info)
683 {
684         appdata_s *ad = data;
685         if (!ad)
686                 return;
687
688         _ctxpopup_dismiss_cb(ad, NULL, NULL);
689         _create_logout_popup(ad);
690 }
691
692 static void _create_more_popup(void *data, Evas_Object *obj, void *event_info)
693 {
694         Elm_Object_Item *it;
695         Evas_Object *ctxpopup;
696         appdata_s *ad = data;
697
698         if (!ad || ad->loading_popup || ad->current_app_state != TG_SETTINGS_SCREEN_STATE)
699                 return;
700
701         ctxpopup = elm_ctxpopup_add(ad->nf);
702         elm_object_style_set(ctxpopup, "more/default");
703         elm_ctxpopup_auto_hide_disabled_set(ctxpopup, EINA_TRUE);
704         eext_object_event_callback_add(ctxpopup, EEXT_CALLBACK_BACK, eext_ctxpopup_back_cb, NULL);
705         eext_object_event_callback_add(ctxpopup, EEXT_CALLBACK_MORE, eext_ctxpopup_back_cb, NULL);
706         evas_object_event_callback_add(ctxpopup, EVAS_CALLBACK_DEL, _ctxpopup_del_cb, ad);
707
708         evas_object_smart_callback_add(ctxpopup, "dismissed", _ctxpopup_dismiss_cb, ad);
709         evas_object_smart_callback_add(ad->win, "rotation,changed", _more_popup_rotate, ad);
710
711         it = elm_ctxpopup_item_append(ctxpopup, i18n_get_text("IDS_TGRAM_OPT_SET_PROFILE_PICTURE_ABB"), NULL, ctxpopup_profile_select_cb, ad);
712         //it = elm_ctxpopup_item_append(ctxpopup, i18n_get_text("IDS_TGRAM_OPT_EDIT_NAME"), NULL, ctxpopup_edit_name_select_cb, ad);
713         it = elm_ctxpopup_item_append(ctxpopup, i18n_get_text("IDS_TGRAM_OPT_LOG_OUT"), NULL, ctxpopup_logout_select_cb, ad);
714         //elm_object_item_domain_text_translatable_set(it, SETTING_PACKAGE, EINA_TRUE);
715         elm_ctxpopup_direction_priority_set(ctxpopup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UNKNOWN, ELM_CTXPOPUP_DIRECTION_UNKNOWN, ELM_CTXPOPUP_DIRECTION_UNKNOWN);
716
717         if (ad->menu_popup)
718                 evas_object_del(ad->menu_popup);
719
720         ad->menu_popup = ctxpopup;
721         _more_popup_rotate(ad, NULL, NULL);
722         evas_object_show(ctxpopup);
723 }
724
725 static Eina_Bool _pop_cb(void *data, Elm_Object_Item *it)
726 {
727         appdata_s* ad = data;
728         if (!ad)
729                 return EINA_FALSE;
730
731         eext_object_event_callback_del(ad->nf, EEXT_CALLBACK_MORE, _create_more_popup);
732         return EINA_TRUE;
733 }
734
735 void refresh_settings_screen(appdata_s* ad)
736 {
737         if (!ad)
738                 return;
739         Evas_Object *list = evas_object_data_get(ad->nf, "settings_list");
740         if (list)
741                 elm_genlist_realized_items_update(list);
742 }
743
744 static void on_message_back_button_clicked(void *data, Evas_Object *obj, void *event_info)
745 {
746         appdata_s *ad = data;
747         if (!ad)
748                 return;
749
750         app_nf_back_cb(data, obj, event_info);
751 }
752
753 void launch_settings_screen(appdata_s* ad)
754 {
755         if (!ad)
756                 return;
757
758         ad->current_app_state = TG_SETTINGS_SCREEN_STATE;
759
760         if (!ad->current_user_data) {
761                 load_registered_user_data(ad);
762         }
763
764         static Elm_Genlist_Item_Class itc;
765         Evas_Object *list = elm_genlist_add(ad->nf);
766         elm_list_mode_set(list, ELM_LIST_COMPRESS);
767         elm_genlist_mode_set(list, ELM_LIST_COMPRESS);
768         evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
769         evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
770         evas_object_show(list);
771
772         evas_object_data_set(list, "app_data", ad);
773
774         evas_object_data_set(ad->nf, "settings_list", list);
775
776         itc.item_style = "type1";
777         itc.func.text_get = _text_requested_cb;
778         itc.func.content_get = _content_requested_cb;
779         itc.func.state_get = NULL;
780         itc.func.del = NULL;
781
782         elm_genlist_item_append(list, &itc, (void*) 0, NULL, ELM_GENLIST_ITEM_NONE, on_camera_button_clicked, (void*) ad);
783         elm_genlist_item_append(list, &itc, (void*) 1, NULL, ELM_GENLIST_ITEM_NONE, on_settings_info_item_clicked, (void*)1);
784
785         Elm_Object_Item* navi_item = elm_naviframe_item_push(ad->nf, i18n_get_text("IDS_TGRAM_OPT_SETTINGS"), NULL, NULL, list, NULL);
786
787         Evas_Object *back_btn = create_button(ad->nf, "naviframe/back_btn/default", NULL);
788         evas_object_smart_callback_add(back_btn, "clicked", on_message_back_button_clicked, ad);
789         elm_object_item_part_content_set(navi_item, "elm.swallow.prev_btn", back_btn);
790
791         elm_naviframe_item_pop_cb_set(navi_item, _pop_cb, ad);
792         eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_MORE, _create_more_popup, ad);
793 }