summaryrefslogtreecommitdiff
path: root/protocols/Skype/src/skype_blocked.cpp
blob: b1a25dc4db77a40e2092a8336565b64d86f4c9a2 (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
#include "skype.h"
#include "skype_chat.h"

int CSkypeProto::BlockCommand(WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact)
	{
		SEString sid(_T2A(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_SID)));
			
		ContactRef contact;
		if ( !this->GetContact(sid, contact) || !contact)
			return 0;

		bool isBlocked = false;
		if (contact->IsMemberOfHardwiredGroup(ContactGroup::CONTACTS_BLOCKED_BY_ME, isBlocked) && !isBlocked)
		{
			BlockParam param(hContact, this);
			if (::DialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_BLOCK), NULL, CSkypeProto::SkypeBlockProc, (LPARAM)&param) == IDOK)
			{
				if ( !contact->SetBlocked(true, param.abuse))
					return 0;

				if (param.remove)
				{
					contact->SetBuddyStatus(false);
					this->contactList.remove_val(contact);
					::CallService(MS_DB_CONTACT_DELETE, wParam, 0);
				}
				else if (this->getByte(hContact, "IsSkypeOut", 0) > 0)
					this->setWord(hContact, "Status", ID_STATUS_OFFLINE);
			}
		}
		else
		{
			if (contact->SetBlocked(false))
				if (this->getByte(hContact, "IsSkypeOut", 0) > 0)
					this->setWord(hContact, "Status", ID_STATUS_ONTHEPHONE);
		}
	}

	return 0;
}

INT_PTR CSkypeProto::OpenBlockedListCommand(WPARAM, LPARAM)
{
	char *title = ::mir_t2a(this->m_tszUserName);
	OPENOPTIONSDIALOG ood;
	ood.cbSize = sizeof(OPENOPTIONSDIALOG);
	ood.pszGroup = "Network";
	ood.pszPage = title;
	ood.pszTab = "Blocked contacts";
	return ::Options_Open(&ood);
	::mir_free(title);
}

INT_PTR CALLBACK CSkypeProto::SkypeBlockProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	BlockParam *param = (BlockParam *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);

	switch (msg)
	{
	case WM_INITDIALOG:
		if (lParam)
		{
			param = (BlockParam *)lParam;
			::SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
			::TranslateDialogDefault(hwndDlg);			

			wchar_t *nick = (wchar_t *)::CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)param->hContact, GCDNF_TCHAR);

			TCHAR oldTitle[256], newTitle[256];
			::GetDlgItemText(hwndDlg, IDC_HEADERBAR, oldTitle, SIZEOF(oldTitle));
			::mir_sntprintf(newTitle, SIZEOF(newTitle), ::TranslateTS(oldTitle), nick);
			::SetDlgItemText(hwndDlg, IDC_HEADERBAR, newTitle);

			::SendMessage(hwndDlg, WM_SETICON, ICON_BIG,	(LPARAM)::Skin_GetIcon("Skype_block", ICON_BIG));
			::SendMessage(hwndDlg, WM_SETICON, ICON_SMALL,	(LPARAM)::Skin_GetIcon("Skype_block"));
			
			wchar_t text[1024];
			::mir_sntprintf(
				text, 
				SIZEOF(text), 
				::TranslateT("Are you sure you want to block \"%s\" (%s)? They won't be able to contact you and won't appear in your Contact List."),
				nick,
				ptrW(::db_get_wsa(param->hContact, param->ppro->m_szModuleName, SKYPE_SETTINGS_SID)));
			::SetDlgItemText(hwndDlg, IDC_MESSAGE, text);

			::Utils_RestoreWindowPosition(hwndDlg, 0, MODULE, "BlockWindow");
		}
		break;

	case WM_COMMAND:
		if (HIWORD( wParam ) == BN_CLICKED)
		{
			switch(LOWORD(wParam))
			{
			case IDOK:
				param->remove = ::IsDlgButtonChecked(hwndDlg, IDC_REMOVE_FROM_CL) > 0;
				param->abuse = ::IsDlgButtonChecked(hwndDlg, IDC_REPORT_ABUSE) > 0;

				::Utils_SaveWindowPosition(hwndDlg, NULL, MODULE, "BlockWindow");
				::Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_BIG, 0));
				::Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, 0));
				::EndDialog(hwndDlg, IDOK);
				break;

			case IDCANCEL:
				::Utils_SaveWindowPosition(hwndDlg, NULL, MODULE, "BlockWindow");
				::Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_BIG, 0));
				::Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, 0));
				::EndDialog(hwndDlg, IDCANCEL);
				break;
			}
		}
		break;
	}
	return FALSE;
}

static WNDPROC oldWndProc = NULL;

