File: layout_document_generic_preview.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (120 lines) | stat: -rw-r--r-- 3,147 bytes parent folder | download | duplicates (3)
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
/*
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 {

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) {
	auto colorIndex = 0;

	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();
	auto lastDot = name.lastIndexOf('.');
	const auto mime = document ? document->mimeString() : QString();
	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)) {
		colorIndex = 0;
	} else if (
		name.endsWith(u".xls"_q) ||
		name.endsWith(u".xlsx"_q) ||
		name.endsWith(u".csv"_q)) {
		colorIndex = 1;
	} else if (
		name.endsWith(u".pdf"_q) ||
		name.endsWith(u".ppt"_q) ||
		name.endsWith(u".pptx"_q) ||
		name.endsWith(u".key"_q)) {
		colorIndex = 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)) {
		colorIndex = 3;
	} else {
		auto ch = (lastDot >= 0 && lastDot + 1 < name.size())
			? name.at(lastDot + 1)
			: (name.isEmpty()
				? (mime.isEmpty() ? '0' : mime.at(0))
				: name.at(0));
		colorIndex = (ch.unicode() % 4) & 3;
	}

	const auto ext = document
		? ((lastDot < 0 || lastDot + 2 > name.size())
			? name
			: name.mid(lastDot + 1))
		: QString();

	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 CreateDocumentGenericPreview.");
}

// Ui::CachedRoundCorners DocumentCorners(int32 colorIndex) {
// 	return Ui::CachedRoundCorners(Ui::Doc1Corners + (colorIndex & 3));
// }

} // namespace Layout