aboutsummaryrefslogtreecommitdiff
path: root/src/stylesheet.c
blob: 96172a4dee4871646d3228f446c0a06c4f94ea54 (plain)
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
/*
 * 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 bg gradient spec '%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));
		return NULL;
	}

	r = json_parser_load_from_data(parser, everything, len, &err);
	if ( r == FALSE ) {
		fprintf(stderr, "Failed to load style sheet: '%s'\n", err->message);
		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) != 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 ( 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)
{
	g_object_unref(ss->root);
	free(ss);
}