dbg.h file is removed.
[profile/tv/apps/native/source.git] / src / view / plugged_layout.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 "def.h"
18 #include <AppCommon.h>
19 #include "external.h"
20 #include "source_mgr.h"
21 #include "menu_view.h"
22 #include "util.h"
23 #include "allsource_view.h"
24 #include "plugged_layout.h"
25
26 #define PLUGGED_GRID 0
27
28 #define PRIV "priv"
29 #define PLUGGED_DATA "plugged_data"
30
31 #define TEXT_NO_SRC_TEXT_LINE1 "No plugged source"
32 #define TEXT_NO_SRC_TEXT_LINE2 "Check connection status or install any cloud service."
33
34 struct SPluggedLayout {
35         Evas_Object *base;
36         Evas_Object *grid;
37         Elm_Object_Item *focus_it;
38         Elm_Object_Item *del_it;
39         CSourceMgr *srcmgr;
40         CViewMgr *vmgr;
41
42         SSigCallbackPair *sigCb;
43
44         SPluggedLayout() {
45                 memset(this, 0, sizeof(SPluggedLayout));
46         }
47 };
48
49 Evas_Object *CPluggedLayout::m_DrawGengrid(Evas_Object *layout)
50 {
51         Evas_Object *grid;
52
53         grid = util_add_grid(layout, ITEM_W, ITEM_H);
54         if (!grid)
55                 return NULL;
56
57         Connect(grid, PLUGGED_GRID, TYPE_KEY_DOWN | TYPE_MOUSE_MOVE
58                         | TYPE_REALIZED | TYPE_UNREALIZED | TYPE_FOCUSED);
59
60         return grid;
61 }
62
63 bool CPluggedLayout::Create(CLayoutMgr *lmgr, void *data)
64 {
65         ASSERT(!m);
66
67         Evas_Object *layout;
68         CViewMgr *vmgr;
69         CSourceMgr *srcmgr;
70
71         if (!lmgr)
72                 return false;
73
74         m = new SPluggedLayout;
75         if (!m)
76                 return false;
77
78         m->base = lmgr->Base();
79         if (!m->base) {
80                 delete m;
81                 m = NULL;
82                 return false;
83         }
84
85         layout = util_add_layout(m->base, GRP_PLUGGED_CONTENT);
86         if (!layout) {
87                 _ERR("Add layout failed");
88                 delete m;
89                 m = NULL;
90                 return false;
91         }
92
93         m->grid = m_DrawGengrid(layout);
94         if (!m->grid) {
95                 _ERR("Add gengrid failed");
96                 delete m;
97                 m = NULL;
98                 evas_object_del(layout);
99                 return false;
100         }
101
102         vmgr = CViewMgr::GetInstance();
103         srcmgr = CSourceMgr::GetInstance();
104
105         m->srcmgr = srcmgr;
106         m->vmgr = vmgr;
107         m->sigCb = (SSigCallbackPair *)data;
108
109         evas_object_data_set(m->base, PLUGGED_DATA, m);
110
111         elm_object_part_content_set(layout, PART_CONTENT_CONTENT, m->grid);
112
113         CBaseLayout::Create(layout);
114
115         return true;
116 }
117
118 void CPluggedLayout::t_OnShow(void)
119 {
120         ASSERT(m);
121
122         CBaseLayout::t_OnShow();
123
124         elm_object_part_content_set(m->base, PART_CONTENT, Layout());
125 }
126
127 void CPluggedLayout::t_OnHide(void)
128 {
129         ASSERT(m);
130
131         CBaseLayout::t_OnHide();
132
133         elm_object_part_content_unset(m->base, PART_CONTENT);
134 }
135
136 void CPluggedLayout::Destroy(void)
137 {
138         ASSERT(m);
139
140         evas_object_del(Layout());
141
142         CBaseLayout::Destroy();
143
144         delete m;
145         m = NULL;
146 }
147
148 void CPluggedLayout::m_DrawNoContentArea(void)
149 {
150         elm_object_part_text_set(Layout(), PART_NO_SRC_TEXT_LINE1,
151                                         _(TEXT_NO_SRC_TEXT_LINE1));
152         elm_object_part_text_set(Layout(), PART_NO_SRC_TEXT_LINE2,
153                                         _(TEXT_NO_SRC_TEXT_LINE2));
154
155         elm_object_part_content_unset(Layout(), PART_CONTENT_CONTENT);
156         evas_object_hide(m->grid);
157
158         m->sigCb->cb(m->sigCb->cookie, E_RETURN);
159 }
160
161 void CPluggedLayout::m_DrawContentArea(void)
162 {
163         elm_object_part_text_set(Layout(), PART_NO_SRC_TEXT_LINE1, "");
164         elm_object_part_text_set(Layout(), PART_NO_SRC_TEXT_LINE2, "");
165
166         elm_object_part_content_set(Layout(), PART_CONTENT_CONTENT, m->grid);
167         evas_object_show(m->grid);
168 }
169
170
171 Eina_Bool CPluggedLayout::sm_CbTimerDelItem(void *data)
172 {
173         CPluggedLayout *root = (CPluggedLayout *)data;
174         if (!root)
175                 return EINA_FALSE;
176
177         root->m_TimerDelItem();
178
179         return EINA_FALSE;
180 }
181
182 void CPluggedLayout::m_TimerDelItem(void)
183 {
184         Elm_Object_Item *prev;
185         Elm_Object_Item *next;
186
187         if (!m->del_it)
188                 return;
189
190         prev = elm_gengrid_item_prev_get(m->del_it);
191         next = elm_gengrid_item_next_get(m->del_it);
192
193         elm_object_item_data_set(m->del_it, NULL);
194         elm_object_item_del(m->del_it);
195
196         if (!prev && !next)
197                 m_DrawNoContentArea();
198         else if (m->del_it == m->focus_it) {
199                 if (prev)
200                         m->focus_it = prev;
201                 else
202                         m->focus_it = next;
203
204                 elm_object_item_focus_set(m->focus_it, EINA_TRUE);
205         }
206
207         m->del_it = NULL;
208 }
209
210 void CPluggedLayout::sm_CbGridItemFocused(void *cookie, Elm_Object_Item *it,
211                 const char *emission, const char *source)
212 {
213         CPluggedLayout *root = (CPluggedLayout *)cookie;
214
215         if (root)
216                 root->m_OnGridItemFocused(it, emission, source);
217 }
218
219 void CPluggedLayout::m_OnGridItemFocused(Elm_Object_Item *it,
220                 const char *emission, const char *source)
221 {
222         if (!it)
223                 return;
224
225         m->focus_it = (Elm_Object_Item *)it;
226 }
227
228 void CPluggedLayout::Focus(void)
229 {
230         ASSERT(m);
231
232         elm_object_focus_set(m->grid, EINA_TRUE);
233         elm_object_item_focus_set(m->focus_it, EINA_TRUE);
234 }
235
236 void CPluggedLayout::Update(void *data, enum update_ui type)
237 {
238         ASSERT(m);
239
240         Eina_List *list, *l;
241         Ecore_Timer *timer;
242         CSourceMgr *srcmgr;
243         CSourceInfo *si;
244         CSourceInfo *si_data;
245         int count;
246         void *obj;
247
248         srcmgr = m->srcmgr;
249         list = srcmgr->PluggedList();
250         count = eina_list_count(list);
251
252         if (type == SRC_PLUG_ADD_ONE) {
253                 si_data = (CSourceInfo *)data;
254                 util_add_item(m->grid, si_data);
255
256                 if (count >= 1)
257                         m_DrawContentArea();
258         } else if (type == SRC_PLUG_DEL_ONE && data) {
259                 si_data = (CSourceInfo *)data;
260                 m->del_it = (Elm_Object_Item*)si_data->Data();
261                 si_data->SetData(NULL);
262
263                 timer = ecore_timer_add(DEL_IT_TIME, sm_CbTimerDelItem, this);
264                 elm_object_item_data_set(m->del_it, timer);
265                 util_dim_item(m->del_it);
266         } else if (type == SRC_UPDATE_ALL) {
267                 if (count == 0) {
268                         m_DrawNoContentArea();
269                         return;
270                 } else
271                         m_DrawContentArea();
272
273                 elm_gengrid_clear(m->grid);
274
275                 EINA_LIST_FOREACH(list, l, obj) {
276                         si = (CSourceInfo *)obj;
277                         util_add_item(m->grid, si);
278                 }
279         }
280 }
281
282 void CPluggedLayout::OnKeyDown(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev)
283 {
284         switch (id) {
285         case PLUGGED_GRID:
286                 CSourceInfo *si;
287
288                 si = m->srcmgr->SourceInfoByData(m->focus_it);
289                 if (!si)
290                         return;
291
292                 if (!strcmp(ev->keyname, KEY_MENU)
293                         || !strcmp(ev->keyname, KEY_MENU_REMOTE))
294                         m->vmgr->PushView(MENU_VIEW, si);
295                 else if (!strcmp(ev->keyname, KEY_BACK)
296                         || !strcmp(ev->keyname, KEY_BACK_REMOTE))
297                         m->sigCb->cb(m->sigCb->cookie, E_RETURN);
298                 break;
299         default:
300                 break;
301         }
302 }
303
304 void CPluggedLayout::OnMouseMove(int id, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev)
305 {
306         switch (id) {
307         case PLUGGED_GRID:
308                 Elm_Object_Item *it;
309
310                 it = elm_gengrid_at_xy_item_get(obj, ev->cur.canvas.x,
311                                 ev->cur.canvas.y, NULL, NULL);
312                 if (!it)
313                         return;
314
315                 if (elm_object_item_focus_get(it))
316                         return;
317
318                 elm_object_item_focus_set(it, EINA_TRUE);
319
320                 m->sigCb->cb(m->sigCb->cookie, E_HIGHLIGHT);
321
322                 break;
323         default:
324                 break;
325         }
326 }
327
328 void CPluggedLayout::OnRealized(int id, Evas_Object *obj, Elm_Object_Item *item)
329 {
330         switch (id) {
331         case PLUGGED_GRID:
332                 elm_object_item_signal_callback_add(item, "item_focused",
333                                 "edc", sm_CbGridItemFocused, this);
334                 break;
335         default:
336                 break;
337         }
338 }
339
340 void CPluggedLayout::OnUnrealized(int id, Evas_Object *obj, Elm_Object_Item *item)
341 {
342         switch (id) {
343         case PLUGGED_GRID:
344                 elm_object_item_signal_callback_del(item, "item_focused",
345                                 "edc", sm_CbGridItemFocused);
346                 break;
347         default:
348                 break;
349         }
350 }
351
352 void CPluggedLayout::OnFocused(int id, Evas_Object *obj, Elm_Object_Item *item)
353 {
354         switch (id) {
355         case PLUGGED_GRID:
356                 m->sigCb->cb(m->sigCb->cookie, E_HIGHLIGHT);
357
358                 break;
359         default:
360                 break;
361         }
362 }
363