summaryrefslogtreecommitdiff
path: root/src/modules/langpack/langpack.cpp
blob: 56fd08f64fbc5e892ebfbc99c25a5b230b21bc09 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"
#include "langpack.h"

MIR_CORE_DLL(int) LoadLangPackDescr(const TCHAR *szLangPack, LANGPACK_INFO *lpInfo);

BOOL EnumLangpacks(ENUM_PACKS_CALLBACK callback, WPARAM wParam, LPARAM lParam)
{
	if (callback == NULL) return FALSE;

	BOOL res = FALSE;

	/* language folder */
	ptrT langpack(db_get_tsa(NULL, "Langpack", "Current"));

	LANGPACK_INFO pack;
	PathToAbsoluteT(_T("\\langpack_*.txt"), pack.tszFullPath);

	BOOL fPackFound = FALSE;
	WIN32_FIND_DATA wfd;
	HANDLE hFind = FindFirstFile(pack.tszFullPath, &wfd);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
			/* get data */
			TCHAR tszFullPath[MAX_PATH];
			PathToAbsoluteT(_T("\\"), tszFullPath);
			lstrcat(tszFullPath, wfd.cFileName);

			ZeroMemory(&pack, sizeof(pack));
			if (!LoadLangPackDescr(tszFullPath, &pack)) {
				pack.ftFileDate = wfd.ftLastWriteTime;
				/* enabled? */
				if (langpack && !lstrcmpi(langpack, wfd.cFileName)) {
					if (!fPackFound) pack.flags |= LPF_ENABLED;
					fPackFound = TRUE;
				}
				/* callback */
				res = callback(&pack, wParam, lParam);
				if (!res) { FindClose(hFind); return FALSE; }
			}
		} while (FindNextFile(hFind, &wfd));
		FindClose(hFind);
	}

	/* default langpack: English */
	if (callback != NULL) {
		ZeroMemory(&pack, sizeof(pack));
		pack.Locale = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
		lstrcpy(pack.tszLanguage, _T("English"));
		lstrcpyA(pack.szAuthors, "Miranda NG Development Team");
		lstrcpyA(pack.szAuthorEmail, "project-info@miranda-ng.org");
		DWORD v = CallService(MS_SYSTEM_GETVERSION, 0, 0);
		mir_snprintf(pack.szLastModifiedUsing, sizeof(pack.szLastModifiedUsing), "%d.%d.%d.%d", ((v >> 24) & 0xFF), ((v >> 16) & 0xFF), ((v >> 8) & 0xFF), (v & 0xFF));
		/* file date */
		if (GetModuleFileName(NULL, pack.tszFullPath, SIZEOF(pack.tszFullPath))) {
			lstrcpy(pack.tszFileName, _tcsrchr(pack.tszFullPath, '\\') + 1);
			HANDLE hFile = CreateFile(pack.tszFileName, 0, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
			if (hFile != INVALID_HANDLE_VALUE) {
				GetFileTime(hFile, NULL, NULL, &pack.ftFileDate);
				CloseHandle(hFile);
			}
		}
		pack.flags = LPF_DEFAULT;
		
		if (!fPackFound) pack.flags |= LPF_ENABLED;
		/* callback */
		if (!callback(&pack, wParam, lParam)) return FALSE;
	}

	return fPackFound;
}

/////////////////////////////////////////////////////////////////////////////////////////

void Langpack_LoadLangpack(void)
{
	HookEvent(ME_OPT_INITIALISE, LangpackOptionsInit);

	TCHAR szSearch[MAX_PATH];
	PathToAbsoluteT(_T("\\"), szSearch);

	/* try to load langpack */
	ptrT langpack(db_get_tsa(NULL, "Langpack", "Current"));
	if (langpack && langpack[0] != '\0') {
		lstrcat(szSearch, langpack);

		DWORD dwAttrib = GetFileAttributes(szSearch);
		if (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
			LoadLangPack(szSearch);
	}
	/* else try to load first file */
	else if (!langpack) {
		lstrcat(szSearch, _T("langpack_*.txt"));

		WIN32_FIND_DATA fd;
		HANDLE hFind = FindFirstFile(szSearch, &fd);
		if (hFind != INVALID_HANDLE_VALUE) {
			do {
				/* search first langpack that could be loaded */
				if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
				/* load langpack */
				PathToAbsoluteT(_T("\\"), szSearch);
				lstrcat(szSearch, fd.cFileName);
				if (!LoadLangPack(szSearch))
					db_set_ws(NULL, "Langpack", "Current", fd.cFileName);
				break;
			} while (FindNextFile(hFind, &fd));
			FindClose(hFind);
		}
	}
}