KCDDB

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

KDE's Doxygen guidelines are available online.