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