summaryrefslogtreecommitdiff
path: root/plugins/SplashScreen/src/main.cpp
blob: 2726801b12828e8a1152b49391427a3e505ee248 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
   Splash Screen Plugin for Miranda-IM (www.miranda-im.org)
   (c) 2004-2007 nullbie, (c) 2005-2007 Thief

   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

   File name      : $URL: http://svn.miranda.im/mainrepo/splashscreen/trunk/src/main.cpp $
   Revision       : $Rev: 1586 $
   Last change on : $Date: 2010-04-09 13:34:01 +0400 (Пт, 09 апр 2010) $
   Last change by : $Author: Thief $

*/

#include "headers.h"

HINSTANCE hInst = 0;

int hLangpack;

static HMODULE hUserDll = NULL;
static HMODULE hAdvaimg = NULL;

pfnConvertPng2dib png2dibConvertor = NULL;

BOOL bstartup = true; // startup?
BOOL bserviceinvoked = false;
BOOL bmodulesloaded = false; // modules are loaded
BOOL png2dibavail = true; // can we use png2dib service?

// path to miranda's dir, config file path, splash path, sound path
TCHAR szDllName[MAX_PATH], szSplashFile[MAX_PATH], szSoundFile[MAX_PATH], szhAdvaimgPath[MAX_PATH], szPrefix[128], inBuf[80];
TCHAR * szMirDir;
char szVersion[MAX_PATH];
HANDLE hShowSplashService, hTestService;
#ifdef _DEBUG
	TCHAR szLogFile[MAX_PATH];
#endif
SPLASHOPTS options;
HWND hwndSplash;
BOOL (WINAPI *MyUpdateLayeredWindow)
   (HWND hwnd, HDC hdcDST, POINT *pptDst, SIZE *psize, HDC hdcSrc, POINT *pptSrc,
    COLORREF crKey, BLENDFUNCTION *pblend, DWORD dwFlags);
HANDLE hSplashThread, hModulesLoaded, hPlugDisableHook, hOptInit, hSystemOKToExit;

PLUGININFOEX pluginInfo={
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	0,
	MIID_SPLASHSCREEN
};

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	hInst = hinstDLL;
	return TRUE;
}

