/* 
 *
 * $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 <tdelocale.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeversion.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( TDEConfig* 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, TQ_SLOT(localQuery()) );
  }
  else if( m_bRemoteCddbQuery ) {
    m_iCurrentQueriedServer = 0;
    TQTimer::singleShot( 0, this, TQ_SLOT(remoteQuery()) );
  }
  else {
    TQTimer::singleShot( 0, this, TQ_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, TQ_SIGNAL(infoMessage(const TQString&)),
	       this, TQ_SIGNAL(infoMessage(const TQString&)) );
      connect( m_httpQuery, TQ_SIGNAL(queryFinished(K3bCddbQuery*)),
	       this, TQ_SLOT(slotQueryFinished(K3bCddbQuery*)) );
      connect( m_httpQuery, TQ_SIGNAL(inexactMatches(K3bCddbQuery*)),
	       this, TQ_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
    }

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

    return m_httpQuery;
  }
  else {
    if( !m_cddbpQuery ) {
      m_cddbpQuery = new K3bCddbpQuery( this );
      connect( m_cddbpQuery, TQ_SIGNAL(infoMessage(const TQString&)),
	       this, TQ_SIGNAL(infoMessage(const TQString&)) );
      connect( m_cddbpQuery, TQ_SIGNAL(queryFinished(K3bCddbQuery*)),
	       this, TQ_SLOT(slotQueryFinished(K3bCddbQuery*)) );
      connect( m_cddbpQuery, TQ_SIGNAL(inexactMatches(K3bCddbQuery*)),
	       this, TQ_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, TQ_SIGNAL(infoMessage(const TQString&)),
	     this, TQ_SIGNAL(infoMessage(const TQString&)) );
    connect( m_localQuery, TQ_SIGNAL(queryFinished(K3bCddbQuery*)),
	     this, TQ_SLOT(slotQueryFinished(K3bCddbQuery*)) );
    connect( m_localQuery, TQ_SIGNAL(inexactMatches(K3bCddbQuery*)),
	     this, TQ_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::QUERY_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, TQ_SIGNAL(submitFinished(K3bCddbSubmit*)),
	     this, TQ_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"

