-
Notifications
You must be signed in to change notification settings - Fork 0
/
gkrellm-pihole.c
663 lines (559 loc) · 22 KB
/
gkrellm-pihole.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* pihole monitor gkrellm plugin
* version 0.1
* shows number of queries in the last 24 hours and number blocked
* and a nice pihole icon
* (c) 2023 JCC gkrellm@cardot.net
*/
#include <gkrellm2/gkrellm.h>
#include <stdio.h>
#include <curl/curl.h>
#include <X11/Xlib.h>
#include <pthread.h>
#include "pihole.xpm"
#define CONFIG_NAME "gkrellm-pihole"
#define STYLE_NAME "gkrellm-pihole"
#define PIHOLE_URL_PATTERN "http://%s/admin/api.php?%s&auth=%s"
#define PIHOLE_DEFAULT_FREQ 10
#define SPACING_BETWEEN_ROWS 4
#define SPACING_BETWEEN_COLUMNS 6
#define PIHOLE_ONLINE 0
#define PIHOLE_OFFLINE 1
static GkrellmMonitor *monitor;
static GkrellmTicks *pGK;
static GkrellmPanel *panel;
static GkrellmDecal *decal_pihole_icon;
static GkrellmDecal *decal_label1;
static GkrellmDecal *decal_text1;
static GkrellmDecal *decal_label2;
static GkrellmDecal *decal_text2;
static GdkPixmap *pihole_gdkpixmap;
static gboolean resources_acquired;
static gchar *pihole_URL, *dns_queries_today, *ads_blocked_today, *status;
static gint style_id;
static gint update=-1;
static gboolean blocking_disabled;
static gint blocking_disabled_time;
static GtkWidget *pihole_hostname_fillin;
static gchar *pihole_hostname;
static GtkWidget *pihole_API_key_fillin;
static gchar *pihole_API_key;
static GtkWidget *pihole_freq_spinner;
static gint pihole_freq = PIHOLE_DEFAULT_FREQ;
static GtkWidget *pihole_url_pattern_fillin;
static gchar *pihole_url_pattern;
CURL *curl;
struct MemoryStruct {
char *memory;
size_t size;
} chunk;
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
gboolean
callURL(gchar *pihole_URL) {
CURLcode res=0;
//printf("calling %s\n", pihole_URL);
if(!curl) {
fprintf(stderr, "curl is not initialized\n");
return FALSE;
}
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_easy_setopt(curl, CURLOPT_URL, pihole_URL);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* pihole must answer quickly, else there is a problem anyway */
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 2000);
res = curl_easy_perform(curl);
//printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
if(res != CURLE_OK || chunk.size == 0) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return FALSE;
}
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
//printf("Response code: %lu\nBody: %s\n", response_code, chunk.memory);
if (response_code >= 400) {
fprintf(stderr, "curl_easy_perform() response code: %lu\n", response_code);
return FALSE;
}
/* if the api_key is incorrect, the api answers with "[]" */
if (!strcmp(chunk.memory, "[]")) {
puts("Incorrect API key");
return FALSE;
}
/* the call when well */
return TRUE;
}
gchar
*parseJson(gchar *key) { // key must be given with the enclosing double quotes
char *mystr;
gchar *value;
mystr = strstr(chunk.memory, key);
if (mystr) {
mystr += strlen(key) + 1;
strtok(mystr, ","); // change ',' to '\0'
value = g_strdup(mystr);
mystr += strlen(mystr);
mystr[0] = ','; // restore the ',' char
return value;
}
return g_strdup("");
}
gboolean
pihole(void)
{
if (pihole_URL == NULL || pihole_URL[0] == 0) {
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_OFFLINE);
puts("No URL defined");
return FALSE;
}
if (!callURL(pihole_URL)) {
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_OFFLINE);
return FALSE;
}
/* find the data in chunk.memory (parse the json),
* named "dns_queries_today", "ads_blocked_today" & "status" */
g_free(dns_queries_today);
dns_queries_today = parseJson("\"dns_queries_today\"");
g_free(ads_blocked_today);
ads_blocked_today = parseJson("\"ads_blocked_today\"");
g_free(status);
status = parseJson("\"status\"");
if (!strcmp(status, "\"disabled\"")) {
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_OFFLINE);
/*if (!blocking_disabled) { // disabled from outside gkrellm, e.g. the dashboard
blocking_disabled = TRUE;
blocking_disabled_time = -1; // indefinitely as we don't know how much
}*/
} else if (blocking_disabled_time == 0) {
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_ONLINE);
// force unblocking (if enabled done outside of gkrellm)
blocking_disabled = FALSE;
blocking_disabled_time = 0;
}
free(chunk.memory);
return TRUE;
}
/***********************************************************/
static gint
panel_expose_event(GtkWidget *widget, GdkEventExpose *ev) {
gdk_draw_pixmap(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
panel->pixmap, ev->area.x, ev->area.y, ev->area.x, ev->area.y,
ev->area.width, ev->area.height);
return FALSE;
}
void
open_dashboard (void) {
gchar *cmd;
cmd = g_strdup_printf("xdg-open http://%s", pihole_hostname);
system(cmd);
free(cmd);
}
void
*update_thread(void *vargp) {
gint w;
GkrellmTextstyle *ts /*, *ts_alt*/;
ts = gkrellm_meter_textstyle(style_id);
//ts_alt = gkrellm_meter_alt_textstyle(style_id);
w = gkrellm_chart_width();
if (!pihole()) { // call the pi-hole, update the labels only if the call was ok
gkrellm_draw_panel_layers(panel);
return NULL;
}
// right align values
decal_text1->x_off =
(w - gdk_string_width(gdk_font_from_description(ts->font), dns_queries_today)) - 4;
//(w - gdk_string_width(decal_text1->text_style.font, dns_queries_today));
if (decal_text1->x_off < 0)
decal_text1->x_off = 0;
decal_text2->x_off =
(w - gdk_string_width(gdk_font_from_description(ts->font), ads_blocked_today)) - 4;
//(w - gdk_string_width(decal_text1->text_style.font, ads_blocked_today));
if (decal_text2->x_off < 0)
decal_text2->x_off = 0;
gkrellm_draw_decal_text(panel, decal_label1, "Total", 0);
gkrellm_draw_decal_text(panel, decal_text1, dns_queries_today, 0);
gkrellm_draw_decal_text(panel, decal_label2, "Ads", 0);
gkrellm_draw_decal_text(panel, decal_text2, ads_blocked_today, 0);
gkrellm_draw_panel_layers(panel);
return NULL;
}
// Callback function for menu items
void
on_menu_item_clicked(GtkMenuItem *item, gpointer user_data) {
if (!strcmp((char *)user_data, "open_dashboard")) {
open_dashboard();
}
else if (!strncmp((char *)user_data, "api:", strlen("api:"))) { // command to send as-is to the API
gboolean callOK;
gchar *pihole_URL = g_strdup_printf(pihole_url_pattern, pihole_hostname, (char *)user_data + strlen("api:"), pihole_API_key);
callOK = callURL(pihole_URL);
if (!callOK)
callOK = callURL(pihole_URL); // try again (timeout?)
g_free(pihole_URL);
if (!callOK)
return;
if (!strncmp((char *)user_data, "api:disable", strlen("api:disable"))) {
blocking_disabled = TRUE;
if (strlen((char *)user_data) == strlen("api:disable"))
blocking_disabled_time = -1; // indefinitely
else
blocking_disabled_time = atoi((char *)user_data + strlen("api:disable") + 1);
//printf("blocking_disabled_time = %i\n", blocking_disabled_time);
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_OFFLINE);
gkrellm_draw_panel_layers(panel);
}
else if (!strcmp((char *)user_data, "api:enable")) {
blocking_disabled = FALSE;
blocking_disabled_time = 0;
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_ONLINE);
gkrellm_draw_panel_layers(panel);
}
}
else if (!strcmp((char *)user_data, "config")) {
gkrellm_open_config_window(monitor);
}
else if (!strcmp((char *)user_data, "update")) {
int i=0;
update_thread(&i);
}
}
gchar
*secondsToHHMMSS(int totalSeconds) {
int hours, minutes, seconds;
hours = totalSeconds / 3600;
totalSeconds %= 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
return g_strdup_printf("%02d:%02d:%02d", hours, minutes, seconds);
}
static gint
panel_button_press_event(GtkWidget *widget, GdkEventButton *ev, gpointer data) {
GtkWidget *menu;
int i=0;
switch (ev->button) {
case 1:
menu = gtk_menu_new();
GtkWidget *title_item1 = gtk_menu_item_new_with_label("Disable blocking indefinitely");
g_signal_connect(G_OBJECT(title_item1), "activate", G_CALLBACK(on_menu_item_clicked), "api:disable");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), title_item1);
GtkWidget *menu_item11 = gtk_menu_item_new_with_label(" or for 10 seconds");
g_signal_connect(G_OBJECT(menu_item11), "activate", G_CALLBACK(on_menu_item_clicked), "api:disable=10");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item11);
GtkWidget *menu_item12 = gtk_menu_item_new_with_label(" or for 30 seconds");
g_signal_connect(G_OBJECT(menu_item12), "activate", G_CALLBACK(on_menu_item_clicked), "api:disable=30");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item12);
GtkWidget *menu_item13 = gtk_menu_item_new_with_label(" or 5 minutes");
g_signal_connect(G_OBJECT(menu_item13), "activate", G_CALLBACK(on_menu_item_clicked), "api:disable=300");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item13);
gchar *label;
if (blocking_disabled && blocking_disabled_time > 0) {
gchar *hhmmss;
hhmmss = secondsToHHMMSS(blocking_disabled_time);
label = g_strdup_printf("Enable blocking (%s)", hhmmss);
g_free(hhmmss);
}
else if (blocking_disabled && blocking_disabled_time < 0) {
label = g_strdup("Enable blocking (disabled)");
}
else {
label = g_strdup("Enable blocking");
}
GtkWidget *menu_item2 = gtk_menu_item_new_with_label(label);
g_signal_connect(G_OBJECT(menu_item2), "activate", G_CALLBACK(on_menu_item_clicked), "api:enable");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item2);
free(label);
GtkWidget *separator1 = gtk_separator_menu_item_new ();
gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator1);
GtkWidget *menu_item3 = gtk_menu_item_new_with_label("Plugin configuration");
g_signal_connect(G_OBJECT(menu_item3), "activate", G_CALLBACK(on_menu_item_clicked), "config");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item3);
GtkWidget *menu_item4 = gtk_menu_item_new_with_label("Open pi-hole dashboard");
g_signal_connect(G_OBJECT(menu_item4), "activate", G_CALLBACK(on_menu_item_clicked), "open_dashboard");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item4);
GtkWidget *menu_item5 = gtk_menu_item_new_with_label("Update display");
g_signal_connect(G_OBJECT(menu_item5), "activate", G_CALLBACK(on_menu_item_clicked), "update");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item5);
gtk_widget_show_all(menu);
// Popup the menu at the event's position
gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, ev->button, ev->time);
break;
case 2:
//open_dashboard();
update_thread(&i);
break;
case 3:
gkrellm_open_config_window(monitor);
break;
}
return TRUE;
}
static void
updateURL() {
//puts(pihole_url_pattern);
if (pihole_hostname != NULL && pihole_API_key != NULL && pihole_url_pattern != NULL) {
if (pihole_URL != NULL)
g_free(pihole_URL);
pihole_URL = g_strdup_printf(pihole_url_pattern, pihole_hostname, "summaryRaw", pihole_API_key);
}
//puts(pihole_URL);
}
static void
update_plugin() {
// Do it only once every n seconds
if (update >= 0) {
if (pGK->second_tick) {
update++;
if (blocking_disabled && blocking_disabled_time > 0) {
blocking_disabled_time--;
if (blocking_disabled_time == 0) {
blocking_disabled = FALSE;
gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, PIHOLE_ONLINE);
gkrellm_draw_panel_layers(panel);
}
//printf("blocking_disabled_time = %i\n", blocking_disabled_time);
}
}
if (update < pihole_freq) return;
update = 0;
}
else
update = 0; // first time
/* we will run the update in a thread in order not to block refreshes of the other krells */
//static pthread_t thread_id = 0;
size_t i=0;
/* in case it has not finished yet, wait for the previous thread */
/*if (thread_id != 0)
pthread_join(thread_id, NULL);
pthread_create(&thread_id, NULL, update_thread, &i);*/
update_thread(&i);
}
static void
enable_plugin(void) {
//printf("plugin is being initialized.\n");
curl = curl_easy_init();
updateURL();
/*
// Initialize the XCB threading system
if (!XInitThreads()) {
fprintf(stderr, "Error: XInitThreads failed.\n");
return;
}
*/
resources_acquired = TRUE;
}
static void
disable_plugin(void) {
//printf("plugin is being disabled.\n");
curl_easy_cleanup(curl);
resources_acquired = FALSE;
}
static void
create_plugin(GtkWidget *vbox, gint first_create) {
GkrellmStyle *style;
GkrellmTextstyle *ts, *ts_alt;
GdkBitmap *mask;
int y;
gint w,h;
if (!resources_acquired) {
enable_plugin();
gkrellm_disable_plugin_connect(monitor, disable_plugin);
}
if (first_create)
panel = gkrellm_panel_new0();
style = gkrellm_meter_style(style_id);
ts = gkrellm_meter_textstyle(style_id);
ts_alt = gkrellm_meter_alt_textstyle(style_id);
y = -1; /* y = -1 places at top margin */
pihole_gdkpixmap = gdk_pixmap_create_from_xpm_d(vbox->window, &mask, NULL, (char **)pihole_xpm);
decal_pihole_icon = gkrellm_create_decal_pixmap(panel, pihole_gdkpixmap, mask, 2, style, 4, 4);
//gkrellm_draw_decal_pixmap(panel, decal_pihole_icon, 0);
gdk_pixmap_get_size(pihole_gdkpixmap, &w, &h);
decal_label1 = gkrellm_create_decal_text(panel,
"Total", /* string used for vertical sizing */
ts_alt,
style,
w + SPACING_BETWEEN_COLUMNS, /* x = -1 places at left margin */
y, /* y = -1 places at top margin */
-1); /* use full width */
decal_text1 = gkrellm_create_decal_text(panel, "0", ts, style, -1, -1, -1);
y += decal_text1->h + SPACING_BETWEEN_ROWS;
decal_label2 = gkrellm_create_decal_text(panel,
"Ads", /* string used for vertical sizing */
ts_alt,
style,
w + SPACING_BETWEEN_COLUMNS, /* x = -1 places at left margin */
y, /* y = -1 places at top margin */
-1); /* use full width */
decal_text2 = gkrellm_create_decal_text(panel, "0", ts, style, -1, y, -1);
gkrellm_panel_configure(panel, NULL, style);
gkrellm_panel_create(vbox, monitor, panel);
if (first_create) {
g_signal_connect(G_OBJECT (panel->drawing_area), "expose_event",
G_CALLBACK (panel_expose_event), NULL);
g_signal_connect(G_OBJECT (panel->drawing_area), "button_press_event",
G_CALLBACK (panel_button_press_event), NULL );
}
}
/********************************************************/
static void
save_plugin_config(FILE *f) {
if (pihole_hostname != NULL)
fprintf(f, "%s pihole_hostname %s\n", CONFIG_NAME, pihole_hostname);
if (pihole_API_key != NULL)
fprintf(f, "%s pihole_api_key %s\n", CONFIG_NAME, pihole_API_key);
if (pihole_freq > 0)
fprintf(f, "%s pihole_freq %u\n", CONFIG_NAME, pihole_freq);
if (pihole_url_pattern != NULL)
fprintf(f, "%s pihole_url_pattern %s\n", CONFIG_NAME, pihole_url_pattern);
}
static void
load_plugin_config(gchar *arg) {
gchar config[64], item[256], value[256];
gint n;
//printf("load_plugin_config(%s)\n", arg);
n = sscanf(arg, "%s %[^\n]", config, item);
if (n == 2) {
if (!strcmp(config, "pihole_hostname")) {
sscanf(item, "%s\n", value);
pihole_hostname = g_strdup(value);
}
else if (!strcmp(config, "pihole_api_key")) {
sscanf(item, "%s\n", value);
pihole_API_key = g_strdup(value);
}
else if (!strcmp(config, "pihole_freq")) {
sscanf(item, "%u\n", &pihole_freq);
}
else if (!strcmp(config, "pihole_url_pattern")) {
sscanf(item, "%s\n", value);
pihole_url_pattern = g_strdup(value);
}
//updateURL();
}
}
static void
apply_plugin_config() {
if (pihole_hostname != NULL) g_free(pihole_hostname);
pihole_hostname = g_strdup(gtk_entry_get_text(GTK_ENTRY(pihole_hostname_fillin)));
if (pihole_API_key != NULL) g_free(pihole_API_key);
pihole_API_key = g_strdup(gtk_entry_get_text(GTK_ENTRY(pihole_API_key_fillin)));
pihole_freq = gtk_spin_button_get_value(GTK_SPIN_BUTTON(pihole_freq_spinner));
if (pihole_url_pattern != NULL) g_free(pihole_url_pattern);
pihole_url_pattern = g_strdup(gtk_entry_get_text(GTK_ENTRY(pihole_url_pattern_fillin)));
updateURL();
update = -1;
update_plugin();
}
static gchar* plugin_info_text[] = {
"<h>Pi-hole monitor\n",
"\n\t(c) 2023 JC <gkrellm@cardot.net>\n",
"\n\tMonitors your Pihole activity.\n",
"\n\tThe menu (on click) allows to trigger actions on your Pi-hole,",
"\n\tsuch as disabling it for a given time or indefinitely, enabling it,",
"\n\tor opening the dashboard in your browser.\n",
"\n\tReleased under the GNU General Public License\n",
"\n\t", "<ul>Note", ": the default URL called on the Pihole is:\n",
"\t" PIHOLE_URL_PATTERN
};
static void
create_plugin_tab(GtkWidget *tab_vbox) {
GtkWidget *tabs, *vbox, *table, *text, *label_hostname, *label_API_key, *label_freq, *label_url;
gint i;
/* Make a couple of tabs. One for setup and one for info
*/
tabs = gtk_notebook_new();
gtk_notebook_set_tab_pos(GTK_NOTEBOOK(tabs), GTK_POS_TOP);
gtk_box_pack_start(GTK_BOX(tab_vbox), tabs, TRUE, TRUE, 0);
/* --Setup tab */
vbox = gkrellm_gtk_framed_notebook_page(tabs, "Setup");
/* configuration widgets */
table = gtk_table_new(3, 2, FALSE);
label_hostname = gtk_label_new("Pihole hostname:");
gtk_misc_set_alignment (GTK_MISC (label_hostname), 1, 1);
gtk_table_attach(GTK_TABLE(table), label_hostname, 0, 1, 0, 1, GTK_FILL, 0, 1, 1);
pihole_hostname_fillin = gtk_entry_new_with_max_length(255);
gtk_table_attach(GTK_TABLE(table), pihole_hostname_fillin, 1, 2, 0, 1, GTK_FILL|GTK_EXPAND, 0, 1, 1);
if (pihole_hostname != NULL)
gtk_entry_set_text(GTK_ENTRY(pihole_hostname_fillin), pihole_hostname);
label_API_key = gtk_label_new("API key:");
gtk_misc_set_alignment (GTK_MISC (label_API_key), 1, 1);
gtk_table_attach(GTK_TABLE(table), label_API_key, 0, 1, 1, 2, GTK_FILL, 0, 1, 1);
pihole_API_key_fillin = gtk_entry_new_with_max_length(255);
gtk_table_attach(GTK_TABLE(table), pihole_API_key_fillin, 1, 3, 1, 2, GTK_FILL|GTK_EXPAND, 0, 1, 1);
if (pihole_API_key != NULL)
gtk_entry_set_text(GTK_ENTRY(pihole_API_key_fillin), pihole_API_key);
label_freq = gtk_label_new("Refresh frequence (seconds):");
gtk_misc_set_alignment (GTK_MISC (label_freq), 1, 1);
gtk_table_attach(GTK_TABLE(table), label_freq, 0, 1, 2, 3, GTK_FILL, 0, 1, 1);
pihole_freq_spinner = gtk_spin_button_new_with_range(1, 999, 1);
gtk_table_attach(GTK_TABLE(table), pihole_freq_spinner, 1, 4, 2, 3, GTK_FILL|GTK_EXPAND, 0, 1, 1);
if (pihole_freq > 0)
gtk_spin_button_set_value(GTK_SPIN_BUTTON(pihole_freq_spinner), pihole_freq);
gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 2);
/* --Advanced tab */
vbox = gkrellm_gtk_framed_notebook_page(tabs, "Advanced");
/* configuration widgets */
table = gtk_table_new(1, 2, FALSE);
label_url = gtk_label_new("URL pattern:");
gtk_misc_set_alignment (GTK_MISC (label_url), 1, 1);
gtk_table_attach(GTK_TABLE(table), label_url, 0, 1, 0, 1, GTK_FILL, 0, 1, 1);
pihole_url_pattern_fillin = gtk_entry_new_with_max_length(255);
gtk_table_attach(GTK_TABLE(table), pihole_url_pattern_fillin, 1, 2, 0, 1, GTK_FILL|GTK_EXPAND, 0, 1, 1);
gtk_entry_set_text(GTK_ENTRY(pihole_url_pattern_fillin), pihole_url_pattern);
gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 2);
/* --Info tab */
vbox = gkrellm_gtk_framed_notebook_page(tabs, "Info");
text = gkrellm_gtk_scrolled_text_view(vbox, NULL,
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
for (i = 0; i < sizeof(plugin_info_text)/sizeof(gchar *); ++i)
gkrellm_gtk_text_view_append(text, plugin_info_text[i]);
}
static GkrellmMonitor plugin_mon = {
CONFIG_NAME, /* Name, for config tab. */
0, /* Id, 0 if a plugin */
create_plugin, /* The create function */
update_plugin, /* The update function */
create_plugin_tab, /* The config tab create function */
apply_plugin_config, /* Apply the config function */
save_plugin_config, /* Save user config */
load_plugin_config, /* Load user config */
CONFIG_NAME, /* config keyword */
NULL, /* Undefined 2 */
NULL, /* Undefined 1 */
NULL, /* private */
MON_INSERT_AFTER|MON_NET, /* Insert plugin before this monitor */
NULL, /* Handle if a plugin, filled in by GKrellM */
NULL /* path if a plugin, filled in by GKrellM */
};
GkrellmMonitor*
gkrellm_init_plugin() {
pGK = gkrellm_ticks();
dns_queries_today = g_strdup("--");
ads_blocked_today = g_strdup("--");
pihole_url_pattern = g_strdup(PIHOLE_URL_PATTERN);
style_id = gkrellm_add_meter_style(&plugin_mon, STYLE_NAME);
monitor = &plugin_mon;
return &plugin_mon;
}