summaryrefslogtreecommitdiff
path: root/plugins/FingerprintNG/src/prepare.cpp
blob: 23b096fccbe492eddd76ff1139f1513a060ad05d (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
/*
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

extern wchar_t g_szSkinLib[MAX_PATH];

/*
*	Prepare
*	prepares upperstring masks and registers them in IcoLib
*/

static wchar_t *getSectionName(int flag)
{
	switch (flag) {
	case MIRANDA_CASE:
		if (g_plugin.getByte("GroupMiranda", 1))
			return LPGENW("Client icons") L"/" LPGENW("Miranda clients");
		break;

	case MIRANDA_VERSION_CASE:
		if (g_plugin.getByte("GroupMirandaVersion", 1))
			return LPGENW("Client icons") L"/" LPGENW("Miranda clients");
		break;

	case MIRANDA_PACKS_CASE:
		if (g_plugin.getByte("GroupMirandaPacks", 1))
			return LPGENW("Client icons") L"/" LPGENW("Miranda clients") L"/" LPGENW("Pack overlays");
		break;

	case MULTI_CASE:
		if (g_plugin.getByte("GroupMulti", 1))
			return LPGENW("Client icons") L"/" LPGENW("Multi-proto clients");
		break;

	case GG_CASE:
		if (g_plugin.getByte("GroupGG", 1))
			return LPGENW("Client icons") L"/" LPGENW("Gadu-Gadu clients");
		break;

	case IRC_CASE:
		if (g_plugin.getByte("GroupIRC", 1))
			return LPGENW("Client icons") L"/" LPGENW("IRC clients");
		break;

	case JABBER_CASE:
		if (g_plugin.getByte("GroupJabber", 1))
			return LPGENW("Client icons") L"/" LPGENW("Jabber clients");
		break;

	case RSS_CASE:
		if (g_plugin.getByte("GroupRSS", 1))
			return LPGENW("Client icons") L"/" LPGENW("RSS clients");
		break;

	case WEATHER_CASE:
		if (g_plugin.getByte("GroupWeather", 1))
			return LPGENW("Client icons") L"/" LPGENW("Weather clients");
		break;

	case FACEBOOK_CASE:
		if (g_plugin.getByte("GroupFacebook", 1))
			return LPGENW("Client icons") L"/" LPGENW("Facebook clients");
		break;

	case VK_CASE:
		if (g_plugin.getByte("GroupVK", 1))
			return LPGENW("Client icons") L"/" LPGENW("VKontakte clients");
		break;

	case OTHER_PROTOS_CASE:
		if (g_plugin.getByte("GroupOtherProtos", 1))
			return LPGENW("Client icons") L"/" LPGENW("Other protocols");
		break;

	case OTHERS_CASE:
		if (g_plugin.getByte("GroupOthers", 1))
			return LPGENW("Client icons") L"/" LPGENW("Other icons");
		break;

	case OVERLAYS_RESOURCE_CASE:
		if (g_plugin.getByte("GroupOverlaysResource", 1))
			return LPGENW("Client icons") L"/" LPGENW("Overlays") L"/" LPGENW("Resource");
		break;

	case OVERLAYS_PLATFORM_CASE:
		if (g_plugin.getByte("GroupOverlaysPlatform", 1))
			return LPGENW("Client icons") L"/" LPGENW("Overlays") L"/" LPGENW("Platform");
		break;

	case OVERLAYS_UNICODE_CASE:
		if (g_plugin.getByte("GroupOverlaysUnicode", 1))
			return LPGENW("Client icons") L"/" LPGENW("Overlays") L"/" LPGENW("Platform");
		break;

	case OVERLAYS_SECURITY_CASE:
		if (g_plugin.getByte("GroupOverlaysSecurity", 1))
			return LPGENW("Client icons") L"/" LPGENW("Overlays") L"/" LPGENW("Security");
		break;

	case OVERLAYS_PROTO_CASE:
		if (g_plugin.getByte("GroupOverlaysProtos", 1))
			return LPGENW("Client icons") L"/" LPGENW("Overlays") L"/" LPGENW("Protocol");
		break;

	default:
		return LPGENW("Client icons");
	}
	return nullptr;
}

void Prepare(KN_FP_MASK *mask, bool bEnable, LIST<KN_FP_MASK> &dest)
{
	mask->szMaskUpper = nullptr;

	if (mask->hIcolibItem)
		IcoLib_RemoveIconByHandle(mask->hIcolibItem);
	mask->hIcolibItem = nullptr;

	if (!mask->szMask || !bEnable)
		return;

	size_t iMaskLen = mir_wstrlen(mask->szMask) + 1;
	LPTSTR pszNewMask = (LPTSTR)HeapAlloc(hHeap, HEAP_NO_SERIALIZE, iMaskLen * sizeof(wchar_t));
	wcscpy_s(pszNewMask, iMaskLen, mask->szMask);
	_wcsupr_s(pszNewMask, iMaskLen);
	mask->szMaskUpper = pszNewMask;

	wchar_t destfile[MAX_PATH];
	if (mask->iIconIndex == IDI_NOTFOUND || mask->iIconIndex == IDI_UNKNOWN || mask->iIconIndex == IDI_UNDETECTED)
		GetModuleFileName(g_plugin.getInst(), destfile, MAX_PATH);
	else {
		wcsncpy_s(destfile, g_szSkinLib, _TRUNCATE);
		if (0 != _waccess(destfile, 0))
			return;
	}

	LPTSTR SectName = getSectionName(mask->iSectionFlag);
	if (SectName == nullptr)
		return;

	SKINICONDESC sid = {};
	sid.flags = SIDF_ALL_UNICODE;
	sid.section.w = SectName;
	sid.pszName = mask->szIconName;
	sid.description.w = mask->szClientDescription;
	sid.defaultFile.w = destfile;
	sid.iDefaultIndex = -mask->iIconIndex;
	mask->hIcolibItem = g_plugin.addIcon(&sid);

	mask->idx = dest.getCount();
	dest.insert(mask);
}

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

static LIST<KN_FP_MASK> arCustom(10);

void UnregisterCustomIcons()
{
	for (auto &it : arCustom) {
		if (it->hIcolibItem)
			IcoLib_RemoveIconByHandle(it->hIcolibItem);

		mir_free(it->szClientDescription);
		mir_free(it->szIconName);
		mir_free(it->szMask);
		mir_free(it);
	}		
}

void RegisterCustomIcons()
{
	UnregisterCustomIcons();

	FILE *in = _wfopen(VARSW(L"%miranda_userdata%\\fingerprint.json"), L"r");
	if (in == nullptr)
		return;

	int cbSize = filelength(fileno(in));
	ptrA szJson((char*)mir_alloc(cbSize+1));
	fread(szJson, 1, cbSize, in);
	fclose(in);

	JSONNode root = JSONNode::parse(szJson);
	if (!root)
		return;

	for (auto &it : root["masks"]) {
		CMStringA szName = it["name"].as_mstring();
		CMStringW wszMask = it["mask"].as_mstring();
		CMStringW szDescription = it["descr"].as_mstring();
		if (wszMask.IsEmpty() || szName.IsEmpty() || szDescription.IsEmpty())
			continue;

		wchar_t wszFullPath[MAX_PATH];
		PathToAbsoluteW(it["iconFile"].as_mstring(), wszFullPath);
		if (0 != _waccess(wszFullPath, 0))
			continue;

		LPTSTR SectName = getSectionName(it["group"].as_int());
		if (SectName == nullptr)
			continue;

		auto *pMask = (KN_FP_MASK *)mir_calloc(sizeof(KN_FP_MASK));

		int iMaskLen = wszMask.GetLength() + 1;
		pMask->szMaskUpper = (LPTSTR)HeapAlloc(hHeap, HEAP_NO_SERIALIZE, iMaskLen * sizeof(wchar_t));
		wcscpy_s(pMask->szMaskUpper, iMaskLen, wszMask);
		_wcsupr_s(pMask->szMaskUpper, iMaskLen);
		pMask->szMask = pMask->szMaskUpper;

		pMask->iIconIndex = it["iconIndex"].as_int();
		pMask->szIconName = szName.Detach();
		pMask->szClientDescription = szDescription.Detach();

		SKINICONDESC sid = {};
		sid.flags = SIDF_ALL_UNICODE;
		sid.section.w = SectName;
		sid.pszName = pMask->szIconName;
		sid.description.w = pMask->szClientDescription;
		sid.defaultFile.w = wszFullPath;
		sid.iDefaultIndex = -pMask->iIconIndex;
		pMask->hIcolibItem = g_plugin.addIcon(&sid);

		pMask->idx = g_arCommon.getCount();
		g_arCommon.insert(pMask, 0);
	}
}