| 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 <joblist.h> |
| 14 | #include <config.h> |
| 15 | #include <utilities.h> |
| 16 | |
| 17 | #include <cddb/cddb.h> |
| 18 | |
| 19 | #include <gui/layer_tooltip.h> |
| 20 | |
| 21 | #include <jobs/joblist/addfiles.h> |
| 22 | #include <jobs/joblist/addfolders.h> |
| 23 | #include <jobs/joblist/removeall.h> |
| 24 | |
| 25 | using namespace smooth::GUI::Dialogs; |
| 26 | using namespace smooth::IO; |
| 27 | |
| 28 | using namespace BoCA; |
| 29 | using namespace BoCA::AS; |
| 30 | |
| 31 | freac::JobList::JobList(const Point &iPos, const Size &iSize) : ListBox(iPos, iSize) |
| 32 | { |
| 33 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 34 | |
| 35 | i18n->SetContext("Joblist"); |
| 36 | |
| 37 | SetFlags(LF_ALLOWREORDER | LF_MULTICHECKBOX); |
| 38 | |
| 39 | headerTabsHash = 0; |
| 40 | |
| 41 | EnableLocking(); |
| 42 | |
| 43 | tracks.EnableLocking(); |
| 44 | |
| 45 | onRegister.Connect(&JobList::OnRegister, this); |
| 46 | onUnregister.Connect(&JobList::OnUnregister, this); |
| 47 | |
| 48 | onSelectEntry.Connect(&JobList::OnSelectEntry, this); |
| 49 | onMarkEntry.Connect(&JobList::OnMarkEntry, this); |
| 50 | |
| 51 | onClickTab.Connect(&JobList::OnClickTab, this); |
| 52 | |
| 53 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 54 | |
| 55 | joblist->onComponentAddTrack.Connect(&JobList::AddTrack, this); |
| 56 | joblist->onComponentRemoveTrack.Connect(&JobList::RemoveTrack, this); |
| 57 | joblist->onComponentModifyTrack.Connect(&JobList::UpdateTrackInfo, this); |
| 58 | joblist->onComponentSelectTrack.Connect(&JobList::OnComponentSelectTrack, this); |
| 59 | |
| 60 | joblist->onComponentMarkTrack.Connect(&JobList::OnComponentMarkTrack, this); |
| 61 | joblist->onComponentUnmarkTrack.Connect(&JobList::OnComponentUnmarkTrack, this); |
| 62 | |
| 63 | joblist->doAddFiles.Connect(&JobList::AddTracksByFileNames, this); |
| 64 | |
| 65 | joblist->doRemoveAllTracks.Connect(&JobList::RemoveAllTracks, this); |
| 66 | |
| 67 | joblist->getTrackList.Connect(&JobList::GetTrackList, this); |
| 68 | |
| 69 | droparea = new DropArea(iPos, iSize); |
| 70 | droparea->onDropFiles.Connect(&JobList::AddTracksByDragAndDrop, this); |
| 71 | |
| 72 | Config *freacConfig = Config::Get(); |
| 73 | |
| 74 | text = new Text(NIL, iPos - Point(9, 19)); |
| 75 | |
| 76 | button_sel_all = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-all.png")), iPos - Point(19, 4), Size(21, 21)); |
| 77 | button_sel_all->onAction.Connect(&JobList::SelectAll, this); |
| 78 | button_sel_all->SetFlags(BF_NOFRAME); |
| 79 | button_sel_all->SetTooltipText(i18n->TranslateString("Select all")); |
| 80 | |
| 81 | button_sel_none = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-none.png")), iPos - Point(19, -10), Size(21, 21)); |
| 82 | button_sel_none->onAction.Connect(&JobList::SelectNone, this); |
| 83 | button_sel_none->SetFlags(BF_NOFRAME); |
| 84 | button_sel_none->SetTooltipText(i18n->TranslateString("Select none")); |
| 85 | |
| 86 | button_sel_toggle = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-toggle.png")), iPos - Point(19, -24), Size(21, 21)); |
| 87 | button_sel_toggle->onAction.Connect(&JobList::ToggleSelection, this); |
| 88 | button_sel_toggle->SetFlags(BF_NOFRAME); |
| 89 | button_sel_toggle->SetTooltipText(i18n->TranslateString("Toggle selection")); |
| 90 | |
| 91 | /* Add tabs and update headerTabsHash. |
| 92 | */ |
| 93 | OnChangeConfigurationSettings(); |
| 94 | } |
| 95 | |
| 96 | freac::JobList::~JobList() |
| 97 | { |
| 98 | /* Save tab field sizes. |
| 99 | */ |
| 100 | String fieldSizes; |
| 101 | |
| 102 | for (Int i = 0; i < GetNOfTabs(); i++) |
| 103 | { |
| 104 | if (i > 0) fieldSizes.Append(","); |
| 105 | |
| 106 | if (GetNthTabWidth(i) <= 0) fieldSizes.Append("*"); |
| 107 | else fieldSizes.Append(String::FromInt(GetNthTabWidth(i))); |
| 108 | } |
| 109 | |
| 110 | BoCA::Config *config = BoCA::Config::Get(); |
| 111 | |
| 112 | config->SetStringValue(Config::CategoryJoblistID, Config::JoblistFieldSizesID, fieldSizes); |
| 113 | |
| 114 | /* Clean up. |
| 115 | */ |
| 116 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 117 | |
| 118 | joblist->onComponentAddTrack.Disconnect(&JobList::AddTrack, this); |
| 119 | joblist->onComponentRemoveTrack.Disconnect(&JobList::RemoveTrack, this); |
| 120 | joblist->onComponentModifyTrack.Disconnect(&JobList::UpdateTrackInfo, this); |
| 121 | joblist->onComponentSelectTrack.Disconnect(&JobList::OnComponentSelectTrack, this); |
| 122 | |
| 123 | joblist->onComponentMarkTrack.Disconnect(&JobList::OnComponentMarkTrack, this); |
| 124 | joblist->onComponentUnmarkTrack.Disconnect(&JobList::OnComponentUnmarkTrack, this); |
| 125 | |
| 126 | joblist->doAddFiles.Disconnect(&JobList::AddTracksByFileNames, this); |
| 127 | |
| 128 | joblist->doRemoveAllTracks.Disconnect(&JobList::RemoveAllTracks, this); |
| 129 | |
| 130 | joblist->getTrackList.Disconnect(&JobList::GetTrackList, this); |
| 131 | |
| 132 | onRegister.Disconnect(&JobList::OnRegister, this); |
| 133 | onUnregister.Disconnect(&JobList::OnUnregister, this); |
| 134 | |
| 135 | DeleteObject(droparea); |
| 136 | DeleteObject(text); |
| 137 | |
| 138 | DeleteObject(button_sel_all); |
| 139 | DeleteObject(button_sel_none); |
| 140 | DeleteObject(button_sel_toggle); |
| 141 | } |
| 142 | |
| 143 | Int freac::JobList::GetNOfTracks() const |
| 144 | { |
| 145 | return tracks.Length(); |
| 146 | } |
| 147 | |
| 148 | const BoCA::Track &freac::JobList::GetNthTrack(Int n) const |
| 149 | { |
| 150 | static Track nil(NIL); |
| 151 | |
| 152 | if (n < 0 || GetNOfTracks() <= n) return nil; |
| 153 | |
| 154 | /* Entries may have been moved in the joblist, |
| 155 | * so get the entry by index instead of position. |
| 156 | */ |
| 157 | return tracks.Get(GetNthEntry(n)->GetHandle()); |
| 158 | } |
| 159 | |
| 160 | const Array<BoCA::Track> *freac::JobList::GetTrackList() |
| 161 | { |
| 162 | return &tracks; |
| 163 | } |
| 164 | |
| 165 | Bool freac::JobList::AddTrack(const Track &iTrack) |
| 166 | { |
| 167 | BoCA::Config *config = BoCA::Config::Get(); |
| 168 | Track track = iTrack; |
| 169 | |
| 170 | track.SetOriginalInfo(track.GetInfo()); |
| 171 | |
| 172 | /* Add entry to joblist. |
| 173 | */ |
| 174 | ListEntry *entry = AddEntry(GetEntryText(track), True); |
| 175 | |
| 176 | if (config->GetIntValue(Config::CategorySettingsID, Config::SettingsShowTooltipsID, Config::SettingsShowTooltipsDefault)) entry->SetTooltipLayer(new LayerTooltip(track)); |
| 177 | |
| 178 | tracks.Add(track, entry->GetHandle()); |
| 179 | |
| 180 | UpdateTextLine(); |
| 181 | |
| 182 | /* Notify components that a track has been added. |
| 183 | */ |
| 184 | BoCA::JobList::Get()->onApplicationAddTrack.Emit(track); |
| 185 | |
| 186 | return True; |
| 187 | } |
| 188 | |
| 189 | Bool freac::JobList::RemoveTrack(const Track &track) |
| 190 | { |
| 191 | ListEntry *entry = GetEntryByTrack(track); |
| 192 | |
| 193 | if (entry != NIL) |
| 194 | { |
| 195 | /* Notify components of track removal. |
| 196 | */ |
| 197 | BoCA::JobList::Get()->onApplicationRemoveTrack.Emit(track); |
| 198 | |
| 199 | /* Remove track from track list and joblist. |
| 200 | */ |
| 201 | Surface *surface = (IsVisible() ? GetDrawSurface() : NIL); |
| 202 | |
| 203 | if (surface) surface->StartPaint(GetVisibleArea()); |
| 204 | |
| 205 | tracks.Remove(entry->GetHandle()); |
| 206 | |
| 207 | entry->Hide(); |
| 208 | |
| 209 | if (entry->GetTooltipLayer() != NIL) |
| 210 | { |
| 211 | delete entry->GetTooltipLayer(); |
| 212 | |
| 213 | entry->SetTooltipLayer(NIL); |
| 214 | } |
| 215 | |
| 216 | Remove(entry); |
| 217 | |
| 218 | if (surface) surface->EndPaint(); |
| 219 | |
| 220 | UpdateTextLine(); |
| 221 | } |
| 222 | |
| 223 | return True; |
| 224 | } |
| 225 | |
| 226 | Bool freac::JobList::RemoveNthTrack(Int n) |
| 227 | { |
| 228 | ListEntry *entry = GetNthEntry(n); |
| 229 | const Track &track = tracks.Get(entry->GetHandle()); |
| 230 | |
| 231 | RemoveTrack(track); |
| 232 | |
| 233 | return True; |
| 234 | } |
| 235 | |
| 236 | Bool freac::JobList::RemoveAllTracks() |
| 237 | { |
| 238 | Surface *surface = (IsVisible() ? GetDrawSurface() : NIL); |
| 239 | |
| 240 | if (surface) surface->StartPaint(GetVisibleArea()); |
| 241 | |
| 242 | for (Int i = 0; i < tracks.Length(); i++) |
| 243 | { |
| 244 | ListEntry *entry = GetNthEntry(i); |
| 245 | |
| 246 | entry->Hide(); |
| 247 | |
| 248 | if (entry->GetTooltipLayer() != NIL) |
| 249 | { |
| 250 | delete entry->GetTooltipLayer(); |
| 251 | |
| 252 | entry->SetTooltipLayer(NIL); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (surface) surface->EndPaint(); |
| 257 | |
| 258 | /* Notify components that all tracks will be removed. |
| 259 | */ |
| 260 | BoCA::JobList::Get()->onApplicationRemoveAllTracks.Emit(); |
| 261 | |
| 262 | /* Actually remove entries and tracks here. |
| 263 | */ |
| 264 | RemoveAllEntries(); |
| 265 | |
| 266 | tracks.RemoveAll(); |
| 267 | |
| 268 | UpdateTextLine(); |
| 269 | |
| 270 | return True; |
| 271 | } |
| 272 | |
| 273 | Void freac::JobList::StartJobRemoveAllTracks() |
| 274 | { |
| 275 | (new JobRemoveAllTracks())->Schedule(); |
| 276 | } |
| 277 | |
| 278 | const BoCA::Track &freac::JobList::GetSelectedTrack() const |
| 279 | { |
| 280 | return GetNthTrack(GetSelectedEntryNumber()); |
| 281 | } |
| 282 | |
| 283 | Int freac::JobList::SetMetrics(const Point &nPos, const Size &nSize) |
| 284 | { |
| 285 | droparea->SetMetrics(nPos, nSize); |
| 286 | |
| 287 | return ListBox::SetMetrics(nPos, nSize); |
| 288 | } |
| 289 | |
| 290 | Void freac::JobList::AddTrackByDialog() |
| 291 | { |
| 292 | BoCA::Config *config = BoCA::Config::Get(); |
| 293 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 294 | |
| 295 | i18n->SetContext("Joblist"); |
| 296 | |
| 297 | FileSelection dialog; |
| 298 | |
| 299 | dialog.SetParentWindow(container->GetContainerWindow()); |
| 300 | dialog.SetFlags(SFD_ALLOWMULTISELECT); |
| 301 | dialog.SetInitialPath(config->GetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedOpenDirID, NIL)); |
| 302 | |
| 303 | Array<String> types; |
| 304 | Array<String> extensions; |
| 305 | |
| 306 | Registry &boca = Registry::Get(); |
| 307 | |
| 308 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 309 | { |
| 310 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_DECODER) continue; |
| 311 | |
| 312 | const Array<FileFormat *> &formats = boca.GetComponentFormats(i); |
| 313 | |
| 314 | foreach (FileFormat *format, formats) |
| 315 | { |
| 316 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 317 | String extension; |
| 318 | |
| 319 | foreach (const String &formatExtension, formatExtensions) |
| 320 | { |
| 321 | extension.Append("*.").Append(formatExtension); |
| 322 | |
| 323 | if (foreachindex < formatExtensions.Length() - 1) extension.Append("; "); |
| 324 | } |
| 325 | |
| 326 | types.Add(format->GetName().Append(" (").Append(extension).Append(")")); |
| 327 | extensions.Add(extension); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | String fileTypes; |
| 332 | |
| 333 | foreach (const String &extension, extensions) |
| 334 | { |
| 335 | if (!fileTypes.Contains(extension)) fileTypes.Append(foreachindex > 0 ? ";" : NIL).Append(extension); |
| 336 | } |
| 337 | |
| 338 | dialog.AddFilter(i18n->TranslateString("Audio Files"), fileTypes); |
| 339 | |
| 340 | foreach (const String &type, types) dialog.AddFilter(type, extensions.GetNth(foreachindex)); |
| 341 | |
| 342 | dialog.AddFilter(i18n->TranslateString("All Files"), "*.*"); |
| 343 | |
| 344 | if (dialog.ShowDialog() == Success()) |
| 345 | { |
| 346 | const Array<String> &files = dialog.GetFileNames(); |
| 347 | |
| 348 | AddTracksByFileNames(files); |
| 349 | |
| 350 | /* Save selected path. |
| 351 | */ |
| 352 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedOpenDirID, File(dialog.GetFileName()).GetFilePath()); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | Bool freac::JobList::AddTracksByFileNames(const Array<String> &files) |
| 357 | { |
| 358 | Array<String> filesToAdd; |
| 359 | Array<String> foldersToAdd; |
| 360 | |
| 361 | foreach (const String &file, files) |
| 362 | { |
| 363 | if (LoadFromPlaylist(file)) continue; |
| 364 | |
| 365 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 366 | |
| 367 | if (File(file).Exists()) filesToAdd.Add(file); |
| 368 | else if (Directory(file).Exists()) foldersToAdd.Add(file); |
| 369 | else BoCA::Utilities::ErrorMessage("Unable to open file: %1\n\nError: %2", File(file).GetFileName(), i18n->TranslateString("File not found", "Errors")); |
| 370 | } |
| 371 | |
| 372 | if (filesToAdd.Length() > 0) (new JobAddFiles(filesToAdd))->Schedule(); |
| 373 | if (foldersToAdd.Length() > 0) (new JobAddFolders(foldersToAdd))->Schedule(); |
| 374 | |
| 375 | return True; |
| 376 | } |
| 377 | |
| 378 | Void freac::JobList::AddTracksByDragAndDrop(const Array<String> &files) |
| 379 | { |
| 380 | AddTracksByFileNames(files); |
| 381 | } |
| 382 | |
| 383 | Void freac::JobList::AddTracksByPattern(const String &directory, const String &pattern, Bool searchSubDirectories) |
| 384 | { |
| 385 | Array<String> jobFiles; |
| 386 | |
| 387 | FindTracksByPattern(jobFiles, directory, pattern, searchSubDirectories); |
| 388 | |
| 389 | if (jobFiles.Length() == 0) |
| 390 | { |
| 391 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 392 | |
| 393 | i18n->SetContext("Joblist::Errors"); |
| 394 | |
| 395 | BoCA::Utilities::ErrorMessage(i18n->TranslateString("No files found matching pattern: %1").Replace("%1", pattern)); |
| 396 | } |
| 397 | |
| 398 | AddTracksByFileNames(jobFiles); |
| 399 | } |
| 400 | |
| 401 | Void freac::JobList::FindTracksByPattern(Array<String> &jobFiles, const String &directory, const String &pattern, Bool searchSubDirectories) const |
| 402 | { |
| 403 | Directory dir = Directory(directory); |
| 404 | |
| 405 | if (searchSubDirectories) |
| 406 | { |
| 407 | const Array<Directory> &directories = dir.GetDirectories(); |
| 408 | |
| 409 | foreach (const Directory &directory, directories) FindTracksByPattern(jobFiles, directory, pattern, True); |
| 410 | } |
| 411 | |
| 412 | if (pattern.Contains(Directory::GetDirectoryDelimiter())) |
| 413 | { |
| 414 | String head = pattern.Head(pattern.Find(Directory::GetDirectoryDelimiter())); |
| 415 | String tail = pattern.Tail(pattern.Length() - pattern.Find(Directory::GetDirectoryDelimiter()) - 1); |
| 416 | |
| 417 | const Array<Directory> &directories = dir.GetDirectoriesByPattern(head); |
| 418 | |
| 419 | foreach (const Directory &directory, directories) FindTracksByPattern(jobFiles, directory, tail, False); |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | const Array<File> &files = dir.GetFilesByPattern(pattern); |
| 424 | |
| 425 | foreach (const File &file, files) jobFiles.Add(file); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | Void freac::JobList::UpdateTrackInfo(const Track &track) |
| 430 | { |
| 431 | ListEntry *entry = GetEntryByTrack(track); |
| 432 | |
| 433 | if (entry != NIL) |
| 434 | { |
| 435 | Track &existingTrack = tracks.GetReference(entry->GetHandle()); |
| 436 | |
| 437 | entry->SetText(GetEntryText(track)); |
| 438 | |
| 439 | if (BoCA::Config::Get()->GetIntValue(Config::CategorySettingsID, Config::SettingsShowTooltipsID, Config::SettingsShowTooltipsDefault)) |
| 440 | { |
| 441 | if (entry->GetTooltipLayer() != NIL) |
| 442 | { |
| 443 | Surface *surface = (entry->IsVisible() ? GetDrawSurface() : NIL); |
| 444 | |
| 445 | if (surface) surface->StartPaint(entry->GetVisibleArea()); |
| 446 | |
| 447 | entry->Hide(); |
| 448 | |
| 449 | ((LayerTooltip *) entry->GetTooltipLayer())->UpdateFromTrack(track); |
| 450 | |
| 451 | entry->Show(); |
| 452 | |
| 453 | if (surface) surface->EndPaint(); |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | entry->SetTooltipLayer(new LayerTooltip(track)); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | existingTrack = track; |
| 462 | } |
| 463 | |
| 464 | BoCA::JobList::Get()->onApplicationModifyTrack.Emit(track); |
| 465 | } |
| 466 | |
| 467 | Void freac::JobList::RemoveSelectedTrack() |
| 468 | { |
| 469 | if (GetSelectedEntry() == NIL) |
| 470 | { |
| 471 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 472 | |
| 473 | i18n->SetContext("Joblist::Errors"); |
| 474 | |
| 475 | BoCA::Utilities::ErrorMessage(i18n->TranslateString("You did not select a track!")); |
| 476 | |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | const Track &track = GetSelectedTrack(); |
| 481 | |
| 482 | for (Int i = 0; i < GetNOfTracks(); i++) |
| 483 | { |
| 484 | if (GetNthTrack(i).GetTrackID() != track.GetTrackID()) continue; |
| 485 | |
| 486 | if (Length() > 1) |
| 487 | { |
| 488 | if (i < Length() - 1) SelectNthEntry(i + 1); |
| 489 | else SelectNthEntry(i - 1); |
| 490 | } |
| 491 | |
| 492 | RemoveNthTrack(i); |
| 493 | |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | Void freac::JobList::SelectAll() |
| 499 | { |
| 500 | for (Int i = 0; i < Length(); i++) |
| 501 | { |
| 502 | if (!GetNthEntry(i)->IsMarked()) GetNthEntry(i)->SetMark(True); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | Void freac::JobList::SelectNone() |
| 507 | { |
| 508 | for (Int i = 0; i < Length(); i++) |
| 509 | { |
| 510 | if (GetNthEntry(i)->IsMarked()) GetNthEntry(i)->SetMark(False); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | Void freac::JobList::ToggleSelection() |
| 515 | { |
| 516 | for (Int i = 0; i < Length(); i++) |
| 517 | { |
| 518 | if (GetNthEntry(i)->IsMarked()) GetNthEntry(i)->SetMark(False); |
| 519 | else GetNthEntry(i)->SetMark(True); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | Void freac::JobList::OpenJobList() |
| 524 | { |
| 525 | BoCA::Config *config = BoCA::Config::Get(); |
| 526 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 527 | |
| 528 | i18n->SetContext("Joblist"); |
| 529 | |
| 530 | FileSelection dialog; |
| 531 | |
| 532 | dialog.SetParentWindow(container->GetContainerWindow()); |
| 533 | dialog.SetInitialPath(config->GetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedJoblistDirID, NIL)); |
| 534 | |
| 535 | /* Add available formats to dialog. |
| 536 | */ |
| 537 | Array<String> types; |
| 538 | Array<String> extensions; |
| 539 | |
| 540 | Registry &boca = Registry::Get(); |
| 541 | |
| 542 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 543 | { |
| 544 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_PLAYLIST) continue; |
| 545 | |
| 546 | const Array<FileFormat *> &formats = boca.GetComponentFormats(i); |
| 547 | |
| 548 | foreach (FileFormat *format, formats) |
| 549 | { |
| 550 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 551 | String extension; |
| 552 | |
| 553 | foreach (const String &formatExtension, formatExtensions) |
| 554 | { |
| 555 | extension.Append("*.").Append(formatExtension); |
| 556 | |
| 557 | if (foreachindex < formatExtensions.Length() - 1) extension.Append("; "); |
| 558 | } |
| 559 | |
| 560 | types.Add(format->GetName().Append(" (").Append(extension).Append(")")); |
| 561 | extensions.Add(extension); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | String fileTypes; |
| 566 | |
| 567 | foreach (const String &extension, extensions) |
| 568 | { |
| 569 | if (!fileTypes.Contains(extension)) fileTypes.Append(foreachindex > 0 ? ";" : NIL).Append(extension); |
| 570 | } |
| 571 | |
| 572 | dialog.AddFilter(i18n->TranslateString("Playlist Files"), fileTypes); |
| 573 | |
| 574 | foreach (const String &type, types) dialog.AddFilter(type, extensions.GetNth(foreachindex)); |
| 575 | |
| 576 | dialog.AddFilter(i18n->TranslateString("All Files"), "*.*"); |
| 577 | |
| 578 | /* Display open file dialog. |
| 579 | */ |
| 580 | if (dialog.ShowDialog() == Success()) |
| 581 | { |
| 582 | File file(dialog.GetFileName()); |
| 583 | |
| 584 | /* Load joblist from playlist file. |
| 585 | */ |
| 586 | LoadFromPlaylist(file); |
| 587 | |
| 588 | /* Save selected path. |
| 589 | */ |
| 590 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedJoblistDirID, file.GetFilePath()); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | Void freac::JobList::SaveJobList() |
| 595 | { |
| 596 | BoCA::Config *config = BoCA::Config::Get(); |
| 597 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 598 | |
| 599 | i18n->SetContext("Joblist"); |
| 600 | |
| 601 | FileSelection dialog; |
| 602 | |
| 603 | dialog.SetParentWindow(container->GetContainerWindow()); |
| 604 | dialog.SetMode(SFM_SAVE); |
| 605 | dialog.SetFlags(SFD_CONFIRMOVERWRITE); |
| 606 | dialog.SetInitialPath(config->GetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedJoblistDirID, NIL)); |
| 607 | |
| 608 | /* Add available formats to dialog. |
| 609 | */ |
| 610 | Registry &boca = Registry::Get(); |
| 611 | Bool first = True; |
| 612 | String defaultExtension; |
| 613 | |
| 614 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 615 | { |
| 616 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_PLAYLIST) continue; |
| 617 | |
| 618 | const Array<FileFormat *> &formats = boca.GetComponentFormats(i); |
| 619 | |
| 620 | foreach (FileFormat *format, formats) |
| 621 | { |
| 622 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 623 | String extension; |
| 624 | |
| 625 | foreach (const String &formatExtension, formatExtensions) |
| 626 | { |
| 627 | if (first || formatExtension == "m3u8") defaultExtension = formatExtension; |
| 628 | |
| 629 | extension.Append("*.").Append(formatExtension); |
| 630 | |
| 631 | if (foreachindex < formatExtensions.Length() - 1) extension.Append("; "); |
| 632 | |
| 633 | first = False; |
| 634 | } |
| 635 | |
| 636 | dialog.AddFilter(format->GetName().Append(" (").Append(extension).Append(")"), extension); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | dialog.AddFilter(i18n->TranslateString("All Files"), "*.*"); |
| 641 | |
| 642 | dialog.SetDefaultExtension(defaultExtension); |
| 643 | dialog.SetFileName(String(i18n->TranslateString("Joblist")).Append(".").Append(defaultExtension)); |
| 644 | |
| 645 | /* Display save file dialog. |
| 646 | */ |
| 647 | if (dialog.ShowDialog() == Success()) |
| 648 | { |
| 649 | File file(dialog.GetFileName()); |
| 650 | |
| 651 | /* Save joblist to playlist file. |
| 652 | */ |
| 653 | SaveToPlaylist(file); |
| 654 | |
| 655 | /* Save selected path. |
| 656 | */ |
| 657 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsLastSelectedJoblistDirID, file.GetFilePath()); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | Bool freac::JobList::LoadFromPlaylist(const String &file) |
| 662 | { |
| 663 | Registry &boca = Registry::Get(); |
| 664 | |
| 665 | /* Create playlist component for file type. |
| 666 | */ |
| 667 | PlaylistComponent *playlist = NIL; |
| 668 | |
| 669 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 670 | { |
| 671 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_PLAYLIST) continue; |
| 672 | |
| 673 | playlist = (PlaylistComponent *) boca.CreateComponentByID(boca.GetComponentID(i)); |
| 674 | |
| 675 | if (playlist != NIL) |
| 676 | { |
| 677 | if (playlist->CanOpenFile(file)) break; |
| 678 | |
| 679 | boca.DeleteComponent(playlist); |
| 680 | |
| 681 | playlist = NIL; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (playlist == NIL) return False; |
| 686 | |
| 687 | /* Load playlist file and delete component. |
| 688 | */ |
| 689 | const Array<Track> &tracks = playlist->ReadPlaylist(file); |
| 690 | Array<String> files; |
| 691 | |
| 692 | foreach (const Track &track, tracks) files.Add(track.fileName); |
| 693 | |
| 694 | (new JobAddFiles(files))->Schedule(); |
| 695 | |
| 696 | boca.DeleteComponent(playlist); |
| 697 | |
| 698 | return True; |
| 699 | } |
| 700 | |
| 701 | Bool freac::JobList::SaveToPlaylist(const String &file) const |
| 702 | { |
| 703 | Registry &boca = Registry::Get(); |
| 704 | |
| 705 | /* Create playlist component for file type. |
| 706 | */ |
| 707 | PlaylistComponent *playlist = NIL; |
| 708 | |
| 709 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 710 | { |
| 711 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_PLAYLIST) continue; |
| 712 | |
| 713 | playlist = (PlaylistComponent *) boca.CreateComponentByID(boca.GetComponentID(i)); |
| 714 | |
| 715 | const Array<FileFormat *> &formats = boca.GetComponentFormats(i); |
| 716 | Bool found = False; |
| 717 | |
| 718 | foreach (FileFormat *format, formats) |
| 719 | { |
| 720 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 721 | |
| 722 | foreach (const String &formatExtension, formatExtensions) |
| 723 | { |
| 724 | if (!file.ToLower().EndsWith(String(".").Append(formatExtension.ToLower()))) continue; |
| 725 | |
| 726 | found = True; |
| 727 | |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | if (found) break; |
| 732 | } |
| 733 | |
| 734 | if (found) break; |
| 735 | |
| 736 | boca.DeleteComponent(playlist); |
| 737 | |
| 738 | playlist = NIL; |
| 739 | } |
| 740 | |
| 741 | if (playlist == NIL) return False; |
| 742 | |
| 743 | /* Save playlist file and delete component. |
| 744 | */ |
| 745 | playlist->SetTrackList(tracks); |
| 746 | playlist->WritePlaylist(file); |
| 747 | |
| 748 | boca.DeleteComponent(playlist); |
| 749 | |
| 750 | return True; |
| 751 | } |
| 752 | |
| 753 | Bool freac::JobList::SortsAfter(const String &str1, const String &str2) |
| 754 | { |
| 755 | Int length = Math::Min(str1.Length(), str2.Length()); |
| 756 | |
| 757 | for (Int i = 0; i < length; i++) |
| 758 | { |
| 759 | if (str1[i] > str2[i]) return True; |
| 760 | else if (str1[i] < str2[i]) return False; |
| 761 | } |
| 762 | |
| 763 | if (str1.Length() > str2.Length()) return True; |
| 764 | else return False; |
| 765 | } |
| 766 | |
| 767 | Void freac::JobList::OnRegister(Widget *container) |
| 768 | { |
| 769 | container->Add(droparea); |
| 770 | container->Add(text); |
| 771 | |
| 772 | container->Add(button_sel_all); |
| 773 | container->Add(button_sel_none); |
| 774 | container->Add(button_sel_toggle); |
| 775 | |
| 776 | BoCA::Settings::Get()->onChangeConfigurationSettings.Connect(&JobList::OnChangeConfigurationSettings, this); |
| 777 | BoCA::Settings::Get()->onChangeLanguageSettings.Connect(&JobList::OnChangeLanguageSettings, this); |
| 778 | } |
| 779 | |
| 780 | Void freac::JobList::OnUnregister(Widget *container) |
| 781 | { |
| 782 | container->Remove(droparea); |
| 783 | container->Remove(text); |
| 784 | |
| 785 | container->Remove(button_sel_all); |
| 786 | container->Remove(button_sel_none); |
| 787 | container->Remove(button_sel_toggle); |
| 788 | |
| 789 | BoCA::Settings::Get()->onChangeConfigurationSettings.Disconnect(&JobList::OnChangeConfigurationSettings, this); |
| 790 | BoCA::Settings::Get()->onChangeLanguageSettings.Disconnect(&JobList::OnChangeLanguageSettings, this); |
| 791 | } |
| 792 | |
| 793 | Void freac::JobList::OnSelectEntry() |
| 794 | { |
| 795 | BoCA::JobList::Get()->onApplicationSelectTrack.Emit(GetSelectedTrack()); |
| 796 | } |
| 797 | |
| 798 | Void freac::JobList::OnMarkEntry(ListEntry *entry) |
| 799 | { |
| 800 | if (tracks.Get(entry->GetHandle()) == NIL) return; |
| 801 | |
| 802 | if (entry->IsMarked()) BoCA::JobList::Get()->onApplicationMarkTrack.Emit(tracks.Get(entry->GetHandle())); |
| 803 | else BoCA::JobList::Get()->onApplicationUnmarkTrack.Emit(tracks.Get(entry->GetHandle())); |
| 804 | } |
| 805 | |
| 806 | Void freac::JobList::OnClickTab(Int n) |
| 807 | { |
| 808 | BoCA::Config *config = BoCA::Config::Get(); |
| 809 | |
| 810 | /* Operate on a copy of the actual track list to avoid locking. |
| 811 | */ |
| 812 | Array<Track> tracks = this->tracks; |
| 813 | |
| 814 | /* Find sorting criteria. |
| 815 | */ |
| 816 | const Array<String> &fields = BoCA::Config::Get()->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldsID, Config::JoblistFieldsDefault).Explode(","); |
| 817 | |
| 818 | Bool sortByArtist = (fields.GetNth(n) == "<artist>"); |
| 819 | Bool sortByAlbum = (fields.GetNth(n) == "<album>"); |
| 820 | Bool sortByAlbumArtist = (fields.GetNth(n) == "<albumartist>"); |
| 821 | Bool sortByTitle = (fields.GetNth(n) == "<title>"); |
| 822 | Bool sortByGenre = (fields.GetNth(n) == "<genre>"); |
| 823 | Bool sortByComposer = (fields.GetNth(n) == "<composer>"); |
| 824 | Bool sortByConductor = (fields.GetNth(n) == "<conductor>"); |
| 825 | Bool sortByYear = (fields.GetNth(n) == "<year>"); |
| 826 | Bool sortByDrive = (fields.GetNth(n) == "<drive>"); |
| 827 | Bool sortByDiscID = (fields.GetNth(n) == "<discid>"); |
| 828 | Bool sortByISRC = (fields.GetNth(n) == "<isrc>"); |
| 829 | Bool sortByFile = (fields.GetNth(n) == "<file>"); |
| 830 | Bool sortByType = (fields.GetNth(n) == "<filetype>"); |
| 831 | Bool sortByOutput = (fields.GetNth(n) == "<outputfile>"); |
| 832 | Bool sortByDisc = (fields.GetNth(n) == "<disc>"); |
| 833 | Bool sortByTrack = (fields.GetNth(n) == "<track>"); |
| 834 | Bool sortByRating = (fields.GetNth(n) == "<rating>"); |
| 835 | Bool sortByTempo = (fields.GetNth(n) == "<tempo>"); |
| 836 | Bool sortByKey = (fields.GetNth(n) == "<initialkey>"); |
| 837 | Bool sortByTime = (fields.GetNth(n) == "<time>"); |
| 838 | Bool sortByBytes = (fields.GetNth(n) == "<bytes>"); |
| 839 | Bool sortByBitrate = (fields.GetNth(n) == "<bitrate>"); |
| 840 | Bool sortBySamplerate = (fields.GetNth(n) == "<samplerate>"); |
| 841 | Bool sortByChannels = (fields.GetNth(n) == "<channels>"); |
| 842 | Bool sortByResolution = (fields.GetNth(n) == "<resolution>"); |
| 843 | |
| 844 | /* Check if reverse sorting is needed. |
| 845 | */ |
| 846 | static String previousCriteria; |
| 847 | static Bool reverse = False; |
| 848 | |
| 849 | if (fields.GetNth(n) == previousCriteria) |
| 850 | { |
| 851 | /* Revert sort order previously reversed. |
| 852 | */ |
| 853 | for (Int i = 0; reverse && i < tracks.Length(); i++) MoveEntry(0, tracks.Length() - i - 1); |
| 854 | |
| 855 | reverse = !reverse; |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | previousCriteria = fields.GetNth(n); |
| 860 | |
| 861 | reverse = False; |
| 862 | } |
| 863 | |
| 864 | /* Get file types and output file names. |
| 865 | */ |
| 866 | Array<String> fileTypes; |
| 867 | Array<String> outputFileNames; |
| 868 | |
| 869 | for (Int i = 0; sortByType && i < tracks.Length(); i++) |
| 870 | { |
| 871 | const Track &track = tracks.Get(GetNthEntry(i)->GetHandle()); |
| 872 | |
| 873 | if (track.fileName.Contains("://")) fileTypes.Add(track.fileName.Head(track.fileName.Find("://")).ToUpper()); |
| 874 | else if (track.fileName.Contains(".")) fileTypes.Add(track.fileName.Tail(track.fileName.Length() - track.fileName.FindLast(".") - 1).ToUpper()); |
| 875 | } |
| 876 | |
| 877 | for (Int i = 0; sortByOutput && i < tracks.Length(); i++) |
| 878 | { |
| 879 | const Track &track = tracks.Get(GetNthEntry(i)->GetHandle()); |
| 880 | String fileName = Utilities::GetOutputFileName(config, track); |
| 881 | |
| 882 | outputFileNames.Add(fileName.Tail(fileName.Length() - config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault).Length())); |
| 883 | } |
| 884 | |
| 885 | /* Sort the list using insertion sort. |
| 886 | */ |
| 887 | for (Int i = 0; i < tracks.Length(); i++) |
| 888 | { |
| 889 | Track &thisTrack = tracks.GetReference(GetNthEntry(i)->GetHandle()); |
| 890 | Info thisInfo = thisTrack.GetInfo(); |
| 891 | const Format &thisFormat = thisTrack.GetFormat(); |
| 892 | |
| 893 | if (sortByArtist) thisInfo.artist = thisInfo.artist.ToLower(); |
| 894 | else if (sortByAlbum) thisInfo.album = thisInfo.album.ToLower(); |
| 895 | else if (sortByTitle) thisInfo.title = thisInfo.title.ToLower(); |
| 896 | else if (sortByGenre) thisInfo.genre = thisInfo.genre.ToLower(); |
| 897 | |
| 898 | else if (sortByAlbumArtist) thisInfo.SetOtherInfo(INFO_ALBUMARTIST, thisInfo.GetOtherInfo(INFO_ALBUMARTIST).Length() > 0 ? thisInfo.GetOtherInfo(INFO_ALBUMARTIST).ToLower() : thisInfo.artist.ToLower()); |
| 899 | |
| 900 | else if (sortByComposer) thisInfo.SetOtherInfo(INFO_COMPOSER, thisInfo.GetOtherInfo(INFO_COMPOSER).ToLower()); |
| 901 | else if (sortByConductor) thisInfo.SetOtherInfo(INFO_CONDUCTOR, thisInfo.GetOtherInfo(INFO_CONDUCTOR).ToLower()); |
| 902 | |
| 903 | else if (sortByFile) thisTrack.fileName = thisTrack.fileName.ToLower(); |
| 904 | |
| 905 | else if (sortByType) fileTypes.SetNth(i, fileTypes.GetNth(i).ToLower()); |
| 906 | else if (sortByOutput) outputFileNames.SetNth(i, outputFileNames.GetNth(i).ToLower()); |
| 907 | |
| 908 | thisTrack.SetInfo(thisInfo); |
| 909 | |
| 910 | /* Find position using optimized binary search. |
| 911 | */ |
| 912 | Int bottom = 0; |
| 913 | Int top = i - 1; |
| 914 | |
| 915 | while (top >= bottom) |
| 916 | { |
| 917 | /* Select element to compare optimizing for pre-sorted lists by |
| 918 | * selecting i - 1 initially before sticking to binary search. |
| 919 | */ |
| 920 | Int m = (top == i - 1 ? top : (top + bottom) / 2); |
| 921 | |
| 922 | /* Perform comparison. |
| 923 | */ |
| 924 | const Track &compTrack = tracks.Get(GetNthEntry(m)->GetHandle()); |
| 925 | const Info &compInfo = compTrack.GetInfo(); |
| 926 | const Format &compFormat = compTrack.GetFormat(); |
| 927 | |
| 928 | if ((sortByArtist && SortsAfter(compInfo.artist, thisInfo.artist) ) || |
| 929 | (sortByAlbum && SortsAfter(compInfo.album, thisInfo.album) ) || |
| 930 | (sortByTitle && SortsAfter(compInfo.title, thisInfo.title) ) || |
| 931 | (sortByGenre && SortsAfter(compInfo.genre, thisInfo.genre) ) || |
| 932 | (sortByFile && SortsAfter(compTrack.fileName, thisTrack.fileName) ) || |
| 933 | (sortByType && SortsAfter(fileTypes.GetNth(m), fileTypes.GetNth(i)) ) || |
| 934 | (sortByOutput && SortsAfter(outputFileNames.GetNth(m), outputFileNames.GetNth(i)) ) || |
| 935 | (sortByAlbumArtist && SortsAfter(compInfo.GetOtherInfo(INFO_ALBUMARTIST), thisInfo.GetOtherInfo(INFO_ALBUMARTIST)) ) || |
| 936 | (sortByComposer && SortsAfter(compInfo.GetOtherInfo(INFO_COMPOSER), thisInfo.GetOtherInfo(INFO_COMPOSER)) ) || |
| 937 | (sortByConductor && SortsAfter(compInfo.GetOtherInfo(INFO_CONDUCTOR), thisInfo.GetOtherInfo(INFO_CONDUCTOR)) ) || |
| 938 | (sortByKey && SortsAfter(compInfo.GetOtherInfo(INFO_INITIALKEY), thisInfo.GetOtherInfo(INFO_INITIALKEY)) ) || |
| 939 | (sortByISRC && SortsAfter(compInfo.isrc, thisInfo.isrc) ) || |
| 940 | (sortByDrive && compTrack.drive > thisTrack.drive ) || |
| 941 | (sortByDiscID && compTrack.discid > thisTrack.discid ) || |
| 942 | (sortByYear && compInfo.year > thisInfo.year ) || |
| 943 | (sortByDisc && compInfo.disc > thisInfo.disc ) || |
| 944 | (sortByTrack && compInfo.track > thisInfo.track ) || |
| 945 | (sortByRating && compInfo.rating > thisInfo.rating ) || |
| 946 | (sortByTempo && compInfo.GetOtherInfo(INFO_BPM).ToFloat() > thisInfo.GetOtherInfo(INFO_BPM).ToFloat() ) || |
| 947 | (sortByTime && (compTrack.length > 0 ? compTrack.length : compTrack.approxLength) / compFormat.rate > |
| 948 | (thisTrack.length > 0 ? thisTrack.length : thisTrack.approxLength) / thisFormat.rate ) || |
| 949 | (sortByBytes && compTrack.fileSize > thisTrack.fileSize ) || |
| 950 | (sortByBitrate && Float(compTrack.fileSize) / ((compTrack.length > 0 ? compTrack.length : compTrack.approxLength) / compFormat.rate) > |
| 951 | Float(thisTrack.fileSize) / ((thisTrack.length > 0 ? thisTrack.length : thisTrack.approxLength) / thisFormat.rate) ) || |
| 952 | (sortBySamplerate && compFormat.rate > thisFormat.rate ) || |
| 953 | (sortByChannels && compFormat.channels > thisFormat.channels ) || |
| 954 | (sortByResolution && compFormat.bits > thisFormat.bits )) top = m - 1; |
| 955 | else bottom = m + 1; |
| 956 | } |
| 957 | |
| 958 | /* Move element to target position. |
| 959 | */ |
| 960 | if (Math::Max(top, bottom) != i) |
| 961 | { |
| 962 | MoveEntry(i, Math::Max(top, bottom)); |
| 963 | |
| 964 | if (sortByType) fileTypes.MoveNth(i, Math::Max(top, bottom)); |
| 965 | if (sortByOutput) outputFileNames.MoveNth(i, Math::Max(top, bottom)); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /* Revert sort order if requested. |
| 970 | */ |
| 971 | for (Int i = 0; reverse && i < tracks.Length(); i++) MoveEntry(0, tracks.Length() - i - 1); |
| 972 | |
| 973 | /* Redraw joblist. |
| 974 | */ |
| 975 | Paint(SP_UPDATE); |
| 976 | } |
| 977 | |
| 978 | Void freac::JobList::OnComponentSelectTrack(const Track &track) |
| 979 | { |
| 980 | ListEntry *entry = GetEntryByTrack(track); |
| 981 | |
| 982 | if (entry != NIL && GetSelectedEntry() != entry) SelectEntry(entry); |
| 983 | } |
| 984 | |
| 985 | Void freac::JobList::OnComponentMarkTrack(const Track &track) |
| 986 | { |
| 987 | ListEntry *entry = GetEntryByTrack(track); |
| 988 | |
| 989 | if (entry != NIL) entry->SetMark(True); |
| 990 | } |
| 991 | |
| 992 | Void freac::JobList::OnComponentUnmarkTrack(const Track &track) |
| 993 | { |
| 994 | ListEntry *entry = GetEntryByTrack(track); |
| 995 | |
| 996 | if (entry != NIL) entry->SetMark(False); |
| 997 | } |
| 998 | |
| 999 | ListEntry *freac::JobList::GetEntryByTrack(const Track &track) const |
| 1000 | { |
| 1001 | for (Int i = 0; i < GetNOfTracks(); i++) |
| 1002 | { |
| 1003 | if (GetNthTrack(i).GetTrackID() == track.GetTrackID()) return GetNthEntry(i); |
| 1004 | } |
| 1005 | |
| 1006 | return NIL; |
| 1007 | } |
| 1008 | |
| 1009 | Void freac::JobList::OnChangeConfigurationSettings() |
| 1010 | { |
| 1011 | BoCA::Config *config = BoCA::Config::Get(); |
| 1012 | |
| 1013 | String headerTabsConfig = String(config->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldsID, Config::JoblistFieldsDefault)). |
| 1014 | Append(config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, Config::SettingsEncoderFilenamePatternDefault)). |
| 1015 | Append(String::FromInt(config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesAllowUnicodeID, Config::SettingsFilenamesAllowUnicodeDefault))). |
| 1016 | Append(String::FromInt(config->GetIntValue(Config::CategorySettingsID, Config::SettingsFilenamesReplaceSpacesID, Config::SettingsFilenamesReplaceSpacesDefault))); |
| 1017 | |
| 1018 | if (headerTabsConfig.ComputeCRC32() != headerTabsHash) OnChangeHeaderColumns(); |
| 1019 | |
| 1020 | headerTabsHash = headerTabsConfig.ComputeCRC32(); |
| 1021 | } |
| 1022 | |
| 1023 | Void freac::JobList::OnChangeLanguageSettings() |
| 1024 | { |
| 1025 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 1026 | |
| 1027 | i18n->SetContext("Joblist"); |
| 1028 | |
| 1029 | UpdateTextLine(); |
| 1030 | |
| 1031 | button_sel_all->SetTooltipText(i18n->TranslateString("Select all")); |
| 1032 | button_sel_none->SetTooltipText(i18n->TranslateString("Select none")); |
| 1033 | button_sel_toggle->SetTooltipText(i18n->TranslateString("Toggle selection")); |
| 1034 | |
| 1035 | Hide(); |
| 1036 | |
| 1037 | for (Int i = 0; i < GetNOfTracks(); i++) |
| 1038 | { |
| 1039 | const Track &track = GetNthTrack(i); |
| 1040 | ListEntry *entry = GetNthEntry(i); |
| 1041 | |
| 1042 | entry->SetText(GetEntryText(track)); |
| 1043 | |
| 1044 | if (BoCA::Config::Get()->GetIntValue(Config::CategorySettingsID, Config::SettingsShowTooltipsID, Config::SettingsShowTooltipsDefault)) |
| 1045 | { |
| 1046 | if (entry->GetTooltipLayer() != NIL) ((LayerTooltip *) entry->GetTooltipLayer())->UpdateFromTrack(track); |
| 1047 | else entry->SetTooltipLayer(new LayerTooltip(track)); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | AddHeaderTabs(); |
| 1052 | |
| 1053 | Show(); |
| 1054 | } |
| 1055 | |
| 1056 | Void freac::JobList::OnChangeHeaderColumns() |
| 1057 | { |
| 1058 | Surface *surface = (IsVisible() ? GetDrawSurface() : NIL); |
| 1059 | |
| 1060 | if (surface) surface->StartPaint(GetVisibleArea()); |
| 1061 | |
| 1062 | Hide(); |
| 1063 | |
| 1064 | for (Int i = 0; i < GetNOfTracks(); i++) |
| 1065 | { |
| 1066 | const Track &track = GetNthTrack(i); |
| 1067 | ListEntry *entry = GetNthEntry(i); |
| 1068 | |
| 1069 | entry->SetText(GetEntryText(track)); |
| 1070 | } |
| 1071 | |
| 1072 | AddHeaderTabs(); |
| 1073 | |
| 1074 | Show(); |
| 1075 | |
| 1076 | if (surface) surface->EndPaint(); |
| 1077 | } |
| 1078 | |
| 1079 | Void freac::JobList::AddHeaderTabs() |
| 1080 | { |
| 1081 | BoCA::Config *config = BoCA::Config::Get(); |
| 1082 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 1083 | |
| 1084 | RemoveAllTabs(); |
| 1085 | |
| 1086 | Int numAlign = i18n->IsActiveLanguageRightToLeft() ? OR_LEFT : OR_RIGHT; |
| 1087 | |
| 1088 | const Array<String> &fields = config->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldsID, Config::JoblistFieldsDefault).Explode(","); |
| 1089 | const Array<String> &sizes = config->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldSizesID, Config::JoblistFieldSizesDefault).Explode(","); |
| 1090 | |
| 1091 | for (Int i = 0; i < fields.Length(); i++) |
| 1092 | { |
| 1093 | const String &field = fields.GetNth(i); |
| 1094 | String tabName = "<invalid tab>"; |
| 1095 | Int tabAlign = OR_LEFT; |
| 1096 | Int tabSize = (fields.Length() == sizes.Length() ? sizes.GetNth(i).ToInt() : 0); |
| 1097 | |
| 1098 | if (field == "<artist>") { tabName = "Artist"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1099 | else if (field == "<album>") { tabName = "Album"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1100 | else if (field == "<albumartist>") { tabName = "Album artist"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1101 | else if (field == "<title>") { tabName = "Title"; tabSize = !config->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldsID, Config::JoblistFieldsDefault).Contains("<file>") ? 0 : |
| 1102 | tabSize <= 0 ? 180 : tabSize; } |
| 1103 | else if (field == "<genre>") { tabName = "Genre"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1104 | else if (field == "<composer>") { tabName = "Composer"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1105 | else if (field == "<conductor>") { tabName = "Conductor"; tabSize = tabSize <= 0 ? 120 : tabSize; } |
| 1106 | else if (field == "<year>") { tabName = "Year"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 50 : tabSize; } |
| 1107 | else if (field == "<tempo>") { tabName = "Tempo"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 50 : tabSize; } |
| 1108 | else if (field == "<initialkey>") { tabName = "Musical key"; tabSize = tabSize <= 0 ? 50 : tabSize; } |
| 1109 | else if (field == "<disc>") { tabName = "Disc"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 50 : tabSize; } |
| 1110 | else if (field == "<track>") { tabName = "Track"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 50 : tabSize; } |
| 1111 | else if (field == "<rating>") { tabName = "Rating"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1112 | else if (field == "<time>") { tabName = "Length"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1113 | else if (field == "<bytes>") { tabName = "Size"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1114 | else if (field == "<bitrate>") { tabName = "Bitrate"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1115 | else if (field == "<samplerate>") { tabName = "Sampling rate"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1116 | else if (field == "<channels>") { tabName = "Channels"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1117 | else if (field == "<resolution>") { tabName = "Sample resolution"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 100 : tabSize; } |
| 1118 | else if (field == "<drive>") { tabName = "CD drive"; tabSize = tabSize <= 0 ? 180 : tabSize; } |
| 1119 | else if (field == "<discid>") { tabName = "Disc ID"; tabAlign = numAlign; tabSize = tabSize <= 0 ? 80 : tabSize; } |
| 1120 | else if (field == "<isrc>") { tabName = "ISRC"; tabSize = tabSize <= 0 ? 100 : tabSize; } |
| 1121 | else if (field == "<file>") { tabName = "File name"; tabSize = 0; } |
| 1122 | else if (field == "<filetype>") { tabName = "File type"; tabSize = tabSize <= 0 ? 60 : tabSize; } |
| 1123 | else if (field == "<outputfile>") { tabName = "Output file name"; tabSize = tabSize <= 0 ? 240 : tabSize; } |
| 1124 | |
| 1125 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 1126 | |
| 1127 | i18n->SetContext("Joblist"); |
| 1128 | |
| 1129 | tabName = i18n->TranslateString(tabName); |
| 1130 | |
| 1131 | AddTab(tabName, tabSize, tabAlign); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | Void freac::JobList::UpdateTextLine() |
| 1136 | { |
| 1137 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 1138 | |
| 1139 | i18n->SetContext("Joblist"); |
| 1140 | |
| 1141 | text->SetText(i18n->AddColon(i18n->TranslateString("%1 file(s) in joblist").Replace("%1", String::FromInt(GetNOfTracks())))); |
| 1142 | } |
| 1143 | |
| 1144 | String freac::JobList::GetEntryText(const Track &track) |
| 1145 | { |
| 1146 | BoCA::Config *config = BoCA::Config::Get(); |
| 1147 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 1148 | |
| 1149 | i18n->SetContext("Joblist"); |
| 1150 | |
| 1151 | Registry &boca = Registry::Get(); |
| 1152 | Array<String> drives; |
| 1153 | |
| 1154 | const Format &format = track.GetFormat(); |
| 1155 | const Info &info = track.GetInfo(); |
| 1156 | const Array<String> &fields = config->GetStringValue(Config::CategoryJoblistID, Config::JoblistFieldsID, Config::JoblistFieldsDefault).Explode(","); |
| 1157 | |
| 1158 | String jlEntry; |
| 1159 | |
| 1160 | foreach (const String &field, fields) |
| 1161 | { |
| 1162 | if (field == "<artist>") jlEntry.Append(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist")); |
| 1163 | else if (field == "<album>") jlEntry.Append(info.album.Length() > 0 ? info.album : i18n->TranslateString("unknown album")); |
| 1164 | else if (field == "<title>") jlEntry.Append(info.title.Length() > 0 ? info.title : i18n->TranslateString("unknown title")); |
| 1165 | else if (field == "<genre>") jlEntry.Append(info.genre.Length() > 0 ? info.genre : i18n->TranslateString("unknown genre")); |
| 1166 | else if (field == "<disc>") jlEntry.Append(info.disc > 0 ? (info.disc < 10 ? String("0").Append(String::FromInt(info.disc)) : String::FromInt(info.disc)) : String()); |
| 1167 | else if (field == "<track>") jlEntry.Append(info.track > 0 ? (info.track < 10 ? String("0").Append(String::FromInt(info.track)) : String::FromInt(info.track)) : String()); |
| 1168 | else if (field == "<rating>") jlEntry.Append(info.rating >= 0 ? String().FillN(0x2605, Math::Round(info.rating / 20.0)).Append(String().FillN(0x2606, 5 - Math::Round(info.rating / 20.0))) : String()); |
| 1169 | else if (field == "<time>") jlEntry.Append(track.GetLengthString()); |
| 1170 | else if (field == "<bytes>") jlEntry.Append(track.GetFileSizeString()); |
| 1171 | |
| 1172 | else if (field == "<albumartist>") jlEntry.Append(info.GetOtherInfo(INFO_ALBUMARTIST).Length() > 0 ? info.GetOtherInfo(INFO_ALBUMARTIST) : info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown album artist")); |
| 1173 | |
| 1174 | else if (field == "<composer>") jlEntry.Append(info.GetOtherInfo(INFO_COMPOSER).Length() > 0 ? info.GetOtherInfo(INFO_COMPOSER) : i18n->TranslateString("unknown composer")); |
| 1175 | else if (field == "<conductor>") jlEntry.Append(info.GetOtherInfo(INFO_CONDUCTOR).Length() > 0 ? info.GetOtherInfo(INFO_CONDUCTOR) : i18n->TranslateString("unknown conductor")); |
| 1176 | |
| 1177 | else if (field == "<year>") jlEntry.Append(info.year > 0 ? String::FromInt(info.year) : String()); |
| 1178 | |
| 1179 | else if (field == "<tempo>") jlEntry.Append(info.GetOtherInfo(INFO_BPM)); |
| 1180 | else if (field == "<initialkey>") jlEntry.Append(info.GetOtherInfo(INFO_INITIALKEY)); |
| 1181 | |
| 1182 | else if (field == "<bitrate>") |
| 1183 | { |
| 1184 | static wchar_t sign[2] = { 0x2248, 0 }; |
| 1185 | |
| 1186 | if (track.length > 0) jlEntry.Append(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", String::FromInt((Int) Math::Round(Float(track.fileSize) / (Float(track.length) / format.rate) * 8.0 / 1000.0)))); |
| 1187 | else if (track.approxLength > 0) jlEntry.Append(sign).Append(" ").Append(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", String::FromInt((Int) Math::Round(Float(track.fileSize) / (Float(track.approxLength) / format.rate) * 8.0 / 1000.0)))); |
| 1188 | } |
| 1189 | |
| 1190 | else if (field == "<samplerate>") jlEntry.Append(i18n->TranslateString("%1 Hz", "Technical").Replace("%1", S::I18n::Number::GetLocalizedNumberString(format.rate))); |
| 1191 | else if (field == "<channels>") jlEntry.Append(format.channels > 2 ? (format.channels != 4 && format.channels != 5 && format.channels <= 8 ? String::FromInt(format.channels - 1).Append(".1") : String::FromInt(format.channels)) : (format.channels == 1 ? i18n->TranslateString("Mono") : i18n->TranslateString("Stereo"))); |
| 1192 | else if (field == "<resolution>") jlEntry.Append(i18n->TranslateString("%1 bit", "Technical").Replace("%1", String::FromInt(format.bits))); |
| 1193 | |
| 1194 | else if (field == "<drive>" && track.isCDTrack) |
| 1195 | { |
| 1196 | if (drives.Get(track.drive) == NIL) |
| 1197 | { |
| 1198 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 1199 | |
| 1200 | if (info != NIL) |
| 1201 | { |
| 1202 | const Device &device = info->GetNthDeviceInfo(track.drive); |
| 1203 | |
| 1204 | drives.Add(device.GetName(), track.drive); |
| 1205 | |
| 1206 | boca.DeleteComponent(info); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | jlEntry.Append(drives.Get(track.drive)); |
| 1211 | } |
| 1212 | |
| 1213 | else if (field == "<discid>") jlEntry.Append(CDDB::DiscIDToString(track.discid)); |
| 1214 | else if (field == "<isrc>") jlEntry.Append(info.isrc); |
| 1215 | else if (field == "<file>") jlEntry.Append(track.fileName); |
| 1216 | |
| 1217 | else if (field == "<filetype>") |
| 1218 | { |
| 1219 | if (track.fileName.Contains("://")) jlEntry.Append(track.fileName.Head(track.fileName.Find("://")).ToUpper()); |
| 1220 | else if (track.fileName.Contains(".")) jlEntry.Append(track.fileName.Tail(track.fileName.Length() - track.fileName.FindLast(".") - 1).ToUpper()); |
| 1221 | } |
| 1222 | |
| 1223 | else if (field == "<outputfile>") |
| 1224 | { |
| 1225 | String inputDirectory = track.fileName.Head(track.fileName.FindLast(Directory::GetDirectoryDelimiter()) + 1); |
| 1226 | String outputDirectory = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Config::SettingsEncoderOutputDirectoryDefault); |
| 1227 | |
| 1228 | String fileName = Utilities::GetOutputFileName(config, track); |
| 1229 | |
| 1230 | if (fileName.StartsWith(outputDirectory)) jlEntry.Append(fileName.Tail(fileName.Length() - outputDirectory.Length())); |
| 1231 | else if (fileName.StartsWith(inputDirectory)) jlEntry.Append(fileName.Tail(fileName.Length() - inputDirectory.Length())); |
| 1232 | } |
| 1233 | |
| 1234 | jlEntry.Append(ListEntry::tabDelimiter); |
| 1235 | } |
| 1236 | |
| 1237 | return jlEntry; |
| 1238 | } |