79b89a53fd56cc92cf504f1fa4508fe2d5f1e360
[apps/home/gallery.git] / libug / libug-gallery-efl / src / ge-ui-util.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 "ge-ui-util.h"
18 #include "ge-util.h"
19 #include "ge-ext-ug-load.h"
20 #include "ge-button.h"
21 #include "ge-gridview.h"
22 #include "ge-albums.h"
23 #include "ge-debug.h"
24 #include "ge-db-handler.h"
25 #include "ge-strings.h"
26
27
28 #define HOME_ALBUM_COVER_NUM 1
29 #define GE_NOBUT_EXIT_POPUP_HIDE_TIME_DELAY 3.0
30 #define GE_SELINFO_HIDE_TIME_DELAY 3.0
31 #define GE_CTRLBAR_STYLE_NAVIFRAME "naviframe"
32 #define GE_BUTTON_STYLE_NAVIBAR_PRE "naviframe/back_btn/gallery_efl"
33 #define GE_NAVIFRAME_PREB_BTN "prev_btn" //ELM_NAVIFRAME_ITEM_PREV_BTN
34 #define GE_NAVIFRAME_CONTROLBAR "controlbar" //ELM_NAVIFRAME_ITEM_CONTROLBAR
35
36 static void _ge_ui_response_cb(void *data, Evas_Object *obj, void *event_info)
37 {
38         ge_dbg("event_info: %d", (int)event_info);
39         GE_CHECK(data);
40         ge_ugdata *ugd = (ge_ugdata *)data;
41         ge_dbg("popup mode: %d", ugd->popup_mode);
42
43         if(ugd->popup_mode == GE_Popup_UgExit)
44         {
45                 ug_destroy_me(ugd->ug);
46         }
47
48         evas_object_del(ugd->popup);
49         ugd->popup = NULL;
50 }
51
52 static Eina_Bool __ge_popup_timeout_cb(void *data)
53 {
54         GE_CHECK_FALSE(data);
55         ge_ugdata *ugd = (ge_ugdata *)data;
56         if (ugd->del_timer) {
57                 ecore_timer_del(ugd->del_timer);
58                 ugd->del_timer = NULL;
59         }
60
61         if (ugd->popup) {
62                 evas_object_del(ugd->popup);
63                 ugd->popup = NULL;
64         }
65         return ECORE_CALLBACK_CANCEL;
66 }
67
68 static int __ge_popup_add_timer(void *data, double to_inc)
69 {
70         GE_CHECK_VAL(data, -1);
71         ge_ugdata *ugd = (ge_ugdata *)data;
72         if (ugd->del_timer) {
73                 ecore_timer_del(ugd->del_timer);
74                 ugd->del_timer = NULL;
75         }
76
77         ugd->del_timer = ecore_timer_add(to_inc, __ge_popup_timeout_cb, data);
78         return 0;
79 }
80
81 static void __ge_ui_done_cb(void *data, Evas_Object *obj, void *event_info)
82 {
83         ge_dbg("");
84         GE_CHECK(data);
85         ge_ugdata *ugd = (ge_ugdata *)data;
86         int i;
87
88         if(ugd->file_select_mode) //file selection mode
89         {
90                 ge_item* sel_item = NULL;
91                 char* filepath_arg = NULL;
92                 int sel_cnt = ge_db_selected_list_count();
93
94                 if(sel_cnt == 0)
95                 {
96                         ge_dbg("No thumbs selected, return!");
97                         ge_ui_create_popup(ugd, GE_Popup_NoButton, GE_POPUP_NO_FILES_SELECTED);
98                         return;
99                 }
100
101                 if(ugd->file_select_mode == GE_File_Select_Multiple)
102                 {
103                         GString* selected_path = g_string_new(NULL);
104
105                         for( i = 1; i <= sel_cnt; i++ )
106                         {
107                                 ge_db_get_selected_item_by_index(&sel_item, i);
108                                 if(sel_item && sel_item->item)
109                                 {
110                                         ge_dbg("Selected [%s]", sel_item->item->file_url);
111                                         g_string_append(selected_path, sel_item->item->file_url);
112                                         g_string_append_c(selected_path, ';');
113                                 }
114                         }
115                         int str_len = strlen(selected_path->str);
116                         ge_dbg("path string length = %d", str_len);
117                         g_string_truncate(selected_path, str_len - 1);
118
119                         filepath_arg = g_string_free(selected_path, false);
120
121                         char t_str[32] = {0,};
122                         eina_convert_itoa(sel_cnt, t_str);
123
124                         bundle_add(ugd->bundle, GE_FILE_SELECT_RETURN_COUNT, t_str);
125                         bundle_add(ugd->bundle, GE_FILE_SELECT_RETURN_PATH, filepath_arg);
126                         ug_send_result(ugd->ug, ugd->bundle);
127
128                         if(filepath_arg)
129                         {
130                                 g_free(filepath_arg);
131                                 filepath_arg = NULL;
132                         }
133                 }
134
135                 ge_db_selected_list_finalize();
136
137                 ug_destroy_me(ugd->ug);
138         }
139 }
140
141 static void _ge_ui_selinfo_destroy_cb(void *data, Evas_Object *obj, void *event_info)
142 {
143         GE_CHECK(data);
144         ge_ugdata *ad = (ge_ugdata *)data;
145
146         ge_ui_destroy_selinfo(ad);
147 }
148
149 static int __ge_ui_disable_done_item(ge_ugdata *ugd, bool disabled)
150 {
151         ge_dbg("Disable Done item(%d)?", disabled);
152         GE_CHECK_VAL(ugd, -1);
153         GE_CHECK_VAL(ugd->thumbs_nf_it, -1);
154
155         Evas_Object *cbar = NULL;
156         cbar = elm_object_item_part_content_get(ugd->thumbs_nf_it,
157                                                 GE_NAVIFRAME_CONTROLBAR);
158         GE_CHECK_VAL(cbar, -1);
159         Elm_Object_Item *cbar_it = NULL;
160         cbar_it = elm_toolbar_first_item_get(cbar);
161         GE_CHECK_VAL(cbar_it, -1);
162         elm_object_item_disabled_set(cbar_it, disabled);
163         return 0;
164 }
165
166 static int __ge_ui_append_object_item(Evas_Object *cbar, Evas_Object *object)
167 {
168         Elm_Object_Item *item = elm_toolbar_item_append(cbar, NULL, NULL, NULL, NULL);
169         if (object)
170                 elm_object_item_part_content_set(item, "object", object);
171         else
172                 elm_object_item_disabled_set(item, EINA_TRUE);
173         return 0;
174 }
175
176 static void _ge_ui_destroy_me_cb(void *data, Evas_Object *obj, void *event_info)
177 {
178         ge_dbg("");
179         GE_CHECK(data);
180         ge_ugdata *ugd = (ge_ugdata *)data;
181
182         if (_ge_get_view_mode() != GE_AlbumsView_Mode) {
183                 if(ugd->file_select_mode == GE_File_Select_Multiple) {
184                         ge_ui_destroy_selinfo(ugd);
185                         ge_db_selected_list_finalize();
186                 }
187                 ge_grid_clear_view(ugd);
188         }
189
190         bundle_add(ugd->bundle, GE_FILE_SELECT_RETURN_QUIT, GE_FILE_SELECT_QUIT_STATE);
191         ug_send_result(ugd->ug, ugd->bundle);
192
193         ug_destroy_me(ugd->ug);
194 }
195
196 static void _ge_ui_back_to_albums_cb(void *data, Evas_Object *obj, void *event_info)
197 {
198         ge_dbg("");
199         GE_CHECK(data);
200         ge_ugdata *ugd = (ge_ugdata *)data;
201
202         ge_albums_back_to_view(ugd);
203 }
204
205 int ge_ui_create_title_and_push(ge_ugdata *ugd, Evas_Object* parent,
206                                 Evas_Object* obj, NaviTitleMode mode,
207                                 char* title)
208 {
209         ge_dbg("");
210         GE_CHECK_VAL(ugd, -1);
211         GE_CHECK_VAL(parent, -1);
212         GE_CHECK_VAL(obj, -1);
213         GE_CHECK_VAL(title, -1);
214         Elm_Object_Item *nf_it = NULL;
215
216         if (mode == AlbumsView_Title) {
217                 ge_dbg("AlbumsView_Title");
218                 /* Add cancel button */
219                 Evas_Object *cancel_btn = NULL;
220                 cancel_btn = elm_button_add(obj);
221                 GE_CHECK_VAL(cancel_btn, -1);
222                 if(ugd->th)
223                         elm_object_theme_set(cancel_btn, ugd->th);
224                 elm_object_style_set(cancel_btn, GE_BUTTON_STYLE_NAVIBAR_PRE);
225                 evas_object_smart_callback_add(cancel_btn, "clicked",
226                                                _ge_ui_destroy_me_cb, ugd);
227                 nf_it = elm_naviframe_item_push(parent, title, cancel_btn, NULL,
228                                                 obj, NULL);
229         } else if(mode == ThumbnailView_Title) {
230                 if(ugd->file_select_mode == GE_File_Select_None) {
231                         ge_dbgE("file_select_mode == GE_File_Select_None!");
232                         return -1;
233                 }
234
235                 ge_dbg("ThumbnailView_Title");
236 #if 1
237                 nf_it = elm_naviframe_top_item_get(parent);
238                 /* For the instant transition, use the xxx_insert_after API */
239                 nf_it = elm_naviframe_item_insert_after(parent, nf_it, title,
240                                                         NULL, NULL, obj, NULL);
241 #else
242                 nf_it = elm_naviframe_item_push(parent, title, NULL, NULL, obj,
243                                                 NULL);
244 #endif
245                 /* Set pre_btn style to back button */
246                 Evas_Object *back_btn = NULL;
247                 back_btn = elm_object_item_part_content_get(nf_it,
248                                                             GE_NAVIFRAME_PREB_BTN);
249                 if(ugd->th)
250                         elm_object_theme_set(back_btn, ugd->th);
251                 elm_object_style_set(back_btn, GE_BUTTON_STYLE_NAVIBAR_PRE);
252                 evas_object_smart_callback_add(back_btn, "clicked",
253                                                _ge_ui_back_to_albums_cb, ugd);
254
255                 /**
256                  * Add  'Done' item.
257                  */
258                 if ((ugd->file_select_mode != GE_File_Select_One) &&
259                     (_ge_get_view_mode() == GE_ThumbnailEdit_Mode)) {
260                         ge_dbg("Multiple selection, show Done");
261                         /* create tool controlbar downside */
262                         Evas_Object *bot_bar = NULL;
263                         bot_bar = elm_toolbar_add(parent);
264                         GE_CHECK_VAL(bot_bar, -1);
265                         elm_toolbar_shrink_mode_set(bot_bar, ELM_TOOLBAR_SHRINK_EXPAND);
266                         elm_toolbar_homogeneous_set(bot_bar, EINA_FALSE);
267                         elm_object_style_set(bot_bar, GE_CBAR_STYLE_DEFAULT);
268                         /* add 'Done' item */
269                         Elm_Object_Item *done_it = NULL;
270                         done_it = elm_toolbar_item_append(bot_bar,
271                                                           GE_CBAR_ICON_DONE,
272                                                           NULL, __ge_ui_done_cb,
273                                                           ugd);
274                         elm_object_item_disabled_set(done_it, EINA_TRUE);
275                         /* add empty item */
276                         __ge_ui_append_object_item(bot_bar, NULL);
277                         __ge_ui_append_object_item(bot_bar, NULL);
278                         __ge_ui_append_object_item(bot_bar, NULL);
279                         /* Set tool bar to bottom */
280                         elm_object_item_part_content_set(nf_it,
281                                                          GE_NAVIFRAME_CONTROLBAR,
282                                                          bot_bar);
283                 }
284                 ugd->thumbs_nf_it = nf_it;
285         }
286
287         return 0;
288 }
289
290 Evas_Object* ge_ui_load_edj(Evas_Object *parent, const char *file, const char *group)
291 {
292         GE_CHECK_NULL(parent);
293         GE_CHECK_NULL(file);
294         GE_CHECK_NULL(group);
295         Evas_Object *eo;
296         int r;
297
298         eo = elm_layout_add(parent);
299         if (eo)
300         {
301                 r = elm_layout_file_set(eo, file, group);
302                 if (!r)
303                 {
304                         evas_object_del(eo);
305                         return NULL;
306                 }
307
308                 evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
309                 evas_object_size_hint_align_set(eo, EVAS_HINT_FILL, EVAS_HINT_FILL);
310         }
311
312         return eo;
313 }
314
315 Evas_Object* ge_ui_create_nocontents(ge_ugdata* ugd)
316 {
317         GE_CHECK_NULL(ugd);
318         Evas_Object *layout = NULL;
319         char label_str[GE_NOCONTENTS_LABEL_LEN_MAX] ={0,};
320
321         /* Full nocontents view layout */
322         layout = elm_layout_add (ugd->navi_bar);
323         GE_CHECK_NULL(layout);
324         elm_layout_file_set (layout, GE_EDJ_FILE, GE_GRP_NOCONTENTS);
325         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
326                                          EVAS_HINT_EXPAND);
327
328         if(ugd->ug_exit_mode == GE_UG_EXIT_NONE) {
329                 if(_ge_get_view_mode() == GE_ThumbnailView_Mode ||
330                    _ge_get_view_mode() == GE_ThumbnailEdit_Mode) {
331                         if(ugd->file_type_mode) {
332                                 if (ugd->file_type_mode == GE_File_Select_Type_Image)
333                                         snprintf(label_str, sizeof(label_str), "%s",
334                                                  (char*)(_GE_GETSYSTEMSTR("IDS_COM_BODY_NO_IMAGES")));
335                                 else if (ugd->file_type_mode == GE_File_Select_Type_Video)
336                                         snprintf(label_str, sizeof(label_str),
337                                                  "%s", (char*)GE_LABEL_NO_VIDEOS);
338                                 else
339                                         snprintf(label_str, sizeof(label_str), "%s",
340                                                  (char*)(_GE_GETSYSTEMSTR("IDS_COM_BODY_NO_ITEMS")));
341                         } else {
342                                 snprintf(label_str, sizeof(label_str), "%s",
343                                          (char*)(_GE_GETSYSTEMSTR("IDS_COM_BODY_NO_ITEMS")));
344                         }
345                 } else if(_ge_get_view_mode() == GE_AlbumsView_Mode) {
346                         snprintf(label_str, sizeof(label_str), "%s",
347                                  (char*)GE_LABEL_NO_ALBUMS);
348                 } else {
349                         ge_dbgE("view mode is error.");
350                 }
351         } else {
352                 snprintf(label_str, sizeof(label_str), "%s<br>%s",
353                          (char*)(_GE_GETSYSTEMSTR("IDS_COM_BODY_NO_CONTENTS")),
354                          (char*)GE_UNABLE_USE_IN_UMS_MODE);
355         }
356
357         ge_dbg("\nNocontents label: %s", label_str);
358         /* Full nocontents view layout */
359         layout = elm_layout_add(ugd->navi_bar);
360         GE_CHECK_NULL(layout);
361         elm_object_part_text_set(layout, "elm.text", label_str);
362         elm_layout_theme_set(layout, "layout", "nocontents", "gallery_efl");
363
364         return layout;
365 }
366
367 Evas_Object* ge_ui_create_main_ly(ge_ugdata* ugd, Evas_Object* parent)
368 {
369         GE_CHECK_NULL(ugd);
370         GE_CHECK_NULL(parent);
371         Evas_Object *layout = NULL;
372
373         layout = elm_layout_add(parent);
374         GE_CHECK_NULL(layout);
375
376         elm_layout_theme_set(layout, "layout", "application", "default");
377         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
378                                          EVAS_HINT_EXPAND);
379         elm_win_resize_object_add(parent, layout);
380         evas_object_show(layout);
381
382         return layout;
383 }
384
385 Evas_Object* ge_ui_create_navibar(ge_ugdata* ugd, Evas_Object* parent)
386 {
387         GE_CHECK_NULL(ugd);
388         GE_CHECK_NULL(parent);
389         Evas_Object *navi_bar = NULL;
390
391         navi_bar = elm_naviframe_add(parent);
392         GE_CHECK_NULL(navi_bar);
393
394         if(ugd->th)
395         {
396                 elm_object_theme_set(navi_bar, ugd->th);
397         }
398         elm_object_style_set(navi_bar, GE_NAVIFRAME_STYLE_DEFAULT);
399         elm_object_part_content_set(parent, "elm.swallow.content", navi_bar);
400
401         evas_object_show(navi_bar);
402
403         return navi_bar;
404 }
405
406 Evas_Object *ge_ui_create_popup(ge_ugdata* ugd, GE_PopupMode mode, char* desc)
407 {
408         ge_dbg("");
409         GE_CHECK_NULL(ugd);
410         GE_CHECK_NULL(desc);
411
412         if(ugd->popup)
413         {
414                 ge_dbg("The existed popup is deleted");
415                 evas_object_del(ugd->popup);
416                 ugd->popup = NULL;
417         }
418
419         Evas_Object *popup = NULL;
420
421         popup = elm_popup_add(ugd->ly_main);
422         GE_CHECK_NULL(popup);
423         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
424         elm_object_text_set(popup, desc);
425
426         switch (mode) {
427         case GE_Popup_NoButton:
428         case GE_Popup_UgExit:
429                 __ge_popup_add_timer(ugd, GE_NOBUT_EXIT_POPUP_HIDE_TIME_DELAY);
430                 break;
431         case GE_Popup_OneButton:
432         {
433                 Evas_Object *btn = NULL;
434                 btn = _ge_but_create_but(popup, GE_BUT_OK, _ge_ui_response_cb,
435                                          ugd);
436                 elm_object_part_content_set(popup, "button1", btn);
437                 break;
438         }
439         case GE_Popup_TwoButton:
440         {
441                 Evas_Object *btn1 = NULL;
442                 btn1 = _ge_but_create_but(popup, GE_BUT_OK, _ge_ui_response_cb,
443                                           ugd);
444                 elm_object_part_content_set(popup, "button1", btn1);
445                 Evas_Object *btn2 = NULL;
446                 btn2 = _ge_but_create_but(popup, GE_BUT_CANCEL,
447                                            _ge_ui_response_cb, ugd);
448                 elm_object_part_content_set(popup, "button2", btn2);
449                 break;
450         }
451         default:
452                 ge_dbgE("mode is not supported...");
453                 break;
454         }
455
456         evas_object_show(popup);
457
458         ugd->popup = popup;
459         ugd->popup_mode = mode;
460
461         return popup;
462 }
463
464 int ge_ui_create_selinfo(ge_ugdata* ugd, Evas_Object* parent)
465 {
466         ge_dbg("");
467         GE_CHECK_VAL(ugd, -1);
468         GE_CHECK_VAL(parent, -1);
469         int count = 1;
470         int all_count = 0;
471         char text[GE_SELINFO_TEXT_LEN_MAX] = {0,};
472
473         count = ge_db_selected_list_count();
474         if(count <= 0)
475         {
476                 __ge_ui_disable_done_item(ugd, true);
477                 if(ugd->file_select_mode == GE_File_Select_Multiple)
478                 {
479                         ge_ui_destroy_selinfo(ugd);
480                 }
481                 ge_dbg("@@@[Warning] no item selected@@@");
482                 return -1;
483         }
484
485         __ge_ui_disable_done_item(ugd, false);
486
487         if(ugd->file_select_mode == GE_File_Select_One)
488         {
489                 return -1;
490         }
491
492         all_count = ge_db_get_count_all();
493         ge_dbg("New count , all count : %d,%d", count, all_count);
494
495         ge_ui_destroy_selinfo(ugd);
496         ugd->selectioninfo = elm_notify_add(parent);
497         GE_CHECK_VAL(ugd->selectioninfo, -1);
498         elm_notify_orient_set(ugd->selectioninfo, ELM_NOTIFY_ORIENT_BOTTOM);
499         evas_object_size_hint_weight_set(ugd->selectioninfo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
500         evas_object_size_hint_align_set(ugd->selectioninfo, EVAS_HINT_FILL, EVAS_HINT_FILL);
501
502         evas_object_smart_callback_add(ugd->selectioninfo, "timeout", _ge_ui_selinfo_destroy_cb, ugd);
503         elm_notify_timeout_set(ugd->selectioninfo, GE_SELINFO_HIDE_TIME_DELAY);
504
505         ugd->selectioninfo_ly = elm_layout_add(parent);
506         GE_CHECK_VAL(ugd->selectioninfo_ly, -1);
507         if(ugd->th)
508         {
509                 elm_object_theme_set(ugd->selectioninfo_ly, ugd->th);
510         }
511         elm_layout_theme_set(ugd->selectioninfo_ly, "standard", "selectioninfo", "ug_selectioninfo_viewer");
512
513         evas_object_size_hint_weight_set(ugd->selectioninfo_ly, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
514         evas_object_size_hint_align_set(ugd->selectioninfo_ly, EVAS_HINT_FILL, EVAS_HINT_FILL);
515         elm_object_content_set(ugd->selectioninfo,ugd->selectioninfo_ly);
516
517         /* Add Space */
518         snprintf(text, sizeof(text), "%s (%d)", GE_SELINFO_SELECTED, count);
519         text[strlen(text)] = '\0';
520
521         edje_object_signal_emit(_EDJ(ugd->thumbs_view_ly), "elm,thumb_view,state,moveup", "elm");/* move view up while selection view shown.*/
522         edje_object_part_text_set(_EDJ(ugd->selectioninfo_ly), "elm.text", text);
523         evas_object_show(ugd->selectioninfo);
524
525         return 0;
526 }
527
528 int ge_ui_destroy_selinfo(ge_ugdata* ugd)
529 {
530         ge_dbg("");
531         GE_CHECK_VAL(ugd, -1);
532         GE_CHECK_VAL(ugd->thumbs_view_ly, -1);
533
534         edje_object_signal_emit(_EDJ(ugd->thumbs_view_ly), "elm,thumb_view,state,default", "elm");/* reset view to default status.*/
535         if(ugd->file_select_mode != GE_File_Select_One && ugd->selectioninfo)
536         {
537                 evas_object_smart_callback_del(ugd->selectioninfo, "timeout", _ge_ui_selinfo_destroy_cb);
538         }
539
540         if(ugd->selectioninfo_ly)
541         {
542                 evas_object_del(ugd->selectioninfo_ly);
543                 ugd->selectioninfo_ly = NULL;
544         }
545
546         if(ugd->selectioninfo)
547         {
548                 evas_object_del(ugd->selectioninfo);
549                 ugd->selectioninfo = NULL;
550         }
551
552         return 0;
553 }
554