initial upload for tizen 2.0 beta
[apps/home/gallery.git] / src / gallery.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 <Evas.h>
18 #include "gallery.h"
19 #include "gl-debug.h"
20 #include "gl-albums.h"
21 #include "gl-gridview.h"
22 #include "gl-listview.h"
23 #include "gl-controlbar.h"
24 #include "gl-ui-util.h"
25 #include "gl-lang.h"
26 #include "gl-util.h"
27 #include "gl-data.h"
28 #include "gl-ext-ug-load.h"
29 #include "gl-ext-exec.h"
30 #include "gl-popup.h"
31 #include "gl-progressbar.h"
32 #include "gl-notify.h"
33 #include "gl-thread-util.h"
34 #include "gl-strings.h"
35
36
37 #define GL_BG_COLOR_DEFAULT 255
38
39 #define GL_BD_LAUNCH_TYPE "launch-type"
40 #define GL_BD_ALBUM_ID "album-id"
41 #define GL_BD_FILE_TYPE "file-type"
42 #define GL_BD_SETAS_TYPE "setas-type"
43 #define GL_BD_CALLERID_SIZE "callerid-size"
44 #define GL_BD_CALLERID_PATH "callerid-path"
45 #define GL_BD_HOMESCREEN_PATH "homescreen-path"
46 #define GL_BD_LOCKSCREEN_PATH "lockscreen-path"
47 #define GL_BD_SELECT_ALBUM "select-album"
48 #define GL_BD_SELECT_ONE "select-one"
49 #define GL_BD_SELECT_MULTIPLE "select-multiple"
50 #define GL_BD_SELECT_SETAS "select-setas"
51 #define GL_BD_FILE_TYPE_IMAGE "image"
52 #define GL_BD_FILE_TYPE_VIDEO "video"
53 #define GL_BD_FILE_TYPE_ALL "all"
54 #define GL_BD_SETAS_WALLPALER "wallpaper"
55 #define GL_BD_SETAS_CALLERID "callerid"
56
57 #define GL_MIME_SEP ":"
58 #define GL_MIME_TYPE_GALLERY "gallery.uri"
59 #define GL_SERVICE_OPERATION_WALLPAPER "http://tizen.org/appsvc/operation/wallpaper"
60
61 struct text_part {
62         char *part;
63         char *msgid;
64 };
65
66 static struct text_part main_txt[] = {
67         {"txt_title", "Application template",},
68         {"txt_mesg", "Click to exit",},
69 };
70
71 /* User-defined function */
72 static int _gallery_init_app_svc(void *data);
73
74 static void _gallery_win_del_cb(void *data, Evas_Object *obj, void *event_info)
75 {
76         gl_dbgW("_gallery_win_del_cb!!");
77         GL_CHECK(data);
78
79         elm_exit();
80 }
81
82 static int _gallery_update_ts(Evas_Object *eo, struct text_part *tp, int size)
83 {
84         int i = 0;
85
86         if (eo == NULL || tp == NULL || size < 0) {
87                 return -1;
88         }
89         for (i = 0; i < size; i++) {
90                 if (tp[i].part && tp[i].msgid) {
91                         edje_object_part_text_set(eo, tp[i].part,
92                                                   tp[i].msgid);
93                 }
94         }
95
96         return 0;
97 }
98
99 static void _gallery_lang_changed_cb(void *data)
100 {
101         GL_CHECK(data);
102         gl_appdata *ad = (gl_appdata *)data;
103         GL_CHECK(ad->maininfo.main_layout);
104
105         _gallery_update_ts(elm_layout_edje_get(ad->maininfo.main_layout),
106                            main_txt, sizeof(main_txt) / sizeof(main_txt[0]));
107         _gl_lang_update(data);
108 }
109
110 static void _gallery_low_battery_cb(void *data)
111 {
112         gl_dbg("");
113
114         /* ug_send_event(UG_EVENT_LOW_BATTERY); */
115         /* 0 : default handler, 1:user handler */
116 }
117
118 void _gallery_rotation_cb(app_device_orientation_e m, void *data)
119 {
120         GL_CHECK(data);
121         gl_dbg("Appcore Rotation mode: %d", m);
122         gl_appdata *ad = (gl_appdata *)data;
123         GL_CHECK(ad->maininfo.win);
124         enum ug_event evt = UG_EVENT_NONE;
125
126         ad->maininfo.rotate_mode = m;
127
128         ug_send_event(evt);
129
130         elm_win_rotation_with_resize_set(ad->maininfo.win, m);
131
132         int view_mode = gl_get_view_mode(ad);
133         if (ad->uginfo.ug_called_by_me) {
134                 /* when ug invoked, just return. */
135                 if (view_mode == GL_VIEW_THUMBS_EDIT ||
136                     view_mode == GL_VIEW_VIDEOLIST_EDIT) {
137                         _gl_notify_rotate_selinfo(ad);
138                 } else if (view_mode == GL_VIEW_ALBUMS) {
139                         _gl_notify_rotate_selinfo(ad);
140                 }
141                 return;
142         }
143
144         _gl_notify_rotate_selinfo(ad);
145 }
146
147 static Evas_Object *_gallery_create_win(void *data, const char *name)
148 {
149         gl_dbg_launch("  _gallery_create_win:start");
150         GL_CHECK_NULL(name);
151         GL_CHECK_NULL(data);
152         gl_appdata *ad = (gl_appdata *)data;
153         Evas_Object *eo = NULL;
154
155         gl_dbg_launch("    elm_win_add:start");
156         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
157         gl_dbg_launch("    elm_win_add:end");
158         if (eo) {
159                 elm_win_autodel_set(eo, EINA_TRUE);
160                 elm_win_title_set(eo, name);
161                 evas_object_smart_callback_add(eo, "delete,request",
162                                                _gallery_win_del_cb, ad);
163                 ad->maininfo.win_scale = elm_scale_get();
164                 gl_dbg("main window scale: %f", ad->maininfo.win_scale);
165         }
166
167         gl_dbg("_gallery_create_win:done");
168         gl_dbg_launch("  _gallery_create_win:end");
169         return eo;
170 }
171
172 static Evas_Object *_gallery_create_bg(Evas_Object *parent)
173 {
174         gl_dbg_launch("    _gallery_create_bg:start");
175         GL_CHECK_NULL(parent);
176         Evas_Object *bg = NULL;
177
178         gl_dbg_launch("      elm_bg_add:start");
179         bg = elm_bg_add(parent);
180         gl_dbg_launch("      elm_bg_add:end");
181         elm_bg_color_set(bg, GL_BG_COLOR_DEFAULT, GL_BG_COLOR_DEFAULT,
182                          GL_BG_COLOR_DEFAULT);
183
184         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND,
185                                          EVAS_HINT_EXPAND);
186         evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
187         /* elm_win_resize_object_add(parent, bg); */
188         evas_object_show(bg);
189         gl_dbg_launch("    _gallery_create_bg:end");
190
191         return bg;
192 }
193
194 static int _gallery_create_view_ly(gl_appdata *ad)
195 {
196         gl_dbg_launch("    _gallery_create_view_ly:start");
197         GL_CHECK_VAL(ad, -1);
198         GL_CHECK_VAL(ad->maininfo.naviframe, -1);
199
200         Evas_Object *layout = _gl_ui_create_view_ly(ad->maininfo.naviframe);
201         ad->albuminfo.layout = layout;
202
203         edje_object_signal_emit(_EDJ(layout), "elm,swallow_view,state,default",
204                                 "elm");
205
206         gl_ui_create_title_and_push(ad, ad->maininfo.naviframe,
207                                     ad->albuminfo.layout, GL_NAVI_ALBUMS,
208                                     GL_STR_CTRL_ITEM_ALBUMS);
209
210         gl_dbg_launch("    _gallery_create_view_ly:end");
211
212         return 0;
213 }
214
215 static Eina_Bool __gallery_activate_win_idler_cb(void *data)
216 {
217         GL_CHECK_CANCEL(data);
218         gl_appdata *ad = (gl_appdata *)data;
219
220         gl_dbgW("elm_win_activate - start!");
221         elm_win_activate(ad->maininfo.win);
222         gl_dbgW("elm_win_activate - done!");
223
224         if (ad->albuminfo.activate_win_idler) {
225                 ecore_idler_del(ad->albuminfo.activate_win_idler);
226                 ad->albuminfo.activate_win_idler = NULL;
227         }
228
229         return ECORE_CALLBACK_CANCEL;
230 }
231
232 static Eina_Bool _gallery_create_albums_idler_cb(void *data)
233 {
234         gl_dbg_launch("  _gallery_create_albums_idler_cb:start");
235         GL_CHECK_CANCEL(data);
236         gl_appdata *ad = (gl_appdata *)data;
237         gl_dbg("_gallery_create_albums_idler_cb:start");
238
239         if (ad->albuminfo.aul_launch_by_mime ||
240             ad->albuminfo.aul_launch_type == GL_AUL_T_VIEW_ALBUM) {
241                 gl_dbgW("\tIdler: From livebox, gallery is launched!");
242         } else {
243                 gl_dbgW("\tIdler: gallery is launched!");
244
245                 /* Initialize libmedia-info */
246                 _gl_data_init();
247                 gl_dbg("DB initialization:done");
248                 /* Get cluster list from libmedia-info */
249                 gl_dbg_launch("    _gl_data_get_cluster_list:start");
250                 _gl_data_get_cluster_list(ad);
251                 gl_dbg_launch("    _gl_data_get_cluster_list:end");
252                 GL_CHECK_FALSE(ad->albuminfo.cluster_list);
253                 gl_dbg("_gl_data_get_cluster_list:done");
254                 /* Set view mode */
255                 gl_set_view_mode(ad, GL_VIEW_ALBUMS);
256                 /**
257                 * Disbale 'edit' button if none editable albums
258                 * (albums except 'All albums'/'Camera shot') exist.
259                 */
260                 if (gl_check_gallery_empty(ad) ||
261                     ad->albuminfo.cluster_list->edit_cnt == 0)
262                         _gl_ctrl_disable_toolbar_item(ad, true, GL_NAVI_ALBUMS,
263                                                       false);
264                 /* Update albums view, append items  */
265                 gl_albums_update_items(ad);
266         }
267
268         gl_dbg("_gallery_create_albums_idler_cb:done");
269
270         if (ad->albuminfo.create_albums_idler) {
271                 ecore_idler_del(ad->albuminfo.create_albums_idler);
272                 ad->albuminfo.create_albums_idler = NULL;
273         }
274
275         gl_dbg_launch("  _gallery_create_albums_idler_cb:end");
276         return ECORE_CALLBACK_CANCEL;
277 }
278
279 static int _gallery_init_view(gl_appdata *ad)
280 {
281         gl_dbg_launch("  _gallery_init_view:start");
282         gl_dbg("");
283         GL_CHECK_VAL(ad, -1);
284
285         /* Base Layout */
286         ad->maininfo.main_layout = gl_ui_create_main_ly(ad->maininfo.win);
287         GL_CHECK_VAL(ad->maininfo.main_layout, -1);
288
289         /* Background. */
290         ad->maininfo.bg = _gallery_create_bg(ad->maininfo.main_layout);
291         GL_CHECK_VAL(ad->maininfo.bg, -1);
292
293         /* Pack bg to swallow */
294         elm_object_part_content_set(ad->maininfo.main_layout, "elm.swallow.bg",
295                                     ad->maininfo.bg);
296         /* Indicator */
297         elm_win_indicator_mode_set(ad->maininfo.win, ELM_WIN_INDICATOR_SHOW);
298         gl_dbg("gl_ui_create_main_ly: done");
299
300         /* Navigation Bar */
301         ad->maininfo.naviframe = gl_ui_create_navi_ly(ad->maininfo.main_layout);
302         GL_CHECK_VAL(ad->maininfo.naviframe, -1);
303
304         elm_object_part_content_set(ad->maininfo.main_layout, "elm.swallow.content",
305                                     ad->maininfo.naviframe);
306
307         if (gl_is_rotation_locked()) {
308                 gl_dbg("Rotation is in locked state!");
309         } else {
310                 int mode = APP_DEVICE_ORIENTATION_0;
311                 mode = app_get_device_orientation();
312                 ad->maininfo.rotate_mode = mode;
313         }
314
315         /* Albums view layout */
316         _gallery_create_view_ly(ad);
317
318         /* Add albums view and append nothing */
319         Evas_Object *layout_inner = NULL;
320         layout_inner = _gl_albums_add_gengrid(ad, ad->albuminfo.layout);
321         GL_CHECK_FALSE(layout_inner);
322         elm_object_part_content_set(ad->albuminfo.layout, "elm.swallow.view",
323                                     layout_inner);
324         ad->albuminfo.view = layout_inner;
325         ad->albuminfo.gallery_launched = true;
326
327         gl_dbg_launch("  _gallery_init_view:end");
328
329         return 0;
330 }
331
332 static bool _gallery_create(void *data)
333 {
334         gl_dbg_launch("create:start");
335         gl_dbgW("==Create==");
336         GL_CHECK_FALSE(data);
337         gl_appdata *ad = (gl_appdata *)data;
338         ad->maininfo.reentrant = TRUE;
339         Evas_Object *win = NULL;
340
341         elm_theme_extension_add(NULL, GL_EDJ_FILE);
342
343         /* create window */
344         win = _gallery_create_win(ad, PACKAGE);
345         GL_CHECK_FALSE(win);
346         ad->maininfo.win = win;
347
348         /* initialize gallery view */
349         if (_gallery_init_view(ad) != 0) {
350                 gl_dbgE("_gallery_init_view failed");
351                 return false;
352         }
353
354         gl_dbg_launch("  evas_object_show(win):start");
355         evas_object_show(win);
356         gl_dbg_launch("  evas_object_show(win):end");
357         gl_dbg("evas_object_show(win):done");
358         /* Register callbacks of DB update */
359         gl_dbg("system event cb:done");
360
361         gl_dbg_launch("  register callbacks:start");
362         gl_reg_db_update_noti(ad);
363         gl_dbg_launch("  register callbacks:end");
364
365         if (ad->albuminfo.create_albums_idler == NULL) {
366                 /* Use idler to load albums view */
367                 Ecore_Idler *idl = NULL;
368                 idl = ecore_idler_add(_gallery_create_albums_idler_cb, ad);
369                 ad->albuminfo.create_albums_idler = idl;
370                 gl_dbg("create_albums_idler added: 0x%x",
371                        ad->albuminfo.create_albums_idler);
372         }
373
374         gl_dbg_launch("create:end");
375         return true;
376 }
377
378 static int _gallery_close_app(void *data)
379 {
380         GL_CHECK_VAL(data, -1);
381         gl_appdata *ad = (gl_appdata *)data;
382
383         if (ad->albuminfo.create_albums_idler) {
384                 ecore_idler_del(ad->albuminfo.create_albums_idler);
385                 ad->albuminfo.create_albums_idler = NULL;
386         }
387
388         if (ad->albuminfo.activate_win_idler) {
389                 ecore_idler_del(ad->albuminfo.activate_win_idler);
390                 ad->albuminfo.activate_win_idler = NULL;
391         }
392
393         /* Destroy idler */
394         _gl_grid_del_append_idler(ad);
395         /* destroy rename album job.*/
396         if(ad->maininfo.rename_album_job) {
397                 ecore_job_del(ad->maininfo.rename_album_job);
398                 ad->maininfo.rename_album_job = NULL;
399         }
400
401         /* Destroy vibration timer */
402         if (ad->entryinfo.vibration_timer) {
403                 ecore_timer_del(ad->entryinfo.vibration_timer);
404                 ad->entryinfo.vibration_timer = NULL;
405         }
406         /* Free Ecore_Pipe object created */
407         if (ad->pbarinfo.sync_pipe) {
408                 ecore_pipe_del(ad->pbarinfo.sync_pipe);
409                 ad->pbarinfo.sync_pipe = NULL;
410         }
411
412         if (ad->pbarinfo.start_thread_timer) {
413                 ecore_timer_del(ad->pbarinfo.start_thread_timer);
414                 ad->pbarinfo.start_thread_timer = NULL;
415         }
416
417         if (ad->pbarinfo.del_pbar_idler) {
418                 ecore_idler_del(ad->pbarinfo.del_pbar_idler);
419                 ad->pbarinfo.del_pbar_idler = NULL;
420         }
421
422         /* Destroy timer for removing popup */
423         if (ad->popupinfo.del_timer) {
424                 ecore_timer_del(ad->popupinfo.del_timer);
425                 ad->popupinfo.del_timer = NULL;
426         }
427
428         /* Clear data in video list view */
429         _gl_list_clear_data(ad);
430         /* delete notification. */
431         _gl_notify_destroy_notiinfo(ad);
432
433         /* DB update callback unregister. */
434         gl_dereg_db_update_noti();
435
436         /* Free memory allocated for appsvc */
437         _gallery_init_app_svc(ad);
438
439         ug_destroy_all();
440
441         gl_albums_free_data(data);
442
443         /* disconnect with db */
444         _gl_data_finalize(ad);
445
446         gl_dbgW("==Exit, bye~ ==");
447
448         return 0;
449 }
450
451 static void _gallery_terminate(void *data)
452 {
453         gl_dbgW("==Terminate==");
454         GL_CHECK(data);
455         _gallery_close_app(data);
456 }
457
458 static void _gallery_pause(void *data)
459 {
460         gl_dbgW("==Pause==");
461         GL_CHECK(data);
462         gl_appdata *ad = (gl_appdata *)data;
463         ad->maininfo.reentrant = TRUE;
464
465         ug_pause();
466
467         /* For select appsvc, destory ug when HOME key is pressed */
468         gl_ext_destroy_gallery_ug(ad);
469 }
470
471 /* auto rotate view after resume if disabling/re-enabling rotate device */
472 static int __gallery_resume_update_rotation_mode(gl_appdata *ad)
473 {
474         GL_CHECK_VAL(ad, -1);
475         int display_mode = -1;
476         int device_mode = APP_DEVICE_ORIENTATION_0;
477
478         /**
479         *  display_mode is view mode, e.x. view is portrait normal
480         *  device_mode is target mode, e.x. device is portrait normal
481         */
482         display_mode = elm_win_rotation_get(ad->maininfo.win);
483         device_mode = app_get_device_orientation();
484         gl_dbg("display_mode is %d, device_mode is %d", display_mode,
485                device_mode);
486
487         /**
488         *  disable rotate when view isn't portrait normal, then after resume,
489         *  rotate view to portrait
490         */
491         if (gl_is_rotation_locked() &&
492             (display_mode != APP_DEVICE_ORIENTATION_0))
493                 _gallery_rotation_cb(APP_DEVICE_ORIENTATION_0, (void*)ad);
494         /**
495         *  re-enable rotate when device isn't in portrait normal
496         *  and view is in portrait normal, then after resume,
497         *  rotate view by device_mode
498         */
499         else if (!gl_is_rotation_locked() && (device_mode != display_mode))
500                 _gallery_rotation_cb(device_mode, (void*)ad);
501         return 0;
502 }
503
504 static void _gallery_resume(void *data)
505 {
506         gl_dbg_launch("resume:start");
507         gl_dbgW("==Resume==");
508         GL_CHECK(data);
509         gl_appdata *ad = (gl_appdata *)data;
510
511         ug_resume();
512         /* Video palyer was close when pause Gallery */
513         ad->uginfo.app_called_by_me = false;
514
515         /* Show cursor and IMF if not popup showed */
516         if (ad->entryinfo.editfield && ad->popupinfo.popup == NULL) {
517                 gl_dbg("Show Entry cursor and IMF.");
518                 evas_object_show(ad->entryinfo.imf_entry);
519                 elm_object_focus_set(ad->entryinfo.imf_entry, EINA_TRUE);
520                 Ecore_IMF_Context *imf = ad->entryinfo.imf_context;
521                 ecore_imf_context_input_panel_show(imf);
522         }
523
524         /* Update view */
525         gl_update_view(ad, GL_UPDATE_VIEW_NORMAL);
526
527         __gallery_resume_update_rotation_mode(ad);
528
529         gl_dbg_launch("resume:done");
530 }
531
532 static int __gallery_reset_app(void *data, const char *cluster_id,
533                                const char *aul_type)
534 {
535         GL_CHECK_VAL(data, -1);
536         gl_appdata *ad = (gl_appdata *)data;
537         GL_CHECK_VAL(ad->maininfo.win, -1);
538         int view_mode = gl_get_view_mode(ad);
539         gl_dbg("");
540
541         if (cluster_id == NULL || aul_type == NULL) {
542                 gl_dbgE("Invalid aul parameters!");
543                 goto GALLERY_RESET_FAILED;
544         }
545
546         if (ad->pbarinfo.sync_pipe) {
547                 gl_dbgW("Cancel move/delete pthread/Pipe!");
548                 /* Set cancel state to make child thread exit */
549                 gl_thread_set_cancel_state(ad, GL_PB_CANCEL_RESET);
550                 return -1;
551         }
552
553         if (view_mode == GL_VIEW_NONE) {
554                 /* First launch gallery then show shortcut album*/
555                 _gl_data_init();
556                 gl_dbg("Launch gallery at the first time");
557                 _gl_data_get_cluster_list(ad);
558                 GL_CHECK_VAL(ad->albuminfo.cluster_list, -1);
559                 /* Set view mode */
560                 gl_set_view_mode(ad, GL_VIEW_ALBUMS);
561         } else {
562                 gl_dbg("In Albums tab");
563                 if (_gl_albums_reset_view(ad, cluster_id, aul_type) < 0)
564                         goto GALLERY_RESET_FAILED;
565         }
566
567         /* Remove idler for other tabbar item */
568         if (ad->albuminfo.create_albums_idler) {
569                 gl_dbgW("Remove album creation idler!");
570                 ecore_idler_del(ad->albuminfo.create_albums_idler);
571                 ad->albuminfo.create_albums_idler = NULL;
572         }
573
574         elm_win_activate(ad->maininfo.win);
575         return 0;
576
577  GALLERY_RESET_FAILED:
578
579         elm_win_activate(ad->maininfo.win);
580         return -1;
581 }
582
583 /* init appsvc launch related global variables */
584 static int _gallery_init_app_svc(void *data)
585 {
586         GL_CHECK_VAL(data, -1);
587         gl_dbg("");
588         gl_appdata *ad = (gl_appdata *)data;
589
590         ad->albuminfo.aul_launch_type = GL_AUL_T_NONE;
591         ad->albuminfo.aul_file_type = GL_AUL_FILE_T_NONE;
592         ad->albuminfo.aul_setas_type = GL_AUL_SETAS_T_NONE;
593         if(ad->albuminfo.aul_id) {
594                 free(ad->albuminfo.aul_id);
595                 ad->albuminfo.aul_id = NULL;
596         }
597         if(ad->albuminfo.aul_type) {
598                 free(ad->albuminfo.aul_type);
599                 ad->albuminfo.aul_type = NULL;
600         }
601
602         ad->albuminfo.aul_launch_by_mime = false;
603
604         if(ad->albuminfo.recv_service) {
605                 service_destroy(ad->albuminfo.recv_service);
606                 ad->albuminfo.recv_service = NULL;
607         }
608
609         if(ad->albuminfo.aul_callid_size) {
610                 free(ad->albuminfo.aul_callid_size);
611                 ad->albuminfo.aul_callid_size = NULL;
612         }
613         if(ad->albuminfo.aul_callid_path) {
614                 free(ad->albuminfo.aul_callid_path);
615                 ad->albuminfo.aul_callid_path = NULL;
616         }
617         if(ad->albuminfo.aul_homescreen_path) {
618                 free(ad->albuminfo.aul_homescreen_path);
619                 ad->albuminfo.aul_homescreen_path = NULL;
620         }
621         if(ad->albuminfo.aul_lockscreen_path) {
622                 free(ad->albuminfo.aul_lockscreen_path);
623                 ad->albuminfo.aul_lockscreen_path = NULL;
624         }
625         return 0;
626 }
627
628 static int _gallery_parse_launch_type(service_h service, void *data)
629 {
630         GL_CHECK_VAL(service, -1);
631         GL_CHECK_VAL(data, -1);
632         gl_appdata *ad = (gl_appdata *)data;
633
634         gl_aul_launch_type launch_type = GL_AUL_T_NONE;
635         char *launch_type_str = NULL;
636         char *setas_type_str = NULL;
637         gl_dbg("");
638
639         /* get launch-type */
640         service_get_extra_data(service, GL_BD_LAUNCH_TYPE, &launch_type_str);
641         if (launch_type_str == NULL) {
642                 gl_dbg("set to default launch type: select-one");
643                 ad->albuminfo.aul_launch_type = GL_AUL_T_SELECT_ONE;
644                 return 0;
645         } else {
646                 gl_dbg("launch-type: %s", launch_type_str);
647         }
648
649         if(!strcasecmp(launch_type_str, GL_BD_SELECT_ALBUM)) {
650
651                 launch_type = GL_AUL_T_SELECT_ALBUM;
652         } else  if(!strcasecmp(launch_type_str, GL_BD_SELECT_ONE)) {
653
654                 launch_type = GL_AUL_T_SELECT_ONE;
655         } else  if(!strcasecmp(launch_type_str, GL_BD_SELECT_MULTIPLE)) {
656
657                 launch_type = GL_AUL_T_SELECT_MULTIPLE;
658         } else  if(!strcasecmp(launch_type_str, GL_BD_SELECT_SETAS)) {
659                 launch_type = GL_AUL_T_SELECT_SETAS;
660
661                 /* get setas-type */
662                 service_get_extra_data(service, GL_BD_SETAS_TYPE, &setas_type_str);
663                 if(setas_type_str == NULL) {
664                         GL_FREE_MEM(launch_type_str);
665                         return 0;
666                 }
667                 gl_dbg("setas-type: %s", setas_type_str);
668
669                 gl_aul_setas_type setas_type = GL_AUL_SETAS_T_NONE;
670                 char *path = NULL;
671                 char *size = NULL;
672
673                 if (!strcasecmp(setas_type_str, GL_BD_SETAS_WALLPALER)) {
674
675                         setas_type = GL_AUL_SETAS_T_WALLPAPER;
676
677                         /* get homscreen-path */
678                         service_get_extra_data(service, GL_BD_HOMESCREEN_PATH,
679                                                &path);
680                         if (path) {
681                                 gl_dbg("homescreen-path: %s", path);
682                                 ad->albuminfo.aul_homescreen_path = path;
683                                 path = NULL;
684                         }
685                         /* get lockscreen-path */
686                         service_get_extra_data(service, GL_BD_LOCKSCREEN_PATH,
687                                                &path);
688                         if (path) {
689                                 gl_dbg("lockscreen-path: %s", path);
690                                 ad->albuminfo.aul_lockscreen_path = path;
691                                 path = NULL;
692                         }
693                 } else if (!strcasecmp(setas_type_str, GL_BD_SETAS_CALLERID)) {
694
695                         setas_type = GL_AUL_SETAS_T_CALLER_ID;
696                         /* get callerid-size */
697                         service_get_extra_data(service, GL_BD_CALLERID_SIZE,
698                                                &size);
699                         if (size) {
700                                 gl_dbg("callerid-size: %s", size);
701                                 ad->albuminfo.aul_callid_size = size;
702                         }
703                         /* get callerid-path */
704                         service_get_extra_data(service, GL_BD_CALLERID_PATH,
705                                                &path);
706                         if (path) {
707                                 gl_dbg("callerid-path: %s", path);
708                                 ad->albuminfo.aul_callid_path = path;
709                         }
710                 }
711
712                 ad->albuminfo.aul_setas_type = setas_type;
713                 GL_FREE_MEM(setas_type_str);
714         }
715
716         ad->albuminfo.aul_launch_type = launch_type;
717         GL_FREE_MEM(launch_type_str);
718         return 0;
719 }
720
721 /* analysis parameters */
722 static int _gallery_parse_param(service_h service, void *data)
723 {
724         GL_CHECK_VAL(service, -1);
725         GL_CHECK_VAL(data, -1);
726         gl_dbg("");
727         gl_appdata *ad = (gl_appdata *)data;
728         char *operation = NULL;
729         char *uri = NULL;
730         char *mime = NULL;
731
732         /* init */
733         _gallery_init_app_svc(ad);
734
735         /* save current service handle */
736         service_clone(&ad->albuminfo.recv_service, service);
737
738         service_get_operation(service, &operation);
739         gl_dbg("operation: %s", operation);
740
741         if(!strcasecmp(operation, SERVICE_OPERATION_DEFAULT)) {
742                 char *mime_type_str = NULL;
743                 service_get_mime(service, &mime_type_str);
744                 if(mime_type_str == NULL) {
745                         gl_dbg("mime_type NULL");
746                         return -1;
747                 }
748                 gl_dbg("mime_type: %s", mime_type_str);
749
750                 if(!g_strcmp0(mime_type_str, GL_MIME_TYPE_GALLERY)) {
751                         char *content = NULL;
752                         GL_CHECK_VAL(content, -1);
753                         gl_dbg("content: %s", content);
754
755                         char *szID = NULL;
756                         char *str_buf = NULL;
757                         szID = strtok_r(content, GL_MIME_SEP, &str_buf);
758                         free(content);
759                         content = NULL;
760                         szID = strtok_r(NULL, GL_MIME_SEP, &str_buf);
761                         GL_CHECK_VAL(szID, -1);
762                         ad->albuminfo.aul_id = strdup(szID);
763                         ad->albuminfo.aul_launch_by_mime = true;
764                         gl_dbg("album_id: %s", ad->albuminfo.aul_id);
765                 }
766                 return 0;
767         }
768
769         service_get_uri(service, &uri);
770         if(uri) {
771                 gl_dbg("uri: %s", uri);
772                 char *str_pre = NULL;
773                 char *str_suf = NULL;
774                 str_pre = strtok_r(uri, GL_MIME_SEP, &str_suf);
775                 gl_dbg("str_pre: %s, str_suf: %s", str_pre, str_suf);
776                 GL_CHECK_VAL(str_pre, -1);
777                 GL_CHECK_VAL(str_suf, -1);
778                 str_pre = strtok_r(str_suf, GL_MIME_SEP, &str_suf);
779                 gl_dbg("str_pre: %s, str_suf: %s", str_pre, str_suf);
780                 GL_CHECK_VAL(str_pre, -1);
781                 GL_CHECK_VAL(str_suf, -1);
782                 ad->albuminfo.aul_id = strdup(str_suf);
783                 ad->albuminfo.aul_type = strdup(str_pre);
784                 ad->albuminfo.aul_launch_by_mime = true;
785                 gl_dbg("Type: %s, ID: %s", ad->albuminfo.aul_type,
786                        ad->albuminfo.aul_id);
787                 return 0;
788         }
789
790         /* OPERATION_VIEW */
791         if(!strcasecmp(operation, SERVICE_OPERATION_VIEW)) {
792                 /* ug destroy */
793                 gl_ext_destroy_gallery_ug(ad);
794
795                 ad->albuminfo.aul_launch_type = GL_AUL_T_VIEW_ALBUM;
796
797                 /* get file-type from service */
798                 char *file_type_str = NULL;
799                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_ALL; /* default */
800                 service_get_extra_data(service, GL_BD_FILE_TYPE, &file_type_str);
801                 if(file_type_str) {
802
803                         gl_dbg("file type: %s", file_type_str);
804                         if (!g_strcmp0(file_type_str, GL_BD_FILE_TYPE_IMAGE)) {
805                                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_IMAGE;
806                         } else if (!g_strcmp0(file_type_str, GL_BD_FILE_TYPE_VIDEO)) {
807                                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_VIDEO;
808                         }
809                         GL_FREE_MEM(file_type_str);
810                 }
811                 /* get album-id */
812                 char *album_id_str = NULL;
813                 service_get_extra_data(service, GL_BD_ALBUM_ID, &album_id_str);
814
815                 if(album_id_str) {
816                         gl_dbg("album-id: %s", album_id_str);
817                         ad->albuminfo.aul_id = album_id_str;
818                 } else {
819                         gl_dbg("album-id is invalid, set to default (ALL)");
820                         ad->albuminfo.aul_id = strdup(GL_ALBUM_ALL_ID);
821                 }
822         } else if(!strcasecmp(operation, SERVICE_OPERATION_PICK)) {
823                 /* OPERATION_PICK */
824
825                 /* get file-type from mime */
826                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_ALL; /* default */
827                 service_get_mime(service, &mime);
828                 if(mime) {
829
830                         gl_dbg("mime: %s", mime);
831                         if (!g_strcmp0(mime, GL_MIME_IMAGE_ALL)) {
832                                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_IMAGE;
833                         } else if (!g_strcmp0(mime, GL_MIME_VIDEO_ALL)) {
834                                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_VIDEO;
835                         }
836                 }
837
838                 _gallery_parse_launch_type(service, ad);
839         } else if (!strcasecmp(operation, GL_SERVICE_OPERATION_WALLPAPER)) {
840                 ad->albuminfo.aul_file_type = GL_AUL_FILE_T_IMAGE;
841                 ad->albuminfo.aul_launch_type = GL_AUL_T_SELECT_SETAS;
842                 ad->albuminfo.aul_setas_type = GL_AUL_SETAS_T_CROP_WALLPAPER;
843
844                 char *path = NULL;
845                 service_get_extra_data(service, GL_BD_HOMESCREEN_PATH, &path);
846                 if (path) {
847                         gl_dbg("homescreen-path: %s", path);
848                         ad->albuminfo.aul_homescreen_path = path;
849                 }
850         }
851
852         return 0;
853 }
854
855 static void _gallery_reset(service_h service, void *data)
856 {
857         gl_dbgW("==Reset==");
858         GL_CHECK(data);
859         gl_appdata *ad = (gl_appdata *)data;
860         GL_CHECK(ad->maininfo.win);
861
862         if (ad->maininfo.reentrant) {
863                 ad->maininfo.reentrant = FALSE;
864         } else {
865                 gl_dbgW("Gallery reentrant is not allowed, return!");
866                 return;
867         }
868
869         /* Destroy gallery ug */
870         if (ad->uginfo.gallery_ug_called_by_me) {
871                 gl_dbgW("Destroy Gallery UG launched, show App view!");
872                 ug_destroy(ad->uginfo.gallery_ug_called_by_me);
873                 ad->uginfo.gallery_ug_called_by_me = NULL;
874         }
875
876         /* Parse parameters */
877         if(_gallery_parse_param(service, data) != 0) {
878                 if (ad->albuminfo.aul_launch_by_mime == false &&
879                     ad->albuminfo.aul_launch_type == GL_AUL_T_NONE) {
880 /*@@ 1. Launch Gallery from menu screen/Smart search app, albums view showed @@*/
881                         int view_m = gl_get_view_mode(ad);
882                         gl_dbg("view_m: %d", view_m);
883                         /* Launch Gallery at the first time */
884                         if (view_m == GL_VIEW_NONE) {
885                                 gl_dbg("Set view mode as GL_VIEW_ALBUMS");
886                                 gl_set_view_mode(ad, GL_VIEW_ALBUMS);
887                         }
888                 } else {
889                         gl_dbgE("Failed to parse parameters!");
890                 }
891                 /* Rotate window if it's not APP_DEVICE_ORIENTATION_0 mode */
892                 if (!gl_is_rotation_locked()) {
893                         int mode = APP_DEVICE_ORIENTATION_0;
894                         mode = app_get_device_orientation();
895                         if (mode != APP_DEVICE_ORIENTATION_0) {
896                                 gl_dbg("Rotation window!");
897                                 _gallery_rotation_cb(mode, ad);
898                         }
899                 }
900                 elm_win_activate(ad->maininfo.win);
901                 return;
902         }
903
904         if (ad->albuminfo.aul_launch_by_mime ||
905             ad->albuminfo.aul_launch_type == GL_AUL_T_VIEW_ALBUM) {
906 /*@@ 2. Launch Gallery from shortcut @@*/
907                 __gallery_reset_app(data, ad->albuminfo.aul_id,
908                                     ad->albuminfo.aul_type);
909         } else {
910 /*@@ 3. Load Gallery UG @@*/
911                 gl_dbg("Start loading Gallery UG");
912                 if (gl_ext_load_gallery_ug(ad) == GL_UG_SUCCESS) {
913                         if (gl_get_view_mode(ad) == GL_VIEW_NONE) {
914                                 gl_dbg("elm_win_activate");
915                                 elm_win_activate(ad->maininfo.win);
916                         } else {
917                                 if (ad->albuminfo.activate_win_idler) {
918                                         ecore_idler_del(ad->albuminfo.activate_win_idler);
919                                         ad->albuminfo.activate_win_idler = NULL;
920                                 }
921                                 /* To show UG faster then app */
922                                 Ecore_Idler *idler = NULL;
923                                 idler = ecore_idler_add(__gallery_activate_win_idler_cb,
924                                                         ad);
925                                 ad->albuminfo.activate_win_idler = idler;
926                         }
927                 }
928         }
929 }
930
931 int gallery_reset_app(void *data)
932 {
933         GL_CHECK_VAL(data, -1);
934         gl_appdata *ad = (gl_appdata *)data;
935         gl_dbg("Reset gallery[cluster ID: %s]", ad->albuminfo.aul_id);
936         return __gallery_reset_app(data, ad->albuminfo.aul_id,
937                                    ad->albuminfo.aul_type);
938 }
939
940 int main(int argc, char *argv[])
941 {
942         gl_dbg_launch("main:start");
943         gl_dbgW("==Gallery==");
944         gl_appdata gl_ad;
945         int ret;
946
947         app_event_callback_s event_callback;
948
949         event_callback.create = _gallery_create;
950         event_callback.terminate = _gallery_terminate;
951         event_callback.pause = _gallery_pause;
952         event_callback.resume = _gallery_resume;
953         event_callback.service = _gallery_reset;
954         event_callback.low_memory = NULL;
955         event_callback.low_battery = _gallery_low_battery_cb;
956         event_callback.device_orientation = _gallery_rotation_cb;
957         event_callback.language_changed = _gallery_lang_changed_cb;
958         event_callback.region_format_changed = NULL;
959
960         memset(&gl_ad, 0x00, sizeof(gl_appdata));
961
962         ret = app_efl_main(&argc, &argv, &event_callback, &gl_ad);
963
964         gl_dbg_launch("main:done");
965         return ret;
966 }
967