apply FSL(Flora Software License)
[apps/home/gallery.git] / libug / libug-gallery-efl / src / ge-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 <sys/stat.h>
18 #include <errno.h>
19 #include "ge-debug.h"
20 #include "ge-albums.h"
21 #include "ge-gridview.h"
22 #include "ge-db-handler.h"
23 #include "ge-util.h"
24 #include "ge-ui-util.h"
25
26 #define GE_TIMER_INTERVAL_UMS_UPDATE 2.0
27
28 /* Global evas objects, which is used in the entire application */
29
30 static ge_item* ge_current_mb_item;
31 static ge_cluster* ge_current_album_item;
32 static int ge_ug_iv_enter = 0;
33
34 ge_item* _ge_get_current_item(void)
35 {
36         return ge_current_mb_item;
37 }
38
39 void _ge_set_current_item(ge_item* gitem)
40 {
41         ge_current_mb_item = gitem;
42 }
43
44 ge_cluster* _ge_get_current_album(void)
45 {
46         return ge_current_album_item;
47 }
48
49 void _ge_set_current_album(ge_cluster* album_item)
50 {
51         ge_current_album_item = album_item;
52 }
53
54 int _ge_ug_iv_get(void)
55 {
56         return ge_ug_iv_enter;
57 }
58
59 void _ge_ug_iv_set(int ug_iv_state)
60 {
61         ge_ug_iv_enter = ug_iv_state;
62 }
63
64 static int _ge_refresh_albums_list(ge_ugdata *ugd)
65 {
66         GE_CHECK_VAL(ugd, -1);
67         ge_cluster* old_cur_cluster = _ge_get_current_album();
68         char cluster_id[GE_MTYPE_ITEN_ID_LEN_MAX] = { 0, };
69         int i = 0;
70         ge_cluster *current = NULL;
71         bool b_get_cur_album = false;
72
73         if(old_cur_cluster && old_cur_cluster->cluster &&
74            old_cur_cluster->cluster->uuid)
75         {
76                 g_strlcpy(cluster_id, old_cur_cluster->cluster->uuid,
77                           GE_MTYPE_ITEN_ID_LEN_MAX);
78                 b_get_cur_album = true;
79         }
80         else
81         {
82                 ge_dbg("None album selected, current album is unavailable\n");
83         }
84         ge_db_get_clusters_list(ugd);
85
86         GE_CHECK_VAL(ugd->cluster_list, -1);
87         int length = eina_list_count(ugd->cluster_list->clist);
88         ge_dbg("album length: %d, current album id: %s", length, cluster_id);
89
90         //need to reset current cluster
91         _ge_set_current_album(NULL);
92         if(b_get_cur_album)
93         {
94                 for (i = 0; i < length; i++)
95                 {
96                         current = eina_list_nth(ugd->cluster_list->clist, i);
97                         if(current && current->cluster &&
98                            current->cluster->uuid) {
99                                 ge_dbg("current album : %s", current->cluster->display_name);
100                         } else {
101                                 ge_dbgE("Current is NULL");
102                                 break;
103                         }
104                         if(!g_strcmp0(current->cluster->uuid, cluster_id))
105                         {
106                                 ge_db_update_items_list(ugd, current);
107                                 ugd->cluster_list->cur_pos = i;
108                                 _ge_set_current_album(current);
109                                 return 0;
110                         }
111                 }
112                 /* Current album was removed, clear items list */
113                 if (ge_db_selected_list_get())
114                         ge_db_selected_list_finalize();
115                 ge_db_clear_items_list();
116         }
117
118         return -1;
119 }
120
121 static void _ge_db_update_noti_cb(keynode_t *key, void* data)
122 {
123         ge_dbgW("VCONFKEY_FILEMANAGER_DB_STATUS changed!");
124         GE_CHECK(data);
125         ge_ugdata* ugd = (ge_ugdata*)data;
126         int mmc_state = -1;
127         int mode = GE_UPDATE_NONE;
128
129         vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state);
130         if(mmc_state == -1) {
131                 ge_dbgE("vconf_get_int failed!");
132                 return;
133         }
134
135         if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
136                 ge_dbg("mmc_state[%d]: VCONFKEY_SYSMAN_MMC_MOUNTED", mmc_state);
137                 mode = GE_UPDATE_MMC_ADDED;
138         } else if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED ||
139                    mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
140                 ge_dbg("mmc_state[%d]: VCONFKEY_SYSMAN_MMC_REMOVED", mmc_state);
141                 mode = GE_UPDATE_MMC_REMOVED;
142         }
143
144         if (ugd->ums_update_timer) {
145                 /**
146                 * MMC noti callback also would be invoked
147                 * if USB disconnected in UMS mode,
148                 * Return to prevent from refreshing view twice.
149                 */
150                 ge_dbgW("View would be update in _ge_ums_update_timer_cb.");
151                 return;
152         }
153         /* Update view */
154         ge_update_view(ugd, mode);
155 }
156
157 static Eina_Bool _ge_ums_update_timer_cb(void *data)
158 {
159         ge_dbg("Timer callback invoked, try to update view.");
160         GE_CHECK_CANCEL(data);
161         ge_ugdata* ugd = (ge_ugdata*)data;
162
163         ge_update_view(ugd, GE_UPDATE_NORMAL);
164
165         GE_DEL_TIMER(ugd->ums_update_timer)
166
167         return ECORE_CALLBACK_CANCEL;
168 }
169
170 static void _ge_ums_noti_cb(keynode_t *key, void* data)
171 {
172         ge_dbg("");
173         int ums_mode = -1;
174         ge_ugdata* ugd = (ge_ugdata*)data;
175         GE_CHECK(ugd);
176         GE_CHECK(ugd->ug);
177
178         vconf_get_int(VCONFKEY_USB_STORAGE_STATUS, &ums_mode);
179         if (ums_mode == -1) {
180                 ge_dbgE("Error when check MASS STORAGE Status");
181                 return;
182         }
183
184         if (ums_mode == VCONFKEY_USB_STORAGE_STATUS_UMS_ON ||
185             ums_mode == VCONFKEY_USB_STORAGE_STATUS_UMS_MMC_ON) {
186                 ge_dbg("VCONFKEY_USB_STORAGE_STATUS_UMS_ON");
187                 /* Destroy UG if UMS on */
188                 ugd->ug_exit_mode = GE_UG_EXIT_MASS_STORAGE;
189                 ug_destroy_me(ugd->ug);
190         } else if (ums_mode == VCONFKEY_USB_STORAGE_STATUS_OFF) {
191                 ge_dbg("VCONFKEY_USB_STORAGE_STATUS_OFF,update view in 2secs.");
192                 /**
193                 * Update view in 2 seconds,
194                 * media-server scan file system and insert records to DB,
195                 * it takes some time.
196                 */
197                 ugd->ug_exit_mode = GE_UG_EXIT_NONE;
198                 Ecore_Timer *ums_timer = NULL;
199                 GE_DEL_TIMER(ugd->ums_update_timer)
200                 ums_timer = ecore_timer_add(GE_TIMER_INTERVAL_UMS_UPDATE,
201                                             _ge_ums_update_timer_cb, data);
202                 ugd->ums_update_timer = ums_timer;
203         }
204 }
205
206 Eina_Bool ge_update_view(ge_ugdata* ugd, int mode)
207 {
208         ge_dbg("mode: %d", mode);
209         GE_CHECK_FALSE(ugd);
210         int view_mode = _ge_get_view_mode();
211         ge_dbg("view_mode: %d", view_mode);
212
213         if (view_mode == GE_AlbumsView_Mode)
214         {
215                 _ge_refresh_albums_list(ugd);
216                 ge_albums_update_view(ugd);
217         }
218         else if (view_mode == GE_ThumbnailView_Mode)
219         {
220                 _ge_refresh_albums_list(ugd);
221                 if(_ge_get_current_album() == NULL)
222                 {
223                         ge_dbg("current album is empty, back to albums view!");
224                         ge_albums_back_to_view(ugd);
225                         elm_naviframe_item_pop(ugd->navi_bar);
226
227                         return EINA_FALSE;
228                 }
229
230                 ge_grid_update_view(ugd);
231         }
232         else if (view_mode == GE_ThumbnailEdit_Mode)
233         {
234                 ge_cluster* cur_album = _ge_get_current_album();
235
236                 /* MMC removed */
237                 if(mode == GE_UPDATE_MMC_REMOVED && cur_album &&
238                    cur_album->cluster &&
239                    cur_album->cluster->type == MINFO_MMC) {
240                         ge_dbgW("MMC album, change to albums view!");
241                         _ge_refresh_albums_list(ugd);
242                         ge_albums_back_to_view(ugd);
243                         elm_naviframe_item_pop(ugd->navi_bar);
244                         return EINA_FALSE;
245                 }
246
247                 /* Update albums list and items list */
248                 _ge_refresh_albums_list(ugd);
249                 if(ge_db_get_count_all() <= 0) {
250                         ge_dbgW("current album is empty, back to albums view!");
251                         ge_albums_back_to_view(ugd);
252                         elm_naviframe_item_pop(ugd->navi_bar);
253                         return EINA_FALSE;
254                 }
255
256                 ge_grid_update_view(ugd);
257
258                 if(ge_db_get_count_all() <= 0) {
259                         ge_dbgW("current album is empty, back to albums view!");
260                         ge_albums_back_to_view(ugd);
261                         elm_naviframe_item_pop(ugd->navi_bar);
262                         return EINA_FALSE;
263                 }
264         }
265
266         return EINA_TRUE;
267 }
268
269 int ge_reg_db_update_noti(ge_ugdata* ugd)
270 {
271         ge_dbg("");
272         GE_CHECK_VAL(ugd, -1);
273         int error_code = 0;
274
275         error_code = vconf_notify_key_changed(VCONFKEY_FILEMANAGER_DB_STATUS,
276                                               _ge_db_update_noti_cb, ugd);
277         if(error_code == -1)
278                 ge_dbgE("vconf_notify_key_changed FAIL!");
279
280         return 0;
281 }
282
283 int ge_dereg_db_update_noti(void)
284 {
285         ge_dbg("");
286         int error_code = -1;
287
288         error_code = vconf_ignore_key_changed(VCONFKEY_FILEMANAGER_DB_STATUS,
289                                               _ge_db_update_noti_cb);
290         if(error_code == -1)
291                 ge_dbgE("vconf_ignore_key_changed FAIL!");
292
293         return 0;
294 }
295
296 int ge_reg_ums_noti(ge_ugdata* ugd)
297 {
298         GE_CHECK_VAL(ugd, -1);
299         int error_code = 0;
300
301         error_code = vconf_notify_key_changed(VCONFKEY_USB_STORAGE_STATUS,
302                                               _ge_ums_noti_cb, ugd);
303         if(error_code == -1) {
304                 ge_dbgE("vconf_notify_key_changed FAIL!");
305         }
306
307         return 0;
308 }
309
310 int ge_dereg_ums_noti(void)
311 {
312         int error_code = -1;
313
314         error_code = vconf_ignore_key_changed(VCONFKEY_USB_STORAGE_STATUS,
315                                               _ge_ums_noti_cb);
316         if (error_code == -1)
317                 ge_dbgE("_gl_vconf_loaded_mass_storage_noti_cb FAIL!");
318
319         return 0;
320 }
321
322 int ge_get_ums_state(ge_ugdata* ugd)
323 {
324         ge_dbg("");
325         GE_CHECK_VAL(ugd, -1);
326         int ums_mode = -1;
327         int exit_mode = GE_UG_EXIT_NONE;
328
329         if(!vconf_get_int(VCONFKEY_USB_STORAGE_STATUS, &ums_mode)) {
330                 ge_dbg("UMS mode: %d.", ums_mode);
331                 if(ums_mode == VCONFKEY_USB_STORAGE_STATUS_UMS_ON ||
332                    ums_mode == VCONFKEY_USB_STORAGE_STATUS_UMS_MMC_ON) {
333                         ge_dbgW("ums_mode[%d]:  UMS_ON", ums_mode);
334                         /* Usb mass storage mode activated */
335                         exit_mode = GE_UG_EXIT_MASS_STORAGE;
336                 }
337         }
338
339         ugd->ug_exit_mode = exit_mode;
340         return 0;
341 }
342
343