| 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 "writer.h" |
| 14 | #include "systeminfo.h" |
| 15 | #include "config.h" |
| 16 | |
| 17 | using namespace smooth::IO; |
| 18 | |
| 19 | Array<BoCA::ProtocolData> BoCA::ProtocolWriter::protocolData; |
| 20 | Int BoCA::ProtocolWriter::instanceCount = 0; |
| 21 | |
| 22 | BoCA::ProtocolData::ProtocolData(Int conversion, const Array<Track> *tracks) |
| 23 | { |
| 24 | preliminary = (conversion >= 0); |
| 25 | conversionID = conversion; |
| 26 | |
| 27 | fileStream = NIL; |
| 28 | copyStream = NIL; |
| 29 | |
| 30 | trackList = tracks; |
| 31 | } |
| 32 | |
| 33 | BoCA::ProtocolData::~ProtocolData() |
| 34 | { |
| 35 | /* Close stream handles. |
| 36 | */ |
| 37 | if (fileStream != NIL) delete fileStream; |
| 38 | if (copyStream != NIL) delete copyStream; |
| 39 | } |
| 40 | |
| 41 | BoCA::ProtocolWriter::ProtocolWriter() |
| 42 | { |
| 43 | if (Threads::Access::Increment(instanceCount) > 1) return; |
| 44 | |
| 45 | /* Connect slots. |
| 46 | */ |
| 47 | Protocol::onUpdateProtocolList.Connect(&ProtocolWriter::OnUpdateProtocolList, this); |
| 48 | Protocol::onUpdateProtocol.Connect(&ProtocolWriter::OnUpdateProtocol, this); |
| 49 | |
| 50 | Engine *engine = Engine::Get(); |
| 51 | |
| 52 | engine->onStartConversion.Connect(&ProtocolWriter::OnStartConversion, this); |
| 53 | engine->onSingleFileConversion.Connect(&ProtocolWriter::OnSingleFileConversion, this); |
| 54 | engine->onFinishConversion.Connect(&ProtocolWriter::OnFinishConversion, this); |
| 55 | engine->onCancelConversion.Connect(&ProtocolWriter::OnFinishConversion, this); |
| 56 | } |
| 57 | |
| 58 | BoCA::ProtocolWriter::~ProtocolWriter() |
| 59 | { |
| 60 | if (Threads::Access::Decrement(instanceCount) > 0) return; |
| 61 | |
| 62 | /* Disconnect slots. |
| 63 | */ |
| 64 | Protocol::onUpdateProtocolList.Disconnect(&ProtocolWriter::OnUpdateProtocolList, this); |
| 65 | Protocol::onUpdateProtocol.Disconnect(&ProtocolWriter::OnUpdateProtocol, this); |
| 66 | |
| 67 | Engine *engine = Engine::Get(); |
| 68 | |
| 69 | engine->onStartConversion.Disconnect(&ProtocolWriter::OnStartConversion, this); |
| 70 | engine->onSingleFileConversion.Disconnect(&ProtocolWriter::OnSingleFileConversion, this); |
| 71 | engine->onFinishConversion.Disconnect(&ProtocolWriter::OnFinishConversion, this); |
| 72 | engine->onCancelConversion.Disconnect(&ProtocolWriter::OnFinishConversion, this); |
| 73 | } |
| 74 | |
| 75 | Void BoCA::ProtocolWriter::OnUpdateProtocolList() |
| 76 | { |
| 77 | /* Check whether new protocol was added. |
| 78 | */ |
| 79 | Protocol *protocol = Protocol::Get().GetLast(); |
| 80 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 81 | |
| 82 | if (protocol == NIL || protocolData.Get(index).fileName != NIL) return; |
| 83 | |
| 84 | /* Add or update protocol data. |
| 85 | */ |
| 86 | if (protocolData.Length() > 0 && protocolData.GetLast().preliminary) |
| 87 | { |
| 88 | ProtocolData data = protocolData.GetLast(); |
| 89 | |
| 90 | data.preliminary = False; |
| 91 | |
| 92 | protocolData.RemoveNth(protocolData.Length() - 1); |
| 93 | protocolData.Add(data, index); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | protocolData.Add(ProtocolData(), index); |
| 98 | } |
| 99 | |
| 100 | /* Format protocol header. |
| 101 | */ |
| 102 | FormatHeader(protocol); |
| 103 | |
| 104 | /* Save protocol. |
| 105 | */ |
| 106 | const Config *config = Config::Get(); |
| 107 | |
| 108 | if (config->GetIntValue(ConfigureProtocols::ConfigID, "ArchiveLogs", True)) SaveProtocol(protocol, GetProtocolFileName(protocol)); |
| 109 | |
| 110 | /* Check whether to save an additional copy |
| 111 | * of conversion logs with the audio files. |
| 112 | */ |
| 113 | ProtocolData &data = protocolData.GetReference(index); |
| 114 | |
| 115 | if (config->GetIntValue(ConfigureProtocols::ConfigID, "SaveConversionLogsWithFiles", False) && data.trackList != NIL) |
| 116 | { |
| 117 | Bool createAdditionalCopy = True; |
| 118 | |
| 119 | /* Check whether it's a CD ripping job. |
| 120 | */ |
| 121 | if (config->GetIntValue(ConfigureProtocols::ConfigID, "SaveRippingLogsOnly", True)) |
| 122 | { |
| 123 | createAdditionalCopy = False; |
| 124 | |
| 125 | foreach (const Track &track, *data.trackList) |
| 126 | { |
| 127 | if (track.isCDTrack) { createAdditionalCopy = True; break; } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Save additional log copy. |
| 132 | */ |
| 133 | if (createAdditionalCopy) SaveProtocol(protocol, GetAdditionalCopyName(protocol)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | Void BoCA::ProtocolWriter::OnUpdateProtocol(const String &name) |
| 138 | { |
| 139 | /* Check if there is a file stream associated with this protocol. |
| 140 | */ |
| 141 | Protocol *protocol = Protocol::Get(name); |
| 142 | |
| 143 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 144 | ProtocolData &data = protocolData.GetReference(index); |
| 145 | |
| 146 | if (data.fileStream == NIL && data.copyStream == NIL) return; |
| 147 | |
| 148 | /* Get new message and output it. |
| 149 | */ |
| 150 | String message = protocol->GetMessages().GetLast(); |
| 151 | |
| 152 | if (data.fileStream != NIL) OutputMessage(data.fileStream, message); |
| 153 | if (data.copyStream != NIL) OutputMessage(data.copyStream, message); |
| 154 | } |
| 155 | |
| 156 | Void BoCA::ProtocolWriter::OnStartConversion(Int conversionID, const Array<Track> &tracks) |
| 157 | { |
| 158 | ProtocolData data(conversionID, &tracks); |
| 159 | |
| 160 | protocolData.Add(data); |
| 161 | } |
| 162 | |
| 163 | Void BoCA::ProtocolWriter::OnSingleFileConversion(Int conversionID, const String &fileName) |
| 164 | { |
| 165 | foreach (ProtocolData &data, protocolData) |
| 166 | { |
| 167 | if (data.conversionID != conversionID) continue; |
| 168 | |
| 169 | data.singleFileName = fileName; |
| 170 | |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | Void BoCA::ProtocolWriter::OnFinishConversion(Int conversionID) |
| 176 | { |
| 177 | foreach (ProtocolData &data, protocolData) |
| 178 | { |
| 179 | if (data.conversionID != conversionID) continue; |
| 180 | |
| 181 | if (data.fileStream != NIL) { delete data.fileStream; data.fileStream = NIL; } |
| 182 | if (data.copyStream != NIL) { delete data.copyStream; data.copyStream = NIL; } |
| 183 | |
| 184 | data.trackList = NIL; |
| 185 | |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | String BoCA::ProtocolWriter::GetDisplayName(const Protocol *protocol) |
| 191 | { |
| 192 | String protocolName = protocol->GetName(); |
| 193 | |
| 194 | if (protocolName.StartsWith("#")) protocolName = protocolName.Tail(protocolName.Length() - protocolName.Find(" ") - 1); |
| 195 | |
| 196 | return protocolName; |
| 197 | } |
| 198 | |
| 199 | String BoCA::ProtocolWriter::FormatHeader(const Protocol *protocol) |
| 200 | { |
| 201 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 202 | ProtocolData &data = protocolData.GetReference(index); |
| 203 | |
| 204 | if (data.fileHeader != NIL) return data.fileHeader; |
| 205 | |
| 206 | /* Format new file header. |
| 207 | */ |
| 208 | const String &operatingSystem = SystemInfo::GetOperatingSystem(); |
| 209 | const String &architecture = SystemInfo::GetArchitecture(); |
| 210 | const String &cpuModel = SystemInfo::GetCPUModel(); |
| 211 | const String &installedRAM = SystemInfo::GetInstalledRAM(); |
| 212 | const String &newLine = SystemInfo::GetNewLineCharacters(); |
| 213 | |
| 214 | Application *app = Application::Get(); |
| 215 | DateTime date = DateTime::Current(); |
| 216 | |
| 217 | data.fileHeader.Append(GetDisplayName(protocol)).Append(newLine) |
| 218 | .Append(newLine) |
| 219 | .Append("Client: ").Append(app->getScreenName.Call()).Append(newLine) |
| 220 | .Append("Version: ").Append(app->getClientVersion.Call()).Append(" (").Append(architecture).Append(")").Append(newLine).Append(newLine) |
| 221 | .Append("System: ").Append(operatingSystem).Append(newLine) |
| 222 | .Append("CPU: ").Append(cpuModel).Append(newLine) |
| 223 | .Append("RAM: ").Append(installedRAM).Append(newLine).Append(newLine) |
| 224 | .Append("Date: ").Append(String::FromInt(date.GetYear())).Append("-") |
| 225 | .Append(date.GetMonth() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMonth())).Append("-") |
| 226 | .Append(date.GetDay() < 10 ? "0" : NIL).Append(String::FromInt(date.GetDay())).Append(newLine) |
| 227 | .Append("Time: ").Append(date.GetHour() < 10 ? "0" : NIL).Append(String::FromInt(date.GetHour())).Append(":") |
| 228 | .Append(date.GetMinute() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMinute())).Append(":") |
| 229 | .Append(date.GetSecond() < 10 ? "0" : NIL).Append(String::FromInt(date.GetSecond())).Append(newLine) |
| 230 | .Append(newLine) |
| 231 | .Append("hh:mm:ss.fff").Append(newLine) |
| 232 | .Append("------------").Append(newLine); |
| 233 | |
| 234 | return data.fileHeader; |
| 235 | } |
| 236 | |
| 237 | String BoCA::ProtocolWriter::GetProtocolFileName(const Protocol *protocol) |
| 238 | { |
| 239 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 240 | ProtocolData &data = protocolData.GetReference(index); |
| 241 | |
| 242 | if (data.fileName != NIL) return data.fileName; |
| 243 | |
| 244 | const Config *config = Config::Get(); |
| 245 | |
| 246 | /* Find file name. |
| 247 | */ |
| 248 | DateTime date = DateTime::Current(); |
| 249 | String logsFolder = Utilities::GetAbsolutePathName(config->GetStringValue(ConfigureProtocols::ConfigID, "LogsFolder", String(config->cacheDir).Append("logs"))); |
| 250 | |
| 251 | if (!logsFolder.EndsWith(Directory::GetDirectoryDelimiter())) logsFolder.Append(Directory::GetDirectoryDelimiter()); |
| 252 | |
| 253 | data.fileName = String(logsFolder).Append("[<date> <time>] <logname>.log"); |
| 254 | |
| 255 | data.fileName.Replace("<date>", String::FromInt(date.GetYear()).Append(date.GetMonth() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMonth())).Append(date.GetDay() < 10 ? "0" : NIL).Append(String::FromInt(date.GetDay()))); |
| 256 | data.fileName.Replace("<time>", String(date.GetHour() < 10 ? "0" : NIL).Append(String::FromInt(date.GetHour())).Append(date.GetMinute() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMinute())).Append(date.GetSecond() < 10 ? "0" : NIL).Append(String::FromInt(date.GetSecond()))); |
| 257 | data.fileName.Replace("<logname>", Utilities::ReplaceIncompatibleCharacters(GetDisplayName(protocol))); |
| 258 | |
| 259 | data.fileName = Utilities::NormalizeFileName(data.fileName); |
| 260 | data.fileStream = new OutStream(STREAM_FILE, Utilities::CreateDirectoryForFile(data.fileName), OS_REPLACE); |
| 261 | |
| 262 | return data.fileName; |
| 263 | } |
| 264 | |
| 265 | String BoCA::ProtocolWriter::GetAdditionalCopyName(const Protocol *protocol) |
| 266 | { |
| 267 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 268 | ProtocolData &data = protocolData.GetReference(index); |
| 269 | |
| 270 | if (data.copyName != NIL) return data.copyName; |
| 271 | |
| 272 | const Config *config = Config::Get(); |
| 273 | |
| 274 | /* Find folder for additional copy. |
| 275 | */ |
| 276 | const Track &firstTrack = data.trackList->GetFirst(); |
| 277 | String inputFolder = File(firstTrack.fileName).GetFilePath(); |
| 278 | String outputFolder = Utilities::GetAbsolutePathName(config->GetStringValue("Settings", "EncoderOutDir", NIL)); |
| 279 | Bool writeToInputDir = config->GetIntValue("Settings", "WriteToInputDirectory", False); |
| 280 | |
| 281 | if (writeToInputDir && !firstTrack.isCDTrack && Utilities::IsFolderWritable(inputFolder)) outputFolder = inputFolder; |
| 282 | |
| 283 | if (!outputFolder.EndsWith(Directory::GetDirectoryDelimiter())) outputFolder.Append(Directory::GetDirectoryDelimiter()); |
| 284 | |
| 285 | /* Find additional copy file name. |
| 286 | */ |
| 287 | String defaultPattern = String("<albumartist> - <album>").Append(Directory::GetDirectoryDelimiter()).Append("<albumartist> - <album>"); |
| 288 | |
| 289 | data.copyName = config->GetStringValue(ConfigureProtocols::ConfigID, "ConversionLogPattern", defaultPattern).Append(".log"); |
| 290 | |
| 291 | if (data.singleFileName != NIL) |
| 292 | { |
| 293 | data.copyName = data.singleFileName.Head(data.singleFileName.FindLast(".")).Append(".log"); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | if (data.copyName != NIL) |
| 298 | { |
| 299 | I18n *i18n = I18n::Get(); |
| 300 | |
| 301 | if (data.copyName.Trim() == NIL) data.copyName = defaultPattern; |
| 302 | |
| 303 | const Info &info = firstTrack.GetInfo(); |
| 304 | DateTime date = DateTime::Current(); |
| 305 | |
| 306 | data.copyName.Replace("<artist>", Utilities::ReplaceIncompatibleCharacters(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist"))); |
| 307 | data.copyName.Replace("<album>", Utilities::ReplaceIncompatibleCharacters(info.album.Length() > 0 ? info.album : i18n->TranslateString("unknown album"))); |
| 308 | data.copyName.Replace("<genre>", Utilities::ReplaceIncompatibleCharacters(info.genre.Length() > 0 ? info.genre : i18n->TranslateString("unknown genre"))); |
| 309 | data.copyName.Replace("<year>", Utilities::ReplaceIncompatibleCharacters(info.year > 0 ? String::FromInt(info.year) : i18n->TranslateString("unknown year"))); |
| 310 | |
| 311 | data.copyName.Replace("<date>", String::FromInt(date.GetYear()).Append(date.GetMonth() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMonth())).Append(date.GetDay() < 10 ? "0" : NIL).Append(String::FromInt(date.GetDay()))); |
| 312 | data.copyName.Replace("<time>", String(date.GetHour() < 10 ? "0" : NIL).Append(String::FromInt(date.GetHour())).Append(date.GetMinute() < 10 ? "0" : NIL).Append(String::FromInt(date.GetMinute())).Append(date.GetSecond() < 10 ? "0" : NIL).Append(String::FromInt(date.GetSecond()))); |
| 313 | data.copyName.Replace("<logname>", Utilities::ReplaceIncompatibleCharacters(GetDisplayName(protocol))); |
| 314 | |
| 315 | /* Replace <disc> pattern. |
| 316 | */ |
| 317 | data.copyName.Replace("<disc>", String::FromInt(info.disc < 0 ? 0 : info.disc)); |
| 318 | |
| 319 | for (Int i = 1; i <= 4; i++) |
| 320 | { |
| 321 | String pattern = String("<disc(").Append(String::FromInt(i)).Append(")>"); |
| 322 | |
| 323 | data.copyName.Replace(pattern, String().FillN('0', i - ((Int) Math::Log10(info.disc > 0 ? info.disc : 1) + 1)).Append(String::FromInt(info.disc < 0 ? 0 : info.disc))); |
| 324 | } |
| 325 | |
| 326 | /* Replace other text fields. |
| 327 | */ |
| 328 | foreach (const String &pair, info.other) |
| 329 | { |
| 330 | String key = pair.Head(pair.Find(":")); |
| 331 | String value = pair.Tail(pair.Length() - pair.Find(":") - 1); |
| 332 | |
| 333 | if (value == NIL) continue; |
| 334 | |
| 335 | if (key == INFO_ALBUMARTIST) data.copyName.Replace("<albumartist>", Utilities::ReplaceIncompatibleCharacters(value)); |
| 336 | else if (key == INFO_CONDUCTOR) data.copyName.Replace("<conductor>", Utilities::ReplaceIncompatibleCharacters(value)); |
| 337 | else if (key == INFO_COMPOSER) data.copyName.Replace("<composer>", Utilities::ReplaceIncompatibleCharacters(value)); |
| 338 | } |
| 339 | |
| 340 | if (info.artist.Length() > 0) data.copyName.Replace("<albumartist>", Utilities::ReplaceIncompatibleCharacters(info.artist)); |
| 341 | |
| 342 | data.copyName.Replace("<albumartist>", Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown album artist"))); |
| 343 | data.copyName.Replace("<conductor>", Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown conductor"))); |
| 344 | data.copyName.Replace("<composer>", Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown composer"))); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | data.copyName = GetDisplayName(protocol); |
| 349 | } |
| 350 | |
| 351 | Bool useUnicode = config->GetIntValue("Settings", "UseUnicodeFilenames", True); |
| 352 | Bool replaceSpaces = config->GetIntValue("Settings", "FilenamesReplaceSpaces", False); |
| 353 | |
| 354 | data.copyName = String(outputFolder).Append(Utilities::ReplaceIncompatibleCharacters(data.copyName, useUnicode, False, replaceSpaces)); |
| 355 | } |
| 356 | |
| 357 | data.copyName = Utilities::NormalizeFileName(data.copyName); |
| 358 | data.copyStream = new OutStream(STREAM_FILE, Utilities::CreateDirectoryForFile(data.copyName), OS_REPLACE); |
| 359 | |
| 360 | return data.copyName; |
| 361 | } |
| 362 | |
| 363 | Void BoCA::ProtocolWriter::SaveProtocol(const Protocol *protocol, const String &fileName) |
| 364 | { |
| 365 | /* Check whether to use an existing stream. |
| 366 | */ |
| 367 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 368 | ProtocolData &data = protocolData.GetReference(index); |
| 369 | |
| 370 | if (fileName == data.fileName) SaveProtocol(protocol, data.fileStream); |
| 371 | else if (fileName == data.copyName) SaveProtocol(protocol, data.copyStream); |
| 372 | |
| 373 | /* Save log to a new file. |
| 374 | */ |
| 375 | if (fileName != data.fileName && fileName != data.copyName) |
| 376 | { |
| 377 | OutStream fileStream(STREAM_FILE, Utilities::CreateDirectoryForFile(fileName), OS_REPLACE); |
| 378 | |
| 379 | SaveProtocol(protocol, &fileStream); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | Void BoCA::ProtocolWriter::SaveProtocol(const Protocol *protocol, OutStream *out) |
| 384 | { |
| 385 | /* Write log header. |
| 386 | */ |
| 387 | String::OutputFormat outFormat("UTF-8"); |
| 388 | |
| 389 | out->OutputString(FormatHeader(protocol)); |
| 390 | |
| 391 | /* Write log messages. |
| 392 | */ |
| 393 | const Array<String> &messages = protocol->GetMessages(); |
| 394 | |
| 395 | foreach (const String &message, messages) |
| 396 | { |
| 397 | out->OutputLine(message.SubString(0, 12).Append(" ").Append(message.Tail(message.Length() - 15))); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | Void BoCA::ProtocolWriter::OutputMessage(OutStream *out, const String &message) |
| 402 | { |
| 403 | /* Open file and output message. |
| 404 | */ |
| 405 | String::OutputFormat outFormat("UTF-8"); |
| 406 | |
| 407 | out->OutputLine(message.SubString(0, 12).Append(" ").Append(message.Tail(message.Length() - 15))); |
| 408 | out->Flush(); |
| 409 | } |