~ubuntu-branches/ubuntu/lucid/gnome-media/lucid

« back to all changes in this revision

Viewing changes to gnome-cd/cddb.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2009-07-28 09:49:30 UTC
  • mfrom: (0.1.33 upstream)
  • Revision ID: james.westby@ubuntu.com-20090728094930-2atfxgwqd0wj55ph
Tags: 2.27.5-0ubuntu1
* New upstream release
  - General:
    + vumeter, gnome-cd and cddb-slave have been removed
  - gnome-volume-control
    + Add card and port selection (Bastien Nocera)
    + Excessive CPU usage when trying to reconnect to PulseAudio (Chris Coulson)
    + Many fixes (Bastien Nocera)
  - Updated translations:
    + es (Jorge González)
    + et (Ivar Smolin)
    + fi (Ilkka Tuohela)
    + gu (Sweta Kothari)
    + he (Yaron Shahrabani)
    + hu (Gabor Kelemen)
    + pt_BR (André Gondim)
    + sv (Daniel Nylander)
    + ta (Dr.T.Vasudevan)
    + zh_HK (Chao-Hsiung Liao)
    + zh_TW (Chao-Hsiung Liao)
* debian/rules: Remove configure flags to disable cddb-slave and gnome-cd
* 01_vumeter_nodisplay.patch: Dropped, as vumeter has been removed
* 03_menu_changes.patch: Removed references to gnome-cd and vumeter files
* 99_autoconf.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
/*
3
 
 *  Authors: Iain Holmes <iain@ximian.com>
4
 
 *
5
 
 *  Copyright 2001 Iain Holmes
6
 
 *
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.
11
 
 *
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.
16
 
 *
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.
20
 
 *
21
 
 */
22
 
 
23
 
#ifdef HAVE_CONFIG_H
24
 
#include <config.h>
25
 
#endif
26
 
 
27
 
#include <string.h>
28
 
#include <bonobo/bonobo-listener.h>
29
 
 
30
 
#include <cddb-slave-client.h>
31
 
#include <GNOME_Media_CDDBSlave2.h>
32
 
 
33
 
#include <libgnome/gnome-util.h>
34
 
 
35
 
#include "gnome-cd.h"
36
 
#include "cdrom.h"
37
 
#include "cddb.h"
38
 
 
39
 
static GHashTable *cddb_cache = NULL;
40
 
static CDDBSlaveClient *slave_client = NULL;
41
 
void freekey(gpointer key, gpointer value,gpointer userdata);
42
 
static int
43
 
count_tracks (CDDBSlaveClientTrackInfo **info)
44
 
{
45
 
        int i;
46
 
 
47
 
        for (i = 0; info[i] != NULL; i++) {
48
 
                ;
49
 
        }
50
 
 
51
 
        return i;
52
 
}
53
 
 
54
 
static void
55
 
get_disc_info (GnomeCD *gcd,
56
 
               const char *discid)
57
 
{
58
 
        GnomeCDDiscInfo *info;
59
 
        
60
 
        gcd_debug ("get_disc_info for discid %s", discid);
61
 
        info = g_hash_table_lookup (cddb_cache, discid);
62
 
        if (info == NULL) {
63
 
                g_warning ("No cache for %s", discid);
64
 
                return;
65
 
        } else {
66
 
                gcd->disc_info = info;
67
 
        }
68
 
 
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);
74
 
 
75
 
        if (count_tracks (info->track_info) != info->ntracks) {
76
 
                /* Duff info */
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;
81
 
        }
82
 
 
83
 
        gnome_cd_build_track_list_menu (gcd);
84
 
}
85
 
 
86
 
static void
87
 
cddb_listener_event_cb (BonoboListener *listener,
88
 
                        const char *name,
89
 
                        const BonoboArg *arg,
90
 
                        CORBA_Environment *ev,
91
 
                        GnomeCD *gcd)
92
 
{
93
 
        GNOME_Media_CDDBSlave2_QueryResult *qr;
94
 
 
95
 
        qr = arg->_value;
96
 
 
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");
101
 
                return;
102
 
        }
103
 
        if (strcmp (gcd->discid, qr->discid) != 0) {
104
 
                gcd_debug ("Not our discid, so ignoring");
105
 
                return;
106
 
        }
107
 
        g_free (gcd->discid);
108
 
        gcd->discid = NULL;
109
 
        switch (qr->result) {
110
 
        case GNOME_Media_CDDBSlave2_OK:
111
 
                get_disc_info (gcd, qr->discid);
112
 
                break;
113
 
 
114
 
        case GNOME_Media_CDDBSlave2_REQUEST_PENDING:
115
 
                /* Do nothing really */
116
 
                gcd->disc_info = NULL;
117
 
                break;
118
 
 
119
 
        case GNOME_Media_CDDBSlave2_ERROR_CONTACTING_SERVER:
120
 
                g_warning ("Could not contact CDDB server");
121
 
                gcd->disc_info = NULL;
122
 
                break;
123
 
 
124
 
        case GNOME_Media_CDDBSlave2_ERROR_RETRIEVING_DATA:
125
 
                g_warning ("Error downloading data");
126
 
                gcd->disc_info = NULL;
127
 
                break;
128
 
 
129
 
        case GNOME_Media_CDDBSlave2_MALFORMED_DATA:
130
 
                g_warning ("Malformed data");
131
 
                gcd->disc_info = NULL;
132
 
                break;
133
 
 
134
 
        case GNOME_Media_CDDBSlave2_IO_ERROR:
135
 
                g_warning ("Generic IO error");
136
 
                gcd->disc_info = NULL;
137
 
                break;
138
 
 
139
 
        default:
140
 
                break;
141
 
        }
