summaryrefslogtreecommitdiffstats
path: root/libk3b/cddb/k3bcddbpquery.cpp
blob: 74d318cb6c1c52d0d409a1b5328053801564470f (plain)
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
/* 
 *
 * $Id: k3bcddbpquery.cpp 619556 2007-01-03 17:38:12Z trueg $
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */


#include "k3bcddbpquery.h"

#include <tqstringlist.h>
#include <tqsocket.h>
#include <textstream.h>

#include <klocale.h>
#include <kdebug.h>




K3bCddbpQuery::K3bCddbpQuery( TQObject* parent, const char* name )
  :K3bCddbQuery( parent, name )
{
  m_socket = new TQSocket( this );
  m_stream.setDevice( m_socket );
  m_stream.setEncoding( TQTextStream::UnicodeUTF8 );

  connect( m_socket, TQT_SIGNAL(connected()), this, TQT_SLOT(slotConnected()) );
  connect( m_socket, TQT_SIGNAL(hostFound()), this, TQT_SLOT(slotHostFound()) );
  connect( m_socket, TQT_SIGNAL(connectionClosed()), this, TQT_SLOT(slotConnectionClosed()) );
  connect( m_socket, TQT_SIGNAL(error(int)), this, TQT_SLOT(slotError(int)) );
  connect( m_socket, TQT_SIGNAL(readyRead()), this, TQT_SLOT(slotReadyRead()) );
}


K3bCddbpQuery::~K3bCddbpQuery()
{
  delete m_socket;
}

void K3bCddbpQuery::doQuery()
{
  setError( WORKING );

  m_state = GREETING;

  // connect to the server

  m_socket->connectToHost( m_server, m_port );
  emit infoMessage( i18n("Searching %1 on port %2").arg(m_server).arg(m_port) );
}


void K3bCddbpQuery::doMatchQuery()
{
  // we should still be connected
  // TODO: check this

  TQString read = TQString( "cddb read %1 %2").arg( header().category ).arg( header().discid );
  
  m_state = READ;
  m_parsingBuffer = "";
  
  kdDebug() <<  "(K3bCddbpQuery) Read: " << read << endl;

  m_stream << read << endl << flush;
}


void K3bCddbpQuery::slotHostFound()
{
  emit infoMessage( i18n("Host found") );
}


void K3bCddbpQuery::slotConnected()
{
  emit infoMessage( i18n("Connected") );
}


void K3bCddbpQuery::slotConnectionClosed()
{
  emit infoMessage( i18n("Connection closed") );
  emitQueryFinished();
}


void K3bCddbpQuery::cddbpQuit()
{
  m_state = TQUIT;
  m_stream << "quit" << endl << flush;
}


void K3bCddbpQuery::slotReadyRead()
{
  while( m_socket->canReadLine() ) {
    TQString line = m_stream.readLine();

    //    kdDebug() << "(K3bCddbpQuery) line: " << line << endl;

    switch( m_state ) {
    case GREETING:
      if( getCode( line ) == 200 || getCode( line ) == 201) {
	emit infoMessage( i18n("OK, read access") );
	m_state = HANDSHAKE;

	m_stream << "cddb hello " << handshakeString() << endl << flush;
      }

      else {
	emit infoMessage( i18n("Connection refused") );
	setError( CONNECTION_ERROR );
	m_socket->close();
      }
      break;

    case HANDSHAKE:
      if( getCode( line ) == 200 ) {
	emit infoMessage( i18n("Handshake successful") );

	m_state = PROTO;

	m_stream << "proto 6" << endl << flush;
      }

      else {
	emit infoMessage( i18n("Handshake failed") );  // server closes connection
	setError( CONNECTION_ERROR );
	m_socket->close(); // just to be sure
      }
      break;

    case PROTO:
      {
	if( getCode( line ) == 501 ) {
	  kdDebug() << "(K3bCddbpQuery) illigal protocol level!" << endl;
	}
	
	// just ignore the reply since it's not important for the functionality
	m_state = TQUERY;
	
	m_stream << queryString() << endl << flush;
	break;
      }

    case TQUERY:
      if( getCode( line ) == 200 ) {
	// parse exact match and send a read command
	K3bCddbResultHeader header;
	parseMatchHeader( line.mid( 4 ), header );

	emit infoMessage( i18n("Found exact match") );

	queryMatch( header );
      }

      else if( getCode( line ) == 210 ) {
	// TODO: perhaps add an "exact" field to K3bCddbEntry
	kdDebug() << "(K3bCddbpQuery) Found multiple exact matches" << endl;

	emit infoMessage( i18n("Found multiple exact matches") );

	m_state = TQUERY_DATA;
      }

      else if( getCode( line ) == 211 ) {
	kdDebug() << "(K3bCddbpQuery) Found inexact matches" << endl;

	emit infoMessage( i18n("Found inexact matches") );

	m_state = TQUERY_DATA;
      }

      else if( getCode( line ) == 202 ) {
	kdDebug() << "(K3bCddbpQuery) no match found" << endl;
	emit infoMessage( i18n("No match found") );
	setError( NO_ENTRY_FOUND );
	cddbpQuit();
      }

      else {
	kdDebug() << "(K3bCddbpQuery) Error while querying: " << line << endl;
	emit infoMessage( i18n("Error while querying") );
	setError( TQUERY_ERROR );
	cddbpQuit();
      }
      break;

    case TQUERY_DATA:
      if( line.startsWith( "." ) ) {
	// finished query
	// go on reading

	emit inexactMatches( this );
	return;
      }
      else {
	kdDebug() << "(K3bCddbpQuery) inexact match: " << line << endl;
	K3bCddbResultHeader header;
	parseMatchHeader( line, header );
	m_inexactMatches.append( header );
      }
      break;

    case READ:
      if( getCode( line ) == 210 ) {

	// we just start parsing the read data
	m_state = READ_DATA;
      }

      else {
	emit infoMessage( i18n("Could not read match") );
	setError( READ_ERROR );
	cddbpQuit();
      }
      break;
    

    case READ_DATA:

      //      kdDebug() << "(K3bCddbpQuery) parsing line: " << line << endl;

      if( line.startsWith( "." ) ) {
	
	kdDebug() << "(K3bCddbpQuery) query finished." << endl;

	TQTextStream strStream( m_parsingBuffer, IO_ReadOnly );
	parseEntry( strStream, result() );

	setError( SUCCESS );
	cddbpQuit();
      }

      else {
	m_parsingBuffer.append(line + "\n");
      }
      break;

    case TQUIT:
      // no parsing needed
      break;
    }
  }
}


void K3bCddbpQuery::slotError( int e )
{
  switch(e) {
  case TQSocket::ErrConnectionRefused:
    kdDebug() <<  i18n("Connection to %1 refused").arg( m_server ) << endl;
    emit infoMessage( i18n("Connection to %1 refused").arg( m_server ) );
    break;
  case TQSocket::ErrHostNotFound:
    kdDebug() <<  i18n("Could not find host %1").arg( m_server ) << endl;
    emit infoMessage( i18n("Could not find host %1").arg( m_server ) );
    break;
  case TQSocket::ErrSocketRead:
    kdDebug() <<  i18n("Error while reading from %1").arg( m_server ) << endl;
    emit infoMessage( i18n("Error while reading from %1").arg( m_server ) );
    break;
  }

  m_socket->close();
  emitQueryFinished();
}

#include "k3bcddbpquery.moc"