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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2013-2014 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2015-2019 Kalev Lember <klember@redhat.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <config.h>
#include <glib/gi18n.h>
#include <gnome-software.h>
#include <xmlb.h>
#include "gs-appstream.h"
/*
* SECTION:
* Uses offline AppStream data to populate and refine package results.
*
* This plugin calls UpdatesChanged() if any of the AppStream stores are
* changed in any way.
*
* Methods: | AddCategory
* Refines: | [source]->[name,summary,pixbuf,id,kind]
*/
struct GsPluginData {
XbSilo *silo;
GRWLock silo_lock;
GSettings *settings;
};
void
gs_plugin_initialize (GsPlugin *plugin)
{
GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
/* XbSilo needs external locking as we destroy the silo and build a new
* one when something changes */
g_rw_lock_init (&priv->silo_lock);
/* need package name */
gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "dpkg");
/* require settings */
priv->settings = g_settings_new ("org.gnome.software");
}
void
gs_plugin_destroy (GsPlugin *plugin)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_object_unref (priv->silo);
g_object_unref (priv->settings);
g_rw_lock_clear (&priv->silo_lock);
}
static const gchar *
gs_plugin_appstream_convert_component_kind (const gchar *kind)
{
if (g_strcmp0 (kind, "web-application") == 0)
return "webapp";
if (g_strcmp0 (kind, "console-application") == 0)
return "console";
return kind;
}
static gboolean
gs_plugin_appstream_upgrade_cb (XbBuilderFixup *self,
XbBuilderNode *bn,
gpointer user_data,
GError **error)
{
if (g_strcmp0 (xb_builder_node_get_element (bn), "application") == 0) {
g_autoptr(XbBuilderNode) id = xb_builder_node_get_child (bn, "id", NULL);
g_autofree gchar *kind = NULL;
if (id != NULL) {
kind = g_strdup (xb_builder_node_get_attr (id, "type"));
xb_builder_node_remove_attr (id, "type");
}
if (kind != NULL)
xb_builder_node_set_attr (bn, "type", kind);
xb_builder_node_set_element (bn, "component");
} else if (g_strcmp0 (xb_builder_node_get_element (bn), "metadata") == 0) {
xb_builder_node_set_element (bn, "custom");
} else if (g_strcmp0 (xb_builder_node_get_element (bn), "component") == 0) {
const gchar *type_old = xb_builder_node_get_attr (bn, "type");
const gchar *type_new = gs_plugin_appstream_convert_component_kind (type_old);
if (type_old != type_new)
xb_builder_node_set_attr (bn, "type", type_new);
}
return TRUE;
}
static gboolean
gs_plugin_appstream_add_icons_cb (XbBuilderFixup *self,
XbBuilderNode *bn,
gpointer user_data,
GError **error)
{
GsPlugin *plugin = GS_PLUGIN (user_data);
if (g_strcmp0 (xb_builder_node_get_element (bn), "component") != 0)
return TRUE;
gs_appstream_component_add_extra_info (plugin, bn);
return TRUE;
}
static gboolean
gs_plugin_appstream_add_origin_keyword_cb (XbBuilderFixup *self,
XbBuilderNode *bn,
gpointer user_data,
GError **error)
{
if (g_strcmp0 (xb_builder_node_get_element (bn), "components") == 0) {
const gchar *origin = xb_builder_node_get_attr (bn, "origin");
GPtrArray *components = xb_builder_node_get_children (bn);
if (origin == NULL || origin[0] == '\0')
return TRUE;
g_debug ("origin %s has %u components", origin, components->len);
if (components->len < 200) {
for (guint i = 0; i < components->len; i++) {
XbBuilderNode *component = g_ptr_array_index (components, i);
gs_appstream_component_add_keyword (component, origin);
}
}
}
return TRUE;
}
static gboolean
gs_plugin_appstream_load_appdata_fn (GsPlugin *plugin,
XbBuilder *builder,
const gchar *filename,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) file = g_file_new_for_path (filename);
g_autoptr(XbBuilderFixup) fixup = NULL;
g_autoptr(XbBuilderNode) info = NULL;
g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
/* add source */
if (!xb_builder_source_load_file (source, file,
#if LIBXMLB_CHECK_VERSION(0, 2, 0)
XB_BUILDER_SOURCE_FLAG_WATCH_DIRECTORY,
#else
XB_BUILDER_SOURCE_FLAG_WATCH_FILE,
#endif
cancellable,
error)) {
return FALSE;
}
/* fix up any legacy installed files */
fixup = xb_builder_fixup_new ("AppStreamUpgrade2",
gs_plugin_appstream_upgrade_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup, 3);
xb_builder_source_add_fixup (source, fixup);
/* add metadata */
info = xb_builder_node_insert (NULL, "info", NULL);
xb_builder_node_insert_text (info, "filename", filename, NULL);
xb_builder_source_set_info (source, info);
/* success */
xb_builder_import_source (builder, source);
return TRUE;
}
static gboolean
gs_plugin_appstream_load_appdata (GsPlugin *plugin,
XbBuilder *builder,
const gchar *path,
GCancellable *cancellable,
GError **error)
{
const gchar *fn;
g_autoptr(GDir) dir = NULL;
g_autoptr(GFile) parent = g_file_new_for_path (path);
if (!g_file_query_exists (parent, cancellable))
return TRUE;
dir = g_dir_open (path, 0, error);
if (dir == NULL)
return FALSE;
while ((fn = g_dir_read_name (dir)) != NULL) {
if (g_str_has_suffix (fn, ".appdata.xml") ||
g_str_has_suffix (fn, ".metainfo.xml")) {
g_autofree gchar *filename = g_build_filename (path, fn, NULL);
g_autoptr(GError) error_local = NULL;
if (!gs_plugin_appstream_load_appdata_fn (plugin,
builder,
filename,
cancellable,
&error_local)) {
g_debug ("ignoring %s: %s", filename, error_local->message);
continue;
}
}
}
/* success */
return TRUE;
}
static GInputStream *
gs_plugin_appstream_load_desktop_cb (XbBuilderSource *self,
XbBuilderSourceCtx *ctx,
gpointer user_data,
GCancellable *cancellable,
GError **error)
{
GString *xml;
g_autoptr(AsApp) app = as_app_new ();
g_autoptr(GBytes) bytes = NULL;
bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
if (bytes == NULL)
return NULL;
as_app_set_id (app, xb_builder_source_ctx_get_filename (ctx));
if (!as_app_parse_data (app, bytes, AS_APP_PARSE_FLAG_USE_FALLBACKS, error))
return NULL;
xml = as_app_to_xml (app, error);
if (xml == NULL)
return NULL;
g_string_prepend (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
return g_memory_input_stream_new_from_data (g_string_free (xml, FALSE), -1, g_free);
}
static gboolean
gs_plugin_appstream_load_desktop_fn (GsPlugin *plugin,
XbBuilder *builder,
const gchar *filename,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) file = g_file_new_for_path (filename);
g_autoptr(XbBuilderFixup) fixup = NULL;
g_autoptr(XbBuilderNode) info = NULL;
g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
/* add support for desktop files */
xb_builder_source_add_adapter (source, "application/x-desktop",
gs_plugin_appstream_load_desktop_cb, NULL, NULL);
/* add source */
if (!xb_builder_source_load_file (source, file,
#if LIBXMLB_CHECK_VERSION(0, 2, 0)
XB_BUILDER_SOURCE_FLAG_WATCH_DIRECTORY,
#else
XB_BUILDER_SOURCE_FLAG_WATCH_FILE,
#endif
cancellable,
error)) {
return FALSE;
}
/* add metadata */
info = xb_builder_node_insert (NULL, "info", NULL);
xb_builder_node_insert_text (info, "filename", filename, NULL);
xb_builder_source_set_info (source, info);
/* success */
xb_builder_import_source (builder, source);
return TRUE;
}
static gboolean
gs_plugin_appstream_load_desktop (GsPlugin *plugin,
XbBuilder *builder,
const gchar *path,
GCancellable *cancellable,
GError **error)
{
const gchar *fn;
g_autoptr(GDir) dir = NULL;
g_autoptr(GFile) parent = g_file_new_for_path (path);
if (!g_file_query_exists (parent, cancellable))
return TRUE;
dir = g_dir_open (path, 0, error);
if (dir == NULL)
return FALSE;
while ((fn = g_dir_read_name (dir)) != NULL) {
if (g_str_has_suffix (fn, ".desktop")) {
g_autofree gchar *filename = g_build_filename (path, fn, NULL);
g_autoptr(GError) error_local = NULL;
if (g_strcmp0 (fn, "mimeinfo.cache") == 0)
continue;
if (!gs_plugin_appstream_load_desktop_fn (plugin,
builder,
filename,
cancellable,
&error_local)) {
g_debug ("ignoring %s: %s", filename, error_local->message);
continue;
}
}
}
/* success */
return TRUE;
}
static GInputStream *
gs_plugin_appstream_load_dep11_cb (XbBuilderSource *self,
XbBuilderSourceCtx *ctx,
gpointer user_data,
GCancellable *cancellable,
GError **error)
{
GString *xml;
g_autoptr(AsStore) store = as_store_new ();
g_autoptr(GBytes) bytes = NULL;
bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
if (bytes == NULL)
return NULL;
if (!as_store_from_bytes (store, bytes, cancellable, error))
return FALSE;
xml = as_store_to_xml (store, AS_NODE_INSERT_FLAG_NONE);
if (xml == NULL)
return NULL;
g_string_prepend (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
return g_memory_input_stream_new_from_data (g_string_free (xml, FALSE), -1, g_free);
}
static gboolean
gs_plugin_appstream_load_appstream_fn (GsPlugin *plugin,
XbBuilder *builder,
const gchar *filename,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) file = g_file_new_for_path (filename);
g_autoptr(XbBuilderNode) info = NULL;
g_autoptr(XbBuilderFixup) fixup1 = NULL;
g_autoptr(XbBuilderFixup) fixup2 = NULL;
g_autoptr(XbBuilderFixup) fixup3 = NULL;
g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
/* add support for DEP-11 files */
xb_builder_source_add_adapter (source,
"application/x-yaml",
gs_plugin_appstream_load_dep11_cb,
NULL, NULL);
/* add source */
if (!xb_builder_source_load_file (source, file,
#if LIBXMLB_CHECK_VERSION(0, 2, 0)
XB_BUILDER_SOURCE_FLAG_WATCH_DIRECTORY,
#else
XB_BUILDER_SOURCE_FLAG_WATCH_FILE,
#endif
cancellable,
error)) {
return FALSE;
}
/* add metadata */
info = xb_builder_node_insert (NULL, "info", NULL);
xb_builder_node_insert_text (info, "scope", "system", NULL);
xb_builder_node_insert_text (info, "filename", filename, NULL);
xb_builder_source_set_info (source, info);
/* add missing icons as required */
fixup1 = xb_builder_fixup_new ("AddIcons",
gs_plugin_appstream_add_icons_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup1, 2);
xb_builder_source_add_fixup (source, fixup1);
/* fix up any legacy installed files */
fixup2 = xb_builder_fixup_new ("AppStreamUpgrade2",
gs_plugin_appstream_upgrade_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup2, 3);
xb_builder_source_add_fixup (source, fixup2);
/* add the origin as a search keyword for small repos */
fixup3 = xb_builder_fixup_new ("AddOriginKeyword",
gs_plugin_appstream_add_origin_keyword_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup3, 1);
xb_builder_source_add_fixup (source, fixup3);
/* success */
xb_builder_import_source (builder, source);
return TRUE;
}
static gboolean
gs_plugin_appstream_load_appstream (GsPlugin *plugin,
XbBuilder *builder,
const gchar *path,
GCancellable *cancellable,
GError **error)
{
const gchar *fn;
g_autoptr(GDir) dir = NULL;
g_autoptr(GFile) parent = g_file_new_for_path (path);
/* parent patch does not exist */
if (!g_file_query_exists (parent, cancellable))
return TRUE;
dir = g_dir_open (path, 0, error);
if (dir == NULL)
return FALSE;
while ((fn = g_dir_read_name (dir)) != NULL) {
if (g_str_has_suffix (fn, ".xml") ||
g_str_has_suffix (fn, ".yml") ||
g_str_has_suffix (fn, ".yml.gz") ||
g_str_has_suffix (fn, ".xml.gz")) {
g_autofree gchar *filename = g_build_filename (path, fn, NULL);
g_autoptr(GError) error_local = NULL;
if (!gs_plugin_appstream_load_appstream_fn (plugin,
builder,
filename,
cancellable,
&error_local)) {
g_debug ("ignoring %s: %s", filename, error_local->message);
continue;
}
}
}
/* success */
return TRUE;
}
static gboolean
gs_plugin_appstream_check_silo (GsPlugin *plugin,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
const gchar *locale;
const gchar *test_xml;
g_autofree gchar *blobfn = NULL;
g_autoptr(XbBuilder) builder = xb_builder_new ();
g_autoptr(XbNode) n = NULL;
g_autoptr(GFile) file = NULL;
g_autoptr(GRWLockReaderLocker) reader_locker = NULL;
g_autoptr(GRWLockWriterLocker) writer_locker = NULL;
g_autoptr(GPtrArray) parent_appdata = g_ptr_array_new_with_free_func (g_free);
g_autoptr(GPtrArray) parent_appstream = g_ptr_array_new_with_free_func (g_free);
reader_locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* everything is okay */
if (priv->silo != NULL && xb_silo_is_valid (priv->silo))
return TRUE;
g_clear_pointer (&reader_locker, g_rw_lock_reader_locker_free);
/* drat! silo needs regenerating */
writer_locker = g_rw_lock_writer_locker_new (&priv->silo_lock);
g_clear_object (&priv->silo);
/* verbose profiling */
if (g_getenv ("GS_XMLB_VERBOSE") != NULL) {
xb_builder_set_profile_flags (builder,
XB_SILO_PROFILE_FLAG_XPATH |
XB_SILO_PROFILE_FLAG_DEBUG);
}
/* add current locales */
locale = g_getenv ("GS_SELF_TEST_LOCALE");
if (locale == NULL) {
const gchar *const *locales = g_get_language_names ();
for (guint i = 0; locales[i] != NULL; i++)
xb_builder_add_locale (builder, locales[i]);
} else {
xb_builder_add_locale (builder, locale);
}
/* only when in self test */
test_xml = g_getenv ("GS_SELF_TEST_APPSTREAM_XML");
if (test_xml != NULL) {
g_autoptr(XbBuilderFixup) fixup1 = NULL;
g_autoptr(XbBuilderFixup) fixup2 = NULL;
g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
if (!xb_builder_source_load_xml (source, test_xml,
XB_BUILDER_SOURCE_FLAG_NONE,
error))
return FALSE;
fixup1 = xb_builder_fixup_new ("AddOriginKeywords",
gs_plugin_appstream_add_origin_keyword_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup1, 1);
xb_builder_source_add_fixup (source, fixup1);
fixup2 = xb_builder_fixup_new ("AddIcons",
gs_plugin_appstream_add_icons_cb,
plugin, NULL);
xb_builder_fixup_set_max_depth (fixup2, 2);
xb_builder_source_add_fixup (source, fixup2);
xb_builder_import_source (builder, source);
} else {
/* add search paths */
g_ptr_array_add (parent_appstream,
g_build_filename (DATADIR, "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename (DATADIR, "app-info", "yaml", NULL));
g_ptr_array_add (parent_appdata,
g_build_filename (DATADIR, "appdata", NULL));
g_ptr_array_add (parent_appdata,
g_build_filename (DATADIR, "metainfo", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename (LOCALSTATEDIR, "cache", "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename (LOCALSTATEDIR, "cache", "app-info", "yaml", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename (LOCALSTATEDIR, "lib", "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename (LOCALSTATEDIR, "lib", "app-info", "yaml", NULL));
/* Add the normal system directories if the installation prefix
* is different from normal — typically this happens when doing
* development builds. It’s useful to still list the system apps
* during development. */
if (g_strcmp0 (DATADIR, "/usr/share") != 0) {
g_ptr_array_add (parent_appstream,
g_build_filename ("/usr/share", "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename ("/usr/share", "app-info", "yaml", NULL));
g_ptr_array_add (parent_appdata,
g_build_filename ("/usr/share", "appdata", NULL));
g_ptr_array_add (parent_appdata,
g_build_filename ("/usr/share", "metainfo", NULL));
}
if (g_strcmp0 (LOCALSTATEDIR, "/var") != 0) {
g_ptr_array_add (parent_appstream,
g_build_filename ("/var", "cache", "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename ("/var", "cache", "app-info", "yaml", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename ("/var", "lib", "app-info", "xmls", NULL));
g_ptr_array_add (parent_appstream,
g_build_filename ("/var", "lib", "app-info", "yaml", NULL));
}
/* import all files */
for (guint i = 0; i < parent_appstream->len; i++) {
const gchar *fn = g_ptr_array_index (parent_appstream, i);
if (!gs_plugin_appstream_load_appstream (plugin, builder, fn,
cancellable, error))
return FALSE;
}
for (guint i = 0; i < parent_appdata->len; i++) {
const gchar *fn = g_ptr_array_index (parent_appdata, i);
if (!gs_plugin_appstream_load_appdata (plugin, builder, fn,
cancellable, error))
return FALSE;
}
if (!gs_plugin_appstream_load_desktop (plugin, builder,
DATADIR "/applications",
cancellable, error)) {
return FALSE;
}
if (g_strcmp0 (DATADIR, "/usr/share") != 0 &&
!gs_plugin_appstream_load_desktop (plugin, builder,
"/usr/share/applications",
cancellable, error)) {
return FALSE;
}
}
/* regenerate with each minor release */
xb_builder_append_guid (builder, PACKAGE_VERSION);
/* create per-user cache */
blobfn = gs_utils_get_cache_filename ("appstream", "components.xmlb",
GS_UTILS_CACHE_FLAG_WRITEABLE,
error);
if (blobfn == NULL)
return FALSE;
file = g_file_new_for_path (blobfn);
g_debug ("ensuring %s", blobfn);
priv->silo = xb_builder_ensure (builder, file,
XB_BUILDER_COMPILE_FLAG_IGNORE_INVALID |
XB_BUILDER_COMPILE_FLAG_SINGLE_LANG,
NULL, error);
if (priv->silo == NULL)
return FALSE;
/* watch all directories too */
for (guint i = 0; i < parent_appstream->len; i++) {
const gchar *fn = g_ptr_array_index (parent_appstream, i);
g_autoptr(GFile) file_tmp = g_file_new_for_path (fn);
if (!xb_silo_watch_file (priv->silo, file_tmp, cancellable, error))
return FALSE;
}
for (guint i = 0; i < parent_appdata->len; i++) {
const gchar *fn = g_ptr_array_index (parent_appdata, i);
g_autoptr(GFile) file_tmp = g_file_new_for_path (fn);
if (!xb_silo_watch_file (priv->silo, file_tmp, cancellable, error))
return FALSE;
}
/* test we found something */
n = xb_silo_query_first (priv->silo, "components/component", NULL);
if (n == NULL) {
g_warning ("No AppStream data, try 'make install-sample-data' in data/");
g_set_error (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"No AppStream data found");
return FALSE;
}
/* success */
return TRUE;
}
gboolean
gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
{
/* set up silo, compiling if required */
return gs_plugin_appstream_check_silo (plugin, cancellable, error);
}
gboolean
gs_plugin_url_to_app (GsPlugin *plugin,
GsAppList *list,
const gchar *url,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autofree gchar *path = NULL;
g_autofree gchar *scheme = NULL;
g_autofree gchar *xpath = NULL;
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(GsApp) app = NULL;
g_autoptr(XbNode) component = NULL;
/* check silo is valid */
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
/* not us */
scheme = gs_utils_get_url_scheme (url);
if (g_strcmp0 (scheme, "appstream") != 0)
return TRUE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* create app */
path = gs_utils_get_url_path (url);
xpath = g_strdup_printf ("components/component/id[text()='%s']", path);
component = xb_silo_query_first (priv->silo, xpath, NULL);
if (component == NULL)
return TRUE;
app = gs_appstream_create_app (plugin, priv->silo, component, error);
if (app == NULL)
return FALSE;
gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM);
gs_app_list_add (list, app);
return TRUE;
}
static void
gs_plugin_appstream_set_compulsory_quirk (GsApp *app, XbNode *component)
{
g_autoptr(GPtrArray) array = NULL;
const gchar *current_desktop;
/*
* Set the core applications for the current desktop that cannot be
* removed.
*
* If XDG_CURRENT_DESKTOP contains ":", indicating that it is made up
* of multiple components per the Desktop Entry Specification, an app
* is compulsory if any of the components in XDG_CURRENT_DESKTOP match
* any value in <compulsory_for_desktops />. In that way,
* "GNOME-Classic:GNOME" shares compulsory apps with GNOME.
*
* As a special case, if the <compulsory_for_desktop /> value contains
* a ":", we match the entire XDG_CURRENT_DESKTOP. This lets people set
* compulsory apps for such compound desktops if they want.
*
*/
array = xb_node_query (component, "compulsory_for_desktop", 0, NULL);
if (array == NULL)
return;
current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
if (current_desktop != NULL) {
g_auto(GStrv) xdg_current_desktops = g_strsplit (current_desktop, ":", 0);
for (guint i = 0; i < array->len; i++) {
XbNode *n = g_ptr_array_index (array, i);
const gchar *tmp = xb_node_get_text (n);
/* if the value has a :, check the whole string */
if (g_strstr_len (tmp, -1, ":")) {
if (g_strcmp0 (current_desktop, tmp) == 0) {
gs_app_add_quirk (app, GS_APP_QUIRK_COMPULSORY);
break;
}
/* otherwise check if any element matches this one */
} else if (g_strv_contains ((const gchar * const *) xdg_current_desktops, tmp)) {
gs_app_add_quirk (app, GS_APP_QUIRK_COMPULSORY);
break;
}
}
}
}
static gboolean
gs_plugin_appstream_refine_state (GsPlugin *plugin, GsApp *app, GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autofree gchar *xpath = NULL;
g_autoptr(GError) error_local = NULL;
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(XbNode) component = NULL;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
xpath = g_strdup_printf ("component/id[text()='%s']", gs_app_get_id (app));
component = xb_silo_query_first (priv->silo, xpath, &error_local);
if (component == NULL) {
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
return TRUE;
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT))
return TRUE;
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
gs_app_set_state (app, AS_APP_STATE_INSTALLED);
return TRUE;
}
static gboolean
gs_plugin_refine_from_id (GsPlugin *plugin,
GsApp *app,
GsPluginRefineFlags flags,
gboolean *found,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
const gchar *id;
g_autoptr(GError) error_local = NULL;
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(GString) xpath = g_string_new (NULL);
g_autoptr(GPtrArray) components = NULL;
/* not enough info to find */
id = gs_app_get_id (app);
if (id == NULL)
return TRUE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* look in AppStream then fall back to AppData */
xb_string_append_union (xpath, "components/component/id[text()='%s']/../pkgname/..", id);
xb_string_append_union (xpath, "components/component[@type='webapp']/id[text()='%s']/..", id);
xb_string_append_union (xpath, "component/id[text()='%s']/..", id);
components = xb_silo_query (priv->silo, xpath->str, 0, &error_local);
if (components == NULL) {
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
return TRUE;
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT))
return TRUE;
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
for (guint i = 0; i < components->len; i++) {
XbNode *component = g_ptr_array_index (components, i);
if (!gs_appstream_refine_app (plugin, app, priv->silo,
component, flags, error))
return FALSE;
gs_plugin_appstream_set_compulsory_quirk (app, component);
}
/* if an installed desktop or appdata file exists set to installed */
if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN) {
if (!gs_plugin_appstream_refine_state (plugin, app, error))
return FALSE;
}
/* success */
*found = TRUE;
return TRUE;
}
static gboolean
gs_plugin_refine_from_pkgname (GsPlugin *plugin,
GsApp *app,
GsPluginRefineFlags flags,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
GPtrArray *sources = gs_app_get_sources (app);
g_autoptr(GError) error_local = NULL;
/* not enough info to find */
if (sources->len == 0)
return TRUE;
/* find all apps when matching any prefixes */
for (guint j = 0; j < sources->len; j++) {
const gchar *pkgname = g_ptr_array_index (sources, j);
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(GString) xpath = g_string_new (NULL);
g_autoptr(XbNode) component = NULL;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* prefer actual apps and then fallback to anything else */
xb_string_append_union (xpath, "components/component[@type='desktop']/pkgname[text()='%s']/..", pkgname);
xb_string_append_union (xpath, "components/component[@type='console']/pkgname[text()='%s']/..", pkgname);
xb_string_append_union (xpath, "components/component[@type='webapp']/pkgname[text()='%s']/..", pkgname);
xb_string_append_union (xpath, "components/component/pkgname[text()='%s']/..", pkgname);
component = xb_silo_query_first (priv->silo, xpath->str, &error_local);
if (component == NULL) {
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
continue;
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT))
continue;
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
if (!gs_appstream_refine_app (plugin, app, priv->silo, component, flags, error))
return FALSE;
gs_plugin_appstream_set_compulsory_quirk (app, component);
}
/* success */
return TRUE;
}
gboolean
gs_plugin_refine (GsPlugin *plugin,
GsAppList *list,
GsPluginRefineFlags flags,
GCancellable *cancellable,
GError **error)
{
gboolean found = FALSE;
/* check silo is valid */
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
for (guint i = 0; i < gs_app_list_length (list); i++) {
GsApp *app = gs_app_list_index (list, i);
/* not us */
if (gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_PACKAGE &&
gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_UNKNOWN)
return TRUE;
/* find by ID then fall back to package name */
if (!gs_plugin_refine_from_id (plugin, app, flags, &found, error))
return FALSE;
if (!found) {
if (!gs_plugin_refine_from_pkgname (plugin, app, flags, error))
return FALSE;
}
}
/* success */
return TRUE;
}
gboolean
gs_plugin_refine_wildcard (GsPlugin *plugin,
GsApp *app,
GsAppList *list,
GsPluginRefineFlags refine_flags,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
const gchar *id;
g_autofree gchar *xpath = NULL;
g_autoptr(GError) error_local = NULL;
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(GPtrArray) components = NULL;
/* check silo is valid */
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
/* not enough info to find */
id = gs_app_get_id (app);
if (id == NULL)
return TRUE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* find all app with package names when matching any prefixes */
xpath = g_strdup_printf ("components/component/id[text()='%s']/../pkgname/..", id);
components = xb_silo_query (priv->silo, xpath, 0, &error_local);
if (components == NULL) {
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
return TRUE;
if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT))
return TRUE;
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
for (guint i = 0; i < components->len; i++) {
XbNode *component = g_ptr_array_index (components, i);
g_autoptr(GsApp) new = NULL;
/* new app */
new = gs_appstream_create_app (plugin, priv->silo, component, error);
if (new == NULL)
return FALSE;
gs_app_set_scope (new, AS_APP_SCOPE_SYSTEM);
gs_app_subsume_metadata (new, app);
if (!gs_appstream_refine_app (plugin, new, priv->silo, component,
refine_flags, error))
return FALSE;
gs_app_list_add (list, new);
}
/* success */
return TRUE;
}
gboolean
gs_plugin_add_category_apps (GsPlugin *plugin,
GsCategory *category,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_category_apps (plugin,
priv->silo,
category,
list,
cancellable,
error);
}
gboolean
gs_plugin_add_search (GsPlugin *plugin,
gchar **values,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_search (plugin,
priv->silo,
(const gchar * const *) values,
list,
cancellable,
error);
}
gboolean
gs_plugin_add_installed (GsPlugin *plugin,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
g_autoptr(GPtrArray) components = NULL;
/* check silo is valid */
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
/* get all installed appdata files (notice no 'components/' prefix...) */
components = xb_silo_query (priv->silo, "component/description/..", 0, NULL);
if (components == NULL)
return TRUE;
for (guint i = 0; i < components->len; i++) {
XbNode *component = g_ptr_array_index (components, i);
g_autoptr(GsApp) app = gs_appstream_create_app (plugin, priv->silo, component, error);
if (app == NULL)
return FALSE;
gs_app_set_state (app, AS_APP_STATE_INSTALLED);
gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM);
gs_app_list_add (list, app);
}
return TRUE;
}
gboolean
gs_plugin_add_categories (GsPlugin *plugin,
GPtrArray *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_categories (plugin, priv->silo, list,
cancellable, error);
}
gboolean
gs_plugin_add_popular (GsPlugin *plugin,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_popular (plugin, priv->silo, list, cancellable, error);
}
gboolean
gs_plugin_add_featured (GsPlugin *plugin,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_featured (plugin, priv->silo, list, cancellable, error);
}
gboolean
gs_plugin_add_recent (GsPlugin *plugin,
GsAppList *list,
guint64 age,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_recent (plugin, priv->silo, list, age,
cancellable, error);
}
gboolean
gs_plugin_add_alternates (GsPlugin *plugin,
GsApp *app,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_autoptr(GRWLockReaderLocker) locker = NULL;
if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
return FALSE;
locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
return gs_appstream_add_alternates (plugin, priv->silo, app, list,
cancellable, error);
}
gboolean
gs_plugin_refresh (GsPlugin *plugin,
guint cache_age,
GCancellable *cancellable,
GError **error)
{
return gs_plugin_appstream_check_silo (plugin, cancellable, error);
}
|