clarifying licensing
[monky] / src / bmpx.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Any original torsmo code is licensed under the BSD license
5  *
6  * All code written since the fork of torsmo is licensed under the GPL
7  *
8  * Please see COPYING for details
9  *
10  * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (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
28 #include <bmp/dbus.hh>
29 #include <dbus/dbus-glib.h>
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "conky.h"
35
36 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE (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 update_bmpx()
44 {
45         GError *error = NULL;
46         struct information *current_info = &info;
47         gint current_track;
48         GHashTable *metadata;
49         
50         if (connected == 0) {
51                 g_type_init();
52                 dbus_g_type_specialized_init();
53                 
54                 bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
55                 if (bus == NULL) {
56                         ERR("BMPx error 1: %s\n", error->message);
57                         goto fail;
58                 }
59                 
60                 remote_object = dbus_g_proxy_new_for_name(bus,
61                         BMP_DBUS_SERVICE,
62                         BMP_DBUS_PATH,
63                         BMP_DBUS_INTERFACE);
64                 if (!remote_object) {
65                         ERR("BMPx error 2: %s\n", error->message);
66                         goto fail;
67                 } 
68
69                 connected = 1;
70         } 
71         
72         if (connected == 1) {
73                 if (dbus_g_proxy_call(remote_object, "GetCurrentTrack", &error,
74                                         G_TYPE_INVALID,
75                                         G_TYPE_INT, &current_track, G_TYPE_INVALID)) {
76                 } else {
77                         ERR("BMPx error 3: %s\n", error->message);
78                         goto fail;
79                 }
80         
81                 if (dbus_g_proxy_call(remote_object, "GetMetadataForListItem", &error,
82                                 G_TYPE_INT,
83                                 current_track,
84                                 G_TYPE_INVALID,
85                                 DBUS_TYPE_G_STRING_VALUE_HASHTABLE,
86                                 &metadata,
87                                 G_TYPE_INVALID)) {
88                         if (current_info->bmpx.title) {
89                                 free(current_info->bmpx.title);
90                                 current_info->bmpx.title = 0;
91                         }
92                         if (current_info->bmpx.artist) {
93                                 free(current_info->bmpx.artist);
94                                 current_info->bmpx.artist = 0;
95                         }
96                         if (current_info->bmpx.album) {
97                                 free(current_info->bmpx.album);
98                                 current_info->bmpx.album = 0;
99                         }
100                         current_info->bmpx.title = g_value_dup_string(g_hash_table_lookup(metadata, "title"));
101                         current_info->bmpx.artist = g_value_dup_string(g_hash_table_lookup(metadata, "artist"));
102                         current_info->bmpx.album = g_value_dup_string(g_hash_table_lookup(metadata, "album"));
103                         current_info->bmpx.bitrate = g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
104                         current_info->bmpx.track = g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
105                         current_info->bmpx.uri = g_value_get_string(g_hash_table_lookup(metadata, "location"));
106                 } else {
107                         ERR("BMPx error 4: %s\n", error->message);
108                         goto fail;
109                 }
110                 
111                 g_hash_table_destroy(metadata);
112         } else {
113 fail: 
114                 if (error)
115                         g_error_free(error);
116                 if (current_info->bmpx.title) {
117                         g_free(current_info->bmpx.title);
118                         current_info->bmpx.title = 0;
119                 }
120                 if (current_info->bmpx.artist) {
121                         g_free(current_info->bmpx.artist);
122                         current_info->bmpx.artist = 0;
123                 }
124                 if (current_info->bmpx.album) {
125                         g_free(current_info->bmpx.album);
126                         current_info->bmpx.album = 0;
127                 }
128                 current_info->bmpx.title = unknown;
129                 current_info->bmpx.artist = unknown;
130                 current_info->bmpx.album = unknown;
131                 current_info->bmpx.bitrate = 0;
132                 current_info->bmpx.track = 0;
133         }
134 }