Commit e5e57aaf authored by Richard Lärkäng's avatar Richard Lärkäng
Browse files

Store CDInfo-entries from MusicBrainz, doesn't load yet though

svn path=/trunk/KDE/kdemultimedia/libkcddb/; revision=690982
parent 1e1b38b2
......@@ -264,12 +264,12 @@ namespace KCDDB
AsyncCDDBPLookup::parseCDInfoData()
{
CDInfo info;
info.set("source", "freedb");
if (info.load( cdInfoBuffer_ ))
{
info.set( "category", category_ );
info.set( "discid", discid_ );
info.set( "source", "freedb" );
cdInfoList_.append( info );
}
......
......@@ -2,6 +2,7 @@
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>
Copyright (C) 2007 Richard Lärkäng <nouseforaname@home.se>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -33,17 +34,6 @@
namespace KCDDB
{
QString
Cache::fileName( const QString &category, const QString& discid, const QString &cacheDir )
{
QDir dir( cacheDir );
if ( !dir.exists( category ) )
dir.mkdir( category );
return cacheDir + '/' + category + '/' + discid;
}
CDInfoList
Cache::lookup( const TrackOffsetList &offsetList, const Config& c )
{
......@@ -89,45 +79,86 @@ namespace KCDDB
}
void
Cache::store(const CDInfoList& list, const Config& c)
Cache::store(const TrackOffsetList& offsetList, const CDInfoList& list, const Config& c)
{
CDInfoList::ConstIterator it=list.begin();
while (it!=list.end())
foreach( CDInfo info, list )
{
CDInfo info( *it );
store(info, c);
++it;
store(offsetList, info, c);
}
}
void
Cache::store(const CDInfo& info, const Config& c)
Cache::store(const TrackOffsetList& offsetList, const CDInfo& info, const Config& c)
{
QString cacheDir = c.cacheLocations().first();
QDir d(cacheDir);
if (!d.exists())
d.mkdir(cacheDir);
// The same entry can contain several discids (separated by a ','),
// so we save the entry to all of them
// FIXME Probably, the list of discids should be replaced by only
// the one that was used for the lookup
QStringList discids = info.get("discid").toString().split(',');
for (QStringList::Iterator it = discids.begin(); it != discids.end(); ++it)
QString discid = info.get("discid").toString();
// Some entries from freedb could contain several discids separated
// by a ','. Store for each discid, but replace the discid line
// so it doesn't happen again.
QStringList discids = discid.split(',');
if (discids.count() > 2)
{
QString cacheFile = fileName(info.get("category").toString(), *it, cacheDir);
foreach(QString newid, discids)
{
CDInfo newInfo = info;
newInfo.set("discid", newid);
store(offsetList, newInfo, c);
}
}
QString source = info.get("source").toString();
QString cacheDir;
QString cacheFile;
kDebug(60010) << "Storing " << cacheFile << " in CDDB cache" << endl;
CDInfo newInfo = info;
QFile f(cacheFile);
if ( f.open(QIODevice::WriteOnly) )
if (source == "freedb")
{
cacheDir = '/' + info.get("category").toString() + '/';
cacheFile = discid;
}
else if (source == "musicbrainz")
{
cacheDir = "/musicbrainz/";
cacheFile = discid;
}
else if (source == "user")
{
cacheDir = "/user/";
QString id = CDDB::trackOffsetListToId(offsetList);
cacheFile = id;
newInfo.set("discid", id);
}
else
{
kWarning(60010) << "Unknown source " << source << " for CDInfo, can't store discinfo" << endl;
return;
}
cacheDir = c.cacheLocations().first() + cacheDir;
QDir dir;
if (!dir.exists(cacheDir))
{
if (!dir.mkpath(cacheDir))
{
QTextStream ts(&f);
ts.setCodec("UTF-8");
ts << info.toString();
f.close();
kWarning(60010) << "Couldn't create cache directory " << cacheDir << endl;
return;
}
}
kDebug(60010) << "Storing " << cacheFile << " in CDDB cache" << endl;
QFile f(cacheDir + '/' + cacheFile);
if ( f.open(QIODevice::WriteOnly) )
{
QTextStream ts(&f);
ts.setCodec("UTF-8");
ts << newInfo.toString();
f.close();
}
}
}
......
......@@ -45,12 +45,8 @@ namespace KCDDB
};
static CDInfoList lookup( const TrackOffsetList & , const Config & );
static void store( const CDInfoList &, const Config & );
// KDE4: Should probably take a TrackOffsetList too, so
// the list can be stored in the file, and we can make
// sure the discid is correct (had to do the same fix in
// both kscd and kaudiocreator)
static void store( const CDInfo &, const Config & );
static void store( const TrackOffsetList &, const CDInfoList &, const Config & );
static void store( const TrackOffsetList &, const CDInfo &, const Config & );
private:
static QString fileName( const QString &category, const QString& discid, const QString &cacheDir );
......
......@@ -475,7 +475,7 @@ namespace KCDDB
// Custom disc data
QMap<QString, QVariant>::const_iterator i = d->data.constBegin();
while (i != d->data.constEnd()){
if (!cddbKeywords.contains(i.key()))
if (!cddbKeywords.contains(i.key()) && i.key() != "SOURCE")
{
s+= d->createLine(i.key(), i.value().toString());
}
......
......@@ -41,6 +41,8 @@ namespace KCDDB
setupUi(w);
setMainWidget(w);
m_info.set("source", "user");
m_categories = KCDDB::Categories();
m_category->addItems(m_categories.i18nList());
m_genres = KCDDB::Genres();
......
......@@ -63,6 +63,7 @@ namespace KCDDB
Config config;
CDInfoList cdInfoList;
TrackOffsetList trackOffsetList;
bool block;
};
......@@ -105,6 +106,7 @@ namespace KCDDB
Client::lookup(const TrackOffsetList & trackOffsetList)
{
d->cdInfoList.clear();
d->trackOffsetList = trackOffsetList;
if ( trackOffsetList.count() <= 1 )
{
......@@ -164,7 +166,7 @@ namespace KCDDB
if ( Success == r )
{
d->cdInfoList = d->cdInfoLookup->lookupResponse();
Cache::store( d->cdInfoList, config() );
Cache::store( d->trackOffsetList, d->cdInfoList, config() );
}
delete d->cdInfoLookup;
......@@ -221,7 +223,7 @@ namespace KCDDB
if ( d->cdInfoLookup && Success == r )
{
d->cdInfoList = d->cdInfoLookup->lookupResponse();
Cache::store( d->cdInfoList, config() );
Cache::store( d->trackOffsetList, d->cdInfoList, config() );
}
else
d->cdInfoList.clear();
......@@ -322,9 +324,9 @@ namespace KCDDB
}
void
Client::store(const CDInfo &cdInfo)
Client::store(const CDInfo &cdInfo, const TrackOffsetList& offsetList)
{
Cache::store(cdInfo, config());
Cache::store(offsetList, cdInfo, config());
}
}
......
......@@ -78,7 +78,7 @@ namespace KCDDB
/**
* Stores the CD-information in the local cache
*/
void store(const CDInfo &cdInfo);
void store(const CDInfo &cdInfo, const TrackOffsetList &trackOffsetList);
void setBlockingMode( bool );
bool blockingMode() const;
......
......@@ -157,12 +157,12 @@ namespace KCDDB
{
CDInfo info;
info.set("source", "freedb");
if ( info.load( QString::fromUtf8(data_,data_.size()) ) )
{
info.set( "category", category_ );
info.set( "discid", discid_ );
info.set( "source", "freedb" );
cdInfoList_.append( info );
}
......
......@@ -84,6 +84,9 @@ namespace KCDDB
CDInfo info;
info.set("source", "musicbrainz");
// FIXME Could have different discid than the one above?
// Will break cache if several entries have same discid
info.set("discid", discId);
info.set(Title, QString::fromUtf8(mb.Data(MBE_AlbumGetAlbumName).c_str()));
info.set(Artist, QString::fromUtf8(mb.Data(MBE_AlbumGetAlbumArtistName).c_str()));
......
......@@ -169,12 +169,12 @@ namespace KCDDB
}
CDInfo info;
info.set("source", "freedb");
if ( info.load( lineList ) )
{
info.set( "category", category_ );
info.set( "discid", discid_ );
info.set( "source", "freedb" );
cdInfoList_.append( info );
}
......
......@@ -25,6 +25,7 @@ KCDDB_UNIT_TESTS(
musicbrainztest
asyncmusicbrainztest
cdinfotest
cachetest
)
KCDDB_EXECUTABLE_TESTS(
......
/*
Copyright (C) 2007 Richard Lärkäng <nouseforaname@home.se>
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 "cachetest.h"
#include "libkcddb/cache.h"
#include <qtest_kde.h>
#include "libkcddb/cdinfo.h"
#include "libkcddb/client.h"
using namespace KCDDB;
void CacheTest::initTestCase()
{
m_client = new Client;
m_client->config().setCacheLocations(QStringList(QDir::homePath()+"/.cddbTest/"));
// a1107d0a
m_list
<< 150 // First track start.
<< 29462
<< 66983
<< 96785
<< 135628
<< 168676
<< 194147
<< 222158
<< 247076
<< 278203 // Last track start.
<< 316732; // Disc end.
m_info.set(Artist, QString().leftJustified(10, QChar(0xe4)));
m_info.set(Title, QString().leftJustified(10, QChar(0xf6)));
for (int i=0; i<10; i++)
{
m_info.track(i).set(Title, QString().leftJustified(30*(i+1), 'a'+i));
}
}
void CacheTest::cleanupTestCase()
{
QDir().rmdir(QDir::homePath()+"/.cddbTest/");
}
bool CacheTest::verify(const QString& source, const QString& discid, const CDInfo& info)
{
Cache::store(m_list, info, m_client->config());
CDInfoList results = Cache::lookup(m_list, m_client->config());
foreach(CDInfo newInfo, results)
{
if (/*newInfo.get("source") == source &&*/ newInfo.get("discid").toString() == discid)
{
if (newInfo.get(Artist) != m_info.get(Artist))
continue;
if (newInfo.get(Title) != m_info.get(Title))
continue;
bool tracksOk = true;
for (int i=0; i < 10; i++)
{
if (newInfo.track(i).get(Title) != m_info.track(i).get(Title))
{
tracksOk = false;
break;
}
}
if (tracksOk)
return true;
}
}
return false;
}
void CacheTest::testFreedb()
{
CDInfo testInfo = m_info;
testInfo.set("source", "freedb");
testInfo.set("discid", "a1107d0a");
testInfo.set("category", "misc");
QVERIFY(verify("freedb", "a1107d0a", testInfo));
QFile::remove(QDir::homePath()+"/.cddbTest/misc/a1107d0a");
QDir().rmdir(QDir::homePath()+"/.cddbTest/misc/");
}
void CacheTest::testUser()
{
CDInfo testInfo = m_info;
testInfo.set("source", "user");
QVERIFY(verify("user", "a1107d0a", testInfo));
QFile::remove(QDir::homePath()+"/.cddbTest/user/a1107d0a");
QDir().rmdir(QDir::homePath()+"/.cddbTest/user/");
}
void CacheTest::testMusicbrainz()
{
CDInfo testInfo = m_info;
testInfo.set("source", "musicbrainz");
testInfo.set("discid", "wdABQ7s86gS7eVmS74CCQ6KwPUI-");
QEXPECT_FAIL("", "MusicBrainz cache doesn't work yet", Continue);
QVERIFY(verify("musicbrainz", "wdABQ7s86gS7eVmS74CCQ6KwPUI-", testInfo));
QFile::remove(QDir::homePath()+"/.cddbTest/musicbrainz/wdABQ7s86gS7eVmS74CCQ6KwPUI-");
QDir().rmdir(QDir::homePath()+"/.cddbTest/musicbrainz/");
}
QTEST_KDEMAIN(CacheTest, NoGUI)
#include "cachetest.moc"
/*
Copyright (C) 2007 Richard Lärkäng <nouseforaname@home.se>
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.
*/
#ifndef CACHETEST_H
#define CACHETEST_H
#include "libkcddb/cdinfo.h"
#include "libkcddb/kcddb.h"
namespace KCDDB
{
class Client;
}
#include <QObject>
class CacheTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void testFreedb();
void testUser();
void testMusicbrainz();
private:
bool verify(const QString& source, const QString& discid, const KCDDB::CDInfo& info);
KCDDB::Client* m_client;
KCDDB::CDInfo m_info;
KCDDB::TrackOffsetList m_list;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment