summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/MraPopUp.cpp
blob: b9a2a1fdcc3575fc7ede6a0e4d8e996b4e7503bf (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
#include "Mra.h"
#include "MraPopUp.h"

#define POPUPS_TYPES_COUNT	7
static LPCWSTR lpcwszPopupsTypes[] =
{
	L"None",
	L"Debug",
	L"Information",
	L"Question",
	L"Warning",
	L"Error",
	L"NewMail",
	NULL
};

INT_PTR CALLBACK MraPopupDlgProcOpts(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CMraProto *ppro = (CMraProto*)GetWindowLongPtr(hWndDlg, GWLP_USERDATA);

	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWndDlg);
		SetWindowLongPtr(hWndDlg, GWLP_USERDATA, lParam);
		ppro = (CMraProto*)lParam;
		{
			HWND hWndCombo = GetDlgItem(hWndDlg, IDC_COMBO_POPUP_TYPE);
			SendMessage(hWndCombo, CB_RESETCONTENT, 0, 0);

			for (size_t i = 0; i < POPUPS_TYPES_COUNT; i++) {
				DWORD dwItem = SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)TranslateW(lpcwszPopupsTypes[i]));
				SendMessage(hWndCombo, CB_SETITEMDATA, dwItem, i);
			}
			SendMessage(hWndCombo, CB_SETCURSEL, 0, 0);
			SendMessage(hWndDlg, WM_COMMAND, MAKELONG(IDC_COMBO_POPUP_TYPE, CBN_SELCHANGE), 0);
		}
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_COMBO_POPUP_TYPE:
			if (HIWORD(wParam) == CBN_SELCHANGE) {
				BOOL bEnabled, bUseWinColors;
				char szBuff[MAX_PATH];
				DWORD dwType = GET_CURRENT_COMBO_DATA(hWndDlg, IDC_COMBO_POPUP_TYPE);

				bEnabled = GetBit(ppro->mraGetDword(NULL, "PopupsEventFilter", MRA_DEFAULT_POPUPS_EVENT_FILTER), dwType);
				CHECK_DLG_BUTTON(hWndDlg, IDC_CHK_ENABLE, bEnabled);

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SUseWinColors", lpcwszPopupsTypes[dwType]);
				bUseWinColors = ppro->mraGetByte(NULL, szBuff, MRA_DEFAULT_POPUP_USE_WIN_COLORS);
				CHECK_DLG_BUTTON(hWndDlg, IDC_CHK_USE_WIN_COLORS, bUseWinColors);
				EnableWindow(GetDlgItem(hWndDlg, IDC_CHK_USE_WIN_COLORS), bEnabled);

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorBack", lpcwszPopupsTypes[dwType]);
				SEND_DLG_ITEM_MESSAGE(hWndDlg, IDC_POPUP_BACKCOLOR, CPM_SETCOLOUR, 0, ppro->mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_COLOR_BACK));
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_BACKCOLOR), (bEnabled && bUseWinColors == FALSE));

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorText", lpcwszPopupsTypes[dwType]);
				SEND_DLG_ITEM_MESSAGE(hWndDlg, IDC_POPUP_TEXTCOLOR, CPM_SETCOLOUR, 0, ppro->mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_COLOR_TEXT));
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_TEXTCOLOR), (bEnabled && bUseWinColors == FALSE));

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%STimeout", lpcwszPopupsTypes[dwType]);
				SetDlgItemInt(hWndDlg, IDC_POPUP_TIMEOUT, ppro->mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_TIMEOUT), FALSE);
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_TIMEOUT), bEnabled);
			}
			break;

		case IDC_PREVIEW:
			{
				for (size_t i = 0; i < POPUPS_TYPES_COUNT; i++) {
					ppro->MraPopupShowFromAgentW(i, 0, TranslateW(lpcwszPopupsTypes[i]));
				}
			}
			break;

		case IDC_CHK_ENABLE:
		case IDC_CHK_USE_WIN_COLORS:
			{
				BOOL bEnabled = IS_DLG_BUTTON_CHECKED(hWndDlg, IDC_CHK_ENABLE);
				BOOL bUseWinColors = IS_DLG_BUTTON_CHECKED(hWndDlg, IDC_CHK_USE_WIN_COLORS);

				EnableWindow(GetDlgItem(hWndDlg, IDC_CHK_USE_WIN_COLORS), bEnabled);
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_BACKCOLOR), (bEnabled && bUseWinColors == FALSE));
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_TEXTCOLOR), (bEnabled && bUseWinColors == FALSE));
				EnableWindow(GetDlgItem(hWndDlg, IDC_POPUP_TIMEOUT), bEnabled);
			}
		default:
			if ((LOWORD(wParam) == IDC_POPUP_TIMEOUT) && (HIWORD(wParam) != EN_CHANGE || (HWND)lParam != GetFocus()))
				return FALSE;
			SendMessage(GetParent(hWndDlg), PSM_CHANGED, 0, 0);
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->code) {
		case PSN_APPLY:
			{
				char szBuff[MAX_PATH];
				DWORD dwType = GET_CURRENT_COMBO_DATA(hWndDlg, IDC_COMBO_POPUP_TYPE);
				DWORD dwPopupsEventFilter = ppro->mraGetDword(NULL, "PopupsEventFilter", MRA_DEFAULT_POPUPS_EVENT_FILTER);
				if (IS_DLG_BUTTON_CHECKED(hWndDlg, IDC_CHK_ENABLE))
					dwPopupsEventFilter |= (1<<dwType);
				else
					dwPopupsEventFilter &= ~(1<<dwType);

				ppro->mraSetDword(NULL, "PopupsEventFilter", dwPopupsEventFilter);

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SUseWinColors", lpcwszPopupsTypes[dwType]);
				ppro->mraSetByte(NULL, szBuff, IS_DLG_BUTTON_CHECKED(hWndDlg, IDC_CHK_USE_WIN_COLORS));

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorBack", lpcwszPopupsTypes[dwType]);
				ppro->mraSetDword(NULL, szBuff, SEND_DLG_ITEM_MESSAGE(hWndDlg, IDC_POPUP_BACKCOLOR, CPM_GETCOLOUR, 0, 0));

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorText", lpcwszPopupsTypes[dwType]);
				ppro->mraSetDword(NULL, szBuff, SEND_DLG_ITEM_MESSAGE(hWndDlg, IDC_POPUP_TEXTCOLOR, CPM_GETCOLOUR, 0, 0));

				mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%STimeout", lpcwszPopupsTypes[dwType]);
				ppro->mraSetDword(NULL, szBuff, GetDlgItemInt(hWndDlg, IDC_POPUP_TIMEOUT, NULL, FALSE));
			}
			return TRUE;
		}
		break;
	}
	return FALSE;
}

