summaryrefslogtreecommitdiff
path: root/plugins/IEView/ChatHTMLBuilder.cpp
blob: 22ab1730facb160f49d962acb974e6af48d6cfde (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*

IEView Plugin for Miranda IM
Copyright (C) 2005-2010  Piotr Piastucki

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "ChatHTMLBuilder.h"

#include "Options.h"
#include "Utils.h"
#include "m_chat.h"

#define CHATMOD 			"Chat"
#define CHATFONTMOD 		"ChatFonts"
#define CHAT_ICON_FLAGS		"IconFlags"

#define FONTF_BOLD   1
#define FONTF_ITALIC 2
#define FONTF_UNDERLINE 4

#define FONT_NUM 17

static const char *classNames[] = {
	".timestamp", ".nameIn", ".nameOut", ".userJoined", ".userLeft", ".userDisconnected",
	".userKicked", ".nickChange", ".notice",
	".messageIn", ".messageOut", ".topicChange", ".information", ".statusEnable", ".statusDisable",
	".action", ".highlight"
};

ChatHTMLBuilder::ChatHTMLBuilder() {
	setLastEventType(-1);
	setLastEventTime(time(NULL));
}

void ChatHTMLBuilder::loadMsgDlgFont(int i, LOGFONTA * lf, COLORREF * colour) {
	char str[32];
	int style;
	DBVARIANT dbv;
	if (colour) {
		wsprintfA(str, "Font%dCol", i);
		*colour = DBGetContactSettingDword(NULL, CHATFONTMOD, str, 0x000000);
	}
	if (lf) {
		wsprintfA(str, "Font%dSize", i);
		lf->lfHeight = (char) DBGetContactSettingByte(NULL, CHATFONTMOD, str, 10);
		lf->lfHeight = abs(lf->lfHeight);
		lf->lfWidth = 0;
		lf->lfEscapement = 0;
		lf->lfOrientation = 0;
		wsprintfA(str, "Font%dSty", i);
		style = DBGetContactSettingByte(NULL, CHATFONTMOD, str, 0);
		lf->lfWeight = style & FONTF_BOLD ? FW_BOLD : FW_NORMAL;
		lf->lfItalic = style & FONTF_ITALIC ? 1 : 0;
		lf->lfUnderline = style & FONTF_UNDERLINE ? 1 : 0;
		lf->lfStrikeOut = 0;
		wsprintfA(str, "Font%dSet", i);
		lf->lfCharSet = DBGetContactSettingByte(NULL, CHATFONTMOD, str, DEFAULT_CHARSET);
		lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
		lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
		lf->lfQuality = DEFAULT_QUALITY;
		lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
		wsprintfA(str, "Font%d", i);
		if (DBGetContactSetting(NULL, CHATFONTMOD, str, &dbv))
			lstrcpyA(lf->lfFaceName, "Verdana");
		else {
			lstrcpynA(lf->lfFaceName, dbv.pszVal, sizeof(lf->lfFaceName));
			DBFreeVariant(&dbv);
		}
	}
}

char *ChatHTMLBuilder::timestampToString(time_t time)
{
	static char szResult[512];
	static char str[80];
	char *pszStamp = "[%H:%M]";
	//InitSetting( &g_Settings.pszTimeStamp, "HeaderTime", _T("[%H:%M]"));
	strftime(str, 79, pszStamp, localtime(&time));
	Utils::UTF8Encode(str, szResult, 500);
	return szResult;
}

void ChatHTMLBuilder::buildHead(IEView *view, IEVIEWEVENT *event) {
	LOGFONTA lf;
	COLORREF color;
	char *output = NULL;
	int outputSize;
	ProtocolSettings *protoSettings = getChatProtocolSettings(event->pszProto);
	if (protoSettings == NULL) {
		return;
	}
	if (protoSettings->getChatMode() == Options::MODE_TEMPLATE) {
//		buildHeadTemplate(view, event);
		return;
	}
	if (protoSettings->getChatMode() == Options::MODE_CSS) {
		const char *externalCSS = protoSettings->getChatCssFilename();
		Utils::appendText(&output, &outputSize, "<html><head><link rel=\"stylesheet\" href=\"%s\"/></head><body class=\"body\">\n", externalCSS);
	} else {
		HDC hdc = GetDC(NULL);
		int logPixelSY = GetDeviceCaps(hdc, LOGPIXELSY);
		ReleaseDC(NULL, hdc);
		Utils::appendText(&output, &outputSize, "<html><head>");
		Utils::appendText(&output, &outputSize, "<style type=\"text/css\">\n");
		COLORREF bkgColor = DBGetContactSettingDword(NULL, CHATMOD, "BackgroundLog", 0xFFFFFF);
		COLORREF inColor, outColor;
		bkgColor= (((bkgColor & 0xFF) << 16) | (bkgColor & 0xFF00) | ((bkgColor & 0xFF0000) >> 16));
		inColor = outColor = bkgColor;
		if (protoSettings->getChatFlags() & Options::LOG_IMAGE_ENABLED) {
			Utils::appendText(&output, &outputSize, ".body {padding: 2px; text-align: left; background-attachment: %s; background-color: #%06X;  background-image: url('%s'); overflow: auto;}\n",
			protoSettings->getChatFlags() & Options::LOG_IMAGE_SCROLL ? "scroll" : "fixed", (int) bkgColor, protoSettings->getChatBackgroundFilename());
		} else {
			Utils::appendText(&output, &outputSize, ".body {margin: 0px; text-align: left; background-color: #%06X; overflow: auto;}\n",
						(int) bkgColor);
		}
		Utils::appendText(&output, &outputSize, ".link {color: #0000FF; text-decoration: underline;}\n");
		Utils::appendText(&output, &outputSize, ".img {vertical-align: middle;}\n");
		if (protoSettings->getChatFlags() & Options::LOG_IMAGE_ENABLED) {
			Utils::appendText(&output, &outputSize, ".divIn {padding-left: 2px; padding-right: 2px; word-wrap: break-word;}\n");
			Utils::appendText(&output, &outputSize, ".divOut {padding-left: 2px; padding-right: 2px; word-wrap: break-word;}\n");
		} else {
			Utils::appendText(&output, &outputSize, ".divIn {padding-left: 2px; padding-right: 2px; word-wrap: break-word; background-color: #%06X;}\n", (int) inColor);
			Utils::appendText(&output, &outputSize, ".divOut {padding-left: 2px; padding-right: 2px; word-wrap: break-word; background-color: #%06X;}\n", (int) outColor);
		}
	 	for(int i = 0; i < FONT_NUM; i++) {
			loadMsgDlgFont(i, &lf, &color);
			Utils::appendText(&output, &outputSize, "%s {font-family: %s; font-size: %dpt; font-weight: %s; color: #%06X; %s }\n",
			classNames[i],
			lf.lfFaceName,
			abs((signed char)lf.lfHeight) *  74 /logPixelSY ,
			lf.lfWeight >= FW_BOLD ? "bold" : "normal",
			(int)(((color & 0xFF) << 16) | (color & 0xFF00) | ((color & 0xFF0000) >> 16)),
			lf.lfItalic ? "font-style: italic;" : "");
		}
		Utils::appendText(&output, &outputSize, "</style></head><body class=\"body\">\n");
	}
	if (output != NULL) {
		view->write(output);
		free(output);
	}
	setLastEventType(-1);
}

/* WORK IN PROGRESS:
 * The following method is going to be completely rewritten soon. Do not modify or complain for the time being...
 */

void ChatHTMLBuilder::appendEventNonTemplate(IEView *view, IEVIEWEVENT *event) {
	DWORD iconFlags = DBGetContactSettingDword(NULL, CHATMOD, CHAT_ICON_FLAGS, 0);
	IEVIEWEVENTDATA* eventData = event->eventData;
	for (int eventIdx = 0; eventData!=NULL && (eventIdx < event->count || event->count==-1); eventData = eventData->next, eventIdx++) {
		//DWORD dwFlags = eventData->dwFlags;
		const char *iconFile = "";
		DWORD dwData = eventData->dwData;
		int isSent = eventData->bIsMe;
		int outputSize = 0;
		char *output = NULL;
		char *szName = NULL, *szText = NULL;
		const char *className = "";
		bool showIcon = false;

		if (eventData->dwFlags & IEEDF_UNICODE_TEXT) {
			szText = encodeUTF8(NULL, event->pszProto, eventData->pszTextW, ENF_ALL | ENF_CHAT_FORMATTING, isSent);
		} else {
			szText = encodeUTF8(NULL, event->pszProto, (char *)eventData->pszText, ENF_ALL | ENF_CHAT_FORMATTING, isSent);
		}
		if (eventData->dwFlags & IEEDF_UNICODE_NICK) {
			szName = encodeUTF8(NULL, event->pszProto, eventData->pszNickW, ENF_NAMESMILEYS, true);
		} else {
			szName = encodeUTF8(NULL, event->pszProto, (char *) eventData->pszNick, ENF_NAMESMILEYS, true);
		}
		if (eventData->iType == IEED_GC_EVENT_MESSAGE) {
			iconFile = isSent ? "message_out_chat.gif" : "message_in_chat.gif";
			showIcon = iconFlags & (isSent ? GC_EVENT_MESSAGE : GC_EVENT_MESSAGE);
			className = isSent ? "messageOut" : "messageIn";
		} else {
			if (eventData->iType == IEED_GC_EVENT_ACTION) {
				iconFile = "action.gif";
				className = "action";
			} else if (eventData->iType == IEED_GC_EVENT_JOIN) {
				iconFile = "join.gif";
				className = "userJoined";
			} else if (eventData->iType == IEED_GC_EVENT_PART) {
				iconFile = "part.gif";
				className = "userLeft";
			} else if (eventData->iType == IEED_GC_EVENT_QUIT) {
				iconFile = "quit.gif";
				className = "userDisconnected";
			} else if (eventData->iType == IEED_GC_EVENT_NICK) {
				iconFile = "nick.gif";
				className = "nickChange";
			} else if (eventData->iType == IEED_GC_EVENT_KICK) {
				iconFile = "kick.gif";
				className = "userKicked";
			} else if (eventData->iType == IEED_GC_EVENT_NOTICE) {
				iconFile = "notice.gif";
				className = "notice";
			} else if (eventData->iType == IEED_GC_EVENT_TOPIC) {
				iconFile = "topic.gif";
				className = "topicChange";
			} else if (eventData->iType == IEED_GC_EVENT_ADDSTATUS) {
				iconFile = "addstatus.gif";
				className = "statusEnable";
			} else if (eventData->iType == IEED_GC_EVENT_REMOVESTATUS) {
				iconFile = "removestatus.gif";
				className = "statusDisable";
			} else if (eventData->iType == IEED_GC_EVENT_INFORMATION) {
				iconFile = "info.gif";
				className = "information";
			}
		}
		Utils::appendText(&output, &outputSize, "<div class=\"%s\">", isSent ? "divOut" : "divIn");
		if (dwData & IEEDD_GC_SHOW_ICON) {
			Utils::appendIcon(&output, &outputSize, iconFile);
		}
		if (dwData & IEEDD_GC_SHOW_TIME) {
			Utils::appendText(&output, &outputSize, "<span class=\"%s\">%s </span>",
						isSent ? "timestamp" : "timestamp", timestampToString(eventData->time));
		}
		if ((dwData & IEEDD_GC_SHOW_NICK) && eventData->iType == IEED_GC_EVENT_MESSAGE) {
			Utils::appendText(&output, &outputSize, "<span class=\"%s\">%s: </span>",
						isSent ? "nameOut" : "nameIn", szName);
		}
		if (dwData & IEEDD_GC_MSG_ON_NEW_LINE) {
			Utils::appendText(&output, &outputSize, "<br>");
		}
		Utils::appendText(&output, &outputSize, "<span class=\"%s\">", className);
		Utils::appendText(&output, &outputSize, "%s", szText);
		Utils::appendText(&output, &outputSize, "</span></div>\n");
		if (output != NULL) {
			view->write(output);
			free(output);
		}
		if (szName!=NULL) delete szName;
		if (szText!=NULL) delete szText;
	}
}

void ChatHTMLBuilder::appendEvent(IEView *view, IEVIEWEVENT *event) {
	ProtocolSettings *protoSettings = getChatProtocolSettings(event->pszProto);
	if (protoSettings == NULL) {
		return;
	}
// 	if (protoSettings->getSRMMMode() == Options::MODE_TEMPLATE) {
	//	appendEventTemplate(view, event);
//	} else {
		appendEventNonTemplate(view, event);
//	}
}

bool ChatHTMLBuilder::isDbEventShown(DBEVENTINFO * dbei) {
	return true;
}