cddb.cpp 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
  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
Dirk Mueller's avatar
Dirk Mueller committed
18
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
19
  Boston, MA 02110-1301, USA.
20 21
*/

22 23 24
#include "cddb.h"

#include "categories.h"
25
#include "kcddbi18n.h"
26

Michał Małek's avatar
Michał Małek committed
27
#include <KCoreAddons/KStringHandler>
28

Yuri Chornoivan's avatar
Yuri Chornoivan committed
29 30
#include <QDebug>
#include <QStringList>
31 32 33 34

namespace KCDDB
{
  CDDB::CDDB()
35 36
    : user_( QLatin1String( "libkcddb-user" ) ),
      localHostName_( QLatin1String( "localHost" ) ),
37 38
      readOnly_( false )
  {
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  }

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

    QString
  CDDB::trackOffsetListToId()
  {
    return trackOffsetListToId( trackOffsetList_ );
  }
    QString
  CDDB::trackOffsetListToId( const TrackOffsetList & list )
  {
    // Taken from version by Michael Matz in kio_audiocd.
    unsigned int id = 0;
Laurent Montel's avatar
Laurent Montel committed
57 58
    if ( list.isEmpty() )
        return QString();
59
    int numTracks = list.count() - 1;
60 61 62 63 64 65 66 67 68 69 70 71

    // 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;
      }
    }

72
    unsigned int l = list[numTracks] / 75;
73
    l -= list[0] / 75;
74 75 76

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

77
    return QString::number( id, 16 ).rightJustified( 8, QLatin1Char( '0' ) );
78 79 80 81 82 83
  }

    QString
  CDDB::trackOffsetListToString()
  {
    QString ret;
84
    uint numTracks = trackOffsetList_.count()-1;
85 86

    // Disc start.
87
    ret.append( QString::number( numTracks ) );
88
    ret.append( QLatin1String( " " ) );
89 90 91 92

    for ( uint i = 0; i < numTracks; i++ )
    {
      ret.append( QString::number( trackOffsetList_[ i ] ) );
93
      ret.append( QLatin1String( " " ) );
94 95
    }

96
    unsigned int discLengthInSec = ( trackOffsetList_[ numTracks ] ) / 75;
97 98 99 100 101 102 103 104 105

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

    return ret;
  }

    uint
  CDDB::statusCode( const QString & line )
  {
106
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
107
    const QStringList tokenList = line.split(QLatin1Char( ' ' ), QString::SkipEmptyParts );
108
#else
109
    const QStringList tokenList = line.split(QLatin1Char( ' ' ), Qt::SkipEmptyParts );
110
#endif
111

112 113
    if (tokenList.isEmpty())
        return 410;
114

115
    return tokenList[ 0 ].toUInt();
116
  }
117

118 119
    CDInfoList
  CDDB::cacheFiles(const TrackOffsetList &offsetList, const Config& config )
120 121 122
  {
    Categories c;
    QStringList categories = c.cddbList();
123
    // Also load user-created entries
124
    categories << QLatin1String( "user" );
125

126 127 128
    CDInfoList infoList;
    QStringList cddbCacheDirs = config.cacheLocations();

Laurent Montel's avatar
Laurent Montel committed
129 130
    for (QStringList::const_iterator cddbCacheDir = cddbCacheDirs.constBegin();
        cddbCacheDir != cddbCacheDirs.constEnd(); ++cddbCacheDir)
131
    {
Albert Astals Cid's avatar
Albert Astals Cid committed
132
      foreach(const QString &category, categories)
133
      {
134
        QFile f( *cddbCacheDir + QLatin1Char( '/' ) + category + QLatin1Char( '/' ) + trackOffsetListToId(offsetList) );
135 136 137 138 139 140 141 142
        if ( f.exists() && f.open(QIODevice::ReadOnly) )
        {
            QTextStream ts(&f);
            ts.setCodec("UTF-8");
            QString cddbData = ts.readAll();
            f.close();
            CDInfo info;
            info.load(cddbData);
143
            if (category != QLatin1String( "user" ))
144 145
            {
              info.set(Category,category);
146
              info.set(QLatin1String( "source" ), QLatin1String( "freedb" ));
147 148 149
            }
            else
            {
150
              info.set(QLatin1String( "source" ), QLatin1String( "user" ));
151 152 153 154 155 156 157 158
            }

            infoList.append( info );
        }
      }
    }

    return infoList;
159
  }
160 161 162
}

// vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1