summaryrefslogtreecommitdiff
path: root/src/modules/addcontact/addcontact.cpp
blob: e81b7c2ce22238d5281056def7b9796d084ac771 (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-12 Miranda IM, 2012-13 Miranda NG project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"

INT_PTR CALLBACK AddContactDlgProc(HWND hdlg, UINT msg, WPARAM wparam, LPARAM lparam)
{
	ADDCONTACTSTRUCT *acs;

	switch(msg) {
	case WM_INITDIALOG:
		{
			char szUin[10];
			acs = (ADDCONTACTSTRUCT *)lparam;
			SetWindowLongPtr(hdlg, GWLP_USERDATA, (LONG_PTR)acs);

			TranslateDialogDefault(hdlg);
			Window_SetIcon_IcoLib(hdlg, SKINICON_OTHER_ADDCONTACT);
			if (acs->handleType == HANDLE_EVENT) {
				DWORD dwUin;
				DBEVENTINFO dbei = { sizeof(dbei) };
				dbei.cbBlob = sizeof(DWORD);
				dbei.pBlob = (PBYTE)&dwUin;
				db_event_get(acs->handle, &dbei);
				_ltoa(dwUin, szUin, 10);
				acs->szProto = dbei.szModule;
			}
			{
				TCHAR *szName = NULL, *tmpStr = NULL;
				if (acs->handleType == HANDLE_CONTACT)
					szName = cli.pfnGetContactDisplayName(acs->handle, GCDNF_TCHAR);
				else {
					int isSet = 0;

					if (acs->handleType == HANDLE_EVENT) {
						DBEVENTINFO dbei = { sizeof(dbei) };
						dbei.cbBlob = db_event_getBlobSize(acs->handle);
						dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob);
						db_event_get(acs->handle, &dbei);
						HANDLE hcontact = *((PHANDLE)(dbei.pBlob+sizeof(DWORD)));
						mir_free(dbei.pBlob);
						if (hcontact != INVALID_HANDLE_VALUE) {
							szName = cli.pfnGetContactDisplayName(hcontact, 0);
							isSet = 1;
						}
					}
					if ( !isSet) {
						szName = (acs->handleType == HANDLE_EVENT) ? (tmpStr = mir_a2t(szUin)) :
							(acs->psr->id ? acs->psr->id : acs->psr->nick);
					}
				}

				if (szName && szName[0]) {
					TCHAR  szTitle[128];
					mir_sntprintf(szTitle, SIZEOF(szTitle), TranslateT("Add %s"), szName);
					SetWindowText(hdlg, szTitle);
				}
				else SetWindowText(hdlg, TranslateT("Add contact"));
				mir_free(tmpStr);
			}
		}

		if (acs->handleType == HANDLE_CONTACT && acs->handle)
			if (acs->szProto == NULL || (acs->szProto != NULL && *acs->szProto == 0))
				acs->szProto = GetContactProto(acs->handle);

		{
			TCHAR *grpName;
			for (int groupId = 1; (grpName = cli.pfnGetGroupName(groupId, NULL)) != NULL; groupId++) {
				int id = SendDlgItemMessage(hdlg, IDC_GROUP, CB_ADDSTRING, 0, (LPARAM)grpName);
				SendDlgItemMessage(hdlg, IDC_GROUP, CB_SETITEMDATA, id, groupId);
			}
		}

		SendDlgItemMessage(hdlg, IDC_GROUP, CB_INSERTSTRING, 0, (LPARAM)TranslateT("None"));
		SendDlgItemMessage(hdlg, IDC_GROUP, CB_SETCURSEL, 0, 0);
		/* acs->szProto may be NULL don't expect it */
		{
			// By default check both checkboxes
			CheckDlgButton(hdlg, IDC_ADDED, BST_CHECKED);
			CheckDlgButton(hdlg, IDC_AUTH, BST_CHECKED);
			
			// Set last choice
			if (db_get_b(NULL, "Miranda", "AuthOpenWindow", 1))
				CheckDlgButton(hdlg, IDC_OPEN_WINDOW, BST_CHECKED);

			DWORD flags = (acs->szProto) ? CallProtoServiceInt(NULL,acs->szProto, PS_GETCAPS, PFLAGNUM_4, 0) : 0;
			if (flags&PF4_FORCEADDED) { // force you were added requests for this protocol
				EnableWindow( GetDlgItem(hdlg, IDC_ADDED), FALSE);
			}
			if (flags&PF4_FORCEAUTH) { // force auth requests for this protocol
				EnableWindow( GetDlgItem(hdlg, IDC_AUTH), FALSE);
			}
			if (flags&PF4_NOCUSTOMAUTH) {
				EnableWindow( GetDlgItem(hdlg, IDC_AUTHREQ), FALSE);
				EnableWindow( GetDlgItem(hdlg, IDC_AUTHGB), FALSE);
			}
			else {
				EnableWindow( GetDlgItem(hdlg, IDC_AUTHREQ), IsDlgButtonChecked(hdlg, IDC_AUTH));
				EnableWindow( GetDlgItem(hdlg, IDC_AUTHGB), IsDlgButtonChecked(hdlg, IDC_AUTH));
				SetDlgItemText(hdlg, IDC_AUTHREQ, TranslateT("Please authorize my request and add me to your contact list."));
			}
		}
		break;

	case WM_COMMAND:
		acs = (ADDCONTACTSTRUCT *)GetWindowLongPtr(hdlg, GWLP_USERDATA);

		switch (LOWORD(wparam)) {
		case IDC_AUTH:
			{
				DWORD flags = CallProtoServiceInt(NULL,acs->szProto, PS_GETCAPS, PFLAGNUM_4, 0);
				if (flags & PF4_NOCUSTOMAUTH) {
					EnableWindow( GetDlgItem(hdlg, IDC_AUTHREQ), FALSE);
					EnableWindow( GetDlgItem(hdlg, IDC_AUTHGB), FALSE);
				}
				else {
					EnableWindow( GetDlgItem(hdlg, IDC_AUTHREQ), IsDlgButtonChecked(hdlg, IDC_AUTH));
					EnableWindow( GetDlgItem(hdlg, IDC_AUTHGB), IsDlgButtonChecked(hdlg, IDC_AUTH));
				}
			}
			break;
		case IDC_OPEN_WINDOW:
			// Remember this choice
			db_set_b(NULL, "Miranda", "AuthOpenWindow", IsDlgButtonChecked(hdlg, IDC_OPEN_WINDOW));
			break;
		case IDOK:
			{
				HANDLE hContact = INVALID_HANDLE_VALUE;
				switch (acs->handleType) {
				case HANDLE_EVENT:
					{
						DBEVENTINFO dbei = { sizeof(dbei) };
						db_event_get(acs->handle, &dbei);
						hContact = (HANDLE)CallProtoServiceInt(NULL,dbei.szModule, PS_ADDTOLISTBYEVENT, 0, (LPARAM)acs->handle);
					}
					break;

				case HANDLE_SEARCHRESULT:
					hContact = (HANDLE)CallProtoServiceInt(NULL,acs->szProto, PS_ADDTOLIST, 0, (LPARAM)acs->psr);
					break;

				case HANDLE_CONTACT:
					hContact = acs->handle;
					break;
				}

				if (hContact == NULL)
					break;

				TCHAR szHandle[256];
				if ( GetDlgItemText(hdlg, IDC_MYHANDLE, szHandle, SIZEOF(szHandle)))
					db_set_ts(hContact, "CList", "MyHandle", szHandle);

				int item = SendDlgItemMessage(hdlg, IDC_GROUP, CB_GETCURSEL, 0, 0);
				if (item > 0) {
					item = SendDlgItemMessage(hdlg, IDC_GROUP, CB_GETITEMDATA, item, 0);
					CallService(MS_CLIST_CONTACTCHANGEGROUP, (WPARAM)hContact, item);
				}

				db_unset(hContact, "CList", "NotOnList");

				if (IsDlgButtonChecked(hdlg, IDC_ADDED))
					CallContactService(hContact, PSS_ADDED, 0, 0);

				if (IsDlgButtonChecked(hdlg, IDC_AUTH)) {
					DWORD flags = CallProtoServiceInt(NULL,acs->szProto, PS_GETCAPS, PFLAGNUM_4, 0);
					if (flags & PF4_NOCUSTOMAUTH)
						CallContactService(hContact, PSS_AUTHREQUESTT, 0, 0);
					else {
						TCHAR szReason[512];
						GetDlgItemText(hdlg, IDC_AUTHREQ, szReason, SIZEOF(szReason));
						CallContactService(hContact, PSS_AUTHREQUESTT, 0, (LPARAM)szReason);
					}
				}

				if (IsDlgButtonChecked(hdlg, IDC_OPEN_WINDOW))
					CallService(MS_CLIST_CONTACTDOUBLECLICKED, (WPARAM)hContact, 0);
			}
			// fall through
		case IDCANCEL:
			if (GetParent(hdlg) == NULL)
				DestroyWindow(hdlg);
			else
				EndDialog(hdlg, 0);
			break;
		}
		break;

	case WM_CLOSE:
		/* if there is no parent for the dialog, its a modeless dialog and can't be killed using EndDialog() */
		if (GetParent(hdlg) == NULL)
			DestroyWindow(hdlg);
		else
			EndDialog(hdlg, 0);
		break;

	case WM_DESTROY:
		Window_FreeIcon_IcoLib(hdlg);
		acs = (ADDCONTACTSTRUCT*)GetWindowLongPtr(hdlg, GWLP_USERDATA);
		if (acs) {
			if (acs->psr) {
				mir_free(acs->psr->nick);
				mir_free(acs->psr->firstName);
				mir_free(acs->psr->lastName);
				mir_free(acs->psr->email);
				mir_free(acs->psr);
			}
			mir_free(acs);
		}
		break;
	}

	return FALSE;
}

