initial upload for tizen 2.0 beta
[apps/home/gallery.git] / src / features / gl-albums.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 "gl-debug.h"
18 #include "gl-albums.h"
19 #include "gl-gridview.h"
20 #include "gl-listview.h"
21 #include "gl-controlbar.h"
22 #include "gl-ui-util.h"
23 #include "gl-util.h"
24 #include "gl-drm.h"
25 #include "gl-button.h"
26 #include "gl-data.h"
27 #include "gl-popup.h"
28 #include "gl-progressbar.h"
29 #include "gl-tile.h"
30 #include "gl-strings.h"
31 #include "gl-nocontents.h"
32 #include "gl-notify.h"
33 #ifdef _USE_ROTATE_BG
34 #include "gl-rotate-bg.h"
35 #include "gl-exif.h"
36 #endif
37
38 void _gl_albums_realized(void *data, Evas_Object *obj, void *event_info)
39 {
40         gl_dbg_launch("      realized");
41 }
42
43 static void _gl_albums_comeback_from_view_cb(void *data,
44                                              Evas_Object *obj, void *event_info)
45 {
46         GL_CHECK(data);
47         gl_appdata *ad = (gl_appdata *)data;
48         gl_dbg("");
49
50         /* Delete callback when it is clicked to prevent it is called for many times */
51         evas_object_smart_callback_del(obj, "clicked",
52                                        _gl_albums_comeback_from_view_cb);
53
54         /* Reset aul launch flag if change to albums view */
55         ad->albuminfo.aul_launch_by_mime = false;
56         ad->albuminfo.aul_launch_type = GL_AUL_T_NONE;
57
58         if (_gl_data_check_update(ad, true))
59                 gl_refresh_albums_list(ad);
60
61         gl_albums_comeback_from_view(ad);
62
63         _gl_ctrl_disable_toolbar_item(ad, false, GL_NAVI_ALBUMS, false);
64 }
65
66 /**
67 * @Brif Update checkbox state and album cover and update selectioninfo.
68 *
69 * @Param data  gl_appdata.
70 * @Param album_item  album item.
71 * @Param check_obj  object of checkbox.
72 * @Param b_ck_box  true: Checkbox selection, false: album cover selection.
73 */
74 static int _gl_albums_update_check_state(void *data,
75                                          gl_cluster *album_item,
76                                          Evas_Object *check_obj, bool b_ck_box)
77 {
78         GL_CHECK_VAL(album_item, -1);
79         GL_CHECK_VAL(album_item->cluster, -1);
80         GL_CHECK_VAL(album_item->cluster->uuid, -1);
81         GL_CHECK_VAL(check_obj, -1);
82         GL_CHECK_VAL(data, -1);
83         gl_appdata *ad = (gl_appdata *)data;
84         gl_dbg("Select checkbox[1]/album_cover[0]: %d.", b_ck_box);
85
86         /* Update state of checkbox */
87         album_item->checked = !album_item->checked;
88         gl_dbg("album (%s) check state: %d", album_item->cluster->display_name,
89                album_item->checked);
90
91         /**
92         * If album cover selected, update checkbox icon manually,
93         * If checkbox selected, its icon is updated by system automatically.
94         */
95         Evas_Object *ck = NULL;
96         if (b_ck_box == false)
97                 ck = elm_object_item_part_content_get(album_item->item,
98                                                       GL_TILE_CHECKBOX);
99         else
100                 ck = elm_object_item_part_content_get(album_item->item,
101                                                       GL_TILE_CHECKBOX_GRID);
102         elm_check_state_set(ck, album_item->checked);
103
104         if (album_item->checked) {
105                 gl_dbg("Append:%s, id:%s", album_item->cluster->display_name,
106                        album_item->cluster->uuid);
107                 _gl_data_albums_selected_list_append(ad, album_item);
108         } else {
109                 gl_dbg("Remove:%s, id:%s", album_item->cluster->display_name,
110                        album_item->cluster->uuid);
111                 _gl_data_albums_selected_list_remove(ad, album_item);
112         }
113
114         /* Display selectioninfo */
115         int cnt = _gl_data_get_albums_selected_cnt(ad);
116         _gl_notify_create_selinfo(ad, ad->albuminfo.edit_layout,
117                                   ad->albuminfo.nf_it_edit,
118                                   ad->albuminfo.albums_cnt, cnt, false);
119         return 0;
120 }
121
122 static void _gl_albums_check_changed(void *data, Evas_Object *obj,
123                                      void *event_info)
124 {
125         GL_CHECK(obj);
126         GL_CHECK(data);
127         gl_cluster *album_item = (gl_cluster *)data;
128         GL_CHECK(album_item->ad);
129         gl_appdata *ad = (gl_appdata *)album_item->ad;
130         gl_dbg("");
131
132         if (gl_get_view_mode(ad) != GL_VIEW_ALBUMS_EDIT) {
133                 gl_dbgE("Wrong view mode!");
134                 return;
135         } else {
136                 /* gl_dbg("EditMode"); */
137         }
138
139         /* Checkbox selected, b_ck_box=true */
140         _gl_albums_update_check_state(ad, album_item, obj, true);
141 }
142
143 static void _gl_albums_check_grid_changed(void *data, Evas_Object *obj,
144                                            void *event_info)
145 {
146         GL_CHECK(obj);
147         GL_CHECK(data);
148         gl_cluster *album_item = (gl_cluster *)data;
149         GL_CHECK(album_item->ad);
150         gl_appdata *ad = (gl_appdata *)album_item->ad;
151         gl_dbg("");
152
153         if (gl_get_view_mode(ad) != GL_VIEW_ALBUMS_EDIT) {
154                 gl_dbgE("Wrong view mode!");
155                 return;
156         }
157
158         /* Grid checkbox selected, b_ck_box=false */
159         _gl_albums_update_check_state(ad, album_item, obj, false);
160 }
161
162 /* Callback of album item selection */
163 static void __gl_albums_sel_cb(void *data, Evas_Object *obj, void *event_info)
164 {
165         GL_CHECK(event_info);
166         GL_CHECK(data);
167         gl_cluster *album_item = (gl_cluster *)data;
168         GL_CHECK(album_item->cluster);
169         GL_CHECK(album_item->ad);
170         gl_appdata *ad = (gl_appdata *)album_item->ad;
171         Evas_Event_Mouse_Up *ev = (Evas_Event_Mouse_Up *)event_info;
172
173         if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
174                 return;
175
176         int view_mode = gl_get_view_mode(ad);
177         gl_dbg("view mode: %d.", view_mode);
178         if (view_mode != GL_VIEW_ALBUMS_EDIT) {
179                 if (album_item->cluster->count == 0) {
180                         gl_dbgW("Empty album, return!");
181                         return;
182                 }
183                 gl_albums_sel_album(album_item);
184         }
185 }
186
187 static void __gl_albums_rename_job_cb(void *userdata)
188 {
189         GL_CHECK(userdata);
190         gl_ui_rename_album(userdata);
191         return;
192 }
193
194 static void _gl_albums_rename_btn_cb(void *data, Evas_Object *obj,
195                                      void *event_info)
196 {
197         gl_dbg("");
198         GL_CHECK(data);
199         gl_cluster *album_item = (gl_cluster *)data;
200         gl_appdata *ad = (gl_appdata *)album_item->ad;
201
202         /* Return if album selected */
203         if (!album_item->checked) {
204                 gl_dbgW("Album checked!");
205                 return;
206         }
207
208         /* Album cover selected, b_ck_box=false, deselect checkbox */
209         Evas_Object *ck = NULL;
210         ck = elm_object_item_part_content_get(album_item->item,
211                                               GL_TILE_CHECKBOX);
212         _gl_albums_update_check_state(ad, album_item, ck, false);
213
214         if(ad->maininfo.rename_album_job) {
215                 ecore_job_del(ad->maininfo.rename_album_job);
216                 ad->maininfo.rename_album_job = NULL;
217         }
218         ad->maininfo.rename_album_job = ecore_job_add(__gl_albums_rename_job_cb,
219                                                       album_item);
220 }
221
222 static char *__gl_albums_get_text(void *data, Evas_Object *obj, const char *part)
223 {
224         GL_CHECK_NULL(part);
225         GL_CHECK_NULL(strlen(part));
226         GL_CHECK_NULL(data);
227         gl_cluster *album_item = (gl_cluster *)data;
228         GL_CHECK_NULL(album_item->cluster);
229         GL_CHECK_NULL(album_item->cluster->display_name);
230         GL_CHECK_NULL(album_item->cluster->uuid);
231         GL_CHECK_NULL(album_item->ad);
232         gl_appdata *ad = (gl_appdata *)album_item->ad;
233         char buf[GL_ALBUM_NAME_LEN_MAX] = { 0, };
234
235         if (!g_strcmp0(part, "elm.text.name")) {
236                 gl_dbg_launch("      text.name");
237                 gl_dbg("text.name");
238                 if (_gl_data_is_root_path(ad, album_item->cluster->path))
239                         snprintf(buf, sizeof(buf), "%s",
240                                  GL_ALBUM_PHOME_ROOT_NAME);
241                 else if (album_item->cluster->display_name &&
242                          strlen(album_item->cluster->display_name))
243                         snprintf(buf, sizeof(buf), "%s",
244                                  (char *)(album_item->cluster->display_name));
245                 else
246                         gl_dbgE("Album name[%s] is wrong!",
247                                 album_item->cluster->display_name);
248         } else if (!g_strcmp0(part, "elm.text.date")) {
249                 int i = 0;
250                 int item_count = GL_ALBUM_COVER_THUMB_NUM;
251                 gl_item **items1 = ad->albuminfo.cover_thumbs;
252                 gl_albums_free_cover_thumbs(ad);
253
254                 _gl_data_get_first_several_items(album_item, items1, &item_count,
255                                               MEDIA_CONTENT_ORDER_DESC);
256
257                 {
258                         if (item_count <= 0) {
259                                 gl_dbg("Empty album...");
260                                 ad->albuminfo.album_medias_cnt = 0;
261                                 return NULL;
262                         }
263
264                         ad->albuminfo.album_medias_cnt = item_count;
265                         if (items1[0] == NULL || items1[0]->item == NULL) {
266                                 gl_dbgE("[Error] Invalid item!");
267                                 ad->albuminfo.album_medias_cnt = 0;
268                                 return NULL;
269                         }
270                         memcpy(&album_item->item_mtime,
271                                &(items1[0]->item->mtime), sizeof(time_t));
272
273                         item_count = 1;
274                         gl_item *items2[1];
275                         int sort_t = MEDIA_CONTENT_ORDER_ASC;
276                         memset(items2, 0x00, item_count * sizeof(int));
277                         _gl_data_get_first_several_items(album_item,
278                                                       items2, &item_count,
279                                                       sort_t);
280
281                         if (item_count <= 0) {
282                                 gl_dbg("Empty album...");
283                                 return NULL;
284                         }
285
286                         if (items2[0] == NULL || items2[0]->item == NULL) {
287                                 gl_dbgE("Invalid items2[0]!");
288                                 ad->albuminfo.album_medias_cnt = 0;
289                                 return NULL;
290                         }
291                         _gl_tile_get_mtime((time_t *)&(album_item->item_mtime),
292                                            (time_t *)&(items2[0]->item->mtime),
293                                            buf, GL_ALBUM_NAME_LEN_MAX);
294
295                         for (i = 0; i < item_count; i++) {
296                                 _gl_data_destroy_item(items2[i]);
297                                 items2[i] = NULL;
298                         }
299                 }
300         } else if (!g_strcmp0(part, "elm.text.count")) {
301                 _gl_data_update_item_cnt(album_item);
302                 snprintf(buf, sizeof(buf), "%d",
303                          (int)(album_item->cluster->count));
304         }
305
306         return strdup(buf);
307
308 }
309
310 static gl_icon_type __gl_albums_set_bg_file(Evas_Object *bg, void *data)
311 {
312         gl_item *git =  (gl_item *)data;
313         char *bg_path = GL_DEFAULT_THUMB_ICON;
314         gl_icon_type ret_val = GL_ICON_CORRUPTED_FILE;
315
316         if (git == NULL || git->item == NULL) {
317                 gl_dbgE("Invalid item :%p", git);
318                 goto GL_ALBUMS_FAILED;
319         }
320
321         /* Is it DRM file? Has a valid RO? */
322         char *path = git->item->file_url;
323         if(gl_drm_is_drm_file(path) &&
324            !gl_drm_check_valid_ro(path, gl_drm_get_permtype(git->item->type))) {
325                 ret_val = GL_ICON_EXPIRED_DRM;
326                 goto GL_ALBUMS_FAILED;
327         }
328
329         ret_val= GL_ICON_NORMAL;
330         bg_path = _GETICON(git->item->thumb_url);
331
332  GL_ALBUMS_FAILED:
333
334 #ifdef _USE_ROTATE_BG
335         _gl_rotate_bg_set_image_file(bg, bg_path);
336 #else
337         elm_bg_file_set(bg, bg_path, NULL);
338 #endif
339
340         return ret_val;
341 }
342
343 static Evas_Object *__gl_albums_get_content(void *data, Evas_Object *obj, const char *part)
344 {
345         GL_CHECK_NULL(part);
346         GL_CHECK_NULL(strlen(part));
347         GL_CHECK_NULL(data);
348         gl_cluster *album_item = (gl_cluster *)data;
349         GL_CHECK_NULL(album_item->cluster);
350         GL_CHECK_NULL(album_item->ad);
351         gl_appdata *ad = (gl_appdata *)album_item->ad;
352         GL_CHECK_NULL(album_item->cluster->uuid);
353         const char *al_id = album_item->cluster->uuid;
354         int view_mode = gl_get_view_mode(ad);
355
356         if (view_mode != GL_VIEW_ALBUMS &&
357             view_mode != GL_VIEW_ALBUMS_EDIT)
358                 return NULL;
359
360         Evas_Object *_obj = NULL;
361         if ((!g_strcmp0(part, GT_TILE_FIRSTICON)) ||
362             (!g_strcmp0(part, GT_TILE_ONLYICON)) ||
363             (!g_strcmp0(part, GT_TILE_ICON))
364 #ifdef _USE_ROTATE_BG
365             || (!g_strcmp0(part, GT_TILE_3ICON1)) ||
366             (!g_strcmp0(part, GT_TILE_3ICON2)) ||
367             (!g_strcmp0(part, GT_TILE_4ICON3)) ||
368             (!g_strcmp0(part, GT_TILE_4ICON4)) ||
369             (!g_strcmp0(part, GT_TILE_5ICON1)) ||
370             (!g_strcmp0(part, GT_TILE_5ICON2)) ||
371             (!g_strcmp0(part, GT_TILE_6ICON4)) ||
372             (!g_strcmp0(part, GT_TILE_6ICON5))
373 #endif
374             ) {
375                 _obj = _gl_tile_show_part_icon(obj, part,
376                                                ad->albuminfo.album_medias_cnt,
377                                                ad->maininfo.win_scale,
378                                                __gl_albums_set_bg_file,
379                                                (void **)ad->albuminfo.cover_thumbs);
380         } else if (!g_strcmp0(part, GT_TILE_LABEL)) {
381                 /**
382                 *  for "All albums" and "Camera shots", their colors should be
383                 *  25:25:25 by new gui
384                 */
385                 bool b_default = false;
386                 if (!g_strcmp0(al_id, GL_ALBUM_ALL_ID) ||
387                     _gl_data_is_default_album(GL_ALBUM_DEFAULT_NAME, album_item->cluster))
388                         b_default = true;
389                 _obj = _gl_tile_show_part_label(obj, album_item->index,
390                                                 b_default);
391
392                 gl_dbg_launch("      content");
393                 if (ad->albuminfo.album_medias_cnt <= 0)
394                         gl_dbg("Empty album");
395         } else if (!g_strcmp0(part, GL_TILE_CHECKBOX_GRID) &&
396                    view_mode == GL_VIEW_ALBUMS_EDIT) {
397                 _obj = _gl_tile_show_part_checkbox_grid(obj, album_item->checked,
398                                                         _gl_albums_check_grid_changed,
399                                                         data);
400         } else if (!g_strcmp0(part, GL_TILE_CHECKBOX) &&
401                    view_mode == GL_VIEW_ALBUMS_EDIT) {
402                 _obj = _gl_tile_show_part_checkbox(obj, album_item->checked,
403                                                    _gl_albums_check_changed,
404                                                    data);
405         } else if (!g_strcmp0(part, GL_TILE_RENAME) &&
406                    view_mode == GL_VIEW_ALBUMS_EDIT) {
407                 if (!g_strcmp0(al_id, GL_ALBUM_ALL_ID) ||
408                     _gl_data_is_default_album(GL_ALBUM_DEFAULT_NAME, album_item->cluster))
409                         return NULL;
410
411                 _obj = _gl_tile_show_part_rename(obj, _gl_albums_rename_btn_cb,
412                                                  data);
413         }
414
415         return _obj;
416 }
417
418 static int _gl_albums_create_items(void *data, Evas_Object *parent)
419 {
420         GL_CHECK_VAL(parent, -1);
421         GL_CHECK_VAL(data, -1);
422         gl_appdata *ad = (gl_appdata *)data;
423         int i = 0;
424         gl_cluster *album_item = NULL;
425         int length = 0;
426         int view_mode = gl_get_view_mode(ad);
427         gl_dbg("");
428
429         /* Clear albums view */
430         elm_gengrid_clear(parent);
431
432         ad->albuminfo.albumgic.item_style = GL_GENGRID_STYLE_ALBUM_VIEW;
433         ad->albuminfo.albumgic.func.text_get = __gl_albums_get_text;
434         ad->albuminfo.albumgic.func.content_get = __gl_albums_get_content;
435         /* Show blue folder name */
436         ad->albuminfo.albumgic_blue.item_style = GL_GENGRID_STYLE_ALBUM_VIEW_BLUE;
437         ad->albuminfo.albumgic_blue.func.text_get = __gl_albums_get_text;
438         ad->albuminfo.albumgic_blue.func.content_get = __gl_albums_get_content;
439
440         GL_CHECK_VAL(ad->albuminfo.cluster_list, -1);
441         GL_CHECK_VAL(ad->albuminfo.cluster_list->clist, -1);
442         Eina_List *clist = ad->albuminfo.cluster_list->clist;
443         length = eina_list_count(clist);
444         gl_dbg("Albums length: %d", length);
445         const char *al_id = NULL;
446         Elm_Gengrid_Item_Class *pgic = &(ad->albuminfo.albumgic);
447         Elm_Gengrid_Item_Class *pgic_blue = &(ad->albuminfo.albumgic_blue);
448         int item_cnt = 0;
449
450         gl_dbg_launch("      for:elm_gengrid_item_append:start");
451         for (i = 0; i < length; i++) {
452                 album_item = eina_list_nth(clist, i);
453                 GL_CHECK_VAL(album_item, -1);
454                 GL_CHECK_VAL(album_item->cluster, -1);
455                 GL_CHECK_VAL(album_item->cluster->display_name, -1);
456                 GL_CHECK_VAL(album_item->cluster->uuid, -1);
457                 al_id = album_item->cluster->uuid;
458
459                 if (view_mode == GL_VIEW_ALBUMS_EDIT) {
460                         if (!g_strcmp0(al_id, GL_ALBUM_ALL_ID))
461                                 continue;
462                 }
463
464                 /* Show blue folder name */
465                 if (!g_strcmp0(album_item->cluster->uuid, GL_ALBUM_ALL_ID) ||
466                     _gl_data_is_default_album(GL_ALBUM_DEFAULT_NAME, album_item->cluster))
467                         album_item->item = elm_gengrid_item_append(parent,
468                                                                    pgic_blue,
469                                                                    album_item,
470                                                                    __gl_albums_sel_cb,
471                                                                    album_item);
472                 else
473                         album_item->item = elm_gengrid_item_append(parent, pgic,
474                                                                    album_item,
475                                                                    __gl_albums_sel_cb,
476                                                                    album_item);
477                 album_item->index = item_cnt;
478                 item_cnt++;
479                 gl_dbg("Append[%s], id=%s.", album_item->cluster->display_name,
480                        al_id);
481         }
482         gl_dbg_launch("      for:elm_gengrid_item_append:end");
483
484         ad->albuminfo.albums_cnt = item_cnt;
485         return 0;
486 }
487
488 static int __gl_albums_reset_view(void *data)
489 {
490         GL_CHECK_VAL(data, -1);
491         gl_appdata *ad = (gl_appdata *)data;
492         int view_mode = gl_get_view_mode(ad);
493         gl_dbg("view_mode: %d", view_mode);
494
495         if (view_mode != GL_VIEW_ALBUMS && view_mode != GL_VIEW_ALBUMS_EDIT &&
496             view_mode != GL_VIEW_ALBUMS_RENAME) {
497                 gl_dbg("Change to albums view");
498                 gl_destroy_thumbs_view(ad);
499                 gl_refresh_albums_list(ad);
500                 gl_albums_comeback_from_view(ad);
501         } else if (view_mode == GL_VIEW_ALBUMS_EDIT ||
502                    view_mode == GL_VIEW_ALBUMS_RENAME) {
503                 gl_del_invalid_widgets(ad, GL_INVALID_NONE);
504                 gl_ui_edit_cancel(ad);
505         }
506
507         return 0;
508 }
509
510 int gl_albums_sel_album(gl_cluster *album_item)
511 {
512         GL_CHECK_VAL(album_item, -1);
513         GL_CHECK_VAL(album_item->cluster, -1);
514         GL_CHECK_VAL(album_item->cluster->uuid, -1);
515         GL_CHECK_VAL(album_item->ad, -1);
516         gl_appdata *ad = (gl_appdata *)album_item->ad;
517         GL_CHECK_VAL(ad->albuminfo.cluster_list, -1);
518         GL_CHECK_VAL(ad->albuminfo.cluster_list->clist, -1);
519         int view_mode = gl_get_view_mode(ad);
520
521         if (view_mode == GL_VIEW_ALBUMS) {
522                 gl_dbg("View mode!");
523         } else {
524                 gl_dbgW("Wrong view mode");
525                 return -1;
526         }
527
528         Evas_Object *layout = NULL;
529         char *al_na = album_item->cluster->display_name;
530         Evas_Object *navi = ad->maininfo.naviframe;
531         gl_dbg("current %s album, id=%s", al_na, album_item->cluster->uuid);
532
533         ad->albuminfo.current_album = album_item;
534
535         _gl_data_get_item_list(ad, album_item, GL_FIRST_VIEW_START_POS,
536                                GL_FIRST_VIEW_END_POS);
537
538         gl_set_view_mode(ad, GL_VIEW_THUMBS);
539         layout = _gl_ui_create_view_ly(navi);
540         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
541                                          EVAS_HINT_EXPAND);
542         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
543         Evas_Object *gv = _gl_grid_create_view(ad, layout, GL_GRID_ALL, true);
544         elm_object_part_content_set(layout, "elm.swallow.view", gv);
545         ad->gridinfo.all_view = gv;
546         ad->gridinfo.layout = layout;
547
548         /* View mode reset, Update view mode */
549         view_mode = gl_get_view_mode(ad);
550         if (view_mode != GL_VIEW_THUMBS) {
551                 gl_dbgE("View mode is wrong!");
552                 return -1;
553         }
554
555         if (_gl_data_is_root_path(ad, album_item->cluster->path)) {
556                 /* check root case */
557                 al_na = GL_ALBUM_PHOME_ROOT_NAME;
558         } else if (ad->albuminfo.aul_launch_type == GL_AUL_T_VIEW_ALBUM) {
559                 /* launch by appsvc */
560                 if (ad->albuminfo.aul_file_type == GL_AUL_FILE_T_IMAGE)
561                         al_na = GL_ALBUM_IMAGES_NAME;
562                 else if (ad->albuminfo.aul_file_type == GL_AUL_FILE_T_VIDEO)
563                         al_na = GL_ALBUM_VIDEOS_NAME;
564                 else
565                         al_na = GL_ALBUM_ALL_NAME;
566         }
567
568         /* Checkme: clear albums view for animation effect pause issue */
569         elm_gengrid_clear(ad->albuminfo.view);
570
571         gl_ui_create_title_and_push(ad, navi, layout, GL_NAVI_THUMBS, al_na);
572
573         gl_dbg("albums_view 0x%x cleared", ad->albuminfo.view);
574
575         Evas_Object *bk_btn = NULL;
576         bk_btn = elm_object_item_part_content_get(ad->gridinfo.nf_it,
577                                                   GL_NAVIFRAME_PREB_BTN);
578         elm_object_style_set(bk_btn, GL_BUTTON_STYLE_NAVI_PRE);
579         evas_object_smart_callback_add(bk_btn, "clicked",
580                                        _gl_albums_comeback_from_view_cb, ad);
581
582         edje_object_signal_emit(_EDJ(layout),
583                                 "elm,swallow_view,state,thumb_default", "elm");
584         gl_dbg("Done albums selected");
585         return 0;
586 }
587
588 Evas_Object *_gl_albums_add_gengrid(void *data, Evas_Object *parent)
589 {
590         GL_CHECK_NULL(parent);
591         GL_CHECK_NULL(data);
592
593         Evas_Object *grid = _gl_tile_add_gengrid(data, parent);
594         evas_object_smart_callback_add(grid, "realized", _gl_albums_realized,
595                                        data);
596         evas_object_show(grid);
597         return grid;
598 }
599
600 Evas_Object *gl_albums_create_view(void *data, Evas_Object *parent)
601 {
602         GL_CHECK_NULL(parent);
603         GL_CHECK_NULL(data);
604         gl_appdata *ad = (gl_appdata *)data;
605         GL_CHECK_NULL(ad->albuminfo.cluster_list);
606         gl_dbg("");
607         gl_dbg_launch("    gl_albums_create_view:start");
608
609         gl_albums_free_cover_thumbs(ad);
610         memset(ad->albuminfo.cover_thumbs, 0x00,
611                GL_ALBUM_COVER_THUMB_NUM * sizeof(int));
612         ad->albuminfo.album_medias_cnt = 0;
613         ad->albuminfo.albums_cnt = 0;
614
615         Evas_Object *grid = _gl_albums_add_gengrid(data, parent);
616         GL_CHECK_NULL(grid);
617
618         if (ad->maininfo.view_mode != GL_VIEW_THUMBS)
619                 _gl_albums_create_items(ad, grid);
620
621         int view_mode = gl_get_view_mode(ad);
622         gl_dbg("view_mode: %d", view_mode);
623
624         /* show no contents none album exists */
625         if (gl_check_gallery_empty(ad)) {
626                 evas_object_del(grid);
627                 grid = NULL;
628
629                 /* Set the grid view mode for creating nocontents view */
630                 ad->gridinfo.grid_view_mode = GL_GRID_ALBUMS;
631                 Evas_Object *noc = _gl_nocontents_create(ad);
632                 evas_object_show(noc);
633                 ad->albuminfo.nocontents = noc;
634
635                 return noc;
636         } else {
637                 ad->albuminfo.nocontents = NULL;
638         }
639
640         gl_dbg("gl_albums_create_view:done");
641         gl_dbg_launch("    gl_albums_create_view:end");
642
643         return grid;
644 }
645
646 /* From thumbnails view to albums view */
647 int gl_albums_comeback_from_view(void *data)
648 {
649         gl_dbg("");
650         GL_CHECK_VAL(data, -1);
651         gl_appdata *ad = (gl_appdata *)data;
652
653         _gl_notify_destroy_notiinfo(ad);
654
655         gl_set_view_mode(ad, GL_VIEW_ALBUMS);
656
657         gl_grid_clear_view(ad);
658         gl_list_clear_view(ad);
659
660         ad->albuminfo.current_album = NULL;
661         gl_albums_update_items(ad);
662
663         ad->maininfo.seg_mode = GL_CTRL_SEG_ALL;
664
665         return 0;
666 }
667
668 /* From albums edit view to albums view */
669 int gl_albums_change_to_view(void *data)
670 {
671         GL_CHECK_VAL(data, -1);
672         gl_appdata *ad = (gl_appdata *)data;
673         GL_CHECK_VAL(ad->albuminfo.cluster_list, -1);
674         gl_dbg("");
675
676         ad->albuminfo.albums_cnt = 0;
677         GL_CHECK_VAL(ad->albuminfo.view, -1);
678         gl_set_view_mode(ad, GL_VIEW_ALBUMS);
679         /* changed to show no contents if needed */
680         if (gl_check_gallery_empty(ad)) {
681                 if (ad->albuminfo.view)
682                         evas_object_del(ad->albuminfo.view);
683
684                 /* Set the grid view mode for creating nocontents view */
685                 ad->gridinfo.grid_view_mode = GL_GRID_ALBUMS;
686                 Evas_Object *noc = _gl_nocontents_create(ad);
687                 ad->albuminfo.nocontents = noc;
688                 ad->albuminfo.view = noc;
689                 evas_object_show(noc);
690
691                 Evas_Object *old_view = NULL;
692                 old_view = elm_object_part_content_unset(ad->albuminfo.layout,
693                                                          "elm.swallow.view");
694                 evas_object_del(old_view);
695                 elm_object_part_content_set(ad->albuminfo.layout,
696                                             "elm.swallow.view", noc);
697         } else {
698                 _gl_albums_create_items(ad, ad->albuminfo.view);
699         }
700
701         return 0;
702 }
703
704 /* Update albums view */
705 int gl_albums_update_items(void *data)
706 {
707         GL_CHECK_VAL(data, -1);
708         gl_appdata *ad = (gl_appdata *)data;
709         GL_CHECK_VAL(ad->albuminfo.cluster_list, -1);
710         int view_mode = gl_get_view_mode(ad);
711         ad->albuminfo.albums_cnt = 0;
712         gl_dbg("");
713         /* Changed to show no contents if needed */
714         if (gl_check_gallery_empty(ad)) {
715                 /* Set the grid view mode for creating nocontents view */
716                 ad->gridinfo.grid_view_mode = GL_GRID_ALBUMS;
717                 evas_object_del(ad->albuminfo.view);
718
719                 Evas_Object *noc = _gl_nocontents_create(ad);
720                 ad->albuminfo.nocontents = noc;
721                 ad->albuminfo.view = noc;
722                 evas_object_show(noc);
723
724                 Evas_Object *old_view = NULL;
725                 old_view = elm_object_part_content_unset(ad->albuminfo.layout,
726                                                          "elm.swallow.view");
727                 evas_object_del(old_view);
728                 elm_object_part_content_set(ad->albuminfo.layout,
729                                             "elm.swallow.view", noc);
730         } else {
731                 if (view_mode == GL_VIEW_ALBUMS_EDIT) {
732                         _gl_albums_create_items(ad, ad->albuminfo.edit_view);
733                         /* Display selectioninfo */
734                         int cnt = _gl_data_get_albums_selected_cnt(ad);
735                         _gl_notify_create_selinfo(ad,
736                                                   ad->albuminfo.edit_layout,
737                                                   ad->albuminfo.nf_it_edit,
738                                                   ad->albuminfo.albums_cnt, cnt,
739                                                   true);
740                 } else {
741                         if (ad->albuminfo.nocontents) {
742                                 /**
743                                 * current view is nocontents,
744                                 * unset it first then create albums view.
745                                 */
746                                 gl_albums_remove_nocontents(ad);
747                         } else {
748                                 Evas_Object *al_view = NULL;
749                                 al_view = ad->albuminfo.view;
750                                 _gl_albums_create_items(ad, al_view);
751                         }
752                 }
753         }
754
755         return 0;
756 }
757
758 /* Update albums list and refresh albums view, remove invalid widgets */
759 int gl_albums_update_view(void *data)
760 {
761         GL_CHECK_VAL(data, -1);
762         gl_appdata *ad = (gl_appdata *)data;
763         int view_mode = gl_get_view_mode(ad);
764         gl_dbg("view_mode: %d.", view_mode);
765
766         gl_albums_free_cover_thumbs(ad);
767         memset(ad->albuminfo.cover_thumbs, 0x00,
768                GL_ALBUM_COVER_THUMB_NUM * sizeof(int));
769         ad->albuminfo.albums_cnt = 0;
770
771         if (view_mode == GL_VIEW_ALBUMS) {
772                 /* Update albums list*/
773                 gl_refresh_albums_list(ad);
774
775                 if (ad->albuminfo.nocontents) {
776                         gl_albums_remove_nocontents(ad);
777                 } else if (!gl_check_gallery_empty(ad)) {
778                         _gl_albums_create_items(ad, ad->albuminfo.view);
779                         if (ad->albuminfo.cluster_list->edit_cnt)
780                                 _gl_ctrl_disable_toolbar_item(ad, false,
781                                                               GL_NAVI_ALBUMS,
782                                                               false);
783                         else
784                                 _gl_ctrl_disable_toolbar_item(ad, true,
785                                                               GL_NAVI_ALBUMS,
786                                                               true);
787                 } else {
788                         gl_dbg("View is empty.");
789                         gl_albums_update_items(ad);
790                         _gl_ctrl_disable_toolbar_item(ad, true, GL_NAVI_ALBUMS,
791                                                       true);
792                 }
793         } else if (view_mode == GL_VIEW_ALBUMS_EDIT ||
794             view_mode == GL_VIEW_ALBUMS_RENAME) {
795                 if (_gl_data_update_cluster_list(ad) != 0) {
796                         gl_dbgE("_gl_data_update_cluster_list failed!");
797                         return -1;
798                 }
799
800                 if (gl_check_gallery_empty(ad) ||
801                     ad->albuminfo.cluster_list->edit_cnt == 0) {
802                         /* Remove invalid widgets */
803                         gl_del_invalid_widgets(ad, GL_INVALID_NONE);
804
805                         gl_dbg("Edit view is empty, back to albums view");
806                         gl_ui_edit_cancel(ad);
807
808                         _gl_ctrl_disable_toolbar_item(ad, true, GL_NAVI_ALBUMS,
809                                                       true);
810                 } else {
811                         int sel_cnt = _gl_data_get_albums_selected_cnt(ad);
812                         int invalid_m = GL_INVALID_NONE;
813                         /* Album selected for rename was deleted */
814                         if (view_mode == GL_VIEW_ALBUMS_RENAME) {
815                                 if (ad->albuminfo.selected_album == NULL) {
816                                         gl_dbgW("Rename invalid album!");
817                                         invalid_m = GL_INVALID_RENAME_ALBUM;
818                                         /* Remove invalid widgets */
819                                         gl_del_invalid_widgets(ad, invalid_m);
820                                         /* Disable control bar buttons */
821                                         _gl_ctrl_disable_toolbar_items(ad,
822                                                                        ad->albuminfo.nf_it_edit,
823                                                                        true,
824                                                                        true);
825                                         if (sel_cnt == 0)
826                                         /* Reset view mode */
827                                         gl_set_view_mode(ad,
828                                                          GL_VIEW_ALBUMS_EDIT);
829                                 }
830                         } else if (sel_cnt == 0) {
831                                 /* If none albums selected */
832                                 gl_dbgW("None albums selected!");
833                                 /* Disable control bar buttons */
834                                 _gl_ctrl_disable_toolbar_items(ad,
835                                                                ad->albuminfo.nf_it_edit,
836                                                                true, true);
837                                 /* Remove invalid widgets */
838                                 gl_del_invalid_widgets(ad, invalid_m);
839                                 /* Reset view mode */
840                                 gl_set_view_mode(ad, GL_VIEW_ALBUMS_EDIT);
841                         }
842
843                         /* Update edit view, other update view in cancel callback */
844                         Evas_Object *edit_view = ad->albuminfo.edit_view;
845                         if (gl_get_view_mode(ad) == GL_VIEW_ALBUMS_EDIT) {
846                                 _gl_albums_create_items(ad, edit_view);
847                                 /* Display selectioninfo */
848                                 _gl_notify_create_selinfo(ad,
849                                                           ad->albuminfo.edit_layout,
850                                                           ad->albuminfo.nf_it_edit,
851                                                           ad->albuminfo.albums_cnt,
852                                                           sel_cnt, true);
853                         } else {
854                                 /* Cluster list updated, clear view */
855                                 elm_gengrid_clear(edit_view);
856                         }
857                 }
858         }
859         return 0;
860 }
861
862 /**
863 * Remove nocontents view and show albums view.
864 * Case 1, gallery is empty->home key tapped
865 *       ->take photos with camera->back to gallery;
866 */
867 int gl_albums_remove_nocontents(void *data)
868 {
869         GL_CHECK_VAL(data, -1);
870         gl_appdata *ad = (gl_appdata *)data;
871         Evas_Object *view = NULL;
872         gl_dbg("");
873
874         if (ad->albuminfo.nocontents && !gl_check_gallery_empty(ad)) {
875                 gl_dbg("Remove nocontents view then create albums view.");
876                 evas_object_del(ad->albuminfo.nocontents);
877                 ad->albuminfo.nocontents = NULL;
878
879                 view = gl_albums_create_view(ad, ad->maininfo.naviframe);
880                 elm_object_part_content_set(ad->albuminfo.layout,
881                                             "elm.swallow.view", view);
882                 evas_object_show(view);
883                 ad->albuminfo.view = view;
884
885                 _gl_ctrl_disable_toolbar_item(ad, false, GL_NAVI_ALBUMS, false);
886         } else if (ad->albuminfo.nocontents) {
887                 gl_dbg("Gallery is empty!");
888                 bool b_update = false;
889                 b_update = _gl_nocontents_update_label(ad->albuminfo.nocontents,
890                                                           GL_STR_NO_ALBUMS);
891                 /* Update toolbar state */
892                 if (b_update)
893                         _gl_ctrl_disable_toolbar_item(ad, false,
894                                                       GL_NAVI_ALBUMS,
895                                                       false);
896         } else {
897                 gl_dbg("Nocontents was removed!");
898         }
899
900         return 0;
901 }
902
903 int gl_albums_free_cover_thumbs(void *data)
904 {
905         gl_dbg("");
906         GL_CHECK_VAL(data, -1);
907         gl_appdata *ad = (gl_appdata *)data;
908         int i = 0;
909
910         for (i = 0; i < GL_ALBUM_COVER_THUMB_NUM; i++) {
911                 if (ad->albuminfo.cover_thumbs[i]) {
912                         _gl_data_destroy_item(ad->albuminfo.cover_thumbs[i]);
913                         ad->albuminfo.cover_thumbs[i] = NULL;
914                 }
915         }
916
917         return 0;
918 }
919
920 /* Free resources allocated for albums view */
921 int gl_albums_free_data(void *data)
922 {
923         gl_dbg("");
924         GL_CHECK_VAL(data, -1);
925         gl_appdata *ad = (gl_appdata *)data;
926
927         gl_albums_free_cover_thumbs(data);
928
929         if (ad->albuminfo.path) {
930                 free(ad->albuminfo.path);
931                 ad->albuminfo.path = NULL;
932         }
933         return 0;
934 }
935
936 int _gl_albums_reset_view(void *data, const char *uuid, const char *aul_type)
937 {
938         GL_CHECK_VAL(aul_type, -1);
939         GL_CHECK_VAL(uuid, -1);
940         GL_CHECK_VAL(data, -1);
941         gl_appdata *ad = (gl_appdata *)data;
942         int view_mode = gl_get_view_mode(ad);
943
944         gl_cluster *album = ad->albuminfo.current_album;
945         if (album && album->cluster && album->cluster->uuid) {
946                 bool b_cur_album_exists = false;
947                 if (!g_strcmp0(album->cluster->uuid, uuid)) {
948                         int its_cnt = 0;
949                         int ret = -1;
950                         ret = _gl_data_get_item_cnt(ad, uuid,
951                                                     album->cluster->type,
952                                                     &its_cnt);
953                         if (ret != 0 || its_cnt == 0)
954                                 gl_dbgE("Empty current album!");
955                         else
956                                 b_cur_album_exists = true;
957                 }
958
959                 /* Current album exists and it's not empty */
960                 if (b_cur_album_exists) {
961                         gl_dbg("Current album, update view");
962                         if (view_mode == GL_VIEW_THUMBS_EDIT ||
963                             view_mode == GL_VIEW_VIDEOLIST_EDIT) {
964                                 gl_del_invalid_widgets(ad, GL_INVALID_NONE);
965                                 /* Set flag to update albums list */
966                                 ad->albuminfo.update_albums_list = true;
967                                 gl_ui_edit_cancel(ad);
968                         } else {
969                                 gl_update_view(ad, GL_UPDATE_VIEW_NORMAL);
970                         }
971
972                         return -1;
973                 }
974         }
975
976         __gl_albums_reset_view(ad);
977
978         return 0;
979 }
980