5639c5ea01849da4c75d68f23c6a875f9d16f8a9
[platform/core/api/maps-service.git] / src / api / maps_place_image.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_image_plugin.h"
19 #include "maps_place_media_plugin.h"
20 #include "maps_place_link_object_plugin.h"
21 #include "maps_util.h"
22 #include "maps_condition.h"
23
24 typedef struct _maps_place_image_s
25 {
26         char *id;
27         char *url;
28         int width;
29         int height;
30         maps_place_media_h media;
31         maps_place_link_object_h user;
32 } maps_place_image_s;
33
34 const gsize _MAPS_PLACE_IMAGE_ID_MAX_LENGTH = MAPS_BASE_ID_MAX_LEN;
35 const gsize _MAPS_PLACE_IMAGE_URL_MAX_LENGTH = MAPS_BASE_URL_MAX_LEN;
36
37 /*----------------------------------------------------------------------------*/
38
39 EXPORT_API int maps_place_image_create(maps_place_image_h *place)
40 {
41         if (!maps_condition_check_maps_feature())
42                 return MAPS_ERROR_NOT_SUPPORTED;
43         if (!place)
44                 return MAPS_ERROR_INVALID_PARAMETER;
45         *place = (maps_place_image_h) g_slice_new0(maps_place_image_s);
46
47         if (*place == NULL) {
48                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
49                 return MAPS_ERROR_OUT_OF_MEMORY;
50         }
51
52         return MAPS_ERROR_NONE;
53 }
54
55 EXPORT_API int maps_place_image_destroy(maps_place_image_h place)
56 {
57         if (!maps_condition_check_maps_feature())
58                 return MAPS_ERROR_NOT_SUPPORTED;
59         if (!place)
60                 return MAPS_ERROR_INVALID_PARAMETER;
61
62         maps_place_image_s *i = (maps_place_image_s *) place;
63
64         if (i->id)
65                 g_free(i->id);
66         if (i->url)
67                 g_free(i->url);
68
69         if (i->media)
70                 maps_place_media_destroy(i->media);
71         if (i->user)
72                 maps_place_link_object_destroy(i->user);
73
74         g_slice_free(maps_place_image_s, place);
75         return MAPS_ERROR_NONE;
76 }
77
78 EXPORT_API int maps_place_image_clone(const maps_place_image_h origin,
79                                       maps_place_image_h *cloned)
80 {
81         if (!maps_condition_check_maps_feature())
82                 return MAPS_ERROR_NOT_SUPPORTED;
83         if (!cloned || !origin)
84                 return MAPS_ERROR_INVALID_PARAMETER;
85
86         int error = MAPS_ERROR_NONE;
87         do {
88                 error = maps_place_image_create(cloned);
89                 if (!(*cloned) || (error != MAPS_ERROR_NONE))
90                         break;
91
92                 maps_place_image_s *i = (maps_place_image_s *) origin;
93
94                 if (i->id) {
95                         error = maps_place_image_set_id(*cloned, i->id);
96                         if (error != MAPS_ERROR_NONE)
97                                 break;
98                 }
99
100                 if (i->url) {
101                         error = maps_place_image_set_url(*cloned, i->url);
102                         if (error != MAPS_ERROR_NONE)
103                                 break;
104                 }
105
106                 error = maps_place_image_set_width(*cloned, i->width);
107                 if (error != MAPS_ERROR_NONE)
108                         break;
109
110                 error = maps_place_image_set_height(*cloned, i->height);
111                 if (error != MAPS_ERROR_NONE)
112                         break;
113
114                 if (i->media) {
115                         error = maps_place_image_set_media(*cloned, i->media);
116                         if (error != MAPS_ERROR_NONE)
117                                 break;
118                 }
119
120                 if (i->user) {
121                         error = maps_place_image_set_user_link(*cloned, i->user);
122                         if (error != MAPS_ERROR_NONE)
123                                 break;
124                 }
125
126                 return MAPS_ERROR_NONE;
127         } while (false);
128
129         maps_place_image_destroy(*cloned);
130         *cloned = NULL;
131         return error;
132 }
133
134 /*----------------------------------------------------------------------------*/
135
136 EXPORT_API int maps_place_image_get_url(const maps_place_image_h place, char **url)
137 {
138         if (!maps_condition_check_maps_feature())
139                 return MAPS_ERROR_NOT_SUPPORTED;
140         if (!place || !url)
141                 return MAPS_ERROR_INVALID_PARAMETER;
142         if (!((maps_place_image_s *) place)->url)
143                 return MAPS_ERROR_NOT_FOUND;
144         return maps_get_string(((maps_place_image_s *) place)->url,
145                 _MAPS_PLACE_IMAGE_URL_MAX_LENGTH, url);
146 }
147
148 EXPORT_API int maps_place_image_get_id(const maps_place_image_h place, char **id)
149 {
150         if (!maps_condition_check_maps_feature())
151                 return MAPS_ERROR_NOT_SUPPORTED;
152         if (!place || !id)
153                 return MAPS_ERROR_INVALID_PARAMETER;
154         if (!((maps_place_image_s *) place)->id)
155                 return MAPS_ERROR_NOT_FOUND;
156         return maps_get_string(((maps_place_image_s *) place)->id,
157                 _MAPS_PLACE_IMAGE_ID_MAX_LENGTH, id);
158 }
159
160 EXPORT_API int maps_place_image_get_width(const maps_place_image_h place, int *width)
161 {
162         if (!maps_condition_check_maps_feature())
163                 return MAPS_ERROR_NOT_SUPPORTED;
164         if (!place || !width)
165                 return MAPS_ERROR_INVALID_PARAMETER;
166         if (!((maps_place_image_s *) place)->width)
167                 return MAPS_ERROR_NOT_FOUND;
168         *width = ((maps_place_image_s *) place)->width;
169         return MAPS_ERROR_NONE;
170 }
171
172 EXPORT_API int maps_place_image_get_height(const maps_place_image_h place, int *height)
173 {
174         if (!maps_condition_check_maps_feature())
175                 return MAPS_ERROR_NOT_SUPPORTED;
176         if (!place || !height)
177                 return MAPS_ERROR_INVALID_PARAMETER;
178         if (!((maps_place_image_s *) place)->height)
179                 return MAPS_ERROR_NOT_FOUND;
180         *height = ((maps_place_image_s *) place)->height;
181         return MAPS_ERROR_NONE;
182 }
183
184 EXPORT_API int maps_place_image_get_user_link(const maps_place_image_h place,
185                                               maps_place_link_object_h *user)
186 {
187         if (!maps_condition_check_maps_feature())
188                 return MAPS_ERROR_NOT_SUPPORTED;
189         if (!place || !user)
190                 return MAPS_ERROR_INVALID_PARAMETER;
191         if (!((maps_place_image_s *) place)->user)
192                 return MAPS_ERROR_NOT_FOUND;
193         return maps_place_link_object_clone(((maps_place_image_s *) place)->user, user);
194 }
195
196 EXPORT_API int maps_place_image_get_media(const maps_place_image_h place,
197                                           maps_place_media_h *media)
198 {
199         if (!maps_condition_check_maps_feature())
200                 return MAPS_ERROR_NOT_SUPPORTED;
201         if (!place || !media)
202                 return MAPS_ERROR_INVALID_PARAMETER;
203         if (!((maps_place_image_s *) place)->media)
204                 return MAPS_ERROR_NOT_FOUND;
205         return maps_place_media_clone(((maps_place_image_s *) place)->media, media);
206 }
207
208 /*----------------------------------------------------------------------------*/
209
210 EXPORT_API int maps_place_image_set_id(maps_place_image_h place, const char *id)
211 {
212         if (!maps_condition_check_maps_feature())
213                 return MAPS_ERROR_NOT_SUPPORTED;
214         if (!place || !id)
215                 return MAPS_ERROR_INVALID_PARAMETER;
216         return maps_set_string(id, _MAPS_PLACE_IMAGE_ID_MAX_LENGTH,
217                 &((maps_place_image_s *) place)->id);
218 }
219
220 EXPORT_API int maps_place_image_set_url(maps_place_image_h place,
221                                         const char *url)
222 {
223         if (!maps_condition_check_maps_feature())
224                 return MAPS_ERROR_NOT_SUPPORTED;
225         if (!place || !url)
226                 return MAPS_ERROR_INVALID_PARAMETER;
227         return maps_set_string(url, _MAPS_PLACE_IMAGE_URL_MAX_LENGTH,
228                 &((maps_place_image_s *) place)->url);
229 }
230
231 EXPORT_API int maps_place_image_set_width(maps_place_image_h place,
232                                           const int width)
233 {
234         if (!maps_condition_check_maps_feature())
235                 return MAPS_ERROR_NOT_SUPPORTED;
236         if (!place || width < 0)
237                 return MAPS_ERROR_INVALID_PARAMETER;
238         ((maps_place_image_s *) place)->width = width;
239         return MAPS_ERROR_NONE;
240 }
241
242 EXPORT_API int maps_place_image_set_height(maps_place_image_h place,
243                                            const int height)
244 {
245         if (!maps_condition_check_maps_feature())
246                 return MAPS_ERROR_NOT_SUPPORTED;
247         if (!place || height < 0)
248                 return MAPS_ERROR_INVALID_PARAMETER;
249         ((maps_place_image_s *) place)->height = height;
250         return MAPS_ERROR_NONE;
251 }
252
253 EXPORT_API int maps_place_image_set_user_link(maps_place_image_h place,
254                                               const maps_place_link_object_h
255                                               user)
256 {
257         if (!maps_condition_check_maps_feature())
258                 return MAPS_ERROR_NOT_SUPPORTED;
259         if (!place || !user)
260                 return MAPS_ERROR_INVALID_PARAMETER;
261         maps_place_image_s *i = (maps_place_image_s *) place;
262         if (i->user)
263                 maps_place_link_object_destroy(i->user);
264         return maps_place_link_object_clone(user, &i->user);
265 }
266
267 EXPORT_API int maps_place_image_set_media(maps_place_image_h place,
268                                           const maps_place_media_h media)
269 {
270         if (!maps_condition_check_maps_feature())
271                 return MAPS_ERROR_NOT_SUPPORTED;
272         if (!place || !media)
273                 return MAPS_ERROR_INVALID_PARAMETER;
274         maps_place_image_s *i = (maps_place_image_s *) place;
275         if (i->media)
276                 maps_place_media_destroy(i->media);
277         return maps_place_media_clone(media, &i->media);
278 }