142
 
}
143
 
 
144
 
void
145
 
cddb_free_disc_info (GnomeCDDiscInfo *info)
146
 
{
147
 
        /* don't leave corrupted entry in hash */
148
 
        if (cddb_cache)
149
 
                g_hash_table_remove (cddb_cache, info->discid);
150
 
 
151
 
        g_free (info->discid);
152
 
        g_free (info->title);
153
 
        g_free (info->artist);
154
 
 
155
 
        if (info->track_info)
156
 
                cddb_slave_client_free_track_info (info->track_info);
157
 
        g_free (info);
158
 
}
159
 
 
160
 
static GnomeCDDiscInfo *
161
 
cddb_make_disc_info (GnomeCDRomCDDBData *data)
162
 
{
163
 
        GnomeCDDiscInfo *discinfo;
164
 
 
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;
171
 
 
172
 
        return discinfo;
173
 
}
174
 
 
175
 
void
176
 
cddb_close_client (void)
177
 
{
178
 
        if (slave_client != NULL)
179
 
                g_object_unref (G_OBJECT (slave_client));
180
 
}
181
 
 
182
 
// when the CD is ejected we need to destroy the cddb_cache hashTable
183
 
void destroy_cache_hashTable()
184
 
{
185
 
        g_hash_table_foreach(cddb_cache,freekey,NULL); //first free the key
186
 
        g_hash_table_destroy(cddb_cache); //destroy the table
187
 
        cddb_cache = NULL;
188
 
}
189
 
 
190
 
//this will only free the keys in hashtable, the values are already freed.
191
 
void freekey(gpointer key, gpointer value,gpointer userdata)
192
 
{
193
 
                if(key != NULL)
194
 
                        g_free(key);
195
 
                key = NULL;
196
 
}
197
 
 
198
 
void
199
 
cddb_get_query (GnomeCD *gcd)
200
 
{
201
 
        GnomeCDRomCDDBData *data;
202
 
        GnomeCDDiscInfo *info;
203
 
        BonoboListener *listener;
204
 
        char *discid;
205
 
        char *offsets = NULL;
206
 
        int i;
207
 
 
208
 
        if (cddb_cache == NULL) {
209
 
                cddb_cache = g_hash_table_new (g_str_hash, g_str_equal); 
210
 
        }
211
 
 
212
 
        if (gnome_cdrom_get_cddb_data (gcd->cdrom, &data, NULL) == FALSE) {
213
 
                g_print ("gnome_cdrom_get_cddb_data returned FALSE");
214
 
                return;
215
 
        }
216
 
 
217
 
        discid = g_strdup_printf ("%08lx", (gulong) data->discid);
218
 
        for (i = 0; i < data->ntrks; i++) {
219
 
                char *tmp, *tmp2;
220
 
 
221
 
                tmp = g_strdup_printf ("%u ", data->offsets[i]);
222
 
                if (offsets == NULL) {
223
 
                        offsets = tmp;
224
 
                } else {
225
 
                        tmp2 = g_strconcat (offsets, tmp, NULL);
226
 
                        g_free (tmp);
227
 
                        offsets = g_strdup (tmp2);
228
 
                        g_free (tmp2);
229
 
                }
230
 
        }
231
 
 
232
 
        info = g_hash_table_lookup (cddb_cache, discid);
233
 
 
234
 
        if (info != NULL) {
235
 
                gcd->disc_info = info;
236
 
 
237
 
                gnome_cd_build_track_list_menu (gcd);
238
 
                return;
239
 
        } else {
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);
245
 
        }
246
 
 
247
 
        /* Remove the last space */
248
 
        offsets[strlen (offsets) - 1] = 0;
249
 
 
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);
257
 
 
258
 
                cddb_slave_client_add_listener (slave_client, listener);
259
 
        }
260
 
 
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);
266
 
        
267
 
        gnome_cdrom_free_cddb_data (data);
268
 
        g_free (offsets);
269
 
}
270
 
 
271
 
int
272
 
cddb_sum (int n)
273
 
{
274
 
        char buf[12], *p = NULL;
275
 
        int ret = 0;
276
 
        
277
 
        /* This is what I get for copying TCD code */
278
 
        sprintf (buf, "%u", n);
279
 
        for (p = buf; *p != '\0'; p++) {
280
 
                ret += (*p - '0');
281
 
        }
282
 
 
283
 
        return ret;
284
 
}