summaryrefslogtreecommitdiffstats
path: root/libk3b/cddb/k3bcddbhttpquery.cpp
blob: 1808584dff400b367d0e901fbbaa679ec496633d (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
/* 
 *
 * $Id: k3bcddbhttpquery.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <[email protected]>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <[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.
 * See the file "COPYING" for the exact licensing terms.
 */



#include "k3bcddbhttpquery.h"

#include "k3bcddbresult.h"

#include <tqstringlist.h>
#include <tqregexp.h>
#include <tqtextstream.h>

#include <tdelocale.h>
#include <kdebug.h>
#include <tdeio/global.h>
#include <tdeio/job.h>


K3bCddbHttpQuery::K3bCddbHttpQuery( TQObject* parent, const char* name )
  : K3bCddbQuery( parent, name )
{
  m_server = "freedb.org";
  m_port = 80;
  m_cgiPath = "/~cddb/cddb.cgi";
}


K3bCddbHttpQuery::~K3bCddbHttpQuery()
{
}


void K3bCddbHttpQuery::doQuery()
{
  setError( WORKING );
  m_state = QUERY;

  performCommand( queryString() );
}


void K3bCddbHttpQuery::doMatchQuery()
{
  setError( WORKING );
  m_state = READ;
  m_parsingBuffer.truncate(0);

  performCommand( TQString( "cddb read %1 %2").arg( header().category ).arg( header().discid ) );
}


void K3bCddbHttpQuery::performCommand( const TQString& cmd )
{
  KURL url;
  url.setProtocol( "http" );
  url.setHost( m_server );
  url.setPort( m_port );
  url.setPath( m_cgiPath );

  url.addQueryItem( "cmd", cmd );
  url.addQueryItem( "hello", handshakeString() );
  url.addQueryItem( "proto", "6" );

  m_data.truncate(0);

  kdDebug() << "(K3bCddbHttpQuery) getting url: " << url.prettyURL() << endl;

  TDEIO::TransferJob* job = TDEIO::get( url, false, false );

  if( !job ) {
    setError( CONNECTION_ERROR );
    emit infoMessage( i18n("Could not connect to host %1").arg(m_server) );
    emitQueryFinished();
    return;
  }

  connect( job, TQT_SIGNAL(data(TDEIO::Job*, const TQByteArray&)),
	   TQT_SLOT(slotData(TDEIO::Job*, const TQByteArray&)) );
  connect( job, TQT_SIGNAL(result(TDEIO::Job*)),
	   TQT_SLOT(slotResult(TDEIO::Job*)) );
}



void K3bCddbHttpQuery::slotData( TDEIO::Job*, const TQByteArray& data )
{
  if( data.size() ) {
    TQDataStream stream( m_data, IO_WriteOnly | IO_Append );
    stream.writeRawBytes( data.data(), data.size() );
  }
}


void K3bCddbHttpQuery::slotResult( TDEIO::Job* job )
{
  if( job->error() ) {
    emit infoMessage( job->errorString() );
    setError( CONNECTION_ERROR );
    emitQueryFinished();
    return;
  }

  TQStringList lines = TQStringList::split( "\n", TQString::fromUtf8( m_data.data(), m_data.size() ) );

  for( TQStringList::const_iterator it = lines.begin(); it != lines.end(); ++it ) {
    TQString line = *it;

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

    switch( m_state ) {

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

	queryMatch( header );
      }

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

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

	m_state = QUERY_DATA;
      }

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

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

	m_state = QUERY_DATA;
      }

      else if( getCode( line ) == 202 ) {
	kdDebug() << "(K3bCddbHttpQuery) no match found" << endl;
	emit infoMessage( i18n("No match found") );
	setError(NO_ENTRY_FOUND);
	m_state = FINISHED;
	emitQueryFinished();
	return;
      }

      else {
	kdDebug() << "(K3bCddbHttpQuery) Error while querying: " << line << endl;
	emit infoMessage( i18n("Error while querying") );
	setError(QUERY_ERROR);
	m_state = FINISHED;
	emitQueryFinished();
	return;
      }
      break;

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


	// here we have the inexact matches headers and should emit the
	// inexactMatches signal
	emit inexactMatches( this );
      }
      else {
	kdDebug() << "(K3bCddbHttpQuery) inexact match: " << line << endl;

	// create a new resultHeader
	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);
	m_state = FINISHED;
	emitQueryFinished();
	return;
      }
      break;
    

    case READ_DATA:

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

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

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

	setError(SUCCESS);
	m_state = FINISHED;
	emitQueryFinished();
	return;
      }

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


#include "k3bcddbhttpquery.moc"