| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 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 <jobs/joblist/addfolders.h> |
| 14 | |
| 15 | #include <cddb/cddbremote.h> |
| 16 | |
| 17 | #include <config.h> |
| 18 | #include <utilities.h> |
| 19 | |
| 20 | #include <boca.h> |
| 21 | |
| 22 | using namespace BoCA; |
| 23 | using namespace BoCA::AS; |
| 24 | |
| 25 | freac::JobAddFolders::JobAddFolders(const Array<String> &iFolders) : JobAddFiles(Array<String>()) |
| 26 | { |
| 27 | folders = iFolders; |
| 28 | } |
| 29 | |
| 30 | freac::JobAddFolders::~JobAddFolders() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Void freac::JobAddFolders::AddFolders(const Array<Directory> &folders) |
| 35 | { |
| 36 | foreach (const Directory &folder, folders) |
| 37 | { |
| 38 | if (!folder.Exists()) continue; |
| 39 | |
| 40 | /* Recurse into subfolders. |
| 41 | */ |
| 42 | const Array<Directory> &myFolders = folder.GetDirectories(); |
| 43 | const Array<File> &myFiles = folder.GetFiles(); |
| 44 | |
| 45 | AddFolders(myFolders); |
| 46 | |
| 47 | if (abort) break; |
| 48 | |
| 49 | /* Find extensions to exclude and add files. |
| 50 | */ |
| 51 | String configString = configuration->GetStringValue(Config::CategorySettingsID, Config::SettingsExcludeExtensionsID, Config::SettingsExcludeExtensionsDefault).ToLower(); |
| 52 | const Array<String> &extensions = configString.Explode("|"); |
| 53 | |
| 54 | foreach (const File &file, myFiles) |
| 55 | { |
| 56 | String fileString = String(file).ToLower(); |
| 57 | Bool add = True; |
| 58 | |
| 59 | /* Skip files without a filename extension. |
| 60 | */ |
| 61 | if (!fileString.Contains(".")) continue; |
| 62 | |
| 63 | /* Check excluded extensions. |
| 64 | */ |
| 65 | foreach (const String &extension, extensions) |
| 66 | { |
| 67 | if (!fileString.EndsWith(extension)) continue; |
| 68 | |
| 69 | add = False; |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (add) files.Add(file); |
| 75 | |
| 76 | if (abort) break; |
| 77 | } |
| 78 | |
| 79 | if (abort) break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Void freac::JobAddFolders::RemoveReferencedFiles() |
| 84 | { |
| 85 | /* Find and remove files referenced by |
| 86 | * cuesheets to avoid adding them twice. |
| 87 | */ |
| 88 | Registry &boca = Registry::Get(); |
| 89 | |
| 90 | for (Int i = 0; i < files.Length(); i++) |
| 91 | { |
| 92 | const String &file = files.GetNth(i); |
| 93 | |
| 94 | if (!file.EndsWith(".cue")) continue; |
| 95 | |
| 96 | /* Get decoder for file. |
| 97 | */ |
| 98 | DecoderComponent *decoder = boca.CreateDecoderForStream(file); |
| 99 | |
| 100 | if (decoder == NIL) continue; |
| 101 | |
| 102 | /* Get album info and loop over tracks. |
| 103 | */ |
| 104 | Track album; |
| 105 | |
| 106 | decoder->GetStreamInfo(file, album); |
| 107 | |
| 108 | foreach (const Track &track, album.tracks) |
| 109 | { |
| 110 | for (Int j = 0; j < files.Length(); j++) |
| 111 | { |
| 112 | const String &file = files.GetNth(j); |
| 113 | |
| 114 | /* Remove file from list if filename matches. |
| 115 | */ |
| 116 | if (file == track.fileName) |
| 117 | { |
| 118 | if (j < i) i--; |
| 119 | |
| 120 | files.RemoveNth(j--); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | boca.DeleteComponent(decoder); |
| 126 | |
| 127 | if (abort) break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | Bool freac::JobAddFolders::ReadyToRun() |
| 132 | { |
| 133 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 134 | |
| 135 | if (joblist->IsLocked()) return False; |
| 136 | |
| 137 | joblist->Lock(); |
| 138 | |
| 139 | return True; |
| 140 | } |
| 141 | |
| 142 | Error freac::JobAddFolders::Perform() |
| 143 | { |
| 144 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 145 | |
| 146 | i18n->SetContext("Jobs::Joblist"); |
| 147 | |
| 148 | SetText(i18n->AddEllipsis(i18n->TranslateString("Reading folders"))); |
| 149 | |
| 150 | Array<Directory> directories; |
| 151 | |
| 152 | foreach (const String &folder, folders) directories.Add(folder); |
| 153 | |
| 154 | AddFolders(directories); |
| 155 | |
| 156 | if (!abort) |
| 157 | { |
| 158 | SetText(i18n->AddEllipsis(i18n->TranslateString("Filtering duplicates"))); |
| 159 | |
| 160 | RemoveReferencedFiles(); |
| 161 | |
| 162 | if (!abort) return JobAddFiles::Perform(); |
| 163 | } |
| 164 | |
| 165 | return Success(); |
| 166 | } |