summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ctrl_edit.cpp
blob: 1da43c143c1197daf7b6b4c6532052364eda1ae3 (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
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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 "stdafx.h"

/**
 * This function creates a CEditCtrl object. 
 *
 * @param	hDlg			- HWND of the owning propertysheet page
 * @param	idCtrl			- the ID of the dialog control to associate with this class's instance
 * @param	pszSetting		- the database setting to be handled by this class
 * @param	dbType			- the expected data type of the setting
 *
 * @return	This static method returns the pointer of the created object.
 **/

CBaseCtrl* CEditCtrl::CreateObj(HWND hDlg, WORD idCtrl, LPCSTR pszSetting, BYTE dbType)
{
	CEditCtrl *ctrl = new CEditCtrl(hDlg, idCtrl, USERINFO, pszSetting);
	if (ctrl)
		ctrl->_dbType = dbType;

	return (ctrl);
}

/**
 * This function creates a CEditCtrl object.
 *
 * @param	hDlg			- HWND of the owning propertysheet page
 * @param	idCtrl			- the ID of the dialog control to associate with this class's instance
 * @param	pszModule		- the database module to be handled by this class
 * @param	pszSetting		- the database setting to be handled by this class
 * @param	dbType			- the expected data type of the setting
 *
 * @return	This static method returns the pointer of the created object.
 **/

CBaseCtrl* CEditCtrl::CreateObj(HWND hDlg, WORD idCtrl, LPCSTR pszModule, LPCSTR pszSetting, BYTE dbType)
{
	CEditCtrl *ctrl = new CEditCtrl(hDlg, idCtrl, pszModule, pszSetting);
	if (ctrl)
		ctrl->_dbType = dbType;

	return ctrl;
}

/**
 *
 *
 **/
CEditCtrl::CEditCtrl(HWND hDlg, WORD idCtrl, LPCSTR pszModule, LPCSTR pszSetting)
	: CBaseCtrl(hDlg, idCtrl, pszModule, pszSetting)
{
	SendDlgItemMessage(hDlg, idCtrl, EM_LIMITTEXT, 0x7fFFffFF, 0L);
}

/**
 * This method deletes the class object
 * and all allocated memory of its members.
 *
 * @param	none
 *
 * @return	nothing
 **/
void CEditCtrl::Release()
{
	delete this;
}

/*
 *
 *
 */
void CEditCtrl::OnReset()
{
}


/**
 * This method controls the changed bit for the control.
 *
 * @param	hCtrl			- HWND of the combobox
 * @param	hContact		- HANDLE of the contact whose timezone to select
 * @param	pszProto		- the contact's protocol
 *
 * @return	nothing
 **/

BOOL CEditCtrl::OnInfoChanged(MCONTACT hContact, LPCSTR pszProto)
{
	if (!_Flags.B.hasChanged) {
		DBVARIANT dbv;
		wchar_t szText[64];

		_Flags.B.hasCustom = _Flags.B.hasProto = _Flags.B.hasMeta = false;
		_Flags.W |= DB::Setting::GetTStringCtrl(hContact, _pszModule, _pszModule, pszProto, _pszSetting, &dbv);

		EnableWindow(_hwnd,
			!hContact || _Flags.B.hasCustom || !db_get_b(NULL, MODNAME, SET_PROPSHEET_PCBIREADONLY, 0));

		MIR_FREE(_pszValue);
		switch (dbv.type) {
		case DBVT_BYTE:
			_itow_s(dbv.bVal, szText, _countof(szText), 10);
			SetWindowText(_hwnd, szText);
			_pszValue = mir_wstrdup(szText);
			break;

		case DBVT_WORD:
			_itow_s(dbv.wVal, szText, _countof(szText), 10);
			SetWindowText(_hwnd, szText);
			_pszValue = mir_wstrdup(szText);
			break;

		case DBVT_DWORD:
			_itow_s(dbv.dVal, szText, _countof(szText), 10);
			SetWindowText(_hwnd, szText);
			_pszValue = mir_wstrdup(szText);
			break;

		case DBVT_WCHAR:
			if (dbv.ptszVal) {
				SetWindowText(_hwnd, dbv.ptszVal);
				_pszValue = dbv.ptszVal;
				break;
			}

		default:
			SetWindowText(_hwnd, L"");
			db_free(&dbv);
			break;
		}
		_Flags.B.hasChanged = false;
	}
	return _Flags.B.hasChanged;
}

/**
 * This method writes the control's information to database
 *
 * @param	hContact		- HANDLE of the contact whose timezone to select
 * @param	pszProto		- the contact's protocol
 *
 * @return	nothing
 **/

void CEditCtrl::OnApply(MCONTACT hContact, LPCSTR pszProto)
{
	if (_Flags.B.hasChanged) {
		const char* pszModule = hContact ? _pszModule : pszProto;

		if (_Flags.B.hasCustom || !hContact) {
			DWORD cch = GetWindowTextLength(_hwnd);

			if (cch > 0) {
				LPTSTR val = (LPTSTR)mir_alloc((cch + 1) * sizeof(wchar_t));

				if (GetWindowText(_hwnd, val, cch + 1) > 0) {
					DBVARIANT dbv;

					dbv.type = _dbType;
					switch (_dbType) {
					case DBVT_BYTE:
						dbv.bVal = (BYTE)wcstol(val, nullptr, 10);
						break;

					case DBVT_WORD:
						dbv.wVal = (WORD)wcstol(val, nullptr, 10);
						break;

					case DBVT_DWORD:
						dbv.dVal = (DWORD)wcstol(val, nullptr, 10);
						break;

					case DBVT_WCHAR:
						dbv.ptszVal = val;
						break;

					default:
						dbv.type = DBVT_DELETED;

					}
					if (dbv.type != DBVT_DELETED) {
						if (!db_set(hContact, pszModule, _pszSetting, &dbv)) {
							if (!hContact) {
								_Flags.B.hasCustom = false;
								_Flags.B.hasProto = true;
							}
							_Flags.B.hasChanged = false;

							// save new value
							MIR_FREE(_pszValue);
							_pszValue = val;
							val = nullptr;
						}
					}
				}
				MIR_FREE(val);
			}
		}
		if (_Flags.B.hasChanged) {
			db_unset(hContact, pszModule, _pszSetting);

			_Flags.B.hasChanged = false;

			OnInfoChanged(hContact, pszProto);
		}
		InvalidateRect(_hwnd, nullptr, TRUE);
	}
}

/**
 * The user changed information stored in the control.
 *
 * @return	nothing
 **/
void CEditCtrl::OnChangedByUser(WORD wChangedMsg)
{
	if ((wChangedMsg == EN_UPDATE) || (wChangedMsg == EN_CHANGE)) {
		DWORD cch = GetWindowTextLength(_hwnd);

		_Flags.B.hasChanged = mir_wstrlen(_pszValue) != cch;
		_Flags.B.hasCustom = (cch > 0);

		if (!_Flags.B.hasChanged && _Flags.B.hasCustom) {
			BYTE need_free = 0;
			LPTSTR szText;

			__try {
				szText = (LPTSTR)alloca((cch + 1) * sizeof(wchar_t));
			}
			__except (EXCEPTION_EXECUTE_HANDLER)
			{
				szText = (LPTSTR)mir_alloc((cch + 1) * sizeof(wchar_t));
				need_free = 1;
			}

			if (szText != nullptr) {
				GetWindowText(_hwnd, szText, cch + 1);
				_Flags.B.hasChanged = mir_wstrcmp(_pszValue, szText) != 0;
				if (need_free)
					MIR_FREE(szText);
			}
			else _Flags.B.hasChanged = false;
		}
		InvalidateRect(_hwnd, nullptr, TRUE);

		if (_Flags.B.hasChanged)
			SendMessage(GetParent(GetParent(_hwnd)), PSM_CHANGED, 0, 0);
	}
}

/**
 * Opens the url given in a editbox in the users default browser
 **/
void CEditCtrl::OpenUrl()
{
	int lenUrl = 1 + Edit_GetTextLength(_hwnd);
	LPTSTR szUrl;
	BYTE need_free = 0;

	__try {
		szUrl = (LPTSTR)alloca((8 + lenUrl) * sizeof(wchar_t));
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		szUrl = (LPTSTR)mir_alloc((8 + lenUrl) * sizeof(wchar_t));
		need_free = 1;
	}

	if (szUrl && (GetWindowText(_hwnd, szUrl, lenUrl) > 0))
		Utils_OpenUrlW(szUrl);

	if (need_free)
		MIR_FREE(szUrl);
}

LRESULT CEditCtrl::LinkNotificationHandler(ENLINK* lnk)
{
	if (lnk == nullptr)
		return FALSE;

	switch (lnk->msg) {
	case WM_SETCURSOR:
		SetCursor(LoadCursor(nullptr, IDC_HAND));
		SetWindowLongPtr(GetParent(_hwnd), DWLP_MSGRESULT, TRUE);
		return TRUE;

	case WM_LBUTTONUP:
		TEXTRANGE tr;
		BYTE need_free = 0;

		// do not call function if user selected some chars of the url string
		SendMessage(_hwnd, EM_EXGETSEL, NULL, (LPARAM)&tr.chrg);
		if (tr.chrg.cpMax == tr.chrg.cpMin) {
			// retrieve the url string
			tr.chrg = lnk->chrg;

			__try {
				tr.lpstrText = (LPTSTR)alloca((tr.chrg.cpMax - tr.chrg.cpMin + 8) * sizeof(wchar_t));
			}
			__except (EXCEPTION_EXECUTE_HANDLER)
			{
				tr.lpstrText = (LPTSTR)mir_alloc((tr.chrg.cpMax - tr.chrg.cpMin + 8) * sizeof(wchar_t));
				need_free = 1;
			}
			if (tr.lpstrText && (SendMessage(_hwnd, EM_GETTEXTRANGE, NULL, (LPARAM)&tr) > 0)) {
				if (wcschr(tr.lpstrText, '@') != nullptr && wcschr(tr.lpstrText, ':') == nullptr && wcschr(tr.lpstrText, '/') == nullptr) {
					memmove(tr.lpstrText + 7, tr.lpstrText, (tr.chrg.cpMax - tr.chrg.cpMin + 1)*sizeof(wchar_t));
					memcpy(tr.lpstrText, L"mailto:", (7 * sizeof(wchar_t)));
				}

				Utils_OpenUrlW(tr.lpstrText);
			}

			if (need_free)
				MIR_FREE(tr.lpstrText);
		}
	}
	return FALSE;
}