summaryrefslogtreecommitdiff
path: root/plugins/Updater/popups.cpp
blob: 079254b8aec87b37e0128441233106a15d3add04 (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
#include "common.h"
#include "popups.h"

HWND hwndPop;
HANDLE hEventPop;
bool pop_cancelled;
bool bPopupsEnabled;

typedef struct
{
	unsigned flags;
	HICON hIcon;
} PopupDataType;


LRESULT CALLBACK NullWindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	switch( message ) {
		case UM_INITPOPUP:
			{
				PopupDataType* popup = (PopupDataType*)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd,0);
				if (popup->flags & POPFLAG_SAVEHWND) hwndPop = hWnd;
			}
			return 0;
		case WMU_CLOSEPOP:
			SetEvent(hEventPop);
			PUDeletePopUp( hWnd );
			return TRUE;

		case WM_COMMAND:
			{
				PopupDataType* popup = (PopupDataType*)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd,0);
				if (popup->flags & POPFLAG_SAVEHWND) pop_cancelled = false;
			}
			SetEvent(hEventPop);
			PUDeletePopUp( hWnd );
			return TRUE;

		case WM_CONTEXTMENU:
			{
				PopupDataType* popup = (PopupDataType*)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd,0);
				if (popup->flags & POPFLAG_SAVEHWND) pop_cancelled = true;
			}
			SetEvent(hEventPop);
			PUDeletePopUp( hWnd );
			return TRUE;

		case UM_FREEPLUGINDATA: {
			PopupDataType* popup = (PopupDataType*)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd, 0);
			if ((INT_PTR)popup != CALLSERVICE_NOTFOUND)
			{
				if (popup->flags & POPFLAG_SAVEHWND) hwndPop = 0;
				if (popup->hIcon) ReleaseIconEx(popup->hIcon);
				mir_free(popup);
			}
			return TRUE;
		}
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

void __stdcall sttPopupProc( void* dwParam )
{
	POPUPDATAT* ppd = ( POPUPDATAT* )dwParam;
	CallService( MS_POPUP_ADDPOPUPT, ( WPARAM )ppd, 0 );
	mir_free( ppd );
}

void ShowPopup( HANDLE hContact, const TCHAR* line1, const TCHAR* line2, int flags, int timeout )
{
	if(CallService(MS_SYSTEM_TERMINATED, 0, 0)) return;

	if ( !ServiceExists( MS_POPUP_ADDPOPUPT )) {	
		TCHAR title[256];
		mir_sntprintf(title, SIZEOF(title), _T("%s Message"), _T(MODULE));

		if(line1 && line2) {
			size_t len = _tcslen(line1) + _tcslen(line2) + 1;
			TCHAR *message = (TCHAR*)alloca(len * sizeof(TCHAR)); // newline and null terminator
			mir_sntprintf(message, len, _T("%s\n%s"), line1, line2);
			MessageBox( NULL, message, title, MB_OK | MB_ICONINFORMATION );
		} else if(line1) {
			MessageBox( NULL, line1, title, MB_OK | MB_ICONINFORMATION );
		} else if(line2) {
			MessageBox( NULL, line2, title, MB_OK | MB_ICONINFORMATION );
		}
		return;
	}

	SetEvent(hEventPop);
	ResetEvent(hEventPop);

	if(ServiceExists(MS_POPUP_ADDPOPUPCLASS))
	{
		POPUPDATACLASS d = {sizeof(d), "updaterpopups"};
		d.ptszTitle = line1;
		d.ptszText  = line2;
		d.PluginData = mir_calloc(sizeof(PopupDataType));
		((PopupDataType*)d.PluginData)->flags = flags;
		CallService(MS_POPUP_ADDPOPUPCLASS, 0, (LPARAM)&d);
	} 
	else 
	{
		POPUPDATAT* ppd = ( POPUPDATAT* )mir_calloc( sizeof( POPUPDATAT ));

		ppd->lchContact = hContact;
		ppd->lchIcon = LoadIconEx(I_CHKUPD);

		if (line1 && line2)
		{
			_tcscpy( ppd->lptzContactName, line1 );
			_tcscpy( ppd->lptzText, line2 );
		} 
		else if (line1) 
			_tcscpy( ppd->lptzText, line1 );
		else if (line2) 
			_tcscpy( ppd->lptzText, line2 );

		if (options.set_colours) 
		{
			ppd->colorBack = options.bkCol;
			ppd->colorText = options.textCol;
		} 
		else
		{
			//ppd->colorText = 0x00FFFFFF;
			ppd->colorText = 0;
			//ppd->colorBack = POPUP_USE_SKINNED_BG;
			ppd->colorBack = 0;
		}

		ppd->iSeconds = timeout;

		ppd->PluginWindowProc = ( WNDPROC )NullWindowProc;
		ppd->PluginData = mir_calloc(sizeof(PopupDataType));
		((PopupDataType*)ppd->PluginData)->flags = flags;
		((PopupDataType*)ppd->PluginData)->hIcon = ppd->lchIcon;

		CallFunctionAsync( sttPopupProc , ppd );
	}
}

void ShowWarning(TCHAR *msg)
{
	static const TCHAR title[512] = _T(MODULE) _T(" Warning");

	if(ServiceExists(MS_POPUP_SHOWMESSAGE))
	{
		TCHAR message[1024];
		mir_sntprintf(message, SIZEOF(message), _T("%s: %s"), _T(MODULE), msg);
		ShowPopup(0, title, message, 0, 10);
		//PUShowMessage(message, SM_WARNING);
	} 
	else
		MessageBox(0, msg, title, MB_OK | MB_ICONWARNING);
}

void ShowError(TCHAR *msg) 
{
	static const TCHAR title[512] = _T(MODULE) _T(" Error");
	NLog(msg);

	if(ServiceExists(MS_POPUP_SHOWMESSAGE)) 
	{
		TCHAR message[1024];
		mir_sntprintf(message, SIZEOF(message), _T("%s: %s"), _T(MODULE), msg);
		//PUShowMessage(message, SM_WARNING);
		ShowPopup(0, title, message, 0, 10);
	} 
	else
		MessageBox(0, msg, title, MB_OK | MB_ICONERROR);
}

void ChangePopupText(HWND hwnd, TCHAR *text) 
{
	CallService(MS_POPUP_CHANGETEXTT, (WPARAM)hwnd, (LPARAM)text);
}

bool ArePopupsEnabled() 
{
	return bPopupsEnabled;
}

HICON hPopupIcon;
void InitPopups() {
	bPopupsEnabled = ServiceExists(MS_POPUP_ADDPOPUPT) != 0;

	hEventPop = CreateEvent( NULL, TRUE, FALSE, NULL );

	if (ServiceExists(MS_POPUP_REGISTERCLASS)) 
	{
		hPopupIcon = LoadIconEx(I_CHKUPD);
		POPUPCLASS test = {0};
		test.cbSize = sizeof(test);
		test.flags = PCF_TCHAR;
		test.hIcon = hPopupIcon;
		test.iSeconds = -1;
		test.ptszDescription = TranslateT(MODULE);
		test.pszName = "updaterpopups";
		test.PluginWindowProc = NullWindowProc;
		CallService(MS_POPUP_REGISTERCLASS, 0, (WPARAM)&test);
	}
}

void DeinitPopups()
{
	CloseHandle(hEventPop);
	ReleaseIconEx(hPopupIcon);
}