forked from ElvishArtisan/rivendell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdcddblookup.cpp
414 lines (362 loc) · 9.95 KB
/
rdcddblookup.cpp
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
// rdcddblookup.cpp
//
// RDDiscLookup instance class for accessing the FreeDB CD Database.
//
// (C) Copyright 2003-2022 Fred Gleason <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License
// version 2 as published by the Free Software Foundation.
//
// 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 <stdlib.h>
#include <string.h>
#include <q3process.h>
#include <qapplication.h>
#include <qdatetime.h>
#include <qregexp.h>
#include <qtimer.h>
#include "rdapplication.h"
#include "rdcddblookup.h"
#include "rdprofile.h"
RDCddbLookup::RDCddbLookup(const QString &caption,FILE *profile_msgs,
QWidget *parent)
: RDDiscLookup(caption,profile_msgs,parent)
{
lookup_state=0;
setWindowTitle(caption+" - "+tr("CDDB Query"));
//
// Socket
//
lookup_socket=new QTcpSocket(this);
connect(lookup_socket,SIGNAL(readyRead()),this,SLOT(readyReadData()));
connect(lookup_socket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(errorData(QAbstractSocket::SocketError)));
}
RDCddbLookup::~RDCddbLookup()
{
delete lookup_socket;
}
QString RDCddbLookup::sourceName() const
{
return QString("CDDB");
}
void RDCddbLookup::lookupRecord()
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
lookup_username=rda->user()->name();
lookup_hostname=rda->libraryConf()->cddbServer();
lookup_appname="rivendell";
lookup_appver=VERSION;
profile("starting CDDB lookup");
lookup_socket->connectToHost(lookup_hostname,RDCDDBLOOKUP_DEFAULT_PORT);
}
void RDCddbLookup::readyReadData()
{
QString line;
QString tag;
QString value;
int index;
int code;
char buffer[2048];
char offset[256];
QStringList f0;
bool ok=false;
int index_line;
while(lookup_socket->canReadLine()) {
line=QString::fromUtf8(lookup_socket->readLine());
profile("recevied from server: \""+line+"\"");
code=line.split(" ").at(0).toInt();
switch(lookup_state) {
case 0: // Login Banner
if((code==200)||(code==201)) {
snprintf(buffer,2048,"cddb hello %s %s %s %s",
(const char *)lookup_username.utf8(),
(const char *)lookup_hostname.utf8(),
(const char *)lookup_appname.utf8(),
(const char *)lookup_appver.utf8());
SendToServer(buffer);
lookup_state=1;
}
else {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
break;
case 1: // Handshake Response
if((code==200)||(code==402)) {
SendToServer("proto 6");
lookup_state=2;
}
else {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
break;
case 2: // Protocol Level Response
if(code==201) {
snprintf(buffer,2048,"cddb query %08x %d",
discRecord()->discId(),discRecord()->tracks());
for(int i=0;i<discRecord()->tracks();i++) {
snprintf(offset,256," %d",discRecord()->trackOffset(i));
strcat(buffer,offset);
}
snprintf(offset,256," %d",discRecord()->discLength()/75);
strcat(buffer,offset);
SendToServer(buffer);
lookup_state=3;
}
else {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
break;
case 3: // Query Response
switch(code) {
case 200: // Exact Match
case 201: // Exact Match,read-only
f0=line.split(" ");
if(f0.size()>=4) {
discRecord()->setDiscId(f0[2].toUInt(&ok,16));
if(!ok) {
FinishCddbLookup(RDCddbLookup::LookupError,
"Invalid discid received from CDDB server");
}
discRecord()->setDiscGenre(f0[1]);
f0.erase(f0.begin());
f0.erase(f0.begin());
f0.erase(f0.begin());
discRecord()->setDiscTitle(RDDiscRecord::RemoteSource,f0.join(" "));
snprintf(buffer,2048,"cddb read %s %08x\n",
(const char *)discRecord()->discGenre().utf8(),
discRecord()->discId());
SendToServer(buffer);
lookup_state=5;
}
else {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
break;
case 210: // Multiple Exact Matches
titlesBox()->clear();
titlesKey()->clear();
lookup_state=4;
break;
case 202: // No Match
case 211: // Inexact Match
case 230:
FinishCddbLookup(RDCddbLookup::NoMatch,"OK");
break;
case 401:
case 402:
FinishCddbLookup(RDCddbLookup::NoMatch,"Server failure");
break;
case 403:
FinishCddbLookup(RDCddbLookup::NoMatch,
"CDDB database entry is corrupt");
break;
case 409:
FinishCddbLookup(RDCddbLookup::NoMatch,"No handshake");
break;
case 431:
FinishCddbLookup(RDCddbLookup::NoMatch,
"Handshake not successful, closing connection");
break;
case 432:
FinishCddbLookup(RDCddbLookup::NoMatch,
"No connections allowed: permission denied");
break;
case 433:
FinishCddbLookup(RDCddbLookup::NoMatch,
"No connections allowed: too many users");
break;
case 434:
FinishCddbLookup(RDCddbLookup::NoMatch,
"No connections allowed: system load too high");
break;
case 501:
case 502:
case 503:
case 530:
FinishCddbLookup(RDCddbLookup::NoMatch,"Server error");
break;
default:
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
break;
}
break;
case 4: // Process Multiple Matches
if(line.trimmed()==".") {
profile("Match list complete, showing chooser dialog...");
QApplication::restoreOverrideCursor();
if((index_line=exec())>=0) {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
f0=titlesKey()->at(index_line).split(" ",QString::SkipEmptyParts);
if(f0.size()!=2) {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
discRecord()->setDiscId(f0.at(1).toUInt(&ok,16));
if(!ok) {
FinishCddbLookup(RDCddbLookup::LookupError,
"Invalid discid received from CDDB server");
}
discRecord()->setDiscGenre(f0.at(0));
f0=titlesBox()->currentText().split("/");
if(f0.size()==2) {
discRecord()->setDiscTitle(RDDiscRecord::RemoteSource,
f0.at(1).trimmed());
}
else {
discRecord()->setDiscTitle(RDDiscRecord::RemoteSource,
titlesBox()->currentText().trimmed());
}
snprintf(buffer,2048,"cddb read %s %08x\n",
(const char *)discRecord()->discGenre().utf8(),
discRecord()->discId());
SendToServer(buffer);
lookup_state=5;
}
else {
FinishCddbLookup(RDCddbLookup::NoMatch,"OK");
}
}
else {
f0.clear();
f0=line.split(" ");
titlesKey()->push_back(f0.at(0).trimmed()+" "+
f0.at(1).trimmed());
f0.removeFirst();
f0.removeFirst();
titlesBox()->insertItem(titlesBox()->count(),f0.join(" ").trimmed());
}
break;
case 5: // Read Response
if((code==210)) {
lookup_state=6;
}
else {
FinishCddbLookup(RDCddbLookup::LookupError,
"Unexpected response from CDDB server");
}
break;
case 6: // Record Lines
if(line[0]!='#') { // Ignore Comments
if(line[0]=='.') { // Done
FinishCddbLookup(RDCddbLookup::ExactMatch,"OK");
}
ParsePair(&line,&tag,&value,&index);
if(tag=="DTITLE") {
discRecord()->setDiscTitle(RDDiscRecord::RemoteSource,
value.left(value.length()-1));
}
if(tag=="DYEAR") {
discRecord()->setDiscYear(value.toUInt());
}
if(tag=="EXTD") {
discRecord()->
setDiscExtended(discRecord()->discExtended()+
DecodeString(value));
}
if(tag=="PLAYORDER") {
discRecord()->setDiscPlayOrder(value);
}
if((tag=="TTITLE")&&(index!=-1)) {
discRecord()->setTrackTitle(RDDiscRecord::RemoteSource,index,
value.left(value.length()-1));
}
if((tag=="EXTT")&&(index!=-1)) {
discRecord()->
setTrackExtended(index,
discRecord()->trackExtended(index)+value);
}
}
break;
}
}
}
void RDCddbLookup::errorData(QAbstractSocket::SocketError err)
{
QString err_msg="Network error";
switch(err) {
case QTcpSocket::ErrConnectionRefused:
err_msg="Connection to \""+rda->libraryConf()->cddbServer()+"\" refused";
break;
case QTcpSocket::ErrHostNotFound:
err_msg="Host \""+rda->libraryConf()->cddbServer()+"\" not found";
break;
case QTcpSocket::ErrSocketRead:
err_msg="Socket read error";
break;
default:
break;
}
lookup_state=0;
QApplication::restoreOverrideCursor();
}
void RDCddbLookup::FinishCddbLookup(RDCddbLookup::Result res,
const QString &err_msg)
{
SendToServer("quit");
lookup_socket->close();
lookup_state=0;
QApplication::restoreOverrideCursor();
profile("CDDB lookup finished");
processLookup(res,err_msg);
}
QString RDCddbLookup::DecodeString(QString &str)
{
QString outstr;
QChar ch;
for(int i=0;i<str.length();i++) {
if((ch=str.at(i))=='\\') {
outstr+=QString("\n");
i++;
}
else {
outstr+=QString(ch);
}
}
return outstr;
}
void RDCddbLookup::ParsePair(QString *line,QString *tag,QString *value,
int *index)
{
for(int i=0;i<line->length();i++) {
if(line->at(i)=='=') {
*tag=line->left(i);
*value=line->right(line->length()-i-1);
*value=value->left(value->length()-1); // Lose the silly linefeed
*index=GetIndex(tag);
return;
}
}
}
int RDCddbLookup::GetIndex(QString *tag)
{
int index;
for(int i=0;i<tag->length();i++) {
if(tag->at(i).isDigit()) {
index=tag->right(tag->length()-i).toInt();
*tag=tag->left(i);
return index;
}
}
return -1;
}
void RDCddbLookup::SendToServer(const QString &msg)
{
lookup_socket->writeBlock(msg+"\n",msg.length()+1);
profile("sent to server: \""+msg+"\"");
}