summaryrefslogtreecommitdiff
path: root/plugins/AuthState/src/main.cpp
blob: dc7c23cadd90bee32a1526aea14439f5e49a3709 (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
/*
   Authorization State plugin for Miranda NG (www.miranda-ng.org)
   (c) 2006-2010 by 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
*/

#include "commonheaders.h"

HINSTANCE g_hInst;
static HANDLE hOptInitialise;
static HANDLE hHookExtraIconsRebuild, hHookExtraIconsApply;
static HANDLE hAuthMenuSelected, hUserMenu;
HANDLE hExtraIcon;
int hLangpack;

BYTE bUseAuthIcon = 0, bUseGrantIcon = 0, bContactMenuItem = 0, bIconsForRecentContacts = 0, bUseAuthGroup = 0;

enum {
icon_none,
icon_auth,
icon_grant,
icon_both
};

PLUGININFOEX pluginInfo = {
    sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {DACE7D41-DFA9-4772-89AE-A59A6153E6B2}
	{0xdace7d41, 0xdfa9, 0x4772, {0x89, 0xae, 0xa5, 0x9a, 0x61, 0x53, 0xe6, 0xb2}}
};

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	g_hInst = hinstDLL;
	return TRUE;
}

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

INT_PTR getIconToUse(HANDLE hContact, LPARAM lParam)
{
	char *proto = GetContactProto(hContact);
//	if (lParam == 1) return icon_none;
	if (!DBGetContactSettingByte(hContact,"AuthState","ShowIcons",!bIconsForRecentContacts)) return icon_none;

	if (DBGetContactSettingByte(0,"ICQ","UseServerCList",0))
		if (DBGetContactSettingWord(hContact,proto,"ServerId",1) == 0)
			return icon_both;

	if (bUseAuthIcon & bUseGrantIcon)
		if (DBGetContactSettingByte(hContact,proto,"Auth",0) && DBGetContactSettingByte(hContact,proto,"Grant",0))
			return icon_both;

	if (bUseAuthIcon)
		if (DBGetContactSettingByte(hContact,proto,"Auth",0))
			return icon_auth;

	if (bUseGrantIcon)
		if (DBGetContactSettingByte(hContact,proto,"Grant",0))
			return icon_grant;
	return icon_none;
}

int onExtraImageApplying(WPARAM wParam, LPARAM lParam)
{
	if (wParam == NULL)
		return 0;

	int usedIcon = getIconToUse((HANDLE) wParam, lParam);

	const char *icon;
	switch (usedIcon) {
		case icon_both:   icon = "authgrant_icon";  break;
		case icon_grant:  icon = "grant_icon";  break;
		case icon_auth:   icon = "auth_icon";  break;
		default:          icon = NULL;  break;
	}
	ExtraIcon_SetIcon(hExtraIcon, (HANDLE)wParam, icon);
	return 0;
}

int onContactSettingChanged(WPARAM wParam,LPARAM lParam)
{
	DBCONTACTWRITESETTING *cws=(DBCONTACTWRITESETTING*)lParam;
	char *proto = GetContactProto((HANDLE)wParam);
	if (!proto) return 0;

	if (!lstrcmpA(cws->szModule,proto))
		if (!lstrcmpA(cws->szSetting,"Auth") || !lstrcmpA(cws->szSetting,"Grant") || !lstrcmpA(cws->szSetting,"ServerId"))
			onExtraImageApplying(wParam, 1);

	return 0;
}

int onDBContactAdded(WPARAM wParam, LPARAM lParam)
{
	// A new contact added, mark it as recent
	DBWriteContactSettingByte((HANDLE)wParam, MODULENAME, "ShowIcons", 1);
	onExtraImageApplying(wParam, 0);

	return 0;
}

INT_PTR onAuthMenuSelected(WPARAM wParam, LPARAM lParam)
{
	byte enabled = DBGetContactSettingByte((HANDLE)wParam,"AuthState","ShowIcons",1);
	DBWriteContactSettingByte((HANDLE)wParam, MODULENAME, "ShowIcons", !enabled);

	onExtraImageApplying(wParam, 0);
	return 0;
}

