summaryrefslogtreecommitdiff
path: root/plugins/AddContactPlus/src/main.cpp
blob: c81962fb5edf17107fa52da9b1eaeda25c258cdc (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
/*

AddContact+ plugin for Miranda IM

Copyright (C) 2007-2011 Bartosz 'Dezeath' Bia³ek

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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "addcontactplus.h"

HINSTANCE hInst;
int hLangpack;
static HANDLE hModulesLoaded = 0, hChangedIcons = 0, hAccListChanged = 0,
			  hMainMenuItem = 0, hToolBarItem = 0, hService = 0;
HANDLE hIconLibItem;

PLUGININFOEX pluginInfo = {
    sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {6471D451-2FE0-4ee2-850E-9F84F3C0D187}
	{ 0x6471d451, 0x2fe0, 0x4ee2, { 0x85, 0xe, 0x9f, 0x84, 0xf3, 0xc0, 0xd1, 0x87 } }
};

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

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

extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_ADDCONTACTPLUS, MIID_LAST};

INT_PTR AddContactPlusDialog(WPARAM, LPARAM)
{
	CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ADDCONTACT), (HWND)NULL, AddContactDlgProc, 0);
	return 0;
}

static int OnIconsChanged(WPARAM, LPARAM)
{
	CLISTMENUITEM mi = {0};

	if (!hMainMenuItem)
		return 0;

	mi.cbSize = sizeof(mi);
	mi.flags = CMIM_ICON | CMIF_ICONFROMICOLIB | CMIF_TCHAR;
	mi.icolibItem = hIconLibItem;
	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hMainMenuItem, (LPARAM)&mi);

	return 0;
}

static int OnAccListChanged(WPARAM, LPARAM)
{
	PROTOACCOUNT** pAccounts;
	int iRealAccCount, iAccCount = 0;
	DWORD dwCaps;

	ProtoEnumAccounts(&iRealAccCount, &pAccounts);
	for (int i = 0; i < iRealAccCount; i++)
	{
		if (!IsAccountEnabled(pAccounts[i])) continue;
		dwCaps = (DWORD)CallProtoService(pAccounts[i]->szModuleName, PS_GETCAPS, PFLAGNUM_1, 0);
		if (dwCaps & PF1_BASICSEARCH || dwCaps & PF1_EXTSEARCH || dwCaps & PF1_SEARCHBYEMAIL || dwCaps & PF1_SEARCHBYNAME)
			iAccCount++;
	}

	if (iAccCount)
	{
		CLISTMENUITEM mi = {0};

		if (hMainMenuItem)
			return 0;

		mi.cbSize = sizeof(mi);
		mi.position = 500020001;
		mi.flags = CMIF_ICONFROMICOLIB | CMIF_TCHAR;
		mi.icolibItem = hIconLibItem;
		mi.ptszName = LPGENT("&Add Contact...");
		mi.pszService = MS_ADDCONTACTPLUS_SHOW;
		hMainMenuItem = Menu_AddMainMenuItem(&mi);
	}
	else
	{
		if (!hMainMenuItem)
			return 0;

		CallService(MS_CLIST_REMOVEMAINMENUITEM, (WPARAM)hMainMenuItem, 0);
		CallService(MS_TTB_REMOVEBUTTON, (WPARAM)hToolBarItem, 0);

		hMainMenuItem = 0;
	}

	return 0;
}

static int CreateButton(WPARAM, LPARAM)
{
	TTBButton tbb = {0};
	tbb.cbSize = sizeof(tbb);
	tbb.dwFlags = TTBBF_VISIBLE | TTBBF_SHOWTOOLTIP;
	tbb.pszService = MS_ADDCONTACTPLUS_SHOW;
	tbb.name = tbb.pszTooltipUp = LPGEN("Add Contact");
	tbb.hIconHandleUp = hIconLibItem;
	hToolBarItem = TopToolbar_AddButton(&tbb);
	return 0;
}

static int OnModulesLoaded(WPARAM, LPARAM)
{
	SKINICONDESC sid = {0};
	char szFile[MAX_PATH];
	GetModuleFileNameA(hInst, szFile, MAX_PATH);
	sid.cbSize = sizeof(SKINICONDESC);
	sid.flags = SIDF_TCHAR;
	sid.pszDefaultFile = szFile;
	sid.ptszSection = _T("AddContact+");
	sid.iDefaultIndex = -IDI_ADDCONTACT;
	sid.ptszDescription = LPGENT("Add Contact");
	sid.pszName = ICON_ADD;
	hIconLibItem = Skin_AddIcon(&sid);
	hChangedIcons = HookEvent(ME_SKIN2_ICONSCHANGED, OnIconsChanged);

	HOTKEYDESC hkd = {0};
	hkd.cbSize = sizeof(hkd);
	hkd.dwFlags = HKD_TCHAR;
	hkd.pszName = "AddContactPlus_OpenDialog";
	hkd.ptszDescription = LPGENT("Open Add Contact Dialog");
	hkd.ptszSection = LPGENT("Main");
	hkd.pszService = MS_ADDCONTACTPLUS_SHOW;
	hkd.DefHotKey = HOTKEYCODE(HOTKEYF_CONTROL | HOTKEYF_SHIFT, 'C') | HKF_MIRANDA_LOCAL;
	Hotkey_Register(&hkd);

	OnAccListChanged(0, 0);
	
	HookEvent(ME_TTB_MODULELOADED, CreateButton);
	return 0;
}

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

	INITCOMMONCONTROLSEX icex = {0};
	icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
	icex.dwICC = ICC_USEREX_CLASSES;
	InitCommonControlsEx(&icex);

	hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	hAccListChanged = HookEvent(ME_PROTO_ACCLISTCHANGED, OnAccListChanged);
	hService = CreateServiceFunction(MS_ADDCONTACTPLUS_SHOW, AddContactPlusDialog);

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	UnhookEvent(hModulesLoaded);
	UnhookEvent(hChangedIcons);
	UnhookEvent(hAccListChanged);
	DestroyServiceFunction(hService);

	return 0;
}