summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/chat_main.cpp
blob: 1dc5e20f7eee0fb3a7452ee30181b6bdab0d74ee (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
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (c) 2012-18 Miranda NG team,
// Copyright (c) 2000-09 Miranda ICQ/IM project,
// all portions of this codebase are copyrighted to the people
// listed in contributors.txt.
//
// 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.
//
// part of tabSRMM messaging plugin for Miranda.
//
// (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
//
// chat module exports and functions to load/unload the plugin.

#include "stdafx.h"

HANDLE g_hWindowList;

pfnDoPopup oldDoPopup, oldLogToFile;
pfnDoTrayIcon oldDoTrayIcon;

TMUCSettings g_Settings;

static void OnCreateSession(SESSION_INFO *si, MODULEINFO *mi)
{
	if (si->hContact)
		Chat_SetFilters(si);
	if (si->pDlg)
		si->pDlg->UpdateStatusBar();

	if (mi)
		mi->idleTimeStamp = time(0);
}

static void OnReplaceSession(SESSION_INFO *si)
{
	if (si->hContact)
		Chat_SetFilters(si);
	if (si->pDlg)
		RedrawWindow(GetDlgItem(si->pDlg->GetHwnd(), IDC_SRMM_NICKLIST), nullptr, nullptr, RDW_INVALIDATE);
}

static void OnSetTopic(SESSION_INFO *si)
{
	if (si->pDlg)
		si->pDlg->m_pPanel.Invalidate(true);
}

static void OnNewUser(SESSION_INFO *si, USERINFO*)
{
	if (si->pDlg)
		si->pDlg->GetMyNick();
}

static void OnChangeNick(SESSION_INFO *si)
{
	if (si->pDlg) {
		si->pDlg->GetMyNick();
		si->pDlg->UpdateStatusBar();
	}
}

static void OnCreateModule(MODULEINFO *mi)
{
	mi->idleTimeStamp = time(0);
}

static void OnLoadSettings()
{
	g_Settings.dwIconFlags = M.GetDword(CHAT_MODULE, "IconFlags", 0x0000);
	g_Settings.bOpenInDefault = M.GetBool(CHAT_MODULE, "DefaultContainer", true);
	g_Settings.UserListColors[CHAT_STATUS_NORMAL] = M.GetDword(CHATFONT_MODULE, "Font18Col", RGB(0, 0, 0));
	g_Settings.UserListColors[CHAT_STATUS_AWAY] = M.GetDword(CHATFONT_MODULE, "Font19Col", RGB(170, 170, 170));
	g_Settings.UserListColors[CHAT_STATUS_OFFLINE] = M.GetDword(CHATFONT_MODULE, "Font5Col", RGB(160, 90, 90));
	g_Settings.bBBCodeInPopups = M.GetBool(CHAT_MODULE, "BBCodeInPopups", false);
	g_Settings.bClassicIndicators = M.GetBool(CHAT_MODULE, "ClassicIndicators", false);
	g_Settings.bLogClassicIndicators = M.GetBool(CHAT_MODULE, "LogClassicIndicators", false);
	g_Settings.bAlternativeSorting = M.GetBool(CHAT_MODULE, "AlternativeSorting", true);
	g_Settings.bAnnoyingHighlight = M.GetBool(CHAT_MODULE, "AnnoyingHighlight", false);
	g_Settings.bCreateWindowOnHighlight = M.GetBool(CHAT_MODULE, "CreateWindowOnHighlight", true);

	g_Settings.bLogSymbols = M.GetBool(CHAT_MODULE, "LogSymbols", true);
	g_Settings.bClickableNicks = M.GetBool(CHAT_MODULE, "ClickableNicks", true);
	g_Settings.bColorizeNicks = M.GetBool(CHAT_MODULE, "ColorizeNicks", true);
	g_Settings.bColorizeNicksInLog = M.GetBool(CHAT_MODULE, "ColorizeNicksInLog", true);
	g_Settings.bScaleIcons = M.GetBool(CHAT_MODULE, "ScaleIcons", true);

	g_Settings.bDoubleClick4Privat = M.GetBool(CHAT_MODULE, "DoubleClick4Privat", false);
	g_Settings.bShowContactStatus = M.GetBool(CHAT_MODULE, "ShowContactStatus", true);
	g_Settings.bContactStatusFirst = M.GetBool(CHAT_MODULE, "ContactStatusFirst", false);

	g_Settings.bNewLineAfterNames = M.GetBool(CHAT_MODULE, "NewlineAfterNames", false);
	g_Settings.bUseCommaAsColon = M.GetBool(CHAT_MODULE, "UseCommaAsColon", false);

	replaceStrW(g_Settings.pszLogDir, M.getChatLogPath());

	g_Settings.LogIconSize = (g_Settings.bScaleIcons) ? 12 : 16;

	// nicklist
	if (g_Settings.UserListFonts[0]) {
		DeleteObject(g_Settings.UserListFonts[CHAT_STATUS_NORMAL]);
		DeleteObject(g_Settings.UserListFonts[CHAT_STATUS_AWAY]);
		DeleteObject(g_Settings.UserListFonts[CHAT_STATUS_OFFLINE]);
	}

	LOGFONT lf;
	g_chatApi.LoadMsgDlgFont(18, &lf, nullptr);
	g_Settings.UserListFonts[CHAT_STATUS_NORMAL] = CreateFontIndirect(&lf);

	g_chatApi.LoadMsgDlgFont(19, &lf, nullptr);
	g_Settings.UserListFonts[CHAT_STATUS_AWAY] = CreateFontIndirect(&lf);

	g_chatApi.LoadMsgDlgFont(5, &lf, nullptr);
	g_Settings.UserListFonts[CHAT_STATUS_OFFLINE] = CreateFontIndirect(&lf);

	int ih = Chat_GetTextPixelSize(L"AQGglo", g_Settings.UserListFonts[CHAT_STATUS_NORMAL], false);
	int ih2 = Chat_GetTextPixelSize(L"AQGglo", g_Settings.UserListFonts[CHAT_STATUS_AWAY], false);
	g_Settings.iNickListFontHeight = max(M.GetByte(CHAT_MODULE, "NicklistRowDist", 12), (ih > ih2 ? ih : ih2));

	for (int i = 0; i < 5; i++) {
		char szBuf[40];
		mir_snprintf(szBuf, "NickColor%d", i);
		g_Settings.nickColors[i] = M.GetDword(CHAT_MODULE, szBuf, g_Settings.UserListColors[0]);
	}
	g_Settings.nickColors[5] = M.GetDword(CHAT_MODULE, "NickColor5", GetSysColor(COLOR_HIGHLIGHT));
	g_Settings.nickColors[6] = M.GetDword(CHAT_MODULE, "NickColor6", GetSysColor(COLOR_HIGHLIGHTTEXT));

	if (g_Settings.SelectionBGBrush)
		DeleteObject(g_Settings.SelectionBGBrush);
	g_Settings.SelectionBGBrush = CreateSolidBrush(g_Settings.nickColors[5]);
}

/////////////////////////////////////////////////////////////////////////////////////////

void Chat_ModulesLoaded()
{
	g_Settings.hIconOverlay = LoadIconEx("overlay");
}

/////////////////////////////////////////////////////////////////////////////////////////
// load the group chat module

static int CopyChatSetting(const char *szSetting, void *param)
{
	LIST<char> *szSettings = (LIST<char>*)param;
	szSettings->insert(mir_strdup(szSetting));
	return 0;
}

static void CheckUpdate()
{
	// already converted?
	int compat = db_get_b(0, "Compatibility", "TabChatFonts", 0);
	if (compat >= 3)
		return;

	if (compat == 0) {
		LIST<char> szSettings(120);
		db_enum_settings(0, CopyChatSetting, CHAT_OLDFONTMODULE, &szSettings);

		DBVARIANT dbv;
		for (auto &p : szSettings.rev_iter()) {
			db_get(0, CHAT_OLDFONTMODULE, p, &dbv);
			db_set(0, CHATFONT_MODULE, p, &dbv);
			db_free(&dbv);
			mir_free(p);
		}

		db_delete_module(0, CHAT_OLDFONTMODULE);
		compat++;
	}

	if (compat == 1) {
		DWORD oldBackColor = db_get_dw(0, FONTMODULE, "BkgColourMUC", SRMSGDEFSET_BKGCOLOUR);
		db_set_dw(0, CHAT_MODULE, "ColorLogBG", oldBackColor);
		db_unset(0, FONTMODULE, "BkgColourMUC");
		compat++;
	}

	if (compat == 2) {
		COLORREF color0 = M.GetDword(CHAT_MODULE, "NickColor2", 0);
		COLORREF color2 = M.GetDword(CHAT_MODULE, "NickColor0", 0);
		db_set_dw(0, CHAT_MODULE, "NickColor0", color0);
		db_set_dw(0, CHAT_MODULE, "NickColor2", color2);
		compat++;
	}

	db_set_b(0, "Compatibility", "TabChatFonts", 3);
}

/////////////////////////////////////////////////////////////////////////////////////////

static void stubShowRoom(SESSION_INFO *si)
{
	ShowRoom(nullptr, si);
}

// load the module
int Chat_Load()
{
	CheckUpdate();
	Utils::RTF_CTableInit();

	CHAT_MANAGER_INITDATA data = { &g_Settings, sizeof(MODULEINFO), sizeof(SESSION_INFO), LPGENW("Message sessions") L"/" LPGENW("Group chats"), FONTMODE_ALTER, &g_plugin };
	Chat_CustomizeApi(&data);
	
	g_chatApi.OnCreateModule = OnCreateModule;
	g_chatApi.OnNewUser = OnNewUser;

	g_chatApi.OnSetTopic = OnSetTopic;

	g_chatApi.OnCreateSession = OnCreateSession;
	g_chatApi.OnReplaceSession = OnReplaceSession;

	g_chatApi.OnChangeNick = OnChangeNick;
	g_chatApi.ShowRoom = stubShowRoom;
	g_chatApi.OnLoadSettings = OnLoadSettings;

	// this operation is unsafe, that's why we restore the old pci state on exit
	g_chatApi.DoSoundsFlashPopupTrayStuff = DoSoundsFlashPopupTrayStuff;
	g_chatApi.IsHighlighted = IsHighlighted;
	oldLogToFile = g_chatApi.LogToFile; g_chatApi.LogToFile = LogToFile;
	oldDoPopup = g_chatApi.DoPopup; g_chatApi.DoPopup = DoPopup;
	oldDoTrayIcon = g_chatApi.DoTrayIcon; g_chatApi.ShowPopup = ShowPopup;
	g_chatApi.Log_CreateRTF = Log_CreateRTF;
	g_chatApi.Log_CreateRtfHeader = Log_CreateRtfHeader;
	g_chatApi.UM_CompareItem = UM_CompareItem;
	g_chatApi.ReloadSettings();

	g_Settings.Highlight = new CMUCHighlight();
	return 0;
}

// unload the module. final cleanup
int Chat_Unload(void)
{
	if (g_Settings.SelectionBGBrush)
		DeleteObject(g_Settings.SelectionBGBrush);

	for (auto &it : g_Settings.UserListFonts)
		if (it)
			DeleteObject(it);

	delete g_Settings.Highlight;
	return 0;
}