summaryrefslogtreecommitdiff
path: root/frontends/gtk/resources.c
blob: 0f0d180e05014709eccad134b2ae841dbaf043fe (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
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
/*
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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/>.
 */

/**
 * \file
 * Implementation of gtk builtin resource handling.
 *
 * This presents a unified interface to the rest of the codebase to
 * obtain resources. Note this is not anything to do with the resource
 * scheme handling beyond possibly providing the underlying data.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

#include "utils/log.h"
#include "utils/filepath.h"

#include "gtk/compat.h"
#include "gtk/resources.h"

/** log contents of gresource /org/netsource */
#ifdef WITH_GRESOURCE
#define SHOW_GRESOURCE
#undef SHOW_GRESOURCE
#endif

#ifdef WITH_BUILTIN_PIXBUF
#ifdef __GNUC__
extern const guint8 menu_cursor_pixdata[] __attribute__ ((__aligned__ (4)));
extern const guint8 favicon_pixdata[] __attribute__ ((__aligned__ (4)));
extern const guint8 netsurf_pixdata[] __attribute__ ((__aligned__ (4)));
#else
extern const guint8 menu_cursor_pixdata[];
extern const guint8 favicon_pixdata[];
extern const guint8 netsurf_pixdata[];
#endif
#endif

/** type of resource entry */
enum nsgtk_resource_type_e {
	NSGTK_RESOURCE_FILE, /**< entry is a file on disc */
	NSGTK_RESOURCE_GLIB, /**< entry is a gresource accessed by path */
	NSGTK_RESOURCE_DIRECT, /**< entry is a gresource accesed by gbytes */
	NSGTK_RESOURCE_INLINE, /**< entry is compiled in accessed by pointer */
};

/** resource entry */
struct nsgtk_resource_s {
	const char *name;
	unsigned int len;
	enum nsgtk_resource_type_e type;
	char *path;
};

#define RES_ENTRY(name) { name, sizeof((name)) - 1, NSGTK_RESOURCE_FILE, NULL }

/** resources that are used for gtk builder */
static struct nsgtk_resource_s ui_resource[] = {
	RES_ENTRY("netsurf"),
	RES_ENTRY("tabcontents"),
	RES_ENTRY("password"),
	RES_ENTRY("login"),
	RES_ENTRY("ssl"),
	RES_ENTRY("toolbar"),
	RES_ENTRY("downloads"),
	RES_ENTRY("globalhistory"),
	RES_ENTRY("localhistory"),
	RES_ENTRY("options"),
	RES_ENTRY("hotlist"),
	RES_ENTRY("cookies"),
	RES_ENTRY("viewdata"),
	RES_ENTRY("warning"),
	{ NULL, 0, NSGTK_RESOURCE_FILE, NULL },
};

/** resources that are used as pixbufs */
static struct nsgtk_resource_s pixbuf_resource[] = {
	RES_ENTRY("favicon.png"),
	RES_ENTRY("netsurf.xpm"),
	RES_ENTRY("menu_cursor.png"),
	RES_ENTRY("arrow_down_8x32.png"),
	RES_ENTRY("throbber/throbber0.png"),
	RES_ENTRY("throbber/throbber1.png"),
	RES_ENTRY("throbber/throbber2.png"),
	RES_ENTRY("throbber/throbber3.png"),
	RES_ENTRY("throbber/throbber4.png"),
	RES_ENTRY("throbber/throbber5.png"),
	RES_ENTRY("throbber/throbber6.png"),
	RES_ENTRY("throbber/throbber7.png"),
	RES_ENTRY("throbber/throbber8.png"),
	{ NULL, 0, NSGTK_RESOURCE_FILE, NULL },
};

/** resources that are used for direct data access */
static struct nsgtk_resource_s direct_resource[] = {
	RES_ENTRY("welcome.html"),
	RES_ENTRY("credits.html"),
	RES_ENTRY("licence.html"),
	RES_ENTRY("maps.html"),
	RES_ENTRY("default.css"),
	RES_ENTRY("adblock.css"),
	RES_ENTRY("internal.css"),
	RES_ENTRY("quirks.css"),
	RES_ENTRY("netsurf.png"),
	RES_ENTRY("default.ico"),
	RES_ENTRY("icons/arrow-l.png"),
	RES_ENTRY("icons/content.png"),
	RES_ENTRY("icons/directory2.png"),
	RES_ENTRY("icons/directory.png"),
	RES_ENTRY("icons/hotlist-add.png"),
	RES_ENTRY("icons/hotlist-rmv.png"),
	RES_ENTRY("icons/search.png"),
	RES_ENTRY("languages"),
	RES_ENTRY("Messages"),
	{ NULL, 0, NSGTK_RESOURCE_FILE, NULL },
};


/* exported interface documented in gtk/resources.h */
GdkCursor *nsgtk_create_menu_cursor(void)
{
	GdkCursor *cursor = NULL;
	GdkPixbuf *pixbuf;
	nserror res;

	res = nsgdk_pixbuf_new_from_resname("menu_cursor.png", &pixbuf);
	if (res == NSERROR_OK) {
		cursor = gdk_cursor_new_from_pixbuf(gdk_display_get_default(),
						    pixbuf, 0, 3);
		g_object_unref(pixbuf);
	}

	return cursor;
}


/**
 * locate a resource
 *
 * The way GTK accesses resource files has changed greatly between
 * releases. This initilises the interface that hides all the
 * implementation details from the rest of the code.
 *
 * If the GResource is not enabled or the item cannot be found in the
 * compiled in resources the files will be loaded directly from disc
 * instead.
 *
 * \param respath A string vector containing the valid resource search paths
 * \param resource A resource entry to initialise
 */
static nserror
init_resource(char **respath, struct nsgtk_resource_s *resource)
{
	char *resname;
#ifdef WITH_GRESOURCE
	int resnamelen;
	gboolean present;
	const gchar * const *langv;
	int langc = 0;

	langv = g_get_language_names();

	while (langv[langc] != NULL) {
		resnamelen = snprintf(NULL, 0,
				      "/org/netsurf/%s/%s",
				      langv[langc], resource->name);

		resname = malloc(resnamelen + 1);
		if (resname == NULL) {
			return NSERROR_NOMEM;
		}
		snprintf(resname, resnamelen + 1,
			 "/org/netsurf/%s/%s",
			 langv[langc], resource->name);

		present = g_resources_get_info(resname,
					       G_RESOURCE_LOOKUP_FLAGS_NONE,
					       NULL, NULL, NULL);
		if (present == TRUE) {
			/* found an entry in the resources */
			resource->path = resname;
			resource->type = NSGTK_RESOURCE_GLIB;
			NSLOG(netsurf, INFO, "Found gresource path %s",
			      resource->path);
			return NSERROR_OK;
		}
		NSLOG(netsurf, DEEPDEBUG,
		      "gresource \"%s\" not found", resname);
		free(resname);

		langc++;
	}
	resnamelen = snprintf(NULL, 0, "/org/netsurf/%s", resource->name);

	resname = malloc(resnamelen + 1);
	if (resname == NULL) {
		return NSERROR_NOMEM;
	}
	snprintf(resname, resnamelen + 1, "/org/netsurf/%s", resource->name);

	present = g_resources_get_info(resname,
				       G_RESOURCE_LOOKUP_FLAGS_NONE,
				       NULL, NULL, NULL);
	if (present == TRUE) {
		/* found an entry in the resources */
		resource->path = resname;
		resource->type = NSGTK_RESOURCE_GLIB;
		NSLOG(netsurf, INFO, "Found gresource path %s",
		      resource->path);
		return NSERROR_OK;
	}
	NSLOG(netsurf, DEEPDEBUG, "gresource \"%s\" not found", resname);*/
	free(resname);

#endif

	resname = filepath_find(respath, resource->name);
	if (resname == NULL) {
		NSLOG(netsurf, INFO,
		      "Unable to find resource %s on resource path",
		      resource->name);
		return NSERROR_NOT_FOUND;
	}

	/* found an entry on the path */
	resource->path = resname;
	resource->type = NSGTK_RESOURCE_FILE;

	NSLOG(netsurf, INFO, "Found file resource path %s", resource->path);
	return NSERROR_OK;
}

/**
 * locate and setup a direct resource
 *
 * Direct resources have general type of NSGTK_RESOURCE_GLIB but have
 *  g_resources_lookup_data() applied and the result stored so the data
 *  can be directly accessed without additional processing.
 *
 * \param respath A string vector containing the valid resource search paths
 * \param resource A resource entry to initialise
 */
static nserror
init_direct_resource(char **respath, struct nsgtk_resource_s *resource)
{
	nserror res;

	res = init_resource(respath, resource);

#ifdef WITH_GRESOURCE
	if ((res == NSERROR_OK) &&
	    (resource->type == NSGTK_RESOURCE_GLIB)) {
		/* found gresource we can convert */
		GBytes *data;

		data = g_resources_lookup_data(resource->path,
					       G_RESOURCE_LOOKUP_FLAGS_NONE,
					       NULL);
		if (data != NULL) {
			resource->type = NSGTK_RESOURCE_DIRECT;
			resource->path = (char *)data;
		}
	}
#endif

	return res;
}

/**
 * locate a pixbuf resource
 *
 * Pixbuf resources can be compiled inline
 *
 * \param respath A string vector containing the valid resource search paths
 * \param resource A resource entry to initialise
 */
static nserror
init_pixbuf_resource(char **respath, struct nsgtk_resource_s *resource)
{
#ifdef WITH_BUILTIN_PIXBUF
	if (strncmp(resource->name, "menu_cursor.png", resource->len) == 0) {
		resource->path = (char *)&menu_cursor_pixdata[0];
		resource->type = NSGTK_RESOURCE_INLINE;
		NSLOG(netsurf, INFO, "Found builtin for %s", resource->name);
		return NSERROR_OK;
	}

	if (strncmp(resource->name, "netsurf.xpm", resource->len) == 0) {
		resource->path = (char *)&netsurf_pixdata[0];
		resource->type = NSGTK_RESOURCE_INLINE;
		NSLOG(netsurf, INFO, "Found builtin for %s", resource->name);
		return NSERROR_OK;
	}

	if (strncmp(resource->name, "favicon.png", resource->len) == 0) {
		resource->path = (char *)&favicon_pixdata[0];
		resource->type = NSGTK_RESOURCE_INLINE;
		NSLOG(netsurf, INFO, "Found builtin for %s", resource->name);
		return NSERROR_OK;
	}
#endif
	return init_resource(respath, resource);
}

/**
 * locate a ui resource
 *
 * UI resources need their resource name changing to account for gtk versions
 *
 * \param respath A string vector containing the valid resource search paths
 * \param ui_res A resource entry to initialise
 */
static nserror init_ui_resource(char **respath, struct nsgtk_resource_s *ui_res)
{
#if GTK_CHECK_VERSION(3,0,0)
	int gtkv = 3;
#else
	int gtkv = 2;
#endif
	int resnamelen;
	char *resname;
	struct nsgtk_resource_s resource;
	nserror res;

	resnamelen = ui_res->len + 10; /* allow for the expanded ui name */

	resname = malloc(resnamelen);
	if (resname == NULL) {
		return NSERROR_NOMEM;
	}
	snprintf(resname, resnamelen, "%s.gtk%d.ui", ui_res->name, gtkv);
	resource.name = resname;
	resource.len = ui_res->len;
	resource.path = NULL;

	res = init_resource(respath, &resource);

	ui_res->path = resource.path;
	ui_res->type = resource.type;

	free(resname);

	return res;
}

/**
 * Find a resource entry by name.
 *
 * \param resname The resource name to match.
 * \param resource The list of resources entries to search.
 */
static struct nsgtk_resource_s *
find_resource_from_name(const char *resname, struct nsgtk_resource_s *resource)
{
	/* find resource from name */
	while ((resource->name != NULL) &&
	       ((resname[0] != resource->name[0]) ||
		(strncmp(resource->name, resname, resource->len) != 0))) {
		resource++;
	}
	return resource;
}

#ifdef SHOW_GRESOURCE
/**
 * Debug dump of all resources compile din via GResource.
 */
static void list_gresource(void)
{
	const char *nspath = "/org/netsurf";
	char **reslist;
	char **cur;
	GError* gerror = NULL;
	reslist = g_resources_enumerate_children(nspath,
						 G_RESOURCE_LOOKUP_FLAGS_NONE,
						 &gerror);
	if (gerror) {
		NSLOG(netsurf, INFO, "gerror %s", gerror->message);
		g_error_free(gerror);

	} else {
		cur = reslist;
		while (cur != NULL && *cur != NULL) {
			NSLOG(netsurf, INFO, "gres %s", *cur);
			cur++;
		}
		g_strfreev(reslist);
	}
}
#endif

/**
 * Initialise UI resource table
 *
 */
/* exported interface documented in gtk/resources.h */
nserror nsgtk_init_resources(char **respath)
{
	struct nsgtk_resource_s *resource;
	nserror res;

#ifdef SHOW_GRESOURCE
	list_gresource();
#endif

	/* iterate the ui resource table and initialise all its members */
	resource = &ui_resource[0];
	while (resource->name != NULL) {
		res = init_ui_resource(respath, resource);
		if (res != NSERROR_OK) {
			return res;
		}
		resource++;
	}

	/* iterate the pixbuf resource table and initialise all its members */
	resource = &pixbuf_resource[0];
	while (resource->name != NULL) {
		res = init_pixbuf_resource(respath, resource);
		if (res != NSERROR_OK) {
			return res;
		}
		resource++;
	}

	/* iterate the direct resource table and initialise all its members */
	resource = &direct_resource[0];
	while (resource->name != NULL) {
		res = init_direct_resource(respath, resource);
		if (res != NSERROR_OK) {
			return res;
		}
		resource++;
	}

	return NSERROR_OK;
}


/* exported interface documented in gtk/resources.h */
nserror
nsgdk_pixbuf_new_from_resname(const char *resname, GdkPixbuf **pixbuf_out)
{
	struct nsgtk_resource_s *resource;
	GdkPixbuf *new_pixbuf = NULL;
	GError* error = NULL;

	resource = find_resource_from_name(resname, &pixbuf_resource[0]);
	if (resource->name == NULL) {
		return NSERROR_NOT_FOUND;
	}

	switch (resource->type) {
	case NSGTK_RESOURCE_FILE:
		new_pixbuf = gdk_pixbuf_new_from_file(resource->path, &error);
		break;

	case NSGTK_RESOURCE_GLIB:
#ifdef WITH_GRESOURCE
		new_pixbuf = gdk_pixbuf_new_from_resource(resource->path, &error);
#endif
		break;

	case NSGTK_RESOURCE_INLINE:
#ifdef WITH_BUILTIN_PIXBUF
		new_pixbuf = gdk_pixbuf_new_from_inline(-1, (const guint8 *)resource->path, FALSE, &error);
#endif
		break;

	case NSGTK_RESOURCE_DIRECT:
		/* pixbuf resources are not currently direct */
		break;
	}

	if (new_pixbuf == NULL) {
		if (error != NULL) {
			NSLOG(netsurf, INFO,
			      "Unable to create pixbuf from file for %s with path %s \"%s\"",
			      resource->name,
			      resource->path,
			      error->message);
			g_error_free(error);
		} else {
			NSLOG(netsurf, INFO,
			      "Unable to create pixbuf from file for %s with path %s",
			      resource->name,
			      resource->path);
		}
		return NSERROR_INIT_FAILED;
	}
	*pixbuf_out = new_pixbuf;

	return NSERROR_OK;
}

/* exported interface documented in gtk/resources.h */
nserror
nsgtk_builder_new_from_resname(const char *resname, GtkBuilder **builder_out)
{
	GtkBuilder *new_builder;
	struct nsgtk_resource_s *ui_res;
	GError* error = NULL;

	ui_res = find_resource_from_name(resname, &ui_resource[0]);
	if (ui_res->name == NULL) {
		return NSERROR_NOT_FOUND;
	}

	new_builder = gtk_builder_new();

	if (ui_res->type == NSGTK_RESOURCE_FILE) {
		if (!gtk_builder_add_from_file(new_builder,
					       ui_res->path,
					       &error)) {
			NSLOG(netsurf, INFO,
			      "Unable to add UI builder from file for %s with path %s \"%s\"",
			      ui_res->name,
			      ui_res->path,
			      error->message);
			g_error_free(error);
			g_object_unref(G_OBJECT(new_builder));
			return NSERROR_INIT_FAILED;
		}
	} else {
		if (!nsgtk_builder_add_from_resource(new_builder,
						     ui_res->path,
						     &error)) {
			NSLOG(netsurf, INFO,
			      "Unable to add UI builder from resource for %s with path %s \"%s\"",
			      ui_res->name,
			      ui_res->path,
			      error->message);
			g_error_free(error);
			g_object_unref(G_OBJECT(new_builder));
			return NSERROR_INIT_FAILED;
		}
	}

	*builder_out = new_builder;

	return NSERROR_OK;
}

/* exported interface documented in gtk/resources.h */
nserror
nsgtk_data_from_resname(const char *resname,
			const uint8_t ** data_out,
			size_t *data_size_out)
{
#ifdef WITH_GRESOURCE
	struct nsgtk_resource_s *resource;
	GBytes *data;
	const gchar *buffer;
	gsize buffer_length;

	resource = find_resource_from_name(resname, &direct_resource[0]);
	if ((resource->name == NULL) ||
	    (resource->type != NSGTK_RESOURCE_DIRECT)) {
		return NSERROR_NOT_FOUND;
	}

	data = (GBytes *)resource->path;

	buffer_length = 0;
	buffer = g_bytes_get_data(data, &buffer_length);

	if (buffer == NULL) {
		return NSERROR_NOMEM;
	}

	*data_out = (const uint8_t *)buffer;
	*data_size_out = (size_t)buffer_length;

	return NSERROR_OK;
#else
	/** \todo consider adding compiled inline resources for things
	 * other than pixbufs.
	 */
	return NSERROR_NOT_FOUND;
#endif
}

/* exported interface documented in gtk/resources.h */
nserror
nsgtk_path_from_resname(const char *resname, const char **path_out)
{
	struct nsgtk_resource_s *resource;

	resource = find_resource_from_name(resname, &direct_resource[0]);
	if ((resource->name == NULL) ||
	    (resource->type != NSGTK_RESOURCE_FILE)) {
		return NSERROR_NOT_FOUND;
	}

	*path_out = (const char *)resource->path;

	return NSERROR_OK;
}