K3b – CD/DVD burning application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
7.2KB

  1. /*
  2. *
  3. * $Id: k3bcddb.cpp 619556 2007-01-03 17:38:12Z trueg $
  4. * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
  5. *
  6. * This file is part of the K3b project.
  7. * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. * See the file "COPYING" for the exact licensing terms.
  14. */
  15. #include <tqstring.h>
  16. #include <tqvaluelist.h>
  17. #include <tqstringlist.h>
  18. #include <tqtimer.h>
  19. #include <tdelocale.h>
  20. #include <tdeconfig.h>
  21. #include <kdebug.h>
  22. #include <tdeversion.h>
  23. #include "k3bcddb.h"
  24. #include "k3bcddbhttpquery.h"
  25. #include "k3bcddbpquery.h"
  26. #include "k3bcddblocalquery.h"
  27. #include "k3bcddblocalsubmit.h"
  28. #include <k3btoc.h>
  29. #include <k3btrack.h>
  30. #include "k3bcddbmultientriesdialog.h"
  31. K3bCddb::K3bCddb( TQObject* parent, const char* name )
  32. : TQObject( parent, name )
  33. {
  34. m_httpQuery = 0;
  35. m_cddbpQuery = 0;
  36. m_localQuery = 0;
  37. m_localSubmit = 0;
  38. m_lastUsedQuery = 0;
  39. }
  40. K3bCddb::~K3bCddb()
  41. {
  42. }
  43. void K3bCddb::readConfig( TDEConfig* c )
  44. {
  45. c->setGroup( "Cddb" );
  46. m_bRemoteCddbQuery = c->readBoolEntry( "use remote cddb", true );
  47. m_bLocalCddbQuery = c->readBoolEntry( "use local cddb query", false );
  48. // old config <= 0.7.3
  49. TQStringList cddbpServer = c->readListEntry( "cddbp server" );
  50. TQStringList httpServer = c->readListEntry( "http server" );
  51. // new config
  52. m_cddbServer = c->readListEntry( "cddb server" );
  53. m_localCddbDirs = c->readPathListEntry( "local cddb dirs" );
  54. m_bUseManualCgiPath = c->readBoolEntry( "use manual cgi path", false );
  55. m_cgiPath = c->readEntry( "cgi path", "/~cddb/cddb.cgi" );
  56. if( m_localCddbDirs.isEmpty() )
  57. m_localCddbDirs.append( "~/.cddb/" );
  58. // old config <= 0.7.3
  59. if( !httpServer.isEmpty() ) {
  60. for( TQStringList::iterator it = httpServer.begin(); it != httpServer.end(); ++it ) {
  61. m_cddbServer.append( "Http " + *it );
  62. }
  63. }
  64. if( !cddbpServer.isEmpty() ) {
  65. for( TQStringList::iterator it = cddbpServer.begin(); it != cddbpServer.end(); ++it ) {
  66. m_cddbServer.append( "Cddbp " + *it );
  67. }
  68. }
  69. if( m_cddbServer.isEmpty() )
  70. m_cddbServer.append( "Http freedb2.org:80" );
  71. }
  72. void K3bCddb::query( const K3bDevice::Toc& toc )
  73. {
  74. m_toc = toc;
  75. if( m_bLocalCddbQuery ) {
  76. m_iCurrentQueriedLocalDir = 0;
  77. TQTimer::singleShot( 0, this, TQT_SLOT(localQuery()) );
  78. }
  79. else if( m_bRemoteCddbQuery ) {
  80. m_iCurrentQueriedServer = 0;
  81. TQTimer::singleShot( 0, this, TQT_SLOT(remoteQuery()) );
  82. }
  83. else {
  84. TQTimer::singleShot( 0, this, TQT_SLOT(slotNoEntry()) );
  85. }
  86. }
  87. void K3bCddb::slotNoEntry()
  88. {
  89. emit queryFinished( K3bCddbQuery::NO_ENTRY_FOUND );
  90. }
  91. void K3bCddb::remoteQuery()
  92. {
  93. K3bCddbQuery* q = getQuery( m_cddbServer[m_iCurrentQueriedServer] );
  94. q->query(m_toc);
  95. }
  96. void K3bCddb::slotMultibleMatches( K3bCddbQuery* query )
  97. {
  98. K3bCddbResultHeader hdr = K3bCddbMultiEntriesDialog::selectCddbEntry( query, 0 );
  99. if( !hdr.discid.isEmpty() )
  100. query->queryMatch( hdr );
  101. else
  102. emit queryFinished( K3bCddbQuery::CANCELED );
  103. }
  104. void K3bCddb::slotQueryFinished( K3bCddbQuery* query )
  105. {
  106. m_lastUsedQuery = query;
  107. if( query->error() == K3bCddbQuery::SUCCESS ) {
  108. m_lastResult = m_lastUsedQuery->result();
  109. // make sure the result has the requested discid since otherwise local saving does not make much sense
  110. m_lastResult.discid = TQString::number( m_toc.discId(), 16 ).rightJustify( 8, '0' );
  111. emit queryFinished( K3bCddbQuery::SUCCESS );
  112. }
  113. else if( query == m_localQuery ) {
  114. m_iCurrentQueriedLocalDir++;
  115. if( m_iCurrentQueriedLocalDir < m_localCddbDirs.size() )
  116. localQuery();
  117. else if( m_bRemoteCddbQuery ) {
  118. m_iCurrentQueriedServer = 0;
  119. remoteQuery();
  120. }
  121. else {
  122. emit queryFinished( query->error() );
  123. }
  124. }
  125. else {
  126. m_iCurrentQueriedServer++;
  127. if( m_iCurrentQueriedServer < m_cddbServer.size() ) {
  128. remoteQuery();
  129. }
  130. else {
  131. emit queryFinished( query->error() );
  132. }
  133. }
  134. }
  135. K3bCddbQuery* K3bCddb::getQuery( const TQString& s )
  136. {
  137. TQStringList buf = TQStringList::split( ":", s.mid( s.find(" ")+1 ) );
  138. TQString server = buf[0];
  139. int port = buf[1].toInt();
  140. if( s.startsWith("Http") ) {
  141. if( !m_httpQuery ) {
  142. m_httpQuery = new K3bCddbHttpQuery( this );
  143. connect( m_httpQuery, TQT_SIGNAL(infoMessage(const TQString&)),
  144. this, TQT_SIGNAL(infoMessage(const TQString&)) );
  145. connect( m_httpQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
  146. this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
  147. connect( m_httpQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
  148. this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
  149. }
  150. m_httpQuery->setServer( server, port );
  151. m_httpQuery->setCgiPath( m_bUseManualCgiPath ? m_cgiPath : TQString::fromLatin1("/~cddb/cddb.cgi") );
  152. return m_httpQuery;
  153. }
  154. else {
  155. if( !m_cddbpQuery ) {
  156. m_cddbpQuery = new K3bCddbpQuery( this );
  157. connect( m_cddbpQuery, TQT_SIGNAL(infoMessage(const TQString&)),
  158. this, TQT_SIGNAL(infoMessage(const TQString&)) );
  159. connect( m_cddbpQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
  160. this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
  161. connect( m_cddbpQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
  162. this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
  163. }
  164. m_cddbpQuery->setServer( server, port );
  165. return m_cddbpQuery;
  166. }
  167. }
  168. void K3bCddb::localQuery()
  169. {
  170. if( !m_localQuery ) {
  171. m_localQuery = new K3bCddbLocalQuery( this );
  172. connect( m_localQuery, TQT_SIGNAL(infoMessage(const TQString&)),
  173. this, TQT_SIGNAL(infoMessage(const TQString&)) );
  174. connect( m_localQuery, TQT_SIGNAL(queryFinished(K3bCddbQuery*)),
  175. this, TQT_SLOT(slotQueryFinished(K3bCddbQuery*)) );
  176. connect( m_localQuery, TQT_SIGNAL(inexactMatches(K3bCddbQuery*)),
  177. this, TQT_SLOT(slotMultibleMatches(K3bCddbQuery*)) );
  178. }
  179. m_localQuery->setCddbDir( m_localCddbDirs[m_iCurrentQueriedLocalDir] );
  180. m_localQuery->query( m_toc );
  181. }
  182. TQString K3bCddb::errorString() const
  183. {
  184. if( !m_lastUsedQuery )
  185. return "no query";
  186. switch( m_lastUsedQuery->error() ) {
  187. case K3bCddbQuery::SUCCESS:
  188. return i18n("Found freedb entry.");
  189. case K3bCddbQuery::NO_ENTRY_FOUND:
  190. return i18n("No entry found");
  191. case K3bCddbQuery::CONNECTION_ERROR:
  192. return i18n("Error while connecting to host.");
  193. case K3bCddbQuery::WORKING:
  194. return i18n("Working...");
  195. case K3bCddbQuery::QUERY_ERROR:
  196. case K3bCddbQuery::READ_ERROR:
  197. case K3bCddbQuery::FAILURE:
  198. default:
  199. return i18n("Communication error.");
  200. }
  201. }
  202. const K3bCddbResultEntry& K3bCddb::result() const
  203. {
  204. // return m_lastUsedQuery->result();
  205. return m_lastResult;
  206. }
  207. void K3bCddb::saveEntry( const K3bCddbResultEntry& entry )
  208. {
  209. if( !m_localSubmit ) {
  210. m_localSubmit = new K3bCddbLocalSubmit( this );
  211. connect( m_localSubmit, TQT_SIGNAL(submitFinished(K3bCddbSubmit*)),
  212. this, TQT_SLOT(slotSubmitFinished(K3bCddbSubmit*)) );
  213. }
  214. m_localSubmit->setCddbDir( m_localCddbDirs[0] );
  215. m_localSubmit->submit( entry );
  216. }
  217. void K3bCddb::slotSubmitFinished( K3bCddbSubmit* s )
  218. {
  219. emit submitFinished( s->error() == K3bCddbSubmit::SUCCESS );
  220. }
  221. #include "k3bcddb.moc"