6 #include "action_list_graphs.h"
8 #include "graph_ident.h"
9 #include "graph_list.h"
10 #include "utils_cgi.h"
13 #include <fcgi_stdio.h>
15 struct callback_data_s
21 typedef struct callback_data_s callback_data_t;
23 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
30 graph_get_title (cfg, desc, sizeof (desc));
32 printf ("{\"title\":\"%s\",\"instances\":[", desc);
35 } /* }}} int json_begin_graph */
37 static int json_end_graph (void) /* {{{ */
42 } /* }}} int json_end_graph */
44 static int json_print_instance (graph_config_t *cfg, /* {{{ */
45 graph_instance_t *inst)
50 if ((cfg == NULL) || (inst == NULL))
53 memset (desc, 0, sizeof (desc));
54 inst_describe (cfg, inst, desc, sizeof (desc));
56 memset (params, 0, sizeof (params));
57 inst_get_params (cfg, inst, params, sizeof (params));
59 printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
63 } /* }}} int json_print_instance */
65 static int print_graph_inst_json (graph_config_t *cfg, /* {{{ */
66 graph_instance_t *inst,
69 callback_data_t *data = user_data;
78 json_begin_graph (cfg);
83 else /* if (not first instance) */
88 json_print_instance (cfg, inst);
97 } /* }}} int print_graph_inst_json */
99 static int list_graphs_json (const char *term) /* {{{ */
101 callback_data_t data;
104 char time_buffer[128];
107 printf ("Content-Type: application/json\n");
110 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
112 printf ("Expires: %s\n"
113 "Cache-Control: public\n",
123 gl_instance_get_all (print_graph_inst_json, /* user_data = */ &data);
125 gl_search (term, print_graph_inst_json, /* user_data = */ &data);
133 } /* }}} int list_graphs_json */
135 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
136 graph_instance_t *inst,
139 callback_data_t *data = user_data;
143 if (data->cfg != cfg)
145 if (data->cfg != NULL)
146 printf (" </ul></li>\n");
148 memset (desc, 0, sizeof (desc));
149 graph_get_title (cfg, desc, sizeof (desc));
150 html_escape_buffer (desc, sizeof (desc));
152 printf (" <li class=\"graph\">%s\n"
153 " <ul class=\"instance_list\">\n", desc);
158 memset (params, 0, sizeof (params));
159 inst_get_params (cfg, inst, params, sizeof (params));
160 html_escape_buffer (params, sizeof (params));
162 memset (desc, 0, sizeof (desc));
163 inst_describe (cfg, inst, desc, sizeof (desc));
164 html_escape_buffer (desc, sizeof (desc));
166 printf (" <li class=\"instance\"><a href=\"%s?action=graph;%s\">%s</a></li>\n",
167 script_name (), params, desc);
172 /* Abort scan if limit is reached. */
173 if (data->limit == 0)
177 } /* }}} int print_graph_inst_html */
179 static int list_graphs_html (const char *term) /* {{{ */
181 callback_data_t data = { NULL, /* limit = */ 20, /* first = */ 1 };
186 term_html = html_escape (term);
188 printf ("Content-Type: text/html\n\n");
190 printf ("<html>\n <head>\n");
192 printf (" <title>c4: Graphs matching "%s"</title>\n", term);
194 printf (" <title>c4: List of all graphs</title>\n");
195 printf (" <link rel=\"stylesheet\" type=\"text/css\" href=\"../share/style.css\" />\n");
196 printf (" <script type=\"text/javascript\" src=\"../share/jquery-1.4.2.min.js\">\n"
198 " <script type=\"text/javascript\" src=\"../share/collection.js\">\n"
200 printf (" </head>\n <body>\n");
202 printf ("<form action=\"%s\" method=\"get\">\n"
203 " <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
204 " <input type=\"text\" name=\"search\" value=\"%s\" id=\"search-input\" />\n"
205 " <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
207 script_name (), (term_html != NULL) ? term_html : "");
211 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
213 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &data);
215 gl_search (term, print_graph_inst_html, /* user_data = */ &data);
217 if (data.cfg != NULL)
218 printf (" </ul></li>\n");
222 printf (" </body>\n</html>\n");
225 } /* }}} int list_graphs_html */
227 int action_list_graphs (void) /* {{{ */
235 search = strtolower_copy (param ("search"));
237 format = param ("format");
241 if (strcmp ("json", format) == 0)
242 status = list_graphs_json (search);
244 status = list_graphs_html (search);
249 } /* }}} int action_list_graphs */
251 /* vim: set sw=2 sts=2 et fdm=marker : */