| 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 <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include "accuraterip.h" |
| 17 | #include "database.h" |
| 18 | #include "drive.h" |
| 19 | #include "driveoffsets.h" |
| 20 | #include "config.h" |
| 21 | |
| 22 | const String &BoCA::VerifierAccurateRip::GetComponentSpecs() |
| 23 | { |
| 24 | Database::InitializeSessionCache(); |
| 25 | |
| 26 | I18n *i18n = I18n::Get(); |
| 27 | |
| 28 | i18n->SetContext("Components::Verifiers"); |
| 29 | |
| 30 | static String componentSpecs = String(" \ |
| 31 | \ |
| 32 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 33 | <component> \ |
| 34 | <name>").Append(i18n->TranslateString("AccurateRip Verifier")).Append("</name> \ |
| 35 | <version>1.0</version> \ |
| 36 | <id>accuraterip-verify</id> \ |
| 37 | <type>verifier</type> \ |
| 38 | </component> \ |
| 39 | \ |
| 40 | "); |
| 41 | |
| 42 | return componentSpecs; |
| 43 | } |
| 44 | |
| 45 | Void smooth::AttachDLL(Void *instance) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | Void smooth::DetachDLL() |
| 50 | { |
| 51 | BoCA::DriveOffsets::Free(); |
| 52 | } |
| 53 | |
| 54 | using namespace smooth::GUI::Dialogs; |
| 55 | using namespace smooth::IO; |
| 56 | using namespace smooth::Net; |
| 57 | |
| 58 | BoCA::VerifierAccurateRip::VerifierAccurateRip() |
| 59 | { |
| 60 | configLayer = NIL; |
| 61 | } |
| 62 | |
| 63 | BoCA::VerifierAccurateRip::~VerifierAccurateRip() |
| 64 | { |
| 65 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 66 | } |
| 67 | |
| 68 | Bool BoCA::VerifierAccurateRip::CanVerifyTrack(const Track &track) |
| 69 | { |
| 70 | const Config *config = GetConfiguration(); |
| 71 | |
| 72 | if (!config->GetIntValue(ConfigureAccurateRip::ConfigID, ConfigureAccurateRip::ConfigEnabledID, ConfigureAccurateRip::ConfigEnabledDefault)) return False; |
| 73 | |
| 74 | if (track.isCDTrack && track.cdTrack > 0) return True; |
| 75 | else return False; |
| 76 | } |
| 77 | |
| 78 | Bool BoCA::VerifierAccurateRip::Activate() |
| 79 | { |
| 80 | return algorithm.Init(track); |
| 81 | } |
| 82 | |
| 83 | Int BoCA::VerifierAccurateRip::ProcessData(Buffer<UnsignedByte> &data) |
| 84 | { |
| 85 | return algorithm.ProcessData(data); |
| 86 | } |
| 87 | |
| 88 | Bool BoCA::VerifierAccurateRip::Verify() |
| 89 | { |
| 90 | /* Check if we are invoked in response to a disc insert. |
| 91 | */ |
| 92 | const Config *config = GetConfiguration(); |
| 93 | |
| 94 | if (config->GetIntValue(ConfigureAccurateRip::ConfigID, "ConfigureDrive", -1) != -1) return OnConfigureDrive(); |
| 95 | if (config->GetIntValue(ConfigureAccurateRip::ConfigID, "DiscInsert", -1) != -1) return OnDiscInsert(); |
| 96 | |
| 97 | /* Calculate AccurateRip disc ID and query database. |
| 98 | */ |
| 99 | String discID = Algorithm::CalculateDiscID(track.GetInfo().mcdi); |
| 100 | const DatabaseEntry &entry = Database::Query(config, discID); |
| 101 | |
| 102 | if (!entry.IsValid()) |
| 103 | { |
| 104 | /* Cannot verify track if not found in AccurateRip database. |
| 105 | */ |
| 106 | errorString = String("Disc ID not found in AccurateRip database: ").Append(discID); |
| 107 | |
| 108 | return False; |
| 109 | } |
| 110 | |
| 111 | /* Verify checksums and construct result message. |
| 112 | */ |
| 113 | UnsignedInt32 arCRCv1 = algorithm.GetCRCv1(); |
| 114 | UnsignedInt32 arCRCv2 = algorithm.GetCRCv2(); |
| 115 | |
| 116 | Int v1Confidence = entry.Verify(track.cdTrack, arCRCv1); |
| 117 | Int v2Confidence = entry.Verify(track.cdTrack, arCRCv2); |
| 118 | |
| 119 | errorState = False; |
| 120 | errorString = NIL; |
| 121 | |
| 122 | if (v1Confidence >= 0 || v2Confidence >= 0) |
| 123 | { |
| 124 | errorString.Append("Track has been accurately ripped:"); |
| 125 | |
| 126 | if (v1Confidence >= 0) errorString.Append("\n Checksum (AccurateRip v1): %1").Append(", Confidence: %2").Replace("%1", Number(Int64(arCRCv1)).ToHexString(8)).Replace("%2", String::FromInt(v1Confidence)); |
| 127 | if (v2Confidence >= 0) errorString.Append("\n Checksum (AccurateRip v2): %1").Append(", Confidence: %2").Replace("%1", Number(Int64(arCRCv2)).ToHexString(8)).Replace("%2", String::FromInt(v2Confidence)); |
| 128 | |
| 129 | return True; |
| 130 | } |
| 131 | |
| 132 | Drive drive(config, track.drive); |
| 133 | ConfigurationLevel configurationLevel = drive.GetConfigurationLevel(); |
| 134 | |
| 135 | if (configurationLevel < ConfigurationLevelAutomatic) |
| 136 | { |
| 137 | errorString.Append("AccurateRip verification skipped as drive offset is not configured."); |
| 138 | |
| 139 | return False; |
| 140 | } |
| 141 | |
| 142 | errorState = True; |
| 143 | errorString.Append("Track could not be verified as accurate:"); |
| 144 | |
| 145 | errorString.Append("\n Checksum (AccurateRip v1): %1").Replace("%1", Number(Int64(arCRCv1)).ToHexString(8)); |
| 146 | errorString.Append("\n Checksum (AccurateRip v2): %1").Replace("%1", Number(Int64(arCRCv2)).ToHexString(8)); |
| 147 | |
| 148 | return False; |
| 149 | } |
| 150 | |
| 151 | Bool BoCA::VerifierAccurateRip::OnConfigureDrive() |
| 152 | { |
| 153 | const Config *config = GetConfiguration(); |
| 154 | |
| 155 | Int driveNumber = config->GetIntValue(ConfigureAccurateRip::ConfigID, "ConfigureDrive", -1); |
| 156 | |
| 157 | NonBlocking1<Int>(&ConfigureDrive).Call(driveNumber); |
| 158 | |
| 159 | return True; |
| 160 | } |
| 161 | |
| 162 | Void BoCA::VerifierAccurateRip::ConfigureDrive(Int driveNumber) |
| 163 | { |
| 164 | const Config *config = Config::Get(); |
| 165 | |
| 166 | Drive drive(config, driveNumber); |
| 167 | |
| 168 | if (drive.GetConfigurationLevel() < ConfigurationLevelAutomatic) drive.ConfigureOffset(True); |
| 169 | } |
| 170 | |
| 171 | Bool BoCA::VerifierAccurateRip::OnDiscInsert() |
| 172 | { |
| 173 | const Config *config = GetConfiguration(); |
| 174 | |
| 175 | if (!config->GetIntValue(ConfigureAccurateRip::ConfigID, ConfigureAccurateRip::ConfigNotifyMissingEntryID, ConfigureAccurateRip::ConfigNotifyMissingEntryDefault)) return True; |
| 176 | |
| 177 | AS::Registry &boca = AS::Registry::Get(); |
| 178 | AS::DeviceInfoComponent *deviceInfo = boca.CreateDeviceInfoComponent(); |
| 179 | |
| 180 | if (deviceInfo != NIL) |
| 181 | { |
| 182 | Int driveNumber = config->GetIntValue(ConfigureAccurateRip::ConfigID, "DiscInsert", -1); |
| 183 | MCDI mcdi = deviceInfo->GetNthDeviceMCDI(driveNumber); |
| 184 | |
| 185 | boca.DeleteComponent(deviceInfo); |
| 186 | |
| 187 | String discID = Algorithm::CalculateDiscID(mcdi); |
| 188 | const DatabaseEntry &entry = Database::Query(config, discID); |
| 189 | |
| 190 | if (!entry.IsValid()) NonBlocking0<>(&NotifyMissingEntry).Call(); |
| 191 | } |
| 192 | |
| 193 | return True; |
| 194 | } |
| 195 | |
| 196 | Void BoCA::VerifierAccurateRip::NotifyMissingEntry() |
| 197 | { |
| 198 | Config *config = Config::Get(); |
| 199 | I18n *i18n = I18n::Get(); |
| 200 | |
| 201 | i18n->SetContext("Verifiers::AccurateRip::Messages"); |
| 202 | |
| 203 | Bool doNotDisplayAgain = False; |
| 204 | MessageDlg messageBox(i18n->TranslateString("The inserted disc could not be found in the AccurateRip database."), i18n->TranslateString("Warning"), Message::Buttons::Ok, Message::Icon::Warning, i18n->TranslateString("Do not display this warning again"), &doNotDisplayAgain); |
| 205 | |
| 206 | messageBox.ShowDialog(); |
| 207 | |
| 208 | config->SetIntValue(ConfigureAccurateRip::ConfigID, ConfigureAccurateRip::ConfigNotifyMissingEntryID, !doNotDisplayAgain); |
| 209 | } |
| 210 | |
| 211 | ConfigLayer *BoCA::VerifierAccurateRip::GetConfigurationLayer() |
| 212 | { |
| 213 | if (configLayer != NIL) return configLayer; |
| 214 | |
| 215 | AS::Registry &boca = AS::Registry::Get(); |
| 216 | AS::DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 217 | |
| 218 | if (info != NIL) |
| 219 | { |
| 220 | configLayer = new ConfigureAccurateRip(); |
| 221 | |
| 222 | boca.DeleteComponent(info); |
| 223 | } |
| 224 | |
| 225 | return configLayer; |
| 226 | } |