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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:as-icon
* @short_description: Object representing a single icon used in a screenshot.
* @include: appstream-glib.h
* @stability: Stable
*
* Screenshot may have multiple versions of an icon in different resolutions
* or aspect ratios. This object allows access to the location and size of a
* single icon.
*
* See also: #AsScreenshot
*/
#include "config.h"
#include "as-icon-private.h"
#include "as-node-private.h"
#include "as-utils-private.h"
#include "as-yaml.h"
typedef struct
{
AsIconKind kind;
gchar *name;
gchar *url;
gchar *filename;
gchar *prefix;
gchar *prefix_private;
guint width;
guint height;
GdkPixbuf *pixbuf;
GBytes *data;
} AsIconPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AsIcon, as_icon, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (as_icon_get_instance_private (o))
/**
* as_icon_error_quark:
*
* Return value: An error quark.
*
* Since: 0.3.1
**/
G_DEFINE_QUARK (as-icon-error-quark, as_icon_error)
/**
* as_icon_finalize:
**/
static void
as_icon_finalize (GObject *object)
{
AsIcon *icon = AS_ICON (object);
AsIconPrivate *priv = GET_PRIVATE (icon);
if (priv->pixbuf != NULL)
g_object_unref (priv->pixbuf);
if (priv->data != NULL)
g_bytes_unref (priv->data);
g_free (priv->name);
g_free (priv->url);
g_free (priv->filename);
g_free (priv->prefix);
g_free (priv->prefix_private);
G_OBJECT_CLASS (as_icon_parent_class)->finalize (object);
}
/**
* as_icon_init:
**/
static void
as_icon_init (AsIcon *icon)
{
}
/**
* as_icon_class_init:
**/
static void
as_icon_class_init (AsIconClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = as_icon_finalize;
}
/**
* as_icon_kind_to_string:
* @icon_kind: the @AsIconKind.
*
* Converts the enumerated value to an text representation.
*
* Returns: string version of @icon_kind
*
* Since: 0.1.0
**/
const gchar *
as_icon_kind_to_string (AsIconKind icon_kind)
{
if (icon_kind == AS_ICON_KIND_CACHED)
return "cached";
if (icon_kind == AS_ICON_KIND_STOCK)
return "stock";
if (icon_kind == AS_ICON_KIND_REMOTE)
return "remote";
if (icon_kind == AS_ICON_KIND_EMBEDDED)
return "embedded";
if (icon_kind == AS_ICON_KIND_LOCAL)
return "local";
return "unknown";
}
/**
* as_icon_kind_from_string:
* @icon_kind: the string.
*
* Converts the text representation to an enumerated value.
*
* Returns: a #AsIconKind or %AS_ICON_KIND_UNKNOWN for unknown
*
* Since: 0.1.0
**/
AsIconKind
as_icon_kind_from_string (const gchar *icon_kind)
{
if (g_strcmp0 (icon_kind, "cached") == 0)
return AS_ICON_KIND_CACHED;
if (g_strcmp0 (icon_kind, "stock") == 0)
return AS_ICON_KIND_STOCK;
if (g_strcmp0 (icon_kind, "remote") == 0)
return AS_ICON_KIND_REMOTE;
if (g_strcmp0 (icon_kind, "embedded") == 0)
return AS_ICON_KIND_EMBEDDED;
if (g_strcmp0 (icon_kind, "local") == 0)
return AS_ICON_KIND_LOCAL;
return AS_ICON_KIND_UNKNOWN;
}
/**
* as_icon_get_name:
* @icon: a #AsIcon instance.
*
* Gets the name of the icon, e.g. "epiphany.png"
*
* Returns: the basename of the icon
*
* Since: 0.3.1
**/
const gchar *
as_icon_get_name (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->name;
}
/**
* as_icon_get_url:
* @icon: a #AsIcon instance.
*
* Gets the full qualified URL for the icon, usually pointing at some mirror.
* NOTE: This is only set for icons of type %AS_ICON_KIND_REMOTE
*
* Returns: the fully qualified URL
*
* Since: 0.3.2
**/
const gchar *
as_icon_get_url (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->url;
}
/**
* as_icon_get_filename:
* @icon: a #AsIcon instance.
*
* Gets the absolute path on disk of the icon.
* NOTE: This is only set for icons of type %AS_ICON_KIND_LOCAL
*
* Returns: the absolute filename on disk
*
* Since: 0.3.2
**/
const gchar *
as_icon_get_filename (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->filename;
}
/**
* as_icon_get_prefix:
* @icon: a #AsIcon instance.
*
* Gets the suggested prefix of the icon.
*
* Returns: filename
*
* Since: 0.1.6
**/
const gchar *
as_icon_get_prefix (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
if (priv->prefix_private != NULL)
return priv->prefix_private;
return priv->prefix;
}
/**
* as_icon_get_width:
* @icon: a #AsIcon instance.
*
* Gets the icon width.
*
* Returns: width in pixels
*
* Since: 0.3.1
**/
guint
as_icon_get_width (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->width;
}
/**
* as_icon_get_height:
* @icon: a #AsIcon instance.
*
* Gets the icon height.
*
* Returns: height in pixels
*
* Since: 0.3.1
**/
guint
as_icon_get_height (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->height;
}
/**
* as_icon_get_kind:
* @icon: a #AsIcon instance.
*
* Gets the icon kind.
*
* Returns: the #AsIconKind
*
* Since: 0.3.1
**/
AsIconKind
as_icon_get_kind (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->kind;
}
/**
* as_icon_get_pixbuf:
* @icon: a #AsIcon instance.
*
* Gets the icon pixbuf if set.
*
* Returns: (transfer none): the #GdkPixbuf, or %NULL
*
* Since: 0.3.1
**/
GdkPixbuf *
as_icon_get_pixbuf (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->pixbuf;
}
/**
* as_icon_get_data:
* @icon: a #AsIcon instance.
*
* Gets the icon data if set.
*
* Returns: (transfer none): the #GBytes, or %NULL
*
* Since: 0.3.1
**/
GBytes *
as_icon_get_data (AsIcon *icon)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
return priv->data;
}
/**
* as_icon_set_name:
* @icon: a #AsIcon instance.
* @name: the icon name, e.g. "gimp.png"
*
* Sets the basename to use for the icon.
*
* Since: 0.3.1
**/
void
as_icon_set_name (AsIcon *icon, const gchar *name)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_free (priv->name);
priv->name = g_strdup (name);
}
/**
* as_icon_set_prefix:
* @icon: a #AsIcon instance.
* @prefix: the new filename prefix.
*
* Sets the icon prefix filename.
*
* Since: 0.1.6
**/
void
as_icon_set_prefix (AsIcon *icon, const gchar *prefix)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_free (priv->prefix);
priv->prefix = g_strdup (prefix);
}
/**
* as_icon_set_url:
* @icon: a #AsIcon instance.
* @url: the new icon URL.
*
* Sets the icon URL.
*
* Since: 0.3.2
**/
void
as_icon_set_url (AsIcon *icon, const gchar *url)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_free (priv->url);
priv->url = g_strdup (url);
}
/**
* as_icon_set_filename:
* @icon: a #AsIcon instance.
* @filename: the new icon URL.
*
* Sets the icon absolute filename.
*
* Since: 0.3.2
**/
void
as_icon_set_filename (AsIcon *icon, const gchar *filename)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_free (priv->filename);
priv->filename = g_strdup (filename);
}
/**
* as_icon_set_width:
* @icon: a #AsIcon instance.
* @width: the width in pixels.
*
* Sets the icon width.
*
* Since: 0.3.1
**/
void
as_icon_set_width (AsIcon *icon, guint width)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
priv->width = width;
}
/**
* as_icon_set_height:
* @icon: a #AsIcon instance.
* @height: the height in pixels.
*
* Sets the icon height.
*
* Since: 0.3.1
**/
void
as_icon_set_height (AsIcon *icon, guint height)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
priv->height = height;
}
/**
* as_icon_set_kind:
* @icon: a #AsIcon instance.
* @kind: the #AsIconKind, e.g. %AS_ICON_KIND_STOCK.
*
* Sets the icon kind.
*
* Since: 0.3.1
**/
void
as_icon_set_kind (AsIcon *icon, AsIconKind kind)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
priv->kind = kind;
}
/**
* as_icon_set_pixbuf:
* @icon: a #AsIcon instance.
* @pixbuf: the #GdkPixbuf, or %NULL
*
* Sets the icon pixbuf.
*
* Since: 0.3.1
**/
void
as_icon_set_pixbuf (AsIcon *icon, GdkPixbuf *pixbuf)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_set_object (&priv->pixbuf, pixbuf);
if (pixbuf != NULL) {
priv->width = gdk_pixbuf_get_width (pixbuf);
priv->height = gdk_pixbuf_get_height (pixbuf);
}
}
/**
* as_icon_set_data:
* @icon: a #AsIcon instance.
* @data: the #GBytes, or %NULL
*
* Sets the icon data.
*
* Since: 0.3.1
**/
void
as_icon_set_data (AsIcon *icon, GBytes *data)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
if (priv->data != NULL)
g_bytes_unref (priv->data);
if (data == NULL) {
priv->data = NULL;
return;
}
priv->data = g_bytes_ref (data);
}
/**
* as_icon_node_insert_embedded:
**/
static GNode *
as_icon_node_insert_embedded (AsIcon *icon, GNode *parent, AsNodeContext *ctx)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
GNode *n;
g_autofree gchar *data = NULL;
/* embedded icon */
n = as_node_insert (parent, "icon", NULL, 0,
"type", as_icon_kind_to_string (priv->kind),
NULL);
if (as_node_context_get_version (ctx) >= 0.8) {
as_node_add_attribute_as_int (n, "width", priv->width);
as_node_add_attribute_as_int (n, "height", priv->height);
}
as_node_insert (n, "name", priv->name, 0, NULL);
data = g_base64_encode (g_bytes_get_data (priv->data, NULL),
g_bytes_get_size (priv->data));
as_node_insert (n, "filecontent", data,
AS_NODE_INSERT_FLAG_BASE64_ENCODED, NULL);
return n;
}
/**
* as_icon_node_insert: (skip)
* @icon: a #AsIcon instance.
* @parent: the parent #GNode to use..
* @ctx: the #AsNodeContext
*
* Inserts the icon into the DOM tree.
*
* Returns: (transfer none): A populated #GNode
*
* Since: 0.3.1
**/
GNode *
as_icon_node_insert (AsIcon *icon, GNode *parent, AsNodeContext *ctx)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
GNode *n;
/* embedded icon */
if (priv->kind == AS_ICON_KIND_EMBEDDED)
return as_icon_node_insert_embedded (icon, parent, ctx);
/* other icons */
switch (priv->kind) {
case AS_ICON_KIND_REMOTE:
n = as_node_insert (parent, "icon", priv->url, 0,
"type", as_icon_kind_to_string (priv->kind),
NULL);
break;
case AS_ICON_KIND_LOCAL:
n = as_node_insert (parent, "icon", priv->filename, 0,
"type", as_icon_kind_to_string (priv->kind),
NULL);
break;
default:
n = as_node_insert (parent, "icon", priv->name, 0,
"type", as_icon_kind_to_string (priv->kind),
NULL);
break;
}
if (priv->kind == AS_ICON_KIND_CACHED &&
as_node_context_get_version (ctx) >= 0.8) {
if (priv->width > 0)
as_node_add_attribute_as_int (n, "width", priv->width);
if (priv->height > 0)
as_node_add_attribute_as_int (n, "height", priv->height);
}
return n;
}
/**
* as_icon_node_parse_embedded:
**/
static gboolean
as_icon_node_parse_embedded (AsIcon *icon, GNode *n, GError **error)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
GNode *c;
gsize size;
g_autofree guchar *data = NULL;
g_autoptr(GdkPixbuf) pixbuf = NULL;
g_autoptr(GInputStream) stream = NULL;
/* get the icon name */
c = as_node_find (n, "name");
if (c == NULL) {
g_set_error_literal (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"embedded icons needs <name>");
return FALSE;
}
g_free (priv->name);
priv->name = as_node_take_data (c);
/* parse the Base64 data */
c = as_node_find (n, "filecontent");
if (c == NULL) {
g_set_error_literal (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"embedded icons needs <filecontent>");
return FALSE;
}
data = g_base64_decode (as_node_get_data (c), &size);
stream = g_memory_input_stream_new_from_data (data, (gssize) size, NULL);
if (stream == NULL) {
g_set_error_literal (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"failed to load embedded data");
return FALSE;
}
/* load the image */
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
if (pixbuf == NULL)
return FALSE;
as_icon_set_pixbuf (icon, pixbuf);
/* save the raw data */
if (priv->data != NULL)
g_bytes_unref (priv->data);
priv->data = g_bytes_new (data, size);
return TRUE;
}
/**
* as_icon_node_parse:
* @icon: a #AsIcon instance.
* @node: a #GNode.
* @ctx: a #AsNodeContext.
* @error: A #GError or %NULL.
*
* Populates the object from a DOM node.
*
* Returns: %TRUE for success
*
* Since: 0.3.1
**/
gboolean
as_icon_node_parse (AsIcon *icon, GNode *node,
AsNodeContext *ctx, GError **error)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
const gchar *tmp;
gint size;
gboolean prepend_size = TRUE;
tmp = as_node_get_attribute (node, "type");
as_icon_set_kind (icon, as_icon_kind_from_string (tmp));
switch (priv->kind) {
case AS_ICON_KIND_EMBEDDED:
if (!as_icon_node_parse_embedded (icon, node, error))
return FALSE;
break;
default:
/* preserve the URL for remote icons */
tmp = as_node_get_data (node);
if (priv->kind == AS_ICON_KIND_REMOTE)
as_icon_set_url (icon, tmp);
else if (priv->kind == AS_ICON_KIND_LOCAL)
as_icon_set_filename (icon, tmp);
/* store the name without any prefix */
if (g_strstr_len (tmp, -1, "/") == NULL) {
as_icon_set_name (icon, tmp);
} else {
g_autofree gchar *basename = NULL;
basename = g_path_get_basename (tmp);
as_icon_set_name (icon, basename);
}
/* width is optional, assume 64px if missing */
size = as_node_get_attribute_as_int (node, "width");
if (size == G_MAXINT) {
size = 64;
prepend_size = FALSE;
}
priv->width = size;
/* height is optional, assume 64px if missing */
size = as_node_get_attribute_as_int (node, "height");
if (size == G_MAXINT) {
size = 64;
prepend_size = FALSE;
}
priv->height = size;
/* only use the size if the metadata has width and height */
if (prepend_size) {
g_free (priv->prefix_private);
priv->prefix_private = g_strdup_printf ("%s/%ix%i",
priv->prefix,
priv->width,
priv->height);
}
break;
}
return TRUE;
}
/**
* as_icon_node_parse_dep11:
* @icon: a #AsIcon instance.
* @node: a #GNode.
* @ctx: a #AsNodeContext.
* @error: A #GError or %NULL.
*
* Populates the object from a DEP-11 node.
*
* Returns: %TRUE for success
*
* Since: 0.3.1
**/
gboolean
as_icon_node_parse_dep11 (AsIcon *im, GNode *node,
AsNodeContext *ctx, GError **error)
{
if (g_strcmp0 (as_yaml_node_get_key (node), "cached") == 0) {
as_icon_set_name (im, as_yaml_node_get_value (node));
as_icon_set_kind (im, AS_ICON_KIND_CACHED);
} else if (g_strcmp0 (as_yaml_node_get_key (node), "stock") == 0) {
as_icon_set_name (im, as_yaml_node_get_value (node));
as_icon_set_kind (im, AS_ICON_KIND_STOCK);
}
return TRUE;
}
/**
* as_icon_load:
* @icon: a #AsIcon instance.
* @flags: a #AsIconLoadFlags, e.g. %AS_ICON_LOAD_FLAG_SEARCH_SIZE
* @error: A #GError or %NULL.
*
* Loads the icon into a local pixbuf.
*
* Returns: %TRUE for success
*
* Since: 0.3.1
**/
gboolean
as_icon_load (AsIcon *icon, AsIconLoadFlags flags, GError **error)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
g_autofree gchar *fn_fallback = NULL;
g_autoptr(GdkPixbuf) pixbuf = NULL;
/* absolute filename */
if (priv->kind == AS_ICON_KIND_LOCAL) {
if (priv->filename == NULL) {
g_set_error (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"unable to load '%s' as no filename set",
priv->name);
return FALSE;
}
pixbuf = gdk_pixbuf_new_from_file_at_size (priv->filename,
priv->width,
priv->height,
error);
if (pixbuf == NULL)
return FALSE;
as_icon_set_pixbuf (icon, pixbuf);
return TRUE;
}
/* not set */
if (priv->prefix == NULL) {
g_set_error (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"unable to load '%s' as no prefix set",
priv->name);
return FALSE;
}
/* try getting a pixbuf of the right size */
if (flags & AS_ICON_LOAD_FLAG_SEARCH_SIZE) {
guint widths[] = { priv->width, 64, 128, 0 };
guint height[] = { priv->height, 64, 128, 0 };
guint i;
for (i = 0; widths[i] != 0; i++) {
g_autofree gchar *fn_size = NULL;
g_autofree gchar *size_str = NULL;
size_str = g_strdup_printf ("%ix%i", widths[i], height[i]);
fn_size = g_build_filename (priv->prefix, size_str, priv->name, NULL);
if (g_file_test (fn_size, G_FILE_TEST_EXISTS)) {
pixbuf = gdk_pixbuf_new_from_file (fn_size, error);
if (pixbuf == NULL)
return FALSE;
as_icon_set_pixbuf (icon, pixbuf);
return TRUE;
}
}
}
/* fall back to the old location */
fn_fallback = g_build_filename (priv->prefix, priv->name, NULL);
pixbuf = gdk_pixbuf_new_from_file (fn_fallback, error);
if (pixbuf == NULL)
return FALSE;
as_icon_set_pixbuf (icon, pixbuf);
return TRUE;
}
/**
* as_icon_convert_to_kind:
* @icon: a #AsIcon instance.
* @kind: a %AsIconKind, e.g. #AS_ICON_KIND_EMBEDDED
* @error: A #GError or %NULL.
*
* Converts the icon from one kind to another.
*
* Returns: %TRUE for success
*
* Since: 0.3.1
**/
gboolean
as_icon_convert_to_kind (AsIcon *icon, AsIconKind kind, GError **error)
{
AsIconPrivate *priv = GET_PRIVATE (icon);
/* these can't be converted */
if (priv->kind == AS_ICON_KIND_STOCK ||
priv->kind == AS_ICON_KIND_REMOTE)
return TRUE;
/* no change */
if (priv->kind == kind)
return TRUE;
/* cached -> embedded */
if (priv->kind == AS_ICON_KIND_CACHED && kind == AS_ICON_KIND_EMBEDDED) {
gsize data_size;
g_autoptr(GBytes) tmp = NULL;
g_autofree gchar *data = NULL;
/* load the pixbuf and save it to a PNG buffer */
if (priv->pixbuf == NULL) {
if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, error))
return FALSE;
}
if (!gdk_pixbuf_save_to_buffer (priv->pixbuf, &data, &data_size,
"png", error, NULL))
return FALSE;
/* set the PNG buffer to a blob of data */
tmp = g_bytes_new (data, data_size);
as_icon_set_data (icon, tmp);
as_icon_set_kind (icon, kind);
return TRUE;
}
/* cached -> embedded */
if (priv->kind == AS_ICON_KIND_EMBEDDED && kind == AS_ICON_KIND_CACHED) {
g_autofree gchar *size_str = NULL;
g_autofree gchar *path = NULL;
g_autofree gchar *fn = NULL;
/* ensure the parent path exists */
size_str = g_strdup_printf ("%ix%i", priv->width, priv->height);
path = g_build_filename (priv->prefix, size_str, NULL);
if (g_mkdir_with_parents (path, 0700) != 0) {
g_set_error (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"Failed to create: %s", path);
return FALSE;
}
/* save the pixbuf */
fn = g_build_filename (path, priv->name, NULL);
if (!gdk_pixbuf_save (priv->pixbuf, fn, "png", error, NULL))
return FALSE;
as_icon_set_kind (icon, kind);
return TRUE;
}
/* not supported */
g_set_error (error,
AS_ICON_ERROR,
AS_ICON_ERROR_FAILED,
"converting %s to %s is not supported",
as_icon_kind_to_string (priv->kind),
as_icon_kind_to_string (kind));
return FALSE;
}
/**
* as_icon_new:
*
* Creates a new #AsIcon.
*
* Returns: (transfer full): a #AsIcon
*
* Since: 0.3.1
**/
AsIcon *
as_icon_new (void)
{
AsIcon *icon;
icon = g_object_new (AS_TYPE_ICON, NULL);
return AS_ICON (icon);
}
|