src/graph_instance.[ch]: Implement "inst_data_to_json".
[collection4.git] / src / action_search_json.c
1 /**
2  * collection4 - action_search_json.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "action_search_json.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
36
37 #include <fcgiapp.h>
38 #include <fcgi_stdio.h>
39
40 #define RESULT_LIMIT 10
41
42 struct callback_data_s
43 {
44   graph_config_t *cfg;
45   int limit;
46   _Bool first;
47 };
48 typedef struct callback_data_s callback_data_t;
49
50 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
51 {
52   char desc[1024];
53
54   if (cfg == NULL)
55     return (EINVAL);
56
57   graph_get_title (cfg, desc, sizeof (desc));
58
59   printf ("{\"title\":\"%s\",\"instances\":[", desc);
60
61   return (0);
62 } /* }}} int json_begin_graph */
63
64 static int json_end_graph (void) /* {{{ */
65 {
66   printf ("]}");
67
68   return (0);
69 } /* }}} int json_end_graph */
70
71 static int json_print_instance (graph_config_t *cfg, /* {{{ */
72     graph_instance_t *inst)
73 {
74   char params[1024];
75   char desc[1024];
76
77   if ((cfg == NULL) || (inst == NULL))
78     return (EINVAL);
79
80   memset (desc, 0, sizeof (desc));
81   inst_describe (cfg, inst, desc, sizeof (desc));
82
83   memset (params, 0, sizeof (params));
84   inst_get_params (cfg, inst, params, sizeof (params));
85
86   printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
87       desc, params);
88
89   return (0);
90 } /* }}} int json_print_instance */
91
92 static int json_print_graph_instance (graph_config_t *cfg, /* {{{ */
93     graph_instance_t *inst,
94     void *user_data)
95 {
96   callback_data_t *data = user_data;
97
98   if (data->cfg != cfg)
99   {
100     if (!data->first)
101     {
102       json_end_graph ();
103       printf (",\n");
104     }
105     json_begin_graph (cfg);
106
107     data->cfg = cfg;
108     data->first = 0;
109   }
110   else /* if (not first instance) */
111   {
112     printf (",\n");
113   }
114
115   json_print_instance (cfg, inst);
116
117   if (data->limit > 0)
118     data->limit--;
119
120   if (data->limit == 0)
121     return (1);
122
123   return (0);
124 } /* }}} int json_print_graph_instance */
125
126 static int list_graphs_json (const char *term) /* {{{ */
127 {
128   callback_data_t data;
129
130   time_t now;
131   char time_buffer[128];
132   int status;
133
134   printf ("Content-Type: application/json\n");
135
136   now = time (NULL);
137   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
138   if (status == 0)
139     printf ("Expires: %s\n"
140         "Cache-Control: public\n",
141         time_buffer);
142   printf ("\n");
143
144   data.cfg = NULL;
145   data.limit = RESULT_LIMIT;
146   data.first = 1;
147
148   printf ("[\n");
149   if (term == NULL)
150     gl_instance_get_all (json_print_graph_instance, /* user_data = */ &data);
151   else
152     gl_search_string (term, json_print_graph_instance, /* user_data = */ &data);
153
154   if (!data.first)
155     json_end_graph ();
156
157   printf ("\n]");
158
159   return (0);
160 } /* }}} int list_graphs_json */
161
162 int action_search_json (void) /* {{{ */
163 {
164   char *search;
165   int status;
166
167   search = strtolower_copy (param ("q"));
168
169   status = list_graphs_json (search);
170
171   free (search);
172
173   return (status);
174 } /* }}} int action_search_json */
175
176 /* vim: set sw=2 sts=2 et fdm=marker : */