/
asc-compose.c
2319 lines (2083 loc) · 68.9 KB
/
asc-compose.c
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2016-2024 Matthias Klumpp <matthias@tenstral.net>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the license, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:asc-compose
* @short_description: Compose catalog metadata easily.
* @include: appstream-compose.h
*/
#include "config.h"
#include "asc-compose.h"
#include <errno.h>
#include <glib/gi18n.h>
#include "as-utils-private.h"
#include "as-yaml.h"
#include "as-curl.h"
#include "asc-globals-private.h"
#include "asc-utils.h"
#include "asc-hint.h"
#include "asc-result.h"
#include "asc-utils-metainfo.h"
#include "asc-utils-l10n.h"
#include "asc-utils-screenshots.h"
#include "asc-utils-fonts.h"
#include "asc-image.h"
typedef struct {
GPtrArray *units;
GPtrArray *results;
AscUnit *locale_unit;
GHashTable *allowed_cids;
GRefString *prefix;
GRefString *origin;
gchar *media_baseurl;
AsFormatKind format;
guint min_l10n_percentage;
GPtrArray *custom_allowed;
gssize max_scr_size_bytes;
gchar *cainfo;
AscComposeFlags flags;
AscIconPolicy *icon_policy;
gchar *data_result_dir;
gchar *icons_result_dir;
gchar *media_result_dir;
gchar *hints_result_dir;
GHashTable *known_cids;
GMutex mutex;
AscCheckMetadataEarlyFn check_md_early_fn;
gpointer check_md_early_fn_udata;
AscTranslateDesktopTextFn de_l10n_fn;
gpointer de_l10n_fn_udata;
} AscComposePrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AscCompose, asc_compose, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (asc_compose_get_instance_private (o))
static void
asc_compose_init (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->units = g_ptr_array_new_with_free_func (g_object_unref);
priv->results = g_ptr_array_new_with_free_func (g_object_unref);
priv->allowed_cids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
priv->known_cids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
priv->custom_allowed = g_ptr_array_new_with_free_func (g_free);
g_mutex_init (&priv->mutex);
/* defaults */
priv->format = AS_FORMAT_KIND_XML;
as_ref_string_assign_safe (&priv->prefix, "/usr");
priv->min_l10n_percentage = 25;
priv->max_scr_size_bytes = -1;
priv->flags = ASC_COMPOSE_FLAG_USE_THREADS | ASC_COMPOSE_FLAG_ALLOW_NET |
ASC_COMPOSE_FLAG_VALIDATE | ASC_COMPOSE_FLAG_STORE_SCREENSHOTS |
ASC_COMPOSE_FLAG_ALLOW_SCREENCASTS | ASC_COMPOSE_FLAG_PROCESS_FONTS |
ASC_COMPOSE_FLAG_PROCESS_TRANSLATIONS;
/* the icon policy will initialize with default settings */
priv->icon_policy = asc_icon_policy_new ();
}
static void
asc_compose_finalize (GObject *object)
{
AscCompose *compose = ASC_COMPOSE (object);
AscComposePrivate *priv = GET_PRIVATE (compose);
g_ptr_array_unref (priv->units);
g_ptr_array_unref (priv->results);
g_ptr_array_unref (priv->custom_allowed);
g_free (priv->cainfo);
g_hash_table_unref (priv->allowed_cids);
g_hash_table_unref (priv->known_cids);
as_ref_string_release (priv->prefix);
as_ref_string_release (priv->origin);
g_free (priv->media_baseurl);
g_free (priv->data_result_dir);
g_free (priv->icons_result_dir);
g_free (priv->media_result_dir);
g_free (priv->hints_result_dir);
if (priv->locale_unit != NULL)
g_object_unref (priv->locale_unit);
g_mutex_clear (&priv->mutex);
G_OBJECT_CLASS (asc_compose_parent_class)->finalize (object);
}
static void
asc_compose_class_init (AscComposeClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = asc_compose_finalize;
}
/**
* asc_compose_reset:
* @compose: an #AscCompose instance.
*
* Reset the results, units and run-specific settings so the
* instance can be reused for another metadata generation run.
**/
void
asc_compose_reset (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
g_hash_table_remove_all (priv->allowed_cids);
g_ptr_array_set_size (priv->units, 0);
g_ptr_array_set_size (priv->results, 0);
g_hash_table_remove_all (priv->known_cids);
}
/**
* asc_compose_add_unit:
* @compose: an #AscCompose instance.
* @unit: The #AscUnit to add
*
* Add an #AscUnit as data source for metadata processing.
**/
void
asc_compose_add_unit (AscCompose *compose, AscUnit *unit)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
/* sanity check */
for (guint i = 0; i < priv->units->len; i++) {
if (unit == g_ptr_array_index (priv->units, i)) {
g_critical ("Not adding unit duplicate for processing!");
return;
}
}
g_ptr_array_add (priv->units, g_object_ref (unit));
}
/**
* asc_compose_add_allowed_cid:
* @compose: an #AscCompose instance.
* @component_id: The component-id to whitelist
*
* Adds a component ID to the allowlist. If the list is not empty, only
* components in the list will be added to the metadata output.
**/
void
asc_compose_add_allowed_cid (AscCompose *compose, const gchar *component_id)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
g_hash_table_add (priv->allowed_cids, g_strdup (component_id));
}
/**
* asc_compose_get_prefix:
* @compose: an #AscCompose instance.
*
* Get the directory prefix used for processing.
*/
const gchar *
asc_compose_get_prefix (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->prefix;
}
/**
* asc_compose_set_prefix:
* @compose: an #AscCompose instance.
* @prefix: a directory prefix, e.g. "/usr"
*
* Set the directory prefix the to-be-processed units are using.
*/
void
asc_compose_set_prefix (AscCompose *compose, const gchar *prefix)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
/* do a bit of sanitizing: "no prefix" means the prefix directory is the root directory */
if (prefix == NULL || g_strcmp0 (prefix, "") == 0)
as_ref_string_assign_safe (&priv->prefix, "/");
else
as_ref_string_assign_safe (&priv->prefix, prefix);
}
/**
* asc_compose_get_origin:
* @compose: an #AscCompose instance.
*
* Get the metadata origin field.
*/
const gchar *
asc_compose_get_origin (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->origin;
}
/**
* asc_compose_set_origin:
* @compose: an #AscCompose instance.
* @origin: the origin.
*
* Set the metadata origin field (e.g. "debian" or "flathub")
*/
void
asc_compose_set_origin (AscCompose *compose, const gchar *origin)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
g_autofree gchar *tmp = NULL;
tmp = g_markup_escape_text (origin, -1);
as_ref_string_assign_safe (&priv->origin, tmp);
}
/**
* asc_compose_get_format:
* @compose: an #AscCompose instance.
*
* get the format type we are generating.
*/
AsFormatKind
asc_compose_get_format (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->format;
}
/**
* asc_compose_set_format:
* @compose: an #AscCompose instance.
* @kind: The format, e.g. %AS_FORMAT_KIND_XML
*
* Set the format kind of the catalog metadata that we should generate.
*/
void
asc_compose_set_format (AscCompose *compose, AsFormatKind kind)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->format = kind;
}
/**
* asc_compose_get_media_baseurl:
* @compose: an #AscCompose instance.
*
* Get the media base URL to be used for the generated data,
* or %NULL if no media is cached.
*/
const gchar *
asc_compose_get_media_baseurl (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->media_baseurl;
}
/**
* asc_compose_set_media_baseurl:
* @compose: an #AscCompose instance.
* @url: (nullable): the media base URL.
*
* Set the media base URL for the generated metadata. Can be %NULL if no media
* should be cached and the original URLs should be kept.
*/
void
asc_compose_set_media_baseurl (AscCompose *compose, const gchar *url)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
as_assign_string_safe (priv->media_baseurl, url);
}
/**
* asc_compose_get_flags:
* @compose: an #AscCompose instance.
*
* Get the flags controlling compose behavior.
*/
AscComposeFlags
asc_compose_get_flags (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->flags;
}
/**
* asc_compose_set_flags:
* @compose: an #AscCompose instance.
* @flags: The compose flags bitfield.
*
* Set compose flags bitfield that controls the enabled features
* for this #AscCompose.
*/
void
asc_compose_set_flags (AscCompose *compose, AscComposeFlags flags)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->flags = flags;
}
/**
* asc_compose_add_flags:
* @compose: an #AscCompose instance.
* @flags: The compose flags to add.
*
* Add compose flags.
*/
void
asc_compose_add_flags (AscCompose *compose, AscComposeFlags flags)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
as_flags_add (priv->flags, flags);
}
/**
* asc_compose_remove_flags:
* @compose: an #AscCompose instance.
* @flags: The compose flags to remove.
*
* Remove compose flags.
*/
void
asc_compose_remove_flags (AscCompose *compose, AscComposeFlags flags)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
as_flags_remove (priv->flags, flags);
}
/**
* asc_compose_get_icon_policy:
* @compose: an #AscCompose instance.
*
* Get the policy for how icons should be distributed to
* any AppStream clients.
*
* Returns: (transfer none): an #AscIconPolicy
*/
AscIconPolicy *
asc_compose_get_icon_policy (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->icon_policy;
}
/**
* asc_compose_set_icon_policy:
* @compose: an #AscCompose instance.
* @policy: (not nullable): an #AscIconPolicy instance
*
* Set an icon policy object, overriding the existing one.
*/
void
asc_compose_set_icon_policy (AscCompose *compose, AscIconPolicy *policy)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_return_if_fail (policy != NULL);
g_object_unref (priv->icon_policy);
priv->icon_policy = g_object_ref (policy);
}
/**
* asc_compose_get_cainfo:
* @compose: an #AscCompose instance.
*
* Get the CA file used to verify peers with, or %NULL for default.
*/
const gchar *
asc_compose_get_cainfo (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->cainfo;
}
/**
* asc_compose_set_cainfo:
* @compose: an #AscCompose instance.
* @cainfo: a valid file path
*
* Set a CA file holding one or more certificates to verify peers with
* for download operations performed by this #AscCompose.
*/
void
asc_compose_set_cainfo (AscCompose *compose, const gchar *cainfo)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
as_assign_string_safe (priv->cainfo, cainfo);
}
/**
* asc_compose_get_data_result_dir:
* @compose: an #AscCompose instance.
*
* Get the data result directory.
*/
const gchar *
asc_compose_get_data_result_dir (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->data_result_dir;
}
/**
* asc_compose_set_data_result_dir:
* @compose: an #AscCompose instance.
* @dir: the metadata save location.
*
* Set an output location where generated metadata should be saved.
* If this is set to %NULL, no metadata will be saved.
*/
void
asc_compose_set_data_result_dir (AscCompose *compose, const gchar *dir)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
as_assign_string_safe (priv->data_result_dir, dir);
}
/**
* asc_compose_get_icons_result_dir:
* @compose: an #AscCompose instance.
*
* Get the icon result directory.
*/
const gchar *
asc_compose_get_icons_result_dir (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->icons_result_dir;
}
/**
* asc_compose_set_icons_result_dir:
* @compose: an #AscCompose instance.
* @dir: the icon storage location.
*
* Set an output location where plain icons for the processed metadata
* are stored.
*/
void
asc_compose_set_icons_result_dir (AscCompose *compose, const gchar *dir)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
as_assign_string_safe (priv->icons_result_dir, dir);
}
/**
* asc_compose_get_media_result_dir:
* @compose: an #AscCompose instance.
*
* Get the media result directory, that can be served on a webserver.
*/
const gchar *
asc_compose_get_media_result_dir (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->media_result_dir;
}
/**
* asc_compose_set_media_result_dir:
* @compose: an #AscCompose instance.
* @dir: the media storage location.
*
* Set an output location to store media (screenshots, icons, ...) that
* will be served on a webserver via the URL set as media baseurl.
*/
void
asc_compose_set_media_result_dir (AscCompose *compose, const gchar *dir)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
as_assign_string_safe (priv->media_result_dir, dir);
}
/**
* asc_compose_get_hints_result_dir:
* @compose: an #AscCompose instance.
*
* Get hints report output directory.
*/
const gchar *
asc_compose_get_hints_result_dir (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->hints_result_dir;
}
/**
* asc_compose_set_hints_result_dir:
* @compose: an #AscCompose instance.
* @dir: the hints data directory.
*
* Set an output location for HTML reports of issues generated
* during a compose run.
*/
void
asc_compose_set_hints_result_dir (AscCompose *compose, const gchar *dir)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
as_assign_string_safe (priv->hints_result_dir, dir);
}
/**
* asc_compose_remove_custom_allowed:
* @compose: an #AscCompose instance.
* @key_id: the custom key to drop from the allowed list.
*
* Remove a key from the allowlist used to filter the `<custom/>` tag entries.
*/
void
asc_compose_remove_custom_allowed (AscCompose *compose, const gchar *key_id)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
for (guint i = 0; i < priv->custom_allowed->len; i++) {
if (g_strcmp0 (g_ptr_array_index (priv->custom_allowed, i), key_id) == 0) {
g_ptr_array_remove_index_fast (priv->custom_allowed, i);
break;
}
}
}
/**
* asc_compose_add_custom_allowed:
* @compose: an #AscCompose instance.
* @key_id: the custom key to add to the allowed list.
*
* Add a key to the allowlist that is used to filter custom tag values.
*/
void
asc_compose_add_custom_allowed (AscCompose *compose, const gchar *key_id)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
g_ptr_array_add (priv->custom_allowed, g_strdup (key_id));
}
/**
* asc_compose_get_max_screenshot_size:
* @compose: an #AscCompose instance.
*
* Get the maximum size a screenshot video or image can have.
* A size < 0 may be returned for no limit, setting a limit of 0
* will disable screenshots.
*/
gssize
asc_compose_get_max_screenshot_size (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->max_scr_size_bytes;
}
/**
* asc_compose_set_max_screenshot_size:
* @compose: an #AscCompose instance.
* @size_bytes: maximum size of a screenshot image or video in bytes
*
* Set the maximum size a screenshot video or image can have.
* A size < 0 may be set to allow unlimited sizes, setting a limit of 0
* will disable screenshot caching entirely.
*/
void
asc_compose_set_max_screenshot_size (AscCompose *compose, gssize size_bytes)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->max_scr_size_bytes = size_bytes;
}
/**
* asc_compose_set_check_metadata_early_func:
* @compose: an #AscCompose instance.
* @func: (scope notified): the #AscCheckMetainfoLoadResultFn function to be called
* @user_data: user data for @func
*
* Set an custom callback to be run when most of the metadata has been loaded,
* but no expensive operations (like downloads or icon rendering) have been done yet.
* This can be used to ignore unwanted components early on.
*
* The callback function may be called from any thread, so it needs to ensure thread safety on its own.
*/
void
asc_compose_set_check_metadata_early_func (AscCompose *compose,
AscCheckMetadataEarlyFn func,
gpointer user_data)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->check_md_early_fn = func;
priv->check_md_early_fn_udata = user_data;
}
/**
* asc_compose_set_desktop_entry_l10n_func:
* @compose: an #AscCompose instance.
* @func: (scope notified): the #AscTranslateDesktopTextFn function to be called
* @user_data: user data for @func
*
* Set a custom desktop-entry field localization functions to be run for specialized
* desktop-entry localization schemes such as used in Ubuntu.
*
* The callback function may be called from any thread, so it needs to ensure thread safety on its own.
*/
void
asc_compose_set_desktop_entry_l10n_func (AscCompose *compose,
AscTranslateDesktopTextFn func,
gpointer user_data)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
priv->de_l10n_fn = func;
priv->de_l10n_fn_udata = user_data;
}
/**
* asc_compose_get_locale_unit:
* @compose: an #AscCompose instance.
*
* Get the unit we use for locale processing
*
* Return: (transfer none) (nullable): The unit used for locale processing, or %NULL for default.
*/
AscUnit *
asc_compose_get_locale_unit (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->locale_unit;
}
/**
* asc_compose_set_locale_unit:
* @compose: an #AscCompose instance.
* @locale_unit: the unit used for locale processing.
*
* Set a specific unit that is used for fetching locale information.
* This may be useful in case a special language pack layout is used,
* but is generally not necessary to be set explicitly, as locale
* will be found in the unit where the metadata is by default.
*/
void
asc_compose_set_locale_unit (AscCompose *compose, AscUnit *locale_unit)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
if (priv->locale_unit == locale_unit)
return;
if (priv->locale_unit != NULL)
g_object_unref (priv->locale_unit);
priv->locale_unit = g_object_ref (locale_unit);
}
/**
* asc_compose_get_results:
* @compose: an #AscCompose instance.
*
* Get the results of the last processing run.
*
* Returns: (transfer none) (element-type AscResult): The results
*/
GPtrArray *
asc_compose_get_results (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
return priv->results;
}
/**
* asc_compose_fetch_components:
* @compose: an #AscCompose instance.
*
* Get the results components extracted in the last data processing run.
*
* Returns: (transfer container) (element-type AsComponent): The components
*/
GPtrArray *
asc_compose_fetch_components (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
GPtrArray *cpts_result = g_ptr_array_new_with_free_func (g_object_unref);
for (guint i = 0; i < priv->results->len; i++) {
g_autoptr(GPtrArray) cpts = NULL;
AscResult *res = ASC_RESULT (g_ptr_array_index (priv->results, i));
cpts = asc_result_fetch_components (res);
for (guint j = 0; j < cpts->len; j++)
g_ptr_array_add (cpts_result,
g_object_ref (AS_COMPONENT (g_ptr_array_index (cpts, j))));
}
return cpts_result;
}
/**
* asc_compose_has_errors:
* @compose: an #AscCompose instance.
*
* Check if the last run generated any errors (which will cause metadata to be ignored).
*
* Returns: %TRUE if we had errors.
*/
gboolean
asc_compose_has_errors (AscCompose *compose)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
for (guint i = 0; i < priv->results->len; i++) {
g_autoptr(GPtrArray) hints = NULL;
AscResult *res = ASC_RESULT (g_ptr_array_index (priv->results, i));
hints = asc_result_fetch_hints_all (res);
for (guint j = 0; j < hints->len; j++) {
AscHint *hint = ASC_HINT (g_ptr_array_index (hints, j));
if (asc_hint_is_error (hint))
return TRUE;
}
}
return FALSE;
}
typedef struct {
AscUnit *unit;
AscResult *result;
GHashTable *files_units_map; /* no ref */
} AscComposeTask;
static AscComposeTask *
asc_compose_task_new (AscUnit *unit)
{
AscComposeTask *ctask;
ctask = g_new0 (AscComposeTask, 1);
ctask->unit = g_object_ref (unit);
ctask->result = asc_result_new ();
return ctask;
}
static void
asc_compose_task_free (AscComposeTask *ctask)
{
g_object_unref (ctask->unit);
g_object_unref (ctask->result);
g_free (ctask);
}
/**
* asc_compose_find_icon_filename:
*/
static gchar *
asc_compose_find_icon_filename (AscCompose *compose,
AscUnit *unit,
const gchar *icon_name,
guint icon_size,
guint icon_scale)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
guint min_size_idx = 0;
guint min_ext_idx = 0;
gboolean vector_relaxed = FALSE;
gboolean scale_relaxed = FALSE;
/* clang-format off */
const gchar *supported_ext[] = { ".png", ".svg", ".svgz", "", NULL };
const struct {
guint size;
const gchar *size_str;
} sizes[] = {
{ 48, "48x48" },
{ 32, "32x32" },
{ 64, "64x64" },
{ 96, "96x96" },
{ 128, "128x128" },
{ 256, "256x256" },
{ 512, "512x512" },
{ 0, "scalable"},
{ 0, NULL }
};
const gchar *types[] = { "actions", "apps", "applets", "categories", "devices",
"emblems", "emotes", "filesystems", "mimetypes", "places",
"preferences", "status", "stock", NULL };
/* clang-format on */
g_return_val_if_fail (icon_name != NULL, NULL);
/* fallbacks & sanitizations */
if (icon_scale <= 0)
icon_scale = 1;
if (icon_size > 512)
icon_size = 512;
/* is this an absolute path */
if (icon_name[0] == '/') {
g_autofree gchar *tmp = NULL;
tmp = g_build_filename (priv->prefix, icon_name, NULL);
if (!asc_unit_file_exists (unit, tmp))
return NULL;
return g_strdup (tmp);
}
/* select minimum size */
for (guint i = 0; sizes[i].size_str != NULL; i++) {
if (sizes[i].size >= icon_size) {
min_size_idx = i;
break;
}
}
while (TRUE) {
/* hicolor icon theme search */
for (guint i = min_size_idx; sizes[i].size_str != NULL; i++) {
g_autofree gchar *size = NULL;
if (icon_scale == 1)
size = g_strdup (sizes[i].size_str);
else
size = g_strdup_printf ("%s@%i", sizes[i].size_str, icon_scale);
for (guint m = 0; types[m] != NULL; m++) {
for (guint j = min_ext_idx; supported_ext[j] != NULL; j++) {
g_autofree gchar *tmp = NULL;
/* skip bitmaps if we only want vector graphics anyway */
if ((scale_relaxed || vector_relaxed) &&
as_str_equal0 (supported_ext[j], ".png"))
continue;
tmp = g_strdup_printf ("%s/share/icons/"
"hicolor/%s/%s/%s%s",
priv->prefix,
size,
types[m],
icon_name,
supported_ext[j]);
if (asc_unit_file_exists (unit, tmp))
return g_strdup (tmp);
}
}
}
/* breeze icon theme search, for KDE Plasma compatibility */
for (guint i = min_size_idx; sizes[i].size_str != NULL; i++) {
g_autofree gchar *size = NULL;
if (icon_scale == 1)
size = g_strdup_printf ("%i", sizes[i].size);
else
size = g_strdup_printf ("%i@%i", sizes[i].size, icon_scale);
for (guint m = 0; types[m] != NULL; m++) {
for (guint j = min_ext_idx; supported_ext[j] != NULL; j++) {
g_autofree gchar *tmp = NULL;
/* skip bitmaps if we only want vector graphica anyway */
if ((scale_relaxed || vector_relaxed) &&
as_str_equal0 (supported_ext[j], ".png"))
continue;
tmp = g_strdup_printf ("%s/share/icons/"
"breeze/%s/%s/%s%s",
priv->prefix,
types[m],
size,
icon_name,
supported_ext[j]);
if (asc_unit_file_exists (unit, tmp))
return g_strdup (tmp);
}
}
}
if (vector_relaxed) {
if (scale_relaxed || icon_scale == 1) {
break;
} else {
/* check if we can scale up a vector graphic to match the requested size */
min_size_idx = 0;
min_ext_idx = 1;
icon_size = icon_size * icon_scale;
icon_scale = 1;
}
} else {
if (g_str_has_suffix (icon_name, ".png"))
break;
/* try again, searching for vector graphics that we can scale up */
vector_relaxed = TRUE;
min_size_idx = 0;
/* start at index 1, where the SVG icons are */
min_ext_idx = 1;
}
}
/* failed to find any icon */
return NULL;
}
static void
asc_compose_process_icons (AscCompose *compose,
AscResult *cres,
AsComponent *cpt,
AscUnit *unit,
const gchar *icon_export_dir)
{
AscComposePrivate *priv = GET_PRIVATE (compose);
GPtrArray *icons = NULL;
g_autoptr(AsIcon) stock_icon = NULL;
const gchar *icon_name = NULL;
AscIconPolicyIter iter;
guint size;
guint scale_factor;
AscIconState icon_state;
/* do nothing if we have no icons to process */
icons = as_component_get_icons (cpt);
if (icons == NULL || icons->len == 0)
return;
/* find a suitable stock icon as template */
for (guint i = 0; i < icons->len; i++) {
AsIcon *icon = AS_ICON (g_ptr_array_index (icons, i));
if (as_icon_get_kind (icon) == AS_ICON_KIND_STOCK) {
stock_icon = icon;
break;
}
/* we cheat here to accomodate for apps which used the "local" icon type wrong */
if (as_icon_get_kind (icon) == AS_ICON_KIND_LOCAL)
stock_icon = icon;
}
/* drop all preexisting icons */
if (stock_icon != NULL)
stock_icon = g_object_ref (stock_icon);
g_ptr_array_set_size (as_component_get_icons (cpt), 0);
if (stock_icon == NULL) {
asc_result_add_hint_simple (cres, cpt, "no-stock-icon");
return;
}
icon_name = as_icon_get_name (stock_icon);
if (as_is_empty (icon_name) || icon_name[0] == ' ') {
/* and invalid stock icon is like having none at all */
asc_result_add_hint_simple (cres, cpt, "no-stock-icon");
return;
}
asc_icon_policy_iter_init (&iter, priv->icon_policy);
while (asc_icon_policy_iter_next (&iter, &size, &scale_factor, &icon_state)) {
g_autofree gchar *icon_fname = NULL;
g_autofree gchar *res_icon_fname = NULL;
g_autofree gchar *res_icon_size_str = NULL;
g_autofree gchar *res_icon_sizedir = NULL;
g_autofree gchar *res_icon_basename = NULL;
g_autoptr(AscImage) img = NULL;
g_autoptr(AsIcon) icon = NULL;
g_autoptr(GBytes) img_bytes = NULL;
gboolean is_vector_icon = FALSE;
const void *img_data;
gsize img_len;