Merge branch 'master' into 2.0
[apps/core/preloaded/ug-image-viewer-efl.git] / main / src / control / ivug-crop-ug.cpp
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 #include <Elementary.h>
18 #include <ui-gadget.h>
19 #include <ui-gadget-module.h>           // ug_destroy_me, ug_send_result
20
21 #include "ivug-crop-view.h"
22 #include "ivug-crop-ug.h"
23
24 #include "ivug-debug.h"
25 #include "ivug-string.h"
26 #include "ivug-context.h"
27 #include "ivug-db.h"
28
29 #include "ivug-setas-view.h"
30
31 #undef LOG_LVL
32 #define LOG_LVL DBG_MSG_LVL_MED
33
34 #undef LOG_CAT
35 #define LOG_CAT "IV-CROP-UG"
36
37 static void
38 _send_result(ui_gadget_h ug, const char *key1, const char *val1, const char *key2, const char *val2)
39 {
40         ivug_ret_if(!ug);
41
42         service_h service;
43         service_create(&service);
44
45         if(key1 && val1)
46         {
47                 MSG_HIGH("Bundle 1 : [%s = %s]", key1, val1);
48                 service_add_extra_data(service, key1, val1);
49         }
50         if(key2 && val2)
51         {
52                 MSG_HIGH("Bundle 2 : [%s = %s]", key2, val2);
53                 service_add_extra_data(service, key2, val2);
54         }
55         ug_send_result(gGetUGHandle(), service);
56
57         service_destroy(service);
58 }
59
60 static void  _ivug_crop_view_ok_clicked_cb(void *data, Evas_Object *obj, void *event_info)
61 {
62         char *path = (char *)event_info;
63
64         evas_object_smart_callback_del(obj, "ok,clicked", _ivug_crop_view_ok_clicked_cb);
65
66         media_handle m_handle = NULL;
67
68         m_handle = ivug_db_insert_file_to_DB(path);
69         if(m_handle == NULL)
70         {
71                 MSG_ERROR("ivug_db_insert_file_to_DB failed %s", path);
72         }
73         else
74         {
75                 ivug_db_destroy_file_handle(m_handle);
76         }
77
78         _send_result(gGetUGHandle(), "crop_image_path", path, NULL, NULL);
79
80         MSG_HIGH("Start destroy ug");
81         ug_destroy_me(gGetUGHandle());
82 }
83
84 static void _ivug_setas_crop_view_ok_clicked_cb(void *data, Evas_Object *obj, void *event_info)
85 {
86         char *path = (char *)event_info;
87
88         MSG_MAIN_HIGH("_ivug_setas_view_ok_clicked_cb path = %s", path);
89
90         evas_object_smart_callback_del(obj, "ok,clicked", _ivug_setas_crop_view_ok_clicked_cb);
91
92         // do not insert to db
93
94         int setas_data = (int)evas_object_data_get(obj, "setas_type");
95         ivug_set_screen_type type = (ivug_set_screen_type)setas_data;
96
97         int setas_mode = (int)evas_object_data_get(obj, "setas_mode");
98         ivug_setas_mode mode = (ivug_setas_mode)setas_mode;
99
100         const char* homescreen_path = IVUG_HOME_SCREEN_PATH;
101         const char* lockscreen_path = IVUG_LOCK_SCREEN_PATH;
102         const char* appsvc_home_path = IVUG_APPSVC_HOME_SCREEN_PATH;
103
104         if(mode == IVUG_SETAS_CROP)
105         {
106                 ivug_copy_file(path, appsvc_home_path);
107                 _send_result(gGetUGHandle(), "crop_image_path", appsvc_home_path, NULL, NULL);
108         }
109         else
110         {
111                 if(type == IVUG_CTRLBAR_SET_SCREEN_HOME)
112                 {
113                         ivug_copy_file(path, homescreen_path);
114                         ivug_config_set_homescreen_image(homescreen_path);
115                         _send_result(gGetUGHandle(), "homescreen_path", homescreen_path, NULL, NULL);
116                 }
117                 else if(type == IVUG_CTRLBAR_SET_SCREEN_LOCK)
118                 {
119                         ivug_copy_file(path, lockscreen_path);
120                         ivug_config_set_lockscreen_image(lockscreen_path);
121                         _send_result(gGetUGHandle(), "lockscreen_path", lockscreen_path, NULL, NULL);
122                 }
123                 else if(type == IVUG_CTRLBAR_SET_SCREEN_BOTH)
124                 {
125                         ivug_copy_file(path, homescreen_path);
126                         ivug_config_set_homescreen_image(homescreen_path);
127                         ivug_copy_file(path, lockscreen_path);
128                         ivug_config_set_lockscreen_image(lockscreen_path);
129                         _send_result(gGetUGHandle(), "homescreen_path", homescreen_path, "lockscreen_path", lockscreen_path);
130                 }
131         }
132
133         MSG_HIGH("Start destroy ug");
134         ug_destroy_me(gGetUGHandle());
135 }
136
137
138 static void  _ivug_crop_view_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
139 {
140         //ivug_crop_ug_destroy((IvugCropUG *)data);
141
142         evas_object_smart_callback_del(obj, "cancel,clicked", _ivug_crop_view_cancel_clicked_cb);
143
144         _send_result(gGetUGHandle(), "crop_image_path", NULL, NULL, NULL);
145
146         MSG_HIGH("Start destroy ug");
147         ug_destroy_me(gGetUGHandle());
148 }
149
150 static void  _ivug_crop_view_destroyed_cb(void *data, Evas_Object *obj, void *event_info)
151 {
152         IvugCropUG *crop_ug = (IvugCropUG *)data;
153
154         ivug_crop_view_destroy(crop_ug->crop_view);
155
156         crop_ug->crop_view = NULL;
157 }
158
159 static void on_crop_view_btn_back_clicked(void *data, Evas_Object *obj, void *event_info)
160 {
161         IvugCropUG *crop_ug = (IvugCropUG *)data;
162
163         ivug_crop_view_destroy(crop_ug->crop_view);
164
165         crop_ug->crop_view = NULL;
166
167         _send_result(gGetUGHandle(), "crop_image_path", NULL, NULL, NULL);
168
169         MSG_HIGH("Start destroy ug");
170         ug_destroy_me(gGetUGHandle());
171 }
172
173 static void  _ivug_crop_view_clicked_cb(void *data, Evas_Object *obj, void *event_info)
174 {
175         IvugCropUG *crop_ug = (IvugCropUG *)data;
176         bool bShowMenu = (bool)event_info;
177
178         if(bShowMenu)
179         {
180                 elm_object_item_signal_emit(crop_ug->navi_it, "elm,state,toolbar,open", "");
181         }
182         else
183         {
184                 elm_object_item_signal_emit(crop_ug->navi_it, "elm,state,toolbar,close", "");
185         }
186 }
187
188 static Evas_Object*
189 _show_exit_popup( Evas_Object *parent, const char *title, const char *desc, void *data)
190 {
191         MSG_SETAS_HIGH( "title: %s, desc %s", title, desc);
192
193         Evas_Object* style1_popup = elm_popup_add(parent);
194         evas_object_size_hint_weight_set(style1_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
195         elm_popup_timeout_set(style1_popup, (double)2.0);
196         elm_object_text_set(style1_popup, desc);
197         elm_object_part_text_set(style1_popup, "title,text", title);
198
199         evas_object_smart_callback_add(style1_popup, "timeout", _ivug_crop_view_cancel_clicked_cb, data);
200
201         evas_object_show(style1_popup);
202         evas_object_layer_set(style1_popup, EVAS_LAYER_MAX);
203
204         return style1_popup;
205 }
206
207 static void
208 _on_msg_load_failed(void *data, Evas_Object *obj, void *event_info)
209 {
210         int error = (int)event_info;
211
212         const char *szMsg = NULL;
213
214         switch(error)
215         {
216                 case CROP_ERROR_TYPE_INVALID_FILE:
217                         szMsg = IDS_INVALID_IMAGE_FILE;
218                         break;
219                 case CROP_ERROR_TYPE_PERMISSION_DENIED:
220                         szMsg = IDS_PERMISSION_DENIED;
221                         break;
222                 case CROP_ERROR_TYPE_UNKNOWN_FORMAT:
223                         szMsg = "Unknown format";
224                         break;
225                 case CROP_ERROR_TYPE_GENERAL:
226                 default:
227                         szMsg = IDS_FAILED;
228                         break;
229         }
230
231         MSG_ERROR("Load failed : %s", szMsg);
232
233         _show_exit_popup(obj, IDS_ERROR, szMsg, data);
234 }
235
236 IvugCropUG * ivug_crop_ug_create(Evas_Object *parent, int w, int h, bool bRatioFix, const char *filepath)
237 {
238         IvugCropUG *crop_ug = (IvugCropUG *)calloc(1, sizeof(IvugCropUG));
239         if(crop_ug == NULL)
240         {
241                 MSG_ERROR("crop_ug calloc error");
242                 return NULL;
243         }
244
245         IvugCropView *crop_view = NULL;
246         Evas_Object *layout = NULL;
247
248         crop_view = ivug_crop_view_create(parent);
249         if(crop_view == NULL)
250         {
251                 MSG_ERROR("ivug_crop_view_create error");
252                 free(crop_ug);
253                 return NULL;
254         }
255
256         ivug_crop_view_box_size_set(crop_view, w, h);
257         //ivug_crop_view_file_set(crop_view, filepath);
258         ivug_crop_view_box_ratio_fix(crop_view, bRatioFix);
259
260         layout = ivug_crop_view_get_object(crop_view);
261
262         evas_object_smart_callback_add(layout, "ok,clicked", _ivug_crop_view_ok_clicked_cb, crop_ug);
263         evas_object_smart_callback_add(layout, "cancel,clicked", _ivug_crop_view_cancel_clicked_cb, crop_ug);
264         evas_object_smart_callback_add(layout, "destroyed", _ivug_crop_view_destroyed_cb, crop_ug);
265         evas_object_smart_callback_add(layout, "download,failed", _on_msg_load_failed, crop_ug);
266         //evas_object_smart_callback_add(layout, "clicked", _ivug_crop_view_clicked_cb, crop_ug);
267
268         crop_ug->crop_view = crop_view;
269         crop_ug->layout = layout;
270         crop_ug->parent = parent;
271         crop_ug->filepath = strdup(filepath);
272
273         crop_ug->navi_bar = elm_naviframe_add(parent);
274
275 #ifdef USE_CUSTOM_STYLE
276         elm_object_theme_set(crop_ug->navi_bar, gGetSystemTheme() );
277 #endif
278         const char *profile = elm_config_profile_get();
279         if (!strcmp(profile,"mobile"))
280         {
281                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/default");
282         }
283         else if (!strcmp(profile,"desktop"))
284         {
285                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/noindicator");
286         }
287         else
288         {
289                 MSG_MAIN_ERROR("Unknown profile : %s", profile);
290         }
291
292         evas_object_name_set(crop_ug->navi_bar, "Crop ug naviframe");
293
294 // Layout life cycle is controlled by application explictily.
295         elm_naviframe_content_preserve_on_pop_set(crop_ug->navi_bar, EINA_TRUE);
296
297         Evas_Object *back_btn = ivug_button_add(crop_ug->navi_bar, "naviframe/end_btn/default",
298                 IDS_BACK, NULL, on_crop_view_btn_back_clicked, crop_ug);
299
300         crop_ug->navi_it = elm_naviframe_item_push(crop_ug->navi_bar, IDS_LOADING, back_btn, NULL, crop_ug->layout, NULL);
301         elm_naviframe_item_title_visible_set(crop_ug->navi_it, EINA_FALSE);
302
303         ivug_crop_view_create_menu(crop_view, crop_ug->navi_bar);
304
305         return crop_ug;
306 }
307
308 IvugCropUG * ivug_setas_crop_ug_create(Evas_Object *parent, const char *filepath, ivug_setas_mode mode, ivug_set_screen_type type)
309 {
310         IvugCropUG *crop_ug = (IvugCropUG *)calloc(1, sizeof(IvugCropUG));
311         if(crop_ug == NULL)
312         {
313                 MSG_ERROR("crop_ug calloc error");
314                 return NULL;
315         }
316
317         IvugCropView *crop_view = NULL;
318         Evas_Object *layout = NULL;
319
320         crop_view = ivug_crop_view_create(parent);
321         if(crop_view == NULL)
322         {
323                 MSG_ERROR("ivug_crop_view_create error");
324                 free(crop_ug);
325                 return NULL;
326         }
327
328         layout = ivug_crop_view_get_object(crop_view);
329
330         evas_object_smart_callback_add(layout, "ok,clicked", _ivug_setas_crop_view_ok_clicked_cb, crop_ug);
331         evas_object_smart_callback_add(layout, "cancel,clicked", _ivug_crop_view_cancel_clicked_cb, crop_ug);
332         evas_object_smart_callback_add(layout, "destroyed", _ivug_crop_view_destroyed_cb, crop_ug);
333         evas_object_smart_callback_add(layout, "download,failed", _on_msg_load_failed, crop_ug);
334         //evas_object_smart_callback_add(layout, "clicked", _ivug_crop_view_clicked_cb, crop_ug);
335
336         evas_object_data_set(layout, "setas_type", (void *)type);
337         evas_object_data_set(layout, "setas_mode", (void *)mode);
338
339         crop_ug->crop_view = crop_view;
340         crop_ug->layout = layout;
341         crop_ug->parent = parent;
342         crop_ug->filepath = strdup(filepath);
343
344         crop_ug->navi_bar = elm_naviframe_add(parent);
345
346 #ifdef USE_CUSTOM_STYLE
347         elm_object_theme_set(crop_ug->navi_bar, gGetSystemTheme() );
348 #endif
349         const char *profile = elm_config_profile_get();
350         if (!strcmp(profile,"mobile"))
351         {
352                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/default");
353         }
354         else if (!strcmp(profile,"desktop"))
355         {
356                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/noindicator");
357         }
358         else
359         {
360                 MSG_MAIN_ERROR("Unknown profile : %s", profile);
361         }
362
363         evas_object_name_set(crop_ug->navi_bar, "Crop ug naviframe");
364
365 // Layout life cycle is controlled by application explictily.
366         elm_naviframe_content_preserve_on_pop_set(crop_ug->navi_bar, EINA_TRUE);
367
368         Evas_Object *back_btn = ivug_button_add(crop_ug->navi_bar, "naviframe/end_btn/default",
369                 IDS_BACK, NULL, on_crop_view_btn_back_clicked, crop_ug);
370
371         crop_ug->navi_it = elm_naviframe_item_push(crop_ug->navi_bar, IDS_LOADING, back_btn, NULL, crop_ug->layout, NULL);
372         elm_naviframe_item_title_visible_set(crop_ug->navi_it, EINA_FALSE);
373
374         int lcd_x = 0;
375         int lcd_y = 0;
376         int lcd_w = 0;
377         int lcd_h = 0;
378         int rot = 0;
379
380         ivug_crop_view_create_menu(crop_view, crop_ug->navi_bar);
381
382         ivug_crop_view_destination_set(crop_view, DATA_PATH, ".iv_setas.jpg");
383
384         evas_object_geometry_get((Evas_Object *)ug_get_window(), &lcd_x, &lcd_y, &lcd_w, &lcd_h);
385         rot = gGetRotationDegree();
386         if(rot == 90 || rot == 270)
387         {
388                 int temp = lcd_w;
389                 lcd_w = lcd_h;
390                 lcd_h = temp;
391         }
392
393         ivug_crop_view_box_size_set(crop_view, lcd_w, lcd_h);
394
395         ivug_crop_view_box_ratio_fix(crop_view, true);
396
397         return crop_ug;
398 }
399
400 Evas_Object * ivug_crop_ug_get_layout(IvugCropUG * crop_ug)
401 {
402         IV_ASSERT(crop_ug != NULL);
403
404         return crop_ug->navi_bar;
405 }
406
407 bool ivug_crop_ug_destroy(IvugCropUG * crop_ug)
408 {
409         IV_ASSERT(crop_ug != NULL);
410
411         if(crop_ug->filepath)
412         {
413                 free(crop_ug->filepath);
414                 crop_ug->filepath = NULL;
415         }
416
417         if(crop_ug->crop_view)
418         {
419                 ivug_crop_view_destroy(crop_ug->crop_view);
420                 crop_ug->crop_view = NULL;
421         }
422         crop_ug->layout = NULL;
423
424         free(crop_ug);
425
426         return true;
427 }
428
429 bool ivug_crop_ug_start(IvugCropUG * crop_ug)
430 {
431         MSG_HIGH("ivug_crop_ug_start");
432         IV_ASSERT(crop_ug != NULL);
433
434         ivug_crop_view_file_set(crop_ug->crop_view, crop_ug->filepath);
435
436         return true;
437 }
438