Ejemplo n.º 1
0
static gchar *
get_lyrics (GrlSource *source,
            const gchar *artist,
            const gchar *title)
{
  GList *keys;
  GrlMedia *audio;
  GrlOperationOptions *options;
  GError *error = NULL;
  gchar *lyrics;

  audio = grl_media_audio_new ();
  grl_media_set_artist (audio, artist);
  grl_media_set_title (audio, title);

  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_LYRICS, NULL);
  options = grl_operation_options_new (NULL);
  grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL);

  grl_source_resolve_sync (source,
                           GRL_MEDIA (audio),
                           keys,
                           options,
                           &error);
  g_assert_no_error (error);

  lyrics = g_strdup (grl_media_get_lyrics (audio));

  g_list_free (keys);
  g_object_unref (options);
  g_object_unref (audio);
  return lyrics;
}
Ejemplo n.º 2
0
static void
source_browser (gpointer data,
                gpointer user_data)
{
  GrlSource *source = GRL_SOURCE (data);
  GrlMedia *media = GRL_MEDIA (user_data);
  GList *media_elements;
  GError *error = NULL;
  GList *keys;
  GrlOperationOptions *options = NULL;
  GrlCaps *caps;

  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE,
                                    GRL_METADATA_KEY_URL,
                                    GRL_METADATA_KEY_MODIFICATION_DATE,
                                    GRL_METADATA_KEY_MIME,
                                    GRL_METADATA_KEY_CHILDCOUNT,
                                    NULL);

  g_debug ("Detected new source available: '%s'",
	   grl_source_get_name (source));

  if (!(grl_source_supported_operations (source) & GRL_OP_BROWSE))
    goto out;

  g_debug ("Browsing source: %s", grl_source_get_name (source));
  /* Here is how you can browse a source, you have to provide:
     1) The source you want to browse contents from.
     2) The container object you want to browse (NULL for the root container)
     3) A list of metadata keys we are interested in.
     4) Options to control certain aspects of the browse operation.
     5) A callback that the framework will invoke for each available result
     6) User data for the callback
     It returns an operation identifier that you can use to match results
     with the corresponding request (we ignore it here) */

  caps = grl_source_get_caps (source, GRL_OP_BROWSE);
  options = grl_operation_options_new (caps);
  grl_operation_options_set_count (options, BROWSE_CHUNK_SIZE);
  grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_IDLE_RELAY);
  media_elements = grl_pls_browse_sync (GRL_SOURCE (source),
                                        media, keys,
                                        options,
                                        NULL,
                                        &error);
  if (!media_elements) {
    g_debug ("No elements found for source: %s!",
             grl_source_get_name (source));
    goto out;
  }

  if (error)
    g_error ("Failed to browse source: %s", error->message);

  g_list_foreach (media_elements, element_browser, source);

