Tizen release 1.0
[apps/home/settings.git] / setting-common / src / setting-common-view.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved 
5  *
6  * This file is part of org.tizen.setting
7  * Written by Hyejin Kim <hyejin0906.kim@samsung.com>
8  *
9  * PROPRIETARY/CONFIDENTIAL
10  * 
11  * This software is the confidential and proprietary information of 
12  * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
13  * disclose such Confidential Information and shall use it only in
14  * accordance with the terms of the license agreement you entered
15  * into with SAMSUNG ELECTRONICS.
16  *
17  * SAMSUNG make no representations or warranties about the suitability 
18  * of the software, either express or implied, including but not limited
19  * to the implied warranties of merchantability, fitness for a particular
20  * purpose, or non-infringement. SAMSUNG shall not be liable for any
21  * damages suffered by licensee as a result of using, modifying or
22  * distributing this software or its derivatives.
23  *
24  */
25 #include <setting-common-view.h>
26
27 #include <Elementary.h>
28
29 #include <setting-common-data-error.h>
30 #include <setting-debug.h>
31
32 #define MAX_VIEWNODE_NUM        15
33 typedef struct _SettingViewNode {
34         setting_view *view;
35         setting_view *topview;
36 } SettingViewNode;
37 static setting_view *g_cur_view;;       /* use by 'End Key' process */
38 static SettingViewNode g_view_node_table[MAX_VIEWNODE_NUM];
39 static int g_view_node_table_cur_size = 0;
40
41 int setting_view_node_set_cur_view(setting_view *view)
42 {
43         g_cur_view = view;
44         return 0;
45 }
46
47 setting_view *setting_view_node_get_cur_view()
48 {
49         return g_cur_view;
50 }
51
52 int setting_view_node_table_intialize()
53 {
54         g_cur_view = NULL;
55         g_view_node_table_cur_size = 0;
56         return 0;
57 }
58
59 int
60 setting_view_node_table_register(setting_view *view, setting_view *topview)
61 {
62         SETTING_TRACE_BEGIN;
63         if (g_view_node_table_cur_size >= MAX_VIEWNODE_NUM) {
64                 return SETTING_RETURN_FAIL;
65         }
66
67         g_view_node_table[g_view_node_table_cur_size].view = view;
68         g_view_node_table[g_view_node_table_cur_size].topview = topview;
69         g_view_node_table_cur_size++;
70         return 0;
71 }
72
73 int setting_view_cb_at_endKey(void *cb)
74 {
75         SETTING_TRACE_BEGIN;
76         int idx = 0;
77         SettingViewNode *viewnode = NULL;
78         for (; idx < g_view_node_table_cur_size; idx++) {
79                 if (g_cur_view == g_view_node_table[idx].view) {
80                         viewnode = &(g_view_node_table[idx]);
81                         break;
82                 }
83         }
84         if (viewnode && viewnode->view && viewnode->topview) {
85                 setting_view_change(viewnode->view, viewnode->topview, cb);
86         }
87         return 0;
88 }
89
90 setting_view *setting_view_get_topview(setting_view *view)
91 {
92         SETTING_TRACE_BEGIN;
93         retv_if(NULL == view, NULL);
94         int idx = 0;
95         SettingViewNode *viewnode = NULL;
96         for (; idx < g_view_node_table_cur_size; idx++) {
97                 if (view == g_view_node_table[idx].view) {
98                         viewnode = &(g_view_node_table[idx]);
99                         break;
100                 }
101         }
102         if (viewnode && viewnode->topview) {
103                 return viewnode->topview;
104         }
105         else
106         {
107                 return NULL;
108         }
109 }
110
111 int setting_view_create(setting_view *view, void *cb)
112 {
113         /* error check */
114         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
115
116         int ret = SETTING_RETURN_FAIL;
117
118         if (!view->is_create && view->create) {//error handle:create only when the view doesn't exit
119                 ret = view->create(cb);
120         }
121
122         return ret;
123 }
124
125 int setting_view_destroy(setting_view *view, void *cb)
126 {
127         /* error check */
128         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
129
130         int ret = SETTING_RETURN_FAIL;
131
132         if (view->is_create && view->destroy) {//error handle:destroy only when the view exits
133                 ret = view->destroy(cb);
134         }
135
136         return ret;
137 }
138
139 int setting_view_update(setting_view *view, void *cb)
140 {
141         /* error check */
142         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
143
144         int ret = SETTING_RETURN_FAIL;
145
146         if (view->is_create && view->update) {//error handle:update only when the view exits
147                 ret = view->update(cb);
148         }
149
150         return ret;
151 }
152
153 int setting_view_cleanup(setting_view *view, void *cb)
154 {
155         /* error check */
156         setting_retvm_if(!view || !cb , SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "Invalid arguement");
157
158         int ret = SETTING_RETURN_FAIL;
159
160         if (view->is_create && view->cleanup) {//error handle:cleanup only when the view exits
161                 ret = view->cleanup(cb);
162         }
163
164         return ret;
165 }
166
167 int
168 setting_view_change(setting_view *from_view, setting_view *to_view, void *cb)
169 {
170         /* error check */
171         setting_retvm_if(!from_view || !to_view || !cb ,
172                          SETTING_GENERAL_ERR_NULL_DATA_PARAMETER,
173                          "Invalid arguement");
174
175         int ret = SETTING_RETURN_SUCCESS;
176         if (from_view == setting_view_get_topview(to_view)) {
177                 //from a parent view to his child view, don't need cleanup parent view
178         } else {
179                 //from a child view to his parent view, need cleanup child view
180                 ret = setting_view_cleanup(from_view, cb);
181         }
182                 
183         if (ret == SETTING_RETURN_FAIL) {
184                 return SETTING_RETURN_FAIL;
185         }
186
187         if (to_view->is_create == 1) {
188                 ret = setting_view_update(to_view, cb);
189         } else {
190                 ret = setting_view_create(to_view, cb);
191         }
192         g_cur_view = to_view;   /* compute new value of g_cur_view. */
193         return ret;
194 }