Tizen 2.1 base
[framework/pim/calendar-service.git] / common / cal_record_extended.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <string.h>
23
24 #include "cal_internal.h"
25 #include "cal_typedef.h"
26 #include "cal_view.h"
27
28 #include "cal_record.h"
29
30 static int __cal_record_extended_create( calendar_record_h* out_record );
31 static int __cal_record_extended_destroy( calendar_record_h record, bool delete_child );
32 static int __cal_record_extended_clone( calendar_record_h record, calendar_record_h* out_record );
33 static int __cal_record_extended_get_str( calendar_record_h record, unsigned int property_id, char** out_str );
34 static int __cal_record_extended_get_str_p( calendar_record_h record, unsigned int property_id, char** out_str );
35 static int __cal_record_extended_get_int( calendar_record_h record, unsigned int property_id, int* out_value );
36 static int __cal_record_extended_set_str( calendar_record_h record, unsigned int property_id, const char* value );
37 static int __cal_record_extended_set_int( calendar_record_h record, unsigned int property_id, int value );
38
39 cal_record_plugin_cb_s _cal_record_extended_plugin_cb = {
40         .create = __cal_record_extended_create,
41         .destroy = __cal_record_extended_destroy,
42         .clone = __cal_record_extended_clone,
43         .get_str = __cal_record_extended_get_str,
44         .get_str_p = __cal_record_extended_get_str_p,
45         .get_int = __cal_record_extended_get_int,
46         .get_double = NULL,
47         .get_lli = NULL,
48         .get_caltime = NULL,
49         .set_str = __cal_record_extended_set_str,
50         .set_int = __cal_record_extended_set_int,
51         .set_double = NULL,
52         .set_lli = NULL,
53         .set_caltime = NULL,
54         .add_child_record = NULL,
55         .remove_child_record = NULL,
56         .get_child_record_count = NULL,
57         .get_child_record_at_p = NULL,
58         .clone_child_record_list = NULL
59 };
60
61 static void __cal_record_extended_struct_init(cal_extended_s *record)
62 {
63     memset(record,0,sizeof(cal_extended_s));
64 }
65
66 static int __cal_record_extended_create( calendar_record_h* out_record )
67 {
68     cal_extended_s *temp = NULL;
69     int ret= CALENDAR_ERROR_NONE, type = 0;
70
71     type = CAL_RECORD_TYPE_EXTENDED;
72
73     temp = (cal_extended_s*)calloc(1,sizeof(cal_extended_s));
74     retvm_if(NULL == temp, CALENDAR_ERROR_OUT_OF_MEMORY, "malloc(cal_extended_s) Failed(%d)", CALENDAR_ERROR_OUT_OF_MEMORY);
75
76     __cal_record_extended_struct_init(temp);
77
78     *out_record = (calendar_record_h)temp;
79
80     return ret;
81 }
82
83 static void __cal_record_extended_struct_free(cal_extended_s *record)
84 {
85     CAL_FREE(record->key);
86     CAL_FREE(record->value);
87     CAL_FREE(record);
88
89 }
90
91 static int __cal_record_extended_destroy( calendar_record_h record, bool delete_child )
92 {
93     int ret = CALENDAR_ERROR_NONE;
94
95     cal_extended_s *temp = (cal_extended_s*)(record);
96
97     __cal_record_extended_struct_free(temp);
98
99     return ret;
100 }
101
102 static int __cal_record_extended_clone( calendar_record_h record, calendar_record_h* out_record )
103 {
104     cal_extended_s *out_data = NULL;
105     cal_extended_s *src_data = NULL;
106
107     src_data = (cal_extended_s*)(record);
108
109     out_data = calloc(1, sizeof(cal_extended_s));
110     retvm_if(NULL == out_data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc(cal_extended_s) Failed(%d)", CALENDAR_ERROR_OUT_OF_MEMORY);
111
112     CAL_RECORD_COPY_COMMON(&(out_data->common), &(src_data->common));
113
114     out_data->id = src_data->id;
115     out_data->record_id = src_data->record_id;
116     out_data->record_type = src_data->record_type;
117     out_data->key = SAFE_STRDUP(src_data->key);
118     out_data->value = SAFE_STRDUP(src_data->value);
119
120     *out_record = (calendar_record_h)out_data;
121
122     return CALENDAR_ERROR_NONE;
123 }
124
125 static int __cal_record_extended_get_str( calendar_record_h record, unsigned int property_id, char** out_str )
126 {
127     cal_extended_s *rec = (cal_extended_s*)(record);
128     switch( property_id )
129     {
130     case CAL_PROPERTY_EXTENDED_KEY:
131         *out_str = SAFE_STRDUP(rec->key);
132         break;
133     case CAL_PROPERTY_EXTENDED_VALUE:
134         *out_str = SAFE_STRDUP(rec->value);
135         break;
136     default:
137         ASSERT_NOT_REACHED("invalid parameter (property:%d)",property_id);
138         return CALENDAR_ERROR_INVALID_PARAMETER;
139     }
140
141     return CALENDAR_ERROR_NONE;
142 }
143
144 static int __cal_record_extended_get_str_p( calendar_record_h record, unsigned int property_id, char** out_str )
145 {
146     cal_extended_s *rec = (cal_extended_s*)(record);
147     switch( property_id )
148     {
149     case CAL_PROPERTY_EXTENDED_KEY:
150         *out_str = (rec->key);
151         break;
152     case CAL_PROPERTY_EXTENDED_VALUE:
153         *out_str = (rec->value);
154         break;
155     default:
156         ASSERT_NOT_REACHED("invalid parameter (property:%d)",property_id);
157         return CALENDAR_ERROR_INVALID_PARAMETER;
158     }
159
160     return CALENDAR_ERROR_NONE;
161 }
162
163 static int __cal_record_extended_get_int( calendar_record_h record, unsigned int property_id, int* out_value )
164 {
165     cal_extended_s *rec = (cal_extended_s*)(record);
166     switch( property_id )
167     {
168     case CAL_PROPERTY_EXTENDED_ID:
169         *out_value = (rec->id);
170         break;
171     case CAL_PROPERTY_EXTENDED_RECORD_ID:
172         *out_value = (rec->record_id);
173         break;
174     case CAL_PROPERTY_EXTENDED_RECORD_TYPE:
175         *out_value = (rec->record_type);
176         break;
177     default:
178         ASSERT_NOT_REACHED("invalid parameter (property:%d)",property_id);
179         return CALENDAR_ERROR_INVALID_PARAMETER;
180     }
181
182     return CALENDAR_ERROR_NONE;
183 }
184
185 static int __cal_record_extended_set_str( calendar_record_h record, unsigned int property_id, const char* value )
186 {
187     cal_extended_s *rec = (cal_extended_s*)(record);
188     switch( property_id )
189     {
190     case CAL_PROPERTY_EXTENDED_KEY:
191         CAL_FREE(rec->key);
192         rec->key = SAFE_STRDUP(value);
193         break;
194     case CAL_PROPERTY_EXTENDED_VALUE:
195         CAL_FREE(rec->value);
196         rec->value = SAFE_STRDUP(value);
197         break;
198     default:
199         ASSERT_NOT_REACHED("invalid parameter (property:%d)",property_id);
200         return CALENDAR_ERROR_INVALID_PARAMETER;
201     }
202
203     return CALENDAR_ERROR_NONE;
204 }
205
206 static int __cal_record_extended_set_int( calendar_record_h record, unsigned int property_id, int value )
207 {
208     cal_extended_s *rec = (cal_extended_s*)(record);
209     switch( property_id )
210     {
211     case CAL_PROPERTY_EXTENDED_ID:
212         (rec->id) = value;
213         break;
214     case CAL_PROPERTY_EXTENDED_RECORD_ID:
215         (rec->record_id) = value;
216         break;
217     case CAL_PROPERTY_EXTENDED_RECORD_TYPE:
218         (rec->record_type) = value;
219         break;
220     default:
221         ASSERT_NOT_REACHED("invalid parameter (property:%d)",property_id);
222         return CALENDAR_ERROR_INVALID_PARAMETER;
223     }
224
225     return CALENDAR_ERROR_NONE;
226 }