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