[gnome-games/wip/exalm/db: 2/9] media: Allow serializing with GVariant



commit a3b898132a00ef213ab6809be0b6d827cd06fe27
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]