int CMraProto::OnPopupOptInit(WPARAM wParam, LPARAM lParam)
{
	if ( ServiceExists(MS_POPUP_ADDPOPUP)) {
		OPTIONSDIALOGPAGE odp = {0};
		odp.cbSize = sizeof(odp);
		odp.dwInitParam = (LPARAM)this;
		odp.position = 100000000;
		odp.hInstance = masMraSettings.hInstance;
		odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_POPUPS);
		odp.pszTitle = m_szModuleName;
		odp.pszGroup = LPGEN("Popups");
		odp.groupPosition = 900000000;
		odp.flags = ODPF_BOLDGROUPS;
		odp.pfnDlgProc = MraPopupDlgProcOpts;
		Options_AddPage(wParam, &odp);
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// ShowPopup - popup plugin support

struct MraPopupData
{
	CMraProto *ppro;
	int iPopupType;
};

void CALLBACK MraPopupThreadMarandaCallback(ULONG_PTR dwParam)
{
	if (dwParam == 0)
		return;

	MraPopupData* dat = (MraPopupData*)((POPUPDATAW*)dwParam)->PluginData;
	if (dat->iPopupType == MRA_POPUP_TYPE_EMAIL_STATUS && dat->ppro->hWndEMailPopupStatus) {
		// 1: PUDeletePopUp(hWndDlg);
		// 1: PUAddPopUpW((POPUPDATAW*)dwParam);
		// 2: PUChangeW(hWndEMailPopupStatus, (POPUPDATAW*)dwParam); //- crash :/
		PUChangeTextW(dat->ppro->hWndEMailPopupStatus, ((POPUPDATAW*)dwParam)->lpwzText);
	}
	else PUAddPopUpW((POPUPDATAW*)dwParam);

	mir_free((void*)dwParam);
}

/////////////////////////////////////////////////////////////////////////////////////////
// Popup plugin window proc

LRESULT CALLBACK MraPopupDlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	MraPopupData* dat = (MraPopupData*)PUGetPluginData(hWndDlg);

	switch (msg) {
	case UM_INITPOPUP:
		if (dat->iPopupType == MRA_POPUP_TYPE_EMAIL_STATUS) {
			// update/load avatar
			dat->ppro->MraAvatarsQueueGetAvatarSimple(dat->ppro->hAvatarsQueueHandle, GAIF_FORCE, PUGetContact(hWndDlg), 0);

			// delete old email popup
			if (dat->ppro->hWndEMailPopupStatus)
				PUDeletePopUp(dat->ppro->hWndEMailPopupStatus);

			dat->ppro->hWndEMailPopupStatus = hWndDlg;
		}
		break;

	case WM_COMMAND:
		if (HIWORD(wParam) == STN_CLICKED) { //It was a click on the Popup.
			if (dat->iPopupType == MRA_POPUP_TYPE_EMAIL_STATUS) {
				CallProtoService(dat->ppro->m_szModuleName, MRA_GOTO_INBOX, 0, 0);
				dat->ppro->hWndEMailPopupStatus = NULL;
			}
		}
		PUDeletePopUp(hWndDlg);
		break;

	case WM_CONTEXTMENU:
		if (dat->iPopupType == MRA_POPUP_TYPE_EMAIL_STATUS)
			dat->ppro->hWndEMailPopupStatus = NULL;
		PUDeletePopUp(hWndDlg);
		break;

	case UM_FREEPLUGINDATA:
		if (dat->iPopupType == MRA_POPUP_TYPE_EMAIL_STATUS)
			dat->ppro->hWndEMailPopupStatus = NULL;
		mir_free(dat);
		break;
	}
	return DefWindowProc(hWndDlg, msg, wParam, lParam);
}