LRESULT CALLBACK CSkypeProto::SkypeBlockedOptionsSubProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (msg == WM_LBUTTONDOWN)
	{
		LVHITTESTINFO hi;
		hi.pt.x = LOWORD(lParam); hi.pt.y = HIWORD(lParam);
		ListView_SubItemHitTest(hwnd, &hi);
		if (hi.iSubItem == 1)
		{
			LVITEM lvi = {0};
			lvi.mask = LVIF_IMAGE | LVIF_PARAM;
			lvi.stateMask = -1;
			lvi.iItem = hi.iItem;
			if (ListView_GetItem(hwnd, &lvi))
			{
				ContactParam *param = (ContactParam *)lvi.lParam;
					
				if (param->contact->SetBlocked(false))
				{
					SEString data;
					param->contact->GetIdentity(data);
					ptrW sid(::mir_utf8decodeW(data));
					
					MCONTACT hContact = param->ppro->GetContactBySid(sid);
					if (::db_get_b(hContact, param->ppro->m_szModuleName, "IsSkypeOut", 0) > 0)
						::db_set_w(hContact, param->ppro->m_szModuleName, "Status", ID_STATUS_ONTHEPHONE);

					ListView_DeleteItem(hwnd, lvi.iItem);
					
					int nItem = ::SendMessage(::GetDlgItem(GetParent(hwnd), IDC_CONTACTS), CB_ADDSTRING, 0, (LPARAM)sid);
					::SendMessage(::GetDlgItem(GetParent(hwnd), IDC_CONTACTS), CB_SETITEMDATA, nItem, hContact);
				}
			}
		}
	}

	return ::CallWindowProc(oldWndProc, hwnd, msg, wParam, lParam);
}

int ImageList_AddIconFromIconLib(HIMAGELIST hIml, const char *name)
{
	HICON icon = ::Skin_GetIconByHandle(::Skin_GetIconHandle(name));
	int res = ImageList_AddIcon(hIml, icon);
	::Skin_ReleaseIcon(icon);
	return res;
}

