c0ccdb10a61c4007ff27857f1ff400b9c5a71a37
[profile/tv/apps/native/settings.git] / src / view_uigadget.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 <ui-gadget.h>
18 #include "view_uigadget.h"
19 #include <AppCommon.h>
20 #include "common_defs.h"
21 #include "settingviewmgr.h"
22
23
24 /**
25  *  This callback function is invoked with base layout for layout arrangement
26  *  after UI Gadget create create operation is completed.
27  *
28  *  @param ug [in] The handler data representing a UI gadget.
29  *  @param mode [in] The UI gadget mode.
30  *  @param priv [in] The function specific data passed by UG caller.
31  *  @return void.
32  */
33 void CUiGadget::sm_CbLayout(ui_gadget_h ug, enum ug_mode mode, void *priv)
34 {
35         CUiGadget *root = (CUiGadget*)priv;
36         if (root)
37                 root->t_OnLayout(ug, mode);
38 }
39
40
41 /**
42  *  After UG module invoke ug_send_message to send result,
43  *  UG library call this callback function registered by calling ug_create.
44  *
45  *  @param    ug     [in] The handler data representing a UI gadget.
46  *  @param    result [in] The Service handler.
47  *  @param    priv   [in] The function specific data passed by UG caller.
48  *  @return void.
49  */
50 void CUiGadget::sm_CbResult(ui_gadget_h ug, service_h result, void *priv)
51 {
52         CUiGadget *root = (CUiGadget*)priv;
53         if (root)
54                 root->t_OnResult(ug, result);
55 }
56
57
58
59 /**
60  *  Send the destroy request, use ug_destroy_me,
61  *  then UG library calls this callback function registered by UG caller.
62  *
63  *  @param    ug   [in] The handler data representing a UI gadget.
64  *  @param    priv [in] The function specific data passed by UG caller.
65  *  @return   void.
66  */
67 void CUiGadget::sm_CbDestroy(ui_gadget_h ug, void *priv)
68 {
69         CUiGadget *root = (CUiGadget*)priv;
70         if (root)
71                 root->t_OnDestroy(ug);
72 }
73
74
75 void CUiGadget::sm_CbEnd(ui_gadget_h ug, void *priv)
76 {
77         CUiGadget *root = (CUiGadget*)priv;
78         if (root)
79                 root->t_OnEnd(ug);
80 }
81
82
83 bool CUiGadget::Create(ui_gadget_h parent, const char *name, ug_mode mode, service_h service)
84 {
85         ASSERT(!m_handler);
86
87         struct ug_cbs cbs = {
88                 sm_CbLayout,
89                 sm_CbResult,
90                 sm_CbDestroy,
91                 sm_CbEnd
92         };
93         cbs.priv = this;
94
95         m_handler = ug_create(NULL, name, UG_MODE_FRAMEVIEW, NULL, &cbs);
96
97         return m_handler ? true : false;
98 }
99
100
101 void CUiGadget::Destroy(void)
102 {
103         if (!m_handler)
104                 return;
105
106         ug_destroy(m_handler);
107         m_handler = NULL;
108 }
109
110
111 struct SUiGadgetView {
112         Evas_Object *win;
113         Evas_Object *base;
114         Evas_Object *subbtn;
115         Evas_Object *main_item_box;
116         Evas_Object *subitem_box;
117
118         const char *display_name;
119         CSettingMgr *mgr;
120         struct settingview_data *view;
121 };
122
123 /**
124 *  This function is invoked to hide view layout.
125 *
126 *  @param base [in] The view layout evas object.
127 *  @return void.
128 */
129 void CUiGadgetView::Hide(void)
130 {
131         ASSERT(m);
132
133         evas_object_hide(m->base);
134 }
135
136 /**
137 *  Hide current ug launcher view, then pop this view from view list and return to main view.
138 *
139 *  @param data [in] The function specific data which hold _data pointer.
140 *  @return void.
141 */
142 void CUiGadgetView::m_BackToMainview(void)
143 {
144         Hide();
145
146         m->mgr->ViewPop();
147 }
148
149
150 /**
151 *  This function is invoked to show sub item buttons evas object.
152 *
153 *  @param box [in] The box evas object contained sub item buttons.
154 *  @return void.
155 */
156 static void _show_item_btns(Evas_Object *box)
157 {
158         Eina_List *list, *l;
159         Evas_Object *btn;
160         void* obj;
161
162         if (!box)
163                 return;
164
165         list = elm_box_children_get(box);
166         if (!list) {
167                 _ERR("button list is null.");
168                 return;
169         }
170
171         EINA_LIST_FOREACH(list, l, obj)
172         {
173                 btn = (Evas_Object *) obj;
174                 evas_object_show(btn);
175         }
176
177         eina_list_free(list);
178 }
179
180
181 void CUiGadgetView::t_OnDestroy(ui_gadget_h ug)
182 {
183         if (!ug) {
184                 _ERR("Invalid parameters in ug destroy callback.");
185                 return;
186         }
187
188         _show_item_btns(m->subitem_box);
189
190         elm_object_focus_set(m->subbtn, EINA_TRUE);
191         elm_object_part_text_set(m->base, UG_TITLE_TEXT, "");
192
193         CUiGadget::Destroy();
194
195         m_BackToMainview();
196 }
197
198 /**
199 *  This function is invoked to create UI Gadget with specific UG name.
200 *
201 *  @param data [in] The function specific data which hold _data pointer.
202 *  @return 0 if success, -1 if fail.
203 */
204 bool CUiGadgetView::m_Load(void)
205 {
206         const char *name;
207         struct settingitem *parent;
208
209         parent = viewdata_get_parentitem(m->view);
210         name = settingitem_get_settingui_name(parent);
211         elm_object_part_text_set(m->base, UG_TITLE_TEXT, m->display_name);
212
213         if (!CUiGadget::Create(NULL, name, UG_MODE_FRAMEVIEW, NULL)) {
214                 return false;
215         }
216
217         m->mgr->FreezeTimeout();
218
219         return true;
220 }
221
222
223 /**
224 *  This function is invoked to hide sub item buttons evas object.
225 *
226 *  @param box [in] The box evas object contained sub item buttons.
227 *  @return void.
228 */
229 static void _hide_item_btns(Evas_Object *box)
230 {
231         Eina_List *list, *l;
232         Evas_Object *btn;
233         void* obj;
234
235         if (!box) {
236                 _ERR("item box is null.");
237                 return;
238         }
239
240         list = elm_box_children_get(box);
241         if (!list) {
242                 _ERR("button list is null.");
243                 return;
244         }
245
246         EINA_LIST_FOREACH(list, l, obj)
247         {
248                 btn = (Evas_Object *) obj;
249                 evas_object_hide(btn);
250         }
251
252         eina_list_free(list);
253 }
254
255
256 Evas_Object *CUiGadgetView::Base(void)
257 {
258         ASSERT(m);
259
260         return m->base;
261 }
262
263
264 /**
265 *  This function is invoked to create UG launcher view layout.
266 *
267 *  @param mgr [in] The setting_mgr data pointer passed by @settingmgr_view_push.
268 *  @param view [in] The settingview_data data pointer passed by @settingmgr_view_push.
269 *  @param prev [in] The parameter data which passed by prev view or NULL.
270 *  @return View layout evas object, NULL on error.
271 */
272 bool CUiGadgetView::Create(struct settingview_data *view, void *prev)
273 {
274         ASSERT(!m);
275         ASSERT(view);
276         ASSERT(prev);
277
278         Evas_Object *win, *base;
279         struct evas_obj_data *param;
280
281         CSettingMgr *mgr = CSettingMgr::GetInstance();
282
283         param = (struct evas_obj_data *) prev;
284         _hide_item_btns(param->subitem_box);
285
286         win = mgr->Window();
287         ASSERT(win);
288
289         m = new SUiGadgetView;
290         if (!m) {
291                 _ERR("calloc SUiGadgetView failed.");
292                 return false;
293         }
294
295         base = elm_layout_add(win);
296         if (!base) {
297                 _ERR("add base layout failed.");
298                 delete m;
299                 m = NULL;
300                 return false;
301         }
302
303         elm_layout_file_set(base, EDJ_FILE, UG_GROUP);
304
305         m->win  = win;
306         m->base = base;
307         m->mgr  = mgr;
308         m->view = view;
309         m->subitem_box  = param->subitem_box;
310         m->display_name = param->display_name;
311         m->subbtn       = param->cur_btn;
312
313         if (!m_Load()) {
314                 _ERR("load ui gadget failed.");
315                 evas_object_del(base);
316                 delete m;
317                 m = NULL;
318                 
319                 return false;
320         }
321
322         return true;
323 }
324
325
326 void CUiGadgetView::Show(void)
327 {
328         ASSERT(m);
329
330         evas_object_show(m->base);
331 }
332
333
334 void CUiGadgetView::Destroy(void)
335 {
336         ASSERT(m);
337
338         CUiGadget::Destroy();
339
340         if (m->view)
341                 viewdata_release(m->view);
342
343         evas_object_del(m->base);
344         delete m;
345         m = NULL;
346 }