dbus-glib on debian lenny.

Fujitaka Daidouji fujitaka.daidouji at gmail.com
Wed Apr 15 17:17:03 PDT 2009


---------- Forwarded message ----------
From: Fujitaka Daidouji <fujitaka.daidouji at gmail.com>
Date: Wed, Apr 15, 2009 at 7:04 PM
Subject: Re: dbus-glib on debian lenny.
To: Simon McVittie <simon.mcvittie at collabora.co.uk>


Thanks. Well. I managed to get my code alittle more functional. Made a xchat
version, and a c version for debuging the code.  But I have two values I
can't find the field type for. Your code

 /* the result of this function is only suitable for debug output */
     gchar *tmp = g_strdup_value_contents (val);

     printf ("%s -> unhandled type %s with value %s",
         key, G_VALUE_TYPE_NAME (value), tmp);
     g_free (tmp);
   }
It works, but prints NULL for the second %s.
The two fields I am tring to get the type of is filesize and length.
Running you code outputs.

file-size -> unahndled type (null) with value 17578504. I have tried to
guess the type such as int uint long etc... No Luck so I ended up using
ti.fs = g_strdup_value_contents (val);
But using that. It won't let me do any math on it. I want to take that
number it outputs which is ofcourse a filesize number and divide it by
1048576 for a value of 16.764168. Then I can do a sprintf("%1.2f, "var
here") on it to make out put 16.76 then take on MB.  I am not sure how to go
about getting the field type. hmm I even tried looking in the banshee-1
source code which is C#. I must be missing something here. x.x Maby you have
an answer.

///////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <dbus/dbus-glib.h>
#include <string.h>



const gchar *mt1a;

struct TrackInfo {
 const gchar *album;
 const gchar *artist;
 const gchar *name;
 const gchar *mt;
 const gchar *length;
 const gchar *br;
 const gchar *fs;
 };
struct TrackInfo ti;


static void get_value (GHashTable *table, gpointer k, gpointer data)
{
  //gpointer v;
  const GValue *value;
  const gchar *key = k;
  const GValue *val = g_hash_table_lookup(table, key);
  if (G_VALUE_HOLDS_STRING (val))
    {
       if (key == "album")
    {
    ti.album = g_value_get_string (val);
    printf("Album: %s\n", ti.album);
    }
    if (key == "artist")
    {
    ti.artist = g_value_get_string (val);
    printf("Artist: %s\n", ti.artist);
    }
    if (key == "name")
    {
    ti.name = g_value_get_string (val);
    printf("Track Title: %s\n", ti.name);
    }
    if (key == "mime-type")
    {
    ti.mt = g_value_get_string (val);
    char *mt1;
    mt1=strrchr(ti.mt,'/');
    if (mt1) mt1++; else mt1=ti.mt;
    mt1a=mt1;
    printf("Mime-Type: %s\n", mt1);
    }
    }
  if (G_VALUE_HOLDS_INT (val))
    {
    if (key == "bit-rate")
    {
    ti.br = g_value_get_int (val);
    printf("Bit-Rate: %u\n", ti.br);
    }

}
    if (key == "length")
    {
    ti.length = g_strdup_value_contents (val);
    printf("Length: %s\n", ti.length);
    }
    if (key == "file-size")
    {
    ti.fs = g_strdup_value_contents (val);
    printf("File-Size: %s\n", ti.fs);
    }
}

static void print_hash_value (gpointer k, gpointer v, gpointer data)
{
  const gchar *key = k;
  const GValue *val = v;
  const GValue *value;


  if (G_VALUE_HOLDS_STRING (val))
    {
      printf ("%s -> %s\n", key, g_value_get_string (val));
    }
  else if (G_VALUE_HOLDS_UINT (val))
    {
      printf ("%s -> %u\n", key, g_value_get_uint (val));
    }
  else if (G_VALUE_HOLDS_INT (val))
    {
      printf ("%s -> %u\n", key, g_value_get_int (val));
    }
  /* ... insert any more types you might need here ... */
  else
    {
      /* the result of this function is only suitable for debug output */
      gchar *tmp = g_strdup_value_contents (val);

      printf ("%s -> unhandled type %s with value %s\n",
          key, G_VALUE_TYPE (value), tmp);
      g_free (tmp);
    }
}


int main (int argc, char **argv)
{
  DBusGConnection *connection;
  GError *error;
  DBusGProxy *proxy;
  GHashTable* table;

  g_type_init ();

  error = NULL;
  connection = dbus_g_bus_get (DBUS_BUS_SESSION,
                               &error);
  if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  /* Create a proxy object for the "bus driver" (name
"org.bansheeproject.Banshee") */

  proxy = dbus_g_proxy_new_for_name (connection,
                                     "org.bansheeproject.Banshee",
                      "/org/bansheeproject/Banshee/PlayerEngine",

"org.bansheeproject.Banshee.PlayerEngine");

  /* Call GetCurrentTrack method, wait for reply */
  error = NULL;

  if (!dbus_g_proxy_call (proxy, "GetCurrentTrack", &error, G_TYPE_INVALID,
                           dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
G_TYPE_VALUE), &table,
                           G_TYPE_INVALID))
    {

      g_printerr ("Error: %s\n", error->message);
      g_error_free (error);
      exit (1);
    }

  /* Print the results */
get_value(table,"album", NULL);
get_value(table,"artist", NULL);
get_value(table,"name", NULL);
get_value(table,"length", NULL);
get_value(table,"bit-rate", NULL);
get_value(table,"file-size", NULL);
get_value(table,"mime-type", NULL);
//printf("Album: %s Artist: %s Title: %s [%s|%u Kbps|%s]\n", ti.album,
ti.artist, ti.name, ti.length, ti.br, mt1a);
//g_hash_table_foreach (table, print_hash_value, NULL);
//banshee_hash_str(table, "album", ti->album);

  g_object_unref (proxy);

  return 0;
}
////////////////////////////////////////////////////////////////////////////////


On Wed, Apr 15, 2009 at 6:24 AM, Simon McVittie <
simon.mcvittie at collabora.co.uk> wrote:

> On Tue, 14 Apr 2009 at 20:43:01 -0500, Fujitaka Daidouji wrote:
> > static void print_hash_value (gpointer key, gpointer val, gpointer data)
> > {
> >   printf ("%s -> %s\n", (char *) key, (char *) val);
> > }
>
> No, val points to a GValue, not a string. Try something like this:
>
> static void print_hash_value (gpointer k, gpointer v, gpointer data)
> {
>  const gchar *key = k;
>  const GValue *val = v;
>
>  if (G_VALUE_HOLDS_STRING (val))
>    {
>      printf ("%s -> %s\n", key, g_value_get_string (val));
>    }
>  else if (G_VALUE_HOLDS_UINT (val))
>    {
>      printf ("%s -> %u\n", key, g_value_get_uint (val));
>    }
>  /* ... insert any more types you might need here ... */
>  else
>    {
>      /* the result of this function is only suitable for debug output */
>      gchar *tmp = g_strdup_value_contents (val);
>
>      printf ("%s -> unhandled type %s with value %s",
>          key, G_VALUE_TYPE_NAME (value), tmp);
>      g_free (tmp);
>    }
> }
> _______________________________________________
> dbus mailing list
> dbus at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dbus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20090415/5a4f1b54/attachment-0001.htm 


More information about the dbus mailing list