| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2020 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/addtracks.h> |
| 14 | #include <jobs/joblist/removeall.h> |
| 15 | |
| 16 | #include <dialogs/cddb/query.h> |
| 17 | |
| 18 | #include <config.h> |
| 19 | #include <utilities.h> |
| 20 | |
| 21 | #include <engine/converter.h> |
| 22 | |
| 23 | #include <support/autorelease.h> |
| 24 | |
| 25 | using namespace BoCA; |
| 26 | using namespace BoCA::AS; |
| 27 | |
| 28 | freac::JobAddTracks::JobAddTracks(const Array<String> &iURLs, Bool iAutoCDRead) |
| 29 | { |
| 30 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 31 | |
| 32 | i18n->SetContext("Jobs"); |
| 33 | |
| 34 | foreach (const String &url, iURLs) urls.Add(url); |
| 35 | |
| 36 | autoCDRead = iAutoCDRead; |
| 37 | |
| 38 | JobRemoveAllTracks::onRemoveAllTracksJobScheduled.Connect(&Job::RequestAbort, this); |
| 39 | |
| 40 | SetText(i18n->AddEllipsis(i18n->TranslateString("Waiting for other jobs to finish"))); |
| 41 | } |
| 42 | |
| 43 | freac::JobAddTracks::~JobAddTracks() |
| 44 | { |
| 45 | JobRemoveAllTracks::onRemoveAllTracksJobScheduled.Disconnect(&Job::RequestAbort, this); |
| 46 | } |
| 47 | |
| 48 | Bool freac::JobAddTracks::ReadyToRun() |
| 49 | { |
| 50 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 51 | |
| 52 | if (joblist->IsLocked()) return False; |
| 53 | |
| 54 | joblist->Lock(); |
| 55 | |
| 56 | return True; |
| 57 | } |
| 58 | |
| 59 | Error freac::JobAddTracks::Perform() |
| 60 | { |
| 61 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 62 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 63 | |
| 64 | Array<Track> tracks; |
| 65 | |
| 66 | CDDBInfo cdInfo; |
| 67 | Bool cddbQueried = False; |
| 68 | |
| 69 | for (Int i = 0; i < urls.Length(); i++) |
| 70 | { |
| 71 | if (abort) break; |
| 72 | |
| 73 | AutoRelease autoRelease; |
| 74 | |
| 75 | /* Get track URL. |
| 76 | */ |
| 77 | const String &url = urls.GetNth(i); |
| 78 | |
| 79 | SetText(i18n->AddEllipsis(i18n->TranslateString("Adding tracks", "Jobs::Joblist")).Append(" - ").Append(url)); |
| 80 | |
| 81 | /* Create decoder component. |
| 82 | */ |
| 83 | DecoderComponent *decoder = Registry::Get().CreateDecoderForStream(url); |
| 84 | |
| 85 | if (decoder == NIL) |
| 86 | { |
| 87 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 88 | |
| 89 | i18n->SetContext("Errors"); |
| 90 | |
| 91 | errors.Add(i18n->TranslateString("Unable to open file: %1\n\nError: %2").Replace("%1", url).Replace("%2", i18n->TranslateString("Unknown file type"))); |
| 92 | |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | /* Query stream info. |
| 97 | */ |
| 98 | Track track; |
| 99 | Error error = decoder->GetStreamInfo(url, track); |
| 100 | String errorString = decoder->GetErrorString(); |
| 101 | |
| 102 | Registry::Get().DeleteComponent(decoder); |
| 103 | |
| 104 | if (error == Error()) |
| 105 | { |
| 106 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 107 | |
| 108 | i18n->SetContext("Errors"); |
| 109 | |
| 110 | errors.Add(i18n->TranslateString("Unable to open file: %1\n\nError: %2").Replace("%1", url).Replace("%2", i18n->TranslateString(errorString))); |
| 111 | |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | /* Add disc ID to CD tracks. |
| 116 | */ |
| 117 | track.discid = CDDB::DiscIDFromMCDI(track.GetInfo().mcdi); |
| 118 | |
| 119 | /* Exit if we are auto-reading a CD that is already loaded in the joblist. |
| 120 | */ |
| 121 | if (autoCDRead) |
| 122 | { |
| 123 | const Array<Track> *tracks = joblist->getTrackList.Call(); |
| 124 | Bool foundTrack = False; |
| 125 | |
| 126 | for (Int i = 0; i < tracks->Length(); i++) |
| 127 | { |
| 128 | const Track &cdTrack = tracks->GetNth(i); |
| 129 | |
| 130 | if (cdTrack.discid == track.discid && cdTrack.cdTrack == track.cdTrack) foundTrack = True; |
| 131 | } |
| 132 | |
| 133 | if (foundTrack) continue; |
| 134 | } |
| 135 | |
| 136 | /* Set special title for hidden tracks. |
| 137 | */ |
| 138 | if (track.cdTrack == 0) |
| 139 | { |
| 140 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 141 | Info info = track.GetInfo(); |
| 142 | |
| 143 | info.title = i18n->TranslateString("hidden track"); |
| 144 | |
| 145 | track.SetInfo(info); |
| 146 | } |
| 147 | |
| 148 | /* Query CDDB and update track info. |
| 149 | */ |
| 150 | if (configuration->GetIntValue(Config::CategoryFreedbID, Config::FreedbAutoQueryID, Config::FreedbAutoQueryDefault)) |
| 151 | { |
| 152 | if (!cddbQueried) |
| 153 | { |
| 154 | cdInfo = cddbQueryDlg::QueryCDDB(track, True); |
| 155 | cddbQueried = True; |
| 156 | } |
| 157 | |
| 158 | if (cdInfo != NIL) cdInfo.UpdateTrack(track); |
| 159 | } |
| 160 | |
| 161 | /* Add track to joblist. |
| 162 | */ |
| 163 | joblist->onComponentAddTrack.Emit(track); |
| 164 | |
| 165 | track = joblist->getTrackList.Call()->GetLast(); |
| 166 | tracks.Add(track); |
| 167 | |
| 168 | SetProgress((i + 1) * 1000 / urls.Length()); |
| 169 | } |
| 170 | |
| 171 | if (abort) errors.RemoveAll(); |
| 172 | |
| 173 | if (!abort) |
| 174 | { |
| 175 | SetText(i18n->TranslateString("Added %1 tracks, %2 errors occurred", "Jobs::Joblist").Replace("%1", String::FromInt(urls.Length() - errors.Length())).Replace("%2", String::FromInt(errors.Length()))); |
| 176 | SetProgress(1000); |
| 177 | |
| 178 | /* Start automatic ripping if enabled. |
| 179 | */ |
| 180 | Bool autoRip = configuration->GetIntValue(Config::CategoryRipperID, Config::RipperAutoRipID, Config::RipperAutoRipDefault); |
| 181 | String filenamePattern = configuration->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, Config::SettingsEncoderFilenamePatternDefault); |
| 182 | |
| 183 | if (autoCDRead && autoRip && (tracks.GetLast().GetInfo().title != NIL || filenamePattern.Contains("<currenttime>"))) Converter().Convert(tracks, autoRip); |
| 184 | } |
| 185 | |
| 186 | urls.RemoveAll(); |
| 187 | |
| 188 | joblist->Unlock(); |
| 189 | |
| 190 | return Success(); |
| 191 | } |