KCDDB

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

KDE's Doxygen guidelines are available online.