[gnome-games/wip/exalm/db: 22/29] media: Allow serializing with GVariant
- From: Alexander Mikhaylenko <alexm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/exalm/db: 22/29] media: Allow serializing with GVariant
- Date: Mon, 10 Feb 2020 18:36:15 +0000 (UTC)
commit 50bf73408980bf8e727db17d61430178d545ae64
Author: Alexander Mikhaylenko <alexm gnome org>
Date: Mon Feb 10 19:28:59 2020 +0500
media: Allow serializing with GVariant
This will be used to serialize MediaSet.
src/core/media.vala | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
---
diff --git a/src/core/media.vala b/src/core/media.vala
index 292bf5fe..c6e1d36e 100644
--- a/src/core/media.vala
+++ b/src/core/media.vala
@@ -10,6 +10,23 @@ public class Games.Media : Object {
this.uris = {};
}
+ public Media.parse (Variant variant) {
+ assert (get_variant_type ().equal (variant.get_type ()));
+
+ var title_child = variant.get_child_value (0);
+ var uris_child = variant.get_child_value (1);
+
+ var maybe_title = title_child.get_maybe ();
+ if (maybe_title != null)
+ title = new GenericTitle (maybe_title.get_string ());
+
+ uris = {};
+ for (int i = 0; i < uris_child.n_children (); i++) {
+ var uri = uris_child.get_child_value (i).get_string ();
+ uris += new Uri (uri);
+ }
+ }
+
public Uri[] get_uris () {
return uris;
}
@@ -17,4 +34,31 @@ public class Games.Media : Object {
public void add_uri (Uri uri) {
uris += uri;
}
+
+ public Variant serialize () {
+ Variant? title_variant = null;
+ if (title != null)
+ try {
+ title_variant = new Variant.string (title.get_title ());
+ }
+ catch (Error e) {
+ critical ("Couldn't get title: %s", e.message);
+ }
+
+ Variant[] uri_variants = {};
+ foreach (var uri in uris)
+ uri_variants += new Variant.string (uri.to_string ());
+
+ return new Variant.tuple ({
+ new Variant.maybe (VariantType.STRING, title_variant),
+ new Variant.array (VariantType.STRING, uri_variants)
+ });
+ }
+
+ public static VariantType get_variant_type () {
+ return new VariantType.tuple ({
+ new VariantType.maybe (VariantType.STRING),
+ new VariantType.array (VariantType.STRING)
+ });
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]