sync main branch
[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
28 #undef LOG_LVL
29 #define LOG_LVL DBG_MSG_LVL_MED
30
31 #undef LOG_CAT
32 #define LOG_CAT "IV-CROP-UG"
33
34 static void
35 _send_result(ui_gadget_h ug, const char *key1, const char *val1, const char *key2, const char *val2)
36 {
37         ivug_ret_if(!ug);
38
39         service_h service;
40         service_create(&service);
41
42         if(key1 && val1)
43         {
44                 MSG_HIGH("Bundle 1 : [%s = %s]", key1, val1);
45                 service_add_extra_data(service, key1, val1);
46         }
47         if(key2 && val2)
48         {
49                 MSG_HIGH("Bundle 2 : [%s = %s]", key2, val2);
50                 service_add_extra_data(service, key2, val2);
51         }
52         ug_send_result(gGetUGHandle(), service);
53
54         service_destroy(service);
55 }
56
57 static void  _ivug_crop_view_ok_clicked_cb(void *data, Evas_Object *obj, void *event_info)
58 {
59         char *path = (char *)event_info;
60
61         evas_object_smart_callback_del(obj, "ok,clicked", _ivug_crop_view_ok_clicked_cb);
62
63         _send_result(gGetUGHandle(), "crop_image_path", path, NULL, NULL);
64
65         MSG_HIGH("Start destroy ug");
66         ug_destroy_me(gGetUGHandle());
67 }
68
69 static void  _ivug_crop_view_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
70 {
71         //ivug_crop_ug_destroy((IvugCropUG *)data);
72
73         evas_object_smart_callback_del(obj, "cancel,clicked", _ivug_crop_view_cancel_clicked_cb);
74
75         _send_result(gGetUGHandle(), "crop_image_path", NULL, NULL, NULL);
76
77         MSG_HIGH("Start destroy ug");
78         ug_destroy_me(gGetUGHandle());
79 }
80
81 static void  _ivug_crop_view_destroyed_cb(void *data, Evas_Object *obj, void *event_info)
82 {
83         IvugCropUG *crop_ug = (IvugCropUG *)data;
84
85         crop_ug->crop_view = NULL;
86 }
87
88 static void on_crop_view_btn_back_clicked(void *data, Evas_Object *obj, void *event_info)
89 {
90         IvugCropUG *crop_ug = (IvugCropUG *)data;
91
92         ivug_crop_view_destroy(crop_ug->crop_view);
93
94         crop_ug->crop_view = NULL;
95
96         _send_result(gGetUGHandle(), "crop_image_path", NULL, NULL, NULL);
97
98         MSG_HIGH("Start destroy ug");
99         ug_destroy_me(gGetUGHandle());
100 }
101
102 static void  _ivug_crop_view_clicked_cb(void *data, Evas_Object *obj, void *event_info)
103 {
104         IvugCropUG *crop_ug = (IvugCropUG *)data;
105         bool bShowMenu = (bool)event_info;
106
107         if(bShowMenu)
108         {
109                 elm_object_item_signal_emit(crop_ug->navi_it, "elm,state,toolbar,open", "");
110         }
111         else
112         {
113                 elm_object_item_signal_emit(crop_ug->navi_it, "elm,state,toolbar,close", "");
114         }
115 }
116
117 static Evas_Object*
118 _show_exit_popup( Evas_Object *parent, const char *title, const char *desc, void *data)
119 {
120         MSG_SETAS_HIGH( "title: %s, desc %s", title, desc);
121
122         Evas_Object* style1_popup = elm_popup_add(parent);
123         evas_object_size_hint_weight_set(style1_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
124         elm_popup_timeout_set(style1_popup, (double)2.0);
125         elm_object_text_set(style1_popup, desc);
126         elm_object_part_text_set(style1_popup, "title,text", title);
127
128         evas_object_smart_callback_add(style1_popup, "timeout", _ivug_crop_view_cancel_clicked_cb, data);
129
130         evas_object_show(style1_popup);
131         evas_object_layer_set(style1_popup, EVAS_LAYER_MAX);
132
133         return style1_popup;
134 }
135
136 static void
137 _on_msg_load_failed(void *data, Evas_Object *obj, void *event_info)
138 {
139         int error = (int)event_info;
140
141         const char *szMsg = NULL;
142
143         switch(error)
144         {
145                 case CROP_ERROR_TYPE_INVALID_FILE:
146                         szMsg = IDS_INVALID_IMAGE_FILE;
147                         break;
148                 case CROP_ERROR_TYPE_PERMISSION_DENIED:
149                         szMsg = IDS_PERMISSION_DENIED;
150                         break;
151                 case CROP_ERROR_TYPE_UNKNOWN_FORMAT:
152                         szMsg = "Unknown format";
153                         break;
154                 case CROP_ERROR_TYPE_GENERAL:
155                 default:
156                         szMsg = IDS_FAILED;
157                         break;
158         }
159
160         MSG_ERROR("Load failed : %s", szMsg);
161
162         _show_exit_popup(obj, IDS_ERROR, szMsg, data);
163 }
164
165 IvugCropUG * ivug_crop_ug_create(Evas_Object *parent, int w, int h, bool bRatioFix, const char *filepath)
166 {
167         IvugCropUG *crop_ug = (IvugCropUG *)calloc(1, sizeof(IvugCropUG));
168         if(crop_ug == NULL)
169         {
170                 MSG_ERROR("crop_ug calloc error");
171                 return NULL;
172         }
173
174         IvugCropView *crop_view = NULL;
175         Evas_Object *layout = NULL;
176
177         crop_view = ivug_crop_view_create(parent);
178         if(crop_view == NULL)
179         {
180                 MSG_ERROR("ivug_crop_view_create error");
181                 free(crop_ug);
182                 return NULL;
183         }
184
185         ivug_crop_view_box_size_set(crop_view, w, h);
186         //ivug_crop_view_file_set(crop_view, filepath);
187         ivug_crop_view_box_ratio_fix(crop_view, bRatioFix);
188
189         layout = ivug_crop_view_get_object(crop_view);
190
191         evas_object_smart_callback_add(layout, "ok,clicked", _ivug_crop_view_ok_clicked_cb, crop_ug);
192         evas_object_smart_callback_add(layout, "cancel,clicked", _ivug_crop_view_cancel_clicked_cb, crop_ug);
193         evas_object_smart_callback_add(layout, "destroyed", _ivug_crop_view_destroyed_cb, crop_ug);
194         evas_object_smart_callback_add(layout, "download,failed", _on_msg_load_failed, crop_ug);
195         //evas_object_smart_callback_add(layout, "clicked", _ivug_crop_view_clicked_cb, crop_ug);
196
197         crop_ug->crop_view = crop_view;
198         crop_ug->layout = layout;
199         crop_ug->parent = parent;
200         crop_ug->filepath = strdup(filepath);
201
202         crop_ug->navi_bar = elm_naviframe_add(parent);
203
204 #ifdef USE_CUSTOM_STYLE
205         elm_object_theme_set(crop_ug->navi_bar, gGetSystemTheme() );
206 #endif
207         const char *profile = elm_config_profile_get();
208         if (!strcmp(profile,"mobile"))
209         {
210                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/default");
211         }
212         else if (!strcmp(profile,"desktop"))
213         {
214                 elm_object_style_set(crop_ug->navi_bar, "ivug-main/noindicator");
215         }
216         else
217         {
218                 MSG_MAIN_ERROR("Unknown profile : %s", profile);
219         }
220
221         evas_object_name_set(crop_ug->navi_bar, "Crop ug naviframe");
222
223 // Layout life cycle is controlled by application explictily.
224         elm_naviframe_content_preserve_on_pop_set(crop_ug->navi_bar, EINA_TRUE);
225
226         Evas_Object *back_btn = ivug_button_add(crop_ug->navi_bar, "naviframe/end_btn/default",
227                 IDS_BACK, NULL, on_crop_view_btn_back_clicked, crop_ug);
228
229         crop_ug->navi_it = elm_naviframe_item_push(crop_ug->navi_bar, IDS_LOADING, back_btn, NULL, crop_ug->layout, NULL);
230         elm_naviframe_item_title_visible_set(crop_ug->navi_it, EINA_FALSE);
231
232         ivug_crop_view_create_menu(crop_view, crop_ug->navi_bar);
233
234         return crop_ug;
235 }
236
237 Evas_Object * ivug_crop_ug_get_layout(IvugCropUG * crop_ug)
238 {
239         IV_ASSERT(crop_ug != NULL);
240
241         return crop_ug->navi_bar;
242 }
243
244 bool ivug_crop_ug_destroy(IvugCropUG * crop_ug)
245 {
246         IV_ASSERT(crop_ug != NULL);
247
248         if(crop_ug->filepath)
249         {
250                 free(crop_ug->filepath);
251                 crop_ug->filepath = NULL;
252         }
253
254         if(crop_ug->crop_view)
255         {
256                 ivug_crop_view_destroy(crop_ug->crop_view);
257                 crop_ug->crop_view = NULL;
258         }
259         crop_ug->layout = NULL;
260
261         free(crop_ug);
262
263         return true;
264 }
265
266 bool ivug_crop_ug_start(IvugCropUG * crop_ug)
267 {
268         MSG_HIGH("ivug_crop_ug_start");
269         IV_ASSERT(crop_ug != NULL);
270
271         ivug_crop_view_file_set(crop_ug->crop_view, crop_ug->filepath);
272
273         return true;
274 }
275