File: bmpx.c

package info (click to toggle)
conky 1.9.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,764 kB
  • ctags: 3,934
  • sloc: ansic: 27,033; sh: 11,594; xml: 6,001; makefile: 283; python: 16
file content (147 lines) | stat: -rw-r--r-- 4,285 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
 * vim: ts=4 sw=4 noet ai cindent syntax=c
 *
 * Conky, a system monitor, based on torsmo
 *
 * Any original torsmo code is licensed under the BSD license
 *
 * All code written since the fork of torsmo is licensed under the GPL
 *
 * Please see COPYING for details
 *
 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
 *	(see AUTHORS)
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"
#include "conky.h"
#include "logging.h"

#include <bmp/dbus.hh>
#include <dbus/dbus-glib.h>

#define DBUS_TYPE_G_STRING_VALUE_HASHTABLE \
	(dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))

static DBusGConnection *bus;
static DBusGProxy *remote_object;
static int connected = 0;
static char *unknown = "unknown";

void fail(GError *error, struct information *);

void update_bmpx()
{
	GError *error = NULL;
	struct information *current_info = &info;
	gint current_track;
	GHashTable *metadata;

	if (connected == 0) {
		g_type_init();
		dbus_g_type_specialized_init();

		bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
		if (bus == NULL) {
			NORM_ERR("BMPx error 1: %s\n", error->message);
			fail(error, current_info);
			return;
		}

		remote_object = dbus_g_proxy_new_for_name(bus, BMP_DBUS_SERVICE,
				BMP_DBUS_PATH__BMP, BMP_DBUS_INTERFACE__BMP);
		if (!remote_object) {
			NORM_ERR("BMPx error 2: %s\n", error->message);
			fail(error, current_info);
			return;
		}

		connected = 1;
	}

	if (connected == 1) {
		if (dbus_g_proxy_call(remote_object, "GetCurrentTrack", &error,
					G_TYPE_INVALID, G_TYPE_INT, &current_track, G_TYPE_INVALID)) {
		} else {
			NORM_ERR("BMPx error 3: %s\n", error->message);
			fail(error, current_info);
			return;
		}

		if (dbus_g_proxy_call(remote_object, "GetMetadataForListItem", &error,
					G_TYPE_INT, current_track, G_TYPE_INVALID,
					DBUS_TYPE_G_STRING_VALUE_HASHTABLE, &metadata,
					G_TYPE_INVALID)) {
			if (current_info->bmpx.title) {
				free(current_info->bmpx.title);
				current_info->bmpx.title = 0;
			}
			if (current_info->bmpx.artist) {
				free(current_info->bmpx.artist);
				current_info->bmpx.artist = 0;
			}
			if (current_info->bmpx.album) {
				free(current_info->bmpx.album);
				current_info->bmpx.album = 0;
			}
			current_info->bmpx.title =
				g_value_dup_string(g_hash_table_lookup(metadata, "title"));
			current_info->bmpx.artist =
				g_value_dup_string(g_hash_table_lookup(metadata, "artist"));
			current_info->bmpx.album =
				g_value_dup_string(g_hash_table_lookup(metadata, "album"));
			current_info->bmpx.bitrate =
				g_value_get_int(g_hash_table_lookup(metadata, "bitrate"));
			current_info->bmpx.track =
				g_value_get_int(g_hash_table_lookup(metadata, "track-number"));
			current_info->bmpx.uri =
				g_value_get_string(g_hash_table_lookup(metadata, "location"));
		} else {
			NORM_ERR("BMPx error 4: %s\n", error->message);
			fail(error, current_info);
			return;
		}

		g_hash_table_destroy(metadata);
	} else {
		fail(error, current_info);
	}
}

void fail(GError *error, struct information *current_info)
{
	if (error) {
		g_error_free(error);
	}
	if (current_info->bmpx.title) {
		g_free(current_info->bmpx.title);
		current_info->bmpx.title = 0;
	}
	if (current_info->bmpx.artist) {
		g_free(current_info->bmpx.artist);
		current_info->bmpx.artist = 0;
	}
	if (current_info->bmpx.album) {
		g_free(current_info->bmpx.album);
		current_info->bmpx.album = 0;
	}
	current_info->bmpx.title = unknown;
	current_info->bmpx.artist = unknown;
	current_info->bmpx.album = unknown;
	current_info->bmpx.bitrate = 0;
	current_info->bmpx.track = 0;
}