File indexing completed on 2024-04-28 16:08:19
0001 /* 0002 SPDX-FileCopyrightText: 2002 Rik Hemsley ( rikkus ) <rik@kde.org> 0003 SPDX-FileCopyrightText: 2002 Benjamin Meyer <ben-devel@meyerhome.net> 0004 SPDX-FileCopyrightText: 2002 Nadeem Hasan <nhasan@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "cddbplookup.h" 0010 #include "logging.h" 0011 0012 #include <QByteArray> 0013 0014 namespace KCDDB 0015 { 0016 CDDBPLookup::CDDBPLookup() 0017 : Lookup() 0018 , socket_(nullptr) 0019 { 0020 0021 } 0022 0023 CDDBPLookup::~CDDBPLookup() 0024 { 0025 if (socket_) 0026 delete socket_; 0027 } 0028 0029 void 0030 CDDBPLookup::sendHandshake() 0031 { 0032 QString handshake = QString::fromLatin1( "cddb hello %1 %2 %3 %4" ) 0033 .arg( user_ ) 0034 .arg( localHostName_ ) 0035 .arg( clientName() ) 0036 .arg( clientVersion() ); 0037 0038 writeLine( handshake ); 0039 } 0040 0041 void 0042 CDDBPLookup::sendProto() 0043 { 0044 writeLine( QLatin1String( "proto 6" ) ); 0045 } 0046 0047 void 0048 CDDBPLookup::sendQuery() 0049 { 0050 QString query = QString::fromLatin1( "cddb query %1 %2" ) 0051 .arg( trackOffsetListToId() ) 0052 .arg( trackOffsetListToString() ); 0053 0054 writeLine( query ); 0055 } 0056 0057 void 0058 CDDBPLookup::sendRead( const CDDBMatch & match ) 0059 { 0060 category_ = match.first; 0061 discid_ = match.second; 0062 0063 QString readRequest = QString::fromLatin1( "cddb read %1 %2" ) 0064 .arg( category_ ) 0065 .arg( discid_ ); 0066 0067 writeLine( readRequest ); 0068 } 0069 0070 void 0071 CDDBPLookup::sendQuit() 0072 { 0073 writeLine( QLatin1String( "quit" ) ); 0074 } 0075 0076 void 0077 CDDBPLookup::close() 0078 { 0079 qCDebug(LIBKCDDB) << "Disconnect from server..."; 0080 if ( isConnected() ) 0081 { 0082 socket_->close(); 0083 } 0084 } 0085 0086 bool 0087 CDDBPLookup::parseGreeting( const QString & line ) 0088 { 0089 uint serverStatus = statusCode( line ); 0090 0091 if ( 200 == serverStatus ) 0092 { 0093 qCDebug(LIBKCDDB) << "Server response: read-only"; 0094 readOnly_ = true; 0095 } 0096 else if ( 201 == serverStatus ) 0097 { 0098 qCDebug(LIBKCDDB) << "Server response: read-write"; 0099 } 0100 else 0101 { 0102 qCDebug(LIBKCDDB) << "Server response: bugger off"; 0103 return false; 0104 } 0105 0106 return true; 0107 } 0108 0109 bool 0110 CDDBPLookup::parseHandshake( const QString & line ) 0111 { 0112 uint serverStatus = statusCode( line ); 0113 0114 if ( ( 200 != serverStatus ) && ( 402 != serverStatus ) ) 0115 { 0116 qCDebug(LIBKCDDB) << "Handshake was too tight. Letting go."; 0117 return false; 0118 } 0119 0120 qCDebug(LIBKCDDB) << "Handshake was warm and firm"; 0121 0122 return true; 0123 } 0124 0125 0126 qint64 0127 CDDBPLookup::writeLine( const QString & line ) 0128 { 0129 if ( !isConnected() ) 0130 { 0131 qCDebug(LIBKCDDB) << "socket status: " << socket_->state(); 0132 return -1; 0133 } 0134 0135 qCDebug(LIBKCDDB) << "WRITE: [" << line << "]"; 0136 QByteArray buf(line.toUtf8()); 0137 buf.append( '\n' ); 0138 0139 return socket_->write( buf ); 0140 } 0141 } 0142 0143 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1