Tizen 2.0 Release
[apps/core/preloaded/ug-image-viewer-efl.git] / main / src / view / ivug-slideshow-view.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://floralicense.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 "ivug-common.h"
18 #include "ivug-parameter.h"
19
20 #include "ivug-slideshow-view.h"
21
22 #include <vconf.h>
23 #include <vconf-keys.h>
24 #include <utilX.h>                      // Key event
25 #include <ui-gadget-module.h>   // ug_send_result
26
27 #include "ivug-filter.h"
28
29 #include "ivug-popup.h"
30
31 #include "EFLUtil.h"
32 #undef LOG_LVL
33 #define LOG_LVL DBG_MSG_LVL_MED
34
35 #undef LOG_CAT
36 #define LOG_CAT "IV-SLIDESHOW"
37
38
39 #define IVUG_SS_EDJ     EDJ_PATH"/ivug-slideshow-view.edj"
40
41
42 static void
43 _on_slideshow_finished(void *data, Evas_Object *obj, void *event_info);
44
45 static void
46 _send_result(ui_gadget_h ug, const char *key1, const char *val1, const char *key2, const char *val2)
47 {
48         ivug_ret_if(!ug);
49
50         service_h service;
51         service_create(&service);
52
53         if(key1 && val1)
54         {
55                 MSG_HIGH("Bundle 1 : [%s = %s]", key1, val1);
56                 service_add_extra_data(service, key1, val1);
57         }
58         if(key2 && val2)
59         {
60                 MSG_HIGH("Bundle 2 : [%s = %s]", key2, val2);
61                 service_add_extra_data(service, key2, val2);
62         }
63         ug_send_result(gGetUGHandle(), service);
64
65         service_destroy(service);
66 }
67
68
69 static bool _destory_slideshow_and_ug(Ivug_SlideShowView *pSSView,
70         int state,
71         bool bMmc_out)
72 {
73         IV_ASSERT(pSSView != NULL);
74         evas_object_smart_callback_del_full(ivug_ss_object_get(pSSView->ssHandle),
75                 "slideshow,finished", _on_slideshow_finished, pSSView);
76
77         ivug_allow_lcd_off();
78         /* send msg to caller */
79         if(state == SLIDE_SHOW_STOPPED)
80         {
81                 _send_result(gGetUGHandle(), "EXIT", "NORMAL", NULL, NULL);
82         }
83         /*from gallery ablum*/
84
85         if (pSSView->ssHandle )
86         {
87                 MSG_HIGH("image viewer end cause slide show ended");
88                 ivug_ss_delete(pSSView->ssHandle);
89         }
90         pSSView->ssHandle = NULL;
91
92         DESTROY_ME();
93         return true;
94 }
95
96 static Eina_Bool __stop_slideshow_cb(void *data)
97 {
98         Ivug_SlideShowView *pSSView = (Ivug_SlideShowView *)data;
99         if (pSSView && (pSSView->ssHandle)) {
100                 if(pSSView->homekey_idler)
101                 {
102                         ecore_idler_del(pSSView->homekey_idler);
103                         pSSView->homekey_idler = NULL;
104                 }
105                 ivug_ss_stop(pSSView->ssHandle);
106         }
107         return ECORE_CALLBACK_CANCEL;
108 }
109
110 static Eina_Bool _on_key_down(void *user_data, int type, void *event)
111 {
112         if (!user_data) {
113                 MSG_IMAGEVIEW_ERROR("user data is NULL");
114                 return ECORE_CALLBACK_PASS_ON;
115         }
116
117         Ivug_SlideShowView *pSSView = static_cast<Ivug_SlideShowView *>(user_data);
118
119         Ecore_Event_Key *key_event = (Ecore_Event_Key *) event;
120
121         if (!strcmp(key_event->keyname, KEY_END))
122         {
123                 MSG_IMAGEVIEW_HIGH("Bakc(End) key");
124         }
125         else if (!strcmp(key_event->keyname, KEY_SELECT))
126         {
127                 MSG_IMAGEVIEW_HIGH("Home key");
128                 /*home key,we need to stop slideshow also*/
129                 if (pSSView->homekey_idler == NULL) {
130                         pSSView->homekey_idler = ecore_idler_add(__stop_slideshow_cb, pSSView);
131                 }
132         }
133         else if (!strcmp(key_event->keyname, KEY_POWER))
134         {
135                 MSG_IMAGEVIEW_HIGH("Power key");
136                 if ( pSSView->ssHandle )
137                 {
138                         ivug_ss_stop(pSSView->ssHandle);
139                 }
140         }
141
142         MSG_IMAGEVIEW_LOW("Key down : %s", key_event->keyname);
143
144         return ECORE_CALLBACK_PASS_ON;
145 }
146
147 static void
148 _on_slideshow_finished(void *data, Evas_Object *obj, void *event_info)
149 {
150         MSG_HIGH("_on_slideshow_finished");
151         IV_ASSERT(data != NULL);
152
153         Ivug_SlideShowView *pSSView = static_cast<Ivug_SlideShowView *>(data);
154
155         int ss_state = (int)event_info;
156         bool bDestoryed = false;
157         bDestoryed = _destory_slideshow_and_ug(pSSView, ss_state, false);
158 }
159
160 static void _on_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
161 {
162         Ivug_SlideShowView *pSSView = static_cast<Ivug_SlideShowView *>(data);
163         IV_ASSERT(pSSView != NULL);
164
165         Evas_Coord x,y,w,h;
166         evas_object_geometry_get(obj, &x, &y, &w, &h);
167
168 /**
169 *  during slideshow, after rotation, resize cb of slideshow obj cannot be invoked,
170     so invoke it manually
171 */
172         if(pSSView->ssHandle)
173                 ivug_ss_resize(pSSView->ssHandle);
174
175         MSG_HIGH("SlideshowView resized geomtery XYWH(%d,%d,%d,%d) Rotate=%d", x, y, w, h, gGetRotationDegree());
176 }
177
178 void _ivug_slideshow_view_on_mmc_state_changed(keynode_t* node, void *data)
179 {
180         Ivug_SlideShowView *pSSView = static_cast<Ivug_SlideShowView *>(data);
181         IV_ASSERT(pSSView != NULL);
182
183         //ivug_timeout_popup_show(ivug_ss_object_get(pSSView->ssHandle), NULL, NULL, IDS_ERROR, IDS_SD_CARD_REMOVED);
184         ivug_ss_set_stop(pSSView->ssHandle);
185         _destory_slideshow_and_ug(pSSView, SLIDE_SHOW_STOPPED, true);
186 }
187
188 static char * _ivug_get_folder_name(char *filepath)
189 {
190         char *folder = ecore_file_dir_get(filepath);
191
192         char *name = NULL;
193
194         name = strrchr(folder, '/');
195         if ( (name != NULL) && ((name+1) != NULL) )
196         {
197                 return strdup(name + 1);
198         }
199
200         return NULL;
201 }
202
203
204 bool
205 ivug_slideshow_view_set_list(Ivug_SlideShowView *pSSView, ivug_parameter *ivug_param)
206 {
207         MSG_HIGH("Load media list.");
208
209         IV_ASSERT(pSSView != NULL);
210
211         PERF_CHECK_BEGIN(LVL2, "create media list");
212
213         Media_List *mlist = ivug_medialist_create();
214         if (mlist == NULL)
215         {
216                 MSG_ERROR("Creating media list failed");
217                 return false;
218         }
219         PERF_CHECK_END(LVL2, "create media list");
220
221         PERF_CHECK_BEGIN(LVL2, "create filter");
222
223         Filter_struct *filter = ivug_param_create_filter(ivug_param);
224         if (filter == NULL)
225         {
226                 MSG_ERROR("Creating filter failed");
227                 ivug_medialist_del(mlist);
228                 return false;
229         }
230
231         PERF_CHECK_END(LVL2, "create filter");
232
233         Media_Item *current = NULL;
234         Media_Data *pData = NULL;
235
236         {
237                 PERF_CHECK_BEGIN(LVL2, "media list load");
238                 current = ivug_medialist_load(mlist, filter);
239                 PERF_CHECK_END(LVL2, "media list load");
240         }
241
242         if(current == NULL)
243         {
244                 MSG_ERROR("Media list load failed");
245                 goto LOAD_LIST_FAILED;
246         }
247
248         pData = ivug_medialist_get_data(current);
249         if(pData == NULL)
250         {
251                 MSG_ERROR("current data is NULL");
252                 goto LOAD_LIST_FAILED;
253         }
254
255         if(pData->fileurl == NULL)
256         {
257                 MSG_ERROR("current fileurl is NULL");
258                 goto LOAD_LIST_FAILED;
259         }
260
261 #ifdef USE_RESCAN_FILE_PATH_AT_LIST
262         if(strncmp(pData->fileurl, ivug_param->filepath, strlen(pData->fileurl)) != 0)
263         {
264                 current = ivug_medialist_find_item_by_filename(mlist, ivug_param->filepath);
265                 if(current == NULL)
266                 {
267                         MSG_ERROR("Media list load failed, %s is not exist at list", ivug_param->filepath);
268                         goto LOAD_LIST_FAILED;
269                 }
270         }
271 #endif
272
273         if(ivug_param->view_by == IVUG_VIEW_BY_ALL)
274         {
275                 pSSView->album_name = strdup(IDS_ALL_ALBUMS);
276         }
277         else if(ivug_param->view_by == IVUG_VIEW_BY_FOLDER)
278         {
279                 /*
280                 media_handle m_handle = ivug_db_get_folder_handle(ecore_file_dir_get(ivug_param->filepath));
281                 if(m_handle == NULL)
282                 {
283                         MSG_IVUG_FATAL("View by Folder. but media handle is NULL" );
284                         return NULL;
285                 }
286
287                 pMainView->album_name = ivug_db_get_folder_name(m_handle);
288                 */
289                 pSSView->album_name = _ivug_get_folder_name(ivug_param->filepath);
290                 if(pSSView->album_name == NULL)
291                 {
292                         pSSView->album_name = strdup(IDS_NO_NAME);
293                 }
294         }
295
296         pSSView->mList = mlist;
297         PERF_CHECK_BEGIN(LVL2, "media list set");
298
299         pSSView->ss_curItem = current;
300
301         PERF_CHECK_END(LVL2, "media list set");
302
303         ivug_data_filter_delete(filter);
304
305         return true;
306
307 LOAD_LIST_FAILED:
308
309         if(filter)
310                 ivug_data_filter_delete(filter);
311
312         if(mlist)
313                 ivug_medialist_del(mlist);
314
315         pSSView->mList = NULL;
316         return false;
317 }
318
319 Ivug_SlideShowView *
320 ivug_slideshow_view_create(Evas_Object* parent, ivug_mode mode, ivug_view_by view_by)
321 {
322         IV_ASSERT(parent != NULL);
323
324         MSG_HIGH("Creating slideshow view. Parent=0x%08x Mode=%d ViewBy=%d", parent, mode, view_by);
325
326         PERF_CHECK_BEGIN(LVL2, "Create layout");
327
328 //create main view layout
329         Ivug_SlideShowView *pSSView = (Ivug_SlideShowView *)calloc(1, sizeof(Ivug_SlideShowView) );
330         IV_ASSERT(pSSView != NULL);
331
332 // Set default value
333         pSSView->parent = parent;
334         pSSView->view_by = view_by;
335
336         Evas_Object *layout;
337         layout = elm_layout_add(parent);
338         if ( layout == NULL)    //if failed
339         {
340                 MSG_ERROR("main layout create failed");
341                 free(pSSView);
342                 return NULL;
343         }
344
345         pSSView->layout = layout;
346         evas_object_name_set(pSSView->layout, "SlideShow Layout");
347
348 #if 0
349         if (elm_layout_file_set(layout, IVUG_SS_EDJ, "ssview") == EINA_FALSE)
350         {
351                 MSG_MAIN_ERROR("edj loading fail, file=%s group=%s", IVUG_SS_EDJ, IVUG_SS_EDJ);
352                 evas_object_del(layout);
353                 free(pSSView);
354                 return NULL;
355         }
356 #endif
357         evas_object_event_callback_add(pSSView->layout, EVAS_CALLBACK_RESIZE, _on_resize, pSSView);
358
359         pSSView->keydown_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _on_key_down, (void *)pSSView);
360
361         MSG_HIGH("Main View Created");
362
363         return pSSView;
364 }
365
366
367 Evas_Object *
368 ivug_slideshow_view_object_get(Ivug_SlideShowView *pSSView)
369 {
370         IV_ASSERT(pSSView != NULL);
371
372         return pSSView->layout;
373 }
374
375
376 void
377 ivug_slideshow_view_start(Ivug_SlideShowView *pSSView)
378 {
379         IV_ASSERT(pSSView != NULL);
380
381         MSG_HIGH("Slide show running");
382
383         Media_Item *current = pSSView->ss_curItem;
384
385         ivug_prohibit_lcd_off();
386         //ivug_main_view_hide_menu_bar(pMainView);
387
388         pSSView->ssHandle = ivug_ss_create(pSSView->layout);
389
390 // Register callback
391         evas_object_smart_callback_add(ivug_ss_object_get(pSSView->ssHandle),  "slideshow,finished", _on_slideshow_finished, pSSView);
392
393         elm_win_indicator_mode_set((Evas_Object *)ug_get_window(), ELM_WIN_INDICATOR_HIDE);
394
395         ivug_ss_start(pSSView->ssHandle, current, pSSView->mList, EINA_FALSE);
396
397 }
398
399
400
401 void
402 ivug_slideshow_view_resume(Ivug_SlideShowView *pSSView)
403 {
404         IV_ASSERT(pSSView != NULL);
405
406         MSG_HIGH("Slideshow View Resumed");
407
408         Media_Item *mitem = pSSView->ss_curItem;
409         Media_Data *mdata = ivug_medialist_get_data(mitem);
410
411         IV_ASSERT(mdata != NULL);
412
413         if (mdata->slide_type == SLIDE_TYPE_IMAGE || mdata->slide_type == SLIDE_TYPE_VIDEO )
414         {
415                 if(mdata->filepath && ecore_file_exists(mdata->filepath)) // Current file is not deleted
416                 {
417                         MSG_HIGH("Current filepath : %s", mdata->filepath);
418                 }
419                 else
420                 {
421                         /*if file is not exist ug terminated temporally */
422                         MSG_ERROR("Center file is not exist. stype=%d name=%s", mdata->slide_type, mdata->filepath);
423                         DESTROY_ME();
424                         return ;
425                 }
426         }
427         else
428         {
429                 MSG_ERROR("Unhandled slide type : %d", mdata->slide_type);
430         }
431 }
432
433
434 void
435 ivug_slideshow_view_pause(Ivug_SlideShowView *pSSView)
436 {
437         IV_ASSERT(pSSView != NULL);
438
439         if ( pSSView->ssHandle )
440         {
441                 ivug_ss_stop(pSSView->ssHandle);
442         }
443
444         // Stop photocam If AGIF
445 }
446
447
448 void
449 ivug_slideshow_view_destroy(Ivug_SlideShowView *pSSView)
450 {
451         IV_ASSERT(pSSView != NULL);
452
453         MSG_HIGH("ENTER : SlideShow View Destroy. pMainView=0x%08x", pSSView);
454
455         MSG_HIGH("Unregister system notifications");
456         if (pSSView->layout) {
457                 evas_object_del(pSSView->layout);
458                 pSSView->layout = NULL;
459         }
460         if(pSSView->keydown_handler)
461         {
462                 ecore_event_handler_del(pSSView->keydown_handler);
463                 pSSView->keydown_handler = NULL;
464         }
465         if(pSSView->homekey_idler)
466         {
467                 ecore_idler_del(pSSView->homekey_idler);
468                 pSSView->homekey_idler = NULL;
469         }
470
471         if ( pSSView->mList )
472         {
473                 MSG_MAIN_HIGH("Remove media list");
474                 ivug_medialist_del(pSSView->mList);             // ivug_medialist_del() is not working on destroy cb.
475                 pSSView->mList = NULL;
476         }
477         if(pSSView->album_name)
478         {
479                 free(pSSView->album_name);
480                 pSSView->album_name = NULL;
481         }
482         EFL::dump_obj(pSSView->parent, 0);
483         free(pSSView);
484         pSSView = NULL;
485         MSG_HIGH("LEAVE : SlideShow View Destroy.");
486
487         return ;
488 }
489
490