• 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
asynccddbplookup.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
3  Copyright (C) 2002 Benjamin Meyer <ben-devel@meyerhome.net>
4  Copyright (C) 2005 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 "asynccddbplookup.h"
23 
24 #include <kdebug.h>
25 #include <ksocketfactory.h>
26 
27 namespace KCDDB
28 {
29  AsyncCDDBPLookup::AsyncCDDBPLookup()
30  : CDDBPLookup(),
31  state_(Idle)
32  {
33 
34  }
35 
36  AsyncCDDBPLookup::~AsyncCDDBPLookup()
37  {
38  }
39 
40  Result
41  AsyncCDDBPLookup::lookup
42  (
43  const QString & hostname,
44  uint port,
45  const TrackOffsetList & trackOffsetList
46  )
47  {
48  socket_ = KSocketFactory::connectToHost(QLatin1String( "cddbp" ), hostname, port);
49 
50  connect (socket_, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(slotGotError(QAbstractSocket::SocketError)));
51 
52  connect (socket_, SIGNAL(connected()),
53  SLOT(slotConnectionSuccess()) );
54 
55  connect (socket_, SIGNAL(readyRead()), SLOT(slotReadyRead()) );
56 
57  trackOffsetList_ = trackOffsetList;
58 
59  state_ = WaitingForConnection;
60 
61  return Success;
62  }
63 
64  void
65  AsyncCDDBPLookup::slotGotError(QAbstractSocket::SocketError error)
66  {
67  state_ = Idle;
68 
69  if ( error == QAbstractSocket::HostNotFoundError )
70  emit finished( HostNotFound );
71  else if ( error == QAbstractSocket::SocketTimeoutError )
72  emit finished( NoResponse );
73  else
74  emit finished( UnknownError );
75  }
76 
77  void
78  AsyncCDDBPLookup::slotConnectionSuccess()
79  {
80  kDebug(60010) << "Connection successful";
81  state_ = WaitingForGreeting;
82  }
83 
84  void
85  AsyncCDDBPLookup::slotReadyRead()
86  {
87  kDebug(60010) << "Ready to read. State: " << stateToString();
88 
89  while ( Idle != state_ && isConnected() && socket_->canReadLine() )
90  read();
91  }
92 
93  void
94  AsyncCDDBPLookup::read()
95  {
96  switch ( state_ )
97  {
98  case WaitingForGreeting:
99 
100  if ( !parseGreeting( readLine() ) )
101  {
102  result_ = ServerError;
103  doQuit();
104  return;
105  }
106 
107  doHandshake();
108 
109  break;
110 
111  case WaitingForHandshake:
112 
113  if ( !parseHandshake( readLine() ) )
114  {
115  result_ = ServerError;
116  doQuit();
117  return;
118  }
119 
120  doProto();
121 
122  break;
123 
124  case WaitingForProtoResponse:
125 
126  // Ignore the response for now
127  readLine();
128 
129  doQuery();
130 
131  break;
132 
133  case WaitingForQueryResponse:
134  result_ = parseQuery( readLine() );
135 
136  switch ( result_ )
137  {
138  case Success:
139  requestCDInfoForMatch();
140  break;
141 
142  case MultipleRecordFound:
143  state_ = WaitingForMoreMatches;
144  break;
145 
146  default: // Error :(
147  doQuit();
148  return;
149  }
150 
151  break;
152 
153  case WaitingForMoreMatches:
154  {
155  QString line = readLine();
156 
157  if (line.startsWith(QLatin1String( "." )))
158  requestCDInfoForMatch();
159  else
160  parseExtraMatch( line );
161  }
162 
163  break;
164 
165  case WaitingForCDInfoResponse:
166  {
167  Result result = parseRead( readLine() );
168 
169  if ( Success != result )
170  {
171  result_ = result;
172  doQuit();
173  return;
174  }
175 
176  state_ = WaitingForCDInfoData;
177  }
178 
179  break;
180 
181  case WaitingForCDInfoData:
182  {
183  QString line = readLine();
184 
185  if (line.startsWith(QLatin1String( "." )))
186  {
187  parseCDInfoData();
188  requestCDInfoForMatch();
189  }
190  else
191  cdInfoBuffer_ << line;
192  }
193 
194  break;
195 
196  case WaitingForQuitResponse:
197 
198  state_ = Idle;
199 
200  char c;
201  while ( socket_->bytesAvailable() )
202  socket_->getChar(&c);
203 
204  close();
205 
206  emit finished( result_ );
207 
208  break;
209 
210  default:
211 
212  break;
213  }
214  }
215 
216  QString
217  AsyncCDDBPLookup::readLine()
218  {
219  return QString::fromUtf8(socket_->readLine());
220  }
221 
222  void
223  AsyncCDDBPLookup::doHandshake()
224  {
225  sendHandshake();
226 
227  state_ = WaitingForHandshake;
228  }
229 
230  void
231  AsyncCDDBPLookup::doProto()
232  {
233  sendProto();
234 
235  state_ = WaitingForProtoResponse;
236  }
237 
238  void
239  AsyncCDDBPLookup::doQuery()
240  {
241  sendQuery();
242 
243  state_ = WaitingForQueryResponse;
244  }
245 
246  void
247  AsyncCDDBPLookup::requestCDInfoForMatch()
248  {
249  if (matchList_.isEmpty())
250  {
251  result_ = cdInfoList_.isEmpty()? NoRecordFound : Success;
252  doQuit();
253  return;
254  }
255 
256  CDDBMatch match = matchList_.takeFirst();
257 
258  sendRead( match );
259 
260  state_ = WaitingForCDInfoResponse;
261  }
262 
263  void
264  AsyncCDDBPLookup::parseCDInfoData()
265  {
266  CDInfo info;
267 
268  if (info.load( cdInfoBuffer_ ))
269  {
270  info.set( QLatin1String( "category" ), category_ );
271  info.set( QLatin1String( "discid" ), discid_ );
272  info.set( QLatin1String( "source" ), QLatin1String( "freedb" ) );
273  cdInfoList_.append( info );
274  }
275 
276  cdInfoBuffer_.clear();
277  }
278 
279  void
280  AsyncCDDBPLookup::doQuit()
281  {
282  state_ = WaitingForQuitResponse;
283 
284  sendQuit();
285  }
286 
287  QString
288  AsyncCDDBPLookup::stateToString() const
289  {
290  switch (state_)
291  {
292  case Idle:
293  return QLatin1String( "Idle" );
294  break;
295 
296  case WaitingForConnection:
297  return QLatin1String( "WaitingForConnection" );
298  break;
299 
300  case WaitingForGreeting:
301  return QLatin1String( "WaitingForGreeting" );
302  break;
303 
304  case WaitingForProtoResponse:
305  return QLatin1String( "WaitingForProtoResponse" );
306  break;
307 
308  case WaitingForHandshake:
309  return QLatin1String( "WaitingForHandshake" );
310  break;
311 
312  case WaitingForQueryResponse:
313  return QLatin1String( "WaitingForQueryResponse" );
314  break;
315 
316  case WaitingForMoreMatches:
317  return QLatin1String( "WaitingForMoreMatches" );
318  break;
319 
320  case WaitingForCDInfoResponse:
321  return QLatin1String( "WaitingForCDInfoResponse" );
322  break;
323 
324  case WaitingForCDInfoData:
325  return QLatin1String( "WaitingForCDInfoData" );
326  break;
327 
328  case WaitingForQuitResponse:
329  return QLatin1String( "WaitingForQuitResponse" );
330  break;
331 
332  default:
333  return QLatin1String( "Unknown" );
334  break;
335  }
336  }
337 }
338 
339 
340 #include "asynccddbplookup.moc"
341 
342 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
KCDDB::CDDBPLookup::sendQuery
void sendQuery()
Definition: cddbplookup.cpp:62
QList::clear
void clear()
KCDDB::Lookup::cdInfoList_
CDInfoList cdInfoList_
Definition: lookup.h:60
KCDDB::AsyncCDDBPLookup::doQuit
void doQuit()
Definition: asynccddbplookup.cpp:280
KCDDB::NoResponse
Definition: kcddb.h:42
KCDDB::AsyncCDDBPLookup::readLine
QString readLine()
Definition: asynccddbplookup.cpp:217
KCDDB::AsyncCDDBPLookup::WaitingForProtoResponse
Definition: asynccddbplookup.h:41
KCDDB::AsyncCDDBPLookup::slotReadyRead
void slotReadyRead()
Definition: asynccddbplookup.cpp:85
KCDDB::NoRecordFound
Definition: kcddb.h:43
KCDDB::CDDBPLookup::sendHandshake
void sendHandshake()
Definition: cddbplookup.cpp:44
KCDDB::Lookup::discid_
QString discid_
Definition: lookup.h:63
KCDDB::AsyncCDDBPLookup::AsyncCDDBPLookup
AsyncCDDBPLookup()
Definition: asynccddbplookup.cpp:29
KCDDB::AsyncCDDBPLookup::~AsyncCDDBPLookup
virtual ~AsyncCDDBPLookup()
Definition: asynccddbplookup.cpp:36
QAbstractSocket::canReadLine
virtual bool canReadLine() const
KCDDB::CDDBPLookup::sendRead
void sendRead(const CDDBMatch &)
Definition: cddbplookup.cpp:72
KCDDB::AsyncCDDBPLookup::WaitingForCDInfoResponse
Definition: asynccddbplookup.h:44
asynccddbplookup.h
KCDDB::CDDBPLookup
Definition: cddbplookup.h:31
KCDDB::AsyncCDDBPLookup::Idle
Definition: asynccddbplookup.h:37
KCDDB::CDDBPLookup::close
void close()
Definition: cddbplookup.cpp:91
KCDDB::CDDBPLookup::sendQuit
void sendQuit()
Definition: cddbplookup.cpp:85
KCDDB::AsyncCDDBPLookup::WaitingForGreeting
Definition: asynccddbplookup.h:39
KCDDB::Lookup::matchList_
CDDBMatchList matchList_
Definition: lookup.h:61
QIODevice::getChar
bool getChar(char *c)
KCDDB::AsyncCDDBPLookup::slotConnectionSuccess
void slotConnectionSuccess()
Definition: asynccddbplookup.cpp:78
KCDDB::CDDBPLookup::parseGreeting
bool parseGreeting(const QString &)
Definition: cddbplookup.cpp:101
KCDDB::AsyncCDDBPLookup::WaitingForMoreMatches
Definition: asynccddbplookup.h:43
QList::append
void append(const T &value)
QString::fromUtf8
QString fromUtf8(const char *str, int size)
KCDDB::CDInfo
Information about a CD.
Definition: cdinfo.h:117
KCDDB::UnknownError
Definition: kcddb.h:47
QList::isEmpty
bool isEmpty() const
KCDDB::Success
Definition: kcddb.h:39
KCDDB::Lookup::parseQuery
Result parseQuery(const QString &)
Definition: lookup.cpp:39
KCDDB::AsyncCDDBPLookup::parseCDInfoData
void parseCDInfoData()
Definition: asynccddbplookup.cpp:264
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QAbstractSocket::bytesAvailable
virtual qint64 bytesAvailable() const
KCDDB::CDInfo::set
void set(const QString &type, const QVariant &data)
Set any data from this disc.
Definition: cdinfo.cpp:522
KCDDB::ServerError
Definition: kcddb.h:40
KCDDB::AsyncCDDBPLookup::read
void read()
Definition: asynccddbplookup.cpp:94
KCDDB::HostNotFound
Definition: kcddb.h:41
QString
QList< uint >
KCDDB::Lookup::category_
QString category_
Definition: lookup.h:62
KCDDB::MultipleRecordFound
Definition: kcddb.h:44
QPair
KCDDB::AsyncCDDBPLookup::stateToString
QString stateToString() const
Definition: asynccddbplookup.cpp:288
KCDDB::AsyncCDDBPLookup::requestCDInfoForMatch
void requestCDInfoForMatch()
Definition: asynccddbplookup.cpp:247
KCDDB::AsyncCDDBPLookup::WaitingForCDInfoData
Definition: asynccddbplookup.h:45
KCDDB::AsyncCDDBPLookup::WaitingForQuitResponse
Definition: asynccddbplookup.h:46
KCDDB::AsyncCDDBPLookup::doProto
void doProto()
Definition: asynccddbplookup.cpp:231
KCDDB::CDDBPLookup::parseHandshake
bool parseHandshake(const QString &)
Definition: cddbplookup.cpp:124
KCDDB::AsyncCDDBPLookup::doHandshake
void doHandshake()
Definition: asynccddbplookup.cpp:223
KCDDB::AsyncCDDBPLookup::slotGotError
void slotGotError(QAbstractSocket::SocketError error)
Definition: asynccddbplookup.cpp:65
KCDDB::CDDBPLookup::sendProto
void sendProto()
Definition: cddbplookup.cpp:56
QList::takeFirst
T takeFirst()
QLatin1String
KCDDB::Result
Result
Definition: kcddb.h:37
KCDDB::AsyncCDDBPLookup::doQuery
void doQuery()
Definition: asynccddbplookup.cpp:239
KCDDB::AsyncCDDBPLookup::finished
void finished(KCDDB::Result)
KCDDB::CDDBPLookup::isConnected
bool isConnected()
Definition: cddbplookup.h:50
KCDDB::AsyncCDDBPLookup::WaitingForConnection
Definition: asynccddbplookup.h:38
KCDDB::CDInfo::load
bool load(const QString &string)
Load CDInfo from a string that is CDDB compatible.
Definition: cdinfo.cpp:282
KCDDB::Lookup::parseRead
Result parseRead(const QString &)
Definition: lookup.cpp:69
KCDDB::Lookup::parseExtraMatch
void parseExtraMatch(const QString &)
Definition: lookup.cpp:62
KCDDB::AsyncCDDBPLookup::WaitingForQueryResponse
Definition: asynccddbplookup.h:42
KCDDB::CDDBPLookup::socket_
QTcpSocket * socket_
Definition: cddbplookup.h:53
KCDDB::AsyncCDDBPLookup::WaitingForHandshake
Definition: asynccddbplookup.h:40
KCDDB::AsyncCDDBPLookup::lookup
Result lookup(const QString &, uint, const TrackOffsetList &)
Definition: asynccddbplookup.cpp:42
QIODevice::readLine
qint64 readLine(char *data, qint64 maxSize)
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