summaryrefslogtreecommitdiff
path: root/plugins/WinterSpeak/src/DialogConfigActive.cpp
blob: 5d20b915862ba7a450a402ef58e8a91a58852386 (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
374
375
376
#include "Common.h"
#include "DialogConfigActive.h"

//------------------------------------------------------------------------------
DialogConfigActive *DialogConfigActive::m_instance = 0;

//------------------------------------------------------------------------------
// public:
//------------------------------------------------------------------------------
DialogConfigActive::DialogConfigActive(ConfigDatabase &db) : m_db(db)
{
	m_instance = this;
}

//------------------------------------------------------------------------------
DialogConfigActive::~DialogConfigActive()
{
	m_instance = 0;
}

//------------------------------------------------------------------------------
INT_PTR CALLBACK DialogConfigActive::process(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
	if (!m_instance)
	{
		return 1;
	}

	switch (message)
	{
	  case WM_INITDIALOG:
		  m_instance->load(window);
		break;

	  case WM_NOTIFY:
		switch(((LPNMHDR)lparam)->idFrom) {
			case IDC_ACTIVE_USERS: {
				m_instance->notify(window, lparam);
				
			} break;
			case 0: {
				switch (reinterpret_cast<LPNMHDR>(lparam)->code)
				{
				  case PSN_APPLY:
					m_instance->save(window);
					break;

				  case LVN_ITEMCHANGED:
					m_instance->changed(window);
					break;
				} break;
			} break;
		} break;
	  case WM_COMMAND:
		switch (LOWORD(wparam))
		{
		  case IDC_ACTIVE_OFFLINE:
		  case IDC_ACTIVE_ONLINE:
		  case IDC_ACTIVE_AWAY:
		  case IDC_ACTIVE_DND:
		  case IDC_ACTIVE_NA:
		  case IDC_ACTIVE_OCCUPIED:
		  case IDC_ACTIVE_FREEFORCHAT:
		  case IDC_ACTIVE_INVISIBLE:
			m_instance->changed(window);
			break;

		/*case IDC_ACTIVE_ALL:
			m_instance->selectAllUsers(window, true);
			break;*/

		/*case IDC_ACTIVE_NONE:
			m_instance->selectAllUsers(window, false);
			break;*/

		  case IDC_ACTIVE_USERS:
			m_instance->changed(window);
			break;
		}
		break;
	}

	return 0;
}







//------------------------------------------------------------------------------
// private:
//------------------------------------------------------------------------------
void DialogConfigActive::notify(HWND hwndDlg, LPARAM lParam) {
	switch (((LPNMHDR)lParam)->code) {
		case CLN_NEWCONTACT:
		case CLN_LISTREBUILT:
			SetAllContactIcons( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), hwndDlg);
			//fall through
		case CLN_CONTACTMOVED:
			SetListGroupIcons( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), (HANDLE)SendDlgItemMessage(hwndDlg, IDC_ACTIVE_USERS, CLM_GETNEXTITEM, CLGN_ROOT, 0), hItemAll, NULL);
			break;
		case CLN_OPTIONSCHANGED:
			
			ResetListOptions( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS));
			break;
		case CLN_CHECKCHANGED:
			m_instance->changed(hwndDlg);
			break;
		case NM_CLICK:
			{
				NMCLISTCONTROL *nm = (NMCLISTCONTROL*)lParam;
				if (nm->iColumn == -1)
					break;
				
				DWORD hitFlags;
				HANDLE hItem = (HANDLE)SendDlgItemMessage(hwndDlg, IDC_ACTIVE_USERS, CLM_HITTEST, (WPARAM)&hitFlags, MAKELPARAM(nm->pt.x, nm->pt.y));
				if (hItem == NULL || !(hitFlags & CLCHT_ONITEMEXTRA))
					break;

				if (nm->iColumn == 2) { // ignore all
					for (int iImage = 0;iImage<2;iImage++)
						SetIconsForColumn( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), hItem, hItemAll, iImage, iImage+3);
				}
				else if (nm->iColumn == 3) {	// ignore none
					for (int iImage = 0;iImage<2;iImage++)
						SetIconsForColumn( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), hItem, hItemAll, iImage, 0);
				}
				else {
					int iImage = SendDlgItemMessage(hwndDlg, IDC_ACTIVE_USERS, CLM_GETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(nm->iColumn, 0));
					if (iImage == 0)
						iImage = nm->iColumn+3;
					else if (iImage != EMPTY_EXTRA_ICON)
						iImage = 0;
					SetIconsForColumn( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), hItem, hItemAll, nm->iColumn, iImage);
				}
				SetListGroupIcons( GetDlgItem(hwndDlg, IDC_ACTIVE_USERS), (HANDLE)SendDlgItemMessage(hwndDlg, IDC_ACTIVE_USERS, CLM_GETNEXTITEM, CLGN_ROOT, 0), hItemAll, NULL);
				m_instance->changed(hwndDlg);
			}
			break;
	}
}

