• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdemultimedia API Reference
  • KDE Home
  • Contact Us
 

libkcddb

  • sources
  • kde-4.14
  • kdemultimedia
  • libkcddb
  • libkcddb
client.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
3  Copyright (C) 2002-2005 Benjamin C. Meyer <ben at meyerhome dot net>
4  Copyright (C) 2003 Richard Lärkäng <nouseforaname@home.se>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "client.h"
23 
24 #include "synccddbplookup.h"
25 #include "asynccddbplookup.h"
26 #include "synchttplookup.h"
27 #include "asynchttplookup.h"
28 #include "syncsmtpsubmit.h"
29 #include "asyncsmtpsubmit.h"
30 #include "synchttpsubmit.h"
31 #include "asynchttpsubmit.h"
32 #include "cache.h"
33 #include "lookup.h"
34 
35 #include "config-musicbrainz.h"
36 #ifdef HAVE_MUSICBRAINZ5
37 #include "musicbrainz/musicbrainzlookup.h"
38 #include "musicbrainz/asyncmusicbrainzlookup.h"
39 #endif
40 
41 #include <kdebug.h>
42 
43 namespace KCDDB
44 {
45  class Client::Private
46  {
47  public:
48 
49  Private()
50  : cdInfoLookup(0),
51  cdInfoSubmit(0),
52  block( true )
53  {}
54 
55  ~Private()
56  {
57  delete cdInfoLookup;
58  delete cdInfoSubmit;
59  }
60 
61  Lookup * cdInfoLookup;
62  Submit * cdInfoSubmit;
63 
64  Config config;
65  CDInfoList cdInfoList;
66  TrackOffsetList trackOffsetList;
67  QList<Lookup *> pendingLookups;
68  bool block;
69  };
70 
71  Client::Client()
72  : d(new Private)
73  {
74  d->config.readConfig();
75  }
76 
77  Client::~Client()
78  {
79  delete d;
80  }
81 
82  Config &
83  Client::config() const
84  {
85  return d->config;
86  }
87 
88  void
89  Client::setBlockingMode( bool enable )
90  {
91  d->block = enable;
92  }
93 
94  bool
95  Client::blockingMode() const
96  {
97  return d->block;
98  }
99 
100  CDInfoList
101  Client::lookupResponse() const
102  {
103  return d->cdInfoList;
104  }
105 
106  Result
107  Client::lookup(const TrackOffsetList & trackOffsetList)
108  {
109  d->cdInfoList.clear();
110  d->trackOffsetList = trackOffsetList;
111 
112  if ( trackOffsetList.count() <= 1 )
113  {
114  kDebug(60010) << "Lookup called with empty offset list";
115  return NoRecordFound;
116  }
117 
118  if ( d->config.cacheLookupEnabled() )
119  {
120  d->cdInfoList = Cache::lookup( trackOffsetList, config() );
121 
122  kDebug(60010) << "Found " << d->cdInfoList.count() << " hit(s)";
123 
124  if ( !d->cdInfoList.isEmpty() )
125  {
126  if ( !blockingMode() )
127  emit finished( Success );
128 
129  return Success;
130  }
131  }
132 
133  Result r = NoRecordFound;
134 
135  // just in case we have an info lookup hanging around, prevent mem leakage
136  delete d->cdInfoLookup;
137  d->cdInfoLookup = 0;
138 
139  if ( blockingMode() )
140  {
141 #ifdef HAVE_MUSICBRAINZ5
142  if ( d->config.musicBrainzLookupEnabled() )
143  {
144  d->cdInfoLookup = new MusicBrainzLookup();
145 
146  r = d->cdInfoLookup->lookup( d->config.hostname(),
147  d->config.port(), trackOffsetList );
148 
149  if ( Success == r )
150  {
151  d->cdInfoList = d->cdInfoLookup->lookupResponse();
152  Cache::store( d->trackOffsetList, d->cdInfoList, config() );
153 
154  return r;
155  }
156 
157  delete d->cdInfoLookup;
158  d->cdInfoLookup = 0L;
159  }
160 #endif
161 
162  if ( d->config.freedbLookupEnabled() )
163  {
164  Lookup::Transport t = ( Lookup::Transport )d->config.freedbLookupTransport();
165  if( Lookup::CDDBP == t )
166  d->cdInfoLookup = new SyncCDDBPLookup();
167  else
168  d->cdInfoLookup = new SyncHTTPLookup();
169 
170  r = d->cdInfoLookup->lookup( d->config.hostname(),
171  d->config.port(), trackOffsetList );
172 
173  if ( Success == r )
174  {
175  d->cdInfoList = d->cdInfoLookup->lookupResponse();
176  Cache::store( d->trackOffsetList, d->cdInfoList, config() );
177 
178  return r;
179  }
180 
181  delete d->cdInfoLookup;
182  d->cdInfoLookup = 0L;
183  }
184 
185  return r;
186  }
187  else
188  {
189 #ifdef HAVE_MUSICBRAINZ5
190  if ( d->config.musicBrainzLookupEnabled() )
191  {
192  AsyncMusicBrainzLookup* lookup = new AsyncMusicBrainzLookup();
193 
194  connect( lookup, SIGNAL(finished(KCDDB::Result)),
195  SLOT(slotFinished(KCDDB::Result)) );
196  d->pendingLookups.append( lookup );
197  }
198 #endif
199 
200  if ( d->config.freedbLookupEnabled() )
201  {
202  Lookup::Transport t = ( Lookup::Transport )d->config.freedbLookupTransport();
203 
204  if( Lookup::CDDBP == t )
205  {
206  AsyncCDDBPLookup* lookup = new AsyncCDDBPLookup();
207 
208  connect( lookup, SIGNAL(finished(KCDDB::Result)),
209  SLOT(slotFinished(KCDDB::Result)) );
210  d->pendingLookups.append( lookup );
211  }
212  else
213  {
214  AsyncHTTPLookup* lookup = new AsyncHTTPLookup();
215 
216  connect( lookup, SIGNAL(finished(KCDDB::Result)),
217  SLOT(slotFinished(KCDDB::Result)) );
218  d->pendingLookups.append( lookup );
219  }
220  }
221 
222  return runPendingLookups();
223  }
224  }
225 
226  void
227  Client::slotFinished( Result r )
228  {
229  if ( d->cdInfoLookup && Success == r )
230  {
231  d->cdInfoList = d->cdInfoLookup->lookupResponse();
232  Cache::store( d->trackOffsetList, d->cdInfoList, config() );
233  }
234  else
235  d->cdInfoList.clear();
236 
237  if ( d->cdInfoLookup ) // in case someone called lookup() while finished() was being processed, and deleted cdInfoLookup.
238  {
239  d->cdInfoLookup->deleteLater();
240  d->cdInfoLookup = 0L;
241  }
242 
243  if ( Success == r )
244  {
245  emit finished( r );
246  qDeleteAll( d->pendingLookups );
247  d->pendingLookups.clear();
248  }
249  else
250  {
251  runPendingLookups();
252  }
253  }
254 
255  void
256  Client::slotSubmitFinished( Result r )
257  {
258  emit finished( r );
259 
260  d->cdInfoSubmit->deleteLater();
261  d->cdInfoSubmit=0L;
262  }
263 
264  Result
265  Client::submit(const CDInfo &cdInfo, const TrackOffsetList& offsetList)
266  {
267  // Check if it's valid
268 
269  if (!cdInfo.isValid())
270  return CannotSave;
271 
272  uint last=0;
273  for (int i=0; i < offsetList.count(); i++)
274  {
275  if(last >= offsetList[i])
276  return CannotSave;
277  last = offsetList[i];
278  }
279 
280  //TODO Check that it is edited
281 
282  // just in case we have a cdInfoSubmit, prevent memory leakage
283  delete d->cdInfoSubmit;
284 
285  QString from = d->config.emailAddress();
286 
287  switch (d->config.freedbSubmitTransport())
288  {
289  case Submit::HTTP:
290  {
291  QString hostname = d->config.httpSubmitServer();
292  uint port = d->config.httpSubmitPort();
293 
294  if ( blockingMode() )
295  d->cdInfoSubmit = new SyncHTTPSubmit(from, hostname, port);
296  else
297  {
298  d->cdInfoSubmit = new AsyncHTTPSubmit(from, hostname, port);
299  connect( static_cast<AsyncHTTPSubmit *>( d->cdInfoSubmit ),
300  SIGNAL(finished(KCDDB::Result)),
301  SLOT(slotSubmitFinished(KCDDB::Result)) );
302  }
303 
304  break;
305  }
306  case Submit::SMTP:
307  {
308  QString hostname = d->config.smtpHostname();
309  uint port = d->config.smtpPort();
310  QString username = d->config.smtpUsername();
311 
312  if ( blockingMode() )
313  d->cdInfoSubmit = new SyncSMTPSubmit( hostname, port, username, from, d->config.submitAddress() );
314  else
315  {
316  d->cdInfoSubmit = new AsyncSMTPSubmit( hostname, port, username, from, d->config.submitAddress() );
317  connect( static_cast<AsyncSMTPSubmit *>( d->cdInfoSubmit ),
318  SIGNAL(finished(KCDDB::Result)),
319  SLOT(slotSubmitFinished(KCDDB::Result)) );
320  }
321  break;
322  }
323  default:
324  kDebug(60010) << "Unsupported transport: ";
325 // << CDDB::transportToString(d->config.submitTransport()) << endl;
326  return UnknownError;
327  break;
328  }
329 
330  Result r = d->cdInfoSubmit->submit( cdInfo, offsetList );
331 
332  if ( blockingMode() )
333  {
334  delete d->cdInfoSubmit;
335  d->cdInfoSubmit = 0L;
336  }
337 
338  return r;
339  }
340 
341  Result
342  Client::runPendingLookups()
343  {
344  if (!d->pendingLookups.empty())
345  {
346  d->cdInfoLookup = d->pendingLookups.takeFirst();
347 
348  Result r = d->cdInfoLookup->lookup( d->config.hostname(),
349  d->config.port(), d->trackOffsetList );
350 
351  if ( Success != r )
352  {
353  delete d->cdInfoLookup;
354  d->cdInfoLookup = 0L;
355  }
356 
357  return r;
358  }
359  else
360  {
361  emit finished( NoRecordFound );
362  return NoRecordFound;
363  }
364  }
365 
366  void
367  Client::store(const CDInfo &cdInfo, const TrackOffsetList& offsetList)
368  {
369  Cache::store(offsetList, cdInfo, config());
370  }
371 }
372 
373 #include "client.moc"
374 
375 
376 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
asyncsmtpsubmit.h
KCDDB::Client::~Client
virtual ~Client()
Definition: client.cpp:77
KCDDB::NoRecordFound
Definition: kcddb.h:43
KCDDB::Submit::SMTP
Definition: submit.h:44
KCDDB::Client::config
Config & config() const
Definition: client.cpp:83
asynchttplookup.h
lookup.h
KCDDB::AsyncSMTPSubmit
Definition: asyncsmtpsubmit.h:32
asynccddbplookup.h
KCDDB::Client::submit
Result submit(const CDInfo &cdInfo, const TrackOffsetList &trackOffsetList)
Definition: client.cpp:265
KCDDB::Cache::lookup
static CDInfoList lookup(const TrackOffsetList &, const Config &)
Definition: cache.cpp:43
KCDDB::Cache::store
static void store(const TrackOffsetList &, const CDInfoList &, const Config &)
Definition: cache.cpp:60
synccddbplookup.h
QList::count
int count(const T &value) const
KCDDB::Lookup::CDDBP
Definition: lookup.h:42
KCDDB::CDInfo
Information about a CD.
Definition: cdinfo.h:117
KCDDB::Client::blockingMode
bool blockingMode() const
Definition: client.cpp:95
KCDDB::CDInfoList
QList< CDInfo > CDInfoList
Definition: cdinfo.h:208
KCDDB::UnknownError
Definition: kcddb.h:47
KCDDB::Success
Definition: kcddb.h:39
KCDDB::Client::slotSubmitFinished
void slotSubmitFinished(KCDDB::Result result)
Called when the submit is finished with the result.
Definition: client.cpp:256
syncsmtpsubmit.h
cache.h
QString
QList
synchttpsubmit.h
KCDDB::SyncCDDBPLookup
Definition: synccddbplookup.h:29
KCDDB::Client::store
void store(const CDInfo &cdInfo, const TrackOffsetList &trackOffsetList)
Stores the CD-information in the local cache.
Definition: client.cpp:367
KCDDB::AsyncHTTPSubmit
Definition: asynchttpsubmit.h:28
KCDDB::Submit::HTTP
Definition: submit.h:43
asynchttpsubmit.h
KCDDB::Client::lookup
Result lookup(const TrackOffsetList &trackOffsetList)
Searches the database for entries matching the offset list.
Definition: client.cpp:107
synchttplookup.h
KCDDB::Client::slotFinished
void slotFinished(KCDDB::Result result)
Called when the lookup is finished with the result.
Definition: client.cpp:227
KCDDB::SyncSMTPSubmit
Definition: syncsmtpsubmit.h:26
KCDDB::TrackOffsetList
QList< uint > TrackOffsetList
This list is used to calculate the CDDB disc id.
Definition: kcddb.h:35
KCDDB::Result
Result
Definition: kcddb.h:37
KCDDB::AsyncHTTPLookup
Definition: asynchttplookup.h:30
KCDDB::CannotSave
Definition: kcddb.h:45
KCDDB::SyncHTTPLookup
Definition: synchttplookup.h:28
KCDDB::Client::finished
void finished(KCDDB::Result result)
emitted when not blocking and lookup() finished.
KCDDB::SyncHTTPSubmit
Definition: synchttpsubmit.h:26
client.h
KCDDB::Config
Definition: kcddbconfig.h:31
KCDDB::Client::lookupResponse
CDInfoList lookupResponse() const
Definition: client.cpp:101
KCDDB::Client::Client
Client()
Uses settings read from config.
Definition: client.cpp:71
KCDDB::CDInfo::isValid
bool isValid() const
Definition: cdinfo.cpp:549
KCDDB::Lookup::Transport
Transport
Definition: lookup.h:40
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KCDDB::AsyncCDDBPLookup
Definition: asynccddbplookup.h:29
KCDDB::Client::setBlockingMode
void setBlockingMode(bool)
Definition: client.cpp:89
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkcddb

Skip menu "libkcddb"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdemultimedia API Reference

Skip menu "kdemultimedia API Reference"
  • libkcddb
  • libkcompactdisc

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal