commit b738b47cc474ffa512a4c9e0542a30db19b4c414
parent 9568999325fdf8e3ffb89dc55d369bd57c3a280f
Author: Sweets <Sweets@users.noreply.github.com>
Date: Sat, 4 Jul 2020 12:58:30 -0700
Add: hints output test
Diffstat:
2 files changed, 49 insertions(+), 10 deletions(-)
diff --git a/callbacks.c b/callbacks.c
@@ -35,7 +35,7 @@ void method_handler(GDBusConnection *connection, const gchar *sender,
g_variant_iter_next(&iterator, "as", &actions);
GVariant *hints;
- g_variant_iter_next(&iterator, "a{sv}", &hints);
+ g_variant_iter_next(&iterator, "@a{s*}", &hints);
gint32 timeout;
g_variant_iter_next(&iterator, "i", &timeout);
diff --git a/format.c b/format.c
@@ -2,6 +2,7 @@
#include "config.h"
#include "format.h"
+#include "tiramisu.h"
char* escape_quotes(char *str, char *out) {
memset(out, 0, strlen(out));
@@ -19,29 +20,67 @@ char* escape_quotes(char *str, char *out) {
void output_notification(gchar *app_name, guint32 replaces_id, gchar *app_icon,
gchar *summary, gchar *body, GVariant *actions, GVariant *hints,
gint32 timeout) {
-
-#ifdef PRINT_JSON
-
+
char *escaped_string = (char *)calloc(512, sizeof(char));
-
+
+ const char *int_format = ", \"%s\": %d";
+ const char *uint_format = ", \"%s\": %u";
+
+ GVariantIter iterator;
+ gchar *key;
+ GVariant *v; // Short-hand for `value`
+
+ g_variant_iter_init(&iterator, hints);
+ while (g_variant_iter_loop(&iterator, "{s*}", &key, NULL)) {
+
+ /* Horrible code, but for now it's just in testing. */
+
+ if ((v = g_variant_lookup_value(hints, key, G_VARIANT_TYPE_STRING)))
+ printf(", \"%s\": \"%s\"", key,
+ escape_quotes(g_variant_dup_string(v, NULL), escaped_string));
+ else if ((v = g_variant_lookup_value(hints, key, G_VARIANT_TYPE_INT16)))
+ printf(int_format, key, g_variant_get_int16(v));
+ else if ((v = g_variant_lookup_value(hints, key, G_VARIANT_TYPE_INT32)))
+ printf(int_format, key, g_variant_get_int32(v));
+ else if ((v = g_variant_lookup_value(hints, key, G_VARIANT_TYPE_INT64)))
+ printf(int_format, key, g_variant_get_int64(v));
+ else if ((v = g_variant_lookup_value(hints, key,G_VARIANT_TYPE_UINT16)))
+ printf(uint_format, key, g_variant_get_uint16(v));
+ else if ((v = g_variant_lookup_value(hints, key,G_VARIANT_TYPE_UINT32)))
+ printf(uint_format, key, g_variant_get_uint32(v));
+ else if ((v = g_variant_lookup_value(hints, key,G_VARIANT_TYPE_UINT64)))
+ printf(uint_format, key, g_variant_get_uint64(v));
+ else if ((v = g_variant_lookup_value(hints, key,G_VARIANT_TYPE_DOUBLE)))
+ printf(", \"%s\": %f", key, g_variant_get_double(v));
+ else if ((v=g_variant_lookup_value(hints,key,G_VARIANT_TYPE_BOOLEAN))) {
+ if (g_variant_get_boolean(v))
+ printf(", \"%s\": 1", key);
+ else
+ printf(", \"%s\": 0", key);
+ } else if ((v = g_variant_lookup_value(hints, key,G_VARIANT_TYPE_BYTE)))
+ printf(", \"%s\": %x", key, g_variant_get_byte(v));
+
+ }
+
+#ifdef PRINT_JSON
+
printf("{ \"app_name\": \"%s\",", escape_quotes(app_name, escaped_string));
printf(" \"app_icon\": \"%s\",", escape_quotes(app_icon, escaped_string));
printf(" \"replaces_id\": %u, \"timeout\": %d,", replaces_id, timeout);
printf(" \"summary\": \"%s\",", escape_quotes(summary, escaped_string));
printf(" \"body\": \"%s\" }\n", escape_quotes(body, escaped_string));
-
- free(escaped_string);
-
+
#else
-
+
printf("%s%s%s%s%u%s%d%s%s%s%s\n",
app_name, OUTPUT_DELIMITER,
app_icon, OUTPUT_DELIMITER,
replaces_id, OUTPUT_DELIMITER,
timeout, OUTPUT_DELIMITER,
summary, OUTPUT_DELIMITER, body);
-
+
#endif
fflush(stdout);
+ free(escaped_string);
}