| 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/config/config_cddb.h> |
| 14 | #include <dialogs/cddb/extsettings.h> |
| 15 | |
| 16 | #include <cddb/cddb.h> |
| 17 | |
| 18 | #include <config.h> |
| 19 | |
| 20 | using namespace smooth::GUI::Dialogs; |
| 21 | |
| 22 | using namespace BoCA::AS; |
| 23 | |
| 24 | freac::ConfigureCDDB::ConfigureCDDB() |
| 25 | { |
| 26 | BoCA::Config *config = BoCA::Config::Get(); |
| 27 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 28 | |
| 29 | i18n->SetContext("Configuration::CDDB"); |
| 30 | |
| 31 | cddb_autoquery = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbAutoQueryID, Config::FreedbAutoQueryDefault); |
| 32 | cddb_autoselect = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbAutoSelectID, Config::FreedbAutoSelectDefault); |
| 33 | |
| 34 | cddb_cdtext = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbOverwriteCDTextID, Config::FreedbOverwriteCDTextDefault); |
| 35 | cddb_cache = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableCacheID, Config::FreedbEnableCacheDefault); |
| 36 | |
| 37 | cddb_local = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableLocalID, Config::FreedbEnableLocalDefault); |
| 38 | cddb_remote = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbEnableRemoteID, Config::FreedbEnableRemoteDefault); |
| 39 | |
| 40 | layer_local_background = new Layer(); |
| 41 | layer_local_background->SetBackgroundColor(Setup::BackgroundColor); |
| 42 | |
| 43 | layer_remote_background = new Layer(); |
| 44 | layer_remote_background->SetBackgroundColor(Setup::BackgroundColor); |
| 45 | |
| 46 | check_local = new CheckBox(i18n->TranslateString("Enable local CDDB database"), Point(2, 0), Size(), &cddb_local); |
| 47 | check_local->onAction.Connect(&ConfigureCDDB::ToggleLocalCDDB, this); |
| 48 | check_local->SetWidth(check_local->GetUnscaledTextWidth() + 20); |
| 49 | |
| 50 | check_remote = new CheckBox(i18n->TranslateString("Enable remote CDDB database"), Point(2, 0), Size(), &cddb_remote); |
| 51 | check_remote->onAction.Connect(&ConfigureCDDB::ToggleRemoteCDDB, this); |
| 52 | check_remote->SetWidth(check_remote->GetUnscaledTextWidth() + 20); |
| 53 | |
| 54 | layer_local_background->SetMetrics(Point(14, 3), check_local->GetSize() + Size(4, 0)); |
| 55 | layer_remote_background->SetMetrics(Point(14, 58), check_remote->GetSize() + Size(4, 0)); |
| 56 | |
| 57 | layer_local_background->Add(check_local); |
| 58 | layer_remote_background->Add(check_remote); |
| 59 | |
| 60 | group_local = new GroupBox(NIL, Point(7, 11), Size(354, 43)); |
| 61 | |
| 62 | text_dir = new Text(i18n->AddColon(i18n->TranslateString("CDDB path")), Point(16, 26)); |
| 63 | edit_dir = new EditBox(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, Config::FreedbDirectoryDefault), Point(122, 23), Size(119, 0), 0); |
| 64 | |
| 65 | button_browse = new Button(i18n->TranslateString("Select"), Point(271, 22), Size()); |
| 66 | button_browse->onAction.Connect(&ConfigureCDDB::SelectDir, this); |
| 67 | |
| 68 | group_cddb = new GroupBox(NIL, Point(7, 66), Size(354, 125)); |
| 69 | |
| 70 | text_mode = new Text(i18n->AddColon(i18n->TranslateString("CDDB access mode")), Point(16, 81)); |
| 71 | |
| 72 | combo_mode = new ComboBox(Point(122, 78), Size(219, 0)); |
| 73 | combo_mode->onSelectEntry.Connect(&ConfigureCDDB::SetCDDBMode, this); |
| 74 | combo_mode->AddEntry("HTTP POST"); |
| 75 | combo_mode->AddEntry("HTTP GET/HTTP POST"); |
| 76 | combo_mode->AddEntry("CDDBP/HTTP POST"); |
| 77 | |
| 78 | text_server = new Text(i18n->AddColon(i18n->TranslateString("CDDB server")), Point(16, 108)); |
| 79 | edit_server = new EditBox(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, Config::FreedbServerDefault), Point(122, 105), Size(146, 0), 0); |
| 80 | |
| 81 | list_server = new List(); |
| 82 | list_server->AddEntry(Config::FreedbServerDefault); |
| 83 | |
| 84 | edit_server->SetDropDownList(list_server); |
| 85 | |
| 86 | text_port = new Text(i18n->AddColon(i18n->TranslateString("Port")), Point(285, 108)); |
| 87 | text_port->SetX(306 - text_port->GetUnscaledTextWidth()); |
| 88 | |
| 89 | edit_port = new EditBox(Point(314, 105), Size(37, 0), 5); |
| 90 | edit_port->SetFlags(EDB_NUMERIC); |
| 91 | |
| 92 | text_email = new Text(i18n->AddColon(i18n->TranslateString("eMail address")), Point(16, 135)); |
| 93 | edit_email = new EditBox(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbEmailID, Config::FreedbEmailDefault), Point(122, 132), Size(146, 0), 0); |
| 94 | |
| 95 | list_email = new List(); |
| 96 | list_email->AddEntry(Config::FreedbEmailDefault); |
| 97 | |
| 98 | edit_email->SetDropDownList(list_email); |
| 99 | |
| 100 | button_http = new Button(i18n->TranslateString("HTTP settings"), Point(17, 159), Size(163, 0)); |
| 101 | button_http->onAction.Connect(&ConfigureCDDB::HTTPSettings, this); |
| 102 | |
| 103 | button_proxy = new Button(i18n->TranslateString("Proxy settings"), Point(188, 159), Size(163, 0)); |
| 104 | button_proxy->onAction.Connect(&ConfigureCDDB::ProxySettings, this); |
| 105 | |
| 106 | group_cddb_auto = new GroupBox(i18n->TranslateString("Automatization"), Point(369, 11), Size(190, 68)); |
| 107 | |
| 108 | check_autoquery = new CheckBox(i18n->TranslateString("Automatic CDDB queries"), Point(10, 14), Size(170, 0), &cddb_autoquery); |
| 109 | check_autoquery->onAction.Connect(&ConfigureCDDB::ToggleAutoCDDB, this); |
| 110 | |
| 111 | check_autoselect = new CheckBox(i18n->TranslateString("Always select first entry"), Point(10, 40), Size(170, 0), &cddb_autoselect); |
| 112 | |
| 113 | group_cddb_auto->Add(check_autoquery); |
| 114 | group_cddb_auto->Add(check_autoselect); |
| 115 | |
| 116 | group_cddb_options = new GroupBox(i18n->TranslateString("Options"), Point(369, 91), Size(190, 68)); |
| 117 | |
| 118 | check_cdtext = new CheckBox(i18n->TranslateString("Prefer CDDB over CD-Text"), Point(10, 14), Size(170, 0), &cddb_cdtext); |
| 119 | check_cache = new CheckBox(i18n->TranslateString("Enable CDDB cache"), Point(10, 40), Size(170, 0), &cddb_cache); |
| 120 | |
| 121 | group_cddb_options->Add(check_cdtext); |
| 122 | group_cddb_options->Add(check_cache); |
| 123 | |
| 124 | Int maxTextSize = Math::Max(Math::Max(check_autoquery->GetUnscaledTextWidth(), check_autoselect->GetUnscaledTextWidth()), Math::Max(check_cdtext->GetUnscaledTextWidth(), check_cache->GetUnscaledTextWidth())); |
| 125 | |
| 126 | check_autoquery->SetWidth(Math::Max(149, maxTextSize + 21)); |
| 127 | check_autoselect->SetWidth(check_autoquery->GetWidth()); |
| 128 | check_cdtext->SetWidth(check_autoquery->GetWidth()); |
| 129 | check_cache->SetWidth(check_autoquery->GetWidth()); |
| 130 | |
| 131 | group_cddb_auto->SetWidth(check_autoquery->GetWidth() + 20); |
| 132 | group_cddb_options->SetWidth(check_autoquery->GetWidth() + 20); |
| 133 | |
| 134 | combo_mode->SelectNthEntry(config->GetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, Config::FreedbModeDefault)); |
| 135 | |
| 136 | SetCDDBMode(); |
| 137 | ToggleAutoCDDB(); |
| 138 | |
| 139 | ToggleLocalCDDB(); |
| 140 | ToggleRemoteCDDB(); |
| 141 | |
| 142 | maxTextSize = Math::Max(Math::Max(text_dir->GetUnscaledTextWidth(), text_email->GetUnscaledTextWidth()), Math::Max(text_mode->GetUnscaledTextWidth(), text_server->GetUnscaledTextWidth())); |
| 143 | |
| 144 | edit_dir->SetMetrics(Point(maxTextSize + 24, edit_dir->GetY()), Size(239 - maxTextSize, edit_dir->GetHeight())); |
| 145 | combo_mode->SetMetrics(Point(maxTextSize + 24, combo_mode->GetY()), Size(327 - maxTextSize, combo_mode->GetHeight())); |
| 146 | edit_server->SetMetrics(Point(maxTextSize + 24, edit_server->GetY()), Size(275 - maxTextSize - text_port->GetUnscaledTextWidth(), edit_server->GetHeight())); |
| 147 | edit_email->SetMetrics(Point(maxTextSize + 24, edit_email->GetY()), Size(327 - maxTextSize, edit_email->GetHeight())); |
| 148 | |
| 149 | Add(group_local); |
| 150 | Add(text_dir); |
| 151 | Add(edit_dir); |
| 152 | Add(button_browse); |
| 153 | Add(group_cddb); |
| 154 | Add(text_mode); |
| 155 | Add(combo_mode); |
| 156 | Add(text_server); |
| 157 | Add(edit_server); |
| 158 | Add(text_port); |
| 159 | Add(edit_port); |
| 160 | Add(text_email); |
| 161 | Add(edit_email); |
| 162 | Add(button_http); |
| 163 | Add(button_proxy); |
| 164 | Add(group_cddb_auto); |
| 165 | Add(group_cddb_options); |
| 166 | Add(layer_local_background); |
| 167 | Add(layer_remote_background); |
| 168 | |
| 169 | SetSize(Size(376 + group_cddb_auto->GetWidth(), 198)); |
| 170 | } |
| 171 | |
| 172 | freac::ConfigureCDDB::~ConfigureCDDB() |
| 173 | { |
| 174 | DeleteObject(group_local); |
| 175 | DeleteObject(text_dir); |
| 176 | DeleteObject(edit_dir); |
| 177 | DeleteObject(button_browse); |
| 178 | DeleteObject(group_cddb); |
| 179 | DeleteObject(text_mode); |
| 180 | DeleteObject(combo_mode); |
| 181 | DeleteObject(text_server); |
| 182 | DeleteObject(edit_server); |
| 183 | DeleteObject(list_server); |
| 184 | DeleteObject(text_port); |
| 185 | DeleteObject(edit_port); |
| 186 | DeleteObject(text_email); |
| 187 | DeleteObject(edit_email); |
| 188 | DeleteObject(list_email); |
| 189 | DeleteObject(button_http); |
| 190 | DeleteObject(button_proxy); |
| 191 | |
| 192 | DeleteObject(group_cddb_auto); |
| 193 | DeleteObject(check_autoquery); |
| 194 | DeleteObject(check_autoselect); |
| 195 | |
| 196 | DeleteObject(group_cddb_options); |
| 197 | DeleteObject(check_cdtext); |
| 198 | DeleteObject(check_cache); |
| 199 | |
| 200 | DeleteObject(layer_local_background); |
| 201 | DeleteObject(layer_remote_background); |
| 202 | DeleteObject(check_local); |
| 203 | DeleteObject(check_remote); |
| 204 | } |
| 205 | |
| 206 | Void freac::ConfigureCDDB::SelectDir() |
| 207 | { |
| 208 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 209 | |
| 210 | i18n->SetContext("Configuration::CDDB"); |
| 211 | |
| 212 | DirSelection dialog; |
| 213 | |
| 214 | dialog.SetParentWindow(GetContainerWindow()); |
| 215 | dialog.SetCaption(i18n->AddColon(i18n->TranslateString("Select the folder of the CDDB database"))); |
| 216 | dialog.SetDirName(edit_dir->GetText()); |
| 217 | |
| 218 | if (dialog.ShowDialog() == Success()) |
| 219 | { |
| 220 | edit_dir->SetText(dialog.GetDirName()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | Void freac::ConfigureCDDB::SetCDDBMode() |
| 225 | { |
| 226 | BoCA::Config *config = BoCA::Config::Get(); |
| 227 | |
| 228 | if (combo_mode->GetSelectedEntryNumber() == FREEDB_MODE_CDDBP) |
| 229 | { |
| 230 | edit_port->Activate(); |
| 231 | edit_port->SetText(String::FromInt(config->GetIntValue(Config::CategoryFreedbID, Config::FreedbCDDBPPortID, Config::FreedbCDDBPPortDefault))); |
| 232 | } |
| 233 | else if (combo_mode->GetSelectedEntryNumber() == FREEDB_MODE_HTTP_GET || |
| 234 | combo_mode->GetSelectedEntryNumber() == FREEDB_MODE_HTTP_POST) |
| 235 | { |
| 236 | edit_port->Deactivate(); |
| 237 | edit_port->SetText(String::FromInt(config->GetIntValue(Config::CategoryFreedbID, Config::FreedbHTTPPortID, Config::FreedbHTTPPortDefault))); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | Void freac::ConfigureCDDB::ToggleLocalCDDB() |
| 242 | { |
| 243 | if (cddb_local) |
| 244 | { |
| 245 | edit_dir->Activate(); |
| 246 | button_browse->Activate(); |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | edit_dir->Deactivate(); |
| 251 | button_browse->Deactivate(); |
| 252 | } |
| 253 | |
| 254 | ToggleCDDBSettings(); |
| 255 | } |
| 256 | |
| 257 | Void freac::ConfigureCDDB::ToggleRemoteCDDB() |
| 258 | { |
| 259 | if (cddb_remote) |
| 260 | { |
| 261 | combo_mode->Activate(); |
| 262 | edit_server->Activate(); |
| 263 | edit_port->Activate(); |
| 264 | edit_email->Activate(); |
| 265 | button_http->Activate(); |
| 266 | button_proxy->Activate(); |
| 267 | |
| 268 | SetCDDBMode(); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | combo_mode->Deactivate(); |
| 273 | edit_server->Deactivate(); |
| 274 | edit_port->Deactivate(); |
| 275 | edit_email->Deactivate(); |
| 276 | button_http->Deactivate(); |
| 277 | button_proxy->Deactivate(); |
| 278 | } |
| 279 | |
| 280 | ToggleCDDBSettings(); |
| 281 | } |
| 282 | |
| 283 | Void freac::ConfigureCDDB::ToggleCDDBSettings() |
| 284 | { |
| 285 | if (cddb_local || cddb_remote) |
| 286 | { |
| 287 | group_cddb_auto->Activate(); |
| 288 | group_cddb_options->Activate(); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | group_cddb_auto->Deactivate(); |
| 293 | group_cddb_options->Deactivate(); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | Void freac::ConfigureCDDB::ToggleAutoCDDB() |
| 298 | { |
| 299 | if (cddb_autoquery) check_cdtext->Activate(); |
| 300 | else check_cdtext->Deactivate(); |
| 301 | } |
| 302 | |
| 303 | Void freac::ConfigureCDDB::HTTPSettings() |
| 304 | { |
| 305 | cddbExtendedSettingsDlg(0).ShowDialog(); |
| 306 | } |
| 307 | |
| 308 | Void freac::ConfigureCDDB::ProxySettings() |
| 309 | { |
| 310 | cddbExtendedSettingsDlg(1).ShowDialog(); |
| 311 | } |
| 312 | |
| 313 | Int freac::ConfigureCDDB::SaveSettings() |
| 314 | { |
| 315 | BoCA::Config *config = BoCA::Config::Get(); |
| 316 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 317 | |
| 318 | i18n->SetContext("Configuration::CDDB::Errors"); |
| 319 | |
| 320 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault) == 1 && combo_mode->GetSelectedEntryNumber() == FREEDB_MODE_CDDBP) |
| 321 | { |
| 322 | Int selection = QuickMessage(i18n->TranslateString("The freedb CDDBP protocol cannot be used over HTTP\nForward proxies!\n\nWould you like to change the protocol to HTTP?"), i18n->TranslateString("Error"), Message::Buttons::YesNoCancel, Message::Icon::Question); |
| 323 | |
| 324 | if (selection == Message::Button::Yes) combo_mode->SelectNthEntry(FREEDB_MODE_HTTP_POST); |
| 325 | else if (selection == Message::Button::Cancel) return Error(); |
| 326 | } |
| 327 | |
| 328 | /* Get number of drives. |
| 329 | */ |
| 330 | Registry &boca = Registry::Get(); |
| 331 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 332 | Int numDrives = 0; |
| 333 | |
| 334 | if (info != NIL) |
| 335 | { |
| 336 | numDrives = info->GetNumberOfDevices(); |
| 337 | |
| 338 | boca.DeleteComponent(info); |
| 339 | } |
| 340 | |
| 341 | /* Check validity of eMail address. |
| 342 | */ |
| 343 | Bool valid = False; |
| 344 | String email = edit_email->GetText(); |
| 345 | |
| 346 | for (Int i = 0; i < email.Length(); i++) if (email[i] == '@') valid = True; |
| 347 | |
| 348 | if (numDrives >= 1 && !valid) |
| 349 | { |
| 350 | BoCA::Utilities::ErrorMessage(i18n->TranslateString("Please enter a valid eMail address.")); |
| 351 | |
| 352 | return Error(); |
| 353 | } |
| 354 | |
| 355 | String freedb_dir = edit_dir->GetText(); |
| 356 | |
| 357 | if (!freedb_dir.EndsWith(Directory::GetDirectoryDelimiter())) freedb_dir.Append(Directory::GetDirectoryDelimiter()); |
| 358 | |
| 359 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbAutoQueryID, cddb_autoquery); |
| 360 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbAutoSelectID, cddb_autoselect); |
| 361 | |
| 362 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbOverwriteCDTextID, cddb_cdtext); |
| 363 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbEnableCacheID, cddb_cache); |
| 364 | |
| 365 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbEnableLocalID, cddb_local); |
| 366 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbEnableRemoteID, cddb_remote); |
| 367 | |
| 368 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbDirectoryID, freedb_dir); |
| 369 | |
| 370 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, edit_server->GetText()); |
| 371 | config->SetStringValue(Config::CategoryFreedbID, Config::FreedbEmailID, edit_email->GetText()); |
| 372 | |
| 373 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, combo_mode->GetSelectedEntryNumber()); |
| 374 | |
| 375 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, Config::FreedbModeDefault) == FREEDB_MODE_CDDBP) config->SetIntValue(Config::CategoryFreedbID, Config::FreedbCDDBPPortID, edit_port->GetText().ToInt()); |
| 376 | else config->SetIntValue(Config::CategoryFreedbID, Config::FreedbHTTPPortID, edit_port->GetText().ToInt()); |
| 377 | |
| 378 | return Success(); |
| 379 | } |