removed useless empty line
[ppastats.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
77         binary_package_name = json_object_get_string
78                 (json_object_object_get(o, "binary_package_name"));
79
80         binary_package_version = json_object_get_string
81                 (json_object_object_get(o, "binary_package_version"));
82
83         distro_arch_series_link = json_object_get_string
84                 (json_object_object_get(o, "distro_arch_series_link"));
85
86         self_link = json_object_get_string
87                 (json_object_object_get(o, "self_link"));
88
89         arch_specific = json_object_get_boolean
90                 (json_object_object_get(o, "architecture_specific"));
91
92         date_created = json_to_time(json_object_object_get(o, "date_created"));
93
94         status = json_object_get_string(json_object_object_get(o, "status"));
95
96         bpph =  bpph_new(binary_package_name,
97                          binary_package_version,
98                          distro_arch_series_link,
99                          self_link,
100                          status,
101                          arch_specific,
102                          date_created);
103
104         return bpph;
105 }
106
107 static json_object *bpph_to_json(struct bpph *bpph)
108 {
109         json_object *json, *time;
110
111         json = json_object_new_object();
112
113         json_object_object_add
114                 (json,
115                  "binary_package_name",
116                  json_object_new_string(bpph->binary_package_name));
117
118         json_object_object_add
119                 (json,
120                  "binary_package_version",
121                  json_object_new_string(bpph->binary_package_version));
122
123         json_object_object_add
124                 (json,
125                  "distro_arch_series_link",
126                  json_object_new_string(bpph->distro_arch_series_link));
127
128         json_object_object_add
129                 (json, "self_link", json_object_new_string(bpph->self_link));
130
131         json_object_object_add
132                 (json,
133                  "architecture_specific",
134                  json_object_new_boolean(bpph->architecture_specific));
135
136         json_object_object_add
137                 (json, "status", json_object_new_string(bpph->status));
138
139         time = time_to_json(bpph->date_created);
140         json_object_object_add
141                 (json, "date_created", time);
142
143         return json;
144 }
145
146 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
147 {
148         const char *display_name;
149         const char *title;
150         const char *architecture_tag;
151         json_bool is_nominated_arch_indep;
152         const char *distroseries_link;
153
154         display_name = json_object_get_string
155                 (json_object_object_get(o, "display_name"));
156
157         title = json_object_get_string
158                 (json_object_object_get(o, "title"));
159
160         architecture_tag = json_object_get_string
161                 (json_object_object_get(o, "architecture_tag"));
162
163         distroseries_link = json_object_get_string
164                 (json_object_object_get(o, "distroseries_link"));
165
166         is_nominated_arch_indep = json_object_get_boolean
167                 (json_object_object_get(o, "is_nominated_arch_indep"));
168
169         return distro_arch_series_new(display_name,
170                                       title,
171                                       architecture_tag,
172                                       is_nominated_arch_indep,
173                                       distroseries_link);
174 }
175
176 struct distro_series *json_object_to_distro_series(json_object *o)
177 {
178         const char *displayname;
179         const char *title;
180         const char *name;
181         const char *version;
182
183         displayname = json_object_get_string
184                 (json_object_object_get(o, "displayname"));
185
186         title = json_object_get_string(json_object_object_get(o, "title"));
187
188         version = json_object_get_string(json_object_object_get(o, "version"));
189
190         name = json_object_get_string(json_object_object_get(o, "name"));
191
192         return distro_series_new(name,
193                                  version,
194                                  title,
195                                  displayname);
196 }
197
198 struct bpph **json_object_to_bpph_list(json_object *o)
199 {
200         json_object *o_entries;
201         int i, n, i2;
202         struct bpph **entries, *h;
203         const struct distro_arch_series *distro;
204
205         o_entries = json_object_object_get(o, "entries");
206
207         if (!o_entries)
208                 return NULL;
209
210         n = json_object_array_length(o_entries);
211
212         entries = malloc
213                 (sizeof(struct bpph *)*(n+1));
214
215         for (i = 0, i2 = 0; i < n; i++) {
216                 h = json_to_bpph(json_object_array_get_idx(o_entries,
217                                                            i));
218
219                 if (!h->architecture_specific) {
220                         distro = get_distro_arch_series
221                                 (h->distro_arch_series_link);
222
223                         if (!distro || !distro->is_nominated_arch_indep) {
224                                 bpph_free(h);
225                                 continue ;
226                         }
227                 }
228
229                 entries[i2] = h;
230                 i2++;
231         }
232         entries[i2] = NULL;
233
234         return entries;
235 }
236
237 json_object *bpph_list_to_json(struct bpph **list)
238 {
239         json_object *result, *entries;
240         struct bpph **cur;
241
242         result = json_object_new_object();
243
244         entries = json_object_new_array();
245         json_object_object_add(result, "entries", entries);
246
247         if (list)
248                 for (cur = list; *cur; cur++)
249                         json_object_array_add(entries, bpph_to_json(*cur));
250
251         return result;
252 }
253
254 struct daily_download_total *
255 json_object_to_daily_download_total(const char *d, json_object *o_c)
256 {
257         struct daily_download_total *result;
258
259         result = malloc(sizeof(struct daily_download_total));
260         result->count = json_object_get_int(o_c);
261
262         memset(&result->date, 0, sizeof(struct tm));
263         strptime(d, "%FT%T%z", &result->date);
264
265         return result;
266 }
267
268 static int json_object_get_fields_count(json_object *o)
269 {
270         int n = 0;
271         struct lh_entry *entry;
272
273         entry = json_object_get_object(o)->head;
274         while (entry) {
275                 entry = entry->next;
276                 n++;
277         }
278
279         return n;
280 }
281
282 struct daily_download_total * *
283 json_object_to_daily_download_totals(json_object *o)
284 {
285         int n, i;
286         struct daily_download_total **result;
287
288         n = json_object_get_fields_count(o);
289
290         result = malloc
291                 (sizeof(struct daily_download_total *)*(n+1));
292
293         i = 0;
294         json_object_object_foreach(o, key, val) {
295                 result[i] = json_object_to_daily_download_total(key, val);
296                 i++;
297         }
298
299         result[n] = NULL;
300
301         return result;
302 }
303
304 struct json_object *date_to_json(struct tm *tm)
305 {
306         json_object *json;
307
308         json = json_object_new_array();
309         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
310         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
311         json_object_array_add(json, json_object_new_int(tm->tm_mday));
312
313         return json;
314 }
315
316 json_object *ddts_to_json(struct daily_download_total **ddts)
317 {
318         json_object *json_ddt, *json_ddts;
319         struct daily_download_total *ddt;
320
321         json_ddts = json_object_new_array();
322
323         while (ddts && *ddts) {
324                 ddt = *ddts;
325
326                 json_ddt = json_object_new_object();
327                 json_object_object_add(json_ddt,
328                                        "value",
329                                        json_object_new_int(ddt->count));
330                 json_object_object_add(json_ddt,
331                                        "time",
332                                        date_to_json(&ddt->date));
333
334                 json_object_array_add(json_ddts, json_ddt);
335
336                 ddts++;
337         }
338
339         return json_ddts;
340 }