a2fcc303389861c87afea413fd9a81d0b9faae53
[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 char unknown[8];
20
21 void update_bmpx()
22 {
23         GError *error = NULL;
24         struct information *current_info = &info;
25         gchar *uri;
26         GHashTable *metadata;
27         
28         if (connected == 0) {
29                 g_type_init();
30                 dbus_g_type_specialized_init();
31                 
32                 bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
33                 if (bus == NULL) {
34                         ERR("BMPx error 1: %s\n", error->message);
35                         goto fail;
36                 }
37                 
38                 remote_object = dbus_g_proxy_new_for_name(bus,
39                         BMP_DBUS_SERVICE,
40                         BMP_DBUS_PATH_SYSTEMCONTROL,
41                         BMP_DBUS_INTERFACE);
42                 if (!remote_object) {
43                         ERR("BMPx error 2: %s\n", error->message);
44                         goto fail;
45                 } 
46
47                 connected = 1;
48         } 
49         
50         if (connected == 1) {
51                 if (dbus_g_proxy_call(remote_object, "GetCurrentUri", &error,
52                                         G_TYPE_INVALID,
53                                         G_TYPE_STRING, &uri, G_TYPE_INVALID)) {
54                         current_info->bmpx.uri = uri;
55                 } else {
56                         ERR("BMPx error 3: %s\n", error->message);
57                         goto fail;
58                 }
59         
60                 if (dbus_g_proxy_call(remote_object, "GetMetadataForUri", &error,
61                                 G_TYPE_STRING,
62                                 uri,
63                                 G_TYPE_INVALID,
64                                 DBUS_TYPE_G_STRING_VALUE_HASHTABLE,
65                                 &metadata,
66                                 G_TYPE_INVALID)) {
67                         current_info->bmpx.title = g_value_get_string(g_hash_table_lookup(metadata, "title"));
68                         current_info->bmpx.artist = g_value_get_string(g_hash_table_lookup(metadata, "artist"));
69                         current_info->bmpx.album = g_value_get_string(g_hash_table_lookup(metadata, "album"));
70                         current_info->bmpx.bitrate = g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
71                         current_info->bmpx.track = g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
72                 } else {
73                         ERR("BMPx error 4: %s\n", error->message);
74                         goto fail;
75                 }
76                 
77                 if (uri)
78                         free(uri);
79                 g_hash_table_destroy(metadata);
80         } else {
81 fail: 
82                 strcpy(unknown, "Unknown");
83                 current_info->bmpx.title = unknown;
84                 current_info->bmpx.artist = unknown;
85                 current_info->bmpx.album = unknown;
86                 current_info->bmpx.bitrate = 0;
87                 current_info->bmpx.track = 0;
88         }
89 }
90