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