disabled notification at program start.

This commit is contained in:
Johannes Findeisen 2023-02-25 02:25:19 +01:00
commit 6fae1665b3

View file

@ -1,23 +1,20 @@
/*
* SpaceAPI based panel applet for UNIX using GTK2
* SpaceAPI based tray applet for BSD* / Linux / UNIX etc. using GTK2
*
* Author: hanez <you@hanez.org>
* Contributors: Haeger, atari, beh and eisbaer
*
* Website: https://hanez.org/project/spacetray/
* Contributors: atari, beh, eisbaer and Haeger
*
* License (Beerware License):
* As long as you retain this notice you can do whatever you want with this
* stuff. If we meet some day, and you think this stuff is worth it, you can buy
* me a beer in return hanez and all other contributers
*
* As long as you retain this notice you can do whatever you want with this
* stuff. If we meet some day, and you think this stuff is worth it, you can
* buy me a beer in return hanez and all other contributers
*
* TODO:
* - Add door open/close date to notification and status
* - Fix: not show notification twice at start when status "open"
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <curl/curl.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixdata.h>
@ -25,6 +22,10 @@
#if LIBNOTIFY
#include <libnotify/notify.h>
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "pesthoernchen.h"
@ -33,21 +34,21 @@ struct string {
size_t len;
};
static char *name = "Dooris for UNIX";
static char *name = "SpaceAPI widget for UNIX";
static char *statusurl = "https://www.hamburg.ccc.de/dooris/status.json";
static char *agent = "Dooris-for-UNIX/0.42";
static char *agent = "Spacetray/0.42";
// The delay for polling the dooris service. Adjust this before compiling
int delay = 900000; // ms. aka 15 minutes
static int delay = 900000; // ms. aka 15 minutes
bool door_open = false;
bool old_door_open = false;
bool door_open = false;
bool old_door_open = false;
void do_it();
void invoke_notification();
GtkStatusIcon *tray_icon;
bool do_it();
void invoke_notification();
void init_string(struct string *s) {
s->len = 0;
s->ptr = malloc(s->len+1);
@ -72,18 +73,14 @@ size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s) {
return size*nmemb;
}
void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data) {
do_it();
}
void get_bouncer_data() {
CURL *curl;
CURLcode res;
struct json_object *response_json_object;
struct json_object *door_json_object;
struct json_object *leaf_json_object;
bool door_status;
int door_last_change;
@ -94,7 +91,7 @@ void get_bouncer_data() {
old_door_open = door_open;
init_string(&s);
curl = curl_easy_init();
if(!curl) {
fprintf(stderr, "curl_easy_init() failed.\n");
@ -134,10 +131,10 @@ void get_bouncer_data() {
// free leaf object
//json_object_put(leaf_json_object);
// free response object
// json_object_put(response_json_object);
printf("Door status: = %d\n", door_status);
printf("Door last change: = %d\n", door_last_change);
@ -150,13 +147,13 @@ void get_bouncer_data() {
void invoke_notification() {
char door[20];
if (door_open == true) {
snprintf(door, 20, "Door open...");
} else {
snprintf(door, 20, "Door closed...");
}
gtk_status_icon_set_tooltip(tray_icon, door);
#if LIBNOTIFY
@ -168,13 +165,13 @@ void invoke_notification() {
if (door_open == true) {
notify_notification_set_icon_from_pixbuf(n, gdk_pixbuf_from_pixdata(
&icon_yellow_pixbuf,
true,
&icon_yellow_pixbuf,
true,
NULL));
} else {
notify_notification_set_icon_from_pixbuf(n, gdk_pixbuf_from_pixdata(
&icon_red_pixbuf,
true,
&icon_red_pixbuf,
true,
NULL));
}
notify_notification_show(n, NULL);
@ -185,50 +182,50 @@ void invoke_notification() {
void set_status() {
if (door_open == true) {
printf("Set to status: open\n");
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_yellow_pixbuf,
true,
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_yellow_pixbuf,
true,
NULL));
} else {
printf("Set to status: closed\n");
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_red_pixbuf,
true,
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_red_pixbuf,
true,
NULL));
}
}
}
bool do_it() {
void do_it() {
get_bouncer_data();
set_status();
if (door_open != old_door_open) {
invoke_notification();
}
return true;
}
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
tray_icon = gtk_status_icon_new();
g_signal_connect(G_OBJECT(tray_icon), "activate",
G_CALLBACK(tray_icon_on_click), NULL);
g_signal_connect(G_OBJECT(tray_icon), "activate",
G_CALLBACK(do_it), NULL);
gtk_status_icon_set_visible(tray_icon, true);
do_it();
// DEBUG STUFF
// DEBUG STUFF
//door_open = true;
//set_status();
// END DEBUG STUFF
invoke_notification();
//invoke_notification();
gtk_timeout_add(delay, (GtkFunction)do_it, (gpointer)NULL);
gtk_main();
return 0;
}