summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/WinPopup/src/messagebox.cpp
blob: 80ddd5471a5684c5023bf70d55a4a3e9638a81e2 (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
/*

WinPopup Protocol plugin for Miranda IM.

Copyright (C) 2004-2011 Nikolay Raspopov

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

class messagebox
{
public:
	messagebox () :
		m_hwndOwner (NULL),
		m_hwndMessageBox (NULL),
		m_Timeout (0),
		m_MonitorTerm (NULL)
	{
	}

	int WINAPI DoModal (const LPMSGBOXPARAMS lpMsgBoxParams, DWORD dwTimeout)
	{
		int ret = 0;
		m_hwndOwner = lpMsgBoxParams->hwndOwner;
		m_Timeout = dwTimeout;
		m_hwndMessageBox = NULL;
		m_MonitorTerm = CreateEvent (NULL, TRUE, FALSE, NULL);
		if (m_MonitorTerm) {
			HANDLE hThread = (HANDLE)mir_forkthread( MsgBox, this );
			if (hThread) {

				ret = MessageBoxIndirect (lpMsgBoxParams);	

				// Îæèäàíèå çàâåðøåíèÿ
				SetEvent (m_MonitorTerm);
				WaitForSingleObject (hThread, INFINITE);
			}
			CloseHandle (m_MonitorTerm);
		}
		return ret;
	}

protected:
	static BOOL CALLBACK EnumWindowsProc (HWND hWnd, LPARAM lParam)
	{
		messagebox* self = reinterpret_cast <messagebox*> (lParam);

		DWORD dwProcessId;
		GetWindowThreadProcessId (hWnd, &dwProcessId);
		const LONG req_style = WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU;
		HWND hButton;
		TCHAR ClassName[ 8 ];
		if ( GetCurrentProcessId () != dwProcessId ||
			( GetWindowLongPtr(hWnd, GWL_STYLE) & req_style ) != req_style ||
			GetParent( hWnd ) != NULL ||
			GetWindow( hWnd, GW_OWNER ) != self->m_hwndOwner ||
			( hButton = GetWindow( hWnd, GW_CHILD ) ) == NULL ||
			GetClassName( hButton, ClassName, _countof( ClassName ) ) == 0 ||
			lstrcmpi( _T("button"), ClassName ) != 0 )
			return TRUE;

		self->m_hwndMessageBox = hWnd;

		return FALSE;
	}

	static void MsgBox (LPVOID param)
	{
		messagebox* self = reinterpret_cast <messagebox*> (param);

		// Ëîâèì äèàëîã 10 ñåêóíä
		DWORD i = 0;
		while (WaitForSingleObject (self->m_MonitorTerm, 250) == WAIT_TIMEOUT &&
			EnumWindows (messagebox::EnumWindowsProc, reinterpret_cast <LPARAM> (self)) &&
			i++ < 10 * 4);
		if (!self->m_hwndMessageBox)
			// Òàê è íå ïîéìàëè
			return;
		
		HWND hButton = GetWindow (self->m_hwndMessageBox, GW_CHILD);

		// Îòñ÷¸ò âðåìåíè
		while (self->m_Timeout-- &&
			WaitForSingleObject (self->m_MonitorTerm, 1000) == WAIT_TIMEOUT && 
			IsWindow (self->m_hwndMessageBox) && IsWindow (hButton)) {
			CString buf, msg;
			int buf_size = GetWindowTextLength (hButton);
			if (buf_size) {
				GetWindowText (hButton, buf.GetBuffer (buf_size + 1), buf_size + 1);
				buf.ReleaseBuffer ();
			}
			int n = buf.ReverseFind (_T('='));
			msg.Format (_T(" = %u"), self->m_Timeout);
			SetWindowText (hButton, ((n < 1) ? buf : buf.Left (n - 1)) + msg);
		}

		// Çàêðûòèå îêíà
		if (IsWindow (self->m_hwndMessageBox) && IsWindow (hButton)) {
			DWORD_PTR res;
			SendMessageTimeout (self->m_hwndMessageBox, WM_COMMAND,
				(WPARAM) GetDlgCtrlID (hButton),
				(LPARAM) hButton, SMTO_ABORTIFHUNG | SMTO_NORMAL, 10000, &res);
		}

		return;
	}

	volatile HWND m_hwndOwner;
	volatile HWND m_hwndMessageBox;
	volatile DWORD m_Timeout;
	HANDLE m_MonitorTerm;
};

static int WINAPI MessageBoxIndirectTimeout (const LPMSGBOXPARAMS lpMsgBoxParams, DWORD dwTimeout)
{
	messagebox mb;
	return mb.DoModal (lpMsgBoxParams, dwTimeout);
}

LPCTSTR const szModules [] = {
	_T("netapi32.dll"),
	_T("netmsg.dll"),
	_T("wininet.dll"),
	_T("ntdll.dll"),
	_T("ntdsbmsg.dll"),
	NULL
};

void GetErrorMessage (DWORD dwLastError, CString &msg)
{
	CString buf;
	if ( HRESULT_FACILITY( dwLastError ) == FACILITY_NETBIOS )
	{
		CString sMessage = (LPCTSTR)CA2T( GetNetbiosError( HRESULT_CODE ( dwLastError ) ) );
		if ( ! sMessage.IsEmpty() )
			msg.Format( _T("%s\r\nNetBIOS %s: %u"), sMessage, T_ERROR, HRESULT_CODE( dwLastError ) );
		else
			msg.Format( _T("NetBIOS %s: %u"), T_ERROR, HRESULT_CODE( dwLastError ) );
	}
	else
	{
		HMODULE hModule = NULL;
		for (int i = 0; szModules [i]; i++)
		{
			hModule = LoadLibraryEx (szModules [i], NULL, LOAD_LIBRARY_AS_DATAFILE);
			LPTSTR MessageBuffer = NULL;
			if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
				FORMAT_MESSAGE_IGNORE_INSERTS |
				FORMAT_MESSAGE_FROM_SYSTEM |
				(hModule ? FORMAT_MESSAGE_FROM_HMODULE : 0u),
				hModule, dwLastError, 0u, (LPTSTR) &MessageBuffer, 0u, NULL))
			{
				buf = MessageBuffer;
				buf.Trim (_T(" \t\r\n"));
				LocalFree (MessageBuffer);
				if (hModule)
					FreeLibrary (hModule);
				break;
			}
			if (hModule)
				FreeLibrary (hModule);
		}
		if ( ! buf.IsEmpty() )
			msg.Format( _T("%s\r\n%s: %u"), (LPCTSTR)buf, T_ERROR, dwLastError );
		else
			msg.Format( _T("%s: %u"), T_ERROR, dwLastError );
	}
}

static void PopupOrMessageBox (LPPOPUPDATAT ppdp)
{
	if (CALLSERVICE_NOTFOUND == PUAddPopupT (ppdp))
	{
		MSGBOXPARAMS mbp = { 0 };
		mbp.cbSize = sizeof (MSGBOXPARAMS);
		mbp.lpszText = ppdp->lptzText;
		mbp.lpszCaption = ppdp->lptzContactName;
		mbp.dwStyle = MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL;
		MessageBoxIndirectTimeout (&mbp, (DWORD)ppdp->iSeconds);
	}
}

void WarningBox (HANDLE hContact /* = NULL */, DWORD dwLastError /* = 0 */, LPCTSTR format, ...)
{
	if (!pluginInstalled)
		return;

	POPUPDATAT pdp = { 0 };
	pdp.lchContact = hContact;
	pdp.lchIcon = (HICON) LoadImage( pluginModule, MAKEINTRESOURCE (IDI_WINPOPUP),
		IMAGE_ICON, 16, 16, LR_SHARED );
	lstrcpy (pdp.lptzContactName, modtitle_t);
	va_list marker;
	va_start (marker, format);
	wvsprintf( pdp.lptzText, format, marker );
	va_end (marker);
	pdp.iSeconds = 10;

	if (dwLastError) {
		CString msg;
		GetErrorMessage (dwLastError, msg);
		int len = lstrlen (pdp.lptzText);
		pdp.lptzText [len] = _T('\r');
		pdp.lptzText [len + 1] = _T('\n');
		lstrcpy (pdp.lptzText + len + 2, msg);
	}

	PopupOrMessageBox (&pdp);
}