• 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
cache.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  Copyright (C) 2007 Richard Lärkäng <nouseforaname@home.se>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "cache.h"
24 
25 #include "kcddbconfig.h"
26 #include "cddb.h"
27 
28 #include "config-musicbrainz.h"
29 #ifdef HAVE_MUSICBRAINZ5
30 #include "musicbrainz/musicbrainzlookup.h"
31 #endif
32 
33 #include <kdebug.h>
34 #include <kstandarddirs.h>
35 
36 #include <QFile>
37 #include <QDir>
38 #include <QTextStream>
39 
40 namespace KCDDB
41 {
42  CDInfoList
43  Cache::lookup( const TrackOffsetList &offsetList, const Config& c )
44  {
45  QString cddbId = CDDB::trackOffsetListToId(offsetList);
46 
47  kDebug(60010) << "Looking up " << cddbId << " in CDDB cache";
48 
49  CDInfoList infoList;
50 
51  infoList << CDDB::cacheFiles(offsetList, c);
52 #ifdef HAVE_MUSICBRAINZ5
53  infoList << MusicBrainzLookup::cacheFiles(offsetList, c);
54 #endif
55 
56  return infoList;
57  }
58 
59  void
60  Cache::store(const TrackOffsetList& offsetList, const CDInfoList& list, const Config& c)
61  {
62  foreach( const CDInfo &info, list )
63  {
64  store(offsetList, info, c);
65  }
66  }
67 
68  void
69  Cache::store(const TrackOffsetList& offsetList, const CDInfo& info, const Config& c)
70  {
71  QString discid = info.get(QLatin1String( "discid" )).toString();
72 
73  // Some entries from freedb could contain several discids separated
74  // by a ','. Store for each discid, but replace the discid line
75  // so it doesn't happen again.
76  QStringList discids = discid.split(QLatin1Char( ',' ));
77  if (discids.count() > 2)
78  {
79  foreach(const QString &newid, discids)
80  {
81  CDInfo newInfo = info;
82  newInfo.set(QLatin1String( "discid" ), newid);
83  store(offsetList, newInfo, c);
84  }
85  }
86 
87  QString source = info.get(QLatin1String( "source" )).toString();
88 
89  QString cacheDir;
90  QString cacheFile;
91 
92  CDInfo newInfo = info;
93 
94  if (source == QLatin1String( "freedb" ))
95  {
96  cacheDir = QLatin1Char( '/' ) + info.get(QLatin1String( "category" )).toString() + QLatin1Char( '/' );
97  cacheFile = discid;
98  }
99  else if (source == QLatin1String( "musicbrainz" ))
100  {
101  cacheDir = QLatin1String( "/musicbrainz/" );
102  cacheFile = discid;
103  }
104  else
105  {
106  if (source != QLatin1String( "user" ))
107  kWarning(60010) << "Unknown source " << source << " for CDInfo";
108 
109  cacheDir = QLatin1String( "/user/" );
110  QString id = CDDB::trackOffsetListToId(offsetList);
111  cacheFile = id;
112  newInfo.set(QLatin1String( "discid" ), id);
113  }
114 
115  const QStringList cacheLocations = c.cacheLocations();
116 
117  if (!cacheLocations.isEmpty()) {
118  cacheDir = cacheLocations.first() + cacheDir;
119 
120  QDir dir;
121 
122  if (!dir.exists(cacheDir))
123  {
124  if (!dir.mkpath(cacheDir))
125  {
126  kWarning(60010) << "Couldn't create cache directory " << cacheDir;
127  return;
128  }
129  }
130 
131  kDebug(60010) << "Storing " << cacheFile << " in CDDB cache";
132 
133  QFile f(cacheDir + QLatin1Char( '/' ) + cacheFile);
134  if ( f.open(QIODevice::WriteOnly) )
135  {
136  QTextStream ts(&f);
137  ts.setCodec("UTF-8");
138  ts << newInfo.toString();
139  f.close();
140  }
141  } else {
142  kDebug(60010) << "There's no cache dir defined, not storing it";
143  }
144  }
145 }
146 
147 // vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
QTextStream::setCodec
void setCodec(QTextCodec *codec)
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::CDInfo::get
QVariant get(const QString &type) const
Get data for type that has been assigned to this disc.
Definition: cdinfo.cpp:518
KCDDB::Cache::lookup
static CDInfoList lookup(const TrackOffsetList &, const Config &)
Definition: cache.cpp:43
QFile
ConfigBase::cacheLocations
QStringList cacheLocations() const
Get cacheLocations.
Definition: configbase.cpp:196
QTextStream
KCDDB::Cache::store
static void store(const TrackOffsetList &, const CDInfoList &, const Config &)
Definition: cache.cpp:60
KCDDB::CDDB::trackOffsetListToId
QString trackOffsetListToId()
Definition: cddb.cpp:48
QList::count
int count(const T &value) const
QDir::exists
bool exists() const
KCDDB::CDInfo
Information about a CD.
Definition: cdinfo.h:117
KCDDB::CDInfoList
QList< CDInfo > CDInfoList
Definition: cdinfo.h:208
QList::isEmpty
bool isEmpty() const
KCDDB::CDInfo::set
void set(const QString &type, const QVariant &data)
Set any data from this disc.
Definition: cdinfo.cpp:522
cache.h
QList::first
T & first()
QString
QList< uint >
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QLatin1Char
QFile::close
virtual void close()
QDir
cddb.h
QLatin1String
KCDDB::CDInfo::toString
QString toString(bool submit=false) const
Definition: cdinfo.cpp:421
KCDDB::Config
Definition: kcddbconfig.h:31
QVariant::toString
QString toString() const
QDir::mkpath
bool mkpath(const QString &dirPath) const
kcddbconfig.h
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