| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2018 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/cddbbatch.h> |
| 14 | #include <cddb/cddblocal.h> |
| 15 | #include <cddb/cddbremote.h> |
| 16 | #include <config.h> |
| 17 | #include <utilities.h> |
| 18 | |
| 19 | #include <dialogs/cddb/query.h> |
| 20 | #include <dialogs/error.h> |
| 21 | |
| 22 | freac::CDDBBatch::CDDBBatch() |
| 23 | { |
| 24 | ReadEntries(); |
| 25 | } |
| 26 | |
| 27 | freac::CDDBBatch::~CDDBBatch() |
| 28 | { |
| 29 | SaveEntries(); |
| 30 | } |
| 31 | |
| 32 | Bool freac::CDDBBatch::ReadEntries() |
| 33 | { |
| 34 | BoCA::Config *config = BoCA::Config::Get(); |
| 35 | |
| 36 | String::InputFormat inputFormat("UTF-8"); |
| 37 | String::OutputFormat outputFormat("UTF-8"); |
| 38 | |
| 39 | /* Read saved queries from XML. |
| 40 | */ |
| 41 | XML::Document *document = new XML::Document(); |
| 42 | |
| 43 | if (document->LoadFile(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("queries.xml")) == Success()) |
| 44 | { |
| 45 | XML::Node *root = document->GetRootNode(); |
| 46 | |
| 47 | if (root != NIL) |
| 48 | { |
| 49 | for (Int i = 0; i < root->GetNOfNodes(); i++) |
| 50 | { |
| 51 | XML::Node *node = root->GetNthNode(i); |
| 52 | |
| 53 | if (node->GetName() == "query") queries.Add(node->GetContent()); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | delete document; |
| 59 | |
| 60 | /* Read saved submits from XML and database cache. |
| 61 | */ |
| 62 | document = new XML::Document(); |
| 63 | |
| 64 | if (document->LoadFile(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("submits.xml")) == Success()) |
| 65 | { |
| 66 | ReadEntriesXML(document); |
| 67 | } |
| 68 | |
| 69 | delete document; |
| 70 | |
| 71 | return True; |
| 72 | } |
| 73 | |
| 74 | Bool freac::CDDBBatch::ReadEntriesXML(XML::Document *document) |
| 75 | { |
| 76 | XML::Node *root = document->GetRootNode(); |
| 77 | |
| 78 | if (root == NIL) return False; |
| 79 | |
| 80 | BoCA::Config *config = BoCA::Config::Get(); |
| 81 | |
| 82 | for (Int i = 0; i < root->GetNOfNodes(); i++) |
| 83 | { |
| 84 | XML::Node *node = root->GetNthNode(i); |
| 85 | |
| 86 | if (node->GetName() == "submit") |
| 87 | { |
| 88 | InStream *in = new InStream(STREAM_FILE, String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append(node->GetAttributeByName("category")->GetContent()).Append(Directory::GetDirectoryDelimiter()).Append(node->GetContent()), IS_READ); |
| 89 | |
| 90 | if (in->Size() > 0) |
| 91 | { |
| 92 | String result = in->InputString(in->Size()); |
| 93 | CDDBInfo cddbInfo; |
| 94 | |
| 95 | ParseCDDBRecord(result, cddbInfo); |
| 96 | |
| 97 | cddbInfo.category = node->GetAttributeByName("category")->GetContent(); |
| 98 | |
| 99 | for (Int j = 0; j < submits.Length(); j++) |
| 100 | { |
| 101 | if (submits.GetNth(j) == cddbInfo) |
| 102 | { |
| 103 | submits.Remove(submits.GetNthIndex(j)); |
| 104 | |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | submits.Add(cddbInfo); |
| 110 | } |
| 111 | |
| 112 | delete in; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return True; |
| 117 | } |
| 118 | |
| 119 | Bool freac::CDDBBatch::SaveEntries() |
| 120 | { |
| 121 | BoCA::Config *config = BoCA::Config::Get(); |
| 122 | |
| 123 | /* Save queued queries. |
| 124 | */ |
| 125 | if (queries.Length() == 0) |
| 126 | { |
| 127 | /* Delete queries file if no more saved queries exist. |
| 128 | */ |
| 129 | File(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("queries.xml")).Delete(); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | Directory(String(config->cacheDir).Append("cddb")).Create(); |
| 134 | |
| 135 | XML::Document *document = new XML::Document(); |
| 136 | XML::Node *root = new XML::Node("cddbQueries"); |
| 137 | |
| 138 | document->SetRootNode(root); |
| 139 | |
| 140 | for (Int i = 0; i < queries.Length(); i++) |
| 141 | { |
| 142 | root->AddNode("query", queries.GetNth(i)); |
| 143 | } |
| 144 | |
| 145 | document->SaveFile(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("queries.xml")); |
| 146 | |
| 147 | delete document; |
| 148 | delete root; |
| 149 | } |
| 150 | |
| 151 | /* Save queued submits. |
| 152 | */ |
| 153 | if (submits.Length() == 0) |
| 154 | { |
| 155 | /* Delete submits file if no more saved submits exist. |
| 156 | */ |
| 157 | File(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("submits.xml")).Delete(); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | Directory(String(config->cacheDir).Append("cddb")).Create(); |
| 162 | |
| 163 | XML::Document *document = new XML::Document(); |
| 164 | XML::Node *root = new XML::Node("cddbSubmits"); |
| 165 | |
| 166 | document->SetRootNode(root); |
| 167 | |
| 168 | for (Int i = 0; i < submits.Length(); i++) |
| 169 | { |
| 170 | XML::Node *node = root->AddNode("submit", DiscIDToString(submits.GetNth(i).discID)); |
| 171 | |
| 172 | node->SetAttribute("category", submits.GetNth(i).category); |
| 173 | } |
| 174 | |
| 175 | document->SaveFile(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append("submits.xml")); |
| 176 | |
| 177 | delete document; |
| 178 | delete root; |
| 179 | } |
| 180 | |
| 181 | return True; |
| 182 | } |
| 183 | |
| 184 | Bool freac::CDDBBatch::AddQuery(const String &query) |
| 185 | { |
| 186 | for (Int i = 0; i < queries.Length(); i++) |
| 187 | { |
| 188 | if (queries.GetNth(i) == query) return False; |
| 189 | } |
| 190 | |
| 191 | queries.Add(query); |
| 192 | |
| 193 | return True; |
| 194 | } |
| 195 | |
| 196 | Bool freac::CDDBBatch::DeleteQuery(Int n) |
| 197 | { |
| 198 | queries.Remove(queries.GetNthIndex(n)); |
| 199 | |
| 200 | return True; |
| 201 | } |
| 202 | |
| 203 | Bool freac::CDDBBatch::AddSubmit(const CDDBInfo &oCddbInfo) |
| 204 | { |
| 205 | BoCA::Config *config = BoCA::Config::Get(); |
| 206 | |
| 207 | CDDBInfo cddbInfo = oCddbInfo; |
| 208 | |
| 209 | UpdateEntry(cddbInfo); |
| 210 | |
| 211 | /* Create directory for entry. |
| 212 | */ |
| 213 | Directory cddbDir(String(config->cacheDir).Append("cddb")); |
| 214 | |
| 215 | if (!cddbDir.Exists()) cddbDir.Create(); |
| 216 | |
| 217 | Directory categoryDir(String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter()).Append(cddbInfo.category)); |
| 218 | |
| 219 | if (!categoryDir.Exists()) categoryDir.Create(); |
| 220 | |
| 221 | /* Save current freedb path. |
| 222 | */ |
| 223 | String configFreedbDir = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 224 | |
| 225 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter())); |
| 226 | |
| 227 | /* Save entry to batch queue. |
| 228 | */ |
| 229 | CDDBLocal().Submit(cddbInfo); |
| 230 | |
| 231 | for (Int i = 0; i < submits.Length(); i++) |
| 232 | { |
| 233 | if (submits.GetNth(i) == cddbInfo) |
| 234 | { |
| 235 | submits.Remove(submits.GetNthIndex(i)); |
| 236 | |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | submits.Add(cddbInfo); |
| 242 | |
| 243 | /* Restore real freedb path. |
| 244 | */ |
| 245 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, configFreedbDir); |
| 246 | |
| 247 | return True; |
| 248 | } |
| 249 | |
| 250 | Bool freac::CDDBBatch::DeleteSubmit(const CDDBInfo &cddbInfo) |
| 251 | { |
| 252 | for (Int i = 0; i < submits.Length(); i++) |
| 253 | { |
| 254 | if (submits.GetNth(i) == cddbInfo) |
| 255 | { |
| 256 | submits.Remove(submits.GetNthIndex(i)); |
| 257 | |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return True; |
| 263 | } |
| 264 | |
| 265 | Int freac::CDDBBatch::Query(Int n) |
| 266 | { |
| 267 | BoCA::Config *config = BoCA::Config::Get(); |
| 268 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 269 | |
| 270 | /* Query entry and delete entry if successful. |
| 271 | */ |
| 272 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableLocalID, Config::FreedbEnableLocalDefault) || |
| 273 | config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableRemoteID, Config::FreedbEnableRemoteDefault)) |
| 274 | { |
| 275 | cddbQueryDlg *dlg = new cddbQueryDlg(queries.GetNth(n)); |
| 276 | Int error = dlg->ShowDialog(); |
| 277 | String errorString = dlg->GetErrorString(); |
| 278 | const CDDBInfo &cddbInfo = dlg->GetCDDBInfo(); |
| 279 | |
| 280 | Object::DeleteObject(dlg); |
| 281 | |
| 282 | if (error == Error()) |
| 283 | { |
| 284 | /* Display error message and return. |
| 285 | */ |
| 286 | BoCA::Utilities::ErrorMessage(i18n->TranslateString("Some error occurred trying to connect to the freedb server.", "CDDB::Query::Errors")); |
| 287 | |
| 288 | return QUERY_RESULT_ERROR; |
| 289 | } |
| 290 | else if (errorString != NIL) |
| 291 | { |
| 292 | /* If there's an info message, add it to the list of errors. |
| 293 | */ |
| 294 | queryErrors.Add(errorString); |
| 295 | } |
| 296 | |
| 297 | if (cddbInfo == NIL) return QUERY_RESULT_NONE; |
| 298 | |
| 299 | /* Save current freedb path. |
| 300 | */ |
| 301 | String configFreedbDir = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault); |
| 302 | |
| 303 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, String(config->cacheDir).Append("cddb").Append(Directory::GetDirectoryDelimiter())); |
| 304 | |
| 305 | /* Save entry to local cache. |
| 306 | */ |
| 307 | CDDBLocal().Submit(cddbInfo); |
| 308 | |
| 309 | /* Restore real freedb path. |
| 310 | */ |
| 311 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, configFreedbDir); |
| 312 | |
| 313 | DeleteQuery(n); |
| 314 | |
| 315 | return QUERY_RESULT_SINGLE; |
| 316 | } |
| 317 | |
| 318 | return QUERY_RESULT_ERROR; |
| 319 | } |
| 320 | |
| 321 | Bool freac::CDDBBatch::QueryAll() |
| 322 | { |
| 323 | /* Run all scheduled queries. |
| 324 | */ |
| 325 | Int skipped = 0; |
| 326 | |
| 327 | while (queries.Length() > skipped) |
| 328 | { |
| 329 | Int result = Query(skipped); |
| 330 | |
| 331 | if (result == QUERY_RESULT_ERROR) return False; |
| 332 | else if (result == QUERY_RESULT_NONE) skipped++; |
| 333 | } |
| 334 | |
| 335 | /* Display errors. |
| 336 | */ |
| 337 | if (queryErrors.Length() > 0) |
| 338 | { |
| 339 | ErrorDialog dialog(queryErrors); |
| 340 | |
| 341 | dialog.ShowDialog(); |
| 342 | } |
| 343 | |
| 344 | return True; |
| 345 | } |
| 346 | |
| 347 | Bool freac::CDDBBatch::Submit(const CDDBInfo &cddbInfo) |
| 348 | { |
| 349 | /* Submit and delete entry if successful. |
| 350 | */ |
| 351 | if (!CDDBRemote().Submit(cddbInfo)) |
| 352 | { |
| 353 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 354 | |
| 355 | i18n->SetContext("CDDB::Submit::Errors"); |
| 356 | |
| 357 | BoCA::Utilities::ErrorMessage(i18n->TranslateString("Some error occurred trying to connect to the freedb server.")); |
| 358 | |
| 359 | return False; |
| 360 | } |
| 361 | |
| 362 | DeleteSubmit(cddbInfo); |
| 363 | |
| 364 | return True; |
| 365 | } |
| 366 | |
| 367 | Bool freac::CDDBBatch::SubmitAll() |
| 368 | { |
| 369 | while (submits.Length() > 0) |
| 370 | { |
| 371 | if (!Submit(submits.GetNth(0))) return False; |
| 372 | } |
| 373 | |
| 374 | return True; |
| 375 | } |