• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdemultimedia API Reference
  • KDE Home
  • Contact Us
 

libkcddb

  • sources
  • kde-4.14
  • kdemultimedia
  • libkcddb
  • libkcddb
cddb.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
3  Copyright (C) 2002 Benjamin Meyer <ben-devel@meyerhome.net>
4  CopyRight (C) 2002 Nadeem Hasan <nhasan@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "cddb.h"
23 
24 #include "categories.h"
25 
26 #include <qstringlist.h>
27 
28 #include <kdebug.h>
29 #include <kstringhandler.h>
30 #include <klocale.h>
31 
32 namespace KCDDB
33 {
34  CDDB::CDDB()
35  : user_( QLatin1String( "libkcddb-user" ) ),
36  localHostName_( QLatin1String( "localHost" ) ),
37  readOnly_( false )
38  {
39 
40  }
41 
42  CDDB::~CDDB()
43  {
44  // Empty.
45  }
46 
47  QString
48  CDDB::trackOffsetListToId()
49  {
50  return trackOffsetListToId( trackOffsetList_ );
51  }
52  QString
53  CDDB::trackOffsetListToId( const TrackOffsetList & list )
54  {
55  // Taken from version by Michael Matz in kio_audiocd.
56  unsigned int id = 0;
57  if ( list.isEmpty() )
58  return QString();
59  int numTracks = list.count() - 1;
60 
61  // The last two in the list are disc begin and disc end.
62  for ( int i = numTracks-1; i >= 0; i-- )
63  {
64  int n = list[ i ]/75;
65  while ( n > 0 )
66  {
67  id += n % 10;
68  n /= 10;
69  }
70  }
71 
72  unsigned int l = list[numTracks] / 75;
73  l -= list[0] / 75;
74 
75  id = ( ( id % 255 ) << 24 ) | ( l << 8 ) | numTracks;
76 
77  return QString::number( id, 16 ).rightJustified( 8, QLatin1Char( '0' ) );
78  }
79 
80  QString
81  CDDB::trackOffsetListToString()
82  {
83  QString ret;
84  uint numTracks = trackOffsetList_.count()-1;
85 
86  // Disc start.
87  ret.append( QString::number( numTracks ) );
88  ret.append( QLatin1String( " " ) );
89 
90  for ( uint i = 0; i < numTracks; i++ )
91  {
92  ret.append( QString::number( trackOffsetList_[ i ] ) );
93  ret.append( QLatin1String( " " ) );
94  }
95 
96  unsigned int discLengthInSec = ( trackOffsetList_[ numTracks ] ) / 75;
97 
98  ret.append( QString::number( discLengthInSec ) );
99 
100  return ret;
101  }
102 
103  uint
104  CDDB::statusCode( const QString & line )
105  {
106  QStringList tokenList = line.split(QLatin1Char( ' ' ), QString::SkipEmptyParts );
107 
108  uint serverStatus = tokenList[ 0 ].toUInt();
109 
110  return serverStatus;
111  }
112 
113  CDInfoList
114  CDDB::cacheFiles(const TrackOffsetList &offsetList, const Config& config )
115  {
116  Categories c;
117  QStringList categories = c.cddbList();
118  // Also load user-created entries
119  categories << QLatin1String( "user" );
120 
121  CDInfoList infoList;
122  QStringList cddbCacheDirs = config.cacheLocations();
123 
124  for (QStringList::const_iterator cddbCacheDir = cddbCacheDirs.constBegin();
125  cddbCacheDir != cddbCacheDirs.constEnd(); ++cddbCacheDir)
126  {
127  foreach(const QString &category, categories)
128  {
129  QFile f( *cddbCacheDir + QLatin1Char( '/' ) + category + QLatin1Char( '/' ) + trackOffsetListToId(offsetList) );
130  if ( f.exists() && f.open(QIODevice::ReadOnly) )
131  {
132  QTextStream ts(&f);
133  ts.setCodec("UTF-8");
134  QString cddbData = ts.readAll();
135  f.close();
136  CDInfo info;
137  info.load(cddbData);
138  if (category != QLatin1String( "user" ))
139  {
140  info.set(Category,category);
141  info.set(QLatin1String( "source" ), QLatin1String( "freedb" ));
142  }
143  else
144  {
145  info.set(QLatin1String( "source" ), QLatin1String( "user" ));
146  }
147 
148  infoList.append( info );
149  }
150  }
151  }
152 
153  return infoList;
154  }
155 }
156 
157 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
QTextStream::setCodec
void setCodec(QTextCodec *codec)
QString::append
QString & append(QChar ch)
categories.h
KCDDB::Category
The freedb category of the entry.
Definition: cdinfo.h:46
KCDDB::CDDB::cacheFiles
static CDInfoList cacheFiles(const TrackOffsetList &, const Config &)
Definition: cddb.cpp:114
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KCDDB::CDDB::trackOffsetList_
TrackOffsetList trackOffsetList_
Definition: cddb.h:57
KCDDB::CDDB::statusCode
static uint statusCode(const QString &)
Definition: cddb.cpp:104
KCDDB::Categories
Category values defined by CDDB.
Definition: categories.h:19
QFile::exists
bool exists() const
QList::const_iterator
QFile
ConfigBase::cacheLocations
QStringList cacheLocations() const
Get cacheLocations.
Definition: configbase.cpp:196
QTextStream
KCDDB::CDDB::trackOffsetListToId
QString trackOffsetListToId()
Definition: cddb.cpp:48
QString::number
QString number(int n, int base)
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KCDDB::CDInfo
Information about a CD.
Definition: cdinfo.h:117
QString::rightJustified
QString rightJustified(int width, QChar fill, bool truncate) const
QList::isEmpty
bool isEmpty() const
KCDDB::Categories::cddbList
const QStringList & cddbList() const
Definition: categories.h:24
KCDDB::CDDB::CDDB
CDDB()
Definition: cddb.cpp:34
KCDDB::CDInfo::set
void set(const QString &type, const QVariant &data)
Set any data from this disc.
Definition: cdinfo.cpp:522
QString
QList< uint >
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QLatin1Char
QFile::close
virtual void close()
cddb.h
QLatin1String
KCDDB::CDDB::trackOffsetListToString
QString trackOffsetListToString()
Definition: cddb.cpp:81
KCDDB::Config
Definition: kcddbconfig.h:31
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
KCDDB::CDInfo::load
bool load(const QString &string)
Load CDInfo from a string that is CDDB compatible.
Definition: cdinfo.cpp:282
QTextStream::readAll
QString readAll()
KCDDB::CDDB::~CDDB
virtual ~CDDB()
Definition: cddb.cpp:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkcddb

Skip menu "libkcddb"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdemultimedia API Reference

Skip menu "kdemultimedia API Reference"
  • libkcddb
  • libkcompactdisc

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal