layout_document_generic_preview.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "layout/layout_document_generic_preview.h"
  8. #include "data/data_document.h"
  9. #include "lang/lang_keys.h"
  10. #include "styles/style_media_view.h"
  11. namespace Layout {
  12. const style::icon *DocumentGenericPreview::icon() const {
  13. switch (index) {
  14. case 0: return &st::mediaviewFileBlue;
  15. case 1: return &st::mediaviewFileGreen;
  16. case 2: return &st::mediaviewFileRed;
  17. case 3: return &st::mediaviewFileYellow;
  18. }
  19. Unexpected("Color index in DocumentGenericPreview::icon.");
  20. }
  21. DocumentGenericPreview DocumentGenericPreview::Create(
  22. DocumentData *document) {
  23. auto colorIndex = 0;
  24. const auto name = (document
  25. ? (document->filename().isEmpty()
  26. ? (document->sticker()
  27. ? tr::lng_in_dlg_sticker(tr::now)
  28. : u"Unknown File"_q)
  29. : document->filename())
  30. : tr::lng_message_empty(tr::now)).toLower();
  31. auto lastDot = name.lastIndexOf('.');
  32. const auto mime = document ? document->mimeString() : QString();
  33. if (name.endsWith(u".doc"_q) ||
  34. name.endsWith(u".docx"_q) ||
  35. name.endsWith(u".txt"_q) ||
  36. name.endsWith(u".psd"_q) ||
  37. mime.startsWith(u"text/"_q)) {
  38. colorIndex = 0;
  39. } else if (
  40. name.endsWith(u".xls"_q) ||
  41. name.endsWith(u".xlsx"_q) ||
  42. name.endsWith(u".csv"_q)) {
  43. colorIndex = 1;
  44. } else if (
  45. name.endsWith(u".pdf"_q) ||
  46. name.endsWith(u".ppt"_q) ||
  47. name.endsWith(u".pptx"_q) ||
  48. name.endsWith(u".key"_q)) {
  49. colorIndex = 2;
  50. } else if (
  51. name.endsWith(u".zip"_q) ||
  52. name.endsWith(u".rar"_q) ||
  53. name.endsWith(u".ai"_q) ||
  54. name.endsWith(u".mp3"_q) ||
  55. name.endsWith(u".mov"_q) ||
  56. name.endsWith(u".avi"_q)) {
  57. colorIndex = 3;
  58. } else {
  59. auto ch = (lastDot >= 0 && lastDot + 1 < name.size())
  60. ? name.at(lastDot + 1)
  61. : (name.isEmpty()
  62. ? (mime.isEmpty() ? '0' : mime.at(0))
  63. : name.at(0));
  64. colorIndex = (ch.unicode() % 4) & 3;
  65. }
  66. const auto ext = document
  67. ? ((lastDot < 0 || lastDot + 2 > name.size())
  68. ? name
  69. : name.mid(lastDot + 1))
  70. : QString();
  71. switch (colorIndex) {
  72. case 0: return {
  73. .index = colorIndex,
  74. .color = st::msgFile1Bg,
  75. .dark = st::msgFile1BgDark,
  76. .over = st::msgFile1BgOver,
  77. .selected = st::msgFile1BgSelected,
  78. .ext = ext,
  79. };
  80. case 1: return {
  81. .index = colorIndex,
  82. .color = st::msgFile2Bg,
  83. .dark = st::msgFile2BgDark,
  84. .over = st::msgFile2BgOver,
  85. .selected = st::msgFile2BgSelected,
  86. .ext = ext,
  87. };
  88. case 2: return {
  89. .index = colorIndex,
  90. .color = st::msgFile3Bg,
  91. .dark = st::msgFile3BgDark,
  92. .over = st::msgFile3BgOver,
  93. .selected = st::msgFile3BgSelected,
  94. .ext = ext,
  95. };
  96. case 3: return {
  97. .index = colorIndex,
  98. .color = st::msgFile4Bg,
  99. .dark = st::msgFile4BgDark,
  100. .over = st::msgFile4BgOver,
  101. .selected = st::msgFile4BgSelected,
  102. .ext = ext,
  103. };
  104. }
  105. Unexpected("Color index in CreateDocumentGenericPreview.");
  106. }
  107. // Ui::CachedRoundCorners DocumentCorners(int32 colorIndex) {
  108. // return Ui::CachedRoundCorners(Ui::Doc1Corners + (colorIndex & 3));
  109. // }
  110. } // namespace Layout