INT_PTR AddContactDialog(WPARAM wParam, LPARAM lParam)
{
	if (lParam) {
		ADDCONTACTSTRUCT* acs = (ADDCONTACTSTRUCT*)mir_alloc(sizeof(ADDCONTACTSTRUCT));
		memmove(acs, (ADDCONTACTSTRUCT*)lParam, sizeof(ADDCONTACTSTRUCT));
		if (acs->psr) {
			PROTOSEARCHRESULT *psr;
			/* bad! structures that are bigger than psr will cause crashes if they define pointers within unreachable structural space */
			psr = (PROTOSEARCHRESULT *)mir_alloc(acs->psr->cbSize);
			memmove(psr, acs->psr, acs->psr->cbSize);
			psr->nick = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->nick) : mir_a2t((char*)psr->nick);
			psr->firstName = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->firstName) : mir_a2t((char*)psr->firstName);
			psr->lastName = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->lastName) : mir_a2t((char*)psr->lastName);
			psr->email = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->email) : mir_a2t((char*)psr->email);
			psr->flags = psr->flags & ~PSR_UNICODE | PSR_TCHAR;
			acs->psr = psr;
			/* copied the passed acs structure, the psr structure with, the pointers within that  */
		}

		if (wParam)
			DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ADDCONTACT), (HWND)wParam, AddContactDlgProc, (LPARAM)acs);
		else
			CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ADDCONTACT), (HWND)wParam, AddContactDlgProc, (LPARAM)acs);
		return 0;
	}
	return 1;
}

int LoadAddContactModule(void)
{
	CreateServiceFunction(MS_ADDCONTACT_SHOW, AddContactDialog);
	return 0;
}