e441cf5081cda632e8702070444d245ea53bc72f
[apps/home/gallery.git] / src / util / gl-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 <Elementary.h>
18 #include <Ecore_X.h>
19 #include <appsvc.h>
20 #include "gl-ext-ug-load.h"
21 #include "gl-debug.h"
22 #include "gl-ui-util.h"
23 #include "gl-util.h"
24 #include "gl-albums.h"
25 #include "gl-gridview.h"
26 #include "gl-listview.h"
27 #include "gallery.h"
28 #include "gl-popup.h"
29 #include "gl-strings.h"
30
31 #define GL_EXT_STR_BUNDLE_LEN 48
32
33 #define GL_UG_PKG_IV                    "image-viewer-efl"
34 #define GL_UG_PKG_GALLERY               "gallery-efl"
35
36 #define GL_ARGV_IV_VIEW_MODE                    "View Mode"
37 #define GL_ARGV_IV_VIEW_GALLERY                 "GALLERY"
38 #define GL_ARGV_IV_PATH                                 "Path"
39 #define GL_ARGV_IV_INDEX                                "Index"
40 #define GL_ARGV_IV_VIEW_BY                              "View By"
41 #define GL_ARGV_IV_VIEW_BY_FOLER                "By Folder"
42 #define GL_ARGV_IV_VIEW_BY_ALL                  "All"
43 #define GL_ARGV_IV_MEDIA_TYPE                   "Media type"
44 #define GL_ARGV_IV_MEDIA_ALL                    "All"
45 #define GL_ARGV_IV_MEDIA_IMAGE                  "Image"
46 #define GL_ARGV_IV_MEDIA_VIDEO                  "Video"
47 #define GL_ARGV_IV_ALBUM_INDEX                  "Album index"
48 #define GL_ARGV_IV_SORT_BY                              "Sort By"
49 #define GL_ARGV_IV_SORT_NAME                    "Name"
50 #define GL_ARGV_IV_SORT_NAMEDESC                "NameDesc"
51 #define GL_ARGV_IV_SORT_DATE                    "Date"
52 #define GL_ARGV_IV_SORT_DATEDESC                "DateDesc"
53
54 #define GL_ARGV_LAUNCH_TYPE                     "launch-type"
55 #define GL_ARGV_FILE_TYPE                       "file-type"
56 #define GL_ARGV_SETAS_TYPE                      "setas-type"
57 #define GL_ARGV_CALLERID_SIZE                   "callerid-size"
58 #define GL_ARGV_CALLERID_PATH                   "callerid-path"
59 #define GL_ARGV_HOMESCREEN_PATH                 "homescreen-path"
60 #define GL_ARGV_LOCKSCREEN_PATH                 "lockscreen-path"
61
62 #define GL_ARGV_SETAS_WALLPALER                 "wallpaper"
63 #define GL_ARGV_SETAS_CALLERID          "callerid"
64 #define GL_ARGV_SELECT_ALBUM                    "select-album"
65 #define GL_ARGV_SELECT_ONE                      "select-one"
66 #define GL_ARGV_SELECT_MULTIPLE                 "select-multiple"
67 #define GL_ARGV_SELECT_SETAS                    "select-setas"
68 #define GL_ARGV_SELECT_IMFT                     "select-imft"
69 #define GL_ARGV_SELECT_RETURN_ALBUM             "album-id"
70 #define GL_ARGV_SELECT_RETURN_FOLDER_PATH       "folder-path"
71 #define GL_ARGV_SELECT_RETURN_COUNT             "count"
72 #define GL_ARGV_SELECT_RETURN_PATH              "path"
73 #define GL_ARGV_SETAS_RETURN_CROP_PATH  "crop-image-path"
74 #define GE_ARGV_SETAS_RETURN_PATH               "image-path"
75 #define GL_ARGV_IMFT_CONTACT                    "contact"
76 #define GL_ARGV_FILE_TYPE_IMAGE                 "image"
77 #define GL_ARGV_FILE_TYPE_VIDEO                 "video"
78 #define GL_ARGV_FILE_TYPE_ALL                   "all"
79
80 static void __gl_ext_destroy_ug(void *data)
81 {
82         GL_CHECK(data);
83         gl_appdata *ad = (gl_appdata *)data;
84         GL_CHECK(ad->uginfo.ug_called_by_me);
85         int view_mode = gl_get_view_mode(ad);
86         gl_dbg("View mode: %d", view_mode);
87
88         gl_list_clear_view(ad);
89
90         /**
91         * view_mode is set as GL_VIEW_THUMBS in gridview,
92         * It means ug_called_by_me is ImageViewer UG.
93         */
94         if (view_mode == GL_VIEW_THUMBS)
95                 gl_update_view(ad, GL_UPDATE_VIEW_NORMAL);
96
97         ug_destroy(ad->uginfo.ug_called_by_me);
98         ad->uginfo.ug_called_by_me = NULL;
99 }
100
101 void
102 _gl_ext_ug_layout_cb(struct ui_gadget *ug, enum ug_mode mode, void *priv)
103 {
104         gl_dbg("");
105         GL_CHECK(ug);
106         GL_CHECK(priv);
107
108         Evas_Object *base = NULL;
109         base = ug_get_layout(ug);
110         if (!base) {
111                 gl_dbgE("ug_get_layout failed!");
112                 ug_destroy(ug);
113                 return;
114         }
115         gl_appdata *ad = (gl_appdata *)priv;
116
117         switch (mode) {
118         case UG_MODE_FULLVIEW:
119                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
120                                                  EVAS_HINT_EXPAND);
121                 elm_win_resize_object_add(ug_get_window(), base);
122                 /* Disable effect when AUL, other show effect */
123                 if(ad->albuminfo.aul_launch_type != GL_AUL_T_NONE &&
124                    ad->albuminfo.aul_launch_type != GL_AUL_T_VIEW_ALBUM) {
125                         gl_dbg("No animation.");
126                         ug_disable_effect(ug);
127                 }
128                 evas_object_show(base);
129                 break;
130         default:
131                 break;
132         }
133 }
134
135 void
136 _gl_ext_ug_destroy_cb(struct ui_gadget *ug, void *priv)
137 {
138         GL_CHECK(priv);
139         gl_appdata *ad = (gl_appdata *)priv;
140         gl_dbg("");
141
142         if(ad->albuminfo.aul_launch_type == GL_AUL_T_NONE ||
143            ad->albuminfo.aul_launch_type == GL_AUL_T_VIEW_ALBUM) {
144                 GL_CHECK(ad->uginfo.ug_called_by_me);
145                 __gl_ext_destroy_ug(ad);
146         } else {
147                 GL_CHECK(ad->uginfo.gallery_ug_called_by_me);
148                 gl_dbg("Pause Gallery App.");
149                 elm_win_lower(ad->maininfo.win);
150
151                 gl_dbg("Destroy Gallery UG directly.");
152                 /* init launch-type after gallery-ug is closed */
153                 ad->albuminfo.aul_launch_type = GL_AUL_T_NONE;
154
155                 ug_destroy(ad->uginfo.gallery_ug_called_by_me);
156                 ad->uginfo.gallery_ug_called_by_me = NULL;
157         }
158 }
159
160 /* Invoke Imageviewer UG */
161 int gl_ext_load_iv_ug(void *data, gl_item *cur_item)
162 {
163         GL_CHECK_VAL(cur_item, GL_UG_FAIL);
164         GL_CHECK_VAL(cur_item->item, GL_UG_FAIL);
165         GL_CHECK_VAL(data, GL_UG_FAIL);
166         gl_appdata *ad = (gl_appdata *)data;
167         GL_CHECK_VAL(ad->maininfo.win, GL_UG_FAIL);
168         Evas_Object *win = ad->maininfo.win;
169         struct ug_cbs cbs;
170         struct ui_gadget *ug = NULL;
171         bundle *param = NULL;
172         const char *album_id = NULL;
173         gl_dbg("");
174
175         if (ad->uginfo.ug_called_by_me) {
176                 gl_dbg("Already exits some UG called by me");
177                 return GL_UG_FAIL;
178         }
179         memset(&cbs, 0x00, sizeof(struct ug_cbs));
180         int view_mode = gl_get_view_mode(ad);
181
182         cbs.layout_cb = _gl_ext_ug_layout_cb;
183         cbs.result_cb = NULL;
184         cbs.destroy_cb = _gl_ext_ug_destroy_cb;
185         cbs.priv = ad;
186
187         param = bundle_create();
188         GL_CHECK_VAL(param, GL_UG_FAIL);
189
190         UG_INIT_EFL(win, UG_OPT_INDICATOR_ENABLE);
191
192         gl_cluster *cur_album = ad->albuminfo.current_album;
193         GL_CHECK_VAL(cur_album, GL_UG_FAIL);
194         GL_CHECK_VAL(cur_album->cluster, GL_UG_FAIL);
195         GL_CHECK_VAL(cur_album->cluster->uuid, GL_UG_FAIL);
196
197         /* Local file */
198         bundle_add(param, GL_ARGV_IV_VIEW_MODE,
199                    GL_ARGV_IV_VIEW_GALLERY);
200         bundle_add(param, GL_ARGV_IV_PATH, cur_item->item->file_url);
201
202         char sequence_str[GL_INTERGER_LEN_MAX] = { 0, };
203         eina_convert_itoa(cur_item->sequence, sequence_str);
204         gl_dbg("sequence : %s", sequence_str);
205         bundle_add(param, GL_ARGV_IV_INDEX, sequence_str);
206
207         int grid_view_m = ad->gridinfo.grid_view_mode;
208
209         switch (view_mode) {
210                 case GL_VIEW_THUMBS:
211                         /* Type according to different seg. */
212                         if (grid_view_m == GL_CTRL_SEG_IMAGES) {
213                                 bundle_add(param, GL_ARGV_IV_MEDIA_TYPE,
214                                            GL_ARGV_IV_MEDIA_IMAGE);
215                                 gl_dbg("Media type: Image");
216                         } else if (grid_view_m == GL_CTRL_SEG_VIDEOS) {
217                                 bundle_add(param, GL_ARGV_IV_MEDIA_TYPE,
218                                            GL_ARGV_IV_MEDIA_VIDEO);
219                                 gl_dbg("Media type: Video");
220                         } else {
221                                 bundle_add(param, GL_ARGV_IV_MEDIA_TYPE,
222                                            GL_ARGV_IV_MEDIA_ALL);
223                                 gl_dbg("Media type: All");
224                         }
225                         break;
226                 case GL_VIEW_VIDEOLIST:
227                         bundle_add(param, GL_ARGV_IV_MEDIA_TYPE,
228                                    GL_ARGV_IV_MEDIA_VIDEO);
229                         gl_dbg("Media type: Video");
230                         break;
231                 default:
232                         bundle_add(param, GL_ARGV_IV_MEDIA_TYPE,
233                                    GL_ARGV_IV_MEDIA_ALL);
234                         gl_dbg("Media type: All");
235                         break;
236         }
237
238         if (!g_strcmp0(cur_album->cluster->uuid, GL_ALBUM_ALL_ID)) {
239                 /* 1.1, 'All' album->thumbnails view */
240                 bundle_add(param, GL_ARGV_IV_VIEW_BY,
241                            GL_ARGV_IV_VIEW_BY_ALL);
242                 gl_dbg("View By: All");
243         }
244         else {
245                 /* 1.2, Other albums->thumbnails view */
246                 bundle_add(param, GL_ARGV_IV_VIEW_BY,
247                            GL_ARGV_IV_VIEW_BY_FOLER);
248                 gl_dbg("View By: By Folder");
249         }
250
251         /* Pass album ID */
252         album_id = cur_album->cluster->uuid;
253         gl_dbg("album_id is %s!", album_id);
254         bundle_add(param, GL_ARGV_IV_ALBUM_INDEX, album_id);
255
256         /* Sort by type */
257         int sort_type = ad->uginfo.sort_type;
258         switch (sort_type) {
259                 case MINFO_MEDIA_SORT_BY_NAME_DESC:
260                         /* Sort by display name descending */
261                         bundle_add(param, GL_ARGV_IV_SORT_BY,
262                                    GL_ARGV_IV_SORT_NAMEDESC);
263                         gl_dbg("Name descending");
264                         break;
265                 case MINFO_MEDIA_SORT_BY_NAME_ASC:
266                         /* Sort by display name ascending */
267                         bundle_add(param, GL_ARGV_IV_SORT_BY,
268                                    GL_ARGV_IV_SORT_NAME);
269                         gl_dbg("Name ascending");
270                         break;
271                 case MINFO_MEDIA_SORT_BY_DATE_DESC:
272                         /* Sort by modified_date descending */
273                         bundle_add(param, GL_ARGV_IV_SORT_BY,
274                                    GL_ARGV_IV_SORT_DATEDESC);
275                         gl_dbg("Date descending");
276                         break;
277                 case MINFO_MEDIA_SORT_BY_DATE_ASC:
278                         /* Sort by modified_date ascending */
279                         bundle_add(param, GL_ARGV_IV_SORT_BY,
280                                    GL_ARGV_IV_SORT_DATE);
281                         gl_dbg("Date ascending");
282                         break;
283                 default:
284                         /* No Sort, use  MINFO_MEDIA_SORT_BY_NAME_ASC */
285                         bundle_add(param, GL_ARGV_IV_SORT_BY,
286                                    GL_ARGV_IV_SORT_NAME);
287                         gl_dbg("no sort, use default Name ascending");
288                         break;
289         }
290
291         ug = ug_create(NULL, GL_UG_PKG_IV, UG_MODE_FULLVIEW, param, &cbs);
292         ad->uginfo.ug_called_by_me = ug;
293         bundle_free(param);
294
295         if (ug) {
296                 return GL_UG_SUCCESS;
297         } else {
298                 gl_dbgE("ug_create failed!");
299                 return GL_UG_FAIL;
300         }
301 }
302
303 void
304 _gl_ext_gallery_ug_result_cb(struct ui_gadget *ug, bundle * result, void *priv)
305 {
306         GL_CHECK(priv);
307         gl_appdata *ad = (gl_appdata *)priv;
308         gl_dbg("");
309
310         /* Send result to caller */
311         const char *ret = NULL;
312         bundle *aul_result = NULL;
313         appsvc_create_result_bundle(ad->albuminfo.recv_bundle, &aul_result);
314
315         switch(ad->albuminfo.aul_launch_type)
316         {
317         case GL_AUL_T_SELECT_ALBUM:
318                 ret = bundle_get_val(result, GL_ARGV_SELECT_RETURN_FOLDER_PATH);
319                 if(ret)
320                 {
321                         gl_dbg("return folder-path: %s", ret);
322                         appsvc_add_data(aul_result, APPSVC_DATA_SELECTED, ret);
323                 }
324                 break;
325
326         case GL_AUL_T_SELECT_ONE:
327                 ret = bundle_get_val(result, GL_ARGV_SELECT_RETURN_PATH);
328                 if(ret)
329                 {
330                         gl_dbg("return path: %s", ret);
331                         appsvc_add_data(aul_result, APPSVC_DATA_SELECTED, ret);
332                 }
333                 break;
334
335         case GL_AUL_T_SELECT_MULTIPLE:
336                 ret = bundle_get_val(result, GL_ARGV_SELECT_RETURN_COUNT);
337                 if(ret)
338                 {
339                         gl_dbg("return count: %s", ret);
340                         appsvc_add_data(aul_result, GL_ARGV_SELECT_RETURN_COUNT, ret);
341                 }
342
343                 ret = bundle_get_val(result, GL_ARGV_SELECT_RETURN_PATH);
344                 if(ret)
345                 {
346                         gl_dbg("return path: %s", ret);
347                         appsvc_add_data(aul_result, APPSVC_DATA_SELECTED, ret);
348                 }
349                 break;
350
351         case GL_AUL_T_SELECT_SETAS:
352                 ret = bundle_get_val(result, GE_ARGV_SETAS_RETURN_PATH);
353                 if(ret)
354                 {
355                         gl_dbg("return image-path: %s", ret);
356                         appsvc_add_data(aul_result, GE_ARGV_SETAS_RETURN_PATH, ret);
357                 }
358
359                 ret = bundle_get_val(result, GL_ARGV_SETAS_RETURN_CROP_PATH);
360                 if(ret && ad->albuminfo.aul_setas_type == GL_AUL_SETAS_T_CALLER_ID)
361                 {
362                         gl_dbg("return image-path: %s", ret);
363                         appsvc_add_data(aul_result, GL_ARGV_SETAS_RETURN_CROP_PATH, ret);
364                 }
365                 break;
366
367         default:
368                 break;
369         }
370
371         if(ret)
372                 appsvc_send_result(aul_result, APPSVC_RES_OK);
373         else
374                 appsvc_send_result(aul_result, APPSVC_RES_CANCEL);
375
376         bundle_free(aul_result);
377 }
378
379 int
380 gl_ext_load_gallery_ug(void *data)
381 {
382         GL_CHECK_VAL(data, GL_UG_FAIL);
383         gl_appdata *ad = (gl_appdata *)data;
384         struct ug_cbs cbs;
385         struct ui_gadget *ug = NULL;
386         bundle *param = NULL;
387         Evas_Object *win = ad->maininfo.win;
388         int launch_type = 0;
389         int setas_type = 0;
390         int file_type = 0;
391         char *val = NULL;
392         char bd_str[GL_EXT_STR_BUNDLE_LEN] = { 0, };
393
394         if (ad->uginfo.gallery_ug_called_by_me)
395         {
396                 gl_dbg("Already exits some Gallery UG called by me, destroy it first!");
397                 ug_destroy(ad->uginfo.gallery_ug_called_by_me);
398                 ad->uginfo.gallery_ug_called_by_me = NULL;
399         }
400
401         memset(&cbs,0x00,sizeof(struct ug_cbs));
402
403         cbs.layout_cb = _gl_ext_ug_layout_cb;
404         cbs.result_cb = _gl_ext_gallery_ug_result_cb;
405         cbs.destroy_cb = _gl_ext_ug_destroy_cb;
406         cbs.priv = ad;
407
408         param = bundle_create();
409         if (!param)
410         {
411                 return GL_UG_FAIL;
412         }
413
414         UG_INIT_EFL(win, UG_OPT_INDICATOR_ENABLE);
415
416         /* add launch-type */
417         launch_type = ad->albuminfo.aul_launch_type;
418         gl_dbg("launch-type: %d", launch_type);
419         switch (launch_type)
420         {
421         case GL_AUL_T_SELECT_ONE:
422                 bundle_add(param, GL_ARGV_LAUNCH_TYPE, GL_ARGV_SELECT_ONE);
423                 break;
424
425         case GL_AUL_T_SELECT_MULTIPLE:
426                 bundle_add(param, GL_ARGV_LAUNCH_TYPE, GL_ARGV_SELECT_MULTIPLE);
427                 break;
428
429         case GL_AUL_T_SELECT_IMFT:
430                 bundle_add(param, GL_ARGV_LAUNCH_TYPE, GL_ARGV_SELECT_IMFT);
431
432                 snprintf(bd_str, sizeof(bd_str), "%d", ad->albuminfo.aul_contact_id);
433                 bundle_add(param, GL_ARGV_IMFT_CONTACT, bd_str);
434                 break;
435
436         case GL_AUL_T_SELECT_SETAS:
437                 bundle_add(param, GL_ARGV_LAUNCH_TYPE, GL_ARGV_SELECT_SETAS);
438
439                 setas_type =  ad->albuminfo.aul_setas_type;
440                 gl_dbg("setas-type: %d", setas_type);
441                 switch(setas_type)
442                 {
443                 case GL_AUL_SETAS_T_WALLPAPER:
444                         bundle_add(param, GL_ARGV_SETAS_TYPE, GL_ARGV_SETAS_WALLPALER);
445                         val = ad->albuminfo.aul_homescreen_path;
446                         if(val) {
447                                 gl_dbg("homescreen-path: %s", val);
448                                 bundle_add(param, GL_ARGV_HOMESCREEN_PATH, val);
449                         }
450
451                         val = ad->albuminfo.aul_lockscreen_path;
452                         if(val) {
453                                 gl_dbg("lockscreen-path: %s", val);
454                                 bundle_add(param, GL_ARGV_LOCKSCREEN_PATH, val);
455                         }
456
457                         break;
458
459                 case GL_AUL_SETAS_T_CALLER_ID:
460                         bundle_add(param, GL_ARGV_SETAS_TYPE, GL_ARGV_SETAS_CALLERID);
461                         val = ad->albuminfo.aul_callid_size;
462                         if(val) {
463                                 gl_dbg("callerid-size: %s", val);
464                                 bundle_add(param, GL_ARGV_CALLERID_SIZE, val);
465                         }
466
467                         val = ad->albuminfo.aul_callid_path;
468                         if(val) {
469                                 gl_dbg("callerid-path: %s", val);
470                                 bundle_add(param, GL_ARGV_CALLERID_PATH, val);
471                         }
472
473                         break;
474
475                 default:
476                         bundle_free(param);
477                         return GL_UG_FAIL;
478                 }
479
480                 break;
481
482         case GL_AUL_T_SELECT_ALBUM:
483                 bundle_add(param, GL_ARGV_LAUNCH_TYPE, GL_ARGV_SELECT_ALBUM);
484                 break;
485
486         default:
487                 bundle_free(param);
488                 return GL_UG_FAIL;
489         }
490
491         /* add file-type */
492         file_type = ad->albuminfo.aul_file_type;
493         gl_dbg("file-type: %d", file_type);
494         switch (file_type)
495         {
496         case GL_AUL_FILE_T_IMAGE:
497                 bundle_add(param, GL_ARGV_FILE_TYPE, GL_ARGV_FILE_TYPE_IMAGE);
498                 break;
499
500         case GL_AUL_FILE_T_VIDEO:
501                 bundle_add(param, GL_ARGV_FILE_TYPE, GL_ARGV_FILE_TYPE_VIDEO);
502                 break;
503
504         case GL_AUL_FILE_T_ALL:
505                 bundle_add(param, GL_ARGV_FILE_TYPE, GL_ARGV_FILE_TYPE_ALL);
506                 break;
507
508         default:
509                 bundle_free(param);
510                 return GL_UG_FAIL;
511         }
512
513         ug = ug_create(NULL, GL_UG_PKG_GALLERY, UG_MODE_FULLVIEW, param, &cbs);
514         bundle_free(param);
515
516         gl_dbg("ug_create: %p.", ug);
517         ad->uginfo.gallery_ug_called_by_me = ug;
518         if (ug) {
519                 Evas_Object *ug_ly = ug_get_layout(ug);
520                 if (ug_ly) {
521                         evas_object_raise(ug_ly);
522                 } else {
523                         gl_dbgE("ug_get_layout failed!");
524                         return GL_UG_FAIL;
525                 }
526                 return GL_UG_SUCCESS;
527         } else {
528                 gl_dbgE("ug_create failed!");
529                 return GL_UG_FAIL;
530         }
531 }
532
533 int
534 gl_ext_destroy_gallery_ug(void *data)
535 {
536         GL_CHECK_VAL(data, GL_UG_FAIL);
537         gl_dbg("");
538         gl_appdata *ad = (gl_appdata *)data;
539
540         /* For select appsvc, destory ug when HOME key is pressed */
541         if (ad->albuminfo.aul_launch_type != GL_AUL_T_NONE &&
542                 ad->albuminfo.aul_launch_type != GL_AUL_T_VIEW_ALBUM) {
543
544                 _gl_ext_ug_destroy_cb(ad->uginfo.gallery_ug_called_by_me, ad);
545         }
546
547         return GL_UG_SUCCESS;
548 }