void DialogConfigActive::load(HWND window)
{
	TranslateDialogDefault(window);

	// initialise the checkboxes
	CheckDlgButton(window, IDC_ACTIVE_ONLINE,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Online));
	CheckDlgButton(window, IDC_ACTIVE_AWAY,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Away));
	CheckDlgButton(window, IDC_ACTIVE_DND,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Dnd));
	CheckDlgButton(window, IDC_ACTIVE_NA,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Na));
	CheckDlgButton(window, IDC_ACTIVE_OCCUPIED,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Occupied));
	CheckDlgButton(window, IDC_ACTIVE_FREEFORCHAT,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_FreeForChat));
	CheckDlgButton(window, IDC_ACTIVE_INVISIBLE,         m_db.getActiveFlag(ConfigDatabase::ActiveFlag_Invisible));

	HWND listview = GetDlgItem(window, IDC_ACTIVE_USERS);

	HIMAGELIST hIml = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 3, 3);
	ImageList_AddIcon_IconLibLoaded(hIml, SKINICON_OTHER_SMALLDOT);
	ImageList_AddIcon_IconLibLoaded(hIml, SKINICON_OTHER_FILLEDBLOB);
	ImageList_AddIcon_IconLibLoaded(hIml, SKINICON_OTHER_EMPTYBLOB);
	ImageList_AddIcon_IconLibLoaded(hIml, SKINICON_EVENT_MESSAGE);
	ImageList_AddIcon_IconLibLoaded(hIml, SKINICON_OTHER_USERONLINE);

	SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_SETEXTRAIMAGELIST, 0, (LPARAM)hIml);
	for (int i=0; i < SIZEOF(hIcons); i++)
		hIcons[i] = ImageList_GetIcon(hIml, 1+i, ILD_NORMAL);

	SendDlgItemMessage(window, IDC_ALLICON, STM_SETICON, (WPARAM)hIcons[0], 0);
	SendDlgItemMessage(window, IDC_NONEICON, STM_SETICON, (WPARAM)hIcons[1], 0);
	SendDlgItemMessage(window, IDC_MSGICON, STM_SETICON, (WPARAM)hIcons[2], 0);
	SendDlgItemMessage(window, IDC_ONLINEICON, STM_SETICON, (WPARAM)hIcons[3], 0);

	this->ResetListOptions(listview);

	SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_SETEXTRACOLUMNS, 4, 0);
	{
		CLCINFOITEM cii = {0};
		cii.cbSize = sizeof(cii);
		cii.flags = CLCIIF_GROUPFONT;
		cii.pszText = TranslateT("** All contacts **");
		hItemAll = (HANDLE)SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_ADDINFOITEM, 0, (LPARAM)&cii);
	
		cii.pszText = TranslateT("** Unknown contacts **");
		hItemUnknown = (HANDLE)SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_ADDINFOITEM, 0, (LPARAM)&cii);
		ConfigDatabase::ActiveUsersMap active_users = m_db.getActiveUsers();
		this->InitialiseItem(listview, hItemUnknown, active_users[0].message, active_users[0].status);
	}
	this->SetAllContactIcons(listview, window);
	this->SetListGroupIcons(GetDlgItem(window, IDC_ACTIVE_USERS), (HANDLE)SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_GETNEXTITEM, CLGN_ROOT, 0), hItemAll, NULL);

}

