• 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
synccddbplookup.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 "synccddbplookup.h"
23 
24 #include <qstringlist.h>
25 #include <kdebug.h>
26 #include <ksocketfactory.h>
27 
28 namespace KCDDB
29 {
30  SyncCDDBPLookup::SyncCDDBPLookup()
31  : CDDBPLookup()
32  {
33  }
34 
35  SyncCDDBPLookup::~SyncCDDBPLookup()
36  {
37  // Empty.
38  }
39 
40  Result
41  SyncCDDBPLookup::lookup
42  (
43  const QString & hostName,
44  uint port,
45  const TrackOffsetList & trackOffsetList
46  )
47  {
48  trackOffsetList_ = trackOffsetList;
49 
50  socket_ = KSocketFactory::synchronousConnectToHost(QLatin1String( "cddbp" ), hostName, port);
51 
52  if ( !socket_->isValid() )
53  {
54  kDebug(60010) << "Couldn't connect to " << socket_->peerName() << ":" << socket_->peerPort();
55  kDebug(60010) << "Socket error: " << socket_->errorString();
56 
57  if ( socket_->error() == QAbstractSocket::HostNotFoundError )
58  return HostNotFound;
59  else if ( socket_->error() == QAbstractSocket::SocketTimeoutError )
60  return NoResponse;
61  else
62  return UnknownError;
63  }
64 
65  Result result;
66 
67  // Try a handshake.
68  result = shakeHands();
69  if ( Success != result )
70  return result;
71 
72  // Run a query.
73  result = runQuery();
74  if ( Success != result )
75  return result;
76 
77  if (matchList_.isEmpty())
78  return NoRecordFound;
79 
80  kDebug(60010) << matchList_.count() << " matches found.";
81 
82  // For each match, read the cd info from the server and save it to
83  // cdInfoList.
84  CDDBMatchList::ConstIterator matchIt = matchList_.constBegin();
85 
86  while ( matchIt != matchList_.constEnd() )
87  {
88  CDDBMatch match( *matchIt );
89  result = matchToCDInfo( match );
90  ++matchIt;
91  }
92 
93  sendQuit();
94 
95  close();
96 
97  return Success;
98  }
99 
100  Result
101  SyncCDDBPLookup::shakeHands()
102  {
103  QString line = readLine();
104 
105  if ( !parseGreeting( line ) )
106  return ServerError;
107 
108  sendHandshake();
109 
110  line = readLine();
111 
112  if ( !parseHandshake( line ) )
113  return ServerError;
114 
115  sendProto();
116 
117  // Ignore the response for now
118  readLine();
119 
120  return Success;
121  }
122 
123  Result
124  SyncCDDBPLookup::runQuery()
125  {
126  Result result;
127 
128  sendQuery();
129 
130  QString line = readLine();
131  result = parseQuery( line );
132 
133  if ( ServerError == result )
134  return ServerError;
135 
136  if ( MultipleRecordFound == result )
137  {
138  // We have multiple matches
139  line = readLine();
140 
141  while (!line.startsWith(QLatin1String( "." )) && !line.isNull() )
142  {
143  parseExtraMatch( line );
144  line = readLine();
145  }
146  }
147 
148  return Success;
149  }
150 
151  Result
152  SyncCDDBPLookup::matchToCDInfo( const CDDBMatch & match )
153  {
154  sendRead( match );
155 
156  QString line = readLine();
157 
158  Result result = parseRead( line );
159  if ( Success != result )
160  return result;
161 
162  QStringList lineList;
163  line = readLine();
164 
165  while ( !line.startsWith(QLatin1String( "." )) && !line.isNull() )
166  {
167  lineList.append( line );
168  line = readLine();
169  }
170 
171  CDInfo info;
172 
173  if ( info.load( lineList ) )
174  {
175  info.set( QLatin1String( "category" ), category_ );
176  info.set( QLatin1String( "discid" ), discid_ );
177  info.set( QLatin1String( "source" ), QLatin1String( "freedb" ) );
178  cdInfoList_.append( info );
179  }
180 
181  return Success;
182  }
183 
184  QString
185  SyncCDDBPLookup::readLine()
186  {
187  if ( !isConnected() )
188  {
189  kDebug(60010) << "socket status: " << socket_->state();
190  return QString();
191  }
192 
193  if (!socket_->canReadLine())
194  {
195  if (!socket_->waitForReadyRead(-1))
196  return QString();
197  }
198 
199  return QString::fromUtf8(socket_->readLine());
200  }
201 }
202 
203 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
KCDDB::CDDBPLookup::sendQuery
void sendQuery()
Definition: cddbplookup.cpp:62
KCDDB::Lookup::cdInfoList_
CDInfoList cdInfoList_
Definition: lookup.h:60
KCDDB::NoResponse
Definition: kcddb.h:42
KCDDB::NoRecordFound
Definition: kcddb.h:43
KCDDB::CDDBPLookup::sendHandshake
void sendHandshake()
Definition: cddbplookup.cpp:44
QAbstractSocket::state
SocketState state() const
KCDDB::Lookup::discid_
QString discid_
Definition: lookup.h:63
QAbstractSocket::canReadLine
virtual bool canReadLine() const
KCDDB::CDDBPLookup::sendRead
void sendRead(const CDDBMatch &)
Definition: cddbplookup.cpp:72
KCDDB::SyncCDDBPLookup::lookup
Result lookup(const QString &, uint, const TrackOffsetList &)
Definition: synccddbplookup.cpp:42
KCDDB::SyncCDDBPLookup::matchToCDInfo
Result matchToCDInfo(const CDDBMatch &)
Definition: synccddbplookup.cpp:152
KCDDB::CDDBPLookup
Definition: cddbplookup.h:31
QString::isNull
bool isNull() const
synccddbplookup.h
KCDDB::CDDBPLookup::parseGreeting
bool parseGreeting(const QString &)
Definition: cddbplookup.cpp:101
KCDDB::SyncCDDBPLookup::SyncCDDBPLookup
SyncCDDBPLookup()
Definition: synccddbplookup.cpp:30
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
KCDDB::Success
Definition: kcddb.h:39
KCDDB::Lookup::parseQuery
Result parseQuery(const QString &)
Definition: lookup.cpp:39
KCDDB::SyncCDDBPLookup::~SyncCDDBPLookup
virtual ~SyncCDDBPLookup()
Definition: synccddbplookup.cpp:35
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) 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::HostNotFound
Definition: kcddb.h:41
QString
QList< uint >
KCDDB::Lookup::category_
QString category_
Definition: lookup.h:62
KCDDB::MultipleRecordFound
Definition: kcddb.h:44
QStringList
QPair
KCDDB::SyncCDDBPLookup::readLine
QString readLine()
Definition: synccddbplookup.cpp:185
KCDDB::CDDBPLookup::parseHandshake
bool parseHandshake(const QString &)
Definition: cddbplookup.cpp:124
KCDDB::SyncCDDBPLookup::runQuery
Result runQuery()
Definition: synccddbplookup.cpp:124
KCDDB::CDDBPLookup::sendProto
void sendProto()
Definition: cddbplookup.cpp:56
QLatin1String
KCDDB::Result
Result
Definition: kcddb.h:37
QList< CDDBMatch >::ConstIterator
typedef ConstIterator
KCDDB::CDDBPLookup::isConnected
bool isConnected()
Definition: cddbplookup.h:50
KCDDB::SyncCDDBPLookup::shakeHands
Result shakeHands()
Definition: synccddbplookup.cpp:101
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
QAbstractSocket::waitForReadyRead
virtual bool waitForReadyRead(int msecs)
KCDDB::CDDBPLookup::socket_
QTcpSocket * socket_
Definition: cddbplookup.h:53
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