summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/column_wordcount.cpp
blob: e764e378de84a876fc97925bbc09afc5d1c6479d (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "stdafx.h"
#include "column_wordcount.h"

#include <algorithm>

/*
 * ColWordCount
 */

ColWordCount::ColWordCount()
	: m_nVisMode(0), m_bDetail(true),
	m_hVisMode(nullptr), m_hDetail(nullptr)
{
}

void ColWordCount::impl_copyConfig(const Column* pSource)
{
	ColBaseWords::impl_copyConfig(pSource);

	const ColWordCount& src = *reinterpret_cast<const ColWordCount*>(pSource);

	m_nVisMode = src.m_nVisMode;
	m_bDetail  = src.m_bDetail;
}

void ColWordCount::impl_configRead(const SettingsTree& settings)
{
	ColBaseWords::impl_configRead(settings);

	m_nVisMode = settings.readIntRanged(con::KeyVisMode, 0, 0, 2);
	m_bDetail  = settings.readBool(con::KeyDetail, true);
}

void ColWordCount::impl_configWrite(SettingsTree& settings) const
{
	ColBaseWords::impl_configWrite(settings);

	settings.writeInt (con::KeyVisMode, m_nVisMode);
	settings.writeBool(con::KeyDetail, m_bDetail);
}

void ColWordCount::impl_configToUI(OptionsCtrl& Opt, OptionsCtrl::Item hGroup)
{
	ColBaseWords::impl_configToUI(Opt, hGroup);

	OptionsCtrl::Group hTemp;
	
	hTemp         = Opt.insertGroup(hGroup, TranslateT("Word count type"));
		m_hVisMode = Opt.insertRadio(hTemp, nullptr, TranslateT("Total words"));
		             Opt.insertRadio(hTemp, m_hVisMode, TranslateT("Distinct words"));
		             Opt.insertRadio(hTemp, m_hVisMode, TranslateT("Ratio total/distinct words"));
	m_hDetail     = Opt.insertCheck(hGroup, TranslateT("Additional info in tooltip (depends on type)"));

	Opt.setRadioChecked(m_hVisMode, m_nVisMode);
	Opt.checkItem      (m_hDetail , m_bDetail );
}

void ColWordCount::impl_configFromUI(OptionsCtrl& Opt)
{
	ColBaseWords::impl_configFromUI(Opt);

	m_nVisMode = Opt.getRadioChecked(m_hVisMode);
	m_bDetail  = Opt.isItemChecked  (m_hDetail );
}

void ColWordCount::impl_contactDataFree(Contact& contact) const
{
	ColBaseWords::impl_contactDataFree(contact);

	size_t* pTrData = reinterpret_cast<size_t*>(contact.getSlot(contactDataTransformSlotGet()));

	if (pTrData)
	{
		delete pTrData;
		contact.setSlot(contactDataTransformSlotGet(), nullptr);
	}
}

void ColWordCount::impl_contactDataTransform(Contact& contact) const
{
	WordMap* pData = reinterpret_cast<WordMap*>(contact.getSlot(contactDataSlotGet()));
	size_t* pTrData = new size_t[2];

	contact.setSlot(contactDataTransformSlotGet(), pTrData);

	// total words
	pTrData[0] = 0;

	citer_each_(WordMap, word, *pData)
	{
		pTrData[0] += word->second.total();
	}

	// distinct words
	pTrData[1] = pData->size();
}

void ColWordCount::impl_contactDataTransformCleanup(Contact& contact) const
{
	WordMap* pData = reinterpret_cast<WordMap*>(contact.getSlot(contactDataSlotGet()));

	if (pData)
	{
		pData->clear();

		delete[] pData;
		contact.setSlot(contactDataSlotGet(), nullptr);
	}
}

void ColWordCount::impl_outputRenderHeader(ext::ostream& tos, int row, int rowSpan) const
{
	static const wchar_t* szTypeDesc[] = {
		LPGENW("Total word count"),
		LPGENW("Distinct word count"),
		LPGENW("Ratio total/distinct words")
	};

	static const wchar_t* szSourceDesc[] = {
		LPGENW("incoming messages"),
		LPGENW("outgoing messages"),
		LPGENW("all messages"),
	};

	if (row == 1)
	{
		ext::string strTitle = str(ext::kformat(TranslateT("#{type} for #{data}"))
			% L"#{type}" * TranslateW(szTypeDesc[m_nVisMode])
			% L"#{data}" * TranslateW(szSourceDesc[m_nSource]));

		writeRowspanTD(tos, getCustomTitle(TranslateW(szTypeDesc[m_nVisMode]), strTitle), row, 1, rowSpan);
	}
}

void ColWordCount::impl_outputRenderRow(ext::ostream& tos, const Contact& contact, DisplayType)
{
	const size_t* pWordCount = reinterpret_cast<const size_t*>(contact.getSlot(contactDataTransformSlotGet()));

	switch (m_nVisMode)
	{
		case 0:
			{
				if (!m_bDetail)
				{
					tos << L"<td class=\"num\">"
						<< utils::intToGrouped(pWordCount[0])
						<< L"</td>" << ext::endl;
				}
				else
				{
					tos << L"<td class=\"num\" title=\""
						<< utils::htmlEscape(ext::str(ext::kformat(TranslateT("#{distict_words} distinct"))
							% L"#{distict_words}" * utils::intToGrouped(pWordCount[1])))
						<< L"\">"
						<< utils::intToGrouped(pWordCount[0])
						<< L"</td>" << ext::endl;
				}
			}
			break;

		case 1:
			{
				if (!m_bDetail)
				{
					tos << L"<td class=\"num\">"
						<< utils::intToGrouped(pWordCount[1])
						<< L"</td>" << ext::endl;
				}
				else
				{
					tos << L"<td class=\"num\" title=\""
						<< utils::htmlEscape(ext::str(ext::kformat(TranslateT("#{words} total"))
							% L"#{words}" * utils::intToGrouped(pWordCount[0])))
						<< L"\">"
						<< utils::intToGrouped(pWordCount[1])
						<< L"</td>" << ext::endl;
				}
			}
			break;

		default:
			{
				if (!m_bDetail)
				{
					tos << L"<td class=\"num\">"
						<< utils::ratioToString(pWordCount[0], pWordCount[1], 2)
						<< L"</td>" << ext::endl;
				}
				else
				{
					tos << L"<td class=\"num\" title=\""
						<< utils::htmlEscape(ext::str(ext::kformat(TranslateT("#{words} total / #{distict_words} distinct"))
							% L"#{words}" * utils::intToGrouped(pWordCount[0])
							% L"#{distict_words}" * utils::intToGrouped(pWordCount[1])))
						<< L"\">"
						<< utils::ratioToString(pWordCount[0], pWordCount[1], 2)
						<< L"</td>" << ext::endl;
				}
			}
			break;
	}
}