void DialogConfigActive::SetAllChildIcons(HWND hwndList, HANDLE hFirstItem, int iColumn, int iImage)
{
	HANDLE hItem;

	int typeOfFirst = SendMessage(hwndList, CLM_GETITEMTYPE, (WPARAM)hFirstItem, 0);
	//check groups
	if (typeOfFirst == CLCIT_GROUP) hItem = hFirstItem;
	else hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, (LPARAM)hFirstItem);
	while (hItem) {
		HANDLE hChildItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_CHILD, (LPARAM)hItem);
		if (hChildItem)
			SetAllChildIcons(hwndList, hChildItem, iColumn, iImage);
		hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, (LPARAM)hItem);
	}
	//check contacts
	if (typeOfFirst == CLCIT_CONTACT) hItem = hFirstItem;
	else hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hFirstItem);
	while (hItem) {
		int iOldIcon = SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, iColumn);
		if (iOldIcon != EMPTY_EXTRA_ICON && iOldIcon != iImage)
			SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(iColumn, iImage));
		hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hItem);
	}
}

void DialogConfigActive::SetIconsForColumn(HWND hwndList, HANDLE hItem, HANDLE hItemAll, int iColumn, int iImage) {
	switch ( SendMessage(hwndList, CLM_GETITEMTYPE, (WPARAM)hItem, 0)) {
	case CLCIT_CONTACT:
		{
			int oldiImage = SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, iColumn);
			if (oldiImage != EMPTY_EXTRA_ICON && oldiImage != iImage)
				SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(iColumn, iImage));
		}
		break;
	case CLCIT_INFO:
		if (hItem == hItemAll)
			SetAllChildIcons(hwndList, hItem, iColumn, iImage);
		else
			SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(iColumn, iImage)); //hItemUnknown
		break;

	case CLCIT_GROUP:
		hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_CHILD, (LPARAM)hItem);
		if (hItem)
			SetAllChildIcons(hwndList, hItem, iColumn, iImage);
	}
}

void DialogConfigActive::InitialiseItem(HWND hwndList, HANDLE hItem, bool message, bool status) {
	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(0, (message)?0:3)); //Message
	
	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(1, (status)?0:4)); //Online

	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(2, 1));
	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(3, 2));
}

void DialogConfigActive::SetAllContactIcons(HWND hwndList, HWND window) {
	ConfigDatabase::ActiveUsersMap active_users = m_db.getActiveUsers();
	ConfigDatabase::ActiveUsersMap::const_iterator iter;
	for (iter = active_users.begin(); iter != active_users.end(); ++iter)
	{
		HANDLE hContact = iter->first;
		if(hContact == 0) {
			
		} else {
			HANDLE hItem = (HANDLE)SendMessage(hwndList, CLM_FINDCONTACT, (WPARAM)hContact, 0);
			if (hItem && SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(1, 0)) == EMPTY_EXTRA_ICON) {
				this->InitialiseItem(hwndList, hItem, iter->second.message, iter->second.status);
			}
		}
	}
}

void DialogConfigActive::ResetListOptions(HWND listview) {
	SendMessage(listview, CLM_SETBKBITMAP, 0, (LPARAM)(HBITMAP)NULL);
	SendMessage(listview, CLM_SETBKCOLOR, GetSysColor(COLOR_WINDOW), 0);
	SendMessage(listview, CLM_SETGREYOUTFLAGS, 0, 0);
	SendMessage(listview, CLM_SETLEFTMARGIN, 2, 0);
	SendMessage(listview, CLM_SETINDENT, 10, 0);
	SendMessage(listview, CLM_SETHIDEEMPTYGROUPS, 1, 0);

	for (int i=0; i <= FONTID_MAX; i++)
		SendMessage(listview, CLM_SETTEXTCOLOR, i, GetSysColor(COLOR_WINDOWTEXT));

	SetWindowLong(listview, GWL_STYLE, GetWindowLong(listview, GWL_STYLE) | CLS_SHOWHIDDEN);
}

