KCDDB

asyncmusicbrainzlookup.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Richard Lärkäng <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "asyncmusicbrainzlookup.h"
8 
9 #include "musicbrainzlookup.h"
10 
11 #include <QDebug>
12 #include <QThread>
13 
14 namespace KCDDB
15 {
16  class LookupThread : public QThread
17  {
18 
19  Q_OBJECT
20 
21  public:
22  void run() override
23  {
24  Result result;
25  CDInfoList lookupResponse;
26  MusicBrainzLookup lookup;
27 
28  result = lookup.lookup(QString(), 0, m_offsetList);
29 
30  if (result == Success)
31  lookupResponse = lookup.lookupResponse();
32 
33  Q_EMIT lookupFinished(result, lookupResponse);
34  }
35 
36  TrackOffsetList m_offsetList;
37 
38  Q_SIGNALS:
39  void lookupFinished( KCDDB::Result, KCDDB::CDInfoList );
40  };
41 
42  AsyncMusicBrainzLookup::AsyncMusicBrainzLookup()
43  {
44  // Register custom data types for the signal-slot connection with the lookup thread:
45  qRegisterMetaType<KCDDB::Result>("KCDDB::Result");
46  qRegisterMetaType<KCDDB::CDInfoList>("KCDDB::CDInfoList");
47  }
48 
49  AsyncMusicBrainzLookup::~AsyncMusicBrainzLookup()
50  {
51 
52  }
53 
54  Result AsyncMusicBrainzLookup::lookup( const QString &, uint, const TrackOffsetList & trackOffsetList )
55  {
56  LookupThread* lookupThread = new LookupThread();
57  lookupThread->m_offsetList = trackOffsetList;
58  connect(lookupThread, &LookupThread::lookupFinished, this, &AsyncMusicBrainzLookup::processLookupResult); // queued connection
59 
60  // Make the thread object "self-destructive"; allows us to keep the destructor non-blocking
61  connect(lookupThread, &LookupThread::finished, lookupThread, &LookupThread::deleteLater);
62  // NOTE: the memory automatically gets cleared after the thread has finished
63 
64  lookupThread->start();
65 
66  return Success;
67  }
68 
69  void AsyncMusicBrainzLookup::processLookupResult( KCDDB::Result result, KCDDB::CDInfoList lookupResponse )
70  {
71  qDebug();
72 
73  cdInfoList_ = lookupResponse;
74 
75  Q_EMIT finished(result);
76  }
77 }
78 
79 #include "asyncmusicbrainzlookup.moc"
80 
81 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
Q_SIGNALSQ_SIGNALS
Q_OBJECTQ_OBJECT
KIOWIDGETS_EXPORT bool run(const QUrl &_url, bool _is_local)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
void finished()
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.