1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
* Authors: Iain Holmes <iain@ximian.com>
5
* Copyright 2001 Iain Holmes
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
28
#include <bonobo/bonobo-listener.h>
30
#include <cddb-slave-client.h>
31
#include <GNOME_Media_CDDBSlave2.h>
33
#include <libgnome/gnome-util.h>
39
static GHashTable *cddb_cache = NULL;
40
static CDDBSlaveClient *slave_client = NULL;
41
void freekey(gpointer key, gpointer value,gpointer userdata);
43
count_tracks (CDDBSlaveClientTrackInfo **info)
47
for (i = 0; info[i] != NULL; i++) {
55
get_disc_info (GnomeCD *gcd,
58
GnomeCDDiscInfo *info;
60
gcd_debug ("get_disc_info for discid %s", discid);
61
info = g_hash_table_lookup (cddb_cache, discid);
63
g_warning ("No cache for %s", discid);
66
gcd->disc_info = info;
69
info->artist = cddb_slave_client_get_artist (slave_client, discid);
70
info->title = cddb_slave_client_get_disc_title (slave_client, discid);
71
info->track_info = cddb_slave_client_get_tracks (slave_client, discid);
72
gcd_debug ("get_disc_info: artist %s, title %s, track_info %p",
73
info->artist, info->title, info->track_info);
75
if (count_tracks (info->track_info) != info->ntracks) {
77
cddb_slave_client_free_track_info (info->track_info);
78
gcd_debug ("Track count doesn't match, bad entry ?");
79
info->track_info = NULL;
80
gcd->disc_info = NULL;
83
gnome_cd_build_track_list_menu (gcd);
87
cddb_listener_event_cb (BonoboListener *listener,
90
CORBA_Environment *ev,
93
GNOME_Media_CDDBSlave2_QueryResult *qr;
97
gcd_debug ("Got a result for discid %s on listener %p", qr->discid, listener);
98
if (gcd->discid == NULL) {
99
/* We weren't looking up, so just return */
100
gcd_debug ("We're not looking up, so ignoring");
103
if (strcmp (gcd->discid, qr->discid) != 0) {
104
gcd_debug ("Not our discid, so ignoring");
107
g_free (gcd->discid);
109
switch (qr->result) {
110
case GNOME_Media_CDDBSlave2_OK:
111
get_disc_info (gcd, qr->discid);
114
case GNOME_Media_CDDBSlave2_REQUEST_PENDING:
115
/* Do nothing really */
116
gcd->disc_info = NULL;
119
case GNOME_Media_CDDBSlave2_ERROR_CONTACTING_SERVER:
120
g_warning ("Could not contact CDDB server");
121
gcd->disc_info = NULL;
124
case GNOME_Media_CDDBSlave2_ERROR_RETRIEVING_DATA:
125
g_warning ("Error downloading data");
126
gcd->disc_info = NULL;
129
case GNOME_Media_CDDBSlave2_MALFORMED_DATA:
130
g_warning ("Malformed data");
131
gcd->disc_info = NULL;
134
case GNOME_Media_CDDBSlave2_IO_ERROR:
135
g_warning ("Generic IO error");
136
gcd->disc_info = NULL;
145
cddb_free_disc_info (GnomeCDDiscInfo *info)
147
/* don't leave corrupted entry in hash */
149
g_hash_table_remove (cddb_cache, info->discid);
151
g_free (info->discid);
152
g_free (info->title);
153
g_free (info->artist);
155
if (info->track_info)
156
cddb_slave_client_free_track_info (info->track_info);
160
static GnomeCDDiscInfo *
161
cddb_make_disc_info (GnomeCDRomCDDBData *data)
163
GnomeCDDiscInfo *discinfo;
165
discinfo = g_new0 (GnomeCDDiscInfo, 1);
166
discinfo->discid = g_strdup_printf ("%08lx", (gulong) data->discid);
167
discinfo->title = NULL;
168
discinfo->artist = NULL;
169
discinfo->ntracks = data->ntrks;
170
discinfo->track_info = NULL;
176
cddb_close_client (void)
178
if (slave_client != NULL)
179
g_object_unref (G_OBJECT (slave_client));
182
// when the CD is ejected we need to destroy the cddb_cache hashTable
183
void destroy_cache_hashTable()
185
g_hash_table_foreach(cddb_cache,freekey,NULL); //first free the key
186
g_hash_table_destroy(cddb_cache); //destroy the table
190
//this will only free the keys in hashtable, the values are already freed.
191
void freekey(gpointer key, gpointer value,gpointer userdata)
199
cddb_get_query (GnomeCD *gcd)
201
GnomeCDRomCDDBData *data;
202
GnomeCDDiscInfo *info;
203
BonoboListener *listener;
205
char *offsets = NULL;
208
if (cddb_cache == NULL) {
209
cddb_cache = g_hash_table_new (g_str_hash, g_str_equal);
212
if (gnome_cdrom_get_cddb_data (gcd->cdrom, &data, NULL) == FALSE) {
213
g_print ("gnome_cdrom_get_cddb_data returned FALSE");
217
discid = g_strdup_printf ("%08lx", (gulong) data->discid);
218
for (i = 0; i < data->ntrks; i++) {
221
tmp = g_strdup_printf ("%u ", data->offsets[i]);
222
if (offsets == NULL) {
225
tmp2 = g_strconcat (offsets, tmp, NULL);
227
offsets = g_strdup (tmp2);
232
info = g_hash_table_lookup (cddb_cache, discid);
235
gcd->disc_info = info;
237
gnome_cd_build_track_list_menu (gcd);
240
info = cddb_make_disc_info (data);
241
// g_strdup is added so that the key will persist
242
g_hash_table_insert (cddb_cache, g_strdup(info->discid), info);
243
gcd->disc_info = info;
244
gnome_cd_build_track_list_menu (gcd);
247
/* Remove the last space */
248
offsets[strlen (offsets) - 1] = 0;
250
/* Create a new slave client if we don't have one yet */
251
if (slave_client == NULL) {
252
slave_client = cddb_slave_client_new ();
253
listener = bonobo_listener_new (NULL, NULL);
254
gcd_debug ("Created a new bonobo listener %p", listener);
255
g_signal_connect (G_OBJECT (listener), "event-notify",
256
G_CALLBACK (cddb_listener_event_cb), gcd);
258
cddb_slave_client_add_listener (slave_client, listener);
261
gcd_debug ("Performing lookup for:");
262
gcd_debug ("%s %d %s %d", discid, data->ntrks, offsets, data->nsecs);
263
gcd->discid = discid;
264
cddb_slave_client_query (slave_client, discid, data->ntrks, offsets,
265
data->nsecs, "GnomeCD", VERSION);
267
gnome_cdrom_free_cddb_data (data);
274
char buf[12], *p = NULL;
277
/* This is what I get for copying TCD code */
278
sprintf (buf, "%u", n);
279
for (p = buf; *p != '\0'; p++) {