summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-app-builder.c
blob: 6ad0d9a6045f58726de98c7edbf9c5f03da0f785 (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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:as-app-builder
 * @short_description: Scan the filesystem for installed languages
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * This object will parse a gettext catalog directory and calculate the
 * language stats for an application.
 *
 * See also: #AsApp
 */

#include "config.h"

#include <string.h>

#include "as-app-builder.h"

typedef struct {
	gchar		*locale;
	guint		 nstrings;
	guint		 percentage;
} AsAppBuilderEntry;

typedef struct {
	guint		 max_nstrings;
	GList		*data;
	GPtrArray	*translations;		/* no ref */
} AsAppBuilderContext;

static AsAppBuilderEntry *
as_app_builder_entry_new (void)
{
	AsAppBuilderEntry *entry;
	entry = g_slice_new0 (AsAppBuilderEntry);
	return entry;
}

static void
as_app_builder_entry_free (AsAppBuilderEntry *entry)
{
	g_free (entry->locale);
	g_slice_free (AsAppBuilderEntry, entry);
}

static AsAppBuilderContext *
as_app_builder_ctx_new (void)
{
	AsAppBuilderContext *ctx;
	ctx = g_new0 (AsAppBuilderContext, 1);
	return ctx;
}

static void
as_app_builder_ctx_free (AsAppBuilderContext *ctx)
{
	g_list_free_full (ctx->data, (GDestroyNotify) as_app_builder_entry_free);
	g_free (ctx);
}

typedef struct {
	guint32		 magic;
	guint32		 revision;
	guint32		 nstrings;
	guint32		 orig_tab_offset;
	guint32		 trans_tab_offset;
	guint32		 hash_tab_size;
	guint32		 hash_tab_offset;
	guint32		 n_sysdep_segments;
	guint32		 sysdep_segments_offset;
	guint32		 n_sysdep_strings;
	guint32		 orig_sysdep_tab_offset;
	guint32		 trans_sysdep_tab_offset;
} AsAppBuilderGettextHeader;

static void
as_app_builder_add_entry (AsAppBuilderContext *ctx, AsAppBuilderEntry *entry)
{
	if (entry->nstrings > ctx->max_nstrings)
		ctx->max_nstrings = entry->nstrings;
	ctx->data = g_list_prepend (ctx->data, entry);
}

static gboolean
as_app_builder_parse_file_gettext (AsAppBuilderContext *ctx,
				   const gchar *locale,
				   const gchar *filename,
				   GError **error)
{
	AsAppBuilderEntry *entry;
	AsAppBuilderGettextHeader h;
	g_autofree gchar *data = NULL;
	gboolean swapped;

	/* read data */
	if (!g_file_get_contents (filename, &data, NULL, error))
		return FALSE;

	/* we only strictly need the header */
	memcpy (&h, data, sizeof (AsAppBuilderGettextHeader));
	if (h.magic == 0x950412de)
		swapped = FALSE;
	else if (h.magic == 0xde120495)
		swapped = TRUE;
	else {
		g_set_error_literal (error,
				     AS_APP_ERROR,
				     AS_APP_ERROR_FAILED,
				     "file is invalid");
		return FALSE;
	}
	entry = as_app_builder_entry_new ();
	entry->locale = g_strdup (locale);
	if (swapped)
		entry->nstrings = GUINT32_SWAP_LE_BE (h.nstrings);
	else
		entry->nstrings = h.nstrings;
	as_app_builder_add_entry (ctx, entry);
	return TRUE;
}

static gboolean
as_app_builder_search_locale_gettext (AsAppBuilderContext *ctx,
				      const gchar *locale,
				      const gchar *messages_path,
				      AsAppBuilderFlags flags,
				      GError **error)
{
	const gchar *filename;
	gboolean found_anything = FALSE;
	guint i;
	g_autoptr(GDir) dir = NULL;
	g_autoptr(GPtrArray) mo_paths = NULL;

	/* list files */
	dir = g_dir_open (messages_path, 0, error);
	if (dir == NULL)
		return FALSE;

	/* do a first pass at this, trying to find the preferred .mo */
	mo_paths = g_ptr_array_new_with_free_func (g_free);
	while ((filename = g_dir_read_name (dir)) != NULL) {
		g_autofree gchar *path = NULL;
		path = g_build_filename (messages_path, filename, NULL);
		if (!g_file_test (path, G_FILE_TEST_EXISTS))
			continue;
		for (i = 0; i < ctx->translations->len; i++) {
			AsTranslation *t = g_ptr_array_index (ctx->translations, i);
			g_autofree gchar *fn = NULL;
			if (as_translation_get_kind (t) != AS_TRANSLATION_KIND_GETTEXT &&
			    as_translation_get_kind (t) != AS_TRANSLATION_KIND_UNKNOWN)
				continue;
			fn = g_strdup_printf ("%s.mo", as_translation_get_id (t));
			if (g_strcmp0 (filename, fn) == 0) {
				if (!as_app_builder_parse_file_gettext (ctx,
									locale,
									path,
									error))
					return FALSE;
				found_anything = TRUE;
			}
		}
		g_ptr_array_add (mo_paths, g_strdup (path));
	}

	/* we got data from one or more of the translations */
	if (found_anything == TRUE)
		return TRUE;

	/* fall back to parsing *everything*, which might give us more
	 * language results than is actually true */
	if (flags & AS_APP_BUILDER_FLAG_USE_FALLBACKS) {
		for (i = 0; i < mo_paths->len; i++) {
			filename = g_ptr_array_index (mo_paths, i);
			if (!as_app_builder_parse_file_gettext (ctx,
								locale,
								filename,
								error))
				return FALSE;
		}
	}

	return TRUE;
}

typedef enum {
	AS_APP_TRANSLATION_QM_TAG_END		= 0x1,
	/* SourceText16 */
	AS_APP_TRANSLATION_QM_TAG_TRANSLATION	= 0x3,
	/* Context16 */
	AS_APP_TRANSLATION_QM_TAG_OBSOLETE1	= 0x5,
	AS_APP_TRANSLATION_QM_TAG_SOURCE_TEXT	= 0x6,
	AS_APP_TRANSLATION_QM_TAG_CONTEXT	= 0x7,
	AS_APP_TRANSLATION_QM_TAG_COMMENT	= 0x8,
	/* Obsolete2 */
	AS_APP_TRANSLATION_QM_TAG_LAST
} AsAppBuilderQmTag;

typedef enum {
	AS_APP_TRANSLATION_QM_SECTION_CONTEXTS	= 0x2f,
	AS_APP_TRANSLATION_QM_SECTION_HASHES	= 0x42,
	AS_APP_TRANSLATION_QM_SECTION_MESSAGES	= 0x69,
	AS_APP_TRANSLATION_QM_SECTION_NUMERUS	= 0x88,
	AS_APP_TRANSLATION_QM_SECTION_DEPS	= 0x96,
	AS_APP_TRANSLATION_QM_SECTION_LAST
} AsAppBuilderQmSection;

static guint8
_read_uint8 (const guint8 *data, guint32 *offset)
{
	guint8 tmp;
	tmp = data[*offset];
	(*offset) += 1;
	return tmp;
}

static guint32
_read_uint32 (const guint8 *data, guint32 *offset)
{
	guint32 tmp = 0;
	memcpy (&tmp, data + *offset, 4);
	(*offset) += 4;
	return GUINT32_FROM_BE (tmp);
}

static void
as_app_builder_parse_data_qt (AsAppBuilderContext *ctx,
			      const gchar *locale,
			      const guint8 *data,
			      guint32 len)
{
	AsAppBuilderEntry *entry;
	guint32 m = 0;
	guint nstrings = 0;

	/* read data */
	while (m < len) {
		guint32 tag_len;
		AsAppBuilderQmTag tag = _read_uint8(data, &m);
		switch (tag) {
		case AS_APP_TRANSLATION_QM_TAG_END:
			break;
		case AS_APP_TRANSLATION_QM_TAG_OBSOLETE1:
			m += 4;
			break;
		case AS_APP_TRANSLATION_QM_TAG_TRANSLATION:
		case AS_APP_TRANSLATION_QM_TAG_SOURCE_TEXT:
		case AS_APP_TRANSLATION_QM_TAG_CONTEXT:
		case AS_APP_TRANSLATION_QM_TAG_COMMENT:
			tag_len = _read_uint32 (data, &m);
			if (tag_len < 0xffffffff)
				m += tag_len;
			if (tag == AS_APP_TRANSLATION_QM_TAG_TRANSLATION)
				nstrings++;
			break;
		default:
			m = G_MAXUINT32;
			break;
		}
	}

	/* add new entry */
	entry = as_app_builder_entry_new ();
	entry->locale = g_strdup (locale);
	entry->nstrings = nstrings;
	as_app_builder_add_entry (ctx, entry);
}

static gboolean
as_app_builder_parse_file_qt (AsAppBuilderContext *ctx,
			      const gchar *locale,
			      const gchar *filename,
			      GError **error)
{
	gsize len;
	guint32 m = 0;
	g_autofree guint8 *data = NULL;
	const guint8 qm_magic[] = {
		0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95,
		0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd
	};

	/* load file */
	if (!g_file_get_contents (filename, (gchar **) &data, &len, error))
		return FALSE;

	/* check header */
	if (len < sizeof(qm_magic) ||
	    memcmp (data, qm_magic, sizeof(qm_magic)) != 0) {
		g_set_error_literal (error,
				     AS_APP_ERROR,
				     AS_APP_ERROR_FAILED,
				     "file is invalid");
		return FALSE;
	}
	m += (guint32) sizeof(qm_magic);

	/* parse each section */
	while (m < len) {
		AsAppBuilderQmSection section = _read_uint8(data, &m);
		guint32 section_len = _read_uint32 (data, &m);
		if (section_len > len - m) {
			g_set_error_literal (error,
					     AS_APP_ERROR,
					     AS_APP_ERROR_FAILED,
					     "file is invalid, section too large");
			return FALSE;
		}
		if (section == AS_APP_TRANSLATION_QM_SECTION_MESSAGES) {
			as_app_builder_parse_data_qt (ctx,
						      locale,
						      data + m,
						      section_len);
		}
		m += section_len;
	}

	return TRUE;
}

static gboolean
as_app_builder_search_translations_qt (AsAppBuilderContext *ctx,
				       const gchar *prefix,
				       AsAppBuilderFlags flags,
				       GError **error)
{
	guint i;

	/* search for each translation ID */
	for (i = 0; i < ctx->translations->len; i++) {
		AsTranslation *t;
		const gchar *filename;
		const gchar *install_dir;
		g_autofree gchar *path = NULL;
		g_autoptr(GDir) dir = NULL;

		/* FIXME: this path probably has to be specified as an attribute
		 * in the <translations> tag from the AppData file */
		t = g_ptr_array_index (ctx->translations, i);
		if (as_translation_get_kind (t) != AS_TRANSLATION_KIND_QT &&
		    as_translation_get_kind (t) != AS_TRANSLATION_KIND_UNKNOWN)
			continue;
		if (as_translation_get_id (t) == NULL)
			continue;
		install_dir = as_translation_get_id (t);
		path = g_build_filename (prefix,
					 "share",
					 install_dir,
					 "translations",
					 NULL);
		if (!g_file_test (path, G_FILE_TEST_EXISTS))
			return TRUE;
		dir = g_dir_open (path, 0, error);
		if (dir == NULL)
			return FALSE;

		/* the format is ${prefix}/share/${install_dir}/translations/${id}_${locale}.qm */
		while ((filename = g_dir_read_name (dir)) != NULL) {
			g_autofree gchar *fn = NULL;
			g_autofree gchar *locale = NULL;
			if (!g_str_has_prefix (filename, as_translation_get_id (t)))
				continue;
			locale = g_strdup (filename + strlen (as_translation_get_id (t)) + 1);
			g_strdelimit (locale, ".", '\0');
			fn = g_build_filename (path, filename, NULL);
			if (!as_app_builder_parse_file_qt (ctx, locale, fn, error))
				return FALSE;
		}
	}

	return TRUE;
}

static gboolean
as_app_builder_search_translations_gettext (AsAppBuilderContext *ctx,
					    const gchar *prefix,
					    AsAppBuilderFlags flags,
					    GError **error)
{
	const gchar *locale;
	g_autofree gchar *path = NULL;
	g_autoptr(GDir) dir = NULL;

	path = g_build_filename (prefix, "share", "locale", NULL);
	if (!g_file_test (path, G_FILE_TEST_EXISTS))
		return TRUE;
	dir = g_dir_open (path, 0, error);
	if (dir == NULL)
		return FALSE;
	while ((locale = g_dir_read_name (dir)) != NULL) {
		g_autofree gchar *fn = NULL;
		fn = g_build_filename (path, locale, "LC_MESSAGES", NULL);
		if (!g_file_test (fn, G_FILE_TEST_EXISTS))
			continue;
		if (!as_app_builder_search_locale_gettext (ctx, locale, fn, flags, error))
			return FALSE;
	}
	return TRUE;
}

static gchar *
as_app_builder_get_locale_from_pak_fn (const gchar *basename)
{
	gchar *locale;
	gchar *str;

	/* FIXME: convert using a lookup table where required */
	locale = g_strdup (basename);
	str = g_strrstr (locale, ".");
	if (str != NULL)
		*str = '\0';
	g_strdelimit (locale, "-", '_');
	return locale;
}

static gboolean
as_app_builder_parse_file_pak (AsAppBuilderContext *ctx,
			       const gchar *locale,
			       const gchar *filename,
			       GError **error)
{
	AsAppBuilderEntry *entry;
	gsize len = 0;
	guint32 nr_resources;
	guint32 version_number;
	guint8 encoding;
	g_autofree gchar *data = NULL;

	if (!g_file_get_contents (filename, &data, &len, error))
		return FALSE;
	if (len < 9) {
		g_set_error (error,
			     AS_APP_ERROR,
			     AS_APP_ERROR_FAILED,
			     "file invalid, %" G_GSIZE_FORMAT "b in size",
			     len);
		return FALSE;
	}

	/* get 4 byte version number */
	memcpy (&version_number, data + 0, 4);
	if (version_number != 4) {
		g_set_error (error,
			     AS_APP_ERROR,
			     AS_APP_ERROR_FAILED,
			     "version number invalid, "
			     "got %" G_GUINT32_FORMAT " expected 4",
			     version_number);
		return FALSE;
	}

	/* get 4 byte number of resources */
	memcpy (&nr_resources, data + 4, 4);
	if (nr_resources == 0) {
		g_set_error_literal (error,
				     AS_APP_ERROR,
				     AS_APP_ERROR_FAILED,
				     "no resources found");
		return FALSE;
	}

	/* get single byte of encoding */
	encoding = (guint8) data[8];
	if (encoding != 0 && encoding != 1) {
		g_set_error (error,
			     AS_APP_ERROR,
			     AS_APP_ERROR_FAILED,
			     "PAK encoding invalid, got %u expected 0 or 1",
			     encoding);
		return FALSE;
	}

	/* create entry without reading resources */
	entry = as_app_builder_entry_new ();
	entry->locale = g_strdup (locale);
	entry->nstrings = nr_resources;
	as_app_builder_add_entry (ctx, entry);
	return TRUE;
}

static gboolean
as_app_builder_search_translations_pak (AsAppBuilderContext *ctx,
					const gchar *prefix,
					AsAppBuilderFlags flags,
					GError **error)
{
	guint i;

	/* search for each translation ID */
	for (i = 0; i < ctx->translations->len; i++) {
		const gchar *tmp;
		g_autoptr(GDir) dir = NULL;
		AsTranslation *t = g_ptr_array_index (ctx->translations, i);
		const gchar *libdirs[] = { "lib64", "lib", NULL };

		/* required */
		if (as_translation_get_id (t) == NULL)
			continue;
		for (guint j = 0; libdirs[j] != NULL; j++) {
			g_autofree gchar *path = NULL;
			path = g_build_filename (prefix,
						 libdirs[j],
						 as_translation_get_id (t),
						 "locales",
						 NULL);
			if (!g_file_test (path, G_FILE_TEST_EXISTS))
				continue;
			dir = g_dir_open (path, 0, error);
			if (dir == NULL)
				return FALSE;

			/* parse file for sanity */
			while ((tmp = g_dir_read_name (dir)) != 0) {
				g_autofree gchar *locale = NULL;
				g_autofree gchar *fn = g_build_filename (path, tmp, NULL);
				locale = as_app_builder_get_locale_from_pak_fn (tmp);
				if (!as_app_builder_parse_file_pak (ctx, locale, fn, error))
					return FALSE;
			}
		}
	}
	return TRUE;
}

static gchar *
as_app_builder_get_locale_from_xpi_fn (const gchar *basename)
{
	gchar *locale;
	gchar *str;

	/* remove prefix */
	if (g_str_has_prefix (basename, "langpack-"))
		basename += 9;

	/* remove suffix */
	locale = g_strdup (basename);
	str = g_strrstr (locale, "@");
	if (str != NULL)
		*str = '\0';
	g_strdelimit (locale, "-", '_');
	return locale;
}

static gboolean
as_app_builder_parse_file_xpi (AsAppBuilderContext *ctx,
			       const gchar *locale,
			       const gchar *filename,
			       GError **error)
{
	AsAppBuilderEntry *entry = as_app_builder_entry_new ();
	entry->locale = g_strdup (locale);
	entry->nstrings = 100;	/* FIXME: parse info */
	as_app_builder_add_entry (ctx, entry);
	return TRUE;
}

static gboolean
as_app_builder_search_translations_xpi (AsAppBuilderContext *ctx,
					const gchar *prefix,
					AsAppBuilderFlags flags,
					GError **error)
{
	const gchar *tmp;
	g_autoptr(GDir) dir = NULL;
	const gchar *libdirs[] = { "lib64", "lib", NULL };

	/* list files */
	for (guint j = 0; libdirs[j] != NULL; j++) {
		g_autofree gchar *path = NULL;
		path = g_build_filename (prefix,
					 libdirs[j],
					 "firefox",
					 "langpacks",
					 NULL);
		if (!g_file_test (path, G_FILE_TEST_EXISTS))
			continue;
		dir = g_dir_open (path, 0, error);
		if (dir == NULL)
			return FALSE;

		/* parse file for sanity */
		while ((tmp = g_dir_read_name (dir)) != 0) {
			g_autofree gchar *locale = NULL;
			g_autofree gchar *fn = g_build_filename (path, tmp, NULL);
			if (g_file_test (fn, G_FILE_TEST_IS_SYMLINK))
				continue;
			locale = as_app_builder_get_locale_from_xpi_fn (tmp);
			if (!as_app_builder_parse_file_xpi (ctx, locale, fn, error))
				return FALSE;
		}
	}

	return TRUE;
}

static gint
as_app_builder_entry_sort_cb (gconstpointer a, gconstpointer b)
{
	return g_strcmp0 (((AsAppBuilderEntry *) a)->locale,
			  ((AsAppBuilderEntry *) b)->locale);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC(AsAppBuilderContext, as_app_builder_ctx_free)

/**
 * as_app_builder_search_translations:
 * @app: an #AsApp
 * @prefix: a prefix to search, e.g. "/usr"
 * @min_percentage: minimum percentage to add language
 * @flags: #AsAppBuilderFlags, e.g. %AS_APP_BUILDER_FLAG_USE_FALLBACKS
 * @cancellable: a #GCancellable or %NULL
 * @error: a #GError or %NULL
 *
 * Searches a prefix for languages, and using a heuristic adds <language>
 * tags to the specified application.
 *
 * If there are no #AsTranslation objects set on the #AsApp then all domains
 * are matched, which may include more languages than you intended to.
 *
 * @min_percentage sets the minimum percentage to add a language tag.
 * The usual value would be 25% and any language less complete than
 * this will not be added.
 *
 * The purpose of this functionality is to avoid blowing up the size
 * of the AppStream metadata with a lot of extra data detailing
 * languages with very few translated strings.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.5.8
 **/
gboolean
as_app_builder_search_translations (AsApp *app,
				    const gchar *prefix,
				    guint min_percentage,
				    AsAppBuilderFlags flags,
				    GCancellable *cancellable,
				    GError **error)
{
	AsAppBuilderEntry *e;
	GList *l;
	g_autoptr(AsAppBuilderContext) ctx = NULL;

	ctx = as_app_builder_ctx_new ();
	ctx->translations = as_app_get_translations (app);

	/* search for QT .qm files */
	if (!as_app_builder_search_translations_qt (ctx, prefix, flags, error))
		return FALSE;

	/* search for gettext .mo files */
	if (!as_app_builder_search_translations_gettext (ctx, prefix, flags, error))
		return FALSE;

	/* search for Google .pak files */
	if (!as_app_builder_search_translations_pak (ctx, prefix, flags, error))
		return FALSE;

	/* search for Mozilla .xpi files */
	if (!as_app_builder_search_translations_xpi (ctx, prefix, flags, error))
		return FALSE;

	/* calculate percentages */
	for (l = ctx->data; l != NULL; l = l->next) {
		e = l->data;
		e->percentage = MIN (e->nstrings * 100 / ctx->max_nstrings, 100);
	}

	/* sort */
	ctx->data = g_list_sort (ctx->data, as_app_builder_entry_sort_cb);

	/* add results */
	for (l = ctx->data; l != NULL; l = l->next) {
		e = l->data;
		if (e->percentage < min_percentage)
			continue;
		as_app_add_language (app, (gint) e->percentage, e->locale);
	}
	return TRUE;
}

static gboolean
as_app_builder_search_path (AsApp *app,
			    const gchar *prefix,
			    const gchar *path,
			    AsAppBuilderFlags flags)
{
	const gchar *tmp;
	g_autofree gchar *fn_prefix = NULL;
	g_autoptr(GDir) dir = NULL;

	/* find dir */
	fn_prefix = g_build_filename (prefix, path, NULL);
	if (!g_file_test (fn_prefix, G_FILE_TEST_IS_DIR))
		return FALSE;
	dir = g_dir_open (fn_prefix, 0, NULL);
	if (dir == NULL)
		return FALSE;

	/* find any file with the app-id prefix */
	while ((tmp = g_dir_read_name (dir)) != NULL) {
		if (g_str_has_prefix (tmp, as_app_get_id (app)))
			return TRUE;
	}

	/* just anything */
	if (flags & AS_APP_BUILDER_FLAG_USE_FALLBACKS)
		return TRUE;

	return FALSE;
}

/**
 * as_app_builder_search_kudos:
 * @app: an #AsApp
 * @prefix: a prefix to search, e.g. "/usr"
 * @flags: #AsAppBuilderFlags, e.g. %AS_APP_BUILDER_FLAG_USE_FALLBACKS
 * @error: a #GError or %NULL
 *
 * Searches a prefix for auto-detected kudos.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.5.8
 **/
gboolean
as_app_builder_search_kudos (AsApp *app,
			     const gchar *prefix,
			     AsAppBuilderFlags flags,
			     GError **error)
{
	/* gnome-shell search provider */
	if (!as_app_has_kudo_kind (app, AS_KUDO_KIND_SEARCH_PROVIDER) &&
	    as_app_builder_search_path (app, prefix,
					"share/gnome-shell/search-providers",
					flags)) {
		g_debug ("auto-adding SearchProvider kudo");
		as_app_add_kudo_kind (AS_APP (app), AS_KUDO_KIND_SEARCH_PROVIDER);
	}

	/* hicolor icon */
	if (!as_app_has_kudo_kind (app, AS_KUDO_KIND_HIGH_CONTRAST) &&
	    as_app_builder_search_path (app, prefix,
					"share/icons/hicolor/symbolic/apps",
					flags)) {
		g_debug ("auto-adding HighContrast kudo");
		as_app_add_kudo_kind (AS_APP (app), AS_KUDO_KIND_HIGH_CONTRAST);
	}
	return TRUE;
}

static gboolean
as_app_builder_search_dbus_file (AsApp *app,
				 const gchar *filename,
				 AsProvideKind provide_kind,
				 GError **error)
{
	g_autofree gchar *name = NULL;
	g_autoptr(AsProvide) provide = NULL;
	g_autoptr(GKeyFile) kf = NULL;

	/* load file */
	kf = g_key_file_new ();
	if (!g_key_file_load_from_file (kf, filename, G_KEY_FILE_NONE, error))
		return FALSE;
	name = g_key_file_get_string (kf, "D-BUS Service", "Name", error);
	if (name == NULL)
		return FALSE;

	/* add provide */
	provide = as_provide_new ();
	as_provide_set_kind (provide, provide_kind);
	as_provide_set_value (provide, name);
	as_app_add_provide (AS_APP (app), provide);
	return TRUE;
}

static gboolean
as_app_builder_search_dbus (AsApp *app,
			    const gchar *prefix,
			    const gchar *path,
			    AsProvideKind provide_kind,
			    AsAppBuilderFlags flags,
			    GError **error)
{
	const gchar *tmp;
	g_autofree gchar *fn_prefix = NULL;
	g_autoptr(GDir) dir = NULL;

	/* find dir */
	fn_prefix = g_build_filename (prefix, path, NULL);
	if (!g_file_test (fn_prefix, G_FILE_TEST_IS_DIR))
		return TRUE;
	dir = g_dir_open (fn_prefix, 0, error);
	if (dir == NULL)
		return FALSE;

	/* find any file with the app-id prefix */
	while ((tmp = g_dir_read_name (dir)) != NULL) {
		g_autofree gchar *fn = NULL;
		if ((flags & AS_APP_BUILDER_FLAG_USE_FALLBACKS) == 0) {
			if (!g_str_has_prefix (tmp, as_app_get_id (app)))
				continue;
		}
		fn = g_build_filename (fn_prefix, tmp, NULL);
		if (!as_app_builder_search_dbus_file (app, fn, provide_kind, error))
			return FALSE;
	}
	return TRUE;
}

/**
 * as_app_builder_search_provides:
 * @app: an #AsApp
 * @prefix: a prefix to search, e.g. "/usr"
 * @flags: #AsAppBuilderFlags, e.g. %AS_APP_BUILDER_FLAG_USE_FALLBACKS
 * @error: a #GError or %NULL
 *
 * Searches a prefix for auto-detected provides.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.5.8
 **/
gboolean
as_app_builder_search_provides (AsApp *app,
				const gchar *prefix,
				AsAppBuilderFlags flags,
				GError **error)
{
	/* skip for addons */
	if (as_app_get_kind (AS_APP (app)) == AS_APP_KIND_ADDON)
		return TRUE;

	if (!as_app_builder_search_dbus (app, prefix,
					 "share/dbus-1/system-services",
					 AS_PROVIDE_KIND_DBUS_SYSTEM,
					 flags, error))
		return FALSE;
	if (!as_app_builder_search_dbus (app, prefix,
					 "share/dbus-1/services",
					 AS_PROVIDE_KIND_DBUS_SESSION,
					 flags, error))
		return FALSE;
	return TRUE;
}