out:
  g_list_free (keys);
  g_clear_object (&options);
}
static GHashTable *
get_properties_cb (MS2Server *server,
                   const gchar *id,
                   const gchar **properties,
                   gpointer data,
                   GError **error)
{
  GHashTable *properties_table = NULL;
  GrlMedia *media;
  GriloMs2Data *grdata;

  grdata = g_slice_new0 (GriloMs2Data);
  grdata->server = g_object_ref (server);
  grdata->source = (GrlSource *) data;
  grdata->options = grl_operation_options_new (NULL);
  grdata->keys = get_grilo_keys (properties, &grdata->other_keys);

  grl_operation_options_set_resolution_flags (grdata->options,
                                              GRL_RESOLVE_FULL |
                                              GRL_RESOLVE_IDLE_RELAY);
  media = unserialize_media (grdata->source, id);

  if (grdata->keys) {
    grl_source_resolve (grdata->source,
                        media,
                        grdata->keys,
                        grdata->options,
                        resolve_cb,
                        grdata);
  } else {
    resolve_cb (grdata->source, 0, media, grdata, NULL);
  }

  wait_for_result (grdata);

  if (grdata->error) {
    if (error) {
      *error = grdata->error;
    }
  } else {
    properties_table = grdata->properties;
  }

  g_object_unref (media);
  g_list_free (grdata->keys);
  g_list_free (grdata->other_keys);
  g_free (grdata->parent_id);
  g_object_unref (grdata->server);
  g_object_unref (grdata->options);
  g_slice_free (GriloMs2Data, grdata);

  return properties_table;
}
static void
set_test_data (GrlSource **source,
               GrlMedia **media,
               GrlOperationOptions **options,
               GList **keys,
               GrlSupportedOps source_op)
{
  *source = test_lua_factory_get_source (FAKE_SOURCE_ID, source_op);
  g_assert_nonnull (*source);

  *media = grl_media_new ();
  grl_data_add_string (GRL_DATA (*media), GRL_METADATA_KEY_URL, LOCAL_CONTENT);

  *keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE, NULL);
  *options = grl_operation_options_new (NULL);
  grl_operation_options_set_resolution_flags (*options, GRL_RESOLVE_NORMAL);
}
static GList *
search_objects_cb (MS2Server *server,
                   const gchar *id,
                   const gchar *query,
                   guint offset,
                   guint max_count,
                   const gchar **properties,
                   gpointer data,
                   GError **error)
{
  GList *objects;
  GriloMs2Data *grdata;
  gint count;

  /* Browse is only allowed in root container */
  if (g_strcmp0 (id, MS2_ROOT) != 0) {
    if (error) {
      /* FIXME: a better error should be reported */
      *error = g_error_new (0, 0, "search is only allowed in root container");
    }
    return NULL;
  }

  grdata = g_slice_new0 (GriloMs2Data);
  grdata->server = g_object_ref (server);
  grdata->source = (GrlSource *) data;
  grdata->options = grl_operation_options_new (NULL);
  grdata->keys = get_grilo_keys (properties, &grdata->other_keys);
  grdata->parent_id = g_strdup (id);
  grdata->list_type = LIST_ALL;

  grl_operation_options_set_resolution_flags (grdata->options,
                                              GRL_RESOLVE_FULL |
                                              GRL_RESOLVE_IDLE_RELAY);

  /* Adjust limits */
  if (offset >= limit) {
    browse_cb (grdata->source, 0, NULL, 0, grdata, NULL);
  } else {
    count = max_count == 0? (limit - offset): CLAMP (max_count,
                                                     1,
                                                     limit - offset);
    grl_operation_options_set_count (grdata->options, count);
    grl_operation_options_set_skip (grdata->options, offset);
    grl_source_search (grdata->source,
                       query,
                       grdata->keys,
                       grdata->options,
                       browse_cb,
                       grdata);
  }

  wait_for_result (grdata);

  if (grdata->error) {
    if (error) {
      *error = grdata->error;
    }
    g_list_foreach (grdata->children, (GFunc) g_hash_table_unref, NULL);
    g_list_free (grdata->children);
    objects = NULL;
  } else {
    objects = grdata->children;
  }

  g_list_free (grdata->keys);
  g_list_free (grdata->other_keys);
  g_free (grdata->parent_id);
  g_object_unref (grdata->server);
  g_object_unref (grdata->options);
  g_slice_free (GriloMs2Data, grdata);

  return objects;
}
static GList *
list_children_cb (MS2Server *server,
                  const gchar *id,
                  ListType list_type,
                  guint offset,
                  guint max_count,
                  const gchar **properties,
                  gpointer data,
                  GError **error)
{
  GList *children;
  GrlMedia *media;
  GriloMs2Data *grdata;
  gint count;

  grdata = g_slice_new0 (GriloMs2Data);
  grdata->server = g_object_ref (server);
  grdata->source = (GrlSource *) data;
  grdata->options = grl_operation_options_new (NULL);
  grdata->keys = get_grilo_keys (properties, &grdata->other_keys);
  grdata->parent_id = g_strdup (id);
  grdata->offset = offset;
  grdata->list_type = list_type;

  grl_operation_options_set_resolution_flags (grdata->options,
                                              GRL_RESOLVE_FULL |
                                              GRL_RESOLVE_IDLE_RELAY);

  media = unserialize_media (grdata->source, id);

  /* Adjust limits */
  if (offset >= limit) {
    browse_cb (grdata->source, 0, NULL, 0, grdata, NULL);
  } else {
    /* TIP: as Grilo is not able to split containers and items, in this case we
       will ask for all elements and then remove unneeded children in callback */
    switch (list_type) {
    case LIST_ALL:
      count = max_count == 0? (limit - offset): CLAMP (max_count,
                                                       1,
                                                       limit - offset);
      grl_operation_options_set_count (grdata->options, count);
      grl_operation_options_set_skip (grdata->options, offset);
      grdata->operation_id = grl_source_browse (grdata->source,
                                                media,
                                                grdata->keys,
                                                grdata->options,
                                                browse_cb,
                                                grdata);
      break;
    case LIST_CONTAINERS:
    case LIST_ITEMS:
      count = max_count == 0? limit: max_count;
      grl_operation_options_set_count  (grdata->options, count);
      grl_operation_options_set_skip (grdata->options, 0);
      grdata->operation_id = grl_source_browse (grdata->source,
                                                media,
                                                grdata->keys,
                                                grdata->options,
                                                browse_cb,
                                                grdata);
      break;
    default:
      /* Protection. It should never be reached, unless ListType is extended */
      browse_cb (grdata->source, 0, NULL, 0, grdata, NULL);
    }
  }

  wait_for_result (grdata);

  if (grdata->error) {
    if (error) {
      *error = grdata->error;
    }
    g_list_foreach (grdata->children, (GFunc) g_hash_table_unref, NULL);
    g_list_free (grdata->children);
    children = NULL;
  } else {
    children = grdata->children;
  }

  g_object_unref (media);
  g_list_free (grdata->keys);
  g_list_free (grdata->other_keys);
  g_free (grdata->parent_id);
  g_object_unref (grdata->server);
  g_object_unref (grdata->options);
  g_slice_free (GriloMs2Data, grdata);

  return children;
}