tizen 2.3 release
[apps/home/settings.git] / setting-common / src / setting-common-view.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-common-view.h>
22
23 #include <Elementary.h>
24
25 #include <setting-common-data-error.h>
26 #include <setting-debug.h>
27
28
29 #define MAX_VIEWNODE_NUM        15
30 typedef struct _SettingViewNode {
31         setting_view *view;
32         setting_view *topview;
33 } SettingViewNode;
34 static setting_view *g_cur_view;        /* use by 'End Key' process */
35 static SettingViewNode g_view_node_table[MAX_VIEWNODE_NUM];
36 static int g_view_node_table_cur_size = 0;
37
38 /**
39  * @brief Set current loaded view
40  *
41  * @param view The view to set
42  * @return #0 on success, else on failed
43  */
44 EXPORT_PUBLIC int setting_view_node_set_cur_view(setting_view *view)
45 {
46         g_cur_view = view;
47         return 0;
48 }
49 /**
50  * @brief Get current loaded view
51  *
52  * @return current view
53  */
54 EXPORT_PUBLIC
55 setting_view *setting_view_node_get_cur_view()
56 {
57         return g_cur_view;
58 }
59
60 /**
61  * @brief intialize the node table
62  *
63  * @return #0 on success, else on failed
64  */
65 EXPORT_PUBLIC int setting_view_node_table_intialize()
66 {
67         g_cur_view = NULL;
68         g_view_node_table_cur_size = 0;
69         return 0;
70 }
71
72 /**
73  * @brief Register a node for a viwe and its top view
74  *
75  * @param view The current view
76  * @param view The top view of current view
77  * @return #0 on success, else on failed
78  */
79 EXPORT_PUBLIC int setting_view_node_table_register(setting_view *view, setting_view *topview)
80 {
81         //SETTING_TRACE_BEGIN;
82         if (g_view_node_table_cur_size >= MAX_VIEWNODE_NUM) {
83                 return SETTING_RETURN_FAIL;
84         }
85         int idx = 0;
86         for (; idx < g_view_node_table_cur_size; idx++) {
87                 if (view == g_view_node_table[idx].view
88                                 && topview == g_view_node_table[idx].topview) {
89                         SETTING_TRACE("view node has been registered, ignore");
90                         return SETTING_RETURN_FAIL;
91                 }
92         }
93
94         g_view_node_table[g_view_node_table_cur_size].view = view;
95         g_view_node_table[g_view_node_table_cur_size].topview = topview;
96         g_view_node_table_cur_size++;
97         return 0;
98 }
99
100 /**
101  * @brief callback invoked when pressed hard end key
102  *
103  * @Deprecated
104  */
105 EXPORT_PUBLIC
106 int setting_view_cb_at_endKey(void *cb)
107 {
108         SETTING_TRACE_BEGIN;
109         int idx = 0;
110         SettingViewNode *viewnode = NULL;
111         for (; idx < g_view_node_table_cur_size; idx++) {
112                 if (g_cur_view == g_view_node_table[idx].view) {
113                         viewnode = &(g_view_node_table[idx]);
114                         break;
115                 }
116         }
117         if (viewnode && viewnode->view && viewnode->topview) {
118                 setting_view_change(viewnode->view, viewnode->topview, cb);
119         }
120         return 0;
121 }
122
123 /**
124  * @brief Get top view of certain view
125  *
126  * @param view The certain view
127  * @return #view's top view on success, else NULL on failed
128  */
129 EXPORT_PUBLIC
130 setting_view *setting_view_get_topview(setting_view *view)
131 {
132         SETTING_TRACE_BEGIN;
133         retv_if(NULL == view, NULL);
134         int idx = 0;
135         SettingViewNode *viewnode = NULL;
136         for (; idx < g_view_node_table_cur_size; idx++) {
137                 if (view == g_view_node_table[idx].view) {
138                         viewnode = &(g_view_node_table[idx]);
139                         break;
140                 }
141         }
142         if (viewnode && viewnode->topview) {
143                 return viewnode->topview;
144         }
145         else
146         {
147                 return NULL;
148         }
149         SETTING_TRACE_END;
150 }
151
152 /**
153  * @brief Replace top view of the certain view
154  *
155  * @param view The certain view
156  * @param topview The new top view
157  */
158 EXPORT_PUBLIC
159 void setting_view_update_topview(setting_view *view, setting_view *topview)
160 {
161         SETTING_TRACE_BEGIN;
162         ret_if(NULL == view);
163         int idx = 0;
164         SettingViewNode *viewnode = NULL;
165         for (; idx < g_view_node_table_cur_size; idx++) {
166                 if (view == g_view_node_table[idx].view) {
167                         viewnode = &(g_view_node_table[idx]);
168                         break;
169                 }
170         }
171         if (viewnode) {
172                 viewnode->topview = topview;
173         }
174         else
175         {
176         }
177 }
178
179
180 /**
181  * @brief Callback of view creating
182  *
183  * @param view The created view
184  * @param cb The view data passed between all callbacks
185  * @return #0 on success, else on failed
186  */
187 EXPORT_PUBLIC
188 int setting_view_create(setting_view *view, void *cb)
189 {
190         LAUNCH_SETTING_IN();
191         /* error check */
192         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
193
194         int ret = SETTING_RETURN_FAIL;
195
196         if (!view->is_create && view->create) {//error handle:create only when the view doesn't exit
197                 ret = view->create(cb);
198         }
199         LAUNCH_SETTING_OUT();
200         return ret;
201 }
202
203 /**
204  * @brief Callback of view destroying
205  *
206  * @param view The view being destroyed
207  * @param cb The view data passed between all callbacks
208  * @return #0 on success, else on failed
209  */
210 EXPORT_PUBLIC int setting_view_destroy(setting_view *view, void *cb)
211 {
212         /* error check */
213         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
214
215         int ret = SETTING_RETURN_FAIL;
216
217         if (view->is_create && view->destroy) {//error handle:destroy only when the view exits
218                 ret = view->destroy(cb);
219         }
220
221         return ret;
222 }
223 /**
224  * @brief Callback of view updating
225  *
226  * @param view The view being updated
227  * @param cb The view data passed between all callbacks
228  * @return #0 on success, else on failed
229  * @warning the function should be invoked on the view which will be toppest view
230  */
231 EXPORT_PUBLIC int setting_view_update(setting_view *view, void *cb)
232 {
233         /* error check */
234         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
235
236         int ret = SETTING_RETURN_FAIL;
237
238         if ((view->is_create == TRUE) && view->update) {//error handle:update only when the view exits
239                 ret = view->update(cb);
240         }
241
242         return ret;
243 }
244
245 /**
246  * @brief Callback of view cleanuping
247  *
248  * @param view The view being cleanuped
249  * @param cb The view data passed between all callbacks
250  * @return #0 on success, else on failed
251  * @warning the function should be invoked on the view which will be covered
252  */
253 EXPORT_PUBLIC
254 int setting_view_cleanup(setting_view *view, void *cb)
255 {
256         /* error check */
257         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
258
259         int ret = SETTING_RETURN_FAIL;
260
261         if (view->is_create && view->cleanup) {//error handle:cleanup only when the view exits
262                 ret = view->cleanup(cb);
263         }
264
265         return ret;
266 }
267
268 /**
269  * @brief Callback of view changing
270  *
271  * @param from_view The view being covered
272  * @param to_view The view go to cover
273  * @param cb The view data passed between all callbacks
274  * @return #0 on success, else on failed
275  * @warning the function should be invoked to change views
276  */
277 EXPORT_PUBLIC
278 int setting_view_change(setting_view *from_view, setting_view *to_view, void *cb)
279 {
280         SETTING_TRACE_BEGIN;
281         /* error check */
282         setting_retvm_if(!from_view || !to_view || !cb ,
283                          SETTING_GENERAL_ERR_NULL_DATA_PARAMETER,
284                          "Invalid arguement");
285
286         int ret = SETTING_RETURN_SUCCESS;
287         if (from_view == setting_view_get_topview(to_view)) {
288                 //from a parent view to his child view, don't need cleanup parent view
289         } else {
290                 //from a child view to his parent view, need cleanup child view
291                 ret = setting_view_cleanup(from_view, cb);
292         }
293
294         if (ret == SETTING_RETURN_FAIL) {
295                 return SETTING_RETURN_FAIL;
296         }
297
298         if (to_view->is_create == 1) {
299                 ret = setting_view_update(to_view, cb);
300         } else {
301                 ret = setting_view_create(to_view, cb);
302         }
303         g_cur_view = to_view;   /* compute new value of g_cur_view. */
304         SETTING_TRACE_END;
305         return ret;
306 }