| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2022 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 <utilities.h> |
| 14 | #include <config.h> |
| 15 | |
| 16 | #include <cddb/cddb.h> |
| 17 | |
| 18 | #ifdef __WIN32__ |
| 19 | # include <windows.h> |
| 20 | #endif |
| 21 | |
| 22 | using namespace BoCA; |
| 23 | using namespace BoCA::AS; |
| 24 | |
| 25 | using namespace smooth::GUI::Dialogs; |
| 26 | using namespace smooth::IO; |
| 27 | |
| 28 | static const String kDot = "."; |
| 29 | static const String kColon = ":"; |
| 30 | |
| 31 | static const String kArtistPattern = "<artist>"; |
| 32 | static const String kTitlePattern = "<title>"; |
| 33 | static const String kAlbumPattern = "<album>"; |
| 34 | static const String kGenrePattern = "<genre>"; |
| 35 | static const String kYearPattern = "<year>"; |
| 36 | static const String kDiscIDPattern = "<discid>"; |
| 37 | static const String kFileNamePattern = "<filename>"; |
| 38 | static const String kFileTypePattern = "<filetype>"; |
| 39 | static const String kDirectoryPattern = "<directory>"; |
| 40 | static const String kDirectoryExtStart = "<directory+"; |
| 41 | static const String kCurrentDatePattern = "<currentdate>"; |
| 42 | static const String kCurrentTimePattern = "<currenttime>"; |
| 43 | static const String kTrackPattern = "<track>"; |
| 44 | static const String kTrackExtStart = "<track("; |
| 45 | static const String kDiscPattern = "<disc>"; |
| 46 | static const String kDiscExtStart = "<disc("; |
| 47 | static const String kAlbumArtistPattern = "<albumartist>"; |
| 48 | static const String kConductorPattern = "<conductor>"; |
| 49 | static const String kComposerPattern = "<composer>"; |
| 50 | static const String kTempoPattern = "<tempo>"; |
| 51 | static const String kInitialKeyPattern = "<initialkey>"; |
| 52 | |
| 53 | Void freac::Utilities::FillGenreList(List *list) |
| 54 | { |
| 55 | BoCA::Config *config = BoCA::Config::Get(); |
| 56 | |
| 57 | list->AddEntry(NIL); |
| 58 | list->AddSeparator(); |
| 59 | |
| 60 | Int customEntries = 0; |
| 61 | |
| 62 | for (Int i = 1; i <= 5; i++) |
| 63 | { |
| 64 | if (config->GetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(i)), NIL) != NIL) |
| 65 | { |
| 66 | list->AddEntry(config->GetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(i)), NIL)); |
| 67 | |
| 68 | customEntries++; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (customEntries > 0) list->AddSeparator(); |
| 73 | |
| 74 | list->AddEntry("A Cappella"); |
| 75 | list->AddEntry("Abstract"); |
| 76 | list->AddEntry("Acid"); |
| 77 | list->AddEntry("Acid Jazz"); |
| 78 | list->AddEntry("Acid Punk"); |
| 79 | list->AddEntry("Acoustic"); |
| 80 | list->AddEntry("Alt. Rock"); |
| 81 | list->AddEntry("Alternative"); |
| 82 | list->AddEntry("Ambient"); |
| 83 | list->AddEntry("Anime"); |
| 84 | list->AddEntry("Art Rock"); |
| 85 | list->AddEntry("Audio Theatre"); |
| 86 | list->AddEntry("Audiobook"); |
| 87 | list->AddEntry("Avantgarde"); |
| 88 | list->AddEntry("Ballad"); |
| 89 | list->AddEntry("Baroque"); |
| 90 | list->AddEntry("Bass"); |
| 91 | list->AddEntry("Beat"); |
| 92 | list->AddEntry("Bebob"); |
| 93 | list->AddEntry("Bhangra"); |
| 94 | list->AddEntry("Big Band"); |
| 95 | list->AddEntry("Big Beat"); |
| 96 | list->AddEntry("Black Metal"); |
| 97 | list->AddEntry("Bluegrass"); |
| 98 | list->AddEntry("Blues"); |
| 99 | list->AddEntry("Booty Bass"); |
| 100 | list->AddEntry("Breakbeat"); |
| 101 | list->AddEntry("BritPop"); |
| 102 | list->AddEntry("Cabaret"); |
| 103 | list->AddEntry("Celtic"); |
| 104 | list->AddEntry("Chamber Music"); |
| 105 | list->AddEntry("Chanson"); |
| 106 | list->AddEntry("Chillout"); |
| 107 | list->AddEntry("Chorus"); |
| 108 | list->AddEntry("Christian Gangsta Rap"); |
| 109 | list->AddEntry("Christian Rap"); |
| 110 | list->AddEntry("Christian Rock"); |
| 111 | list->AddEntry("Classic Rock"); |
| 112 | list->AddEntry("Classical"); |
| 113 | list->AddEntry("Club"); |
| 114 | list->AddEntry("Club-House"); |
| 115 | list->AddEntry("Comedy"); |
| 116 | list->AddEntry("Contemporary Christian"); |
| 117 | list->AddEntry("Country"); |
| 118 | list->AddEntry("Cover"); |
| 119 | list->AddEntry("Crossover"); |
| 120 | list->AddEntry("Cult"); |
| 121 | list->AddEntry("Dance"); |
| 122 | list->AddEntry("Dance Hall"); |
| 123 | list->AddEntry("Darkwave"); |
| 124 | list->AddEntry("Death Metal"); |
| 125 | list->AddEntry("Disco"); |
| 126 | list->AddEntry("Downtempo"); |
| 127 | list->AddEntry("Dream"); |
| 128 | list->AddEntry("Drum & Bass"); |
| 129 | list->AddEntry("Drum Solo"); |
| 130 | list->AddEntry("Dub"); |
| 131 | list->AddEntry("Dubstep"); |
| 132 | list->AddEntry("Duet"); |
| 133 | list->AddEntry("Easy Listening"); |
| 134 | list->AddEntry("EBM"); |
| 135 | list->AddEntry("Eclectic"); |
| 136 | list->AddEntry("Electro"); |
| 137 | list->AddEntry("Electroclash"); |
| 138 | list->AddEntry("Electronic"); |
| 139 | list->AddEntry("Emo"); |
| 140 | list->AddEntry("Ethnic"); |
| 141 | list->AddEntry("Eurodance"); |
| 142 | list->AddEntry("Euro-House"); |
| 143 | list->AddEntry("Euro-Techno"); |
| 144 | list->AddEntry("Experimental"); |
| 145 | list->AddEntry("Fast-Fusion"); |
| 146 | list->AddEntry("Folk"); |
| 147 | list->AddEntry("Folk/Rock"); |
| 148 | list->AddEntry("Folklore"); |
| 149 | list->AddEntry("Freestyle"); |
| 150 | list->AddEntry("Funk"); |
| 151 | list->AddEntry("Fusion"); |
| 152 | list->AddEntry("G-Funk"); |
| 153 | list->AddEntry("Game"); |
| 154 | list->AddEntry("Gangsta Rap"); |
| 155 | list->AddEntry("Garage"); |
| 156 | list->AddEntry("Garage Rock"); |
| 157 | list->AddEntry("Global"); |
| 158 | list->AddEntry("Goa"); |
| 159 | list->AddEntry("Gospel"); |
| 160 | list->AddEntry("Gothic"); |
| 161 | list->AddEntry("Gothic Rock"); |
| 162 | list->AddEntry("Grunge"); |
| 163 | list->AddEntry("Hard Rock"); |
| 164 | list->AddEntry("Hardcore"); |
| 165 | list->AddEntry("Heavy Metal"); |
| 166 | list->AddEntry("Hip-Hop"); |
| 167 | list->AddEntry("House"); |
| 168 | list->AddEntry("Humour"); |
| 169 | list->AddEntry("IDM"); |
| 170 | list->AddEntry("Illbient"); |
| 171 | list->AddEntry("Indie"); |
| 172 | list->AddEntry("Indie Rock"); |
| 173 | list->AddEntry("Industrial"); |
| 174 | list->AddEntry("Industro-Goth"); |
| 175 | list->AddEntry("Instrumental"); |
| 176 | list->AddEntry("Instrumental Pop"); |
| 177 | list->AddEntry("Instrumental Rock"); |
| 178 | list->AddEntry("Jam Band"); |
| 179 | list->AddEntry("Jazz"); |
| 180 | list->AddEntry("Jazz+Funk"); |
| 181 | list->AddEntry("JPop"); |
| 182 | list->AddEntry("Jungle"); |
| 183 | list->AddEntry("Krautrock"); |
| 184 | list->AddEntry("Latin"); |
| 185 | list->AddEntry("Leftfield"); |
| 186 | list->AddEntry("Lo-Fi"); |
| 187 | list->AddEntry("Lounge"); |
| 188 | list->AddEntry("Math Rock"); |
| 189 | list->AddEntry("Meditative"); |
| 190 | list->AddEntry("Merengue"); |
| 191 | list->AddEntry("Metal"); |
| 192 | list->AddEntry("Musical"); |
| 193 | list->AddEntry("National Folk"); |
| 194 | list->AddEntry("Native American"); |
| 195 | list->AddEntry("Negerpunk"); |
| 196 | list->AddEntry("Neoclassical"); |
| 197 | list->AddEntry("Neue Deutsche Welle"); |
| 198 | list->AddEntry("New Age"); |
| 199 | list->AddEntry("New Romantic"); |
| 200 | list->AddEntry("New Wave"); |
| 201 | list->AddEntry("Noise"); |
| 202 | list->AddEntry("Nu-Breakz"); |
| 203 | list->AddEntry("Oldies"); |
| 204 | list->AddEntry("Opera"); |
| 205 | list->AddEntry("Other"); |
| 206 | list->AddEntry("Podcast"); |
| 207 | list->AddEntry("Polka"); |
| 208 | list->AddEntry("Polsk Punk"); |
| 209 | list->AddEntry("Pop"); |
| 210 | list->AddEntry("Pop/Funk"); |
| 211 | list->AddEntry("Pop-Folk"); |
| 212 | list->AddEntry("Porn Groove"); |
| 213 | list->AddEntry("Post-Punk"); |
| 214 | list->AddEntry("Post-Rock"); |
| 215 | list->AddEntry("Power Ballad"); |
| 216 | list->AddEntry("Pranks"); |
| 217 | list->AddEntry("Primus"); |
| 218 | list->AddEntry("Progressive Rock"); |
| 219 | list->AddEntry("Psybient"); |
| 220 | list->AddEntry("Psychedelic"); |
| 221 | list->AddEntry("Psychedelic Rock"); |
| 222 | list->AddEntry("Psytrance"); |
| 223 | list->AddEntry("Punk"); |
| 224 | list->AddEntry("Punk Rock"); |
| 225 | list->AddEntry("R&B"); |
| 226 | list->AddEntry("Rap"); |
| 227 | list->AddEntry("Rave"); |
| 228 | list->AddEntry("Reggae"); |
| 229 | list->AddEntry("Remix"); |
| 230 | list->AddEntry("Retro"); |
| 231 | list->AddEntry("Revival"); |
| 232 | list->AddEntry("Rhythmic Soul"); |
| 233 | list->AddEntry("Rock"); |
| 234 | list->AddEntry("Rock & Roll"); |
| 235 | list->AddEntry("Salsa"); |
| 236 | list->AddEntry("Samba"); |
| 237 | list->AddEntry("Satire"); |
| 238 | list->AddEntry("Shoegaze"); |
| 239 | list->AddEntry("Showtunes"); |
| 240 | list->AddEntry("Ska"); |
| 241 | list->AddEntry("Slow Jam"); |
| 242 | list->AddEntry("Slow Rock"); |
| 243 | list->AddEntry("Sonata"); |
| 244 | list->AddEntry("Soul"); |
| 245 | list->AddEntry("Sound Clip"); |
| 246 | list->AddEntry("Soundtrack"); |
| 247 | list->AddEntry("Southern Rock"); |
| 248 | list->AddEntry("Space"); |
| 249 | list->AddEntry("Space Rock"); |
| 250 | list->AddEntry("Speech"); |
| 251 | list->AddEntry("Swing"); |
| 252 | list->AddEntry("Symphonic Rock"); |
| 253 | list->AddEntry("Symphony"); |
| 254 | list->AddEntry("Synthpop"); |
| 255 | list->AddEntry("Tango"); |
| 256 | list->AddEntry("Techno"); |
| 257 | list->AddEntry("Techno-Industrial"); |
| 258 | list->AddEntry("Terror"); |
| 259 | list->AddEntry("Thrash-Metal"); |
| 260 | list->AddEntry("Top 40"); |
| 261 | list->AddEntry("Trailer"); |
| 262 | list->AddEntry("Trance"); |
| 263 | list->AddEntry("Tribal"); |
| 264 | list->AddEntry("Trip-Hop"); |
| 265 | list->AddEntry("Trop Rock"); |
| 266 | list->AddEntry("Vocal"); |
| 267 | list->AddEntry("World Music"); |
| 268 | } |
| 269 | |
| 270 | Void freac::Utilities::UpdateGenreList(List *list, const String &genre) |
| 271 | { |
| 272 | if (genre == NIL) return; |
| 273 | |
| 274 | BoCA::Config *config = BoCA::Config::Get(); |
| 275 | |
| 276 | Int number = 5; |
| 277 | |
| 278 | for (Int i = 1; i <= 5; i++) |
| 279 | { |
| 280 | if (config->GetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(i)), NIL) == genre) |
| 281 | { |
| 282 | number = i; |
| 283 | |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | for (Int i = number; i > 1; i--) |
| 289 | { |
| 290 | config->SetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(i)), config->GetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(i - 1)), NIL)); |
| 291 | } |
| 292 | |
| 293 | config->SetStringValue(Config::CategorySettingsID, String(Config::SettingsLastUsedGenreID).Append(String::FromInt(1)), genre); |
| 294 | |
| 295 | list->RemoveAllEntries(); |
| 296 | |
| 297 | FillGenreList(list); |
| 298 | } |
| 299 | |
| 300 | String freac::Utilities::GetOutputFileName(const BoCA::Config *config, const Track &track) |
| 301 | { |
| 302 | String filePattern = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, Config::SettingsEncoderFilenamePatternDefault); |
| 303 | String outputDirectory = BoCA::Utilities::GetAbsolutePathName(config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault)); |
| 304 | Bool writeToInputDir = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, Config::SettingsWriteToInputDirectoryDefault); |
| 305 | String encoderID = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 306 | Bool useUnicode = config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesAllowUnicodeID, Config::SettingsFilenamesAllowUnicodeDefault); |
| 307 | Bool replaceSpaces = config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesReplaceSpacesID, Config::SettingsFilenamesReplaceSpacesDefault); |
| 308 | Bool useFallback = config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesUseFallbackID, Config::SettingsFilenamesUseFallbackDefault); |
| 309 | |
| 310 | if (filePattern.Trim() == NIL) filePattern = Config::SettingsEncoderFilenamePatternDefault; |
| 311 | |
| 312 | return GetOutputFileName(track, filePattern, outputDirectory, writeToInputDir, encoderID, True, useUnicode, replaceSpaces, useFallback); |
| 313 | } |
| 314 | |
| 315 | String freac::Utilities::GetOutputFileName(const Track &track, const String &filePattern, const String &outputDirectory, Bool writeToInputDir, const String &encoderID, Bool normalizeFileName, Bool useUnicode, Bool replaceSpaces, Bool useFallback) |
| 316 | { |
| 317 | if (track.outputFile != NIL) return track.outputFile; |
| 318 | |
| 319 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 320 | |
| 321 | const Info &info = track.GetInfo(); |
| 322 | |
| 323 | /* Check if input file folder is writable. |
| 324 | */ |
| 325 | String inFileDirectory = track.isCDTrack ? String() : File(track.fileName).GetFilePath(); |
| 326 | |
| 327 | if (writeToInputDir && (track.isCDTrack || !BoCA::Utilities::IsFolderWritable(inFileDirectory))) writeToInputDir = False; |
| 328 | |
| 329 | /* Construct output filename. |
| 330 | */ |
| 331 | String outputFileName; |
| 332 | |
| 333 | if (writeToInputDir) outputFileName.Copy(inFileDirectory).Append(Directory::GetDirectoryDelimiter()); |
| 334 | else outputFileName.Copy(outputDirectory); |
| 335 | |
| 336 | /* Get file extension from selected encoder component |
| 337 | * caching the result for requests in quick succession. |
| 338 | */ |
| 339 | static String fileExtension = NIL; |
| 340 | static String selectedEncoder = NIL; |
| 341 | static UnsignedInt64 lastRequest = 0; |
| 342 | |
| 343 | if (selectedEncoder != encoderID || S::System::System::Clock() - lastRequest >= 750) |
| 344 | { |
| 345 | Registry &boca = Registry::Get(); |
| 346 | EncoderComponent *encoder = (EncoderComponent *) boca.CreateComponentByID(encoderID); |
| 347 | |
| 348 | fileExtension = (encoder != NIL) ? encoder->GetOutputFileExtension() : "audio"; |
| 349 | selectedEncoder = encoderID; |
| 350 | |
| 351 | boca.DeleteComponent(encoder); |
| 352 | } |
| 353 | |
| 354 | lastRequest = S::System::System::Clock(); |
| 355 | |
| 356 | /* Check available metadata. |
| 357 | */ |
| 358 | Bool metadataAvailable = False; |
| 359 | |
| 360 | if ((info.title != NIL && filePattern.Contains(kTitlePattern) ) || |
| 361 | (info.album != NIL && filePattern.Contains(kAlbumPattern) ) || |
| 362 | (info.artist != NIL && (filePattern.Contains(kArtistPattern) || filePattern.Contains(kAlbumArtistPattern))) || |
| 363 | (info.track > 0 && (filePattern.Contains(kTrackPattern) || filePattern.Contains(kTrackExtStart) ))) metadataAvailable = True; |
| 364 | |
| 365 | /* Replace patterns. |
| 366 | */ |
| 367 | if (metadataAvailable || !useFallback || filePattern.Contains(kFileNamePattern) || filePattern.Contains(kCurrentDatePattern) || filePattern.Contains(kCurrentTimePattern)) |
| 368 | { |
| 369 | String shortOutFileName = filePattern; |
| 370 | |
| 371 | if (shortOutFileName.Contains(kArtistPattern)) shortOutFileName.Replace(kArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist"))); |
| 372 | if (shortOutFileName.Contains(kTitlePattern)) shortOutFileName.Replace(kTitlePattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.title.Length() > 0 ? info.title : i18n->TranslateString("unknown title"))); |
| 373 | if (shortOutFileName.Contains(kAlbumPattern)) shortOutFileName.Replace(kAlbumPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.album.Length() > 0 ? info.album : i18n->TranslateString("unknown album"))); |
| 374 | if (shortOutFileName.Contains(kGenrePattern)) shortOutFileName.Replace(kGenrePattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.genre.Length() > 0 ? info.genre : i18n->TranslateString("unknown genre"))); |
| 375 | if (shortOutFileName.Contains(kYearPattern)) shortOutFileName.Replace(kYearPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.year > 0 ? String::FromInt(info.year) : i18n->TranslateString("unknown year"))); |
| 376 | if (shortOutFileName.Contains(kDiscIDPattern)) shortOutFileName.Replace(kDiscIDPattern, CDDB::DiscIDToString(track.discid)); |
| 377 | if (shortOutFileName.Contains(kFileTypePattern)) shortOutFileName.Replace(kFileTypePattern, fileExtension.ToUpper()); |
| 378 | |
| 379 | /* Replace <track> pattern. |
| 380 | */ |
| 381 | if (shortOutFileName.Contains(kTrackPattern)) shortOutFileName.Replace(kTrackPattern, String(info.track < 10 ? "0" : NIL).Append(String::FromInt(info.track < 0 ? 0 : info.track))); |
| 382 | |
| 383 | for (Int i = 1; i <= 4 && shortOutFileName.Contains(kTrackExtStart); i++) |
| 384 | { |
| 385 | String pattern = String(kTrackExtStart).Append(String::FromInt(i)).Append(")>"); |
| 386 | |
| 387 | shortOutFileName.Replace(pattern, String().FillN('0', i - ((Int) Math::Log10(info.track > 0 ? info.track : 1) + 1)).Append(String::FromInt(info.track < 0 ? 0 : info.track))); |
| 388 | } |
| 389 | |
| 390 | /* Replace <disc> pattern. |
| 391 | */ |
| 392 | if (shortOutFileName.Contains(kDiscPattern)) shortOutFileName.Replace(kDiscPattern, String::FromInt(Math::Max(1, info.disc))); |
| 393 | |
| 394 | for (Int i = 1; i <= 4 && shortOutFileName.Contains(kDiscExtStart); i++) |
| 395 | { |
| 396 | String pattern = String(kDiscExtStart).Append(String::FromInt(i)).Append(")>"); |
| 397 | |
| 398 | shortOutFileName.Replace(pattern, String().FillN('0', i - ((Int) Math::Log10(info.disc > 0 ? info.disc : 1) + 1)).Append(String::FromInt(Math::Max(1, info.disc)))); |
| 399 | } |
| 400 | |
| 401 | /* Replace <currentdate> pattern. |
| 402 | */ |
| 403 | if (shortOutFileName.Contains(kCurrentDatePattern)) |
| 404 | { |
| 405 | DateTime currentDateTime = DateTime::Current(); |
| 406 | String currentDate = String().FillN('0', 3 - Math::Floor(Math::Log10(currentDateTime.GetYear() ))) .Append(String::FromInt(currentDateTime.GetYear())) |
| 407 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetMonth())))).Append(String::FromInt(currentDateTime.GetMonth())) |
| 408 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetDay() )))).Append(String::FromInt(currentDateTime.GetDay())); |
| 409 | |
| 410 | shortOutFileName.Replace(kCurrentDatePattern, currentDate); |
| 411 | } |
| 412 | |
| 413 | /* Replace <currenttime> pattern. |
| 414 | */ |
| 415 | if (shortOutFileName.Contains(kCurrentTimePattern)) |
| 416 | { |
| 417 | DateTime currentDateTime = DateTime::Current(); |
| 418 | String currentTime = String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetHour() > 0 ? currentDateTime.GetHour() : 1))) .Append(String::FromInt(currentDateTime.GetHour())) |
| 419 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetMinute() > 0 ? currentDateTime.GetMinute() : 1)))).Append(String::FromInt(currentDateTime.GetMinute())); |
| 420 | |
| 421 | shortOutFileName.Replace(kCurrentTimePattern, currentTime); |
| 422 | } |
| 423 | |
| 424 | /* Replace other text fields. |
| 425 | */ |
| 426 | foreach (const String &pair, info.other) |
| 427 | { |
| 428 | String key = pair.Head(pair.Find(kColon)); |
| 429 | String value = pair.Tail(pair.Length() - pair.Find(kColon) - 1); |
| 430 | |
| 431 | if (value == NIL) continue; |
| 432 | |
| 433 | if (key == INFO_ALBUMARTIST && shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 434 | else if (key == INFO_CONDUCTOR && shortOutFileName.Contains(kConductorPattern)) shortOutFileName.Replace(kConductorPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 435 | else if (key == INFO_COMPOSER && shortOutFileName.Contains(kComposerPattern)) shortOutFileName.Replace(kComposerPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 436 | else if (key == INFO_BPM && shortOutFileName.Contains(kTempoPattern)) shortOutFileName.Replace(kTempoPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 437 | else if (key == INFO_INITIALKEY && shortOutFileName.Contains(kInitialKeyPattern)) shortOutFileName.Replace(kInitialKeyPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 438 | } |
| 439 | |
| 440 | if (info.artist.Length() > 0 && shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.artist)); |
| 441 | |
| 442 | if (shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown album artist"))); |
| 443 | if (shortOutFileName.Contains(kConductorPattern)) shortOutFileName.Replace(kConductorPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown conductor"))); |
| 444 | if (shortOutFileName.Contains(kComposerPattern)) shortOutFileName.Replace(kComposerPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown composer"))); |
| 445 | if (shortOutFileName.Contains(kTempoPattern)) shortOutFileName.Replace(kTempoPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown tempo"))); |
| 446 | if (shortOutFileName.Contains(kInitialKeyPattern)) shortOutFileName.Replace(kInitialKeyPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown key"))); |
| 447 | |
| 448 | /* Replace <filename> pattern. |
| 449 | */ |
| 450 | if (shortOutFileName.Contains(kFileNamePattern)) |
| 451 | { |
| 452 | String shortInFileName = File(track.fileName).GetFileName(); |
| 453 | |
| 454 | if (shortInFileName.Find(kDot) > 0) shortInFileName = shortInFileName.Head(shortInFileName.FindLast(kDot)); |
| 455 | |
| 456 | shortOutFileName.Replace(kFileNamePattern, BoCA::Utilities::ReplaceIncompatibleCharacters(shortInFileName)); |
| 457 | } |
| 458 | |
| 459 | /* Replace <directory> pattern. |
| 460 | */ |
| 461 | String directory = inFileDirectory; |
| 462 | |
| 463 | if (directory[1] == ':') directory = directory.Tail(directory.Length() - 3); |
| 464 | else if (directory.StartsWith("\\\\")) directory = directory.Tail(directory.Length() - 2); |
| 465 | |
| 466 | if (shortOutFileName.Contains(kDirectoryPattern)) shortOutFileName.Replace(kDirectoryPattern, directory); |
| 467 | |
| 468 | for (Int i = 0; i < 10 && shortOutFileName.Contains(kDirectoryExtStart); i++) |
| 469 | { |
| 470 | String pattern = String(kDirectoryExtStart).Append(String::FromInt(i)).Append(">"); |
| 471 | |
| 472 | if (shortOutFileName.Contains(pattern)) |
| 473 | { |
| 474 | String value = directory; |
| 475 | |
| 476 | for (Int n = 0; n < i; n++) value = value.Tail(value.Length() - value.Find(Directory::GetDirectoryDelimiter()) - 1); |
| 477 | |
| 478 | shortOutFileName.Replace(pattern, value); |
| 479 | } |
| 480 | |
| 481 | for (Int j = 0; j < 10 && shortOutFileName.Contains(String(kDirectoryExtStart).Append(String::FromInt(i)).Append("(")); j++) |
| 482 | { |
| 483 | String pattern = String(kDirectoryExtStart).Append(String::FromInt(i)).Append("(").Append(String::FromInt(j + 1)).Append(")>"); |
| 484 | |
| 485 | if (!shortOutFileName.Contains(pattern)) continue; |
| 486 | |
| 487 | String value = directory; |
| 488 | |
| 489 | for (Int n = 0; n < i; n++) value = value.Tail(value.Length() - value.Find(Directory::GetDirectoryDelimiter()) - 1); |
| 490 | |
| 491 | Int bsCount = 0; |
| 492 | |
| 493 | for (Int n = 0; n < value.Length(); n++) |
| 494 | { |
| 495 | if (value[n] == '\\' || value[n] == '/') bsCount++; |
| 496 | |
| 497 | if (bsCount == j + 1) |
| 498 | { |
| 499 | value[n] = 0; |
| 500 | |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | shortOutFileName.Replace(pattern, value); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* Replace incompatible characters and limit length of path components. |
| 510 | */ |
| 511 | if (normalizeFileName) |
| 512 | { |
| 513 | outputFileName.Append(BoCA::Utilities::ReplaceIncompatibleCharacters(shortOutFileName, useUnicode, False, replaceSpaces)); |
| 514 | outputFileName = BoCA::Utilities::NormalizeFileName(outputFileName); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | outputFileName.Append(shortOutFileName); |
| 519 | } |
| 520 | } |
| 521 | else if (track.isCDTrack) |
| 522 | { |
| 523 | outputFileName.Append("cd").Append(String::FromInt(track.drive)).Append("track"); |
| 524 | |
| 525 | if (info.track < 10) outputFileName.Append("0"); |
| 526 | |
| 527 | outputFileName.Append(String::FromInt(info.track)); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | String shortInFileName = File(track.fileName).GetFileName(); |
| 532 | |
| 533 | if (shortInFileName.Find(kDot) > 0) shortInFileName = shortInFileName.Head(shortInFileName.FindLast(kDot)); |
| 534 | |
| 535 | outputFileName.Append(shortInFileName); |
| 536 | } |
| 537 | |
| 538 | /* Append file extension. |
| 539 | */ |
| 540 | if (fileExtension != NIL) outputFileName.Append(kDot).Append(fileExtension); |
| 541 | |
| 542 | return outputFileName; |
| 543 | } |
| 544 | |
| 545 | String freac::Utilities::GetSingleOutputFileName(const BoCA::Config *config, const Array<Track> &tracks) |
| 546 | { |
| 547 | /* Check if an output filename has already been set. |
| 548 | */ |
| 549 | Bool enableConsole = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault); |
| 550 | |
| 551 | String selectedEncoder = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 552 | String singleOutputFileName = config->GetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, Config::SettingsSingleFilenameDefault); |
| 553 | |
| 554 | if (singleOutputFileName != NIL || enableConsole) return singleOutputFileName; |
| 555 | |
| 556 | /* Instantiate selected encoder. |
| 557 | */ |
| 558 | Registry &boca = Registry::Get(); |
| 559 | EncoderComponent *encoder = (EncoderComponent *) boca.CreateComponentByID(selectedEncoder); |
| 560 | |
| 561 | if (encoder == NIL) return NIL; |
| 562 | |
| 563 | /* Find main window and create dialog. |
| 564 | */ |
| 565 | Window *mainWnd = Window::GetNthWindow(0); |
| 566 | FileSelection dialog; |
| 567 | |
| 568 | dialog.SetParentWindow(mainWnd); |
| 569 | dialog.SetMode(SFM_SAVE); |
| 570 | dialog.SetFlags(SFD_CONFIRMOVERWRITE); |
| 571 | |
| 572 | /* Get list of supported formats from selected encoder |
| 573 | */ |
| 574 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 575 | |
| 576 | const Array<FileFormat *> &formats = encoder->GetFormats(); |
| 577 | String defaultExtension = encoder->GetOutputFileExtension(); |
| 578 | |
| 579 | foreach (FileFormat *format, formats) |
| 580 | { |
| 581 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 582 | String extension; |
| 583 | |
| 584 | foreach (const String &formatExtension, formatExtensions) |
| 585 | { |
| 586 | extension.Append("*.").Append(formatExtension); |
| 587 | |
| 588 | if (foreachindex < formatExtensions.Length() - 1) extension.Append("; "); |
| 589 | } |
| 590 | |
| 591 | dialog.AddFilter(format->GetName().Append(extension != NIL ? String(" (").Append(extension).Append(")") : String()), extension); |
| 592 | } |
| 593 | |
| 594 | boca.DeleteComponent(encoder); |
| 595 | |
| 596 | dialog.AddFilter(i18n->TranslateString("All Files", "Joblist"), "*.*"); |
| 597 | |
| 598 | dialog.SetDefaultExtension(defaultExtension); |
| 599 | dialog.SetFileName(GetSingleOutputFileNameDefault(config, tracks)); |
| 600 | |
| 601 | dialog.SetInitialPath(config->GetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedSaveDirID, NIL)); |
| 602 | |
| 603 | if (dialog.ShowDialog() == Success()) |
| 604 | { |
| 605 | singleOutputFileName = dialog.GetFileName(); |
| 606 | |
| 607 | /* Save selected path. |
| 608 | */ |
| 609 | BoCA::Config::Get()->SetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedSaveDirID, File(singleOutputFileName).GetFilePath()); |
| 610 | } |
| 611 | |
| 612 | return singleOutputFileName; |
| 613 | } |
| 614 | |
| 615 | String freac::Utilities::GetSingleOutputFileNameDefault(const BoCA::Config *config, const Array<Track> &tracks) |
| 616 | { |
| 617 | /* Get configuration. |
| 618 | */ |
| 619 | String selectedEncoder = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 620 | |
| 621 | /* Instantiate selected encoder and get file extension. |
| 622 | */ |
| 623 | Registry &boca = Registry::Get(); |
| 624 | EncoderComponent *encoder = (EncoderComponent *) boca.CreateComponentByID(selectedEncoder); |
| 625 | |
| 626 | String fileExtension = (encoder != NIL) ? encoder->GetOutputFileExtension() : "audio"; |
| 627 | |
| 628 | boca.DeleteComponent(encoder); |
| 629 | |
| 630 | /* Find artist and album to use for file name. |
| 631 | */ |
| 632 | Info info = tracks.GetFirst().GetInfo(); |
| 633 | |
| 634 | if (info.HasOtherInfo(INFO_ALBUMARTIST)) info.artist = info.GetOtherInfo(INFO_ALBUMARTIST); |
| 635 | |
| 636 | foreach (const Track &chapterTrack, tracks) |
| 637 | { |
| 638 | const Info &chapterInfo = chapterTrack.GetInfo(); |
| 639 | |
| 640 | if (( chapterInfo.HasOtherInfo(INFO_ALBUMARTIST) && chapterInfo.GetOtherInfo(INFO_ALBUMARTIST) != info.artist) || |
| 641 | (!chapterInfo.HasOtherInfo(INFO_ALBUMARTIST) && chapterInfo.artist != info.artist)) info.artist = NIL; |
| 642 | |
| 643 | if (chapterInfo.album != info.album) info.album = NIL; |
| 644 | } |
| 645 | |
| 646 | /* Get file name and return. |
| 647 | */ |
| 648 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 649 | |
| 650 | return BoCA::Utilities::NormalizeFileName(BoCA::Utilities::ReplaceIncompatibleCharacters(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist")).Append(" - ") |
| 651 | .Append(BoCA::Utilities::ReplaceIncompatibleCharacters(info.album.Length() > 0 ? info.album : i18n->TranslateString("unknown album")))).Append(fileExtension != NIL ? kDot : String()).Append(fileExtension); |
| 652 | } |
| 653 | |
| 654 | String freac::Utilities::GetPlaylistFileName(const BoCA::Config *config, const Track &track, const Array<Track> &originalTracks) |
| 655 | { |
| 656 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 657 | |
| 658 | const Info &info = track.GetInfo(); |
| 659 | |
| 660 | Bool enableConsole = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault); |
| 661 | |
| 662 | if (enableConsole) return NIL; |
| 663 | |
| 664 | /* Find playlist output folder. |
| 665 | */ |
| 666 | const Track &originalTrack = originalTracks.Get(track.GetTrackID()); |
| 667 | |
| 668 | String inputDir = File(originalTrack.fileName).GetFilePath().Append(Directory::GetDirectoryDelimiter()); |
| 669 | String outputDir = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault); |
| 670 | Bool writeToInputDir = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, Config::SettingsWriteToInputDirectoryDefault); |
| 671 | |
| 672 | if (writeToInputDir && !originalTrack.isCDTrack && BoCA::Utilities::IsFolderWritable(inputDir)) outputDir = inputDir; |
| 673 | |
| 674 | /* Generate playlist file name. |
| 675 | */ |
| 676 | Bool useUnicode = config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesAllowUnicodeID, Config::SettingsFilenamesAllowUnicodeDefault); |
| 677 | Bool replaceSpaces = config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesReplaceSpacesID, Config::SettingsFilenamesReplaceSpacesDefault); |
| 678 | |
| 679 | String playlistOutputDir = BoCA::Utilities::GetAbsolutePathName(config->GetIntValue(Config::CategoryPlaylistID, Config::PlaylistUseEncoderOutputDirID, Config::PlaylistUseEncoderOutputDirDefault) ? outputDir : config->GetStringValue(Config::CategoryPlaylistID, Config::PlaylistOutputDirID, outputDir)); |
| 680 | String playlistFileName = playlistOutputDir; |
| 681 | |
| 682 | if (info.artist != NIL || info.album != NIL) |
| 683 | { |
| 684 | String shortOutFileName = config->GetStringValue(Config::CategoryPlaylistID, Config::PlaylistFilenamePatternID, Config::PlaylistFilenamePatternDefault); |
| 685 | |
| 686 | if (shortOutFileName.Trim() == NIL) shortOutFileName = Config::PlaylistFilenamePatternDefault; |
| 687 | |
| 688 | if (shortOutFileName.Contains(kArtistPattern)) shortOutFileName.Replace(kArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist"))); |
| 689 | if (shortOutFileName.Contains(kAlbumPattern)) shortOutFileName.Replace(kAlbumPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.album.Length() > 0 ? info.album : i18n->TranslateString("unknown album"))); |
| 690 | if (shortOutFileName.Contains(kGenrePattern)) shortOutFileName.Replace(kGenrePattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.genre.Length() > 0 ? info.genre : i18n->TranslateString("unknown genre"))); |
| 691 | if (shortOutFileName.Contains(kYearPattern)) shortOutFileName.Replace(kYearPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.year > 0 ? String::FromInt(info.year) : i18n->TranslateString("unknown year"))); |
| 692 | if (shortOutFileName.Contains(kDiscIDPattern)) shortOutFileName.Replace(kDiscIDPattern, CDDB::DiscIDToString(track.discid)); |
| 693 | |
| 694 | /* Replace <disc> pattern. |
| 695 | */ |
| 696 | if (shortOutFileName.Contains(kDiscPattern)) shortOutFileName.Replace(kDiscPattern, String::FromInt(Math::Max(1, info.disc))); |
| 697 | |
| 698 | for (Int i = 1; i <= 4 && shortOutFileName.Contains(kDiscExtStart); i++) |
| 699 | { |
| 700 | String pattern = String(kDiscExtStart).Append(String::FromInt(i)).Append(")>"); |
| 701 | |
| 702 | shortOutFileName.Replace(pattern, String().FillN('0', i - ((Int) Math::Log10(info.disc > 0 ? info.disc : 1) + 1)).Append(String::FromInt(Math::Max(1, info.disc)))); |
| 703 | } |
| 704 | |
| 705 | /* Replace <currentdate> pattern. |
| 706 | */ |
| 707 | if (shortOutFileName.Contains(kCurrentDatePattern)) |
| 708 | { |
| 709 | DateTime currentDateTime = DateTime::Current(); |
| 710 | String currentDate = String().FillN('0', 3 - Math::Floor(Math::Log10(currentDateTime.GetYear() ))) .Append(String::FromInt(currentDateTime.GetYear())) |
| 711 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetMonth())))).Append(String::FromInt(currentDateTime.GetMonth())) |
| 712 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetDay() )))).Append(String::FromInt(currentDateTime.GetDay())); |
| 713 | |
| 714 | shortOutFileName.Replace(kCurrentDatePattern, currentDate); |
| 715 | } |
| 716 | |
| 717 | /* Replace <currenttime> pattern. |
| 718 | */ |
| 719 | if (shortOutFileName.Contains(kCurrentTimePattern)) |
| 720 | { |
| 721 | DateTime currentDateTime = DateTime::Current(); |
| 722 | String currentTime = String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetHour() > 0 ? currentDateTime.GetHour() : 1))) .Append(String::FromInt(currentDateTime.GetHour())) |
| 723 | .Append(String().FillN('0', 1 - Math::Floor(Math::Log10(currentDateTime.GetMinute() > 0 ? currentDateTime.GetMinute() : 1)))).Append(String::FromInt(currentDateTime.GetMinute())); |
| 724 | |
| 725 | shortOutFileName.Replace(kCurrentTimePattern, currentTime); |
| 726 | } |
| 727 | |
| 728 | /* Replace other text fields. |
| 729 | */ |
| 730 | foreach (const String &pair, info.other) |
| 731 | { |
| 732 | String key = pair.Head(pair.Find(kColon)); |
| 733 | String value = pair.Tail(pair.Length() - pair.Find(kColon) - 1); |
| 734 | |
| 735 | if (value == NIL) continue; |
| 736 | |
| 737 | if (key == INFO_ALBUMARTIST && shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 738 | else if (key == INFO_CONDUCTOR && shortOutFileName.Contains(kConductorPattern)) shortOutFileName.Replace(kConductorPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 739 | else if (key == INFO_COMPOSER && shortOutFileName.Contains(kComposerPattern)) shortOutFileName.Replace(kComposerPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 740 | else if (key == INFO_BPM && shortOutFileName.Contains(kTempoPattern)) shortOutFileName.Replace(kTempoPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 741 | else if (key == INFO_INITIALKEY && shortOutFileName.Contains(kInitialKeyPattern)) shortOutFileName.Replace(kInitialKeyPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(value)); |
| 742 | } |
| 743 | |
| 744 | if (info.artist.Length() > 0 && shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(info.artist)); |
| 745 | |
| 746 | if (shortOutFileName.Contains(kAlbumArtistPattern)) shortOutFileName.Replace(kAlbumArtistPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown album artist"))); |
| 747 | if (shortOutFileName.Contains(kConductorPattern)) shortOutFileName.Replace(kConductorPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown conductor"))); |
| 748 | if (shortOutFileName.Contains(kComposerPattern)) shortOutFileName.Replace(kComposerPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown composer"))); |
| 749 | if (shortOutFileName.Contains(kTempoPattern)) shortOutFileName.Replace(kTempoPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown tempo"))); |
| 750 | if (shortOutFileName.Contains(kInitialKeyPattern)) shortOutFileName.Replace(kInitialKeyPattern, BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown key"))); |
| 751 | |
| 752 | playlistFileName.Append(BoCA::Utilities::ReplaceIncompatibleCharacters(shortOutFileName, useUnicode, False, replaceSpaces)); |
| 753 | } |
| 754 | else if (track.isCDTrack) |
| 755 | { |
| 756 | playlistFileName.Append("cd").Append(String::FromInt(track.drive)); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | playlistFileName.Append(BoCA::Utilities::ReplaceIncompatibleCharacters(i18n->TranslateString("unknown playlist"), useUnicode, True, replaceSpaces)); |
| 761 | } |
| 762 | |
| 763 | return BoCA::Utilities::NormalizeFileName(playlistFileName); |
| 764 | } |
| 765 | |
| 766 | String freac::Utilities::FormatFileNameForLogging(const String &fileName, const String &basePath) |
| 767 | { |
| 768 | BoCA::Config *config = BoCA::Config::Get(); |
| 769 | |
| 770 | Bool logCompletePaths = config->GetIntValue(Config::CategoryLoggingID, Config::LoggingLogCompletePathsID, Config::LoggingLogCompletePathsDefault); |
| 771 | |
| 772 | if (logCompletePaths || fileName.Contains("://")) return fileName; |
| 773 | |
| 774 | String fixedBasePath = basePath; |
| 775 | String shortenedFileName = fileName; |
| 776 | |
| 777 | if (fixedBasePath != NIL && !fixedBasePath.EndsWith(Directory::GetDirectoryDelimiter())) fixedBasePath.Append(Directory::GetDirectoryDelimiter()); |
| 778 | |
| 779 | if (fixedBasePath != NIL && fileName.StartsWith(fixedBasePath)) shortenedFileName = fileName.Tail(fileName.Length() - fixedBasePath.Length()); |
| 780 | else shortenedFileName = File(fileName).GetFileName(); |
| 781 | |
| 782 | return shortenedFileName; |
| 783 | } |
| 784 | |
| 785 | String freac::Utilities::GetOutputBasePath(const BoCA::Config *config, const Track &track, const String &outputFile) |
| 786 | { |
| 787 | Bool encodeToSingleFile = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, Config::SettingsEncodeToSingleFileDefault); |
| 788 | |
| 789 | if (encodeToSingleFile) return NIL; |
| 790 | |
| 791 | String encoderOutputDirectory = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault); |
| 792 | Bool writeToInputDirectory = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, Config::SettingsWriteToInputDirectoryDefault); |
| 793 | |
| 794 | String basePath = encoderOutputDirectory; |
| 795 | String inputPath = track.fileName.StartsWith("device://") ? String() : File(track.fileName).GetFilePath(); |
| 796 | |
| 797 | if (writeToInputDirectory && inputPath != NIL && outputFile.StartsWith(inputPath)) basePath = inputPath; |
| 798 | |
| 799 | return basePath; |
| 800 | } |
| 801 | |
| 802 | String freac::Utilities::GetPlaylistBasePath(const BoCA::Config *config, const Track &track, const Array<Track> &originalTracks) |
| 803 | { |
| 804 | Bool encodeToSingleFile = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, Config::SettingsEncodeToSingleFileDefault); |
| 805 | |
| 806 | if (encodeToSingleFile) return NIL; |
| 807 | |
| 808 | String encoderOutputDirectory = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault); |
| 809 | Bool writeToInputDirectory = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, Config::SettingsWriteToInputDirectoryDefault); |
| 810 | |
| 811 | const Track &originalTrack = originalTracks.Get(track.GetTrackID()); |
| 812 | String inputDirectory = File(originalTrack.fileName).GetFilePath().Append(Directory::GetDirectoryDelimiter()); |
| 813 | |
| 814 | if (writeToInputDirectory && !originalTrack.isCDTrack && BoCA::Utilities::IsFolderWritable(inputDirectory)) encoderOutputDirectory = inputDirectory; |
| 815 | |
| 816 | String playlistOutputDirectory = config->GetStringValue(Config::CategoryPlaylistID, Config::PlaylistOutputDirID, encoderOutputDirectory); |
| 817 | Bool useEncoderOutputDir = config->GetIntValue(Config::CategoryPlaylistID, Config::PlaylistUseEncoderOutputDirID, Config::PlaylistUseEncoderOutputDirDefault); |
| 818 | |
| 819 | return BoCA::Utilities::GetAbsolutePathName(useEncoderOutputDir ? encoderOutputDirectory : playlistOutputDirectory); |
| 820 | } |
| 821 | |
| 822 | Bool freac::Utilities::SetProcessPriority() |
| 823 | { |
| 824 | #ifdef __WIN32__ |
| 825 | BoCA::Config *config = BoCA::Config::Get(); |
| 826 | |
| 827 | HANDLE currentProcess = GetCurrentProcess(); |
| 828 | DWORD priorityClass = NORMAL_PRIORITY_CLASS; |
| 829 | |
| 830 | switch (config->GetIntValue(Config::CategoryResourcesID, Config::ResourcesPriorityID, Config::ResourcesPriorityDefault)) |
| 831 | { |
| 832 | case -2: priorityClass = IDLE_PRIORITY_CLASS; break; |
| 833 | case -1: priorityClass = BELOW_NORMAL_PRIORITY_CLASS; break; |
| 834 | case 0: priorityClass = NORMAL_PRIORITY_CLASS; break; |
| 835 | case 1: priorityClass = ABOVE_NORMAL_PRIORITY_CLASS; break; |
| 836 | case 2: priorityClass = HIGH_PRIORITY_CLASS; break; |
| 837 | } |
| 838 | |
| 839 | return SetPriorityClass(currentProcess, priorityClass); |
| 840 | #endif |
| 841 | |
| 842 | return False; |
| 843 | } |
| 844 | |
| 845 | Bool freac::Utilities::MirrorBitmap(Bitmap &bitmap) |
| 846 | { |
| 847 | Point point; |
| 848 | Size size = bitmap.GetSize(); |
| 849 | |
| 850 | for (point.y = 0; point.y < size.cy; point.y++) |
| 851 | { |
| 852 | for (point.x = 0; point.x < size.cx / 2; point.x++) |
| 853 | { |
| 854 | Point mirror = Point(size.cx - point.x - 1, point.y); |
| 855 | |
| 856 | Color pointPixel = bitmap.GetPixel(point); |
| 857 | Color mirrorPixel = bitmap.GetPixel(mirror); |
| 858 | |
| 859 | bitmap.SetPixel(point, mirrorPixel); |
| 860 | bitmap.SetPixel(mirror, pointPixel); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return True; |
| 865 | } |