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