void SplashMain()
{
	if (bstartup)
	{
		// Retrive path to exe of current running Miranda is located
		szMirDir = Utils_ReplaceVarsT(_T("%miranda_path%"));
		mir_sntprintf(szhAdvaimgPath, SIZEOF(szhAdvaimgPath), _T("%s\\plugins\\advaimg.dll"), szMirDir);
		CallService(MS_SYSTEM_GETVERSIONTEXT, MAX_PATH, (LPARAM)szVersion);

		#ifdef _DEBUG
			mir_sntprintf(szLogFile, SIZEOF(szLogFile), _T("%s\\%s.log"), szMirDir, _T(__INTERNAL_NAME));
			initLog();
			logTimeStamp();
			TCHAR* mirandaVerString = mir_a2t(szVersion);
			logMessage(_T("Miranda version"), mirandaVerString);
			mir_free(mirandaVerString);
			logMessage(_T("Dll Name"), _T(__FILENAME));
			logMessage(_T("Advaimg path"), szhAdvaimgPath);
		#endif

		ReadIniConfig();
	}

	if (bstartup & (options.active == 1))
	{
		if (options.runonce)
		{
			DBWriteContactSettingByte(NULL, MODNAME, "Active", 0);
			DBWriteContactSettingByte(NULL, MODNAME, "DisableAfterStartup", 0);
		}

		if (hUserDll == NULL)
		{
			hUserDll = LoadLibrary(_T("user32.dll"));
			if (hUserDll)
				MyUpdateLayeredWindow = (BOOL (WINAPI *)(HWND, HDC, POINT *, SIZE *, HDC, POINT *, COLORREF, BLENDFUNCTION *, DWORD))GetProcAddress(hUserDll, "UpdateLayeredWindow");
		}

		if (hAdvaimg == NULL)
		{
			hAdvaimg = LoadLibrary(szhAdvaimgPath);
			if (hAdvaimg == NULL)
			{
				png2dibavail = false;
				bstartup = false;
			}
			if (hAdvaimg)
			{
				png2dibConvertor = (pfnConvertPng2dib) GetProcAddress(hAdvaimg, "mempng2dib");
				if (png2dibConvertor == NULL)
				{
					FreeLibrary(hAdvaimg); hAdvaimg = NULL;
					MessageBox(NULL,
					_T("Your advaimg.dll is either obsolete or damaged. Get latest from Miranda alpha builds."),
					_T("Error"),
					MB_OK | MB_ICONSTOP);
				}
				#ifdef _DEBUG
					if (png2dibConvertor)
						logMessage(_T("Loading advaimg"), _T("done"));
				#endif
			}
		}

		//for 9x "alfa" testing
		//MyUpdateLayeredWindow = 0;
		DBVARIANT dbv = {0};
		DBGetContactSettingTString(NULL, MODNAME, "VersionPrefix", &dbv);
		if (lstrcmp(dbv.ptszVal, NULL) == 0)
		{
			_tcscpy_s(szPrefix, _T(""));
			DBFreeVariant(&dbv);
		}
		else
			_tcscpy_s(szPrefix, dbv.ptszVal);
		dbv.ptszVal = NULL;

		DBGetContactSettingTString(NULL, MODNAME, "Path", &dbv);
		if (lstrcmp(dbv.ptszVal, NULL) == 0)
		{
			_tcscpy_s(inBuf, _T("splash\\splash.png"));
			DBFreeVariant(&dbv);
		}
		else
			_tcscpy_s(inBuf, dbv.ptszVal);
		dbv.ptszVal = NULL;

		TCHAR szExpandedSplashFile[MAX_PATH];
		ExpandEnvironmentStrings(inBuf, szExpandedSplashFile, SIZEOF(szExpandedSplashFile));
		lstrcpy(inBuf, szExpandedSplashFile);

		TCHAR *pos3 = 0;
		pos3 = _tcsrchr(inBuf, _T(':'));
		if (pos3 == NULL)
			mir_sntprintf(szSplashFile, SIZEOF(szSplashFile), _T("%s\\%s"), szMirDir, inBuf);
		else
			lstrcpy(szSplashFile, inBuf);

		DBGetContactSettingTString(NULL, MODNAME, "Sound", &dbv);
		if (lstrcmp(dbv.ptszVal, NULL) == 0)
		{
			_tcscpy_s(inBuf, _T("sounds\\startup.wav"));
			DBFreeVariant(&dbv);
		}
		else
			_tcscpy_s(inBuf, dbv.ptszVal);

		TCHAR szExpandedSoundFile[MAX_PATH];
		ExpandEnvironmentStrings(inBuf, szExpandedSoundFile, SIZEOF(szExpandedSoundFile));
		lstrcpy(inBuf, szExpandedSoundFile);

		TCHAR *pos2;
		pos2 = _tcschr(inBuf, _T(':'));
		if (pos2 == NULL)
			mir_sntprintf(szSoundFile, SIZEOF(szSoundFile), _T("%s\\%s"), szMirDir, inBuf);
		else
			lstrcpy(szSoundFile, inBuf);

		#ifdef _DEBUG
			logMessage(_T("SoundFilePath"), szSoundFile);
		#endif

		TCHAR szOldPath[MAX_PATH] = {0};

		if(options.random) // randomly select a splash file
		{
			int filescount = 0;
			TCHAR szSplashDir[MAX_PATH] = {0}, szSearch[MAX_PATH] = {0};
			TCHAR* p = 0;
			TCHAR files [255][50]; //TODO: make memory allocation dynamic

			lstrcpy(szSplashDir, szSplashFile);
			lstrcpy(szOldPath, szSplashFile);
			// find the last \ and null it out, this leaves no trailing slash
			p = _tcsrchr(szSplashDir, _T('\\'));
			if (p) *p = 0;
			// create the search filter
			mir_sntprintf(szSearch, SIZEOF(szSearch), _T("%s\\*.*"), szSplashDir);
			// FFFN will return filenames
			HANDLE hFind = INVALID_HANDLE_VALUE;
			WIN32_FIND_DATA ffd;
			hFind = FindFirstFile(szSearch, &ffd);
			if ( hFind != INVALID_HANDLE_VALUE )
			{
				do
				{
					if (!(ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) 
					{
						#ifdef _DEBUG
							logMessage(_T("Found file"), ffd.cFileName);
						#endif
						//files = new char[strlen(ffd.cFileName)];
						//files[filescount] = new char[strlen(ffd.cFileName)];
						TCHAR ext[5];
						tmemcpy(ext, ffd.cFileName + (_tcslen(ffd.cFileName)-4), 5);

						#ifdef _DEBUG
							logMessage(_T("Extention"), ext);
						#endif

						if (lstrcmpi(ext, _T(".png")) & lstrcmpi(ext, _T(".bmp")))
							continue;

						#ifdef _DEBUG
							logMessage(_T("File has valid ext"), ext);
						#endif
						_tcscpy_s(files[filescount++], ffd.cFileName);
					} //if
				} while (FindNextFile(hFind, &ffd));

				srand((unsigned) time(NULL));
				int r = 0;
				if (filescount) r = (rand() % filescount) + 1;

				mir_sntprintf(szSplashFile, SIZEOF(szSplashFile), _T("%s\\%s"), szSplashDir, files[r-1]);

				#ifdef _DEBUG
					logMessage(_T("final file"), szSplashFile);
				#endif
				FindClose(hFind);
			} //if
		}

		// Call splash display routine
		ShowSplash(false);
	}
	bstartup = false;
}

int onSystemOKToExit(WPARAM wParam,LPARAM lParam)
{
	// Hooked events need to be unhooked
	UnhookEvent(hModulesLoaded);
	UnhookEvent(hSystemOKToExit);
	UnhookEvent(hPlugDisableHook);
	UnhookEvent(hOptInit);

	DestroyServiceFunction(hShowSplashService);
	#ifdef _DEBUG
		DestroyServiceFunction(hTestService);
	#endif

	return 0;
}

int PlugDisableHook(WPARAM wParam, LPARAM lParam)
{
	#ifdef _DEBUG
		TCHAR buf [128];
	#endif
	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING*)lParam;
	TCHAR * tszModule= mir_a2t(cws->szModule), *tszSetting = mir_a2t(cws->szSetting);
	if(options.inheritGS)
	{
		if (!lstrcmp(tszModule, _T("Skin")) & !lstrcmp(tszSetting, _T("UseSound")))
		{
			DBWriteContactSettingByte(NULL, MODNAME, "PlaySound", cws->value.bVal);
			#ifdef _DEBUG
				cws->value.bVal ? _DebugPopup(NULL, _T("Sounds enabled."), _T("")) : _DebugPopup(NULL, _T("Sounds disabled."), _T(""));
				logMessage(_T("Module"), tszModule);
				logMessage(_T("Setting"), tszSetting);
				logMessage(_T("Value"), _itot(cws->value.bVal, buf, 10));
			#endif
		}
		if (!lstrcmp(tszModule, _T("PluginDisable")) & (!lstrcmp(tszSetting, szDllName)))
		{
			DBWriteContactSettingByte(NULL, MODNAME, "Active", cws->value.bVal);
			#ifdef _DEBUG
				cws->value.bVal ? _DebugPopup(NULL, _T("Disabled."), "") : _DebugPopup(NULL, _T("Enabled."), _T(""));
				logMessage(_T("PlugDisableHook"), _T("Triggered"));
				logMessage(_T("Module"), tszModule);
				logMessage(_T("Setting"), tszSetting);
				logMessage(_T("Value"), _itot(cws->value.bVal, buf, 10));
			#endif
		}
	}
	mir_free(tszModule);
	mir_free(tszSetting);

	return 0;
}

