graph_def.c: Split the config handling into two functions.
[collection4.git] / action_list_graphs.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "action_list_graphs.h"
7 #include "graph_list.h"
8 #include "utils_params.h"
9
10 #include <fcgiapp.h>
11 #include <fcgi_stdio.h>
12
13 static int print_graph_inst_json (__attribute__((unused)) graph_config_t *cfg, /* {{{ */
14     graph_instance_t *inst,
15     void *user_data)
16 {
17   _Bool *first;
18   graph_ident_t *ident;
19   char *json;
20
21   first = user_data;
22
23   ident = gl_instance_get_selector (inst);
24   if (ident == NULL)
25     return (-1);
26
27   json = ident_to_json (ident);
28   if (json == NULL)
29   {
30     ident_destroy (ident);
31     return (ENOMEM);
32   }
33
34   if (*first)
35     printf ("%s", json);
36   else
37     printf (",\n%s", json);
38
39   *first = 0;
40
41   ident_destroy (ident);
42   return (0);
43 } /* }}} int print_graph_inst_json */
44
45 static int print_graph_json (graph_config_t *cfg, /* {{{ */
46     void *user_data)
47 {
48   return (gl_graph_instance_get_all (cfg, print_graph_inst_json, user_data));
49 } /* }}} int print_graph_json */
50
51 static int list_graphs_json (void) /* {{{ */
52 {
53   _Bool first = 1;
54
55   printf ("Content-Type: application/json\n\n");
56
57   printf ("[\n");
58   gl_graph_get_all (print_graph_json, /* user_data = */ &first);
59   printf ("\n]");
60
61   return (0);
62 } /* }}} int list_graphs_json */
63
64 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
65     graph_instance_t *inst,
66     __attribute__((unused)) void *user_data)
67 {
68   char buffer[1024];
69
70   memset (buffer, 0, sizeof (buffer));
71   gl_instance_get_params (cfg, inst, buffer, sizeof (buffer));
72
73   printf ("<li><a href=\"test.fcgi?action=graph;%s\">%s</a></li>\n", buffer, buffer);
74
75   return (0);
76 } /* }}} int print_graph_inst_html */
77
78 static int print_graph_html (graph_config_t *cfg, /* {{{ */
79     __attribute__((unused)) void *user_data)
80 {
81   char buffer[1024];
82
83   memset (buffer, 0, sizeof (buffer));
84   gl_graph_get_title (cfg, buffer, sizeof (buffer));
85
86   printf ("<li>%s\n<ul>\n", buffer);
87   gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
88   printf ("</ul>\n");
89
90   return (0);
91 } /* }}} int print_graph_html */
92
93 static int list_graphs_html (void) /* {{{ */
94 {
95   printf ("Content-Type: text/html\n\n");
96
97   printf ("<ul>\n");
98   gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
99   printf ("</ul>\n");
100
101   return (0);
102 } /* }}} int list_graphs_html */
103
104 int action_list_graphs (void) /* {{{ */
105 {
106   const char *format;
107
108   gl_update ();
109
110   format = param ("format");
111   if (format == NULL)
112     format = "html";
113
114   if (strcmp ("json", format) == 0)
115     return (list_graphs_json ());
116   else
117     return (list_graphs_html ());
118 } /* }}} int action_list_graphs */
119
120 /* vim: set sw=2 sts=2 et fdm=marker : */