68717291a15ec268a7bd6974caa507839e46f9ef
[platform/core/api/maps-service.git] / src / api / maps_place_category.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_extra_types.h"
19 #include "maps_util.h"
20 #include "maps_place_category.h"
21 #include "maps_condition.h"
22
23 typedef struct _maps_place_category_s
24 {
25         char *name;
26         char *id;
27         char *url;
28 } maps_place_category_s;
29
30 const gsize _MAPS_PLACE_CATEGORY_ID_MAX_LENGTH = MAPS_BASE_ID_MAX_LEN;
31 const gsize _MAPS_PLACE_CATEGORY_NAME_MAX_LENGTH = MAPS_BASE_NAME_MAX_LEN;
32 const gsize _MAPS_PLACE_CATEGORY_URL_MAX_LENGTH = MAPS_BASE_URL_MAX_LEN;
33
34 /*----------------------------------------------------------------------------*/
35
36 EXPORT_API int maps_place_category_create(maps_place_category_h *place)
37 {
38         if (!maps_condition_check_maps_feature())
39                 return MAPS_ERROR_NOT_SUPPORTED;
40         if (!place)
41                 return MAPS_ERROR_INVALID_PARAMETER;
42         *place = g_slice_new0(maps_place_category_s);
43
44         if (*place == NULL) {
45                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
46                 return MAPS_ERROR_OUT_OF_MEMORY;
47         }
48
49         return MAPS_ERROR_NONE;
50 }
51
52 EXPORT_API int maps_place_category_destroy(maps_place_category_h place)
53 {
54         if (!maps_condition_check_maps_feature())
55                 return MAPS_ERROR_NOT_SUPPORTED;
56         if (!place)
57                 return MAPS_ERROR_INVALID_PARAMETER;
58         int error = MAPS_ERROR_NONE;
59
60         maps_place_category_s *c = (maps_place_category_s *) place;
61
62         if (c->id)
63                 g_free(c->id);
64         if (c->name)
65                 g_free(c->name);
66         if (c->url)
67                 g_free(c->url);
68
69         g_slice_free(maps_place_category_s, place);
70         return error;
71 }
72
73 EXPORT_API int maps_place_category_clone(const maps_place_category_h origin,
74                                          maps_place_category_h *cloned)
75 {
76         if (!maps_condition_check_maps_feature())
77                 return MAPS_ERROR_NOT_SUPPORTED;
78         if (!cloned || !origin)
79                 return MAPS_ERROR_INVALID_PARAMETER;
80
81         int error = MAPS_ERROR_NONE;
82
83         do {
84                 error = maps_place_category_create(cloned);
85                 if (error != MAPS_ERROR_NONE)
86                         return error;
87
88                 maps_place_category_s *c = (maps_place_category_s *) origin;
89
90                 if (c->id) {
91                         error = maps_place_category_set_id(*cloned, c->id);
92                         if (error != MAPS_ERROR_NONE)
93                                 break;
94                 }
95
96                 if (c->name) {
97                         error = maps_place_category_set_name(*cloned, c->name);
98                         if (error != MAPS_ERROR_NONE)
99                                 break;
100                 }
101
102                 if (c->url) {
103                         error = maps_place_category_set_url(*cloned, c->url);
104                         if (error != MAPS_ERROR_NONE)
105                                 break;
106                 }
107
108                 return MAPS_ERROR_NONE;
109         } while (false);
110
111         maps_place_category_destroy(*cloned);
112         *cloned = NULL;
113         return error;
114 }
115
116 /*----------------------------------------------------------------------------*/
117
118 EXPORT_API int maps_place_category_get_name(const maps_place_category_h place, char **name)
119 {
120         if (!maps_condition_check_maps_feature())
121                 return MAPS_ERROR_NOT_SUPPORTED;
122         if (!place || !name)
123                 return MAPS_ERROR_INVALID_PARAMETER;
124         if (!((maps_place_category_s *) place)->name)
125                 return MAPS_ERROR_NOT_FOUND;
126         return maps_get_string(((maps_place_category_s *) place)->name,
127                 _MAPS_PLACE_CATEGORY_NAME_MAX_LENGTH, name);
128 }
129
130 EXPORT_API int maps_place_category_get_id(const maps_place_category_h place, char **id)
131 {
132         if (!maps_condition_check_maps_feature())
133                 return MAPS_ERROR_NOT_SUPPORTED;
134         if (!place || !id)
135                 return MAPS_ERROR_INVALID_PARAMETER;
136         if (!((maps_place_category_s *) place)->id)
137                 return MAPS_ERROR_NOT_FOUND;
138         return maps_get_string(((maps_place_category_s *) place)->id,
139                 _MAPS_PLACE_CATEGORY_ID_MAX_LENGTH, id);
140 }
141
142 EXPORT_API int maps_place_category_get_url(const maps_place_category_h place, char **url)
143 {
144         if (!maps_condition_check_maps_feature())
145                 return MAPS_ERROR_NOT_SUPPORTED;
146         if (!place || !url)
147                 return MAPS_ERROR_INVALID_PARAMETER;
148         if (!((maps_place_category_s *) place)->url)
149                 return MAPS_ERROR_NOT_FOUND;
150         return maps_get_string(((maps_place_category_s *) place)->url,
151                 _MAPS_PLACE_CATEGORY_URL_MAX_LENGTH, url);
152 }
153
154 /*----------------------------------------------------------------------------*/
155
156 EXPORT_API int maps_place_category_set_id(maps_place_category_h place, const char *id)
157 {
158         if (!maps_condition_check_maps_feature())
159                 return MAPS_ERROR_NOT_SUPPORTED;
160         if (!place || !id)
161                 return MAPS_ERROR_INVALID_PARAMETER;
162         return maps_set_string(id, _MAPS_PLACE_CATEGORY_ID_MAX_LENGTH,
163                 &((maps_place_category_s *) place)->id);
164 }
165
166 EXPORT_API int maps_place_category_set_name(maps_place_category_h place, const char *name)
167 {
168         if (!maps_condition_check_maps_feature())
169                 return MAPS_ERROR_NOT_SUPPORTED;
170         if (!place || !name)
171                 return MAPS_ERROR_INVALID_PARAMETER;
172         return maps_set_string(name, _MAPS_PLACE_CATEGORY_NAME_MAX_LENGTH,
173                 &((maps_place_category_s *) place)->name);
174 }
175
176 EXPORT_API int maps_place_category_set_url(maps_place_category_h place, const char *url)
177 {
178         if (!maps_condition_check_maps_feature())
179                 return MAPS_ERROR_NOT_SUPPORTED;
180         if (!place || !url)
181                 return MAPS_ERROR_INVALID_PARAMETER;
182         return maps_set_string(url, _MAPS_PLACE_CATEGORY_URL_MAX_LENGTH,
183                 &((maps_place_category_s *) place)->url);
184 }