~ubuntu-branches/ubuntu/raring/grilo/raring

« back to all changes in this revision

Viewing changes to examples/browsing.c

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Garcia
  • Date: 2011-07-02 13:48:46 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110702134846-u10hi90nwf6wf3v0
Tags: 0.1.16-1
* New upstream release.
* debian/{grl-inspect.1,libgrilo-0.1-0.manpages,libgrilo-0.1-0.install}:
  use manpage shipped by upstream.
* debian/libgrilo-0.1-0.shlibs: new API, bump shlibs to 0.1.16.
* debian/copyright: Author(s) => Authors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Browsing in Grilo.
 
4
 * Shows the first 5 elements of each browsable source
 
5
 */
 
6
 
 
7
#include <grilo.h>
 
8
 
 
9
#define GRL_LOG_DOMAIN_DEFAULT  example_log_domain
 
10
GRL_LOG_DOMAIN_STATIC(example_log_domain);
 
11
 
 
12
/* This callback is invoked for each result that matches our
 
13
   browse operation. The arguments are:
 
14
   1) The source we obtained the content from.
 
15
   2) The operation identifier this result relates to.
 
16
   3) A media object representing content that matched the browse operation.
 
17
   4) Estimation of the number of remaining media objects that will be sent
 
18
   after this one as part of the same resultset (0 means that the browse
 
19
   operation is finished).
 
20
   5) User data passed to the grl_media_source_browse method.
 
21
   6) A GError if an error happened, NULL otherwise */
 
22
static void
 
23
browse_cb (GrlMediaSource *source,
 
24
           guint browse_id,
 
25
           GrlMedia *media,
 
26
           guint remaining,
 
27
           gpointer user_data,
 
28
           const GError *error)
 
29
{
 
30
  /* First we check if the operation failed for some reason */
 
31
  if (error) {
 
32
    g_error ("Browse operation failed. Reason: %s", error->message);
 
33
  }
 
34
 
 
35
  /* Check if we got a valid media object as some plugins may call the callback
 
36
     with a NULL media under certain circumstances (for example when they
 
37
     cannot estimate the number of remaining results and they find suddenly they
 
38
     don't have any more results to send) */
 
39
  if (media) {
 
40
    /* Get the metadata we are interested in */
 
41
    const gchar *title = grl_media_get_title (media);
 
42
 
 
43
    /* If the media is a container (box) that means we could
 
44
       browse it again (that is, we could use it as the second parameter
 
45
       of the grl_media_source_browse method) */
 
46
    if (GRL_IS_MEDIA_BOX (media)) {
 
47
      guint childcount = grl_media_box_get_childcount (GRL_MEDIA_BOX (media));
 
48
      g_debug ("\t Got '%s' (container with %d elements)", title, childcount);
 
49
    } else {
 
50
      guint seconds = grl_media_get_duration (media);
 
51
      const gchar *url = grl_media_get_url (media);
 
52
      g_debug ("\t Got '%s' (media - length: %d seconds)", title, seconds);
 
53
      g_debug ("\t\t URL: %s", url);
 
54
    }
 
55
    g_object_unref (media);
 
56
  }
 
57
 
 
58
  /* Check if this was the last result */
 
59
  if (remaining == 0) {
 
60
    g_debug ("Browse operation finished!");
 
61
  }
 
62
}
 
63
 
 
64
static void
 
65
source_added_cb (GrlPluginRegistry *registry, gpointer user_data)
 
66
{
 
67
  static gboolean first = TRUE;
 
68
  GrlMetadataSource *source = GRL_METADATA_SOURCE (user_data);
 
69
  GList * keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE,
 
70
                                            GRL_METADATA_KEY_DURATION,
 
71
                                            GRL_METADATA_KEY_URL,
 
72
                                            GRL_METADATA_KEY_CHILDCOUNT,
 
73
                                            NULL);
 
74
  g_debug ("Detected new source available: '%s'",
 
75
           grl_metadata_source_get_name (source));
 
76
 
 
77
  /* We will just issue a browse operation on the first browseble
 
78
     source we find */
 
79
  if (first &&
 
80
      grl_metadata_source_supported_operations (source) & GRL_OP_BROWSE) {
 
81
    first = FALSE;
 
82
    g_debug ("Browsing source: %s", grl_metadata_source_get_name (source));
 
83
    /* Here is how you can browse a source, you have to provide:
 
84
       1) The source you want to browse contents from.
 
85
       2) The container object you want to browse (NULL for the root container)
 
86
       3) A list of metadata keys we are interested in.
 
87
       4) Flags to control certain aspects of the browse operation.
 
88
       5) A callback that the framework will invoke for each available result
 
89
       6) User data for the callback
 
90
       It returns an operation identifier that you can use to match results
 
91
       with the corresponding request (we ignore it here) */
 
92
    grl_media_source_browse (GRL_MEDIA_SOURCE (source),
 
93
                             NULL,
 
94
                             keys,
 
95
                             0, 5,
 
96
                             GRL_RESOLVE_IDLE_RELAY,
 
97
                             browse_cb,
 
98
                             NULL);
 
99
  }
 
100
 
 
101
  g_list_free (keys);
 
102
}
 
103
 
 
104
static void
 
105
load_plugins (void)
 
106
{
 
107
  GrlPluginRegistry *registry;
 
108
  GError *error = NULL;
 
109
 
 
110
  registry = grl_plugin_registry_get_default ();
 
111
  g_signal_connect (registry, "source-added",
 
112
                    G_CALLBACK (source_added_cb), NULL);
 
113
  if (!grl_plugin_registry_load_all (registry, &error)) {
 
114
    g_error ("Failed to load plugins: %s", error->message);
 
115
  }
 
116
}
 
117
 
 
118
gint
 
119
main (int argc, gchar *argv[])
 
120
{
 
121
  GMainLoop *loop;
 
122
  grl_init (&argc, &argv);
 
123
  GRL_LOG_DOMAIN_INIT (example_log_domain, "example");
 
124
  load_plugins ();
 
125
  loop = g_main_loop_new (NULL, FALSE);
 
126
  g_main_loop_run (loop);
 
127
  return 0;
 
128
}