| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2023 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 <dialogs/cddb/query.h> |
| 14 | #include <dialogs/cddb/multimatch.h> |
| 15 | |
| 16 | #include <config.h> |
| 17 | #include <dllinterfaces.h> |
| 18 | #include <utilities.h> |
| 19 | |
| 20 | #include <cddb/cddblocal.h> |
| 21 | #include <cddb/cddbremote.h> |
| 22 | |
| 23 | #include <cddb/cddbcache.h> |
| 24 | #include <cddb/cddbbatch.h> |
| 25 | |
| 26 | using namespace smooth::GUI::Dialogs; |
| 27 | |
| 28 | freac::cddbQueryDlg::cddbQueryDlg(const String &iQueryString) |
| 29 | { |
| 30 | BoCA::Config *config = BoCA::Config::Get(); |
| 31 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 32 | |
| 33 | i18n->SetContext("CDDB::Query"); |
| 34 | |
| 35 | errorState = False; |
| 36 | |
| 37 | queryThread = NIL; |
| 38 | queryString = iQueryString; |
| 39 | |
| 40 | stopQueryThread = False; |
| 41 | |
| 42 | if (!config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault)) |
| 43 | { |
| 44 | mainWnd = new Window(i18n->TranslateString("CDDB query"), Point(config->GetIntValue(Config::CategorySettingsID, Config::SettingsWindowPosXID, Config::SettingsWindowPosXDefault), config->GetIntValue(Config::CategorySettingsID, Config::SettingsWindowPosYID, Config::SettingsWindowPosYDefault)) + Point(40, 40), Size(310, 84)); |
| 45 | mainWnd->SetRightToLeft(i18n->IsActiveLanguageRightToLeft()); |
| 46 | |
| 47 | mainWnd_titlebar = new Titlebar(TB_CLOSEBUTTON); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | mainWnd = NIL; |
| 52 | mainWnd_titlebar = NIL; |
| 53 | } |
| 54 | |
| 55 | text_status = new Text(NIL, Point(7, 5)); |
| 56 | prog_status = new Progressbar(Point(7, 24), Size(200, 0), OR_HORZ, PB_NOTEXT, 0, 100, 0); |
| 57 | |
| 58 | btn_cancel = new Button(i18n->TranslateString("Cancel"), Point(215, 23), Size()); |
| 59 | btn_cancel->onAction.Connect(&cddbQueryDlg::Cancel, this); |
| 60 | |
| 61 | if (mainWnd != NIL) |
| 62 | { |
| 63 | Add(mainWnd); |
| 64 | |
| 65 | mainWnd->Add(btn_cancel); |
| 66 | mainWnd->Add(text_status); |
| 67 | mainWnd->Add(prog_status); |
| 68 | mainWnd->Add(mainWnd_titlebar); |
| 69 | |
| 70 | mainWnd->SetFlags(mainWnd->GetFlags() | WF_NOTASKBUTTON | WF_MODAL); |
| 71 | mainWnd->SetIcon(ImageLoader::Load(String(Config::Get()->resourcesPath).Append("icons/freac.png"))); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | freac::cddbQueryDlg::~cddbQueryDlg() |
| 76 | { |
| 77 | if (mainWnd == NIL) return; |
| 78 | |
| 79 | DeleteObject(mainWnd_titlebar); |
| 80 | DeleteObject(mainWnd); |
| 81 | DeleteObject(text_status); |
| 82 | DeleteObject(prog_status); |
| 83 | DeleteObject(btn_cancel); |
| 84 | } |
| 85 | |
| 86 | const Error &freac::cddbQueryDlg::ShowDialog() |
| 87 | { |
| 88 | /* Show dialog only in GUI mode. |
| 89 | */ |
| 90 | if (mainWnd != NIL) mainWnd->Show(); |
| 91 | |
| 92 | /* Start query thread and wait for operations to complete. |
| 93 | */ |
| 94 | queryThread = NonBlocking0<>(&cddbQueryDlg::QueryThread, this).Call(); |
| 95 | |
| 96 | if (mainWnd != NIL) mainWnd->WaitUntilClosed(); |
| 97 | |
| 98 | queryThread->Wait(); |
| 99 | |
| 100 | return error; |
| 101 | } |
| 102 | |
| 103 | Void freac::cddbQueryDlg::Cancel() |
| 104 | { |
| 105 | if (queryThread == NIL) return; |
| 106 | |
| 107 | /* Close window and signal thread to exit. |
| 108 | */ |
| 109 | mainWnd->Close(); |
| 110 | |
| 111 | stopQueryThread = True; |
| 112 | |
| 113 | /* Wait up to one second for thread to finish. |
| 114 | */ |
| 115 | for (Int i = 0; i < 100; i++) |
| 116 | { |
| 117 | if (queryThread->GetStatus() != Threads::THREAD_RUNNING) return; |
| 118 | |
| 119 | S::System::System::Sleep(10); |
| 120 | } |
| 121 | |
| 122 | /* Kill thead if it did not finish after one second. |
| 123 | */ |
| 124 | queryThread->Stop(); |
| 125 | } |
| 126 | |
| 127 | Int freac::cddbQueryDlg::QueryThread() |
| 128 | { |
| 129 | /* Get config values. |
| 130 | */ |
| 131 | BoCA::Config *config = BoCA::Config::Get(); |
| 132 | |
| 133 | Bool enableLocal = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableLocalID, Config::FreedbEnableLocalDefault); |
| 134 | Bool enableRemote = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableRemoteID, Config::FreedbEnableRemoteDefault); |
| 135 | |
| 136 | /* Query local CDDB. |
| 137 | */ |
| 138 | Bool result = False; |
| 139 | |
| 140 | if (enableLocal) |
| 141 | { |
| 142 | CDDBLocal cddbLocal; |
| 143 | |
| 144 | result = Query(cddbLocal); |
| 145 | } |
| 146 | |
| 147 | /* Query remote CDDB. |
| 148 | */ |
| 149 | if (!result && enableRemote) |
| 150 | { |
| 151 | CDDBRemote cddbRemote; |
| 152 | |
| 153 | result = Query(cddbRemote); |
| 154 | } |
| 155 | |
| 156 | /* Close window and return. |
| 157 | */ |
| 158 | if (mainWnd != NIL) mainWnd->Close(); |
| 159 | |
| 160 | if (result) return Success(); |
| 161 | else return Error(); |
| 162 | } |
| 163 | |
| 164 | Bool freac::cddbQueryDlg::Query(CDDB &cddb) |
| 165 | { |
| 166 | BoCA::Config *config = BoCA::Config::Get(); |
| 167 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 168 | |
| 169 | i18n->SetContext("CDDB::Query"); |
| 170 | |
| 171 | errorState = False; |
| 172 | errorString = NIL; |
| 173 | |
| 174 | Int result; |
| 175 | |
| 176 | prog_status->SetValue(0); |
| 177 | text_status->SetText(i18n->AddEllipsis(i18n->TranslateString("Connecting to freedb server at").Append(" ").Append(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, Config::FreedbServerDefault)))); |
| 178 | |
| 179 | cddb.ConnectToServer(); |
| 180 | |
| 181 | if (stopQueryThread) { cddb.CloseConnection(); return False; } |
| 182 | |
| 183 | prog_status->SetValue(20); |
| 184 | text_status->SetText(i18n->AddEllipsis(i18n->TranslateString("Requesting CD information"))); |
| 185 | |
| 186 | /* Perform query using query string. |
| 187 | */ |
| 188 | result = cddb.Query(queryString); |
| 189 | |
| 190 | if (stopQueryThread) { cddb.CloseConnection(); return False; } |
| 191 | |
| 192 | /* Process result. |
| 193 | */ |
| 194 | String category; |
| 195 | UnsignedInt32 discID = 0; |
| 196 | Bool fuzzy = False; |
| 197 | |
| 198 | if (result == QUERY_RESULT_NONE) |
| 199 | { |
| 200 | errorState = True; |
| 201 | errorString = i18n->TranslateString("%1:", "Characters").Replace("%1", queryString.SubString(11, 8)).Append(" ").Append(i18n->TranslateString("No freedb entry for this disc.")).Append("\n\n").Append(queryString); |
| 202 | } |
| 203 | else if (result == QUERY_RESULT_SINGLE) |
| 204 | { |
| 205 | category = cddb.GetNthCategory(0); |
| 206 | discID = cddb.GetNthDiscID(0); |
| 207 | } |
| 208 | else if (result == QUERY_RESULT_MULTIPLE || result == QUERY_RESULT_FUZZY) |
| 209 | { |
| 210 | if (result == QUERY_RESULT_FUZZY) fuzzy = True; |
| 211 | |
| 212 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbAutoSelectID, Config::FreedbAutoSelectDefault)) |
| 213 | { |
| 214 | category = cddb.GetNthCategory(0); |
| 215 | discID = cddb.GetNthDiscID(0); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | cddbMultiMatchDlg dlg(cddb, fuzzy); |
| 220 | |
| 221 | for (Int i = 0; i < cddb.GetNumberOfMatches(); i++) dlg.AddEntry(cddb.GetNthCategory(i), cddb.GetNthTitle(i), cddb.GetNthDiscID(i)); |
| 222 | |
| 223 | if (fuzzy) dlg.AddEntry(i18n->TranslateString("none"), NIL, 0); |
| 224 | |
| 225 | if (dlg.ShowDialog() == Success()) |
| 226 | { |
| 227 | Int index = dlg.GetSelectedEntryNumber(); |
| 228 | |
| 229 | if (index < cddb.GetNumberOfMatches() && index >= 0) |
| 230 | { |
| 231 | category = cddb.GetNthCategory(index); |
| 232 | discID = cddb.GetNthDiscID(index); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (stopQueryThread) { cddb.CloseConnection(); return False; } |
| 239 | |
| 240 | /* Read actual CDDB data. |
| 241 | */ |
| 242 | Bool readError = False; |
| 243 | |
| 244 | if (category != NIL && discID != 0) |
| 245 | { |
| 246 | prog_status->SetValue(60); |
| 247 | |
| 248 | if (!cddb.Read(category, discID, cddbInfo)) readError = True; |
| 249 | |
| 250 | /* Update track offsets and disc ID when we had a fuzzy match. |
| 251 | */ |
| 252 | if (fuzzy) |
| 253 | { |
| 254 | cddbInfo.revision = -1; |
| 255 | |
| 256 | cddbInfo.UpdateFromQueryString(queryString); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (stopQueryThread) { cddbInfo = NIL; cddb.CloseConnection(); return False; } |
| 261 | |
| 262 | /* Process read errors. |
| 263 | */ |
| 264 | if (readError || result == QUERY_RESULT_ERROR) |
| 265 | { |
| 266 | errorState = True; |
| 267 | errorString = i18n->TranslateString("Some error occurred trying to connect to the freedb server.", "CDDB::Query::Errors"); |
| 268 | error = Error(); |
| 269 | } |
| 270 | |
| 271 | cddb.CloseConnection(); |
| 272 | |
| 273 | prog_status->SetValue(100); |
| 274 | text_status->SetText(NIL); |
| 275 | |
| 276 | if (category == NIL || discID == 0) return False; |
| 277 | else return True; |
| 278 | } |
| 279 | |
| 280 | freac::CDDBInfo freac::cddbQueryDlg::QueryCDDB(const BoCA::Track &track, Bool autoQuery) |
| 281 | { |
| 282 | BoCA::Config *config = BoCA::Config::Get(); |
| 283 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 284 | |
| 285 | String queryString = CDDB::QueryStringFromMCDI(track.GetInfo().mcdi); |
| 286 | CDDBInfo cdInfo; |
| 287 | |
| 288 | /* Query CDDB cache first. |
| 289 | */ |
| 290 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableCacheID, Config::FreedbEnableCacheDefault)) |
| 291 | { |
| 292 | cdInfo = CDDBCache::Get()->GetCacheEntry(queryString); |
| 293 | |
| 294 | if (cdInfo != NIL) return cdInfo; |
| 295 | } |
| 296 | |
| 297 | /* Perform local or remote CDDB query. |
| 298 | */ |
| 299 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableLocalID, Config::FreedbEnableLocalDefault) || |
| 300 | config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableRemoteID, Config::FreedbEnableRemoteDefault)) |
| 301 | { |
| 302 | cddbQueryDlg dlg(queryString); |
| 303 | |
| 304 | Error errorStatus = dlg.ShowDialog(); |
| 305 | String errorString = dlg.GetErrorString(); |
| 306 | |
| 307 | if (errorStatus == Error() && !config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault)) |
| 308 | { |
| 309 | /* Ask whether to perform this query later. |
| 310 | */ |
| 311 | if (QuickMessage(String(errorString).Append("\n\n").Append(i18n->TranslateString("Would you like to perform this query again later?", "CDDB::Query::Errors")), i18n->TranslateString("Error"), Message::Buttons::YesNo, Message::Icon::Error) == Message::Button::Yes) |
| 312 | { |
| 313 | CDDBBatch().AddQuery(queryString); |
| 314 | } |
| 315 | } |
| 316 | else if (errorString != NIL && autoQuery && !config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault)) |
| 317 | { |
| 318 | /* Display info message. |
| 319 | */ |
| 320 | Bool doNotDisplayAgain = !config->GetIntValue(Config::CategoryFreedbID, Config::FreedbDisplayNotFoundID, Config::FreedbDisplayNotFoundDefault); |
| 321 | |
| 322 | if (!doNotDisplayAgain) |
| 323 | { |
| 324 | MessageDlg messageBox(errorString, i18n->TranslateString("Info", "Messages"), Message::Buttons::Ok, Message::Icon::Information, i18n->TranslateString("Do not display this note again"), &doNotDisplayAgain); |
| 325 | |
| 326 | messageBox.ShowDialog(); |
| 327 | |
| 328 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbDisplayNotFoundID, !doNotDisplayAgain); |
| 329 | } |
| 330 | } |
| 331 | else if (errorString != NIL) |
| 332 | { |
| 333 | /* Display error or info message. |
| 334 | */ |
| 335 | if (errorStatus == Error()) BoCA::Utilities::ErrorMessage(errorString); |
| 336 | else BoCA::Utilities::InfoMessage(errorString); |
| 337 | } |
| 338 | |
| 339 | cdInfo = dlg.GetCDDBInfo(); |
| 340 | } |
| 341 | |
| 342 | if (cdInfo != NIL) CDDBCache::Get()->AddCacheEntry(cdInfo); |
| 343 | |
| 344 | return cdInfo; |
| 345 | } |