format_song_name.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/text/format_song_name.h"
  8. namespace Ui::Text {
  9. namespace {
  10. FormatSongName::ComposedName ComputeComposedName(
  11. const QString &filename,
  12. const QString &songTitle,
  13. const QString &songPerformer) {
  14. const auto unknown = u"Unknown Track"_q;
  15. if (songTitle.isEmpty() && songPerformer.isEmpty()) {
  16. return {
  17. .title = filename.isEmpty() ? unknown : filename,
  18. .performer = QString(),
  19. };
  20. }
  21. if (songPerformer.isEmpty()) {
  22. return {
  23. .title = songTitle,
  24. .performer = QString(),
  25. };
  26. }
  27. return {
  28. .title = (songTitle.isEmpty() ? unknown : songTitle),
  29. .performer = songPerformer,
  30. };
  31. }
  32. } // namespace
  33. FormatSongName::FormatSongName(
  34. const QString &filename,
  35. const QString &songTitle,
  36. const QString &songPerformer)
  37. : _composedName(ComputeComposedName(filename, songTitle, songPerformer)) {
  38. }
  39. FormatSongName::ComposedName FormatSongName::composedName() const {
  40. return _composedName;
  41. }
  42. QString FormatSongName::string() const {
  43. const auto &[title, performer] = _composedName;
  44. const auto dash = (title.isEmpty() || performer.isEmpty())
  45. ? QString()
  46. : QString::fromUtf8(" \xe2\x80\x93 ");
  47. return performer + dash + title;
  48. }
  49. TextWithEntities FormatSongName::textWithEntities(
  50. bool boldOnlyPerformer) const {
  51. TextWithEntities result;
  52. result.text = string();
  53. if (!boldOnlyPerformer || !_composedName.performer.isEmpty()) {
  54. result.entities.push_back({
  55. EntityType::Semibold,
  56. 0,
  57. _composedName.performer.isEmpty()
  58. ? int(result.text.size())
  59. : int(_composedName.performer.size()),
  60. });
  61. }
  62. return result;
  63. }
  64. } // namespace Ui::Text