void CMraProto::MraPopupShowFromContactW(HANDLE hContact, DWORD dwType, DWORD dwFlags, LPWSTR lpszMessage)
{
	WCHAR szNick[MAX_EMAIL_LEN], szEMail[MAX_EMAIL_LEN], szTitle[MAX_CONTACTNAME];

	mraGetStaticStringW(hContact, "Nick", szNick, SIZEOF(szNick), NULL);
	mraGetStaticStringW(hContact, "e-mail", szEMail, SIZEOF(szEMail), NULL);
	if (hContact) {
		mir_sntprintf(szTitle, SIZEOF(szTitle), L"%s <%s>", szNick, szEMail);
	}else {
		mir_sntprintf(szTitle, SIZEOF(szTitle), L"%s:  %s <%s>", m_tszUserName, szNick, szEMail);
	}
	MraPopupShowW(hContact, dwType, dwFlags, szTitle, lpszMessage);
}


void CMraProto::MraPopupShowW(HANDLE hContact, DWORD dwType, DWORD dwFlags, LPWSTR lpszTitle, LPWSTR lpszMessage)
{
	if (mraGetByte(NULL, "PopupsEnabled", MRA_DEFAULT_POPUPS_ENABLED))
	if (GetBit(mraGetDword(NULL, "PopupsEventFilter", MRA_DEFAULT_POPUPS_EVENT_FILTER), dwType))
	if ( ServiceExists(MS_POPUP_ADDPOPUPW)) {
		BOOL bUseWinColors;
		char szBuff[MAX_PATH];
		POPUPDATAW *ppd = (POPUPDATAW*)mir_calloc(sizeof(POPUPDATAW));

		//if ( ServiceExists(MS_POPUP2_SHOW) == FALSE)// yapp used
		if (dwType == MRA_POPUP_TYPE_EMAIL_STATUS && hWndEMailPopupStatus) {
			// delete old email popup
			PUDeletePopUp(hWndEMailPopupStatus);
			hWndEMailPopupStatus = NULL;
		}

		// load icon
		switch (dwType) {
		case MRA_POPUP_TYPE_NONE:// proto icon
			ppd->lchIcon = (HICON)LoadImage(masMraSettings.hInstance, MAKEINTRESOURCE(IDI_MRA), IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_DEBUG:// IDI_APPLICATION
			ppd->lchIcon = (HICON)LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_INFORMATION:// IDI_INFORMATION
			ppd->lchIcon = (HICON)LoadImage(NULL, IDI_INFORMATION, IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_QUESTION:// IDI_QUESTION
			ppd->lchIcon = (HICON)LoadImage(NULL, IDI_QUESTION, IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_WARNING:// IDI_WARNING
			ppd->lchIcon = (HICON)LoadImage(NULL, IDI_WARNING, IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_ERROR:// IDI_ERROR
			ppd->lchIcon = (HICON)LoadImage(NULL, IDI_ERROR, IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		case MRA_POPUP_TYPE_EMAIL_STATUS:
			ppd->lchIcon = (HICON)LoadImage(masMraSettings.hInstance, MAKEINTRESOURCE(IDI_MAIL_NOTIFY), IMAGE_ICON, 0, 0, LR_SHARED);
			break;
		}

		MraPopupData *dat = (MraPopupData*)mir_calloc( sizeof(MraPopupData));
		dat->iPopupType = dwType;
		dat->ppro = this;

		ppd->lchContact = hContact;
		if (lpszTitle)
			lstrcpynW(ppd->lpwzContactName, lpszTitle, SIZEOF(ppd->lpwzContactName));
		if (lpszMessage)
			lstrcpynW(ppd->lpwzText, lpszMessage, SIZEOF(ppd->lpwzText));
		ppd->PluginWindowProc = (WNDPROC)MraPopupDlgProc;
		ppd->PluginData = dat;

		mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SUseWinColors", lpcwszPopupsTypes[dwType]);
		bUseWinColors = mraGetByte(NULL, szBuff, MRA_DEFAULT_POPUP_USE_WIN_COLORS);
		if (bUseWinColors) {
			ppd->colorBack = GetSysColor(COLOR_BTNFACE);
			ppd->colorText = GetSysColor(COLOR_WINDOWTEXT);
		}
		else {
			mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorBack", lpcwszPopupsTypes[dwType]);
			ppd->colorBack = mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_COLOR_BACK);

			mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%SColorText", lpcwszPopupsTypes[dwType]);
			ppd->colorText = mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_COLOR_TEXT);
		}

		mir_snprintf(szBuff, SIZEOF(szBuff), "PopupType%STimeout", lpcwszPopupsTypes[dwType]);
		ppd->iSeconds = mraGetDword(NULL, szBuff, MRA_DEFAULT_POPUP_TIMEOUT);

		MraPopupThreadMarandaCallback((ULONG_PTR)ppd);
	}
	else if (dwFlags & MRA_POPUP_ALLOW_MSGBOX)
		MessageBox(NULL, lpszMessage, lpszTitle, MB_OK+(dwType == MRA_POPUP_TYPE_WARNING)?MB_ICONERROR:MB_ICONINFORMATION);
}