apply FSL(Flora Software License)
[apps/core/preloaded/settings.git] / setting-display / src / setting-display-wallpaper.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.tizenopensource.org/license
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17
18 #include <setting-display-wallpaper.h>
19
20 static int setting_display_wallpaper_create(void *cb);
21 static int setting_display_wallpaper_destroy(void *cb);
22 static int setting_display_wallpaper_update(void *cb);
23 static int setting_display_wallpaper_cleanup(void *cb);
24
25 setting_view setting_view_display_wallpaper = {
26         .create = setting_display_wallpaper_create,
27         .destroy = setting_display_wallpaper_destroy,
28         .update = setting_display_wallpaper_update,
29         .cleanup = setting_display_wallpaper_cleanup,
30 };
31
32 //#define MAX_IMAGE_H_SIZE      (600.0)
33 //#define MAX_IMAGE_W_SIZE      (336.0)
34
35 /* ***************************************************
36  *
37  *basic func
38  *
39  ***************************************************/
40
41 static void __destroy_gallery_ug_cb(struct ui_gadget *ug, void *priv)
42 {
43         SETTING_TRACE_BEGIN;
44
45         /* restore the '<-' button on the navigate bar */
46         ret_if(!priv);
47         SettingDisplayUG *ad = (SettingDisplayUG *) priv;       /* ad is point to priv */
48
49         if (ug) {
50                 ug_destroy(ug);
51                 ad->ug_loading = NULL;
52         }
53
54         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navi_bar);
55         ret_if(!navi_it);
56         Evas_Object *back_btn = elm_object_item_part_content_get(navi_it, "prev_btn");
57
58         if (back_btn != NULL) {
59                 elm_object_style_set(back_btn, NAVI_BACK_BUTTON_STYLE); /* take into effect */
60         }
61 }
62
63 static void setting_display_wallpaper_string_vconf_change_cb(keynode_t *key,
64                                                              void *data)
65 {
66         SettingDisplayUG *ad = data;
67         if (!ad)
68                 return;         /*  do nothing if ad is NULL */
69         char *value = vconf_keynode_get_str(key);
70         char *vconf_name = vconf_keynode_get_name(key);
71
72         int w;
73         int h;
74         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
75
76         double max_image_w_size = w/2 - w*0.03;
77         double max_image_h_size = h*0.46875;
78
79         if (!safeStrCmp(vconf_name, VCONFKEY_BGSET)) {
80                 evas_object_image_file_set(ad->img_homescreen, NULL, NULL);     /* give a littile time for gallery to copy the tmp file */
81                 Evas_Object *temp_img = evas_object_image_add(ad->evas);
82                 int temp_w;
83                 int temp_h;
84                 double ratio_w, ratio_h, ratio;
85
86                 /* A:VCONFKEY_BGSET */
87                 evas_object_image_file_set(temp_img, value, NULL);
88                 evas_object_image_size_get(temp_img, &temp_w, &temp_h);
89                 SETTING_TRACE("temp_w:%d, temp_h:%d", temp_w, temp_h);
90                 ratio_w = temp_w / max_image_w_size;
91                 ratio_h = temp_h / max_image_h_size;
92                 ratio = (ratio_w > ratio_h ? ratio_w : ratio_h);
93                 SETTING_TRACE("ratio_w:%f, ratio_h:%f, ratio:%f", ratio_w,
94                               ratio_h, ratio);
95
96                 evas_object_image_file_set(ad->img_homescreen, value, NULL);
97                 evas_object_image_fill_set(ad->img_homescreen, 0, 0,
98                                            temp_w / ratio, temp_h / ratio);
99                 evas_object_size_hint_min_set(ad->img_homescreen,
100                                               temp_w / ratio, temp_h / ratio);
101                 evas_object_del(temp_img);
102
103         } else if (!safeStrCmp(vconf_name, VCONFKEY_IDLE_LOCK_BGSET)) {
104                 /* SETTING_TRACE("vconf_name is %s.\tvalue is %s", vconf_name, value); */
105                 evas_object_image_file_set(ad->img_lockscreen, NULL, NULL);     /* give a littile time for gallery to copy the tmp file */
106                 Evas_Object *temp_img = evas_object_image_add(ad->evas);
107                 int temp_w;
108                 int temp_h;
109                 double ratio_w, ratio_h, ratio;
110                 /* A:VCONFKEY_BGSET */
111                 evas_object_image_file_set(temp_img, value, NULL);
112                 evas_object_image_size_get(temp_img, &temp_w, &temp_h);
113                 SETTING_TRACE("temp_w:%d, temp_h:%d", temp_w, temp_h);
114                 ratio_w = temp_w / max_image_w_size;
115                 ratio_h = temp_h / max_image_h_size;
116                 ratio = (ratio_w > ratio_h ? ratio_w : ratio_h);
117                 SETTING_TRACE("ratio_w:%f, ratio_h:%f, ratio:%f", ratio_w,
118                               ratio_h, ratio);
119
120                 evas_object_image_file_set(ad->img_lockscreen, value, NULL);
121                 evas_object_image_fill_set(ad->img_lockscreen, 0, 0,
122                                            temp_w / ratio, temp_h / ratio);
123                 evas_object_size_hint_min_set(ad->img_lockscreen,
124                                               temp_w / ratio, temp_h / ratio);
125                 evas_object_del(temp_img);
126
127         }
128 }
129
130 int setting_display_wallpaper_listen_vconf_change(void *data)
131 {
132         SETTING_TRACE_BEGIN;
133         int ret;
134         ret =
135             vconf_notify_key_changed(VCONFKEY_BGSET,
136                                      setting_display_wallpaper_string_vconf_change_cb,
137                                      data);
138         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
139                          (char *)VCONFKEY_BGSET, ret);
140
141         ret =
142             vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_BGSET,
143                                      setting_display_wallpaper_string_vconf_change_cb,
144                                      data);
145         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
146                          (char *)VCONFKEY_IDLE_LOCK_BGSET, ret);
147         return ret;
148 }
149
150 int setting_display_wallpaper_unlisten_vconf_change(void *data)
151 {
152         SETTING_TRACE_BEGIN;
153
154         (void)vconf_ignore_key_changed(VCONFKEY_BGSET,
155                                        setting_display_wallpaper_string_vconf_change_cb);
156         (void)vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_BGSET,
157                                        setting_display_wallpaper_string_vconf_change_cb);
158    return 0;
159 }
160
161 static void
162 __wall_button_clicked_cb(void *data, Evas_Object *obj, void *event_info)
163 {
164         SETTING_TRACE_BEGIN;
165         SettingDisplayUG *ad = (SettingDisplayUG *) data;
166         if (!ad) return;                /*  do nothing if ad is NULL */
167
168         if (ad->ug_loading) return; // prevent double-clicked
169                 
170         bundle *b = bundle_create();
171         if (!b) {
172                 return;
173         }
174
175         bundle_add(b, "launch-type", "select-setas");
176         bundle_add(b, "setas-type", "wallpaper");
177         bundle_add(b, "file-type", "image");
178
179         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
180         if (!cbs) {
181                 bundle_free(b);
182                 return;
183         }
184
185         cbs->layout_cb = setting_display_layout_ug_cb;
186         cbs->result_cb = NULL;
187         cbs->destroy_cb = __destroy_gallery_ug_cb;
188         cbs->priv = (void *)ad;
189
190         ad->ug_loading =
191             ug_create(ad->ug, "gallery-efl", UG_MODE_FULLVIEW, b, cbs);
192
193         if (NULL == ad->ug_loading) {   /* error handling */
194                 SETTING_TRACE_ERROR("NULL == ad->ug_loading");
195         setting_create_popup_without_title(ad, ad->win_get, _("Cannot support this function"));
196                 // popup message 
197
198         }
199         bundle_free(b);
200         FREE(cbs);
201 }
202
203 static int setting_display_wallpaper_create(void *cb)
204 {
205         SETTING_TRACE_BEGIN;
206         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
207
208         SettingDisplayUG *ad = (SettingDisplayUG *) cb; /* ad is point to cb */
209
210         Evas_Object *sub_layout = elm_layout_add(ad->win_main_layout);
211         elm_layout_file_set(sub_layout, SETTING_THEME_EDJ_NAME,
212                             "wallpaper_layout");
213         evas_object_size_hint_weight_set(sub_layout, EVAS_HINT_EXPAND,
214                                          EVAS_HINT_EXPAND);
215
216         ad->ly_wall =
217             setting_create_layout_navi_bar_scroller(ad->win_main_layout,
218                                                     ad->win_get,
219                                                     _(KeyStr_Wallpaper),
220                                                     (char *)dgettext("sys_string", "IDS_COM_BODY_BACK"),
221                                                     NULL,
222                                                     setting_display_wallpaper_click_softkey_cancel_cb,
223                                                     NULL, ad, &(ad->scrl_wall),
224                                                     &(ad->navi_bar));
225
226         /* pack outer box into scroller */
227         elm_object_content_set(ad->scrl_wall, sub_layout);
228
229         int w;
230         int h;
231         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
232
233         double max_image_w_size = w/2 - w*0.03;
234         double max_image_h_size = h*0.46875;
235
236         /* elm_scroller_content_set(scroller, sub_layout); */
237         Evas_Object *eo_left, *eo_right;
238         Evas_Object *temp_img = evas_object_image_add(ad->evas);
239         int temp_w;
240         int temp_h;
241         double ratio_w, ratio_h, ratio;
242         char *pa_image_path = NULL;
243
244         /* A:VCONFKEY_BGSET */
245         pa_image_path = vconf_get_str(VCONFKEY_BGSET);
246         evas_object_image_file_set(temp_img, pa_image_path, NULL);
247         evas_object_image_size_get(temp_img, &temp_w, &temp_h);
248         SETTING_TRACE("temp_w:%d, temp_h:%d", temp_w, temp_h);
249         ratio_w = temp_w / max_image_w_size;
250         ratio_h = temp_h / max_image_h_size;
251         ratio = (ratio_w > ratio_h ? ratio_w : ratio_h);
252         SETTING_TRACE("ratio_w:%f, ratio_h:%f, ratio:%f", ratio_w, ratio_h,
253                       ratio);
254         create_image_box_add_ex(NULL, ad->win_main_layout, ad->evas, pa_image_path,
255                                 temp_w / ratio, temp_h / ratio, NULL, NULL,
256                                 &eo_left, &(ad->img_homescreen));
257
258         evas_object_del(temp_img);
259         FREE(pa_image_path);
260
261         /*  temp_img are being used in both bgset & lock screen. */
262         Evas_Object *temp_img2 = evas_object_image_add(ad->evas);
263
264         /* B:VCONFKEY_IDLE_LOCK_BGSET */
265         pa_image_path = vconf_get_str(VCONFKEY_IDLE_LOCK_BGSET);
266         evas_object_image_file_set(temp_img2, pa_image_path, NULL);
267         evas_object_image_size_get(temp_img2, &temp_w, &temp_h);
268         SETTING_TRACE("temp_w:%d, temp_h:%d", temp_w, temp_h);
269         ratio_w = temp_w / max_image_w_size;
270         ratio_h = temp_h / max_image_h_size;
271         ratio = (ratio_w > ratio_h ? ratio_w : ratio_h);
272         SETTING_TRACE("ratio_w:%f, ratio_h:%f, ratio:%f", ratio_w, ratio_h,
273                       ratio);
274         create_image_box_add_ex(NULL, ad->win_main_layout, ad->evas, pa_image_path,
275                                 temp_w / ratio, temp_h / ratio, NULL, NULL,
276                                 &eo_right, &(ad->img_lockscreen));
277         evas_object_del(temp_img2);
278         FREE(pa_image_path);
279
280         elm_object_part_content_set(sub_layout, "left_swallow", eo_left);
281         elm_object_part_content_set(sub_layout, "right_swallow", eo_right);
282
283         edje_object_part_text_set(_EDJ(sub_layout), "left_text",
284                                   _(KeyStr_HomeScreen));
285         edje_object_part_text_set(_EDJ(sub_layout), "right_text",
286                                   _(KeyStr_LockedScreen));
287
288         /*  button : change wallpaper */
289         Evas_Object *btn = elm_button_add(sub_layout);
290         elm_object_text_set(btn, _(SETING_DISPLAY_WALLPAPER_CHANGE_WALLPAPER_STR));
291         evas_object_smart_callback_add(btn, "clicked", __wall_button_clicked_cb,
292                                        ad);
293         elm_object_part_content_set(sub_layout, "btn", btn);
294
295         SETTING_TRACE_DEBUG("get theme : %d", setting_get_theme());
296         if (Theme_Tizen_Black == setting_get_theme()) {
297                 SETTING_TRACE_DEBUG("black theme");
298                 elm_object_style_set(btn, "sweep");
299                 edje_object_signal_emit(_EDJ(sub_layout), "change,non-white,theme", "elm");
300         } else if  (Theme_Tizen_Blue == setting_get_theme()) {
301                 SETTING_TRACE_DEBUG("blue theme");
302                 edje_object_signal_emit(_EDJ(sub_layout), "change,non-white,theme", "elm");
303         } else if  (Theme_Tizen_Grey == setting_get_theme()) {
304                 SETTING_TRACE_DEBUG("grey theme");
305                 edje_object_signal_emit(_EDJ(sub_layout), "change,non-white,theme", "elm");
306         }
307
308         setting_view_display_wallpaper.is_create = 1;
309         setting_display_wallpaper_listen_vconf_change(ad);
310         return SETTING_RETURN_SUCCESS;
311 }
312
313 static int setting_display_wallpaper_destroy(void *cb)
314 {
315         SETTING_TRACE_BEGIN;
316         /* error check */
317         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
318
319         SettingDisplayUG *ad = (SettingDisplayUG *) cb; /* ad is point to cb */
320         setting_display_wallpaper_unlisten_vconf_change(ad);
321
322         if (ad->ly_wall != NULL) {
323                 evas_object_del(ad->ly_wall);
324         }
325         setting_view_display_wallpaper.is_create = 0;
326
327         return SETTING_RETURN_SUCCESS;
328 }
329
330 static int setting_display_wallpaper_update(void *cb)
331 {
332         return SETTING_RETURN_SUCCESS;
333 }
334
335 static int setting_display_wallpaper_cleanup(void *cb)
336 {
337         return SETTING_RETURN_SUCCESS;
338 }
339
340 /* ***************************************************
341  *
342  *general func
343  *
344  ***************************************************/
345
346 /* ***************************************************
347  *
348  *call back func
349  *
350  ***************************************************/
351
352 static void
353 setting_display_wallpaper_click_softkey_cancel_cb(void *data, Evas_Object *obj,
354                                                   void *event_info)
355 {
356         SETTING_TRACE_BEGIN;
357         /* error check */
358         retm_if(data == NULL, "Data parameter is NULL");
359         SettingDisplayUG *ad = (SettingDisplayUG *) data;       /* ad is point to cb */
360
361         /*  called when this shared gadget is terminated. similar with app_exit */
362         /* Send destroy request */
363         ug_destroy_me(ad->ug);
364 }