tizen 2.4 release
[apps/home/attach-panel.git] / src / attach_bundle.c
1 /*
2  * Copyright (c) 2015 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 <app_control.h>
18 #include <bundle.h>
19 #include <Elementary.h>
20 #include <ui-gadget.h>
21 #include <tizen.h>
22
23 #include "attach_panel.h"
24 #include "attach_panel_internal.h"
25 #include "log.h"
26
27
28
29 static void __foreach_bundle_to_add_cb(const char *key, const int type, bundle_keyval_t *kv, void *data)
30 {
31         app_control_h app_control = data;
32         char *basic_val = NULL;
33         void **arr = NULL;
34
35         size_t size = 0;
36         size_t *array_element_size = NULL;
37         unsigned int array_length = 0;
38         int ret = BUNDLE_ERROR_NONE;
39
40         ret_if(!key);
41         ret_if(!app_control);
42
43         _D("key: %s", key);
44
45         switch(type) {
46         case BUNDLE_TYPE_STR:
47                 ret = bundle_keyval_get_basic_val(kv, (void *) &basic_val, &size);
48                 ret_if(ret != BUNDLE_ERROR_NONE);
49                 ret_if(!basic_val);
50
51                 ret = app_control_add_extra_data(app_control, key, basic_val);
52                 ret_if(ret != APP_CONTROL_ERROR_NONE);
53                 break;
54
55         case BUNDLE_TYPE_STR_ARRAY:
56                 ret = bundle_keyval_get_array_val(kv, &arr, &array_length, &array_element_size);
57                 ret_if(ret != BUNDLE_ERROR_NONE);
58                 ret_if(!arr);
59
60                 ret = app_control_add_extra_data_array(app_control, key, (const char **) arr, array_length);
61                 ret_if(ret != APP_CONTROL_ERROR_NONE);
62                 break;
63
64         case BUNDLE_TYPE_BYTE:
65                 ret = bundle_keyval_get_basic_val(kv, (void *) &basic_val, &size);
66                 ret_if(ret != BUNDLE_ERROR_NONE);
67                 ret_if(!basic_val);
68
69                 ret = app_control_add_extra_data(app_control, key, basic_val);
70                 ret_if(ret != APP_CONTROL_ERROR_NONE);
71                 break;
72
73         case BUNDLE_TYPE_BYTE_ARRAY:
74                 ret = bundle_keyval_get_array_val(kv, &arr, &array_length, &array_element_size);
75                 ret_if(ret != BUNDLE_ERROR_NONE);
76                 ret_if(!arr);
77
78                 ret = app_control_add_extra_data_array(app_control, key, (const char **) arr, array_length);
79                 ret_if(ret != APP_CONTROL_ERROR_NONE);
80                 break;
81
82         default:
83                 _E("There is wrong type");
84                 break;
85         }
86 }
87
88
89
90 int _bundle_add_to_app_control(bundle *b, app_control_h app_control)
91 {
92         retv_if(!b, ATTACH_PANEL_ERROR_INVALID_PARAMETER);
93         retv_if(!app_control, ATTACH_PANEL_ERROR_INVALID_PARAMETER);
94
95         bundle_foreach(b, (void *) __foreach_bundle_to_add_cb, app_control);
96         retv_if(get_last_result() != BUNDLE_ERROR_NONE, ATTACH_PANEL_ERROR_OUT_OF_MEMORY);
97
98         return ATTACH_PANEL_ERROR_NONE;
99 }
100
101
102
103 static void __foreach_bundle_to_change_cb(const char *key, const int type, bundle_keyval_t *kv, void *data)
104 {
105         bundle *origin = data;
106         char *basic_val = NULL;
107         void **arr = NULL;
108
109         size_t size;
110         size_t *array_element_size = NULL;
111         unsigned int array_length = 0;
112         int ret = BUNDLE_ERROR_NONE;
113
114         ret_if(!key);
115         ret_if(!origin);
116
117         switch(type) {
118         case BUNDLE_TYPE_STR:
119                 ret = bundle_keyval_get_basic_val(kv, (void *) &basic_val, &size);
120                 ret_if(ret != BUNDLE_ERROR_NONE);
121                 ret_if(!basic_val);
122
123                 ret = bundle_del(origin, key);
124                 ret_if(ret != BUNDLE_ERROR_NONE && ret != BUNDLE_ERROR_KEY_NOT_AVAILABLE);
125
126                 ret = bundle_add_str(origin, key, basic_val);
127                 ret_if(ret != BUNDLE_ERROR_NONE);
128                 break;
129
130         case BUNDLE_TYPE_STR_ARRAY:
131                 ret = bundle_keyval_get_array_val(kv, &arr, &array_length, &array_element_size);
132                 ret_if(ret != BUNDLE_ERROR_NONE);
133                 ret_if(!arr);
134
135                 ret = bundle_del(origin, key);
136                 ret_if(ret != BUNDLE_ERROR_NONE && ret != BUNDLE_ERROR_KEY_NOT_AVAILABLE);
137
138                 ret = bundle_add_str_array(origin, key, (const char **) arr, array_length);
139                 ret_if(ret != BUNDLE_ERROR_NONE);
140                 break;
141
142         case BUNDLE_TYPE_BYTE:
143                 ret = bundle_keyval_get_basic_val(kv, (void *) &basic_val, &size);
144                 ret_if(ret != BUNDLE_ERROR_NONE);
145                 ret_if(!basic_val);
146
147                 ret = bundle_del(origin, key);
148                 ret_if(ret != BUNDLE_ERROR_NONE && ret != BUNDLE_ERROR_KEY_NOT_AVAILABLE);
149
150                 ret = bundle_add_byte(origin, key, basic_val, size);
151                 ret_if(ret != BUNDLE_ERROR_NONE);
152                 break;
153
154         case BUNDLE_TYPE_BYTE_ARRAY:
155                 ret = bundle_keyval_get_array_val(kv, &arr, &array_length, &array_element_size);
156                 ret_if(ret != BUNDLE_ERROR_NONE);
157                 ret_if(!arr);
158
159                 ret = bundle_del(origin, key);
160                 ret_if(ret != BUNDLE_ERROR_NONE && ret != BUNDLE_ERROR_KEY_NOT_AVAILABLE);
161
162                 ret = bundle_add_str_array(origin, key, (const char **) arr, array_length);
163                 ret_if(ret != BUNDLE_ERROR_NONE);
164                 break;
165
166         default:
167                 _E("There is wrong type");
168                 break;
169         }
170 }
171
172
173
174 int _bundle_add_to_bundle(bundle *origin, bundle *replace)
175 {
176         retv_if(!origin, ATTACH_PANEL_ERROR_INVALID_PARAMETER);
177         retv_if(!replace, ATTACH_PANEL_ERROR_INVALID_PARAMETER);
178
179         bundle_foreach(replace, (void *) __foreach_bundle_to_change_cb, origin);
180         retv_if(get_last_result() != BUNDLE_ERROR_NONE, ATTACH_PANEL_ERROR_OUT_OF_MEMORY);
181
182         return ATTACH_PANEL_ERROR_NONE;
183 }