31d523771d3ae12ecb56b8d3f946d99d7f166616
[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  * $Id$ */
26
27 #include <bmp/dbus.hh>
28 #include <dbus/dbus-glib.h>
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "conky.h"
34
35 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE \
36         (dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
37
38 static DBusGConnection *bus;
39 static DBusGProxy *remote_object;
40 static int connected = 0;
41 static char *unknown = "unknown";
42
43 void fail(GError *error);
44
45 void update_bmpx()
46 {
47         GError *error = NULL;
48         struct information *current_info = &info;
49         gint current_track;
50         GHashTable *metadata;
51
52         if (connected == 0) {
53                 g_type_init();
54                 dbus_g_type_specialized_init();
55
56                 bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
57                 if (bus == NULL) {
58                         ERR("BMPx error 1: %s\n", error->message);
59                         fail(error);
60                         return;
61                 }
62
63                 remote_object = dbus_g_proxy_new_for_name(bus, BMP_DBUS_SERVICE,
64                                 BMP_DBUS_PATH, BMP_DBUS_INTERFACE);
65                 if (!remote_object) {
66                         ERR("BMPx error 2: %s\n", error->message);
67                         fail(error);
68                         return;
69                 }
70
71                 connected = 1;
72         }
73
74         if (connected == 1) {
75                 if (dbus_g_proxy_call(remote_object, "GetCurrentTrack", &error,
76                                         G_TYPE_INVALID, G_TYPE_INT, &current_track, G_TYPE_INVALID)) {
77                 } else {
78                         ERR("BMPx error 3: %s\n", error->message);
79                         fail(error);
80                         return;
81                 }
82
83                 if (dbus_g_proxy_call(remote_object, "GetMetadataForListItem", &error,
84                                         G_TYPE_INT, current_track, G_TYPE_INVALID,
85                                         DBUS_TYPE_G_STRING_VALUE_HASHTABLE, &metadata,
86                                         G_TYPE_INVALID)) {
87                         if (current_info->bmpx.title) {
88                                 free(current_info->bmpx.title);
89                                 current_info->bmpx.title = 0;
90                         }
91                         if (current_info->bmpx.artist) {
92                                 free(current_info->bmpx.artist);
93                                 current_info->bmpx.artist = 0;
94                         }
95                         if (current_info->bmpx.album) {
96                                 free(current_info->bmpx.album);
97                                 current_info->bmpx.album = 0;
98                         }
99                         current_info->bmpx.title =
100                                 g_value_dup_string(g_hash_table_lookup(metadata, "title"));
101                         current_info->bmpx.artist =
102                                 g_value_dup_string(g_hash_table_lookup(metadata, "artist"));
103                         current_info->bmpx.album =
104                                 g_value_dup_string(g_hash_table_lookup(metadata, "album"));
105                         current_info->bmpx.bitrate =
106                                 g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
107                         current_info->bmpx.track =
108                                 g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
109                         current_info->bmpx.uri =
110                                 g_value_get_string(g_hash_table_lookup(metadata, "location"));
111                 } else {
112                         ERR("BMPx error 4: %s\n", error->message);
113                         fail(error);
114                         return;
115                 }
116
117                 g_hash_table_destroy(metadata);
118         } else {
119                 fail(error);
120         }
121 }
122
123 void fail(GError *error)
124 {
125         if (error) {
126                 g_error_free(error);
127         }
128         if (current_info->bmpx.title) {
129                 g_free(current_info->bmpx.title);
130                 current_info->bmpx.title = 0;
131         }
132         if (current_info->bmpx.artist) {
133                 g_free(current_info->bmpx.artist);
134                 current_info->bmpx.artist = 0;
135         }
136         if (current_info->bmpx.album) {
137                 g_free(current_info->bmpx.album);
138                 current_info->bmpx.album = 0;
139         }
140         current_info->bmpx.title = unknown;
141         current_info->bmpx.artist = unknown;
142         current_info->bmpx.album = unknown;
143         current_info->bmpx.bitrate = 0;
144         current_info->bmpx.track = 0;
145 }