summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/ctrl_tzcombo.cpp
blob: 26dfbfee327caf167baf4cb660fb4f7f6b880e82 (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
/*
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.

===============================================================================

File name      : $HeadURL: https://userinfoex.googlecode.com/svn/trunk/ctrl_tzcombo.cpp $
Revision       : $Revision: 194 $
Last change on : $Date: 2010-09-20 15:57:18 +0400 (Пн, 20 сен 2010) $
Last change by : $Author: ing.u.horn $

===============================================================================
*/
#include "commonheaders.h"
#include "svc_timezone.h"
#include "ctrl_tzcombo.h"


static INT_PTR EnumNamesProc(CTimeZone *pTimeZone, INT index, LPARAM lParam)
{
	if (pTimeZone && pTimeZone->ptszDisplay)
	{
		INT added = ComboBox_AddString((HWND)lParam, pTimeZone->ptszDisplay);
		if (SUCCEEDED(added)) 
		{
			if (FAILED(ComboBox_SetItemData((HWND)lParam, added, pTimeZone))) 
			{
				ComboBox_DeleteString((HWND)lParam, added);
			}
		}
	}
	return 0;
}

/**
 * This functions fills a combobox given by @hCtrl with
 * all items of the global timezone manager
 *
 * @param	hDlg			- HWND of the owning propertysheet page
 * @param	idCtrl			- the ID of the control to associate with this class's instance
 * @param	pszSetting		- the database setting to be handled by this class
 *
 * @return	CTzCombo*
 **/
CBaseCtrl* CTzCombo::CreateObj(HWND hDlg, WORD idCtrl, LPCSTR pszSetting)
{
	CTzCombo *ctrl = NULL;
	HWND hCtrl = GetDlgItem(hDlg, idCtrl);

	ctrl = new CTzCombo(hDlg, idCtrl, pszSetting);
	if (ctrl) {
		//use new core tz interface
		if(tmi.prepareList) {
			//set the adress of our timezone handle as itemdata
			//caller can obtain the handle htz to extract all relevant information
			ctrl->_curSel = 0;
			tmi.prepareList(NULL, hCtrl, TZF_PLF_CB);
		}
		//fallback use old UIEX method
		else {
			ctrl->_curSel = ComboBox_AddString(hCtrl, TranslateT("<Unspecified>"));
			if (SUCCEEDED(ctrl->_curSel)) {
				ComboBox_SetItemData(hCtrl, ctrl->_curSel, NULL);
			}
			ComboBox_SetCurSel(hCtrl, ctrl->_curSel);
			EnumTimeZones(EnumNamesProc, (LPARAM)hCtrl);
		}
	}
	return (ctrl);
}

/**
 *
 *
 **/
CTzCombo::CTzCombo() : CBaseCtrl()
{
	_curSel = CB_ERR;
}

/**
 *
 *
 **/
CTzCombo::CTzCombo(HWND hDlg, WORD idCtrl, LPCSTR pszSetting)
	: CBaseCtrl(hDlg, idCtrl, pszSetting)
{
	_curSel = CB_ERR;
}

/**
 * This method does a binary search in the sorted
 * ComboBox for the item index. (old UIEX method)
 *
 * @param	pTimeZone		- CTimeZone compobox item data.
 *
 * @retval	CB_ERR			- item not found
 * @retval	0...n			- index of the combobox item
 **/
INT CTzCombo::Find(CTimeZone *pTimeZone) const
{
	INT nItemIndex;
	INT nItemCount = ComboBox_GetCount(_hwnd);

	for (nItemIndex = 0; nItemIndex < nItemCount; nItemIndex++)
	{
		if (pTimeZone == (CTimeZone *)ComboBox_GetItemData(_hwnd, nItemIndex))
			return nItemIndex;
	}
	return CB_ERR;
}

/**
 * This method does a binary search in the sorted
 * ComboBox for the item index. (new core tz interface)
 *
 * @param	pTimeZone		- LPTIME_ZONE_INFORMATION to find.
 *
 * @retval	CB_ERR			- item not found
 * @retval	0...n			- index of the combobox item
 **/
INT CTzCombo::Find(LPTIME_ZONE_INFORMATION pTimeZone) const
{
	INT nItemIndex;
	INT nItemCount = ComboBox_GetCount(_hwnd);

	for (nItemIndex = 0; nItemIndex < nItemCount; nItemIndex++) {
		if (pTimeZone == tmi.getTzi((HANDLE)ComboBox_GetItemData(_hwnd, nItemIndex)))
			return nItemIndex;
	}
	return CB_ERR;
}

/**
 * This functions removes the user data from a combobox.
 *
 * @param	hCtrl	-	HWND of the combobox
 *
 * @return	nothing
 **/
VOID CTzCombo::Release()
{
	delete this;
}

/**
 * This functions selects the combobox item which matches the contact's timezone.
 *
 * @param	hCtrl		- HWND of the combobox
 * @param	hContact	- HANDLE of the contact whose timezone to select
 * @param	pszProto	- the contact's protocol (not used by new core tz interface)
 *
 * @return	_Flags.B.hasChanged member
 **/
BOOL CTzCombo::OnInfoChanged(HANDLE hContact, LPCSTR pszProto)
{
	if (!_Flags.B.hasChanged) {
		//use new core tz interface to change the cbbox
		if(tmi.storeListResults) {
			_curSel = CB_ERR;
//			_curSel = tmi.selectListItem(hContact, _hwnd, TZF_PLF_CB);
										//dident work well, coz no fallback to proto setting.
										//we use saver way by getTziByContact
			LPTIME_ZONE_INFORMATION pTimeZone;
			pTimeZone = tmi.getTziByContact(hContact);
			ComboBox_SetCurSel(_hwnd, Find(pTimeZone));
			_curSel = ComboBox_GetCurSel(_hwnd);
		}
		//fallback use old UIEX method
		else {
			CTimeZone *pTimeZone;
			_curSel		= CB_ERR;
			_Flags.W	= GetContactTimeZoneCtrl(hContact, pszProto, &pTimeZone);
			if (_Flags.W) {
				ComboBox_SetCurSel(_hwnd, Find(pTimeZone));
				_curSel = ComboBox_GetCurSel(_hwnd);
			}
			if (_curSel == CB_ERR) {
				ComboBox_SetCurSel(_hwnd, NULL);
				_curSel = ComboBox_GetCurSel(_hwnd);
			}
		}
		SendMessage(GetParent(_hwnd), WM_TIMER, 0, 0);
	}
	return _Flags.B.hasChanged;
}

/**
 * This method writes the combobox's item as the contact's timezone.
 *
 * @param		hContact	- HANDLE of the contact whose timezone to select
 * @param		pszProto	- the contact's protocol (not used by new core tz interface)
 *
 * @return	nothing
 **/
VOID CTzCombo::OnApply(HANDLE hContact, LPCSTR pszProto)
{
	if (_Flags.B.hasChanged)
	{
		const char* pszModule = hContact ? USERINFO : pszProto;
		if (_Flags.B.hasCustom || !hContact) {
			//use new core tz interface
			if(tmi.storeListResults) {
				tmi.storeListResults(hContact, _hwnd, TZF_PLF_CB);
				if (!hContact) {
					_Flags.B.hasCustom = 0;
					_Flags.B.hasProto = 1;
				}
				_Flags.B.hasChanged = 0;
			}
			//fallback use old UIEX method
			else {
				const CTimeZone* pTimeZone = (CTimeZone*)ComboBox_GetItemData(_hwnd, _curSel);
				if (PtrIsValid(pTimeZone)) {
					DB::Setting::WriteTString(hContact, USERINFO, SET_CONTACT_TIMEZONENAME, pTimeZone->ptszName);
					DB::Setting::WriteByte(hContact, pszModule, SET_CONTACT_TIMEZONE, pTimeZone->ToMirandaTimezone());

					if (!hContact) {
						_Flags.B.hasCustom = 0;
						_Flags.B.hasProto = 1;
					}
					_Flags.B.hasChanged = 0;
				}
			}
		}

		if (_Flags.B.hasChanged)
		{
			DB::Setting::Delete(hContact, USERINFO, SET_CONTACT_TIMEZONENAME);
			DB::Setting::Delete(hContact, USERINFO, SET_CONTACT_TIMEZONEINDEX);
			DB::Setting::Delete(hContact, pszModule, SET_CONTACT_TIMEZONE);

			_Flags.B.hasChanged = 0;
			OnInfoChanged(hContact, pszProto);
		}
		InvalidateRect(_hwnd, NULL, TRUE);
	}
}

/**
 * The user changed combobox selection, so mark it changed.
 *
 * @return	nothing
 **/
VOID CTzCombo::OnChangedByUser(WORD wChangedMsg)
{
	if (wChangedMsg == CBN_SELCHANGE) {
		INT c = ComboBox_GetCurSel(_hwnd);

		if (_curSel != c) {
			if (!_Flags.B.hasChanged) {
				_Flags.B.hasChanged = 1;
				_Flags.B.hasCustom = 1;
				SendMessage(GetParent(GetParent(_hwnd)), PSM_CHANGED, 0, 0);
			}
			_curSel = c;
			SendMessage(GetParent(_hwnd), WM_TIMER, 0, 0);
		}
	}
}

/**
 * This method fills @szTime with the current time
 * according to the combobox's selected timezone.
 *
 * @param		szTime		- string to fill with the current time
 * @param		cchTime		- number of characters the string can take
 *
 * @return	nothing
 **/
VOID CTzCombo::GetTime(LPTSTR szTime, WORD cchTime)
{
	//use new core tz interface
	if(tmi.printDateTime) {
		tmi.printDateTime((HANDLE)ComboBox_GetItemData(_hwnd, _curSel), _T("t"), szTime, cchTime, 0);
	}
	//fallback use old UIEX method
	else {
		const CTimeZone *pTimeZone = (CTimeZone*)ComboBox_GetItemData(_hwnd, _curSel);
		if (PtrIsValid(pTimeZone)) {
			MTime now;
			TIME_ZONE_INFORMATION tzi = *pTimeZone;

			now.GetTimeUTC();
			now.UTCToTzSpecificLocal(&tzi);
			now.TimeFormat(szTime, cchTime);
		}
		else mir_tcscpy(szTime, _T("--:--"));
	}
}