| 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/cddblocal.h> |
| 14 | #include <freac.h> |
| 15 | #include <config.h> |
| 16 | |
| 17 | using namespace smooth::IO; |
| 18 | |
| 19 | freac::CDDBLocal::CDDBLocal() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | freac::CDDBLocal::~CDDBLocal() |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | Bool freac::CDDBLocal::QueryUnixDB(const String &queryString) |
| 28 | { |
| 29 | static String array[] = { "rock", "misc", "newage", "soundtrack", "blues", "jazz", "folk", "country", "reggae", "classical", "data", NIL }; |
| 30 | |
| 31 | /* Get configuration. |
| 32 | */ |
| 33 | BoCA::Config *config = BoCA::Config::Get(); |
| 34 | |
| 35 | String cddbFolder = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 36 | |
| 37 | /* Parse query string. |
| 38 | */ |
| 39 | const Array<String> &values = queryString.Explode(" "); |
| 40 | |
| 41 | Int numTocEntries = values.GetNth(3).ToInt(); |
| 42 | UnsignedInt32 discID = StringToDiscID(values.GetNth(2)); |
| 43 | Int discLength = values.GetNth(numTocEntries + 4).ToInt(); |
| 44 | Array<Int> discOffsets; |
| 45 | |
| 46 | for (Int i = 0; i < numTocEntries; i++) discOffsets.Add(values.GetNth(i + 4).ToInt()); |
| 47 | |
| 48 | String::InputFormat inputFormat("UTF-8"); |
| 49 | String::OutputFormat outputFormat("UTF-8"); |
| 50 | |
| 51 | for (Int i = 0; array[i] != NIL; i++) |
| 52 | { |
| 53 | String fileName = String(cddbFolder).Append(array[i]).Append(Directory::GetDirectoryDelimiter()).Append(DiscIDToString(discID)); |
| 54 | |
| 55 | if (!File(fileName).Exists()) continue; |
| 56 | |
| 57 | /* Read CDDB record. |
| 58 | */ |
| 59 | InStream in(STREAM_FILE, fileName, IS_READ); |
| 60 | String result = in.InputString(in.Size()); |
| 61 | |
| 62 | /* Parse CDDB record. |
| 63 | */ |
| 64 | CDDBInfo cddbInfo; |
| 65 | |
| 66 | ParseCDDBRecord(result, cddbInfo); |
| 67 | |
| 68 | if (discLength == cddbInfo.discLength) |
| 69 | { |
| 70 | Bool match = True; |
| 71 | |
| 72 | for (Int j = 0; j < cddbInfo.trackOffsets.Length(); j++) |
| 73 | { |
| 74 | if (discOffsets.GetNth(j) != cddbInfo.trackOffsets.GetNth(j)) match = False; |
| 75 | } |
| 76 | |
| 77 | if (match) |
| 78 | { |
| 79 | ids.Add(discID); |
| 80 | categories.Add(array[i]); |
| 81 | titles.Add(String(cddbInfo.dArtist).Append(" / ").Append(cddbInfo.dTitle)); |
| 82 | results.Add(result); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return (results.Length() != 0); |
| 88 | } |
| 89 | |
| 90 | Bool freac::CDDBLocal::QueryWinDB(const String &queryString) |
| 91 | { |
| 92 | static String array[] = { "rock", "misc", "newage", "soundtrack", "blues", "jazz", "folk", "country", "reggae", "classical", "data", NIL }; |
| 93 | |
| 94 | /* Get configuration. |
| 95 | */ |
| 96 | BoCA::Config *config = BoCA::Config::Get(); |
| 97 | |
| 98 | String cddbFolder = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 99 | |
| 100 | /* Parse query string. |
| 101 | */ |
| 102 | const Array<String> &values = queryString.Explode(" "); |
| 103 | |
| 104 | Int numTocEntries = values.GetNth(3).ToInt(); |
| 105 | UnsignedInt32 discID = StringToDiscID(values.GetNth(2)); |
| 106 | Int discLength = values.GetNth(numTocEntries + 4).ToInt(); |
| 107 | Array<Int> discOffsets; |
| 108 | |
| 109 | for (Int i = 0; i < numTocEntries; i++) discOffsets.Add(values.GetNth(i + 4).ToInt()); |
| 110 | |
| 111 | String::InputFormat inputFormat("UTF-8"); |
| 112 | String::OutputFormat outputFormat("UTF-8"); |
| 113 | |
| 114 | for (Int i = 0; array[i] != NIL; i++) |
| 115 | { |
| 116 | /* Find file containing CDDB record. |
| 117 | */ |
| 118 | Directory categoryDir = Directory(String(cddbFolder).Append(array[i])); |
| 119 | String pattern = String().CopyN(DiscIDToString(discID), 2).Append("to??"); |
| 120 | String fileName; |
| 121 | |
| 122 | do |
| 123 | { |
| 124 | const Array<File> &files = categoryDir.GetFilesByPattern(pattern); |
| 125 | |
| 126 | if (files.Length() == 1) fileName = files.GetFirst(); |
| 127 | |
| 128 | if (pattern[1] == 'a') pattern[1] = '9'; |
| 129 | else if (pattern[1] == '0') pattern[1] = 'f'; |
| 130 | else pattern[1] -= 1; |
| 131 | |
| 132 | if (pattern[1] == 'f') |
| 133 | { |
| 134 | if (pattern[0] == 'a') pattern[0] = '9'; |
| 135 | else if (pattern[0] == '0') pattern[0] = 'f'; |
| 136 | else pattern[0] -= 1; |
| 137 | } |
| 138 | } |
| 139 | while (fileName == NIL && !(pattern[0] == 'f' && pattern[1] == 'f')); |
| 140 | |
| 141 | if (fileName == NIL) continue; |
| 142 | |
| 143 | /* Read CDDB record. |
| 144 | */ |
| 145 | InStream in(STREAM_FILE, fileName, IS_READ); |
| 146 | String idString = String("#FILENAME=").Append(DiscIDToString(discID)); |
| 147 | String result; |
| 148 | |
| 149 | while (in.GetPos() < in.Size()) |
| 150 | { |
| 151 | if (in.InputLine() == idString) |
| 152 | { |
| 153 | Int start = in.GetPos(); |
| 154 | Int end = in.GetPos(); |
| 155 | |
| 156 | while (in.GetPos() < in.Size()) |
| 157 | { |
| 158 | end = in.GetPos(); |
| 159 | |
| 160 | if (in.InputLine().StartsWith("#FILENAME=")) break; |
| 161 | } |
| 162 | |
| 163 | in.Seek(start); |
| 164 | |
| 165 | result = in.InputString(end - start); |
| 166 | |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (result == NIL) continue; |
| 172 | |
| 173 | /* Parse CDDB record. |
| 174 | */ |
| 175 | CDDBInfo cddbInfo; |
| 176 | |
| 177 | ParseCDDBRecord(result, cddbInfo); |
| 178 | |
| 179 | if (discLength == cddbInfo.discLength) |
| 180 | { |
| 181 | Bool match = True; |
| 182 | |
| 183 | for (Int j = 0; j < cddbInfo.trackOffsets.Length(); j++) |
| 184 | { |
| 185 | if (discOffsets.GetNth(j) != cddbInfo.trackOffsets.GetNth(j)) match = False; |
| 186 | } |
| 187 | |
| 188 | if (match) |
| 189 | { |
| 190 | ids.Add(discID); |
| 191 | categories.Add(array[i]); |
| 192 | titles.Add(String(cddbInfo.dArtist).Append(" / ").Append(cddbInfo.dTitle)); |
| 193 | results.Add(result); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return (results.Length() != 0); |
| 199 | } |
| 200 | |
| 201 | Bool freac::CDDBLocal::ConnectToServer() |
| 202 | { |
| 203 | return True; |
| 204 | } |
| 205 | |
| 206 | Int freac::CDDBLocal::Query(const String &queryString) |
| 207 | { |
| 208 | /* Try to find Unix style record first; if no match is found, try Windows style. |
| 209 | */ |
| 210 | if (!QueryUnixDB(queryString)) QueryWinDB(queryString); |
| 211 | |
| 212 | /* No match found. |
| 213 | */ |
| 214 | if (categories.Length() == 0) return QUERY_RESULT_NONE; |
| 215 | |
| 216 | /* Exact match. |
| 217 | */ |
| 218 | if (categories.Length() == 1) return QUERY_RESULT_SINGLE; |
| 219 | |
| 220 | /* Multiple exact matches. |
| 221 | */ |
| 222 | if (categories.Length() > 1) return QUERY_RESULT_MULTIPLE; |
| 223 | |
| 224 | return QUERY_RESULT_ERROR; |
| 225 | } |
| 226 | |
| 227 | Bool freac::CDDBLocal::Read(const String &category, UnsignedInt32 discID, CDDBInfo &cddbInfo) |
| 228 | { |
| 229 | cddbInfo.discID = discID; |
| 230 | cddbInfo.category = category; |
| 231 | |
| 232 | String result; |
| 233 | |
| 234 | for (Int i = 0; i < categories.Length(); i++) if (categories.GetNth(i) == cddbInfo.category) result = results.GetNth(i); |
| 235 | |
| 236 | if (result == NIL) return False; |
| 237 | else return ParseCDDBRecord(result, cddbInfo); |
| 238 | } |
| 239 | |
| 240 | Bool freac::CDDBLocal::Submit(const CDDBInfo &oCddbInfo) |
| 241 | { |
| 242 | BoCA::Config *config = BoCA::Config::Get(); |
| 243 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 244 | |
| 245 | String cddbFolder = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 246 | |
| 247 | protocol->Write(String("Writing info to local CDDB database at: ").Append(cddbFolder)); |
| 248 | |
| 249 | CDDBInfo cddbInfo = oCddbInfo; |
| 250 | |
| 251 | UpdateEntry(cddbInfo); |
| 252 | |
| 253 | String content = FormatCDDBRecord(cddbInfo); |
| 254 | |
| 255 | /* See if we have a Windows or Unix style DB. |
| 256 | */ |
| 257 | Directory categoryDir = Directory(String(cddbFolder).Append(cddbInfo.category)); |
| 258 | String pattern = String("??to??"); |
| 259 | |
| 260 | /* Create directory if it doesn't exist. |
| 261 | */ |
| 262 | categoryDir.Create(); |
| 263 | |
| 264 | if (categoryDir.GetFilesByPattern(pattern).Length() >= 1) |
| 265 | { |
| 266 | protocol->Write(" Found Windows style DB."); |
| 267 | |
| 268 | pattern = String().CopyN(DiscIDToString(cddbInfo.discID), 2).Append("to??"); |
| 269 | |
| 270 | String fileName; |
| 271 | |
| 272 | while (fileName == NIL && !(pattern[0] == '0' && pattern[1] == '0')) |
| 273 | { |
| 274 | const Array<File> &files = categoryDir.GetFilesByPattern(pattern); |
| 275 | |
| 276 | if (files.Length() == 1) fileName = files.GetFirst(); |
| 277 | |
| 278 | if (pattern[1] == 'a') pattern[1] = '9'; |
| 279 | else if (pattern[1] == '0') pattern[1] = 'f'; |
| 280 | else pattern[1] -= 1; |
| 281 | |
| 282 | if (pattern[1] == 'f') |
| 283 | { |
| 284 | if (pattern[0] == 'a') pattern[0] = '9'; |
| 285 | else pattern[0] -= 1; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | protocol->Write(String(" Writing to: ").Append(String(fileName).Tail(fileName.Length() - cddbFolder.Length()))); |
| 290 | |
| 291 | InStream in(STREAM_FILE, fileName, IS_READ); |
| 292 | OutStream out(STREAM_FILE, String(fileName).Append(".new"), OS_REPLACE); |
| 293 | String idString = String("#FILENAME=").Append(DiscIDToString(cddbInfo.discID)); |
| 294 | Bool written = False; |
| 295 | |
| 296 | String::InputFormat inputFormat("ISO-8859-1"); |
| 297 | String::OutputFormat outputFormat("ISO-8859-1"); |
| 298 | |
| 299 | while (in.GetPos() < in.Size()) |
| 300 | { |
| 301 | String line = in.InputLine(); |
| 302 | |
| 303 | out.OutputString(String(line).Append("\n")); |
| 304 | |
| 305 | if (line == idString) |
| 306 | { |
| 307 | out.OutputString(content.ConvertTo("UTF-8")); |
| 308 | |
| 309 | while (in.GetPos() < in.Size()) |
| 310 | { |
| 311 | line = in.InputLine(); |
| 312 | |
| 313 | if (line.StartsWith("#FILENAME=")) { out.OutputString(String(line).Append("\n")); break; } |
| 314 | } |
| 315 | |
| 316 | written = True; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (!written) |
| 321 | { |
| 322 | out.OutputString(String(idString).Append("\n")); |
| 323 | out.OutputString(content.ConvertTo("UTF-8")); |
| 324 | } |
| 325 | |
| 326 | in.Close(); |
| 327 | out.Close(); |
| 328 | |
| 329 | File(fileName).Delete(); |
| 330 | File(String(fileName).Append(".new")).Move(fileName); |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | protocol->Write(" Found Unix style DB."); |
| 335 | |
| 336 | String fileName = String(categoryDir).Append(Directory::GetDirectoryDelimiter()).Append(DiscIDToString(cddbInfo.discID)); |
| 337 | |
| 338 | protocol->Write(String(" Writing to: ").Append(String(fileName).Tail(fileName.Length() - cddbFolder.Length()))); |
| 339 | |
| 340 | OutStream out(STREAM_FILE, fileName, OS_REPLACE); |
| 341 | |
| 342 | String::OutputFormat outputFormat("UTF-8"); |
| 343 | |
| 344 | out.OutputString(content); |
| 345 | } |
| 346 | |
| 347 | protocol->Write("Finished CDDB transaction."); |
| 348 | |
| 349 | return True; |
| 350 | } |
| 351 | |
| 352 | Bool freac::CDDBLocal::CloseConnection() |
| 353 | { |
| 354 | return True; |
| 355 | } |