[PATCH 1/6] Add a serializing function for medias



It returns a string that represent a media.

Lot of times, having a "serialized" version of a media is very useful. For
instance, one can save this serial in a list, or send it to a non-grilo
compliant application.

Then, the media can be recovered from the serial.

Serial follows the same scheme as an URI:

  <type>://<escaped_source_name>/<escaped_id>

As it can be seen, both "id" and "source" are required in media in order to
build the serial.

<type> denotes the type of media we are dealing with. Thus, we will have:

   GrlMedia      => "grl"
   GrlMediaAudio => "grlaudio"
   GrlMediaVideo => "grlvideo"
   GrlMediaBox   => "grlbox"
   GrlMediaImage => "grlimage"

It is worth to tell that the implementation takes in account future new medias.
Thus, if in future a GrlMediaFoo type is added, then <type> will be "grlfoo"
for this new media, without needing changes in code.
---
 src/data/grl-media.c |   35 +++++++++++++++++++++++++++++++++++
 src/data/grl-media.h |    3 +++
 2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/src/data/grl-media.c b/src/data/grl-media.c
index 50310d1..4a85103 100644
--- a/src/data/grl-media.c
+++ b/src/data/grl-media.c
@@ -103,3 +103,38 @@ grl_media_set_rating (GrlMedia *media, gfloat rating, gfloat max)
 		      GRL_METADATA_KEY_RATING,
 		      normalized_value);
 }
+
+gchar *
+grl_media_serialize (GrlMedia *media)
+{
+  GRegex *type_regex;
+  const gchar *id;
+  const gchar *source;
+  const gchar *type_name;
+  gchar *escaped_id;
+  gchar *escaped_source;
+  gchar *protocol;
+  gchar *serial;
+
+  g_return_val_if_fail (GRL_IS_MEDIA (media), NULL);
+  g_return_val_if_fail ((id = grl_media_get_id (media)), NULL);
+  g_return_val_if_fail ((source = grl_media_get_source (media)), NULL);
+
+  type_name = g_type_name (G_TYPE_FROM_INSTANCE (media));
+
+  /* Convert typename to scheme protocol */
+  type_regex = g_regex_new ("GrlMedia(.*)", 0, 0, NULL);
+  protocol = g_regex_replace (type_regex, type_name, -1, 0, "grl\\L\\1\\E", 0, NULL);
+  g_regex_unref (type_regex);
+
+  /* Build serial string with escaped components */
+  escaped_id = g_uri_escape_string (id, NULL, TRUE);
+  escaped_source = g_uri_escape_string (source, NULL, TRUE);
+
+  serial = g_strconcat (protocol, "://", escaped_source, "/", escaped_id, NULL);
+
+  g_free (escaped_id);
+  g_free (escaped_source);
+
+  return serial;
+}
diff --git a/src/data/grl-media.h b/src/data/grl-media.h
index 627ec35..adcaccd 100644
--- a/src/data/grl-media.h
+++ b/src/data/grl-media.h
@@ -384,8 +384,11 @@ void grl_media_set_rating (GrlMedia *media, gfloat rating, gfloat max);
   grl_data_get_string(GRL_DATA((data)), GRL_METADATA_KEY_LAST_PLAYED)
 
 GType grl_media_get_type (void) G_GNUC_CONST;
+
 GrlMedia *grl_media_new (void);
 
+gchar *grl_media_serialize (GrlMedia *media);
+
 G_END_DECLS
 
 #endif /* _GRL_MEDIA_H_ */
-- 
1.7.0.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]