| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2020 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <cddb/cddbcache.h> |
| 14 | #include <cddb/cddblocal.h> |
| 15 | |
| 16 | #include <config.h> |
| 17 | #include <boca.h> |
| 18 | |
| 19 | freac::CDDBCache *freac::CDDBCache::instance = NIL; |
| 20 | |
| 21 | freac::CDDBCache::CDDBCache() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | freac::CDDBCache::~CDDBCache() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | freac::CDDBCache *freac::CDDBCache::Get() |
| 30 | { |
| 31 | if (instance == NIL) instance = new CDDBCache(); |
| 32 | |
| 33 | return instance; |
| 34 | } |
| 35 | |
| 36 | Void freac::CDDBCache::Free() |
| 37 | { |
| 38 | if (instance != NIL) delete instance; |
| 39 | } |
| 40 | |
| 41 | const freac::CDDBInfo &freac::CDDBCache::GetCacheEntry(const String &queryString) |
| 42 | { |
| 43 | BoCA::Config *config = BoCA::Config::Get(); |
| 44 | |
| 45 | const Array<String> &values = queryString.Explode(" "); |
| 46 | UnsignedInt32 discID = CDDB::StringToDiscID(values.GetNth(2)); |
| 47 | const CDDBInfo &cddbInfo = infoCache.Get(discID); |
| 48 | |
| 49 | if (cddbInfo != NIL) |
| 50 | { |
| 51 | /* Softcache entry found. |
| 52 | */ |
| 53 | return cddbInfo; |
| 54 | } |
| 55 | |
| 56 | /* Save current freedb path. |
| 57 | */ |
| 58 | String configFreedbDir = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 59 | |
| 60 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter())); |
| 61 | |
| 62 | /* Try to find an entry in the persistant cache. |
| 63 | */ |
| 64 | CDDBLocal cddbLocal; |
| 65 | Int result = cddbLocal.Query(queryString); |
| 66 | |
| 67 | if (result == QUERY_RESULT_SINGLE || result == QUERY_RESULT_MULTIPLE) |
| 68 | { |
| 69 | CDDBInfo newInfo; |
| 70 | |
| 71 | cddbLocal.Read(cddbLocal.GetNthCategory(0), cddbLocal.GetNthDiscID(0), newInfo); |
| 72 | |
| 73 | /* Add entry to softcache. |
| 74 | */ |
| 75 | infoCache.Add(newInfo, discID); |
| 76 | } |
| 77 | |
| 78 | /* Restore real freedb path. |
| 79 | */ |
| 80 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, configFreedbDir); |
| 81 | |
| 82 | return infoCache.Get(discID); |
| 83 | } |
| 84 | |
| 85 | Bool freac::CDDBCache::AddCacheEntry(const CDDBInfo &nCddbInfo) |
| 86 | { |
| 87 | BoCA::Config *config = BoCA::Config::Get(); |
| 88 | const CDDBInfo &cddbInfo = infoCache.Get(nCddbInfo.discID); |
| 89 | |
| 90 | if (cddbInfo != NIL) |
| 91 | { |
| 92 | /* Delete existing cache entry. |
| 93 | */ |
| 94 | infoCache.Remove(nCddbInfo.discID); |
| 95 | } |
| 96 | |
| 97 | infoCache.Add(nCddbInfo, nCddbInfo.discID); |
| 98 | |
| 99 | /* Save current freedb path. |
| 100 | */ |
| 101 | String configFreedbDir = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 102 | |
| 103 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter())); |
| 104 | |
| 105 | /* Update existing entry in persistant cache. |
| 106 | */ |
| 107 | String fileName = String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append(nCddbInfo.category).Append(Directory::GetDirectoryDelimiter()).Append(CDDB::DiscIDToString(nCddbInfo.discID)); |
| 108 | |
| 109 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableCacheID, Config::FreedbEnableCacheDefault) || File(fileName).Exists()) |
| 110 | { |
| 111 | File(fileName).Delete(); |
| 112 | |
| 113 | CDDBLocal().Submit(nCddbInfo); |
| 114 | } |
| 115 | |
| 116 | /* Restore real freedb path. |
| 117 | */ |
| 118 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, configFreedbDir); |
| 119 | |
| 120 | return True; |
| 121 | } |
| 122 | |
| 123 | Int freac::CDDBCache::RemoveNthEntry(Int n) |
| 124 | { |
| 125 | if (n >= GetNOfEntries()) return Error(); |
| 126 | |
| 127 | BoCA::Config *config = BoCA::Config::Get(); |
| 128 | UnsignedInt32 discID = infoCache.GetNthIndex(n); |
| 129 | const CDDBInfo &cddbInfo = infoCache.Get(discID); |
| 130 | |
| 131 | File(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append(cddbInfo.category).Append(Directory::GetDirectoryDelimiter()).Append(CDDB::DiscIDToString(cddbInfo.discID))).Delete(); |
| 132 | |
| 133 | infoCache.Remove(discID); |
| 134 | |
| 135 | return Success(); |
| 136 | } |