aboutsummaryrefslogtreecommitdiff
path: root/src-old/stylesheet.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.me.uk>2019-02-19 18:17:56 +0100
committerThomas White <taw@bitwiz.me.uk>2019-02-19 18:17:56 +0100
commit24c20239779d0ec616adde651c594c7bf08d58c7 (patch)
treec1900bda6b2c5f58a66d4b8d89f4a946a24465dc /src-old/stylesheet.c
parent9fbe4d66565e6ab11c5022bcfe5ed0ddfb91dc7e (diff)
WIP
Diffstat (limited to 'src-old/stylesheet.c')
-rw-r--r--src-old/stylesheet.c288
1 files changed, 288 insertions, 0 deletions
diff --git a/src-old/stylesheet.c b/src-old/stylesheet.c
new file mode 100644
index 0000000..a6e2531
--- /dev/null
+++ b/src-old/stylesheet.c
@@ -0,0 +1,288 @@
+/*
+ * stylesheet.c
+ *
+ * Copyright © 2013-2018 Thomas White <taw@bitwiz.org.uk>
+ *
+ * This file is part of Colloquium.
+ *
+ * Colloquium is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <json-glib/json-glib.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <gio/gio.h>
+#include <gdk/gdk.h>
+
+#include "stylesheet.h"
+#include "utils.h"
+
+
+struct _stylesheet {
+ JsonNode *root;
+};
+
+
+static int find_comma(const char *a)
+{
+ int i = 0;
+ int in_brackets = 0;
+ size_t len = strlen(a);
+
+ do {
+ if ( (a[i] == ',') && !in_brackets ) return i;
+ if ( a[i] == '(' ) in_brackets++;
+ if ( a[i] == ')' ) in_brackets--;
+ i++;
+ } while ( i < len );
+ return 0;
+}
+
+
+int parse_colour_duo(const char *a, GdkRGBA *col1, GdkRGBA *col2)
+{
+ char *acopy;
+ int cpos;
+
+ acopy = strdup(a);
+ if ( acopy == NULL ) return 1;
+
+ cpos = find_comma(acopy);
+ if ( cpos == 0 ) {
+ fprintf(stderr, _("Invalid background gradient '%s'\n"), a);
+ return 1;
+ }
+
+ acopy[cpos] = '\0';
+
+ if ( gdk_rgba_parse(col1, acopy) != TRUE ) {
+ fprintf(stderr, _("Failed to parse colour: %s\n"), acopy);
+ }
+ if ( gdk_rgba_parse(col2, &acopy[cpos+1]) != TRUE ) {
+ fprintf(stderr, _("Failed to parse colour: %s\n"), &acopy[cpos+1]);
+ }
+
+ free(acopy);
+ return 0;
+}
+
+
+Stylesheet *stylesheet_load(GFile *file)
+{
+ JsonParser *parser;
+ gboolean r;
+ GError *err = NULL;
+ Stylesheet *ss;
+ char *everything;
+ gsize len;
+
+ printf("Trying stylesheet '%s'\n", g_file_get_uri(file));
+
+ ss = calloc(1, sizeof(Stylesheet));
+ if ( ss == NULL ) return NULL;
+
+ parser = json_parser_new();
+
+ if ( !g_file_load_contents(file, NULL, &everything, &len, NULL, NULL) ) {
+ fprintf(stderr, _("Failed to load stylesheet '%s'\n"),
+ g_file_get_uri(file));
+ free(ss);
+ return NULL;
+ }
+
+ r = json_parser_load_from_data(parser, everything, len, &err);
+ if ( r == FALSE ) {
+ fprintf(stderr, _("Failed to load stylesheet: %s\n"),
+ err->message);
+ free(ss);
+ return NULL;
+ }
+
+ ss->root = json_parser_steal_root(parser);
+ g_object_unref(parser);
+
+ return ss;
+}
+
+
+static JsonObject *find_stylesheet_object(Stylesheet *ss, const char *path,
+ JsonNode **freeme)
+{
+ JsonNode *node;
+ JsonObject *obj;
+ JsonArray *array;
+ GError *err = NULL;
+
+ node = json_path_query(path, ss->root, &err);
+ array = json_node_get_array(node);
+
+ if ( json_array_get_length(array) == 0 ) {
+ json_node_unref(node);
+ return NULL;
+ }
+
+ if ( json_array_get_length(array) > 1 ) {
+ json_node_unref(node);
+ fprintf(stderr, "More than one result in SS lookup (%s)!\n", path);
+ return NULL;
+ }
+
+ obj = json_array_get_object_element(array, 0);
+ if ( obj == NULL ) {
+ printf("%s not a JSON object\n", path);
+ json_node_unref(node);
+ return NULL;
+ }
+
+ *freeme = node;
+ return obj;
+}
+
+
+char *stylesheet_lookup(Stylesheet *ss, const char *path, const char *key)
+{
+ JsonObject *obj;
+ char *ret = NULL;
+ JsonNode *node = NULL;
+
+ if ( ss == NULL ) {
+ fprintf(stderr, "No stylesheet!\n");
+ return NULL;
+ }
+
+ obj = find_stylesheet_object(ss, path, &node);
+ if ( obj == NULL ) return NULL;
+
+ if ( json_object_has_member(obj, key) ) {
+
+ const gchar *v;
+ v = json_object_get_string_member(obj, key);
+ if ( v != NULL ) {
+ ret = strdup(v);
+ } else {
+ fprintf(stderr, "Error retrieving %s.%s\n", path, key);
+ }
+
+ } /* else not found, too bad */
+
+ if ( node != NULL ) json_node_unref(node);
+ return ret;
+}
+
+
+int stylesheet_set(Stylesheet *ss, const char *path, const char *key,
+ const char *new_val)
+{
+ JsonObject *obj;
+ JsonNode *node = NULL;
+ int r = 1;
+
+ if ( ss == NULL ) {
+ fprintf(stderr, "No stylesheet!\n");
+ return 1;
+ }
+
+ obj = find_stylesheet_object(ss, path, &node);
+ if ( obj != NULL ) {
+ json_object_set_string_member(obj, key, new_val);
+ r = 0;
+ } /* else most likely the object (e.g. "$.slide", "$.slide.frame",
+ * "$.narrative" etc doesn't exist */
+
+ if ( node != NULL ) json_node_unref(node);
+ return r;
+}
+
+
+int stylesheet_delete(Stylesheet *ss, const char *path, const char *key)
+{
+ JsonObject *obj;
+ JsonNode *node = NULL;
+ int r = 1;
+
+ if ( ss == NULL ) {
+ fprintf(stderr, "No stylesheet!\n");
+ return 1;
+ }
+
+ obj = find_stylesheet_object(ss, path, &node);
+ if ( obj != NULL ) {
+ json_object_remove_member(obj, key);
+ r = 0;
+ } /* else most likely the object (e.g. "$.slide", "$.slide.frame",
+ * "$.narrative" etc doesn't exist */
+
+ if ( node != NULL ) json_node_unref(node);
+ return r;
+}
+
+
+void stylesheet_free(Stylesheet *ss)
+{
+ json_node_unref(ss->root);
+ free(ss);
+}
+
+
+int stylesheet_save(Stylesheet *ss, GFile *file)
+{
+ JsonGenerator *gen;
+ GError *error = NULL;
+ GFileOutputStream *fh;
+
+ fh = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
+ if ( fh == NULL ) {
+ fprintf(stderr, _("Open failed: %s\n"), error->message);
+ return 1;
+ }
+
+ gen = json_generator_new();
+ json_generator_set_root(gen, ss->root);
+ json_generator_set_pretty(gen, TRUE);
+ json_generator_set_indent(gen, 1);
+ json_generator_set_indent_char(gen, '\t');
+ if ( !json_generator_to_stream(gen, G_OUTPUT_STREAM(fh), NULL, &error) ) {
+ fprintf(stderr, _("Open failed: %s\n"), error->message);
+ return 1;
+ }
+ g_object_unref(fh);
+ return 0;
+}
+
+
+char *stylesheet_data(Stylesheet *ss)
+{
+ return json_to_string(ss->root, FALSE);
+}
+
+
+void stylesheet_set_data(Stylesheet *ss, const char *data)
+{
+ JsonNode *new_root;
+ GError *err = NULL;
+ new_root = json_from_string(data, &err);
+ if ( new_root == NULL ) {
+ fprintf(stderr, "Internal parse error: %s\n", err->message);
+ return;
+ }
+ json_node_unref(ss->root);
+ ss->root = new_root;
+}