summaryrefslogtreecommitdiff
path: root/plugins/SkypeStatusChange/src/options.cpp
blob: 1fc7a4b93bf5f3970ed3ac1c0f519131052b9d9a (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
#include "stdafx.h"
#include "resource.h"

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

struct CTreeItemData
{
	enum EType
	{
		Protocol,
		Status
	};

	EType m_nType;
	char* m_pszModule;
	int m_nStatus;
};

enum ETreeCheckBoxState
{
	// tree check box state
	TCBS_NOSTATEBOX = 0,
	TCBS_UNCHECKED = 1,
	TCBS_CHECKED = 2,
	TCBS_DISABLE_UNCHECKED = 3,
	TCBS_DISABLE_CHECKED = 4,
};

HTREEITEM tree_insert_item(HWND hwndTree, wchar_t *pName, HTREEITEM htiParent, ETreeCheckBoxState nState, CTreeItemData *pData)
{
	TVINSERTSTRUCT tvi = {};
	tvi.hParent = htiParent;
	tvi.hInsertAfter = TVI_LAST;
	tvi.item.mask = TVIF_TEXT|TVIF_PARAM|TVIF_STATE;
	tvi.item.pszText = pName;
	tvi.item.lParam = reinterpret_cast<LPARAM>(pData);
	tvi.item.stateMask = TVIS_STATEIMAGEMASK;
	tvi.item.state = INDEXTOSTATEIMAGEMASK(nState);
	return TreeView_InsertItem(hwndTree,&tvi);
}

void InitProtocolTree(HWND hwndTreeCtrl)
{
	int cAccounts = 0;
	PROTOACCOUNT** ppAccount;

	enum{OFFLINE_STATUS_INDEX = 5};

	Proto_EnumAccounts(&cAccounts, &ppAccount);
	for(int i = 0; i < cAccounts;++i)
	{
		PROTOACCOUNT* pAccount = ppAccount[i];
		CTreeItemData* pItemData = new CTreeItemData;
		pItemData->m_nType = CTreeItemData::Protocol;
		pItemData->m_pszModule = pAccount->szModuleName;

		bool bProtocolExcluded = g_Options.IsProtocolExcluded(pAccount->szModuleName);
		HTREEITEM hti = tree_insert_item(hwndTreeCtrl,pAccount->tszAccountName,TVI_ROOT,((true == bProtocolExcluded) ? TCBS_CHECKED : TCBS_UNCHECKED),pItemData);
		if (hti)
		{
			int nStatusBits = CallProtoService(pAccount->szModuleName,PS_GETCAPS,PFLAGNUM_2,0);
			int nStatusExcluded = CallProtoService(pAccount->szModuleName,PS_GETCAPS,PFLAGNUM_5,0);
			pItemData = new CTreeItemData;
			pItemData->m_nType = CTreeItemData::Status;
			pItemData->m_pszModule = pAccount->szModuleName;
			pItemData->m_nStatus = ID_STATUS_OFFLINE;
			bool bStatusExcluded = g_Options.IsProtocolStatusExcluded(pAccount->szModuleName,pItemData->m_nStatus);
			ETreeCheckBoxState nState = TCBS_UNCHECKED;
			if (bProtocolExcluded) {
				if (bStatusExcluded)
					nState = TCBS_DISABLE_CHECKED;
				else
					nState = TCBS_DISABLE_UNCHECKED;
			}
			else {
				if (bStatusExcluded)
					nState = TCBS_CHECKED;
				else
					nState = TCBS_UNCHECKED;
			}
			tree_insert_item(hwndTreeCtrl,TranslateW(g_aStatusCode[OFFLINE_STATUS_INDEX].m_ptszStatusName),hti,nState,pItemData);
			for(size_t k = 0; k < _countof(g_aStatusCode); ++k) {
				const CMirandaStatus2SkypeStatus& m2s = g_aStatusCode[k];
				unsigned long statusFlags = Proto_Status2Flag(m2s.m_nMirandaStatus);
				if ((m2s.m_nMirandaStatus != ID_STATUS_OFFLINE) && (nStatusBits & statusFlags) && !(nStatusExcluded & statusFlags)) {
					pItemData = new CTreeItemData;
					pItemData->m_nType = CTreeItemData::Status;
					pItemData->m_pszModule = pAccount->szModuleName;
					pItemData->m_nStatus = m2s.m_nMirandaStatus;
					bStatusExcluded = g_Options.IsProtocolStatusExcluded(pAccount->szModuleName,pItemData->m_nStatus);
					if (bProtocolExcluded) {
						if (bStatusExcluded)
							nState = TCBS_DISABLE_CHECKED;
						else
							nState = TCBS_DISABLE_UNCHECKED;
					}
					else {
						if (bStatusExcluded)
							nState = TCBS_CHECKED;
						else
							nState = TCBS_UNCHECKED;
					}

					tree_insert_item(hwndTreeCtrl,TranslateW(m2s.m_ptszStatusName),hti,nState,pItemData);
				}
			}

			TreeView_Expand(hwndTreeCtrl,hti,TVE_EXPAND);
		}
	}
}

inline HTREEITEM tree_get_child_item(HWND hwndTreeCtrl,HTREEITEM hti)
{
	return TreeView_GetChild(hwndTreeCtrl,hti);
}

inline HTREEITEM tree_get_next_sibling_item(HWND hwndTreeCtrl,HTREEITEM hti)
{
	return TreeView_GetNextSibling(hwndTreeCtrl,hti);
}

const CTreeItemData* get_item_data(HWND hwndTreeCtrl,HTREEITEM hti)
{
	TVITEM tvi = {0};
	tvi.hItem = hti;
	tvi.mask = TVIF_PARAM|TVIF_HANDLE;
	if (TRUE == ::SendMessage(hwndTreeCtrl,TVM_GETITEM,0,reinterpret_cast<LPARAM>(&tvi))) {
		CTreeItemData* pData = reinterpret_cast<CTreeItemData*>(tvi.lParam);
		return pData;
	}
	return nullptr;
}


inline ETreeCheckBoxState tree_get_state_image(HWND hwndTree,HTREEITEM hti)
{
	TVITEM tvi;
	tvi.hItem = hti;
	tvi.mask = TVIF_STATE|TVIF_HANDLE;
	tvi.stateMask = TVIS_STATEIMAGEMASK;
	if (TRUE == ::SendMessage(hwndTree,TVM_GETITEM,0,reinterpret_cast<LPARAM>(&tvi)))
	{
		UINT nState = (tvi.state >> 12);
		return static_cast<ETreeCheckBoxState>(nState);
	}

	assert(!"we should never get here!");
	return TCBS_UNCHECKED;
}

void FreeMemory(HWND hwndTreeCtrl,HTREEITEM hti)
{
	for(HTREEITEM h = tree_get_child_item(hwndTreeCtrl,hti);h;h = tree_get_next_sibling_item(hwndTreeCtrl,h)) {
		FreeMemory(hwndTreeCtrl,h);
		const CTreeItemData* pData = get_item_data(hwndTreeCtrl,h);
		if (pData)
			delete pData;
	}
}

bool tree_set_item_state(HWND hwndTree,HTREEITEM hti,ETreeCheckBoxState nState)
{
	TVITEM tvi;
	memset(&tvi, 0, sizeof(tvi));

	tvi.mask = TVIF_STATE|TVIF_HANDLE;
	tvi.hItem = hti;

	tvi.stateMask = TVIS_STATEIMAGEMASK;
	tvi.state = INDEXTOSTATEIMAGEMASK(nState);

	return TRUE == ::SendMessage(hwndTree,TVM_SETITEM,0,reinterpret_cast<LPARAM>(&tvi));
}

void disable_children(HWND hwndTree,HTREEITEM htiParent,bool bDisable)
{
	for(HTREEITEM hti = tree_get_child_item(hwndTree,htiParent);hti;hti = tree_get_next_sibling_item(hwndTree,hti)) {
		ETreeCheckBoxState nState = tree_get_state_image(hwndTree,hti);
		if (bDisable) {
			if (TCBS_CHECKED == nState)
				nState = TCBS_DISABLE_CHECKED;
			else if (TCBS_UNCHECKED == nState)
				nState = TCBS_DISABLE_UNCHECKED;
		}
		else {
			if (TCBS_DISABLE_CHECKED == nState)
				nState = TCBS_CHECKED;
			else if (TCBS_DISABLE_UNCHECKED == nState)
				nState = TCBS_UNCHECKED;
		}
		tree_set_item_state(hwndTree,hti,nState);
	}
}

void save_exclusion_list(HWND hwndTree,HTREEITEM htiParent)
{
	for(HTREEITEM hti = tree_get_child_item(hwndTree,htiParent);hti;hti = tree_get_next_sibling_item(hwndTree,hti)) {
		const CTreeItemData* pData = get_item_data(hwndTree,hti);
		ETreeCheckBoxState nState = tree_get_state_image(hwndTree,hti);
		if (CTreeItemData::Protocol == pData->m_nType) {
			g_Options.ExcludeProtocol(pData->m_pszModule,TCBS_CHECKED == nState);
			save_exclusion_list(hwndTree,hti);
		}
		else g_Options.ExcludeProtocolStatus(pData->m_pszModule,pData->m_nStatus,((TCBS_CHECKED == nState) || (TCBS_DISABLE_CHECKED == nState)));
	}			
}

class CImageListWrapper
{
public:
	CImageListWrapper()
		: m_hImageList(ImageList_LoadImage(g_hModule,MAKEINTRESOURCE(IDB_TREE_STATE),16,0,RGB(255,255,255),IMAGE_BITMAP,LR_DEFAULTCOLOR))
	{
	}

	~CImageListWrapper()
	{
		if (m_hImageList)
			ImageList_Destroy(m_hImageList);
	}

	operator HIMAGELIST()const
	{
		return m_hImageList;
	}

private:
	HIMAGELIST m_hImageList;
};

HIMAGELIST get_image_list()
{
	static CImageListWrapper wrapper;
	return wrapper;
}

static INT_PTR CALLBACK SettingsDlgProc(HWND hdlg,UINT msg,WPARAM wp,LPARAM lp)
{
	switch(msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hdlg);
		{
			HWND hwndTreeCtrl = GetDlgItem(hdlg,IDC_TREE_PROTOCOLS);
			HIMAGELIST hImage = get_image_list();
			if (hImage)
				TreeView_SetImageList(hwndTreeCtrl,hImage,TVSIL_STATE);
			InitProtocolTree(hwndTreeCtrl);
		}
		CheckDlgButton(hdlg, IDC_CHECK_SYNCK_STATUS_MSG, (g_Options.GetSyncStatusMsgFlag()) ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hdlg, IDC_CHECK_STATUSES, (g_Options.GetSyncStatusStateFlag()) ? BST_CHECKED : BST_UNCHECKED);				
		return TRUE;

	case WM_NOTIFY:
		{
			LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lp);
			switch(pNMHDR->code) {
			case PSN_APPLY:
				save_exclusion_list(GetDlgItem(hdlg,IDC_TREE_PROTOCOLS),TVI_ROOT);
				g_Options.SetSyncStatusMsgFlag(1 == IsDlgButtonChecked(hdlg,IDC_CHECK_SYNCK_STATUS_MSG));
				g_Options.SetSyncStatusStateFlag(1 == IsDlgButtonChecked(hdlg,IDC_CHECK_STATUSES));
				break;

			case NM_CLICK:
				if (IDC_TREE_PROTOCOLS == wp) {
					DWORD pos = ::GetMessagePos();

					HWND hwndTree = ::GetDlgItem(hdlg,IDC_TREE_PROTOCOLS);

					TVHITTESTINFO tvhti;
					tvhti.pt.x = LOWORD(pos);
					tvhti.pt.y = HIWORD(pos);
					::ScreenToClient(hwndTree,&(tvhti.pt));

					HTREEITEM hti = reinterpret_cast<HTREEITEM>(::SendMessage(hwndTree,TVM_HITTEST,0,reinterpret_cast<LPARAM>(&tvhti)));
					if (hti && (tvhti.flags&(TVHT_ONITEMSTATEICON|TVHT_ONITEMICON))) {
						ETreeCheckBoxState nState = tree_get_state_image(hwndTree,hti);
						if (TCBS_CHECKED == nState || TCBS_UNCHECKED == nState) {
							if (TCBS_CHECKED == nState)
								nState = TCBS_UNCHECKED;
							else
								nState = TCBS_CHECKED;

							tree_set_item_state(hwndTree,hti,nState);
							disable_children(hwndTree,hti,TCBS_CHECKED == nState);
							PropSheet_Changed(::GetParent(hdlg),hdlg);
						}
					}
				}
			}
		}
		break;

	case WM_COMMAND:
		if (BN_CLICKED == HIWORD(wp) && ((IDC_CHECK_SYNCK_STATUS_MSG == LOWORD(wp)) || (IDC_CHECK_STATUSES == LOWORD(wp))))
			PropSheet_Changed(::GetParent(hdlg),hdlg);
		break;

	case WM_DESTROY:
		FreeMemory(GetDlgItem(hdlg,IDC_TREE_PROTOCOLS),TVI_ROOT);
		break;
	}
	return FALSE;
}

int SSC_OptInitialise(WPARAM wp, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 910000000;
	odp.hInstance = g_hModule;
	odp.szTitle.a = LPGEN("Change Skype status");
	odp.szGroup.a = LPGEN("Plugins");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG_SETTINGS);
	odp.pfnDlgProc = SettingsDlgProc;
	Options_AddPage(wp, &odp);
	return 0;
}