Minor fixes.
[monky] / src / bmpx.c
1 /** bmpx.c
2  * BMPx client
3  *
4  * $Id$
5  */
6
7 #include <dbus/dbus-glib.h>
8 #include <bmpx/dbus.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "conky.h"
13
14 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
15
16 static DBusGConnection *bus;
17 static DBusGProxy *remote_object;
18 static int connected = 0;
19
20 void update_bmpx()
21 {
22         GError *error = NULL;
23         struct information *current_info = &info;
24         gchar *uri;
25         GHashTable *metadata;
26         
27         if (connected == 0) {
28                 g_type_init();
29                 dbus_g_type_specialized_init();
30                 
31                 bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
32                 if (bus == NULL) {
33                         ERR("%s\n", error->message);
34                         goto fail;
35                 }
36                 
37                 remote_object = dbus_g_proxy_new_for_name(bus,
38                         BMP_DBUS_SERVICE,
39                         BMP_DBUS_PATH_SYSTEMCONTROL,
40                         BMP_DBUS_INTERFACE);
41                 if (!remote_object) {
42                         ERR("%s\n", error->message);
43                         goto fail;
44                 } 
45
46                 connected = 1;
47         } 
48         
49         if (connected == 1) {
50                 if (dbus_g_proxy_call(remote_object, "GetCurrentUri", &error,
51                                         G_TYPE_INVALID,
52                                         G_TYPE_STRING, &uri, G_TYPE_INVALID)) {
53                         current_info->bmpx.uri = uri;
54                 } else {
55                         ERR("%s\n", error->message);
56                         goto fail;
57                 }
58         
59                 if (dbus_g_proxy_call(remote_object, "GetMetadataForUri", &error,
60                                 G_TYPE_STRING,
61                                 uri,
62                                 G_TYPE_INVALID,
63                                 DBUS_TYPE_G_STRING_VALUE_HASHTABLE,
64                                 &metadata,
65                                 G_TYPE_INVALID)) {
66                         current_info->bmpx.title= g_value_get_string(g_hash_table_lookup(metadata, "title"));
67                         current_info->bmpx.artist = g_value_get_string(g_hash_table_lookup(metadata, "artist"));
68                         current_info->bmpx.album = g_value_get_string(g_hash_table_lookup(metadata, "album"));
69                         current_info->bmpx.bitrate = g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
70                         current_info->bmpx.track = g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
71                 } else {
72                         ERR("%s\n", error->message);
73                         goto fail;
74                 }
75                 
76                 if (uri)
77                         free(uri);
78                 g_hash_table_destroy(metadata);
79         } else {
80 fail:
81                 current_info->bmpx.title = strdup("Unknown");
82                 current_info->bmpx.artist = strdup("Unknown");
83                 current_info->bmpx.album = strdup("Unknown");
84                 current_info->bmpx.bitrate = 0;
85                 current_info->bmpx.track = 0;
86         }
87 }