summaryrefslogtreecommitdiffstats
path: root/libkcddb/cddb.cpp
blob: 841d52154b57535a682de4b62f76961cbe864b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
  Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
  Copyright (C) 2002 Benjamin Meyer <ben-devel@meyerhome.net>
  CopyRight (C) 2002 Nadeem Hasan <nhasan@kde.org>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include <tqregexp.h>
#include <tqstringlist.h>

#include <kdebug.h>
#include <kstringhandler.h>
#include <tdelocale.h>

#include "cddb.h"

namespace KCDDB
{
  CDDB::CDDB()
    : user_( "libkcddb-user" ),
      localHostName_( "localHost" ),
      readOnly_( false )
  {

  }

  CDDB::~CDDB()
  {
    // Empty.
  }

    TQString
  CDDB::trackOffsetListToId()
  {
    return trackOffsetListToId( trackOffsetList_ );
  }
    TQString
  CDDB::trackOffsetListToId( const TrackOffsetList & list )
  {
    // Taken from version by Michael Matz in tdeio_audiocd.
    unsigned int id = 0;
    int numTracks = list.count() - 2;

    // The last two in the list are disc begin and disc end.
    for ( int i = numTracks-1; i >= 0; i-- )
    {
      int n = list[ i ]/75;
      while ( n > 0 )
      {
        id += n % 10;
        n /= 10;
      }
    }

    unsigned int l = list[numTracks + 1] / 75;
    l -= list[0] / 75;

    id = ( ( id % 255 ) << 24 ) | ( l << 8 ) | numTracks;

    return TQString::number( id, 16 ).rightJustify( 8, '0' );
  }

    TQString
  CDDB::trackOffsetListToString()
  {
    TQString ret;
    uint numTracks = trackOffsetList_.count()-2;

    // Disc start.
    ret.append( TQString::number( numTracks ) );
    ret.append( " " );

    for ( uint i = 0; i < numTracks; i++ )
    {
      ret.append( TQString::number( trackOffsetList_[ i ] ) );
      ret.append( " " );
    }

    unsigned int discLengthInSec = ( trackOffsetList_[ numTracks+1 ] ) / 75;

    ret.append( TQString::number( discLengthInSec ) );

    return ret;
  }

    bool
  CDDB::parseGreeting( const TQString & line )
  {
    uint serverStatus = statusCode( line );

    if ( 200 == serverStatus )
    {
      kdDebug(60010) << "Server response: read-only" << endl;
      readOnly_ = true;
    }
    else if ( 201 == serverStatus )
    {
      kdDebug(60010) << "Server response: read-write" << endl;
    }
    else
    {
      kdDebug(60010) << "Server response: bugger off" << endl;
      return false;
    }

    return true;
  }

    bool
  CDDB::parseHandshake( const TQString & line )
  {
    uint serverStatus = statusCode( line );

    if ( ( 200 != serverStatus ) && ( 402 != serverStatus ) )
    {
      kdDebug(60010) << "Handshake was too tight. Letting go." << endl;
      return false;
    }

    kdDebug(60010) << "Handshake was warm and firm" << endl;

    return true;
  }

    uint
  CDDB::statusCode( const TQString & line )
  {
    TQStringList tokenList = TQStringList::split( ' ', line );

    uint serverStatus = tokenList[ 0 ].toUInt();

    return serverStatus;
  }

/*    CDDB::Transport
  CDDB::stringToTransport(const TQString & s)
  {
    if  ("HTTP" == s )
      return HTTP;
    else if ( "CDDBP" == s )
      return CDDBP;
    else
      return SMTP;
  }*/

    TQString
  CDDB::resultToString(Result r)
  {
    switch (r)
    {
      case Success:
        return i18n("Success");
        break;

      case ServerError:
        return i18n("Server error");
        break;

      case HostNotFound:
        return i18n("Host not found");
        break;

      case NoResponse:
        return i18n("No response");
        break;

      case NoRecordFound:
        return i18n("No record found");
        break;

      case MultipleRecordFound:
        return i18n("Multiple records found");
        break;

      case CannotSave:
        return i18n("Cannot save");
        break;

      case InvalidCategory:
        return i18n("Invalid category");
        break;

      default:
        return i18n("Unknown error");
        break;
    }
  }

/*    TQString
  CDDB::transportToString(uint t)
  {
    switch (Transport(t))
    {
      case HTTP:
        return "HTTP";
        break;

      case CDDBP:
        return "CDDBP";
        break;

      case SMTP:
        return "SMTP";
        break;

      default:
        return "UnknownTransport";
        break;
    }
  }*/
}