KCDDB

cache.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Rik Hemsley (rikkus) <[email protected]>
3  SPDX-FileCopyrightText: 2002 Benjamin Meyer <[email protected]>
4  SPDX-FileCopyrightText: 2002 Nadeem Hasan <[email protected]>
5  SPDX-FileCopyrightText: 2007 Richard Lärkäng <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "cache.h"
11 
12 #include "kcddbconfig.h"
13 #include "cddb.h"
14 #include "logging.h"
15 
16 #include "config-musicbrainz.h"
17 #ifdef HAVE_MUSICBRAINZ5
18 #include "musicbrainz/musicbrainzlookup.h"
19 #endif
20 
21 #include <QFile>
22 #include <QDir>
23 #include <QTextStream>
24 
25 namespace KCDDB
26 {
27  CDInfoList
28  Cache::lookup( const TrackOffsetList &offsetList, const Config& c )
29  {
30  QString cddbId = CDDB::trackOffsetListToId(offsetList);
31 
32  qCDebug(LIBKCDDB) << "Looking up " << cddbId << " in CDDB cache";
33 
34  CDInfoList infoList;
35 
36  infoList << CDDB::cacheFiles(offsetList, c);
37 #ifdef HAVE_MUSICBRAINZ5
38  infoList << MusicBrainzLookup::cacheFiles(offsetList, c);
39 #endif
40 
41  return infoList;
42  }
43 
44  void
45  Cache::store(const TrackOffsetList& offsetList, const CDInfoList& list, const Config& c)
46  {
47  for (const CDInfo &info : list) {
48  store(offsetList, info, c);
49  }
50  }
51 
52  void
53  Cache::store(const TrackOffsetList& offsetList, const CDInfo& info, const Config& c)
54  {
55  QString discid = info.get(QLatin1String( "discid" )).toString();
56 
57  // Some entries from freedb could contain several discids separated
58  // by a ','. Store for each discid, but replace the discid line
59  // so it doesn't happen again.
60  const QStringList discids = discid.split(QLatin1Char( ',' ));
61  if (discids.count() > 2)
62  {
63  for (const QString &newid : discids) {
64  CDInfo newInfo = info;
65  newInfo.set(QLatin1String( "discid" ), newid);
66  store(offsetList, newInfo, c);
67  }
68  }
69 
70  QString source = info.get(QLatin1String( "source" )).toString();
71 
72  QString cacheDir;
73  QString cacheFile;
74 
75  CDInfo newInfo = info;
76 
77  if (source == QLatin1String( "freedb" ))
78  {
79  cacheDir = QLatin1Char( '/' ) + info.get(QLatin1String( "category" )).toString() + QLatin1Char( '/' );
80  cacheFile = discid;
81  }
82  else if (source == QLatin1String( "musicbrainz" ))
83  {
84  cacheDir = QLatin1String( "/musicbrainz/" );
85  cacheFile = discid;
86  }
87  else
88  {
89  if (source != QLatin1String( "user" ))
90  qCWarning(LIBKCDDB) << "Unknown source " << source << " for CDInfo";
91 
92  cacheDir = QLatin1String( "/user/" );
93  QString id = CDDB::trackOffsetListToId(offsetList);
94  cacheFile = id;
95  newInfo.set(QLatin1String( "discid" ), id);
96  }
97 
98  const QStringList cacheLocations = c.cacheLocations();
99 
100  if (!cacheLocations.isEmpty()) {
101  cacheDir = cacheLocations.first() + cacheDir;
102 
103  QDir dir;
104 
105  if (!dir.exists(cacheDir))
106  {
107  if (!dir.mkpath(cacheDir))
108  {
109  qCWarning(LIBKCDDB) << "Couldn't create cache directory " << cacheDir;
110  return;
111  }
112  }
113 
114  qCDebug(LIBKCDDB) << "Storing " << cacheFile << " in CDDB cache";
115 
116  QFile f(cacheDir + QLatin1Char( '/' ) + cacheFile);
117  if ( f.open(QIODevice::WriteOnly) )
118  {
119  QTextStream ts(&f);
120  ts.setCodec("UTF-8");
121  ts << newInfo.toString();
122  f.close();
123  }
124  } else {
125  qDebug() << "There's no cache dir defined, not storing it";
126  }
127  }
128 }
129 
130 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
int count(const T &value) const const
bool exists() const const
bool isEmpty() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
T & first()
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
bool mkpath(const QString &dirPath) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:22:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.