| 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 <dialogs/cddb/multimatch.h> |
| 14 | |
| 15 | #include <config.h> |
| 16 | |
| 17 | Int freac::cddbMultiMatchDlg::previewCount = 0; |
| 18 | |
| 19 | freac::cddbMultiMatchDlg::cddbMultiMatchDlg(CDDB &iCDDB, Bool fuzzy) : cddb(iCDDB) |
| 20 | { |
| 21 | BoCA::Config *config = BoCA::Config::Get(); |
| 22 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 23 | |
| 24 | i18n->SetContext("CDDB::Query"); |
| 25 | |
| 26 | Point wndPos = Point(config->GetIntValue(Config::CategorySettingsID, Config::SettingsWindowPosXID, Config::SettingsWindowPosXDefault), config->GetIntValue(Config::CategorySettingsID, Config::SettingsWindowPosYID, Config::SettingsWindowPosYDefault)) + Point(80, 80); |
| 27 | Size wndSize = Size(config->GetIntValue(Config::CategoryDialogsID, Config::DialogsCDDBMultiMatchSizeXID, Config::DialogsCDDBMultiMatchSizeXDefault), config->GetIntValue(Config::CategoryDialogsID, Config::DialogsCDDBMultiMatchSizeYID, Config::DialogsCDDBMultiMatchSizeYDefault)); |
| 28 | |
| 29 | mainWnd = new Window(i18n->TranslateString(fuzzy ? "No exact matches found" : "Multiple matches found"), wndPos, wndSize); |
| 30 | mainWnd->SetMinimumSize(Size(360, 369)); |
| 31 | mainWnd->SetRightToLeft(i18n->IsActiveLanguageRightToLeft()); |
| 32 | mainWnd->GetMainLayer()->onChangeSize.Connect(&cddbMultiMatchDlg::OnChangeSize, this); |
| 33 | |
| 34 | mainWnd_titlebar = new Titlebar(TB_CLOSEBUTTON); |
| 35 | divbar = new Divider(39, OR_HORZ | OR_BOTTOM); |
| 36 | |
| 37 | btn_cancel = new Button(i18n->TranslateString("Cancel"), Point(175, 29), Size()); |
| 38 | btn_cancel->onAction.Connect(&cddbMultiMatchDlg::Cancel, this); |
| 39 | btn_cancel->SetOrientation(OR_LOWERRIGHT); |
| 40 | |
| 41 | btn_ok = new Button(i18n->TranslateString("OK"), Point(87, 29), Size()); |
| 42 | btn_ok->onAction.Connect(&cddbMultiMatchDlg::OK, this); |
| 43 | btn_ok->SetOrientation(OR_LOWERRIGHT); |
| 44 | |
| 45 | group_match = new GroupBox(i18n->TranslateString("Matches"), Point(7, 11), Size(330, 270)); |
| 46 | |
| 47 | text_match = new Text(i18n->AddColon(i18n->TranslateString("Select the entry that best fits your CD")), Point(11, 11)); |
| 48 | combo_match = new ComboBox(Point(10, 30), Size(310, 0)); |
| 49 | combo_match->onSelectEntry.Connect(&cddbMultiMatchDlg::SelectEntry, this); |
| 50 | |
| 51 | text_preview = new Text(i18n->AddColon(i18n->TranslateString("Preview")), Point(11, 55)); |
| 52 | |
| 53 | edit_preview = new MultiEdit(Point(10, 74), Size(310, 186), 0); |
| 54 | edit_preview->Deactivate(); |
| 55 | |
| 56 | text_loading_preview = new Text(i18n->AddEllipsis(i18n->TranslateString("loading preview")), Point()); |
| 57 | text_loading_preview->SetFont(GUI::Font(GUI::Font::Default, 10, GUI::Font::Bold, 0, Setup::InactiveTextColor)); |
| 58 | text_loading_preview->SetPosition(edit_preview->GetPosition() + Point((edit_preview->GetWidth() - text_loading_preview->GetUnscaledTextWidth()) / 2, (edit_preview->GetHeight() - text_loading_preview->GetUnscaledTextHeight()) / 2)); |
| 59 | |
| 60 | group_match->Add(text_match); |
| 61 | group_match->Add(combo_match); |
| 62 | group_match->Add(text_preview); |
| 63 | group_match->Add(edit_preview); |
| 64 | group_match->Add(text_loading_preview); |
| 65 | |
| 66 | Add(mainWnd); |
| 67 | |
| 68 | mainWnd->Add(group_match); |
| 69 | |
| 70 | mainWnd->Add(btn_ok); |
| 71 | mainWnd->Add(btn_cancel); |
| 72 | mainWnd->Add(mainWnd_titlebar); |
| 73 | mainWnd->Add(divbar); |
| 74 | |
| 75 | mainWnd->SetFlags(WF_NOTASKBUTTON | WF_MODAL); |
| 76 | mainWnd->SetIcon(ImageLoader::Load(String(Config::Get()->resourcesPath).Append("icons/freac.png"))); |
| 77 | } |
| 78 | |
| 79 | freac::cddbMultiMatchDlg::~cddbMultiMatchDlg() |
| 80 | { |
| 81 | DeleteObject(mainWnd_titlebar); |
| 82 | DeleteObject(mainWnd); |
| 83 | DeleteObject(divbar); |
| 84 | |
| 85 | DeleteObject(group_match); |
| 86 | DeleteObject(text_match); |
| 87 | DeleteObject(combo_match); |
| 88 | DeleteObject(text_preview); |
| 89 | DeleteObject(edit_preview); |
| 90 | DeleteObject(text_loading_preview); |
| 91 | |
| 92 | DeleteObject(btn_ok); |
| 93 | DeleteObject(btn_cancel); |
| 94 | } |
| 95 | |
| 96 | const Error &freac::cddbMultiMatchDlg::ShowDialog() |
| 97 | { |
| 98 | error = Error(); |
| 99 | |
| 100 | mainWnd->WaitUntilClosed(); |
| 101 | |
| 102 | return error; |
| 103 | } |
| 104 | |
| 105 | Void freac::cddbMultiMatchDlg::OnChangeSize(const Size &nSize) |
| 106 | { |
| 107 | BoCA::Config *config = BoCA::Config::Get(); |
| 108 | |
| 109 | config->SetIntValue(Config::CategoryDialogsID, Config::DialogsCDDBMultiMatchSizeXID, mainWnd->GetSize().cx); |
| 110 | config->SetIntValue(Config::CategoryDialogsID, Config::DialogsCDDBMultiMatchSizeYID, mainWnd->GetSize().cy); |
| 111 | |
| 112 | Rect clientRect = Rect(mainWnd->GetMainLayer()->GetPosition(), mainWnd->GetMainLayer()->GetSize()); |
| 113 | Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 114 | |
| 115 | group_match->SetSize(clientSize - Size(14, 58)); |
| 116 | |
| 117 | combo_match->SetWidth(group_match->GetWidth() - 20); |
| 118 | edit_preview->SetSize(group_match->GetSize() - Size(20, 84)); |
| 119 | |
| 120 | text_loading_preview->SetPosition(edit_preview->GetPosition() + Point((edit_preview->GetWidth() - text_loading_preview->GetUnscaledTextWidth()) / 2, (edit_preview->GetHeight() - text_loading_preview->GetUnscaledTextHeight()) / 2)); |
| 121 | } |
| 122 | |
| 123 | Void freac::cddbMultiMatchDlg::OK() |
| 124 | { |
| 125 | /* Hide window. |
| 126 | */ |
| 127 | mainWnd->Hide(); |
| 128 | |
| 129 | CleanUpPreviews(); |
| 130 | |
| 131 | /* Close window. |
| 132 | */ |
| 133 | error = Success(); |
| 134 | |
| 135 | mainWnd->Close(); |
| 136 | } |
| 137 | |
| 138 | Void freac::cddbMultiMatchDlg::Cancel() |
| 139 | { |
| 140 | /* Hide window. |
| 141 | */ |
| 142 | mainWnd->Hide(); |
| 143 | |
| 144 | CleanUpPreviews(); |
| 145 | |
| 146 | /* Close window. |
| 147 | */ |
| 148 | mainWnd->Close(); |
| 149 | } |
| 150 | |
| 151 | Int freac::cddbMultiMatchDlg::AddEntry(const String &category, const String &title, UnsignedInt32 discID) |
| 152 | { |
| 153 | combo_match->AddEntry(String("(").Append(category).Append(") ").Append(title)); |
| 154 | |
| 155 | categories.Add(category); |
| 156 | discIDs.Add(discID); |
| 157 | |
| 158 | return Success(); |
| 159 | } |
| 160 | |
| 161 | Void freac::cddbMultiMatchDlg::SelectEntry() |
| 162 | { |
| 163 | /* Give last preview loader thread enough time to exit. |
| 164 | */ |
| 165 | previewCount++; |
| 166 | |
| 167 | for (Int i = 0; i < 10; i++) |
| 168 | { |
| 169 | if (loadPreviewThreads.Length() > 0 && |
| 170 | loadPreviewThreads.GetLast()->GetStatus() == Threads::THREAD_RUNNING) S::System::System::Sleep(10); |
| 171 | else break; |
| 172 | } |
| 173 | |
| 174 | /* Start new preview loader thread. |
| 175 | */ |
| 176 | Int index = combo_match->GetSelectedEntryNumber(); |
| 177 | |
| 178 | edit_preview->SetText(NIL); |
| 179 | |
| 180 | if (discIDs.GetNth(index) != 0) loadPreviewThreads.Add(NonBlocking1<Int>(&cddbMultiMatchDlg::LoadPreview, this).Call(index)); |
| 181 | else text_loading_preview->Hide(); |
| 182 | } |
| 183 | |
| 184 | Void freac::cddbMultiMatchDlg::LoadPreview(Int index) |
| 185 | { |
| 186 | Int me = previewCount; |
| 187 | |
| 188 | text_loading_preview->Show(); |
| 189 | |
| 190 | CDDBInfo cddbInfo; |
| 191 | |
| 192 | cddb.Read(categories.GetNth(index), discIDs.GetNth(index), cddbInfo); |
| 193 | |
| 194 | if (cddbInfo != NIL && previewCount == me) |
| 195 | { |
| 196 | String preview; |
| 197 | |
| 198 | for (Int i = 0; i < cddbInfo.trackTitles.Length(); i++) |
| 199 | { |
| 200 | preview.Append(i < 9 ? "0" : NIL).Append(String::FromInt(i + 1)).Append(": ").Append(cddbInfo.dArtist == CDDBInfo::VariousArtistsID ? cddbInfo.trackArtists.GetNth(i).Append(" - ") : String()).Append(cddbInfo.trackTitles.GetNth(i)).Append(i < cddbInfo.trackTitles.Length() - 1 ? "\n" : NIL); |
| 201 | } |
| 202 | |
| 203 | text_loading_preview->Hide(); |
| 204 | |
| 205 | edit_preview->SetText(preview); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | Void freac::cddbMultiMatchDlg::CleanUpPreviews() |
| 210 | { |
| 211 | /* Give preview loader threads enough time to exit. |
| 212 | */ |
| 213 | previewCount++; |
| 214 | |
| 215 | for (Int i = 0; i < 100; i++) |
| 216 | { |
| 217 | Int runningThreads = 0; |
| 218 | |
| 219 | foreach (Threads::Thread *thread, loadPreviewThreads) if (thread->GetStatus() == Threads::THREAD_RUNNING) runningThreads++; |
| 220 | |
| 221 | if (runningThreads > 0) S::System::System::Sleep(10); |
| 222 | else break; |
| 223 | } |
| 224 | |
| 225 | /* Kill still active threads. |
| 226 | */ |
| 227 | foreach (Threads::Thread *thread, loadPreviewThreads) thread->Stop(); |
| 228 | } |
| 229 | |
| 230 | Int freac::cddbMultiMatchDlg::GetSelectedEntryNumber() const |
| 231 | { |
| 232 | return combo_match->GetSelectedEntryNumber(); |
| 233 | } |