summaryrefslogtreecommitdiff
path: root/plugins/Exchange/src/dlg_handlers.cpp
blob: cc337564d54294fb0bd3dd454d96afba4d065a4d (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
/*
Exchange notifier plugin for Miranda IM

Copyright © 2006 Cristian Libotean, Attila Vajda

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 "dlg_handlers.h"

#define MIN_EMAILS_WIDTH 300
#define MIN_EMAILS_HEIGHT 250

static WNDPROC OldListProc;

INT_PTR CALLBACK DlgProcOptions(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int bInitializing; //true when dialog is being created
	
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		{
			bInitializing = 1;
			DBVARIANT dbv = {0};
			dbv.type = DBVT_ASCIIZ;

			int bCheck = db_get_b(NULL, ModuleName, "Check", 1);

			TCHAR buffer[4096];
			GetStringFromDatabase("Username", _T(""), buffer, SIZEOF(buffer));
			SetWindowText(GetDlgItem(hWnd, IDC_USER_EDIT), buffer);

			GetStringFromDatabase("Password", _T(""), buffer, SIZEOF(buffer));
			SetWindowText(GetDlgItem(hWnd, IDC_PASSWORD_EDIT), buffer);

			GetStringFromDatabase("Server", _T(""), buffer, sizeof(buffer));
			SetWindowText(GetDlgItem(hWnd, IDC_SERVER_EDIT), buffer);

			SetDlgItemInt(hWnd, IDC_PORT_EDIT, db_get_dw(NULL, ModuleName, "Port", EXCHANGE_PORT), FALSE);

			SetDlgItemInt(hWnd, IDC_INTERVAL_EDIT, db_get_dw(NULL, ModuleName, "Interval", DEFAULT_INTERVAL), FALSE);

			CheckDlgButton(hWnd, IDC_RECONNECT, (db_get_b(NULL, ModuleName, "Reconnect", 0)) ? BST_CHECKED : BST_UNCHECKED);

			SetDlgItemInt(hWnd, IDC_RECONNECT_INTERVAL, db_get_dw(NULL, ModuleName, "ReconnectInterval", DEFAULT_RECONNECT_INTERVAL), FALSE);
			CheckDlgButton(hWnd, IDC_USE_POPUPS, (BOOL) db_get_b(NULL, ModuleName, "UsePopups", 0) ? BST_CHECKED : BST_UNCHECKED);
			EnableWindow(GetDlgItem(hWnd, IDC_USE_POPUPS), ServiceExists(MS_POPUP_ADDPOPUP)); //disable the popups checkbox if no popup module is present

			CheckDlgButton(hWnd, IDC_CHECK_EMAILS, (bCheck) ? BST_CHECKED : BST_UNCHECKED);
			EnableWindow(GetDlgItem(hWnd, IDC_INTERVAL_EDIT), bCheck);

			int portCheck = db_get_b(NULL, ModuleName, "UsePortCheck", 1);
			CheckDlgButton(hWnd, IDC_USE_PORTCHECK, (portCheck) ? BST_CHECKED : BST_UNCHECKED);
			EnableWindow(GetDlgItem(hWnd, IDC_PORT_EDIT), portCheck);

			int retries = db_get_b(NULL, ModuleName, "MaxRetries", MAX_EXCHANGE_CONNECT_RETRIES);
			SetDlgItemInt(hWnd,IDC_MAX_RETRIES,retries,FALSE);

			EnableWindow(GetDlgItem(hWnd, IDC_RECONNECT_INTERVAL), IsDlgButtonChecked(hWnd, IDC_RECONNECT));

			bInitializing = 0;
		}
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_USER_EDIT:
		case IDC_PASSWORD_EDIT:
		case IDC_SERVER_EDIT:
		case IDC_PORT_EDIT:
		case IDC_INTERVAL_EDIT:
		case IDC_RECONNECT_INTERVAL:
		case IDC_MAX_RETRIES:
			if ((!bInitializing) && (HIWORD(wParam) == EN_CHANGE))// || (HIWORD(wParam) == CBN_SELENDOK))
				SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
			break;

		case IDC_USE_POPUPS:
		case IDC_CHECK_EMAILS:
		case IDC_RECONNECT:
		case IDC_USE_PORTCHECK:
			int portCheck = IsDlgButtonChecked(hWnd, IDC_USE_PORTCHECK);
			SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);

			EnableWindow(GetDlgItem(hWnd, IDC_INTERVAL_EDIT), IsDlgButtonChecked(hWnd, IDC_CHECK_EMAILS));
			EnableWindow(GetDlgItem(hWnd, IDC_RECONNECT_INTERVAL), IsDlgButtonChecked(hWnd, IDC_RECONNECT));
			EnableWindow(GetDlgItem(hWnd, IDC_PORT_EDIT), portCheck);
			break;
		}
		break;

	case WM_NOTIFY:
		switch(((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				db_set_b(NULL, ModuleName, "Check", IsDlgButtonChecked(hWnd, IDC_CHECK_EMAILS));

				TCHAR buffer[4096];
				GetWindowText(GetDlgItem(hWnd, IDC_USER_EDIT), buffer, SIZEOF(buffer));
				db_set_ts(NULL, ModuleName, "Username", buffer);

				GetWindowText(GetDlgItem(hWnd, IDC_PASSWORD_EDIT), buffer, SIZEOF(buffer));
				db_set_ts(NULL, ModuleName, "Password", buffer);

				GetWindowText(GetDlgItem(hWnd, IDC_SERVER_EDIT), buffer, SIZEOF(buffer));
				db_set_ts(NULL, ModuleName, "Server", buffer);

				GetWindowText(GetDlgItem(hWnd, IDC_PORT_EDIT), buffer, SIZEOF(buffer));
				db_set_dw(NULL, ModuleName, "Port", GetDlgItemInt(hWnd,IDC_PORT_EDIT,NULL,FALSE));

				db_set_dw(NULL, ModuleName, "Interval", GetDlgItemInt(hWnd,IDC_INTERVAL_EDIT,NULL,FALSE));
				db_set_dw(NULL, ModuleName, "ReconnectInterval", GetDlgItemInt(hWnd,IDC_RECONNECT_INTERVAL,NULL,FALSE));

				db_set_b(NULL, ModuleName, "Reconnect", IsDlgButtonChecked(hWnd, IDC_RECONNECT));

				db_set_b(NULL, ModuleName, "UsePopups", IsDlgButtonChecked(hWnd, IDC_USE_POPUPS));
				db_set_b(NULL, ModuleName, "UsePortCheck", IsDlgButtonChecked(hWnd, IDC_USE_PORTCHECK));

				db_set_b(NULL, ModuleName, "MaxRetries", GetDlgItemInt(hWnd,IDC_MAX_RETRIES,NULL,FALSE));

				exchangeServer.Reconnect(); //login info may be changed
				UpdateTimers(); //interval might get changed
			}
		}

		break;
	}

	return 0;
}

#include "commctrl.h"

void AddAnchorWindowToDeferList(HDWP &hdWnds, HWND window, RECT *rParent, WINDOWPOS *wndPos, int anchors)
{
	RECT rChild = AnchorCalcPos(window, rParent, wndPos, anchors);
	hdWnds = DeferWindowPos(hdWnds, window, HWND_NOTOPMOST, rChild.left, rChild.top, rChild.right - rChild.left, rChild.bottom - rChild.top, SWP_NOZORDER);
}

int CALLBACK ListSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_KEYUP:
		if (wParam == VK_ESCAPE)
			SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);

		break;

	case WM_SYSKEYDOWN:
		if (wParam == 'X')
			SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);						

		break;

	case WM_LBUTTONDBLCLK:
		int count = ListView_GetItemCount(hWnd);
		for (int i = 0; i < count; i++) {
			if (ListView_GetItemState(hWnd, i, LVIS_SELECTED)) {
				TCHAR emailID[4096]; //uhh
				ListView_GetItemText(hWnd, i, 2, emailID, sizeof(emailID));
				exchangeServer.OpenMessage(emailID);
			}
		}
		break;
	}

	return CallWindowProc(OldListProc, hWnd, msg, wParam, lParam);
}

INT_PTR CALLBACK DlgProcEmails(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		{
			SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hiMailIcon);

			LVCOLUMN col = {0};
			HWND hList = GetDlgItem(hWnd, IDC_EMAILS_LIST);
			OldListProc = (WNDPROC) SetWindowLongPtr(hList, GWLP_WNDPROC, (LONG_PTR) ListSubclassProc);
			ListView_SetExtendedListViewStyle(hList, LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES);
			col.mask = LVCF_TEXT | LVCF_WIDTH;
			col.cx = 100;
			col.pszText = TranslateT("Entry ID");
			col.cchTextMax = (int)_tcslen(col.pszText) + 1;
			ListView_InsertColumn(hList, 0, &col);
			col.pszText = TranslateT("Subject");
			col.cx = 300;
			col.cchTextMax = (int)_tcslen(col.pszText) + 1;
			ListView_InsertColumn(hList, 0, &col);
			col.cx = 200;
			col.iSubItem = 1;
			col.pszText = TranslateT("Sender");
			col.cchTextMax = (int)_tcslen(col.pszText) + 1;
			ListView_InsertColumn(hList, 0, &col);
		}
		return TRUE;

	case WM_DESTROY:
		hEmailsDlg = NULL;
		break;

	case WM_CLOSE:
		DestroyWindow(hWnd); //close the window - no need to keep it in memory
		break;

	case EXM_UPDATE_EMAILS:
		{
			HWND hList = GetDlgItem(hWnd, IDC_EMAILS_LIST);
			ListView_DeleteAllItems(hList);
			int count = GetWindowLongPtr(hWnd, GWLP_USERDATA);
			LVITEM item = {0};
			TEmailHeader email = {0};
			email.cbSize = sizeof(TEmailHeader);
			TCHAR sender[1024] = _T("");
			TCHAR subject[1024] = _T("");
			//char buffer[4096];
			email.cSender = sizeof(sender);
			email.cSubject = sizeof(subject);
			email.szSender = sender;
			email.szSubject = subject;
			item.mask = LVIF_TEXT;

			for (int i = 0; i < count; i++)
			{
				exchangeServer.GetEmailHeader(i, &email);
				item.iItem = i;
				item.iSubItem = 0;
				item.pszText = email.szSender;
				ListView_InsertItem(hList, &item);
				ListView_SetItemText(hList, i, 1, email.szSubject);
				ListView_SetItemText(hList, i, 2, mir_a2t(email.emailID));
			}
			SetFocus(hList);
		}
		break;

	case WM_SHOWWINDOW:
		if (wParam)
			SendMessage(hWnd, EXM_UPDATE_EMAILS, 0, 0);
		break;

	case WM_KEYUP:
		if (wParam == VK_ESCAPE)
			SendMessage(hWnd, WM_CLOSE, 0, 0);
		break;

	case WM_SYSKEYDOWN:
		if (wParam == 'X')
			SendMessage(hWnd, WM_CLOSE, 0, 0);
		break;

	case WM_WINDOWPOSCHANGING:
		{
			HDWP hdWnds = BeginDeferWindowPos(3);
			RECT rParent;
			WINDOWPOS *wndPos = (WINDOWPOS *) lParam;

			if ((!wndPos) || (wndPos->flags & SWP_NOSIZE))
				break;

			GetWindowRect(hWnd, &rParent);
			if (wndPos->cx < MIN_EMAILS_WIDTH)
				wndPos->cx = MIN_EMAILS_WIDTH;
			if (wndPos->cy < MIN_EMAILS_HEIGHT)
				wndPos->cy = MIN_EMAILS_HEIGHT;

			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_EMAILS_LIST), &rParent, wndPos, ANCHOR_ALL);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_CLOSE), &rParent, wndPos, ANCHOR_BOTTOM | ANCHOR_RIGHT);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_MARK_READ), &rParent, wndPos, ANCHOR_BOTTOM | ANCHOR_LEFT);

			EndDeferWindowPos(hdWnds);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_CLOSE:
			SendMessage(hWnd, WM_CLOSE, 0, 0);
			break;

		case IDC_MARK_READ:
			HWND hList = GetDlgItem(hWnd, IDC_EMAILS_LIST);
			int count = ListView_GetItemCount(hList);
			for (int i = 0; i < count; i++) {
				if (ListView_GetCheckState(hList, i)) {
					TCHAR emailID[2048]; //uhh ohh
					LVITEM item = {0};
					item.iItem = i;
					item.mask = LVIF_TEXT;
					item.iSubItem = 2;
					item.cchTextMax = sizeof(emailID);
					item.pszText = emailID;
					ListView_GetItem(hList, &item);
					exchangeServer.MarkEmailAsRead(emailID);
				}
			}
			count = exchangeServer.GetUnreadEmailsCount();
			if (count > 0)
			{
				SetWindowLongPtr(hWnd, GWLP_USERDATA, count);
				SendMessage(hWnd, EXM_UPDATE_EMAILS, 0, 0);
			}
			else SendMessage(hWnd, WM_CLOSE, 0, 0);
		}

		break;
	}

	return 0;
}

LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case STN_CLICKED:
			{
				int count = (int) PUGetPluginData(hWnd);
				ShowEmailsWindow(count);
				PUDeletePopup(hWnd);
				break;
			}
		}
		break;

	case WM_CONTEXTMENU:
		PUDeletePopup(hWnd);
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}