-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathlayout_document_generic_preview.cpp
More file actions
144 lines (131 loc) · 3.74 KB
/
Copy pathlayout_document_generic_preview.cpp
File metadata and controls
144 lines (131 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "layout/layout_document_generic_preview.h"
#include "data/data_document.h"
#include "lang/lang_keys.h"
#include "styles/style_media_view.h"
namespace Layout {
namespace {
[[nodiscard]] int ColorIndexForName(
const QString &name,
const QString &mime = QString()) {
const auto lastDot = name.lastIndexOf('.');
if (name.endsWith(u".doc"_q) ||
name.endsWith(u".docx"_q) ||
name.endsWith(u".txt"_q) ||
name.endsWith(u".psd"_q) ||
mime.startsWith(u"text/"_q)) {
return 0;
} else if (
name.endsWith(u".xls"_q) ||
name.endsWith(u".xlsx"_q) ||
name.endsWith(u".csv"_q)) {
return 1;
} else if (
name.endsWith(u".pdf"_q) ||
name.endsWith(u".ppt"_q) ||
name.endsWith(u".pptx"_q) ||
name.endsWith(u".key"_q)) {
return 2;
} else if (
name.endsWith(u".zip"_q) ||
name.endsWith(u".rar"_q) ||
name.endsWith(u".ai"_q) ||
name.endsWith(u".mp3"_q) ||
name.endsWith(u".mov"_q) ||
name.endsWith(u".avi"_q)) {
return 3;
}
const auto ch = (lastDot >= 0 && lastDot + 1 < name.size())
? name.at(lastDot + 1)
: (name.isEmpty()
? (mime.isEmpty() ? QChar('0') : mime.at(0))
: name.at(0));
return (ch.unicode() % 4) & 3;
}
[[nodiscard]] DocumentGenericPreview BuildFromColorIndex(
int colorIndex,
const QString &ext) {
switch (colorIndex) {
case 0: return {
.index = colorIndex,
.color = st::msgFile1Bg,
.dark = st::msgFile1BgDark,
.over = st::msgFile1BgOver,
.selected = st::msgFile1BgSelected,
.ext = ext,
};
case 1: return {
.index = colorIndex,
.color = st::msgFile2Bg,
.dark = st::msgFile2BgDark,
.over = st::msgFile2BgOver,
.selected = st::msgFile2BgSelected,
.ext = ext,
};
case 2: return {
.index = colorIndex,
.color = st::msgFile3Bg,
.dark = st::msgFile3BgDark,
.over = st::msgFile3BgOver,
.selected = st::msgFile3BgSelected,
.ext = ext,
};
case 3: return {
.index = colorIndex,
.color = st::msgFile4Bg,
.dark = st::msgFile4BgDark,
.over = st::msgFile4BgOver,
.selected = st::msgFile4BgSelected,
.ext = ext,
};
}
Unexpected("Color index in BuildFromColorIndex.");
}
} // namespace
const style::icon *DocumentGenericPreview::icon() const {
switch (index) {
case 0: return &st::mediaviewFileBlue;
case 1: return &st::mediaviewFileGreen;
case 2: return &st::mediaviewFileRed;
case 3: return &st::mediaviewFileYellow;
}
Unexpected("Color index in DocumentGenericPreview::icon.");
}
DocumentGenericPreview DocumentGenericPreview::Create(
DocumentData *document) {
const auto name = (document
? (document->filename().isEmpty()
? (document->sticker()
? tr::lng_in_dlg_sticker(tr::now)
: u"Unknown File"_q)
: document->filename())
: tr::lng_message_empty(tr::now)).toLower();
const auto mime = document ? document->mimeString() : QString();
const auto colorIndex = ColorIndexForName(name, mime);
const auto lastDot = name.lastIndexOf('.');
const auto ext = document
? ((lastDot < 0 || lastDot + 2 > name.size())
? name
: name.mid(lastDot + 1))
: QString();
return BuildFromColorIndex(colorIndex, ext);
}
DocumentGenericPreview DocumentGenericPreview::Create(
const QString &filename) {
const auto name = filename.toLower();
const auto colorIndex = ColorIndexForName(name);
const auto lastDot = name.lastIndexOf('.');
const auto ext = (lastDot < 0 || lastDot + 2 > name.size())
? name
: name.mid(lastDot + 1);
return BuildFromColorIndex(colorIndex, ext);
}
// Ui::CachedRoundCorners DocumentCorners(int32 colorIndex) {
// return Ui::CachedRoundCorners(Ui::Doc1Corners + (colorIndex & 3));
// }
} // namespace Layout