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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
/*
Copyright (C) 2000 Michael Matz <[email protected]>
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <errno.h>
#include <unistd.h>
#include <qdir.h>
#include <qtextstream.h>
#include <qregexp.h>
//#include <qapp.h>
#include <qstring.h>
// #include <qcursor.h>
//#include <kdebug.h>
#include <ksock.h>
#include <kextsock.h>
#include <klocale.h>
#include <kinputdialog.h>
#include "cddb.h"
#include "cddb.moc"
// FIXME //kdDebug
CDDB::CDDB()
: ks(0), port(80), remote(false), save_local(false)
{
QString s = QDir::homeDirPath()+"/.cddb";
cddb_dirs +=s;
}
CDDB::~CDDB()
{
deinit();
}
bool
CDDB::set_server(const char *hostname, unsigned short int _port)
{
if (ks)
{
if (h_name == hostname && port == _port)
return true;
deinit();
}
remote = (hostname != 0) && (*hostname != 0);
//kdDebug(7101) << "CDDB: set_server, host=" << hostname << "port=" << _port << endl;
if (remote)
{
ks = new KExtendedSocket(hostname, _port);
if (ks->connect() < 0)
{
//kdDebug(7101) << "CDDB: Can't connect!" << endl;
delete ks;
ks = 0;
return false;
}
h_name = hostname;
port = _port;
QCString r;
readLine(r); // the server greeting
writeLine("cddb hello kde-user blubb kio_audiocd 0.4");
readLine(r);
}
return true;
}
bool
CDDB::deinit()
{
if (ks)
{
writeLine("quit");
QCString r;
readLine(r);
ks->close();
}
h_name.resize(0);
port = 0;
remote = false;
ks = 0;
return true;
}
bool
CDDB::readLine(QCString& ret)
{
int read_length = 0;
char small_b[128];
//fd_set set;
ret.resize(0);
while (read_length < 40000)
{
// Look for a \n in buf
int ni = buf.find('\n');
if (ni >= 0)
{
// Nice, so return this substring (without the \n),
// and truncate buf accordingly
ret = buf.left(ni);
if (ret.length() && ret[ret.length()-1] == '\r')
ret.resize(ret.length());
buf.remove(0, ni+1);
//kdDebug(7101) << "CDDB: got `" << ret << "'" << endl;
return true;
}
// Try to refill the buffer
ks->waitForMore(60 * 1000);
ssize_t l = ks->readBlock(small_b, sizeof(small_b)-1);
if (l <= 0)
{
// l==0 normally means fd got closed, but we really need a lineend
return false;
}
small_b[l] = 0;
read_length += l;
buf += small_b;
}
return false;
}
bool
CDDB::writeLine(const QCString& line)
{
const char *b = line.data();
int l = line.length();
//kdDebug(7101) << "CDDB: send `" << line << "'" << endl;
while (l)
{
ssize_t wl = ks->writeBlock(b, l);
if (wl < 0 && errno != EINTR)
return false;
if (wl < 0)
wl = 0;
l -= wl;
b += wl;
}
l = line.length();
if (l && line.data()[l-1] != '\n')
{
char c = '\n';
ssize_t wl;
do {
wl = ks->writeBlock(&c, 1);
} while (wl <= 0 && errno == EINTR);
if (wl<=0 && errno != EINTR)
return false;
}
return true;
}
unsigned int
CDDB::get_discid(QValueList<int>& track_ofs)
{
unsigned int id = 0;
int num_tracks = track_ofs.count() - 2;
// the last two track_ofs[] are disc begin and disc end
for (int i = num_tracks - 1; i >= 0; i--)
{
int n = track_ofs[i];
n /= 75;
while (n > 0)
{
id += n % 10;
n /= 10;
}
}
unsigned int l = track_ofs[num_tracks + 1];
l -= track_ofs[num_tracks];
l /= 75;
id = ((id % 255) << 24) | (l << 8) | num_tracks;
return id;
}
static int
get_code (const QCString &s)
{
bool ok;
int code = s.left(3).toInt(&ok);
if (!ok)
code = -1;
return code;
}
static void
parse_query_resp (const QCString& _r, QCString& catg, QCString& d_id, QCString& title)
{
QCString r = _r.stripWhiteSpace();
int i = r.find(' ');
if (i)
{
catg = r.left(i).stripWhiteSpace();
r.remove(0, i+1);
r = r.stripWhiteSpace();
}
i = r.find(' ');
if (i)
{
d_id = r.left(i).stripWhiteSpace();
r.remove(0, i+1);
r = r.stripWhiteSpace();
}
title = r;
}
QString
CDDB::track(int i) const
{
if (i < 0 || i >= int(m_names.count()))
return QString();
return m_names[i].utf8();
}
QString
CDDB::artist(int i) const
{
if (i < 0 || i >= int(m_artists.count()))
return QString();
return m_artists[i].utf8();
}
bool
CDDB::parse_read_resp(QTextStream *stream, QTextStream *write_stream)
{
/* Note, that m_names and m_title should be empty */
QCString end = ".";
m_disc = 0;
m_year = 0;
m_genre = "";
/* Fill table, so we can index it below. */
for (int i = 0; i < m_tracks; i++)
{
m_names.append("");
m_artists.append("");
}
while (1)
{
QCString r;
if (stream)
{
if (stream->atEnd())
break;
r = stream->readLine().latin1();
}
else
{
if (!readLine(r))
return false;
}
/* Normally the "." is not saved into the local files, but be
tolerant about this. */
if (r == end)
break;
if (write_stream)
*write_stream << r << endl;
r = r.stripWhiteSpace();
if (r.isEmpty() || r[0] == '#')
continue;
if (r.left(7) == "DTITLE=")
{
r.remove(0, 7);
m_title += QString::fromLocal8Bit(r.stripWhiteSpace());
}
else if (r.left(6) == "TTITLE")
{
r.remove(0, 6);
int e = r.find('=');
if (e)
{
bool ok;
int i = r.left(e).toInt(&ok);
if (ok && i >= 0 && i < m_tracks)
{
r.remove(0, e+1);
m_names[i] += QString::fromLocal8Bit(r);
}
}
}
else if (r.left(6) == "DYEAR=")
{
r.remove(0, 6);
QString year = QString::fromLocal8Bit(r.stripWhiteSpace());
m_year = year.toInt();
//kdDebug(7101) << "CDDB: found Year: " << QString().sprintf("%04i",m_year) << endl;
}
else if (r.left(7) == "DGENRE=")
{
r.remove(0, 7);
m_genre = QString::fromLocal8Bit(r.stripWhiteSpace());
//kdDebug(7101) << "CDDB: found Genre: " << m_genre << endl;
}
}
/* XXX We should canonicalize the strings ("\n" --> '\n' e.g.) */
int si = m_title.find(" / ");
if (si > 0)
{
m_artist = m_title.left(si).stripWhiteSpace();
m_title.remove(0, si+3);
m_title = m_title.stripWhiteSpace();
}
si = m_title.find(" - CD");
if (si > 0)
{
QString disc = m_title.right(m_title.length()-(si+5)).stripWhiteSpace();
m_disc = disc.toInt();
//kdDebug(7101) << "CDDB: found Disc: " << disc << endl;
m_title = m_title.left(si).stripWhiteSpace();
}
if (m_title.isEmpty())
m_title = i18n("No Title");
/*else
m_title.replace(QRegExp("/"), "%2f");*/
if (m_artist.isEmpty())
m_artist = i18n("Unknown");
/*else
m_artist.replace(QRegExp("/"), "%2f");*/
//kdDebug(7101) << "CDDB: found Title: `" << m_title << "'" << endl;
for (int i = 0; i < m_tracks; i++)
{
if (m_names[i].isEmpty())
m_names[i] += i18n("Track %1").arg(i);
//m_names[i].replace(QRegExp("/"), "%2f");
si = m_names[i].find(" - ");
if (si < 0)
{
si = m_names[i].find(" / ");
}
if (si > 0)
{
m_artists[i] = m_names[i].left(si).stripWhiteSpace();
m_names[i].remove(0, si+3);
m_names[i] = m_names[i].stripWhiteSpace();
}
else
{
m_artists[i] = m_artist;
}
//kdDebug(7101) << "CDDB: found Track " << i+1 << ": `" << m_names[i] << "'" << endl;
}
return true;
}
void
CDDB::add_cddb_dirs(const QStringList& list)
{
QString s = QDir::homeDirPath()+"/.cddb";
cddb_dirs = list;
if (cddb_dirs.isEmpty())
cddb_dirs += s;
}
/* Locates and opens the local file corresponding to that discid.
Returns TRUE, if file is found and ready for reading.
Returns FALSE, if file isn't found. In this case ret_file is initialized
with a QFile which resides in the first cddb_dir, and has a temp name
(the ID + getpid()). You can open it for writing. */
bool
CDDB::searchLocal(unsigned int id, QFile *ret_file)
{
QDir dir;
QString filename;
filename = QString("%1").arg(id, 0, 16).rightJustify(8, '0');
QStringList::ConstIterator it;
for (it = cddb_dirs.begin(); it != cddb_dirs.end(); ++it)
{
dir.setPath(*it);
if (!dir.exists())
continue;
/* First look in dir directly. */
ret_file->setName (*it + "/" + filename);
if (ret_file->exists() && ret_file->open(IO_ReadOnly))
return true;
/* And then in the subdirs of dir (representing the categories normally).
*/
const QFileInfoList *subdirs = dir.entryInfoList (QDir::Dirs);
QFileInfoListIterator fiit(*subdirs);
QFileInfo *fi;
while ((fi = fiit.current()) != 0)
{
ret_file->setName (*it + "/" + fi->fileName() + "/" + filename);
if (ret_file->exists() && ret_file->open(IO_ReadOnly))
return true;
++fiit;
}
}
QString pid;
pid.setNum(::getpid());
ret_file->setName (cddb_dirs[0] + "/" + filename + "." + pid);
/* Try to create the save location. */
dir.setPath(cddb_dirs[0]);
if (save_local && !dir.exists())
{
//dir = QDir::current();
dir.mkdir(cddb_dirs[0]);
}
return false;
}
bool
CDDB::queryCD(QValueList<int>& track_ofs)
{
int num_tracks = track_ofs.count() - 2;
if (num_tracks < 1)
return false;
unsigned int id = get_discid(track_ofs);
QFile file;
bool local;
/* Already read this ID. */
if (id == m_discid)
return true;
emit cddbMessage(i18n("Searching local cddb entry ..."));
qApp->processEvents();
/* First look for a local file. */
local = searchLocal (id, &file);
/* If we have no local file, and no remote connection, barf. */
if (!local && (!remote || ks == 0))
return false;
m_tracks = num_tracks;
m_title = "";
m_artist = "";
m_names.clear();
m_discid = id;
if (local)
{
QTextStream stream(&file);
/* XXX Hmm, what encoding is used by CDDB files? local? Unicode?
Nothing? */
//stream.setEncoding(QTextStream::Locale);
parse_read_resp(&stream, 0);
file.close();
return true;
}
emit cddbMessage(i18n("Searching remote cddb entry ..."));
qApp->processEvents();
/* Remote CDDB query. */
unsigned int length = track_ofs[num_tracks+1] - track_ofs[num_tracks];
QCString q;
q.sprintf("cddb query %08x %d", id, num_tracks);
QCString num;
for (int i = 0; i < num_tracks; i++)
q += " " + num.setNum(track_ofs[i]);
q += " " + num.setNum(length / 75);
if (!writeLine(q))
return false;
QCString r;
if (!readLine(r))
return false;
r = r.stripWhiteSpace();
int code = get_code(r);
if (code == 200)
{
QCString catg, d_id, title;
QDir dir;
QCString s, pid;
emit cddbMessage(i18n("Found exact match cddb entry ..."));
qApp->processEvents();
/* an exact match */
r.remove(0, 3);
parse_query_resp(r, catg, d_id, title);
//kdDebug(7101) << "CDDB: found exact CD: category=" << catg << " DiscId=" << d_id << " Title=`" << title << "'" << endl;
q = "cddb read " + catg + " " + d_id;
if (!writeLine(q))
return false;
if (!readLine(r))
return false;
r = r.stripWhiteSpace();
code = get_code(r);
if (code != 210)
return false;
pid.setNum(::getpid());
s = cddb_dirs[0].latin1();
//s = s + "/" +catg; // xine seems to not search in local subdirs
dir.setPath( s );
if ( !dir.exists() ) dir.mkdir( s );
s = s+"/"+ d_id;
file.setName( s );
if (save_local && file.open(IO_WriteOnly))
{
//kdDebug(7101) << "CDDB: file name to save =" << file.name() << endl;
QTextStream stream(&file);
if (!parse_read_resp(0, &stream))
{
file.remove();
return false;
}
file.close();
/*QString newname (file.name());
newname.truncate(newname.findRev('.'));
if (QDir::current().rename(file.name(), newname)) {
//kdDebug(7101) << "CDDB: rename failed" << endl;
file.remove();
} */
}
else if (!parse_read_resp(0, 0))
return false;
}
else if (code == 211)
{
// Found some close matches. We'll read the query response and ask the user
// which one should be fetched from the server.
QCString end = ".";
QCString catg, d_id, title;
QDir dir;
QCString s, pid, first_match;
QStringList disc_ids;
/* some close matches */
//XXX may be try to find marker based on r
emit cddbMessage(i18n("Found close cddb entry ..."));
qApp->processEvents();
int i=0;
while (1)
{
if (!readLine(r))
return false;
r = r.stripWhiteSpace();
if (r == end)
break;
disc_ids.append(r);
if (i == 0)
first_match = r;
i++;
}
bool ok = false;
// We don't want to be thinking too much, do we?
// QApplication::restoreOverrideCursor();
// Oh, mylord, which match should I serve you?
QString _answer = KInputDialog::getItem(i18n("CDDB Matches"), i18n("Several close CDDB entries found. Choose one:"),
disc_ids, 0, false, &ok );
QCString answer = _answer.utf8();
if (ok){ // Get user selected match
parse_query_resp(answer, catg, d_id, title);
}
else{ // Get first match
parse_query_resp(first_match, catg, d_id, title);
}
// Now we can continue thinking...
// QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
/*kdDebug(7101) << "CDDB: found close CD: category=" << catg << " DiscId="
<< d_id << " Title=`" << title << "'" << endl;*/
// ... and forth we go as usual
q = "cddb read " + catg + " " + d_id;
if (!writeLine(q))
return false;
if (!readLine(r))
return false;
r = r.stripWhiteSpace();
code = get_code(r);
if (code != 210)
return false;
pid.setNum(::getpid());
s = cddb_dirs[0].latin1();
dir.setPath( s );
if ( !dir.exists() ) dir.mkdir( s );
s = s+"/"+ d_id;
file.setName( s );
if (save_local && file.open(IO_WriteOnly))
{
//kdDebug(7101) << "CDDB: file name to save =" << file.name() << endl;
QTextStream stream(&file);
if (!parse_read_resp(0, &stream))
{
file.remove();
return false;
}
file.close();
}
else if (!parse_read_resp(0, 0))
return false;
}
else
{
/* 202 - no match found
403 - Database entry corrupt
409 - no handshake */
//kdDebug(7101) << "CDDB: query returned code " << code << endl;
return false;
}
return true;
}
|