KCDDB

cddb.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 "cddb.h"
10 
11 #include "categories.h"
12 #include "kcddbi18n.h"
13 
14 #include <KStringHandler>
15 
16 #include <QDebug>
17 #include <QStringList>
18 
19 namespace KCDDB
20 {
21  CDDB::CDDB()
22  : user_( QLatin1String( "libkcddb-user" ) ),
23  localHostName_( QLatin1String( "localHost" ) ),
24  readOnly_( false )
25  {
26 
27  }
28 
29  CDDB::~CDDB()
30  {
31  // Empty.
32  }
33 
34  QString
35  CDDB::trackOffsetListToId()
36  {
37  return trackOffsetListToId( trackOffsetList_ );
38  }
39  QString
40  CDDB::trackOffsetListToId( const TrackOffsetList & list )
41  {
42  // Taken from version by Michael Matz in kio_audiocd.
43  unsigned int id = 0;
44  if ( list.isEmpty() )
45  return QString();
46  int numTracks = list.count() - 1;
47 
48  // The last two in the list are disc begin and disc end.
49  for ( int i = numTracks-1; i >= 0; i-- )
50  {
51  int n = list[ i ]/75;
52  while ( n > 0 )
53  {
54  id += n % 10;
55  n /= 10;
56  }
57  }
58 
59  unsigned int l = list[numTracks] / 75;
60  l -= list[0] / 75;
61 
62  id = ( ( id % 255 ) << 24 ) | ( l << 8 ) | numTracks;
63 
64  return QString::number( id, 16 ).rightJustified( 8, QLatin1Char( '0' ) );
65  }
66 
67  QString
68  CDDB::trackOffsetListToString()
69  {
70  QString ret;
71  uint numTracks = trackOffsetList_.count()-1;
72 
73  // Disc start.
74  ret.append( QString::number( numTracks ) );
75  ret.append( QLatin1String( " " ) );
76 
77  for ( uint i = 0; i < numTracks; i++ )
78  {
79  ret.append( QString::number( trackOffsetList_[ i ] ) );
80  ret.append( QLatin1String( " " ) );
81  }
82 
83  unsigned int discLengthInSec = ( trackOffsetList_[ numTracks ] ) / 75;
84 
85  ret.append( QString::number( discLengthInSec ) );
86 
87  return ret;
88  }
89 
90  uint
91  CDDB::statusCode( const QString & line )
92  {
93 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
94  const QStringList tokenList = line.split(QLatin1Char( ' ' ), QString::SkipEmptyParts );
95 #else
96  const QStringList tokenList = line.split(QLatin1Char( ' ' ), Qt::SkipEmptyParts );
97 #endif
98 
99  if (tokenList.isEmpty())
100  return 410;
101 
102  return tokenList[ 0 ].toUInt();
103  }
104 
105  CDInfoList
106  CDDB::cacheFiles(const TrackOffsetList &offsetList, const Config& config )
107  {
108  Categories c;
109  QStringList categories = c.cddbList();
110  // Also load user-created entries
111  categories << QLatin1String( "user" );
112 
113  CDInfoList infoList;
114  QStringList cddbCacheDirs = config.cacheLocations();
115 
116  for (QStringList::const_iterator cddbCacheDir = cddbCacheDirs.constBegin();
117  cddbCacheDir != cddbCacheDirs.constEnd(); ++cddbCacheDir)
118  {
119  for (const QString &category : qAsConst(categories)) {
120  QFile f( *cddbCacheDir + QLatin1Char( '/' ) + category + QLatin1Char( '/' ) + trackOffsetListToId(offsetList) );
121  if ( f.exists() && f.open(QIODevice::ReadOnly) )
122  {
123  QTextStream ts(&f);
124  ts.setCodec("UTF-8");
125  QString cddbData = ts.readAll();
126  f.close();
127  CDInfo info;
128  info.load(cddbData);
129  if (category != QLatin1String( "user" ))
130  {
131  info.set(Category,category);
132  info.set(QLatin1String( "source" ), QLatin1String( "freedb" ));
133  }
134  else
135  {
136  info.set(QLatin1String( "source" ), QLatin1String( "user" ));
137  }
138 
139  infoList.append( info );
140  }
141  }
142  }
143 
144  return infoList;
145  }
146 }
147 
148 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
QString & append(QChar ch)
QString number(int n, int base)
QString rightJustified(int width, QChar fill, bool truncate) const const
bool isEmpty() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
SkipEmptyParts
int count() const const
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:22:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.