int onPrebuildContactMenu(WPARAM wParam, LPARAM lParam)
{
	char *proto = GetContactProto((HANDLE)wParam);
	if (!proto)
		return 0;

	CLISTMENUITEM mi = { sizeof(mi) };

	if (!DBGetContactSettingByte((HANDLE)wParam,proto,"Auth",0) && !DBGetContactSettingByte((HANDLE)wParam,proto,"Grant",0) && DBGetContactSettingWord((HANDLE)wParam,proto,"ServerId",0))
		mi.flags = CMIF_TCHAR | CMIM_FLAGS | CMIF_HIDDEN;
	else
		mi.flags = CMIF_TCHAR | CMIM_FLAGS;

	if (DBGetContactSettingByte((HANDLE)wParam,"AuthState","ShowIcons",1))
	{
		mi.flags |= CMIF_TCHAR | CMIM_NAME;
		mi.ptszName = LPGENT("Disable AuthState icons");
	}
	else
	{
		mi.flags |= CMIF_TCHAR | CMIM_NAME;
		mi.ptszName = LPGENT("Enable AuthState icons");
	}

	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hUserMenu, (LPARAM)&mi);

	return 0;
}

static IconItem iconList[] = 
{
	{ LPGEN("Auth"),  "auth_icon", IDI_AUTH },
	{ LPGEN("Grant"), "grant_icon", IDI_GRANT },
	{ LPGEN("Auth & Grant"), "authgrant_icon", IDI_AUTHGRANT }
};

int onModulesLoaded(WPARAM wParam,LPARAM lParam)
{
	// IcoLib support
	Icon_Register(g_hInst, "Auth State", iconList, SIZEOF(iconList));

	// extra icons
	hExtraIcon = ExtraIcon_Register("authstate", LPGEN("Auth State"), "authgrant_icon");

	// Set initial value for all contacts
	HANDLE hContact = db_find_first();
	while (hContact != NULL) {
		onExtraImageApplying((WPARAM)hContact, 1);
		hContact = db_find_next(hContact);
	}

	hOptInitialise = HookEvent(ME_OPT_INITIALISE, onOptInitialise);
	if (bContactMenuItem)
		HookEvent(ME_CLIST_PREBUILDCONTACTMENU, onPrebuildContactMenu);

	return 0;
}

int onSystemOKToExit(WPARAM wParam,LPARAM lParam)
{
	DestroyServiceFunction(hAuthMenuSelected);
	return 0;
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);

	HookEvent(ME_SYSTEM_MODULESLOADED, onModulesLoaded);
	HookEvent(ME_SYSTEM_OKTOEXIT,onSystemOKToExit);
	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, onContactSettingChanged);

	bUseAuthIcon = DBGetContactSettingByte(NULL, MODULENAME, "EnableAuthIcon", 1);
	bUseGrantIcon = DBGetContactSettingByte(NULL, MODULENAME, "EnableGrantIcon", 1);
	bContactMenuItem = DBGetContactSettingByte(NULL, MODULENAME, "MenuItem", 0);
	bIconsForRecentContacts = DBGetContactSettingByte(NULL, MODULENAME, "EnableOnlyForRecent", 0);

	HookEvent(ME_DB_CONTACT_ADDED, onDBContactAdded);

	if (bContactMenuItem)
	{
		hAuthMenuSelected = CreateServiceFunction("AuthState/MenuItem", onAuthMenuSelected);

		CLISTMENUITEM mi = { sizeof(mi) };
		mi.position = -1999901005;
		mi.flags = CMIF_TCHAR;
		mi.ptszName = LPGENT("Enable AuthState icons");
		mi.pszService = "AuthState/MenuItem";
		hUserMenu = Menu_AddContactMenuItem(&mi);
	}

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	return 0;
}