summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/userprefs.cpp
blob: cb3533cf9172c00aaf3d293bd3e1768b220ce7ef (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (C) 2012-24 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
//
// global/local message log options
// local (per user) template overrides
// view mode (ieview/default)
// text formatting

#include "stdafx.h"

#define UPREF_ACTION_APPLYOPTIONS 1
#define UPREF_ACTION_REMAKELOG 2
#define UPREF_ACTION_SWITCHLOGVIEWER 4

class CUserPrefsOptDlg : public CDlgBase
{
	typedef CDlgBase CSuper;

	MCONTACT m_hContact;

	CCtrlCombo cmbPanel, cmbAvatar, cmbTextFormat;
	CCtrlCheck chkAlwaysTrim;

public:
	CUserPrefsOptDlg(MCONTACT hContact) :
		CSuper(g_plugin, IDD_USERPREFS),
		m_hContact(hContact),
		cmbPanel(this, IDC_INFOPANEL),
		cmbAvatar(this, IDC_SHOWAVATAR),
		cmbTextFormat(this, IDC_TEXTFORMATTING),
		chkAlwaysTrim(this, IDC_ALWAYSTRIM2)
	{
		chkAlwaysTrim.OnChange = Callback(this, &CUserPrefsOptDlg::onChange_Trim);
	}

	bool OnInitDialog() override
	{
		uint32_t maxhist = M.GetDword(m_hContact, "maxhist", 0);
		int iLocalFormat = M.GetDword(m_hContact, "sendformat", 0);
		uint8_t bSplit = M.GetByte(m_hContact, "splitoverride", 0);
		uint8_t bInfoPanel = M.GetByte(m_hContact, "infopanel", 0);
		uint8_t bAvatarVisible = M.GetByte(m_hContact, "hideavatar", -1);

		cmbPanel.AddString(TranslateT("Use global setting"));
		cmbPanel.AddString(TranslateT("Always on"));
		cmbPanel.AddString(TranslateT("Always off"));
		cmbPanel.SetCurSel(bInfoPanel);

		cmbAvatar.AddString(TranslateT("Use global setting"));
		cmbAvatar.AddString(TranslateT("Show always (if present)"));
		cmbAvatar.AddString(TranslateT("Never show it at all"));
		cmbAvatar.SetCurSel(bAvatarVisible == 0xff ? 0 : (bAvatarVisible == 1 ? 1 : 2));

		cmbTextFormat.AddString(TranslateT("Use global setting"));
		cmbTextFormat.AddString(TranslateT("BBCode"));
		cmbTextFormat.AddString(TranslateT("Force off"));
		cmbTextFormat.SetCurSel(iLocalFormat == 0 ? 0 : (iLocalFormat == -1 ? 2 : 1));

		CheckDlgButton(m_hwnd, IDC_PRIVATESPLITTER, bSplit);
		CheckDlgButton(m_hwnd, IDC_TEMPLOVERRIDE, db_get_b(m_hContact, TEMPLATES_MODULE, "enabled", 0) ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(m_hwnd, IDC_RTLTEMPLOVERRIDE, db_get_b(m_hContact, RTLTEMPLATES_MODULE, "enabled", 0) ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(m_hwnd, IDC_LOADONLYACTUAL, M.GetByte(m_hContact, "ActualHistory", 0) ? BST_CHECKED : BST_UNCHECKED);

		SendDlgItemMessage(m_hwnd, IDC_TRIMSPIN, UDM_SETRANGE, 0, MAKELONG(1000, 5));
		SendDlgItemMessage(m_hwnd, IDC_TRIMSPIN, UDM_SETPOS, 0, maxhist);
		Utils::enableDlgControl(m_hwnd, IDC_TRIMSPIN, maxhist != 0);
		Utils::enableDlgControl(m_hwnd, IDC_TRIM, maxhist != 0);
		chkAlwaysTrim.SetState(maxhist != 0);

		CheckDlgButton(m_hwnd, IDC_IGNORETIMEOUTS, M.GetByte(m_hContact, "no_ack", 0) ? BST_CHECKED : BST_UNCHECKED);

		ShowWindow(m_hwnd, SW_SHOW);
		return true;
	}

	bool OnApply() override
	{
		CMsgDialog *dat = Srmm_FindDialog(m_hContact);
		uint8_t bOldInfoPanel = M.GetByte(m_hContact, "infopanel", 0);

		int iIndex = SendDlgItemMessage(m_hwnd, IDC_TEXTFORMATTING, CB_GETCURSEL, 0, 0);
		if (iIndex != CB_ERR) {
			if (iIndex == 0)
				db_unset(m_hContact, SRMSGMOD_T, "sendformat");
			else
				db_set_dw(m_hContact, SRMSGMOD_T, "sendformat", iIndex == 2 ? -1 : 1);
		}

		db_set_b(m_hContact, SRMSGMOD_T, "splitoverride", (uint8_t)(IsDlgButtonChecked(m_hwnd, IDC_PRIVATESPLITTER) ? 1 : 0));

		db_set_b(m_hContact, TEMPLATES_MODULE, "enabled", (uint8_t)(IsDlgButtonChecked(m_hwnd, IDC_TEMPLOVERRIDE)));
		db_set_b(m_hContact, RTLTEMPLATES_MODULE, "enabled", (uint8_t)(IsDlgButtonChecked(m_hwnd, IDC_RTLTEMPLOVERRIDE)));

		uint8_t bAvatarVisible = (uint8_t)SendDlgItemMessage(m_hwnd, IDC_SHOWAVATAR, CB_GETCURSEL, 0, 0);
		if (bAvatarVisible == 0)
			db_unset(m_hContact, SRMSGMOD_T, "hideavatar");
		else
			db_set_b(m_hContact, SRMSGMOD_T, "hideavatar", (uint8_t)(bAvatarVisible == 1 ? 1 : 0));

		uint8_t bInfoPanel = (uint8_t)SendDlgItemMessage(m_hwnd, IDC_INFOPANEL, CB_GETCURSEL, 0, 0);
		if (bInfoPanel != bOldInfoPanel) {
			db_set_b(m_hContact, SRMSGMOD_T, "infopanel", (uint8_t)(bInfoPanel == 0 ? 0 : (bInfoPanel == 1 ? 1 : -1)));
			if (dat)
				SendMessage(dat->GetHwnd(), DM_SETINFOPANEL, 0, 0);
		}
		if (chkAlwaysTrim.GetState())
			db_set_dw(m_hContact, SRMSGMOD_T, "maxhist", (uint32_t)SendDlgItemMessage(m_hwnd, IDC_TRIMSPIN, UDM_GETPOS, 0, 0));
		else
			db_set_dw(m_hContact, SRMSGMOD_T, "maxhist", 0);

		if (IsDlgButtonChecked(m_hwnd, IDC_LOADONLYACTUAL)) {
			db_set_b(m_hContact, SRMSGMOD_T, "ActualHistory", 1);
			if (dat)
				dat->m_bActualHistory = true;
		}
		else {
			db_set_b(m_hContact, SRMSGMOD_T, "ActualHistory", 0);
			if (dat)
				dat->m_bActualHistory = false;
		}

		if (IsDlgButtonChecked(m_hwnd, IDC_IGNORETIMEOUTS)) {
			db_set_b(m_hContact, SRMSGMOD_T, "no_ack", 1);
			if (dat)
				dat->m_sendMode |= SMODE_NOACK;
		}
		else {
			db_unset(m_hContact, SRMSGMOD_T, "no_ack");
			if (dat)
				dat->m_sendMode &= ~SMODE_NOACK;
		}
		if (dat) {
			SendMessage(dat->GetHwnd(), DM_CONFIGURETOOLBAR, 0, 1);
			dat->ShowPicture(false);
			dat->OnResize();
			dat->DM_ScrollToBottom(0, 1);
		}
		return true;
	}

	void onChange_Trim(CCtrlCheck *)
	{
		bool bChecked = chkAlwaysTrim.GetState();
		Utils::enableDlgControl(m_hwnd, IDC_TRIMSPIN, bChecked);
		Utils::enableDlgControl(m_hwnd, IDC_TRIM, bChecked);
	}
};

/////////////////////////////////////////////////////////////////////////////////////////
// loads message log and other "per contact" flags
// it uses the global flag value (0, mwflags) and then merges per contact settings
// based on the mask value.
//
// ALWAYS mask dat->dwFlags with MWF_LOG_ALL to only affect real flag bits and
// ignore temporary bits.

struct
{
	UINT	uId;
	UINT	uFlag;
}
static checkboxes[] = {
	{ IDC_UPREFS_GRID,            MWF_LOG_GRID            },
	{ IDC_UPREFS_SHOWICONS,       MWF_LOG_SHOWICONS       },
	{ IDC_UPREFS_SHOWSYMBOLS,     MWF_LOG_SYMBOLS         },
	{ IDC_UPREFS_INOUTICONS,      MWF_LOG_INOUTICONS      },
	{ IDC_UPREFS_SHOWTIMESTAMP,   MWF_LOG_SHOWTIME        },
	{ IDC_UPREFS_SHOWDATES,       MWF_LOG_SHOWDATES       },
	{ IDC_UPREFS_SHOWSECONDS,     MWF_LOG_SHOWSECONDS     },
	{ IDC_UPREFS_LOCALTIME,       MWF_LOG_LOCALTIME       },
	{ IDC_UPREFS_INDENT,          MWF_LOG_INDENT          },
	{ IDC_UPREFS_GROUPING,        MWF_LOG_GROUPMODE       },
	{ IDC_UPREFS_BBCODE,          MWF_LOG_BBCODE          },
	{ IDC_UPREFS_RTL,             MWF_LOG_RTL             },
	{ IDC_UPREFS_NORMALTEMPLATES, MWF_LOG_NORMALTEMPLATES },
};

int CMsgDialog::LoadLocalFlags()
{
	uint32_t	dwMask = M.GetDword(m_hContact, "mwmask", 0);
	uint32_t	dwLocal = M.GetDword(m_hContact, "mwflags", 0);
	uint32_t	dwGlobal = M.GetDword("mwflags", MWF_LOG_DEFAULT);

	m_dwFlags &= ~MWF_LOG_ALL;
	if (m_pContainer->m_theme.isPrivate)
		m_dwFlags |= (m_pContainer->m_theme.dwFlags & MWF_LOG_ALL);
	else
		m_dwFlags |= (dwGlobal & MWF_LOG_ALL);

	for (auto &it : checkboxes) {
		uint32_t	maskval = it.uFlag;
		if (dwMask & maskval)
			m_dwFlags = (dwLocal & maskval) ? m_dwFlags | maskval : m_dwFlags & ~maskval;
	}

	return m_dwFlags & MWF_LOG_ALL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// dialog procedure for the user preferences dialog 
// (Second page, "per contact" message log options)

class CUserPrefsLogDlg : public CDlgBase
{
	typedef CDlgBase CSuper;

	MCONTACT m_hContact;

	CCtrlButton btnReset;

public:
	CUserPrefsLogDlg(MCONTACT hContact) :
		CSuper(g_plugin, IDD_USERPREFS1),
		m_hContact(hContact),
		btnReset(this, IDC_REVERTGLOBAL)
	{
		btnReset.OnClick = Callback(this, &CUserPrefsLogDlg::onClick_Reset);
	}

	bool OnInitDialog() override
	{
		uint32_t	dwLocalFlags = M.GetDword(m_hContact, "mwflags", 0);
		uint32_t	dwLocalMask = M.GetDword(m_hContact, "mwmask", 0);

		for (auto &it : checkboxes) {
			uint32_t	maskval = it.uFlag;

			if (dwLocalMask & maskval)
				CheckDlgButton(m_hwnd, it.uId, (dwLocalFlags & maskval) ? BST_CHECKED : BST_UNCHECKED);
			else
				CheckDlgButton(m_hwnd, it.uId, BST_INDETERMINATE);
		}

		if (M.GetByte("logstatuschanges", 0) == M.GetByte(m_hContact, "logstatuschanges", 0))
			CheckDlgButton(m_hwnd, IDC_UPREFS_LOGSTATUS, BST_INDETERMINATE);
		else
			CheckDlgButton(m_hwnd, IDC_UPREFS_LOGSTATUS, M.GetByte(m_hContact, "logstatuschanges", 0) ? BST_CHECKED : BST_UNCHECKED);
		return true;
	}

	bool OnApply() override
	{
		uint32_t dwMask = 0, dwFlags = 0;

		for (auto &it : checkboxes) {
			uint32_t maskval = it.uFlag;

			int state = IsDlgButtonChecked(m_hwnd, it.uId);
			if (state != BST_INDETERMINATE) {
				dwMask |= maskval;
				dwFlags = (state == BST_CHECKED) ? (dwFlags | maskval) : (dwFlags & ~maskval);
			}
		}

		int state = IsDlgButtonChecked(m_hwnd, IDC_UPREFS_LOGSTATUS);
		if (state != BST_INDETERMINATE)
			db_set_b(m_hContact, SRMSGMOD_T, "logstatuschanges", (uint8_t)state);

		if (dwMask) {
			db_set_dw(m_hContact, SRMSGMOD_T, "mwmask", dwMask);
			db_set_dw(m_hContact, SRMSGMOD_T, "mwflags", dwFlags);
		}
		else {
			db_unset(m_hContact, SRMSGMOD_T, "mwmask");
			db_unset(m_hContact, SRMSGMOD_T, "mwflags");
		}
		return true;
	}

	void onClick_Reset(CCtrlButton *)
	{
		db_unset(m_hContact, SRMSGMOD_T, "mwmask");
		db_unset(m_hContact, SRMSGMOD_T, "mwflags");
		OnInitDialog();
	}
};

/////////////////////////////////////////////////////////////////////////////////////////
// dialog procedure for the user preferences dialog

class CUserPrefsDlg : public CDlgBase
{
	typedef CDlgBase CSuper;

	MCONTACT m_hContact;

	CCtrlPages m_tab;

public:
	CUserPrefsDlg(MCONTACT hContact) :
		CSuper(g_plugin, IDD_USERPREFS_FRAME),
		m_hContact(hContact),
		m_tab(this, IDC_OPTIONSTAB)
	{
		m_tab.SetPageOwner();
	}

	bool OnInitDialog() override
	{
		WindowList_Add(PluginConfig.hUserPrefsWindowList, m_hwnd, m_hContact);

		wchar_t szBuffer[180];
		mir_snwprintf(szBuffer, TranslateT("Set messaging options for %s"), Clist_GetContactDisplayName(m_hContact));
		SetCaption(szBuffer);

		m_tab.AddPage(TranslateT("General"), nullptr, new CUserPrefsOptDlg(m_hContact));
		if (!Srmm_IsCustomLogUsed(Contact::IsGroupChat(m_hContact)))
			m_tab.AddPage(TranslateT("Message Log"), nullptr, new CUserPrefsLogDlg(m_hContact));
		return true;
	}

	bool OnApply() override
	{
		if (auto *dat = Srmm_FindDialog(m_hContact)) {
			uint32_t dwOldFlags = (dat->m_dwFlags & MWF_LOG_ALL);
			dat->SetDialogToType();
			dat->LoadLocalFlags();
			if ((dat->m_dwFlags & MWF_LOG_ALL) != dwOldFlags) {
				bool fShouldHide = true;
				if (IsIconic(dat->m_pContainer->m_hwnd))
					fShouldHide = false;
				else
					ShowWindow(dat->m_pContainer->m_hwnd, SW_HIDE);
				dat->OnOptionsApplied();
				dat->RemakeLog();
				if (fShouldHide)
					ShowWindow(dat->m_pContainer->m_hwnd, SW_SHOWNORMAL);
			}
		}
		return true;
	}

	void OnDestroy() override
	{
		WindowList_Remove(PluginConfig.hUserPrefsWindowList, m_hwnd);
	}
};

/////////////////////////////////////////////////////////////////////////////////////////
// service function. Invokes the user preferences dialog for the contact given in wParam

INT_PTR SetUserPrefs(WPARAM hContact, LPARAM)
{
	HWND hWnd = WindowList_Find(PluginConfig.hUserPrefsWindowList, hContact);
	if (hWnd)
		SetForegroundWindow(hWnd);			// already open, bring it to front
	else
		(new CUserPrefsDlg(hContact))->Show();
	return 0;
}