int ModulesLoaded(WPARAM wParam, LPARAM lParam)
{
	bmodulesloaded = true; // all modules are loaded now, let other parts know about this fact

	if (hwndSplash)
	{
		if (PostMessage(hwndSplash, WM_LOADED, 0, 0))
		{
			#ifdef _DEBUG
				logMessage(_T("Posted WM_LOADED message"), _T("done"));
			#endif
		}
	}

	// Options initialize hook
	hOptInit = HookEvent(ME_OPT_INITIALISE, OptInit);

	hPlugDisableHook = HookEvent(ME_DB_CONTACT_SETTINGCHANGED, PlugDisableHook);

	// Service to call splash
	hShowSplashService = CreateServiceFunction(MS_SHOWSPLASH, ShowSplashService);

	#ifdef _DEBUG
		hTestService = CreateServiceFunction("Splash/Test", TestService);
		CLISTMENUITEM mi;
		ZeroMemory(&mi,sizeof(mi));
		mi.cbSize = sizeof(mi);
		mi.flags = CMIF_TCHAR;
		mi.hIcon = LoadSkinnedIcon(SKINICON_OTHER_MIRANDA);
		mi.hotKey = 0;
		mi.position = -0x7FFFFFFF;
		mi.ptszName = _T("Call Splash Service");
		mi.pszService = "Splash/Test";
		Menu_AddMainMenuItem(&mi);
	#endif

	if (ServiceExists(MS_UPDATE_REGISTER))
	{
		// register with updater
		Update update = {0};
		char szVersion[16];

		update.cbSize = sizeof(Update);

		update.szComponentName = pluginInfo.shortName;
		update.pbVersion = (BYTE *)CreateVersionString(pluginInfo.version, szVersion);
		update.cpbVersion = (int)strlen((char *)update.pbVersion);

		update.szUpdateURL = UPDATER_AUTOREGISTER;

		// these are the three lines that matter - the archive, the page containing the version string, and the text (or data) 
		// before the version that we use to locate it on the page
		// (note that if the update URL and the version URL point to standard file listing entries, the backend xml
		// data will be used to check for updates rather than the actual web page - this is not true for beta urls)
		update.szBetaUpdateURL = "http://thief.miranda.im/advsplashscreen.zip";
		update.szBetaVersionURL = "http://thief.miranda.im/updater/splash_version.txt";
		update.szBetaChangelogURL = "http://thief.miranda.im";
		update.pbBetaVersionPrefix = (BYTE *)"Splash Screen ";

		update.cpbBetaVersionPrefix = (int)strlen((char *)update.pbBetaVersionPrefix);

		CallService(MS_UPDATE_REGISTER, 0, (WPARAM)&update);
	}

	#ifdef _DEBUG
		logMessage(_T("Loading modules"), _T("done"));
	#endif

	return 0;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
{
	return &pluginInfo;
}

extern "C" int __declspec(dllexport) Load(void)
{

	mir_getLP(&pluginInfo);

	hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
	hSystemOKToExit = HookEvent(ME_SYSTEM_OKTOEXIT,onSystemOKToExit);

	SplashMain();
	mir_free(szMirDir);

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	if (hSplashThread) CloseHandle(hSplashThread);

	UnregisterClass(_T(SPLASH_CLASS), hInst);

	// Freeing loaded libraries
	if (hUserDll) FreeLibrary(hUserDll);
	if (hAdvaimg) FreeLibrary(hAdvaimg);

	#ifdef _DEBUG
		logMessage(_T("Unload"), _T("Job done"));
	#endif

	return 0;
}