summaryrefslogtreecommitdiffstats
path: root/libkcddb/cdinfo.cpp
blob: f41b2f2c0a16cdc0d61dfdb7d853cc97dc2adc59 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
  Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
  Copyright (C) 2002 Benjamin Meyer <ben-devel@meyerhome.net>
  Copyright (C) 2002-2004 Nadeem Hasan <nhasan@nadmm.com>

  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 <kdebug.h>
#include <kstringhandler.h>

#include "cdinfo.h"
#include "cddb.h"

namespace KCDDB
{
  TrackInfo::TrackInfo()
  {
  }

  TrackInfo::TrackInfo(const TrackInfo& clone)
      : title(clone.title),
        extt(clone.extt)
  {
  }

  TrackInfo::~TrackInfo()
  {
  }

  TrackInfo& TrackInfo::operator=(const TrackInfo& clone)
  {
    title = clone.title;
    extt = clone.extt;
    return *this;
  }


  CDInfo::CDInfo()
    : year(0),
      length(0),
      revision(0)
  {
  }

  CDInfo::CDInfo(const CDInfo& clone)
    : id(clone.id),
      artist(clone.artist),
      title(clone.title),
      genre(clone.genre),
      category(clone.category),
      extd(clone.extd),
      year(clone.year),
      length(clone.length),
      revision(clone.revision),
      trackInfoList(clone.trackInfoList)

  {
  }

  CDInfo::~CDInfo()
  {
  }

  CDInfo& CDInfo::operator=(const CDInfo& clone)
  {
    id = clone.id;
    artist = clone.artist;
    title = clone.title;
    genre = clone.genre;
    category = clone.category;
    extd = clone.extd;
    year = clone.year;
    length = clone.length;
    revision = clone.revision;
    trackInfoList = clone.trackInfoList;
    return *this;
  }

  TQVariant TrackInfo::get(const TQString &type) const {
    if(type == "title")
      return title;
    if(type == "extt")
      return extt;
    return TQVariant();
  }
  
    bool
  CDInfo::load(const TQString & s)
  {
    return load(TQStringList::split('\n', s));
  }

    bool
  CDInfo::load(const TQStringList & lineList)
  {
    clear();

    // We'll append to this until we've seen all the lines, then parse it after.
    TQString dtitle;

    TQStringList::ConstIterator it = lineList.begin();

    TQRegExp rev("# Revision: (\\d+)");

    while ( it != lineList.end() )
    {
      TQString line(*it);
      ++it;

      TQStringList tokenList = KStringHandler::perlSplit('=', line, 2);

      if (rev.search(line) != -1)
      {
        revision = rev.cap(1).toUInt();;
        continue;
      }

      TQString key = tokenList[0].stripWhiteSpace();
      TQString value;
      if (2 != tokenList.count())
      {
        if (!key.startsWith("EXT"))
          continue;
      }
      else
        value = unescape ( tokenList[1].stripWhiteSpace() );

      if ( "DISCID" == key )
      {
        id = value;
      }
      else if ( "DTITLE" == key )
      {
        dtitle += value;
      }
      else if ( "DYEAR" == key )
      {
        year = value.toUInt();
      }
      else if ( "DGENRE" == key )
      {
        genre += value;
      }
      else if ( "TTITLE" == key.left( 6 ) )
      {
        uint trackNumber = key.mid(6).toUInt();

        checkTrack( trackNumber );

        trackInfoList[ trackNumber ].title.append( value );
      }
      else if ( "EXTD" == key )
      {
        extd.append( value );
      }
      else if ( "EXTT" == key.left( 4 ) )
      {
        uint trackNumber = key.mid( 4 ).toUInt();

        checkTrack( trackNumber );

        trackInfoList[ trackNumber ].extt.append( value );
      }
    }

    int slashPos = dtitle.find('/');

    if (-1 == slashPos)
    {
      // Use string for title _and_ artist.
      artist = title = dtitle;
    }
    else
    {
      artist  = dtitle.left(slashPos).stripWhiteSpace();
      title   = dtitle.mid(slashPos + 1).stripWhiteSpace();
    }

    if ( genre.isEmpty() )
      genre = "Unknown";

    kdDebug(60010) << "Loaded CDInfo for " << id << endl;

    return true;
  }

    TQString
  CDInfo::toString(bool submit) const
  {
    TQString s;

    if (revision != 0)
      s += "# Revision: " + TQString::number(revision) + "\n";

    if (submit)
    {
      s += "#\n";
      s += TQString("# Submitted via: %1 %2\n").arg(CDDB::clientName(),
        CDDB::clientVersion());
    }

    s += "DISCID=" + escape( id ) + "\n";
    s += createLine("DTITLE",escape( artist ) + " / " + escape( title ));
    s += "DYEAR=" + (0 == year ? TQString() : TQString::number(year)) + "\n";
    s += createLine("DGENRE",escape( genre ));

    for (uint i = 0; i < trackInfoList.count(); ++i)
    {
      s += createLine(TQString("TTITLE%1").arg(i), 
                escape( trackInfoList[ i ].title));
    }

    s += createLine("EXTD", escape( extd ));

    for (uint i = 0; i < trackInfoList.count(); ++i)
    {
      s += createLine(TQString("EXTT%1").arg(i), escape(trackInfoList[i].extt));
    }

    s +="PLAYORDER=\n";

    return s;
  }

  // Creates a line in the form NAME=VALUE, and splits it into several
  // lines if the line gets longer than 256 chars
    TQString
  CDInfo::createLine(const TQString& name, const TQString& value) const
  {
    Q_ASSERT(name.length() < 254);

    uint maxLength = 256 - name.length() - 2;

    TQString tmpValue = value;

    TQString lines;

    while (tmpValue.length() > maxLength)
    {
      lines += TQString("%1=%2\n").arg(name,tmpValue.left(maxLength));
      tmpValue = tmpValue.mid(maxLength);
    }

    lines += TQString("%1=%2\n").arg(name,tmpValue);

    return lines;
  }

    void
  CDInfo::checkTrack( uint trackNumber )
  {
    if ( trackInfoList.count() < trackNumber + 1 )
    {
      while ( trackInfoList.count() < trackNumber + 1 )
        trackInfoList.append(TrackInfo());
    }
  }

    TQString
  CDInfo::escape( const TQString& value )
  {
    TQString s = value;
    s.replace( "\\", "\\\\" );
    s.replace( "\n", "\\n" );
    s.replace( "\t", "\\t" );

    return s;
  }

    TQString
  CDInfo::unescape( const TQString& value )
  {
    TQString s = value;

    s.replace( "\\n", "\n" );
    s.replace( "\\t", "\t" );
    s.replace( "\\\\", "\\" );

    return s;
  }

    void
  CDInfo::clear()
  {
    id = artist = title = genre = extd = TQString();
    length = year = revision = 0;
    trackInfoList.clear();
  }

    bool
  CDInfo::isValid() const
  {
    if (id.isEmpty())
      return false;

    if (id == "0")
      return false;

    return true;
  }

  TQVariant CDInfo::get(const TQString &type) const {
    if(type == "id")
      return id;
    if(type == "artist")
      return artist;
    if(type == "title")
      return title;
    if(type == "genre")
      return genre;
    if(type == "category")
      return category;
    if(type == "extd")
      return extd;
    if(type == "year")
      return year;
    if(type == "length")
      return length;
    if(type == "revision")
      return revision;
    return TQVariant();
  }
}