INT_PTR CALLBACK CSkypeProto::SkypeBlockedOptionsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CSkypeProto *ppro = (CSkypeProto *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);

	switch (msg)
	{
	case WM_INITDIALOG:
		if (lParam)
		{
			ppro = (CSkypeProto *)lParam;
			::SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);

			::TranslateDialogDefault(hwndDlg);			

			HWND hwndList = ::GetDlgItem(hwndDlg, IDC_LIST);
			{   // IDC_BM_LIST setup
				oldWndProc = (WNDPROC)::SetWindowLongPtr(hwndList, GWLP_WNDPROC, (LONG_PTR)SkypeBlockedOptionsSubProc);

				HIMAGELIST hIml = ImageList_Create(16, 16, ILC_MASK | ILC_COLOR32, 4, 0);
				ImageList_AddIconFromIconLib(hIml, "Skype_contact");
				ImageList_AddIconFromIconLib(hIml, "Skype_delete");
				ListView_SetImageList(hwndList, hIml, LVSIL_SMALL);
				///
				LVCOLUMN lvc = {0};
				lvc.mask = LVCF_WIDTH | LVCF_TEXT;
				//lvc.fmt = LVCFMT_JUSTIFYMASK;
				lvc.pszText = TranslateT("Name");
				lvc.cx = 220; // width of column in pixels
				ListView_InsertColumn(hwndList, 0, &lvc);
				//lvc.fmt = LVCFMT_RIGHT;
				lvc.pszText = L"";
				lvc.cx = 32 - GetSystemMetrics(SM_CXVSCROLL); // width of column in pixels
				ListView_InsertColumn(hwndList, 1, &lvc);
				///
				::SendMessage(hwndList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);

				if ( !ppro->IsOnline())
				{
					::EnableWindow(hwndList, FALSE);
					::EnableWindow(::GetDlgItem(hwndDlg, IDC_CONTACTS), FALSE);
				}
			}

			if (ppro->IsOnline())
			{
				SEString data;
				ContactGroupRef blockedList;
				ppro->GetHardwiredContactGroup(ContactGroup::CONTACTS_BLOCKED_BY_ME, blockedList);
			
				CContact::Refs contacts;
				blockedList->GetContacts(contacts);
				for (size_t i = 0; i < contacts.size(); i++)
				{
					auto contact = contacts[i];

					ptrW sid(::mir_utf8decodeW(contact->GetSid()));

					LVITEM lvi = {0};
					lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
					lvi.iItem = (int)i;
					lvi.iImage = 0;
					lvi.lParam = (LPARAM)new ContactParam(contact, ppro);
					lvi.pszText = sid;
					int iRow = ListView_InsertItem(hwndList, &lvi);

					if (iRow != -1)
					{
						lvi.iItem = iRow;
						lvi.mask = LVIF_IMAGE;
						lvi.iSubItem = 1;
						lvi.iImage = 1;
						ListView_SetItem(hwndList, &lvi);
					}
				}
				///
				int nItem = 0;
				MCONTACT hContact = NULL;

				::EnterCriticalSection(&ppro->contact_search_lock);

				for (hContact = ::db_find_first(ppro->m_szModuleName); hContact && !ppro->isChatRoom(hContact); hContact = ::db_find_next(hContact, ppro->m_szModuleName))
				{
					ptrW sid(::db_get_wsa(hContact, ppro->m_szModuleName, SKYPE_SETTINGS_SID));

					ContactRef contact;
					ppro->GetContact((char *)_T2A(sid), contact);
					if ( !contacts.contains(contact))
					{
						nItem = ::SendMessage(::GetDlgItem(hwndDlg, IDC_CONTACTS), CB_ADDSTRING, 0, (LPARAM)sid);
						::SendMessage(::GetDlgItem(hwndDlg, IDC_CONTACTS), CB_SETITEMDATA, nItem, hContact);
					}
				}

				::LeaveCriticalSection(&ppro->contact_search_lock);
			}

		}
		break;

	case WM_COMMAND:
		{
			switch(LOWORD(wParam))
			{
			case IDC_CONTACTS:
				::EnableWindow(::GetDlgItem(hwndDlg, IDC_BLOCK), TRUE);
				break;

			case IDC_BLOCK:
				{
					int i = ::SendMessage(::GetDlgItem(hwndDlg, IDC_CONTACTS), CB_GETCURSEL, 0, 0);

					MCONTACT hContact = (MCONTACT)::SendMessage(GetDlgItem(hwndDlg, IDC_CONTACTS), CB_GETITEMDATA, i, 0);
					if ( !hContact)
						break;

					ptrW sid(::db_get_wsa(hContact, ppro->m_szModuleName, SKYPE_SETTINGS_SID));

					SEString data;
					ContactRef contact;
					if (!ppro->GetContact((char *)_T2A(sid), contact) || !contact)
						break;

					BlockParam param(hContact, ppro);
					if (::DialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_BLOCK), NULL, CSkypeProto::SkypeBlockProc, (LPARAM)&param) != IDOK)
						break;

					contact->SetBlocked(true, param.abuse);
					if (::db_get_b(hContact, ppro->m_szModuleName, "IsSkypeOut", 0) > 0)
						::db_set_w(hContact, ppro->m_szModuleName, "Status", ID_STATUS_OFFLINE);

					if (param.remove)
					{
						contact->SetBuddyStatus(false);
						ppro->contactList.remove_val(contact);
						::CallService(MS_DB_CONTACT_DELETE, wParam, 0);
					}

					if (contact->SetBlocked(true))
					{
						LVITEM lvi = {0};
						lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
						lvi.iItem = (int)i;
						lvi.iImage = 0;
						lvi.lParam = (LPARAM)new ContactParam(contact, ppro);
						lvi.pszText = sid;
						int iRow = ListView_InsertItem(::GetDlgItem(hwndDlg, IDC_LIST), &lvi);

						if (iRow != -1)
						{
							lvi.iItem = iRow;
							lvi.mask = LVIF_IMAGE;
							lvi.iSubItem = 1;
							lvi.iImage = 1;
							ListView_SetItem(::GetDlgItem(hwndDlg, IDC_LIST), &lvi);
						}
						::SendMessage(::GetDlgItem(hwndDlg, IDC_CONTACTS), CB_DELETESTRING, i, 0);
					}
				}
				break;
			}
		}
		break;

	case WM_NOTIFY:
		if (reinterpret_cast<NMHDR*>(lParam)->code == PSN_APPLY && !ppro->IsOnline())
		{
			return TRUE;
		}
		break;

		switch(LOWORD(wParam))
        {
        case IDC_LIST:
            if (((LPNMHDR)lParam)->code == NM_DBLCLK)
            {
				HWND hwndList = ::GetDlgItem(hwndDlg, IDC_BM_LIST);
				int iItem = ListView_GetNextItem(hwndList, -1, LVNI_ALL | LVNI_SELECTED);
				if (iItem < 0) break;
				LVITEM lvi = {0};
				lvi.mask = LVIF_PARAM | LVIF_GROUPID;
				lvi.stateMask = -1;
				lvi.iItem = iItem;
				if (ListView_GetItem(hwndList, &lvi))
				{
					SEString data;
					if (lvi.iGroupId == 1)
					{
						ContactParam *param = (ContactParam *)lvi.lParam;

						param->contact->GetIdentity(data);
						ptrW sid(::mir_utf8decodeW(data));
						::CallService(MS_MSG_SENDMESSAGE, (WPARAM)ppro->GetContactBySid(sid), 0);
					}
				}
			}
		}
		break;
	}
	return FALSE;
}