3f52e7466dd02559fc074956115f3dc3ac82a906
[platform/core/api/maps-service.git] / src / api / maps_place_contact.cpp
1 /* Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <glib.h>
17 #include "maps_error.h"
18 #include "maps_place_contact_plugin.h"
19 #include "maps_util.h"
20 #include "maps_condition.h"
21
22 typedef struct _maps_place_contact_s
23 {
24         char *label;
25         char *type;
26         char * value;
27 } maps_place_contact_s;
28
29 const gsize _MAPS_PLACE_CONTACT_LABEL_MAX_LENGTH = MAPS_BASE_NAME_MAX_LEN;
30 const gsize _MAPS_PLACE_CONTACT_TYPE_MAX_LENGTH = MAPS_BASE_TYPE_MAX_LEN;
31 const gsize _MAPS_PLACE_CONTACT_VALUE_MAX_LENGTH = MAPS_BASE_DESC_MAX_LEN;
32
33 /*----------------------------------------------------------------------------*/
34
35 EXPORT_API int maps_place_contact_create(maps_place_contact_h *place)
36 {
37         if (!maps_condition_check_maps_feature())
38                 return MAPS_ERROR_NOT_SUPPORTED;
39         if (!place)
40                 return MAPS_ERROR_INVALID_PARAMETER;
41
42         *place = (maps_place_contact_h) g_slice_new0(maps_place_contact_s);
43         if (*place == NULL) {
44                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
45                 return MAPS_ERROR_OUT_OF_MEMORY;
46         }
47
48         return MAPS_ERROR_NONE;
49 }
50
51 EXPORT_API int maps_place_contact_destroy(maps_place_contact_h place)
52 {
53         if (!maps_condition_check_maps_feature())
54                 return MAPS_ERROR_NOT_SUPPORTED;
55         if (!place)
56                 return MAPS_ERROR_INVALID_PARAMETER;
57
58         maps_place_contact_s *c = (maps_place_contact_s *) place;
59
60         if (c->label)
61                 g_free(c->label);
62         if (c->type)
63                 g_free(c->type);
64         if (c->value)
65                 g_free(c->value);
66
67         g_slice_free(maps_place_contact_s, place);
68         return MAPS_ERROR_NONE;
69 }
70
71 EXPORT_API int maps_place_contact_clone(const maps_place_contact_h origin,
72                                         maps_place_contact_h *cloned)
73 {
74         if (!maps_condition_check_maps_feature())
75                 return MAPS_ERROR_NOT_SUPPORTED;
76         if (!cloned || !origin)
77                 return MAPS_ERROR_INVALID_PARAMETER;
78
79         int error = MAPS_ERROR_NONE;
80         do {
81                 error = maps_place_contact_create(cloned);
82                 if (!(*cloned) || (error != MAPS_ERROR_NONE))
83                         break;
84
85                 maps_place_contact_s *c = (maps_place_contact_s *) origin;
86
87                 if (c->label) {
88                         error = maps_place_contact_set_label(*cloned, c->label);
89                         if (error != MAPS_ERROR_NONE)
90                                 break;
91                 }
92
93                 if (c->type) {
94                         error = maps_place_contact_set_type(*cloned, c->type);
95                         if (error != MAPS_ERROR_NONE)
96                                 break;
97                 }
98
99                 if (c->value) {
100                         error = maps_place_contact_set_value(*cloned, c->value);
101                         if (error != MAPS_ERROR_NONE)
102                                 break;
103                 }
104
105                 return MAPS_ERROR_NONE;
106         } while (false);
107
108         maps_place_contact_destroy(*cloned);
109         *cloned = NULL;
110         return error;
111 }
112
113 /*----------------------------------------------------------------------------*/
114
115 EXPORT_API int maps_place_contact_get_label(const maps_place_contact_h place, char **label)
116 {
117         if (!maps_condition_check_maps_feature())
118                 return MAPS_ERROR_NOT_SUPPORTED;
119         if (!place || !label)
120                 return MAPS_ERROR_INVALID_PARAMETER;
121         if (!((maps_place_contact_s *) place)->label)
122                 return MAPS_ERROR_NOT_FOUND;
123         return maps_get_string(((maps_place_contact_s *) place)->label,
124                 _MAPS_PLACE_CONTACT_LABEL_MAX_LENGTH, label);
125 }
126
127 EXPORT_API int maps_place_contact_get_type(const maps_place_contact_h place, char **type)
128 {
129         if (!maps_condition_check_maps_feature())
130                 return MAPS_ERROR_NOT_SUPPORTED;
131         if (!place || !type)
132                 return MAPS_ERROR_INVALID_PARAMETER;
133         if (!((maps_place_contact_s *) place)->type)
134                 return MAPS_ERROR_NOT_FOUND;
135         return maps_get_string(((maps_place_contact_s *) place)->type,
136                 _MAPS_PLACE_CONTACT_TYPE_MAX_LENGTH, type);
137 }
138
139 EXPORT_API int maps_place_contact_get_value(const maps_place_contact_h place, char **value)
140 {
141         if (!maps_condition_check_maps_feature())
142                 return MAPS_ERROR_NOT_SUPPORTED;
143         if (!place || !value)
144                 return MAPS_ERROR_INVALID_PARAMETER;
145         if (!((maps_place_contact_s *) place)->value)
146                 return MAPS_ERROR_NOT_FOUND;
147         return maps_get_string(((maps_place_contact_s *) place)->value,
148                 _MAPS_PLACE_CONTACT_VALUE_MAX_LENGTH, value);
149 }
150
151 /*----------------------------------------------------------------------------*/
152
153 EXPORT_API int maps_place_contact_set_label(maps_place_contact_h place, const char *label)
154 {
155         if (!maps_condition_check_maps_feature())
156                 return MAPS_ERROR_NOT_SUPPORTED;
157         if (!place || !label)
158                 return MAPS_ERROR_INVALID_PARAMETER;
159         return maps_set_string(label, _MAPS_PLACE_CONTACT_LABEL_MAX_LENGTH,
160                 &((maps_place_contact_s *) place)->label);
161 }
162
163 EXPORT_API int maps_place_contact_set_type(maps_place_contact_h place, const char *type)
164 {
165         if (!maps_condition_check_maps_feature())
166                 return MAPS_ERROR_NOT_SUPPORTED;
167         if (!place || !type)
168                 return MAPS_ERROR_INVALID_PARAMETER;
169         return maps_set_string(type, _MAPS_PLACE_CONTACT_TYPE_MAX_LENGTH,
170                 &((maps_place_contact_s *) place)->type);
171 }
172
173 EXPORT_API int maps_place_contact_set_value(maps_place_contact_h place, const char *value)
174 {
175         if (!maps_condition_check_maps_feature())
176                 return MAPS_ERROR_NOT_SUPPORTED;
177         if (!place || !value)
178                 return MAPS_ERROR_INVALID_PARAMETER;
179         return maps_set_string(value, _MAPS_PLACE_CONTACT_VALUE_MAX_LENGTH,
180                 &((maps_place_contact_s *) place)->value);
181 }