initial upload for tizen 2.0 beta
[apps/home/gallery.git] / ug / ug-gallery-efl / src / ge-ext-ug-load.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 #include <Ecore_X.h>
18 #include "ge-ext-ug-load.h"
19 #include "ge-debug.h"
20 #include "ge-ui-util.h"
21 #include "ge-util.h"
22 #include "ge-albums.h"
23 #include "ge-gridview.h"
24
25 #define GE_IV_UG_NAME "image-viewer-efl"
26 #define GE_IV_STR_LEN_MAX 32
27
28 static bool ge_destroy_me = false;
29
30 static void _ge_ext_destroy_me(ge_ugdata *ugd)
31 {
32         ge_dbg("");
33         GE_CHECK(ugd);
34         GE_CHECK(ugd->ug_called_by_me);
35         GE_CHECK(ugd->ug);
36         GE_CHECK(ugd->service);
37         bool send_result = false;
38
39         if (_ge_ug_iv_get())
40         {
41                 _ge_ug_iv_set(false);
42         }
43
44         if(ge_destroy_me == false)
45         {
46                 ge_dbg("Destroy ug_called_by_me");
47                 ug_destroy(ugd->ug_called_by_me);
48                 ugd->ug_called_by_me = NULL;
49         }
50         if(ugd->file_select_mode == GE_File_Select_Setas)
51         {
52                 if(ugd->file_setas_image_path)
53                 {
54                         ge_dbg("GE_SETAS_IMAGE_PATH:%s", ugd->file_setas_image_path);
55                         service_add_extra_data(ugd->service,
56                                                GE_SETAS_IMAGE_PATH,
57                                                ugd->file_setas_image_path);
58
59                         free(ugd->file_setas_image_path);
60                         ugd->file_setas_image_path = NULL;
61                         send_result = true;
62                 }
63
64                 if(ugd->file_setas_crop_image_path &&
65                    (ugd->file_select_setas_mode == GE_File_Select_Setas_CallerID ||
66                     ugd->file_select_setas_mode == GE_FILE_SELECT_SETAS_MODE_CROP_WALLPAPER)) {
67                         ge_dbg("GE_SETAS_CALLERID_CROP_IMAGE_PATH:%s",
68                                ugd->file_setas_crop_image_path);
69                         service_add_extra_data(ugd->service,
70                                                GE_SETAS_CALLERID_CROP_IMAGE_PATH,
71                                                ugd->file_setas_crop_image_path);
72
73                         free(ugd->file_setas_crop_image_path);
74                         ugd->file_setas_crop_image_path = NULL;
75                         send_result = true;
76                 }
77
78                 if(send_result)
79                 {
80                         ge_dbg("Call ug_send_result() to send result.");
81                         ug_send_result(ugd->ug, ugd->service);
82                 }
83         }
84
85         if(ge_destroy_me)
86         {
87                 ge_dbg("Setting is appllied, destroy gallery UG.");
88                 ge_destroy_me = false;
89                 /* Destroy self */
90                 ug_destroy_me(ugd->ug);
91                 /* ug_called_by_me would be destroyed by ui-gadget */
92                 ugd->ug_called_by_me = NULL;
93         }
94         else
95         {
96                 ge_dbg("Cancel button tapped, back to thumbnails view.");
97         }
98 }
99
100 static void _ge_ext_iv_layout_cb(ui_gadget_h ug, enum ug_mode mode, void* priv)
101 {
102         ge_dbg("");
103         GE_CHECK(priv);
104         GE_CHECK(ug);
105
106         Evas_Object *base = (Evas_Object *) ug_get_layout(ug);
107         if (!base) {
108                 ge_dbgE("ug_get_layout failed!");
109                 ug_destroy(ug);
110                 return;
111         }
112
113         ge_ugdata *ugd = (ge_ugdata *)priv;
114
115         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
116         elm_win_resize_object_add(ugd->win, base);
117         /* Disable effect to avoid BS caused by ui-gadget to
118              unset ug layout after deleting it */
119         ug_disable_effect(ug);
120         evas_object_show(base);
121 }
122
123 static void _ge_ext_iv_result_cb(ui_gadget_h ug, service_h result, void *priv)
124 {
125         ge_dbg("");
126         GE_CHECK(priv);
127         GE_CHECK(result);
128         ge_ugdata *ugd = (ge_ugdata *)priv;
129         char* path = NULL;
130
131         if(ugd->file_select_mode == GE_File_Select_Setas)
132         {
133                 service_get_extra_data(result, GE_IMAGEVIEWER_IMAGE_PATH, &path);
134                 ge_dbg("SETAS_IMAGE_PATH");
135                 ge_destroy_me = true;
136                 if(path)
137                 {
138                         ge_dbg(":%s", path);
139                         ugd->file_setas_image_path = path;
140                         path = NULL;
141                 }
142
143                 if(ugd->file_select_setas_mode == GE_File_Select_Setas_CallerID ||
144                    ugd->file_select_setas_mode == GE_FILE_SELECT_SETAS_MODE_CROP_WALLPAPER) {
145                         service_get_extra_data(result,
146                                                GE_IMAGEVIEWER_CROP_IMAGE_PATH,
147                                                &path);
148                         ge_dbg("CALLERID_CROP_IMAGE_PATH");
149                         if(path)
150                         {
151                                 ge_dbg(":%s", path);
152                                 ugd->file_setas_crop_image_path = path;
153                         }
154                 }
155         }
156
157         char *error_state = NULL;
158         service_get_extra_data(result, GE_IMAGEVIEWER_RETURN_ERROR,
159                                &error_state);
160         if (error_state)
161         {
162                 ge_dbg("error string : %s", error_state);
163
164                 if (!g_strcmp0(error_state, "not_supported_file_type")) {
165                         ge_destroy_me = FALSE;
166                         ge_item* cur_item = _ge_get_current_item();
167                         if(cur_item == NULL || cur_item->item == NULL ||
168                            cur_item->item->file_url == NULL) {
169                                 ge_dbgE("current item is NULL");
170                                 GE_FREE_MEM(error_state);
171                                 return;
172                         }
173                         service_h service;
174                         service_create(&service);
175                         GE_CHECK(service);
176                         service_set_operation(service, SERVICE_OPERATION_VIEW);
177                         service_set_uri(service, cur_item->item->file_url);
178                         service_send_launch_request(service, NULL, NULL);
179                         service_destroy(service);
180                 }
181                 GE_FREE_MEM(error_state);
182         }
183 }
184
185 static void _ge_ext_iv_destroy_cb(ui_gadget_h ug, void *priv)
186 {
187         ge_dbg("");
188         GE_CHECK(priv);
189         ge_ugdata *ad = (ge_ugdata *) priv;
190
191         _ge_ext_destroy_me(ad);
192 }
193
194 int ge_ext_load_iv_ug(ge_ugdata* ugd)
195 {
196         ge_dbg("");
197         GE_CHECK_VAL(ugd, -1);
198
199         struct ug_cbs cbs;
200         ui_gadget_h ug = NULL;
201         service_h service = NULL;
202
203         if(ugd->ug_called_by_me)
204         {
205                 ge_dbg("Already exits some UG called by me");
206                 return -1;
207         }
208
209         ge_item* cur_item = _ge_get_current_item();
210         if(cur_item == NULL || cur_item->item == NULL)
211         {
212                 ge_dbg("current item is NULL");
213                 return -1;
214         }
215         memset(&cbs,0x00,sizeof(struct ug_cbs));
216         cbs.layout_cb = _ge_ext_iv_layout_cb;
217         cbs.result_cb = _ge_ext_iv_result_cb;
218         cbs.destroy_cb = _ge_ext_iv_destroy_cb;
219         cbs.priv = ugd;
220
221         service_create(&service);
222         GE_CHECK_VAL(service, -1);
223
224         _ge_ug_iv_set(true);
225
226         service_add_extra_data(service, "View Mode", "SETAS" );
227         service_add_extra_data(service, "Path", cur_item->item->file_url);
228
229         if(ugd->file_select_setas_mode == GE_File_Select_Setas_Wallpaper) {
230                 service_add_extra_data(service, "Setas type", "Wallpaper");
231
232                 if(ugd->file_setas_homescreen_path)
233                 {
234                         ge_dbg("homescreen_path:%s", ugd->file_setas_homescreen_path);
235                         service_add_extra_data(service, GE_IMAGEVIEWER_HOMESCREEN_PATH, ugd->file_setas_homescreen_path);
236                         free(ugd->file_setas_homescreen_path);
237                         ugd->file_setas_homescreen_path = NULL;
238                 }
239
240                 if(ugd->file_setas_lockscreen_path)
241                 {
242                         ge_dbg("lockscreen_path:%s", ugd->file_setas_lockscreen_path);
243                         service_add_extra_data(service, GE_IMAGEVIEWER_LOCKSCREEN_PATH, ugd->file_setas_lockscreen_path);
244                         free(ugd->file_setas_lockscreen_path);
245                         ugd->file_setas_lockscreen_path = NULL;
246                 }
247         } else if(ugd->file_select_setas_mode == GE_FILE_SELECT_SETAS_MODE_CROP_WALLPAPER) {
248                 service_add_extra_data(service, "Setas type", "Crop");
249                 service_add_extra_data(service, "Fixed ratio", "TRUE");
250
251                 int w = 0;
252                 int h = 0;
253                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
254                                         &w, &h);
255                 char *reso_str = (char *)calloc(1, GE_IV_STR_LEN_MAX);
256                 GE_CHECK_VAL(reso_str, -1);
257                 snprintf(reso_str, GE_IV_STR_LEN_MAX, "%dx%d", w, h);
258                 ge_dbgW("Window Resolution: %dx%d, %s", w, h, reso_str);
259                 service_add_extra_data(service, "Resolution", reso_str);
260                 free(reso_str);
261                 reso_str = NULL;
262         } else if(ugd->file_select_setas_mode == GE_File_Select_Setas_CallerID) {
263                 service_add_extra_data(service, "Setas type", "CallerID");
264                 if(ugd->file_setas_callid_size)
265                 {
266                         ge_dbg("callerid_size:%s", ugd->file_setas_callid_size);
267                         service_add_extra_data(service, GE_IMAGEVIEWER_CALLERID_SIZE, ugd->file_setas_callid_size);
268                         free(ugd->file_setas_callid_size);
269                         ugd->file_setas_callid_size = NULL;
270                 }
271
272                 if(ugd->file_setas_callid_path)
273                 {
274                         ge_dbg("callerid_path:%s", ugd->file_setas_callid_path);
275                         service_add_extra_data(service, GE_IMAGEVIEWER_CALLERID_PATH, ugd->file_setas_callid_path);
276                         free(ugd->file_setas_callid_path);
277                         ugd->file_setas_callid_path = NULL;
278                 }
279         }
280         ug = ug_create(ugd->ug, GE_IV_UG_NAME, UG_MODE_FULLVIEW, service, &cbs);
281         ugd->ug_called_by_me = ug;
282         service_destroy(service);
283
284         return 0;
285 }
286