KCDDB

cddbplookup.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Rik Hemsley ( rikkus ) <[email protected]>
3  SPDX-FileCopyrightText: 2002 Benjamin Meyer <[email protected]>
4  SPDX-FileCopyrightText: 2002 Nadeem Hasan <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "cddbplookup.h"
10 #include "logging.h"
11 
12 #include <QByteArray>
13 
14 namespace KCDDB
15 {
16  CDDBPLookup::CDDBPLookup()
17  : Lookup()
18  , socket_(nullptr)
19  {
20 
21  }
22 
23  CDDBPLookup::~CDDBPLookup()
24  {
25  if (socket_)
26  delete socket_;
27  }
28 
29  void
30  CDDBPLookup::sendHandshake()
31  {
32  QString handshake = QString::fromLatin1( "cddb hello %1 %2 %3 %4" )
33  .arg( user_ )
34  .arg( localHostName_ )
35  .arg( clientName() )
36  .arg( clientVersion() );
37 
38  writeLine( handshake );
39  }
40 
41  void
42  CDDBPLookup::sendProto()
43  {
44  writeLine( QLatin1String( "proto 6" ) );
45  }
46 
47  void
48  CDDBPLookup::sendQuery()
49  {
50  QString query = QString::fromLatin1( "cddb query %1 %2" )
51  .arg( trackOffsetListToId() )
52  .arg( trackOffsetListToString() );
53 
54  writeLine( query );
55  }
56 
57  void
58  CDDBPLookup::sendRead( const CDDBMatch & match )
59  {
60  category_ = match.first;
61  discid_ = match.second;
62 
63  QString readRequest = QString::fromLatin1( "cddb read %1 %2" )
64  .arg( category_ )
65  .arg( discid_ );
66 
67  writeLine( readRequest );
68  }
69 
70  void
71  CDDBPLookup::sendQuit()
72  {
73  writeLine( QLatin1String( "quit" ) );
74  }
75 
76  void
77  CDDBPLookup::close()
78  {
79  qCDebug(LIBKCDDB) << "Disconnect from server...";
80  if ( isConnected() )
81  {
82  socket_->close();
83  }
84  }
85 
86  bool
87  CDDBPLookup::parseGreeting( const QString & line )
88  {
89  uint serverStatus = statusCode( line );
90 
91  if ( 200 == serverStatus )
92  {
93  qCDebug(LIBKCDDB) << "Server response: read-only";
94  readOnly_ = true;
95  }
96  else if ( 201 == serverStatus )
97  {
98  qCDebug(LIBKCDDB) << "Server response: read-write";
99  }
100  else
101  {
102  qCDebug(LIBKCDDB) << "Server response: bugger off";
103  return false;
104  }
105 
106  return true;
107  }
108 
109  bool
110  CDDBPLookup::parseHandshake( const QString & line )
111  {
112  uint serverStatus = statusCode( line );
113 
114  if ( ( 200 != serverStatus ) && ( 402 != serverStatus ) )
115  {
116  qCDebug(LIBKCDDB) << "Handshake was too tight. Letting go.";
117  return false;
118  }
119 
120  qCDebug(LIBKCDDB) << "Handshake was warm and firm";
121 
122  return true;
123  }
124 
125 
126  qint64
127  CDDBPLookup::writeLine( const QString & line )
128  {
129  if ( !isConnected() )
130  {
131  qCDebug(LIBKCDDB) << "socket status: " << socket_->state();
132  return -1;
133  }
134 
135  qCDebug(LIBKCDDB) << "WRITE: [" << line << "]";
136  QByteArray buf(line.toUtf8());
137  buf.append( '\n' );
138 
139  return socket_->write( buf );
140  }
141 }
142 
143 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
std::optional< QSqlQuery > query(const QString &queryStatement)
QByteArray & append(char ch)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:10:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.