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