4dd19afeb0291678e93d2cce38a1a993ee3ade4c
[monky] / src / bmpx.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26
27 #include <bmp/dbus.hh>
28 #include <dbus/dbus-glib.h>
29
30 #include "conky.h"
31
32 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE \
33         (dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
34
35 static DBusGConnection *bus;
36 static DBusGProxy *remote_object;
37 static int connected = 0;
38 static char *unknown = "unknown";
39
40 void fail(GError *error);
41
42 void update_bmpx()
43 {
44         GError *error = NULL;
45         struct information *current_info = &info;
46         gint current_track;
47         GHashTable *metadata;
48
49         if (connected == 0) {
50                 g_type_init();
51                 dbus_g_type_specialized_init();
52
53                 bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
54                 if (bus == NULL) {
55                         ERR("BMPx error 1: %s\n", error->message);
56                         fail(error);
57                         return;
58                 }
59
60                 remote_object = dbus_g_proxy_new_for_name(bus, BMP_DBUS_SERVICE,
61                                 BMP_DBUS_PATH, BMP_DBUS_INTERFACE);
62                 if (!remote_object) {
63                         ERR("BMPx error 2: %s\n", error->message);
64                         fail(error);
65                         return;
66                 }
67
68                 connected = 1;
69         }
70
71         if (connected == 1) {
72                 if (dbus_g_proxy_call(remote_object, "GetCurrentTrack", &error,
73                                         G_TYPE_INVALID, G_TYPE_INT, &current_track, G_TYPE_INVALID)) {
74                 } else {
75                         ERR("BMPx error 3: %s\n", error->message);
76                         fail(error);
77                         return;
78                 }
79
80                 if (dbus_g_proxy_call(remote_object, "GetMetadataForListItem", &error,
81                                         G_TYPE_INT, current_track, G_TYPE_INVALID,
82                                         DBUS_TYPE_G_STRING_VALUE_HASHTABLE, &metadata,
83                                         G_TYPE_INVALID)) {
84                         if (current_info->bmpx.title) {
85                                 free(current_info->bmpx.title);
86                                 current_info->bmpx.title = 0;
87                         }
88                         if (current_info->bmpx.artist) {
89                                 free(current_info->bmpx.artist);
90                                 current_info->bmpx.artist = 0;
91                         }
92                         if (current_info->bmpx.album) {
93                                 free(current_info->bmpx.album);
94                                 current_info->bmpx.album = 0;
95                         }
96                         current_info->bmpx.title =
97                                 g_value_dup_string(g_hash_table_lookup(metadata, "title"));
98                         current_info->bmpx.artist =
99                                 g_value_dup_string(g_hash_table_lookup(metadata, "artist"));
100                         current_info->bmpx.album =
101                                 g_value_dup_string(g_hash_table_lookup(metadata, "album"));
102                         current_info->bmpx.bitrate =
103                                 g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
104                         current_info->bmpx.track =
105                                 g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
106                         current_info->bmpx.uri =
107                                 g_value_get_string(g_hash_table_lookup(metadata, "location"));
108                 } else {
109                         ERR("BMPx error 4: %s\n", error->message);
110                         fail(error);
111                         return;
112                 }
113
114                 g_hash_table_destroy(metadata);
115         } else {
116                 fail(error);
117         }
118 }
119
120 void fail(GError *error)
121 {
122         if (error) {
123                 g_error_free(error);
124         }
125         if (current_info->bmpx.title) {
126                 g_free(current_info->bmpx.title);
127                 current_info->bmpx.title = 0;
128         }
129         if (current_info->bmpx.artist) {
130                 g_free(current_info->bmpx.artist);
131                 current_info->bmpx.artist = 0;
132         }
133         if (current_info->bmpx.album) {
134                 g_free(current_info->bmpx.album);
135                 current_info->bmpx.album = 0;
136         }
137         current_info->bmpx.title = unknown;
138         current_info->bmpx.artist = unknown;
139         current_info->bmpx.album = unknown;
140         current_info->bmpx.bitrate = 0;
141         current_info->bmpx.track = 0;
142 }