dbg.h file is removed.
[profile/tv/apps/native/filebrowser.git] / src / views / BaseView / FileGrid.cpp
1 /*
2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <Ecore.h>
18 #include <aul.h>
19
20 #include <AppCommon.h>
21 #include "define.h"
22 #include "common.h"
23
24 #include "Mediadata.h"
25 #include "GengridItemClass.h"
26 #include "FileGrid.h"
27 #include "Info.h"
28 #include "FbContextView.h"
29
30
31 #define GENGRID_ITEM_SIZE_W (263 + 20)
32 #define GENGRID_ITEM_SIZE_H (359 + 20)
33
34
35 static Elm_Object_Item *_find_item_by_id(Eina_List *list, char *fid)
36 {
37         Eina_List *l;
38         SItemInfo *pItemInfo;
39         void* obj;
40         char *id;
41
42         EINA_LIST_FOREACH(list, l, obj) {
43                 pItemInfo = (SItemInfo *)obj;
44                 id = pItemInfo->pInfo->Id();
45                 if (id && !strcmp(id, fid))
46                         return pItemInfo->item;
47         }
48
49         return NULL;
50 }
51
52 static SItemInfo *_find_item_info(Eina_List *list, Elm_Object_Item *item)
53 {
54         Eina_List *l;
55         SItemInfo *pItemInfo;
56         void* obj;
57
58         EINA_LIST_FOREACH(list, l, obj) {
59                 pItemInfo = (SItemInfo*)obj;
60                 if (pItemInfo->item == item)
61                         return pItemInfo;
62         }
63
64         return NULL;
65 }
66
67
68 struct SFileGrid {
69         CMediadata *pMediadata;
70
71         Evas_Object *eoWin;
72         Evas_Object *eoBase;
73         Evas_Object *eoGrid;
74
75         Eina_List   *elFile;
76         Eina_List   *elItemInfo;
77
78         CFileGrid::SCallback callback;
79
80         int depth;
81         SItemInfo *ctxt_itinfo;
82         SItemInfo *handle_itinfo;
83
84         char *fid;
85
86         CGengridItemClass *pGengridItemClass;
87
88         SFileGrid() {
89                 memset(this, 0, sizeof(SFileGrid));
90         }
91 };
92
93
94 void CFileGrid::sm_CbRealized(void *data, Evas_Object *obj, void *event_info)
95 {
96         _DBG();
97         if (!data || !obj || !event_info)
98                 return;
99
100         elm_object_item_signal_callback_add((Elm_Object_Item*)event_info,
101                 FBR_SIGNAL_BTN_CLICKED, FBR_BASE_VIEW,
102                 sm_CbSelectItem, data);
103 }
104
105 void CFileGrid::sm_CbUnrealized(void *data, Evas_Object *obj, void *event_info)
106 {
107         _DBG();
108         if (!data || !obj || !event_info)
109                 return;
110
111         elm_object_item_signal_callback_del((Elm_Object_Item*)event_info,
112                 FBR_SIGNAL_BTN_CLICKED, FBR_BASE_VIEW,
113                 sm_CbSelectItem);
114 }
115
116 void CFileGrid::Action(void)
117 {
118         _DBG();
119         m->handle_itinfo = m->ctxt_itinfo;
120         m_HandleSelection();
121         m_DeleteContextView();
122 }
123
124 void CFileGrid::m_DeleteContextView(void)
125 {
126         if(CViewMgr::GetInstance()->CheckTop(FBR_CONTEXT_VIEW))
127                 CViewMgr::GetInstance()->PopView();
128 }
129
130 void CFileGrid::sm_CbSelectItem(void *data, Elm_Object_Item *it, const char *emission, const char *source)
131 {
132         _DBG();
133         CFileGrid* root = (CFileGrid*)data;
134         SItemInfo *pItemInfo;
135
136         if (!data || !it)
137                 return;
138
139         pItemInfo = _find_item_info(root->m->elItemInfo, it);
140         if (!pItemInfo) {
141                 _ERR(" no item info found ");
142                 return;
143         }
144
145         root->m->handle_itinfo = pItemInfo;
146         root->m_HandleSelection();
147 }
148
149 void CFileGrid::m_HandleSelection(void)
150 {
151         char *path;
152         int type;
153         char *id;
154         SItemInfo *pItemInfo = m->handle_itinfo;
155
156         if (!pItemInfo)
157                 return;
158
159         type = pItemInfo->pInfo->Type();
160         if (type == E_GRP_FOLDER) {
161                 if (m->fid) {
162                         free(m->fid);
163                         m->fid = NULL;
164                 }
165                 id = pItemInfo->pInfo->Id();
166                 if (!id)
167                         return;
168
169                 m->fid = strdup(id);
170                 if (!m->fid)
171                         return;
172
173                 elm_object_tree_focus_allow_set(m->eoBase, EINA_FALSE);
174                 t_Update(m->fid, false);//_update_filegrid(m, m->fid, false);
175                 elm_object_tree_focus_allow_set(m->eoBase, EINA_TRUE);
176                 elm_object_focus_set(m->eoGrid, EINA_TRUE);
177         }
178         else {
179                 path = pItemInfo->pInfo->Path();
180                 if (!path)
181                         return;
182
183                 if (m->callback.cbLinkApp)
184                         m->callback.cbLinkApp(m->callback.cookie, type, CInfo::SourceType(), path);
185                 elm_gengrid_item_selected_set(pItemInfo->item, EINA_FALSE);
186         }
187 }
188
189 void CFileGrid::m_GetMediaList(char *fid)
190 {
191         bool ret;
192         ret = m->pMediadata->GetMediaList(fid, CInfo::GroupIndex(), CInfo::SourceType(), &m->elFile);
193         if (ret == false) {
194                 _ERR(" Fetching all list from file failed ");
195                 return;
196         }
197 }
198
199 void CFileGrid::m_GetFolderList(void)
200 {
201         bool ret;
202         ret = m->pMediadata->GetFolderList(CInfo::GroupIndex(), CInfo::SourceType(), &m->elFile);
203         if (ret == false) {
204                 _ERR(" Fetching all list from file failed ");
205                 return;
206         }
207 }
208
209 void CFileGrid::t_CreateFileGrid(void)
210 {
211         Evas_Object *grid;
212         
213         grid = elm_gengrid_add(m->eoBase);
214         if (!grid)
215                 return;
216         evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
217         elm_gengrid_align_set(grid, 0, 0);
218         elm_object_part_content_set(m->eoBase, FBR_PART_MEDIA_GRID, grid);
219
220         Connect(grid);
221
222         evas_object_smart_callback_add(grid, FBR_SIGNAL_REALIZED,      sm_CbRealized, this);
223         evas_object_smart_callback_add(grid, FBR_SIGNAL_UNREALIZED,    sm_CbUnrealized, this);
224         elm_gengrid_item_size_set(grid, elm_config_scale_get() * GENGRID_ITEM_SIZE_W, elm_config_scale_get() * GENGRID_ITEM_SIZE_H);
225
226         m->eoGrid = grid;
227
228         m->pGengridItemClass = new CGengridItemClass;
229         if (!m->pGengridItemClass->Create()) {
230                 delete m->pGengridItemClass;
231                 m->pGengridItemClass = NULL;
232                 evas_object_del(grid);
233                 _ERR(" elm_genlist_item_class_new failed ");
234                 return;
235         }
236
237         evas_object_show(grid);
238 }
239
240
241 void CFileGrid::t_Empty(bool sort_flag)
242 {
243         if (m->eoGrid) {
244                 elm_gengrid_clear(m->eoGrid);
245                 evas_object_del(m->eoGrid);
246                 m->eoGrid = NULL;
247         }
248
249         if (m->pGengridItemClass)
250         {
251                 m->pGengridItemClass->Destroy();
252                 delete m->pGengridItemClass;
253                 m->pGengridItemClass = NULL;
254         }
255
256         if (sort_flag == false && m->elFile != NULL) {
257                 eina_list_free(m->elFile);
258                 m->elFile = NULL;
259         }
260
261         m->elItemInfo = NULL;
262         if (m->callback.cbUpdateEmptyFocusSequence)
263                 m->callback.cbUpdateEmptyFocusSequence(m->callback.cookie);
264 }
265
266
267 void CFileGrid::t_Update(char *fid, bool sort_flag)
268 {
269         _DBG("fid: %s, sort_flag: %d", fid, sort_flag);
270
271         elm_gengrid_clear(m->eoGrid);
272         if (sort_flag == false && m->elFile != NULL) {
273                 eina_list_free(m->elFile);
274                 m->elFile = NULL;
275         }
276
277         m->elItemInfo = NULL;
278         if (m->callback.cbUpdateEmptyFocusSequence)
279                 m->callback.cbUpdateEmptyFocusSequence(m->callback.cookie);
280
281         if (sort_flag == false) {
282                 if (fid)
283                         m_GetMediaList(fid);
284                 else
285                         m_GetFolderList();
286         }
287         
288         const char* sortType;
289         switch(CInfo::SortType())
290         {
291                 case 0:
292                         sortType = SORT_BY_NAME_AZ;
293                         break;
294                 case 1:
295                         sortType = SORT_BY_NAME_ZA;
296                         break;
297                 case 2:
298                         sortType = SORT_BY_DATE_ASC;
299                         break;
300                 case 3:
301                         sortType = SORT_BY_DATE_DES;
302                         break;
303                 case 4:
304                         sortType = SORT_BY_SIZE_ASC;
305                         break;
306                 case 5:
307                         sortType = SORT_BY_SIZE_DES;
308                         break;
309                 default:
310                         _DBG("Fix your code : sortType is invalid");
311                         sortType = SORT_BY_NAME_AZ;
312                         break;
313         }
314
315         m->elFile = CSortMgr::Sort(m->elFile, sortType);
316         t_Fill();
317
318         if (fid)    // folder
319                 m->depth = 1;
320         else
321                 m->depth = 0;
322 }
323
324
325 void CFileGrid::t_Fill(void)
326 {
327         CExtNameInfo *pInfo;
328         Eina_List *l;
329         SItemInfo *pItemInfo;
330         void* obj;
331         int ts;
332
333         _DBG("grid: %x, flist: %x", m->eoGrid, m->elFile);
334         if (!m->eoGrid || !m->elFile)
335                 return;
336
337         ts = eina_list_count(m->elFile);
338         _DBG("count: %d", ts);
339         if (ts == 0) {
340                 return;
341         }
342
343         EINA_LIST_FOREACH(m->elFile, l, obj) {
344                 pInfo = (CExtNameInfo*)obj;
345                 pItemInfo = (SItemInfo *)calloc(1, sizeof(SItemInfo));
346                 if (!pItemInfo) {
347                         _DBG("Fail to allocate memory!!!!!!");
348                         return;
349                 }
350
351                 pItemInfo->pInfo = pInfo;
352                 pItemInfo->item = elm_gengrid_item_append(m->eoGrid, m->pGengridItemClass->Handle(), pItemInfo, NULL, m);
353                 m->elItemInfo = eina_list_append(m->elItemInfo, pItemInfo);
354         }
355
356         if (m->callback.cbUpdatedGrid)
357                 m->callback.cbUpdatedGrid(m->callback.cookie);
358 }
359
360
361 bool CFileGrid::Create(Evas_Object* win, Evas_Object *base, const SCallback *cb)
362 {
363         ASSERT(!m);
364         ASSERT(cb);
365
366         _CREATE_BEGIN{
367                 _CHECK(m = new SFileGrid)
368                 _CHECK(m->pMediadata = new CMediadata )
369                 _CHECK(m->pMediadata->Create())
370
371                 _WHEN_SUCCESS{
372                         m->eoWin    = win;
373                         m->eoBase   = base;
374                         m->callback = *cb;
375                         t_CreateFileGrid();
376                 }
377
378                 _CHECK_FAIL{ m->pMediadata->Destroy(); }
379                 _CHECK_FAIL{ delete m->pMediadata; }
380                 _CHECK_FAIL{ delete m; m = NULL; }
381         } _CREATE_END_AND_CATCH{ return false; }
382
383
384         return true;
385 }
386
387
388 void CFileGrid::Destroy(void)
389 {
390         ASSERT(m);
391
392         t_Empty(false);
393
394         m_DeleteContextView();
395
396         free(m->fid);
397
398         m->pMediadata->Destroy();
399         delete m->pMediadata;
400         delete m;
401         m = NULL;
402 }
403
404
405 void CFileGrid::Update(bool sort_flag)
406 {
407         ASSERT(m);
408
409         char* fid = NULL;
410         if (sort_flag == true) {
411                 fid = m->fid;
412         }
413         t_Update(fid, sort_flag);
414 }
415
416
417 void CFileGrid::SetFocus(Eina_Bool flag)
418 {
419         ASSERT(m);
420
421         elm_object_focus_set(m->eoGrid, flag);
422 }
423
424
425 Evas_Object* CFileGrid::Grid(void)
426 {
427         ASSERT(m);
428
429         return m->eoGrid;
430 }
431
432
433 void CFileGrid::OnKeyUp(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Up *ev) {
434         Elm_Object_Item *it;
435         SItemInfo *pItemInfo;
436
437         if(ev->keyname)
438                 _DBG("%s", ev->keyname);
439
440         if (!strcmp(ev->keyname, KEY_BACK) || !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
441                 if (m->depth == 0) {
442                         if (m->callback.cbUnsetFocus)
443                                 m->callback.cbUnsetFocus(m->callback.cookie);
444                 }
445                 else {
446                         t_Update(NULL, false);
447                         it = _find_item_by_id(m->elItemInfo, m->fid);
448                         elm_gengrid_item_show(it, ELM_GENGRID_ITEM_SCROLLTO_IN);
449                         elm_object_item_focus_set(it, EINA_TRUE);
450                 }
451         }
452         else if (!strcmp(ev->keyname, KEY_ENTER) || !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
453                 it = elm_object_focused_item_get(obj);
454                 if (!it) {
455                         _ERR(" unable to get focused item ");
456                         return;
457                 }
458                 pItemInfo = _find_item_info(m->elItemInfo, it);
459                 if (!pItemInfo)
460                         return;
461
462                 m->handle_itinfo = pItemInfo;
463                 m_HandleSelection();
464         }
465         else if (!strcmp(ev->keyname, KEY_MENU) || !strcmp(ev->keyname, KEY_MENU_REMOTE)) {
466                 it = elm_object_focused_item_get(obj);
467                 if (!it) {
468                         _ERR(" unable to get focused item ");
469                         return;
470                 }
471                 pItemInfo = _find_item_info(m->elItemInfo, it);
472                 if (!pItemInfo)
473                         return;
474
475                 m->ctxt_itinfo = pItemInfo;
476
477                 CViewMgr::GetInstance()->PushView(FBR_CONTEXT_VIEW, pItemInfo->pInfo);
478         }
479 }
480
481
482 void CFileGrid::OnMouseMove(int id, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev) {
483         _DBG();
484         Elm_Object_Item *item;
485
486         if (!obj)
487                 return;
488
489         item = elm_gengrid_at_xy_item_get(obj, ev->cur.canvas.x,
490                 ev->cur.canvas.y, NULL, NULL);
491         if (!item)
492                 return;
493
494         elm_object_item_focus_set(item, EINA_TRUE);
495 }