summaryrefslogtreecommitdiffstats
path: root/libk3b/cddb/k3bcddb.cpp
blob: 23da69560ec032eb47a493b1c0a4f7f3d3f6568c (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
279
280
/* 
 *
 * $Id: k3bcddb.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * 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 <tqstring.h>
#include <tqvaluelist.h>
#include <tqstringlist.h>
#include <tqtimer.h>

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

#include "k3bcddb.h"
#include "k3bcddbhttpquery.h"
#include "k3bcddbpquery.h"
#include "k3bcddblocalquery.h"
#include "k3bcddblocalsubmit.h"

#include <k3btoc.h>
#include <k3btrack.h>
#include "k3bcddbmultientriesdialog.h"


K3bCddb::K3bCddb( TQObject* parent, const char* name )
  : TQObject( parent, name )
{
  m_httpQuery = 0;
  m_cddbpQuery = 0;
  m_localQuery = 0;
  m_localSubmit = 0;

  m_lastUsedQuery = 0;
}


K3bCddb::~K3bCddb()
{
}


void K3bCddb::readConfig( KConfig* c )
{
  c->setGroup( "Cddb" );

  m_bRemoteCddbQuery = c->readBoolEntry( "use remote cddb", true );
  m_bLocalCddbQuery = c->readBoolEntry( "use local cddb query", false );

  // old config <= 0.7.3
  TQStringList cddbpServer = c->readListEntry( "cddbp server" );
  TQStringList httpServer = c->readListEntry( "http server" );

  // new config
  m_cddbServer = c->readListEntry( "cddb server" );

  m_localCddbDirs = c->readPathListEntry( "local cddb dirs" );

  m_bUseManualCgiPath = c->readBoolEntry( "use manual cgi path", false );
  m_cgiPath = c->readEntry( "cgi path", "/~cddb/cddb.cgi" );

  if( m_localCddbDirs.isEmpty() )
    m_localCddbDirs.append( "~/.cddb/" );

  // old config <= 0.7.3
  if( !httpServer.isEmpty() ) {
    for( TQStringList::iterator it = httpServer.begin(); it != httpServer.end(); ++it ) {
      m_cddbServer.append( "Http " + *it );
    }
  }
  if( !cddbpServer.isEmpty() ) {
    for( TQStringList::iterator it = cddbpServer.begin(); it != cddbpServer.end(); ++it ) {
      m_cddbServer.append( "Cddbp " + *it );
    }
  }

  if( m_cddbServer.isEmpty() )
    m_cddbServer.append( "Http freedb2.org:80" );
}


void K3bCddb::query( const K3bDevice::Toc& toc )
{
  m_toc = toc;

  if( m_bLocalCddbQuery ) {
    m_iCurrentQueriedLocalDir = 0;
    TQTimer::singleShot( 0, this, TQT_SLOT(localQuery()) );
  }
  else if( m_bRemoteCddbQuery ) {
    m_iCurrentQueriedServer = 0;
    TQTimer::singleShot( 0, this, TQT_SLOT(remoteQuery()) );
  }
  else {
    TQTimer::singleShot( 0, this, TQT_SLOT(slotNoEntry()) );
  }
}


void K3bCddb::slotNoEntry()
{
  emit queryFinished( K3bCddbQuery::NO_ENTRY_FOUND );
}


void K3bCddb::remoteQuery()
{
  K3bCddbQuery* q = getQuery( m_cddbServer[m_iCurrentQueriedServer] );
  q->query(m_toc);
}


void K3bCddb::slotMultibleMatches( K3bCddbQuery* query )
{
  K3bCddbResultHeader hdr = K3bCddbMultiEntriesDialog::selectCddbEntry( query, 0 );
  if( !hdr.discid.isEmpty() )
    query->queryMatch( hdr );
  else
    emit queryFinished( K3bCddbQuery::CANCELED );
}


void K3bCddb::slotQueryFinished( K3bCddbQuery* query )
{
  m_lastUsedQuery = query;

  if( query->error() == K3bCddbQuery::SUCCESS ) {
    m_lastResult = m_lastUsedQuery->result();

    // make sure the result has the requested discid since otherwise local saving does not make much sense
    m_lastResult.discid = TQString::number( m_toc.discId(), 16 ).rightJustify( 8, '0' );

    emit queryFinished( K3bCddbQuery::SUCCESS );
  }
  else if( query == m_localQuery ) {
    m_iCurrentQueriedLocalDir++;
    if( m_iCurrentQueriedLocalDir < m_localCddbDirs.size() )
      localQuery();
    else if( m_bRemoteCddbQuery ) {
      m_iCurrentQueriedServer = 0;
      remoteQuery();
    }
    else {
      emit queryFinished( query->error() );
    }
  }
  else {
    m_iCurrentQueriedServer++;
    if( m_iCurrentQueriedServer < m_cddbServer.size() ) {
      remoteQuery();
    }
    else {
      emit queryFinished( query->error() );
    }
  }
}


K3bCddbQuery* K3bCddb::getQuery( const TQString& s )
{
  TQStringList buf = TQStringList::split( ":", s.mid( s.find(" ")+1 ) );
  TQString server = buf[0];
  int port = buf[1].toInt();

  if( s.startsWith("Http") ) {
    if( !m_httpQuery ) {
      m_httpQuery = new K3bCddbHttpQuery( this );
      connect( m_httpQuery, TQT_SIGNAL(infoMessage(const TQString&)),
	       this, TQT_SIGNAL(infoMessage(const TQString&)) );
      connect( m_httpQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
	       this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
      connect( m_httpQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
	       this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
    }

    m_httpQuery->setServer( server, port );
    m_httpQuery->setCgiPath( m_bUseManualCgiPath ? m_cgiPath : TQString::tqfromLatin1("/~cddb/cddb.cgi") );

    return m_httpQuery;
  }
  else {
    if( !m_cddbpQuery ) {
      m_cddbpQuery = new K3bCddbpQuery( this );
      connect( m_cddbpQuery, TQT_SIGNAL(infoMessage(const TQString&)),
	       this, TQT_SIGNAL(infoMessage(const TQString&)) );
      connect( m_cddbpQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
	       this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
      connect( m_cddbpQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
	       this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
    }

    m_cddbpQuery->setServer( server, port );

    return m_cddbpQuery;
  }
}


void K3bCddb::localQuery()
{
  if( !m_localQuery ) {
    m_localQuery = new K3bCddbLocalQuery( this );
    connect( m_localQuery, TQT_SIGNAL(infoMessage(const TQString&)),
	     this, TQT_SIGNAL(infoMessage(const TQString&)) );
    connect( m_localQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
	     this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
    connect( m_localQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
	     this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
  }
  
  m_localQuery->setCddbDir( m_localCddbDirs[m_iCurrentQueriedLocalDir] );
  
  m_localQuery->query( m_toc );
}


TQString K3bCddb::errorString() const
{
  if( !m_lastUsedQuery )
    return "no query";

  switch( m_lastUsedQuery->error() ) {
  case K3bCddbQuery::SUCCESS:
    return i18n("Found freedb entry.");
  case K3bCddbQuery::NO_ENTRY_FOUND:
    return i18n("No entry found");
  case K3bCddbQuery::CONNECTION_ERROR:
    return i18n("Error while connecting to host.");
  case K3bCddbQuery::WORKING:
    return i18n("Working...");
  case K3bCddbQuery::TQUERY_ERROR:
  case K3bCddbQuery::READ_ERROR:
  case K3bCddbQuery::FAILURE:
  default:
    return i18n("Communication error.");
  }
}


const K3bCddbResultEntry& K3bCddb::result() const
{
  //  return m_lastUsedQuery->result();
  return m_lastResult;
}


void K3bCddb::saveEntry( const K3bCddbResultEntry& entry )
{
  if( !m_localSubmit ) {
    m_localSubmit = new K3bCddbLocalSubmit( this );
    connect( m_localSubmit, TQT_SIGNAL(submitFinished(K3bCddbSubmit*)),
	     this, TQT_SLOT(slotSubmitFinished(K3bCddbSubmit*)) );
  }
  
  m_localSubmit->setCddbDir( m_localCddbDirs[0] );
  
  m_localSubmit->submit( entry );
}


void K3bCddb::slotSubmitFinished( K3bCddbSubmit* s )
{
  emit submitFinished( s->error() == K3bCddbSubmit::SUCCESS );
}

#include "k3bcddb.moc"