File indexing completed on 2024-03-24 04:57:11
0001 /* 0002 SPDX-FileCopyrightText: 2002 Rik Hemsley (rikkus) <rik@kde.org> 0003 SPDX-FileCopyrightText: 2002 Benjamin Meyer <ben-devel@meyerhome.net> 0004 SPDX-FileCopyrightText: 2002 Nadeem Hasan <nhasan@kde.org> 0005 SPDX-FileCopyrightText: 2007 Richard Lärkäng <nouseforaname@home.se> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "cache.h" 0011 0012 #include "config.h" 0013 #include "cddb.h" 0014 #include "logging.h" 0015 0016 #include "config-musicbrainz.h" 0017 #ifdef HAVE_MUSICBRAINZ5 0018 #include "musicbrainz/musicbrainzlookup.h" 0019 #endif 0020 0021 #include <QFile> 0022 #include <QDir> 0023 #include <QTextStream> 0024 0025 namespace KCDDB 0026 { 0027 CDInfoList 0028 Cache::lookup( const TrackOffsetList &offsetList, const Config& c ) 0029 { 0030 QString cddbId = CDDB::trackOffsetListToId(offsetList); 0031 0032 qCDebug(LIBKCDDB) << "Looking up " << cddbId << " in CDDB cache"; 0033 0034 CDInfoList infoList; 0035 0036 infoList << CDDB::cacheFiles(offsetList, c); 0037 #ifdef HAVE_MUSICBRAINZ5 0038 infoList << MusicBrainzLookup::cacheFiles(offsetList, c); 0039 #endif 0040 0041 return infoList; 0042 } 0043 0044 void 0045 Cache::store(const TrackOffsetList& offsetList, const CDInfoList& list, const Config& c) 0046 { 0047 for (const CDInfo &info : list) { 0048 store(offsetList, info, c); 0049 } 0050 } 0051 0052 void 0053 Cache::store(const TrackOffsetList& offsetList, const CDInfo& info, const Config& c) 0054 { 0055 QString discid = info.get(QLatin1String( "discid" )).toString(); 0056 0057 // Some entries from freedb could contain several discids separated 0058 // by a ','. Store for each discid, but replace the discid line 0059 // so it doesn't happen again. 0060 const QStringList discids = discid.split(QLatin1Char( ',' )); 0061 if (discids.count() > 2) 0062 { 0063 for (const QString &newid : discids) { 0064 CDInfo newInfo = info; 0065 newInfo.set(QLatin1String( "discid" ), newid); 0066 store(offsetList, newInfo, c); 0067 } 0068 } 0069 0070 QString source = info.get(QLatin1String( "source" )).toString(); 0071 0072 QString cacheDir; 0073 QString cacheFile; 0074 0075 CDInfo newInfo = info; 0076 0077 if (source == QLatin1String( "freedb" )) 0078 { 0079 cacheDir = QLatin1Char( '/' ) + info.get(QLatin1String( "category" )).toString() + QLatin1Char( '/' ); 0080 cacheFile = discid; 0081 } 0082 else if (source == QLatin1String( "musicbrainz" )) 0083 { 0084 cacheDir = QLatin1String( "/musicbrainz/" ); 0085 cacheFile = discid; 0086 } 0087 else 0088 { 0089 if (source != QLatin1String( "user" )) 0090 qCWarning(LIBKCDDB) << "Unknown source " << source << " for CDInfo"; 0091 0092 cacheDir = QLatin1String( "/user/" ); 0093 QString id = CDDB::trackOffsetListToId(offsetList); 0094 cacheFile = id; 0095 newInfo.set(QLatin1String( "discid" ), id); 0096 } 0097 0098 const QStringList cacheLocations = c.cacheLocations(); 0099 0100 if (!cacheLocations.isEmpty()) { 0101 cacheDir = cacheLocations.first() + cacheDir; 0102 0103 QDir dir; 0104 0105 if (!dir.exists(cacheDir)) 0106 { 0107 if (!dir.mkpath(cacheDir)) 0108 { 0109 qCWarning(LIBKCDDB) << "Couldn't create cache directory " << cacheDir; 0110 return; 0111 } 0112 } 0113 0114 qCDebug(LIBKCDDB) << "Storing " << cacheFile << " in CDDB cache"; 0115 0116 QFile f(cacheDir + QLatin1Char( '/' ) + cacheFile); 0117 if ( f.open(QIODevice::WriteOnly) ) 0118 { 0119 QTextStream ts(&f); 0120 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0121 ts.setCodec("UTF-8"); 0122 #endif 0123 ts << newInfo.toString(); 0124 f.close(); 0125 } 0126 } else { 0127 qDebug() << "There's no cache dir defined, not storing it"; 0128 } 0129 } 0130 } 0131 0132 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1