void DialogConfigActive::SetListGroupIcons(HWND hwndList, HANDLE hFirstItem, HANDLE hParentItem, int *groupChildCount) {
	int iconOn[2] = {1, 1};
	int childCount[2] = {0, 0}, i;
	int iImage;
	HANDLE hItem, hChildItem;

	int typeOfFirst = SendMessage(hwndList, CLM_GETITEMTYPE, (WPARAM)hFirstItem, 0);
	//check groups
	if (typeOfFirst == CLCIT_GROUP) hItem = hFirstItem;
	else hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, (LPARAM)hFirstItem);
	while (hItem) {
		hChildItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_CHILD, (LPARAM)hItem);
		if (hChildItem) SetListGroupIcons(hwndList, hChildItem, hItem, childCount);
		for (i=0; i < SIZEOF(iconOn); i++)
			if (iconOn[i] && SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, i) == 0) iconOn[i] = 0;
		hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, (LPARAM)hItem);
	}
	//check contacts
	if (typeOfFirst == CLCIT_CONTACT) hItem = hFirstItem;
	else hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hFirstItem);
	while (hItem) {
		for (i=0; i < SIZEOF(iconOn); i++) {
			iImage = SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, i);
			if (iconOn[i] && iImage == 0) iconOn[i] = 0;
			if (iImage != EMPTY_EXTRA_ICON)
				childCount[i]++;
		}
		hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hItem);
	}
	//set icons
	for (i=0; i < SIZEOF(iconOn); i++) {
		SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hParentItem, MAKELPARAM(i, childCount[i]?(iconOn[i]?i+3:0) : EMPTY_EXTRA_ICON));
		if (groupChildCount) groupChildCount[i]+=childCount[i];
	}
	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hParentItem, MAKELPARAM(2, 1));
	SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hParentItem, MAKELPARAM(3, 2));
}

//------------------------------------------------------------------------------
void DialogConfigActive::save(HWND window)
{
	// store the checkboxes
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Online, 		(IsDlgButtonChecked(window, IDC_ACTIVE_ONLINE) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Away, 		(IsDlgButtonChecked(window, IDC_ACTIVE_AWAY) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Dnd, 		(IsDlgButtonChecked(window, IDC_ACTIVE_DND) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Na, 		(IsDlgButtonChecked(window, IDC_ACTIVE_NA) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Occupied, 		(IsDlgButtonChecked(window, IDC_ACTIVE_OCCUPIED) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_FreeForChat, 		(IsDlgButtonChecked(window, IDC_ACTIVE_FREEFORCHAT) != 0));
	m_db.setActiveFlag(ConfigDatabase::ActiveFlag_Invisible, 		(IsDlgButtonChecked(window, IDC_ACTIVE_INVISIBLE) != 0));

	for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
		HANDLE hItem = (HANDLE)SendDlgItemMessage(window, IDC_ACTIVE_USERS, CLM_FINDCONTACT, (WPARAM)hContact, 0);
		if (hItem) 
			SaveItemMask(GetDlgItem(window, IDC_ACTIVE_USERS), hContact, hItem);
	}

	SaveItemMask( GetDlgItem(window, IDC_ACTIVE_USERS), NULL, hItemUnknown);

	m_db.save();
}

void DialogConfigActive::SaveItemMask(HWND hwndList, HANDLE hContact, HANDLE hItem) {
	ConfigDatabase::act mask;
	mask.message = true;
	mask.status = true;
	for (int i=0; i < 2; i++) {
		int iImage = SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(i, 0));
		if (iImage && iImage != EMPTY_EXTRA_ICON) {
			if(i == 1) { //Online
				mask.status = false;
			} 
			if(i == 0) { //message
				mask.message = false;
			}
		}
	}
	m_db.setActiveUser(hContact, mask);
}

//------------------------------------------------------------------------------
void DialogConfigActive::selectAllUsers(HWND window, bool state)
{
	HWND listview = GetDlgItem(window, IDC_ACTIVE_USERS);

	for (int i = 0; i < ListView_GetItemCount(listview); ++i)
	{
		ListView_SetCheckState(listview, i, state);
	}

	changed(window);
}

//==============================================================================