add dh-autoreconf
[ppastats-pkg-ubuntu.git] / src / lp_json.c
1 /*
2  * Copyright (C) 2011-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #define _XOPEN_SOURCE_EXTENDED
20 #define _XOPEN_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include <lp_json.h>
27 #include <lp_ws.h>
28 #include <ptime.h>
29
30 static time_t json_to_time(json_object *json)
31 {
32         const char *str;
33         struct tm tm;
34         char *ret;
35
36         str = json_object_get_string(json);
37         if (!str)
38                 return -1;
39
40         memset(&tm, 0, sizeof(struct tm));
41         tm.tm_isdst = -1;
42         ret = strptime(str, "%FT%T", &tm);
43
44         if (ret)
45                 return mktime(&tm);
46         else
47                 return -1;
48 }
49
50 json_object *time_to_json(time_t t)
51 {
52         char *str;
53         json_object *j;
54
55         str = time_to_ISO8601_time(&t);
56
57         if (str) {
58                 j = json_object_new_string(str);
59                 free(str);
60                 return j;
61         } else {
62                 return NULL;
63         }
64 }
65
66 static struct bpph *json_to_bpph(json_object *o)
67 {
68         const char *binary_package_name;
69         const char *binary_package_version;
70         const char *distro_arch_series_link;
71         const char *self_link;
72         int arch_specific;
73         struct bpph *bpph;
74         const char *status;
75         time_t date_created;
76         json_object *j;
77
78         json_object_object_get_ex(o, "binary_package_name", &j);
79         binary_package_name = json_object_get_string(j);
80
81         json_object_object_get_ex(o, "binary_package_version", &j);
82         binary_package_version = json_object_get_string(j);
83
84         json_object_object_get_ex(o, "distro_arch_series_link", &j);
85         distro_arch_series_link = json_object_get_string(j);
86
87         json_object_object_get_ex(o, "self_link", &j);
88         self_link = json_object_get_string(j);
89
90         json_object_object_get_ex(o, "architecture_specific", &j);
91         arch_specific = json_object_get_boolean(j);
92
93         json_object_object_get_ex(o, "date_created", &j);
94         date_created = json_to_time(j);
95
96         json_object_object_get_ex(o, "status", &j);
97         status = json_object_get_string(j);
98
99         bpph =  bpph_new(binary_package_name,
100                          binary_package_version,
101                          distro_arch_series_link,
102                          self_link,
103                          status,
104                          arch_specific,
105                          date_created);
106
107         return bpph;
108 }
109
110 static json_object *bpph_to_json(struct bpph *bpph)
111 {
112         json_object *json, *time;
113
114         json = json_object_new_object();
115
116         json_object_object_add
117                 (json,
118                  "binary_package_name",
119                  json_object_new_string(bpph->binary_package_name));
120
121         json_object_object_add
122                 (json,
123                  "binary_package_version",
124                  json_object_new_string(bpph->binary_package_version));
125
126         json_object_object_add
127                 (json,
128                  "distro_arch_series_link",
129                  json_object_new_string(bpph->distro_arch_series_link));
130
131         json_object_object_add
132                 (json, "self_link", json_object_new_string(bpph->self_link));
133
134         json_object_object_add
135                 (json,
136                  "architecture_specific",
137                  json_object_new_boolean(bpph->architecture_specific));
138
139         json_object_object_add
140                 (json, "status", json_object_new_string(bpph->status));
141
142         time = time_to_json(bpph->date_created);
143         json_object_object_add
144                 (json, "date_created", time);
145
146         return json;
147 }
148
149 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
150 {
151         const char *display_name;
152         const char *title;
153         const char *architecture_tag;
154         json_bool is_nominated_arch_indep;
155         const char *distroseries_link;
156         json_object *j;
157
158         json_object_object_get_ex(o, "display_name", &j);
159         display_name = json_object_get_string(j);
160
161         json_object_object_get_ex(o, "title", &j);
162         title = json_object_get_string(j);
163
164         json_object_object_get_ex(o, "architecture_tag", &j);
165         architecture_tag = json_object_get_string(j);
166
167         json_object_object_get_ex(o, "distroseries_link", &j);
168         distroseries_link = json_object_get_string(j);
169
170         json_object_object_get_ex(o, "is_nominated_arch_indep", &j);
171         is_nominated_arch_indep = json_object_get_boolean
172                 (j);
173
174         return distro_arch_series_new(display_name,
175                                       title,
176                                       architecture_tag,
177                                       is_nominated_arch_indep,
178                                       distroseries_link);
179 }
180
181 struct distro_series *json_object_to_distro_series(json_object *o)
182 {
183         const char *displayname;
184         const char *title;
185         const char *name;
186         const char *version;
187         json_object *j;
188
189         json_object_object_get_ex(o, "displayname", &j);
190         displayname = json_object_get_string(j);
191
192         json_object_object_get_ex(o, "title", &j);
193         title = json_object_get_string(j);
194
195         json_object_object_get_ex(o, "version", &j);
196         version = json_object_get_string(j);
197
198         json_object_object_get_ex(o, "name", &j);
199         name = json_object_get_string(j);
200
201         return distro_series_new(name,
202                                  version,
203                                  title,
204                                  displayname);
205 }
206
207 struct bpph **json_object_to_bpph_list(json_object *o)
208 {
209         json_object *o_entries;
210         int i, n, i2;
211         struct bpph **entries, *h;
212         const struct distro_arch_series *distro;
213
214         json_object_object_get_ex(o, "entries", &o_entries);
215
216         if (!o_entries)
217                 return NULL;
218
219         n = json_object_array_length(o_entries);
220
221         entries = malloc
222                 (sizeof(struct bpph *)*(n+1));
223
224         for (i = 0, i2 = 0; i < n; i++) {
225                 h = json_to_bpph(json_object_array_get_idx(o_entries,
226                                                            i));
227
228                 if (!h->architecture_specific) {
229                         distro = get_distro_arch_series
230                                 (h->distro_arch_series_link);
231
232                         if (!distro || !distro->is_nominated_arch_indep) {
233                                 bpph_free(h);
234                                 continue ;
235                         }
236                 }
237
238                 entries[i2] = h;
239                 i2++;
240         }
241         entries[i2] = NULL;
242
243         return entries;
244 }
245
246 json_object *bpph_list_to_json(struct bpph **list)
247 {
248         json_object *result, *entries;
249         struct bpph **cur;
250
251         result = json_object_new_object();
252
253         entries = json_object_new_array();
254         json_object_object_add(result, "entries", entries);
255
256         if (list)
257                 for (cur = list; *cur; cur++)
258                         json_object_array_add(entries, bpph_to_json(*cur));
259
260         return result;
261 }
262
263 struct daily_download_total *
264 json_object_to_daily_download_total(const char *d, json_object *o_c)
265 {
266         struct daily_download_total *result;
267
268         result = malloc(sizeof(struct daily_download_total));
269         result->count = json_object_get_int(o_c);
270
271         memset(&result->date, 0, sizeof(struct tm));
272         strptime(d, "%FT%T%z", &result->date);
273
274         return result;
275 }
276
277 static int json_object_get_fields_count(json_object *o)
278 {
279         int n = 0;
280         struct lh_entry *entry;
281
282         entry = json_object_get_object(o)->head;
283         while (entry) {
284                 entry = entry->next;
285                 n++;
286         }
287
288         return n;
289 }
290
291 struct daily_download_total * *
292 json_object_to_daily_download_totals(json_object *o)
293 {
294         int n, i;
295         struct daily_download_total **result;
296
297         n = json_object_get_fields_count(o);
298
299         result = malloc
300                 (sizeof(struct daily_download_total *)*(n+1));
301
302         i = 0;
303         json_object_object_foreach(o, key, val) {
304                 result[i] = json_object_to_daily_download_total(key, val);
305                 i++;
306         }
307
308         result[n] = NULL;
309
310         return result;
311 }
312
313 struct json_object *date_to_json(struct tm *tm)
314 {
315         json_object *json;
316
317         json = json_object_new_array();
318         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
319         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
320         json_object_array_add(json, json_object_new_int(tm->tm_mday));
321
322         return json;
323 }
324
325 json_object *ddts_to_json(struct daily_download_total **ddts)
326 {
327         json_object *json_ddt, *json_ddts;
328         struct daily_download_total *ddt;
329
330         json_ddts = json_object_new_array();
331
332         while (ddts && *ddts) {
333                 ddt = *ddts;
334
335                 json_ddt = json_object_new_object();
336                 json_object_object_add(json_ddt,
337                                        "value",
338                                        json_object_new_int(ddt->count));
339                 json_object_object_add(json_ddt,
340                                        "time",
341                                        date_to_json(&ddt->date));
342
343                 json_object_array_add(json_ddts, json_ddt);
344
345                 ddts++;
346         }
347
348         return json_ddts;
349 }