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