Modify parameters of t_SetList()
[profile/tv/apps/native/filebrowser.git] / src / views / BaseView / FbBaseView.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 "i18n.h"
22 #include "define.h"
23
24 #include "AppCommon.h"
25 #include "ExtNameInfo.h"
26 #include "common.h"
27 #include "dbg.h"
28 #include "ManagerUsb.h"
29 #include "SortName.h"
30 #include "Popup.h"
31 #include "InputHandler.h"
32
33 #include <CtxPopup.h>
34
35 #include <BaseView.h>
36 #include "FbBaseView.h"
37 #include "Info.h"
38
39 #include "SourceCtxPopup.h"
40 #include "SortCtxPopup.h"
41
42 #include <ViewMgr.h>
43 #include <LayoutMgr.h>
44 #include <BaseLayout.h>
45 #include "FbBaseLayout.h"
46
47 #define TOTAL_GROUP_BTNS        4
48 #define TOTAL_TOP_BTNS          2
49 #define TOTAL_UPDATE_CALLBACKS  2
50 #define PARAM_URI               "uri"
51 #define PARAM_SOURCE            "source"
52
53 struct _btn_info {
54         const char *txt;
55         const char *part;
56         const char *style;
57         const char *icon_path;
58 };
59
60 enum topbtns {
61         TOPBTN_SOURCE,
62         TOPBTN_SORT,
63 };
64
65 const char *source_arg[] = {
66         "all",
67         "tv",
68         "pUsb"
69 };
70
71 const char *linked_app[] = {
72         N_(""),
73         N_("org.tizen.gallery-tv-ref"),
74         N_("org.tizen.video-player-tv-ref"),
75         N_("org.tizen.music-player-tv-ref"),
76         N_(""),
77 };
78
79 struct SFbBaseView {
80         Evas_Object *eoWin;
81         Evas_Object *eoBase;
82         Evas_Object *eoBtnGroup[TOTAL_GROUP_BTNS];
83         Evas_Object *eoSelectedBtnGroup;//Evas_Object *c_grpbtn;
84         Evas_Object *eoBtnSource;
85         Evas_Object *eoBtnSort;
86
87         CCtxPopup   *pCtxPopup;
88
89         CPopup      *pPopup;
90
91         CLayoutMgr    *pLayoutMgr;
92         CFbBaseLayout *pFbBaseLayout;
93
94         SFbBaseView() {
95                 int a;
96
97                 memset(eoBtnGroup, 0, sizeof(eoBtnGroup));
98                 pHandlerBase = new CHandlerBase(this);
99                 for (a = 0; a < TOTAL_TOP_BTNS; a++)
100                         pHandlerButton[a] = new CHandlerButton(this);
101                 for (a = 0; a < TOTAL_GROUP_BTNS; a++){
102                         pHandlerGroup[a] = new CHandlerGroup(this);
103                 }
104         }
105         virtual ~SFbBaseView() {
106                 int a;
107
108                 delete pHandlerBase;
109                 for (a = 0; a < TOTAL_TOP_BTNS; a++)
110                         delete pHandlerButton[a];
111                 for (a = 0; a < TOTAL_GROUP_BTNS; a++)
112                         delete pHandlerGroup[a];
113         }
114         
115         class CHandlerBase : public CListenerMgr, public IKeyDownListener {
116                 SFbBaseView *m;
117
118         public:
119                 CHandlerBase(SFbBaseView *ins) : IKeyDownListener(this) { m = ins; }
120
121                 virtual void OnKeyDown(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev) {
122                         if (!strcmp(ev->keyname, KEY_EXIT))
123                                 elm_exit();
124                         else if (!strcmp(ev->keyname, KEY_ENTER)) {
125                                 if (elm_object_focus_get(m->eoSelectedBtnGroup)) {
126                                         m->pFbBaseLayout->SetFocus(EINA_TRUE);
127                                 }
128                         }
129                 }
130         } *pHandlerBase;
131
132         class CHandlerButton : public CListenerMgr, protected IKeyDownListener, IMouseMoveListener, IMouseClickedListener {
133         private:
134                 SFbBaseView *m;
135         private:
136                 static void sm_CbCtxPopupSelected(void* cookie, CCtxPopup* instance, const char* text)
137                 {
138                         SFbBaseView* m = (SFbBaseView*)cookie;
139                         if (!m)
140                                 return;
141
142                         _DBG(" instance->Type(): %d", instance->Type());
143                         switch (instance->Type()) {
144                         case CCtxPopup::TOPBTN_SORT:
145                                 elm_object_text_set(m->eoBtnSort, text);
146                                 m->pFbBaseLayout->Update(true);
147                                 break;
148
149                         case CCtxPopup::TOPBTN_SOURCE:
150                                 elm_object_text_set(m->eoBtnSource, text);
151                                 m->pFbBaseLayout->Update(false);
152                                 break;
153
154                         default:
155                                 break;
156                         }
157                 }
158
159         public:
160                 CHandlerButton(SFbBaseView *ins) : IKeyDownListener(this), IMouseMoveListener(this), IMouseClickedListener(this) { m = ins; }
161
162                 virtual void OnKeyDown(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev) {
163                         if (!strcmp(ev->keyname, KEY_BACK))
164                                 elm_object_focus_set(m->eoSelectedBtnGroup, EINA_TRUE);
165                 }
166                 virtual void OnMouseMove(int id, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev) {
167                         if (!elm_object_focus_get(obj))
168                                 elm_object_focus_set(obj, EINA_TRUE);
169                 }
170                 virtual void OnMouseClicked(int id, Evas_Object *obj) {
171                         CSourceCtxPopup::SCallback cb;
172                         cb.cookie = m;
173                         cb.onSelected = sm_CbCtxPopupSelected;
174
175                         if (obj == m->eoBtnSource) {
176                                 CSourceCtxPopup* pCtxPopup = new CSourceCtxPopup;
177                                 m->pCtxPopup = pCtxPopup;
178                                 pCtxPopup->Create(m->eoBase, &cb);
179                         }
180                         else if (obj == m->eoBtnSort) {
181                                 CSortCtxPopup* pCtxPopup = new CSortCtxPopup;
182                                 m->pCtxPopup = pCtxPopup;
183                                 pCtxPopup->Create(m->eoBase, &cb);
184                         }
185                 }
186         } *pHandlerButton[TOTAL_TOP_BTNS];
187
188         class CHandlerGroup : public CListenerMgr, IMouseMoveListener, IKeyDownListener, IMouseClickedListener {
189         private:
190                 SFbBaseView *m;
191         public:
192                 CHandlerGroup(SFbBaseView *ins) :
193                         IMouseMoveListener(this),
194                         IKeyDownListener(this),
195                         IMouseClickedListener(this) { m = ins; }
196
197                 virtual void OnMouseMove(int id, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev) {
198                         if (!elm_object_focus_get(obj))
199                                 elm_object_focus_set(obj, EINA_TRUE);
200                 }
201                 virtual void OnKeyDown(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev) {
202                         if (!strcmp(ev->keyname, KEY_BACK))
203                                 elm_exit();
204                 }
205                 virtual void OnMouseClicked(int id, Evas_Object *obj) {
206                         if (m->eoSelectedBtnGroup == obj)
207                                 return;
208
209                         elm_object_signal_emit(m->eoSelectedBtnGroup, FBR_SIGNAL_GROUP_UNSELECTED, "");
210                         m->eoSelectedBtnGroup = obj;
211                         elm_object_signal_emit(m->eoSelectedBtnGroup, FBR_SIGNAL_GROUP_SELECTED, "");
212
213                         m->pFbBaseLayout->Update(false);
214                 }
215         } *pHandlerGroup[TOTAL_GROUP_BTNS];
216 };
217
218
219
220 Evas_Object *_add_button(Evas_Object *box)
221 {
222         Evas_Object *button;
223
224         if (!box)
225                 return NULL;
226
227         button = elm_button_add(box);
228         if (!button)
229                 return NULL;
230
231         evas_object_size_hint_weight_set(button,
232                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
233         evas_object_show(button);
234
235         return button;
236 }
237
238 void CFbBaseView::UpdateEmptyFocusSequence(void)
239 {
240         int i;
241         _DBG("");
242
243         for (i = 0; i < TOTAL_GROUP_BTNS; i++)
244                 elm_object_focus_next_object_set(m->eoBtnGroup[i], m->eoBtnSort, ELM_FOCUS_RIGHT);
245
246         elm_object_focus_next_object_set(m->eoBtnGroup[E_GRP_ALL], m->eoBtnSort,              ELM_FOCUS_UP);
247         elm_object_focus_next_object_set(m->eoBtnSort,              m->eoBtnGroup[E_GRP_ALL], ELM_FOCUS_DOWN);
248         elm_object_focus_next_object_set(m->eoBtnSource,               m->eoBtnGroup[E_GRP_ALL], ELM_FOCUS_DOWN);
249
250         if (t_SelectedGroupButton() == m->eoBtnGroup[E_GRP_ALL]) {
251                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT,     _(NO_FILE_MESSAGE));
252                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT_SUB, _(NO_FILE_MESSAGE_SUB));
253         }
254         else if (t_SelectedGroupButton() == m->eoBtnGroup[E_GRP_PHOTO]) {
255                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT,     _(NO_PHOTO_MESSAGE));
256                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT_SUB, _(NO_PHOTO_MESSAGE_SUB));
257         }
258         else if (t_SelectedGroupButton() == m->eoBtnGroup[E_GRP_VIDEO]) {
259                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT,     _(NO_VIDEO_MESSAGE));
260                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT_SUB, _(NO_VIDEO_MESSAGE_SUB));
261         }
262         else if (t_SelectedGroupButton() == m->eoBtnGroup[E_GRP_MUSIC]) {
263                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT,     _(NO_MUSIC_MESSAGE));
264                 elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT_SUB, _(NO_MUSIC_MESSAGE_SUB));
265         }
266 }
267
268 void CFbBaseView::UnsetFocus(void)
269 {
270         elm_object_focus_set(t_SelectedGroupButton(), EINA_TRUE);
271 }
272
273 void CFbBaseView::CbLinkApp(int type, int source_type, char *path)
274 {
275         bundle *args;
276         int r;
277
278         args = bundle_create();
279         if (!args) {
280                 _ERR("unable to create bundle");
281                 return;
282         }
283
284         bundle_add(args, PARAM_SOURCE, source_arg[source_type]);
285         bundle_add(args, PARAM_URI, path);
286
287         r = aul_launch_app(linked_app[type], args);
288         if (args)
289                 bundle_free(args);
290         if (r < 0) {
291                 _ERR(" Unable to launch linked app");
292                 return;
293         }
294
295         return;
296 }
297
298 void CFbBaseView::UpdatedGrid(void)
299 {
300         int i;
301
302         for (i = 0; i < TOTAL_GROUP_BTNS; i++)
303                 elm_object_focus_next_object_set(m->eoBtnGroup[i],
304                 m->pFbBaseLayout->Grid(), ELM_FOCUS_RIGHT);
305
306         elm_object_focus_next_object_set(m->eoBtnGroup[E_GRP_ALL],
307                 m->eoBtnSort, ELM_FOCUS_UP);
308         elm_object_focus_next_object_set(m->eoBtnSort,
309                 m->eoBtnGroup[E_GRP_ALL], ELM_FOCUS_LEFT);
310         elm_object_focus_next_object_set(m->eoBtnSort,
311                 m->pFbBaseLayout->Grid(), ELM_FOCUS_DOWN);
312         elm_object_focus_next_object_set(m->eoBtnSource,
313                 m->pFbBaseLayout->Grid(), ELM_FOCUS_DOWN);
314
315         elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT, "");
316         elm_object_part_text_set(m->eoBase, FBR_PART_NO_CONTENT_SUB, "");
317 }
318
319 void CFbBaseView::t_CreateTopSession(void)
320 {
321         _DBG();
322         int i;
323         Evas_Object *btn;
324         struct _btn_info btninfo[TOTAL_TOP_BTNS];
325         btninfo[TOPBTN_SOURCE].style = FBR_STYLE_SOURCE_BTN;
326         btninfo[TOPBTN_SOURCE].part  = FBR_PART_SOURCE_HOVERSEL;
327         
328         btninfo[TOPBTN_SORT].style = FBR_STYLE_SORT_BTN;
329         btninfo[TOPBTN_SORT].part  = FBR_PART_SORT_HOVERSEL;
330
331         elm_object_part_text_set(m->eoBase, FBR_PART_TITLE,
332                 _(FBR_TEXT_TITLE));
333
334         for (i = 0; i < TOTAL_TOP_BTNS; i++) {
335                 btn = _add_button(m->eoBase);
336                 if (!btn) {
337                         _ERR(" Adding btn failed ");
338                         return;
339                 }
340
341                 elm_object_style_set(btn, btninfo[i].style);
342                 elm_object_part_content_set(m->eoBase, btninfo[i].part, btn);
343
344                 m->pHandlerButton[i]->Connect(btn);
345
346                 if (i == TOPBTN_SOURCE)
347                         m->eoBtnSource = btn;
348                 else if (i == TOPBTN_SORT)
349                         m->eoBtnSort = btn;
350         }
351
352         elm_object_text_set(m->eoBtnSource, _(CSourceCtxPopup::SourceText(CInfo::SourceType())));
353         elm_object_text_set(m->eoBtnSort, _(CSortCtxPopup::SortText(CInfo::SortType())));
354 }
355
356 void CFbBaseView::t_CreateLeftSession(void)
357 {
358         _DBG();
359         Evas_Object *box, *ic;
360         int i;
361         char buf[MAX_LENGTH];
362         static struct _btn_info btninfo[TOTAL_GROUP_BTNS];
363         btninfo[E_GRP_ALL].txt = N_("ALL");
364         btninfo[E_GRP_ALL].icon_path = FBR_IMAGE_GRPBTN_ALL;
365         btninfo[E_GRP_PHOTO].txt = N_("PHOTO");
366         btninfo[E_GRP_PHOTO].icon_path = FBR_IMAGE_GRPBTN_PHOTO;
367         btninfo[E_GRP_VIDEO].txt = N_("VIDEO");
368         btninfo[E_GRP_VIDEO].icon_path = FBR_IMAGE_GRPBTN_VIDEO;
369         btninfo[E_GRP_MUSIC].txt = N_("MUSIC");
370         btninfo[E_GRP_MUSIC].icon_path = FBR_IMAGE_GRPBTN_MUSIC;
371
372         box = elm_box_add(m->eoBase);
373         if (!box)
374                 return;
375         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
376         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
377         evas_object_show(box);
378
379         for (i = 0; i < TOTAL_GROUP_BTNS; i++) {
380                 m->eoBtnGroup[i] = _add_button(box);
381                 if (!m->eoBtnGroup[i]) {
382                         _ERR(" failed to add button to box ");
383                         continue;
384                 }
385
386                 elm_box_pack_end(box, m->eoBtnGroup[i]);
387                 elm_object_style_set(m->eoBtnGroup[i], FBR_STYLE_GROUPBTN);
388                 elm_object_text_set(m->eoBtnGroup[i], _(btninfo[i].txt));
389                 
390                 m->pHandlerGroup[i]->Connect(m->eoBtnGroup[i]);
391                 ic = elm_image_add(m->eoBtnGroup[i]);
392                 if (!ic)
393                         continue;
394                 _DBG("");
395
396                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
397                         btninfo[i].icon_path);
398                 elm_image_file_set(ic, buf, NULL);
399                 elm_object_part_content_set(m->eoBtnGroup[i],
400                         FBR_PART_ELM_SWALLOWICON, ic);
401         }
402         _DBG("");
403
404         t_SetSelectedGroupButton(m->eoBtnGroup[E_GRP_ALL]);
405         _DBG("");
406
407         elm_object_signal_emit(m->eoBtnGroup[E_GRP_ALL],
408                 FBR_SIGNAL_GROUP_SELECTED, "");
409         elm_object_focus_set(m->eoBtnGroup[E_GRP_ALL], EINA_TRUE);
410         elm_object_part_content_set(m->eoBase, FBR_PART_GROUPBTN_BOX, box);
411 }
412
413 void CFbBaseView::t_CreateFullView(void)
414 {
415         t_CreateTopSession();
416         t_CreateLeftSession();
417         _DBG("UpdateLayout");
418         m->pFbBaseLayout->Update(false);
419 }
420
421 void CFbBaseView::t_SetSelectedGroupButton(Evas_Object* obj)
422 {
423         m->eoSelectedBtnGroup = obj;
424         int idx = -1;
425
426         int a;
427         for (a = 0; a < TOTAL_GROUP_BTNS; a++) {
428                 if (m->eoBtnGroup[a] == obj)
429                         idx = a;
430         }
431         CInfo::SetGroupIndex(idx);
432 }
433
434 Evas_Object* CFbBaseView::t_SelectedGroupButton(void)
435 {
436         return m->eoSelectedBtnGroup;
437 }
438
439 void CFbBaseView::t_OnShow(void)
440 {
441         _DBG();
442
443         evas_object_show(m->eoBase);
444
445         CBaseView::t_OnShow();
446 }
447
448 void CFbBaseView::t_OnUpdate(void *data)
449 {
450         _DBG();
451         m->pFbBaseLayout->Action();
452 }
453
454 void CFbBaseView::t_OnHide(void)
455 {
456         evas_object_hide(m->eoBase);
457
458         CBaseView::t_OnHide();
459 }
460
461 bool CFbBaseView::Create(void *data)
462 {
463         ASSERT(!m);
464
465         Evas_Object *eoWin = CViewMgr::GetInstance()->Window();
466         Evas_Object *eoBase = NULL;
467
468         int source_type = E_ALL;
469
470         _CREATE_BEGIN{
471                 _CHECK(m = new SFbBaseView)
472                 _CHECK(eoBase = elm_layout_add(eoWin))
473                 _CHECK(elm_layout_file_set(eoBase, EDJEFILE, FBR_BASE_VIEW))
474                 _COMMAND{
475                         evas_object_size_hint_weight_set(eoBase, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
476                         elm_win_resize_object_add(eoWin, eoBase);
477
478                         /* get the source_type using 'data' string */
479                         if (data) {
480                                 // Find Index
481                                 int i;
482                                 int arrsize = ARRAY_SIZE(source_arg);
483                                 for (i = 0; i < arrsize; i++) {
484                                         if (source_arg[i] && !strcmp(source_arg[i], (const char*)data)) {
485                                                 source_type = i;
486                                                 break;
487                                         }
488                                 }
489                                 if (source_type < E_ALL || source_type > E_USB)
490                                         source_type = E_ALL;
491                                 CInfo::SetSourceType(source_type);
492                                 m->eoWin = eoWin;
493                                 m->eoBase = eoBase;
494                         }
495                 }
496                 _CHECK(m->pLayoutMgr = new CLayoutMgr)
497                 _CHECK(m->pLayoutMgr->Create(eoBase, NULL))
498                 _CHECK(m->pFbBaseLayout = new CFbBaseLayout(FB_BASE_LAYOUT))
499                 _CHECK(m->pFbBaseLayout->Create(m->pLayoutMgr, NULL))
500                 _CHECK(m->pLayoutMgr->AddLayout(m->pFbBaseLayout))
501                 _CHECK(CUsbConnectionListener::Create())
502
503                 _WHEN_SUCCESS{}
504
505                 _CHECK_FAIL{ CUsbConnectionListener::Destroy(); }
506                 _CHECK_FAIL{ m->pLayoutMgr->RemoveLayout(m->pFbBaseLayout); }
507                 _CHECK_FAIL{ m->pFbBaseLayout->Destroy(); }
508                 _CHECK_FAIL{ delete m->pFbBaseLayout; m->pFbBaseLayout = NULL; }
509                 _CHECK_FAIL{ m->pLayoutMgr->Destroy(); }
510                 _CHECK_FAIL{ delete m->pLayoutMgr; m->pLayoutMgr = NULL; }
511                 _CHECK_FAIL{ /* elm_layout_file_set*/ }
512                 _CHECK_FAIL{ evas_object_del(eoBase); }
513                 _CHECK_FAIL{ delete m; m = NULL; }
514         } _CREATE_END_AND_CATCH{ return false; }
515
516
517         // original create grid
518         m->pLayoutMgr->Show(FB_BASE_LAYOUT);
519         CInfo::SetSortType(0/*CSort::SORT_NAME_AZ*/);
520         evas_object_data_set(eoBase, BASE_VIEW_DATA, this);
521         t_CreateFullView();
522         elm_object_focus_allow_set(eoBase, EINA_FALSE);
523         m->pHandlerBase->Connect(eoBase);
524         CBaseView::Create(NULL);
525         return true;
526 }
527
528 void CFbBaseView::Destroy(void)
529 {
530         ASSERT(m);
531
532         CBaseView::Destroy();
533         CUsbConnectionListener::Destroy();
534
535         m->pLayoutMgr->RemoveLayout(m->pFbBaseLayout);
536         m->pFbBaseLayout->Destroy();
537         delete m->pFbBaseLayout;
538
539         m->pLayoutMgr->Destroy();
540         delete m->pLayoutMgr;
541
542         evas_object_del(m->eoBase);
543
544         delete m;
545         m = NULL;
546 }
547
548
549 Evas_Object* CFbBaseView::Base(void)
550 {
551         ASSERT(m);
552
553         return m->eoBase;
554 }
555
556
557 void CFbBaseView::OnConnect(void)
558 {
559         m->pPopup = new CPopup;
560         if (!m->pPopup)
561                 return;
562         if (!m->pPopup->Create(m->eoBase)) {
563                 delete m->pPopup;
564                 m->pPopup = NULL;
565                 return;
566         }
567
568         if (CInfo::SourceType() == E_USB) {
569                 if (!FlagConnected()) {
570                         CInfo::SetSourceType(E_ALL);
571                         elm_object_text_set(m->eoBtnSource,
572                                 _(CSourceCtxPopup::SourceText(CInfo::SourceType())));
573                 }
574         }
575 }
576
577
578 void CFbBaseView::OnUpdated(const SEntity *entity)
579 {
580         m->pPopup->Destroy();
581         delete m->pPopup;
582         m->pPopup = NULL;
583
584         if (m->pCtxPopup) {
585                 m->pCtxPopup->Destroy();
586                 delete m->pCtxPopup;
587                 m->pCtxPopup = NULL;
588         }
589         if (CInfo::SourceType() != E_TV) {
590                 m->pFbBaseLayout->Update(false);
591                 elm_object_focus_set(t_SelectedGroupButton(), EINA_TRUE);
592         }
593 }
594