summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/theme.cpp
blob: a457f9a6037dff843427ec1a45f8a27a48ad8c10 (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
	#include "common.h"

extern OBJLIST<WhatsAppProto> g_Instances;

static IconItem icons[] =
{
	{ LPGEN("WhatsApp Icon"),				 "whatsApp",					  IDI_WHATSAPP },
	{ LPGEN("Add To Group"),				  "addContactToGroup",		  IDI_ADD_USER_TO_GROUP},
	{ LPGEN("Create Chat Group"),			"createGroup",				  IDI_ADD_GROUP },
	{ LPGEN("Remove From Chat Group"),	 "removeContactFromGroup",	IDI_REMOVE_USER_FROM_GROUP },
	{ LPGEN("Leave And Delete Group"),	 "leaveAndDeleteGroup",		IDI_LEAVE_GROUP },
	{ LPGEN("Leave Group"),					"leaveGroup",					IDI_LEAVE_GROUP },
	{ LPGEN("Change Group Subject"),		"changeGroupSubject",		 IDI_CHANGE_GROUP_SUBJECT }
};

void InitIcons(void)
{
	Icon_Register(g_hInstance, "Protocols/WhatsApp", icons, SIZEOF(icons), "WhatsApp");
}

HANDLE GetIconHandle(const char* name)
{
	for(size_t i=0; i<SIZEOF(icons); i++)
		if(strcmp(icons[i].szName, name) == 0)
			return icons[i].hIcolib;

	return 0;
}

char *GetIconDescription(const char* name)
{
	for(size_t i=0; i<SIZEOF(icons); i++)
		if(strcmp(icons[i].szName, name) == 0)
			return icons[i].szDescr;

	return "";
}

// Contact List menu stuff
HANDLE hHookPreBuildMenu;
HANDLE g_hContactMenuItems[CMITEMS_COUNT];
HANDLE g_hContactMenuSvc[CMITEMS_COUNT];


// Helper functions
static WhatsAppProto* GetInstanceByHContact(HANDLE hContact)
{
	char *proto = GetContactProto(hContact);
	if( !proto )
		return 0;

	for(int i=0; i<g_Instances.getCount(); i++)
		if(!strcmp(proto,g_Instances[i].m_szModuleName))
			return &g_Instances[i];

	return 0;
}

template<int (__cdecl WhatsAppProto::*Fcn)(WPARAM,LPARAM)>
INT_PTR GlobalService(WPARAM wParam,LPARAM lParam)
{
	WhatsAppProto *proto = GetInstanceByHContact(reinterpret_cast<HANDLE>(wParam));
	return proto ? (proto->*Fcn)(wParam,lParam) : 0;
}

template<INT_PTR (__cdecl WhatsAppProto::*Fcn)(WPARAM,LPARAM,LPARAM)>
INT_PTR GlobalServiceParam(WPARAM wParam,LPARAM lParam, LPARAM lParam2)
{
	WhatsAppProto *proto = GetInstanceByHContact(reinterpret_cast<HANDLE>(wParam));
	return proto ? (proto->*Fcn)(wParam,lParam,lParam2) : 0;
}


static int PrebuildContactMenu(WPARAM wParam,LPARAM lParam)
{
	for (size_t i=0; i<SIZEOF(g_hContactMenuItems); i++)
	{
		EnableMenuItem(g_hContactMenuItems[i], false);
	}

	WhatsAppProto *proto = GetInstanceByHContact(reinterpret_cast<HANDLE>(wParam));
	return proto ? proto->OnPrebuildContactMenu(wParam,lParam) : 0;
}

void WhatsAppProto::InitContactMenus()
{
	hHookPreBuildMenu = HookEvent(ME_CLIST_PREBUILDCONTACTMENU, PrebuildContactMenu);

	CLISTMENUITEM mi = {sizeof(mi)};

	mi.position = -2000006100;
	mi.icolibItem = GetIconHandle("leaveGroup");
	mi.pszName = GetIconDescription("leaveGroup");
	mi.pszService = "WhatsAppProto/LeaveGroup";
	g_hContactMenuSvc[CMI_LEAVE_GROUP] =
		CreateServiceFunction(mi.pszService,GlobalService<&WhatsAppProto::OnLeaveGroup>);
	g_hContactMenuItems[CMI_LEAVE_GROUP] = Menu_AddContactMenuItem(&mi);

	mi.position = -2000006100;
	mi.icolibItem = GetIconHandle("leaveAndDeleteGroup");
	mi.pszName = GetIconDescription("leaveAndDeleteGroup");
	g_hContactMenuSvc[CMI_REMOVE_GROUP] = g_hContactMenuSvc[CMI_LEAVE_GROUP];
	g_hContactMenuItems[CMI_REMOVE_GROUP] = Menu_AddContactMenuItem(&mi);

	mi.position = -2000006099;
	mi.icolibItem = GetIconHandle("changeGroupSubject");
	mi.pszName = GetIconDescription("changeGroupSubject");
	mi.pszService = "WhatsAppProto/ChangeGroupSubject";
	g_hContactMenuSvc[CMI_CHANGE_GROUP_SUBJECT] =
		CreateServiceFunction(mi.pszService,GlobalService<&WhatsAppProto::OnChangeGroupSubject>);
	g_hContactMenuItems[CMI_CHANGE_GROUP_SUBJECT] = Menu_AddContactMenuItem(&mi);

}

void EnableMenuItem(HANDLE hMenuItem, bool enable)
{
	CLISTMENUITEM clmi = { sizeof(clmi) };
	clmi.flags = CMIM_FLAGS;
	if (!enable)
		clmi.flags |= CMIF_HIDDEN;

	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hMenuItem, (LPARAM)&clmi);
}

int WhatsAppProto::OnPrebuildContactMenu(WPARAM wParam,LPARAM lParam)
{	
	HANDLE hContact = reinterpret_cast<HANDLE>(wParam);
	if (hContact)
		LOG(this->GetContactDisplayName(hContact).c_str());
	else
		LOG("No contact found");

	if (g_hContactMenuItems[CMI_ADD_CONTACT_TO_GROUP] != NULL)
	{
		CallService("CList/RemoveContactMenuItem", (WPARAM) g_hContactMenuItems[CMI_ADD_CONTACT_TO_GROUP], (LPARAM) 0);
	}
	if (g_hContactMenuItems[CMI_REMOVE_CONTACT_FROM_GROUP] != NULL)
	{
		CallService("CList/RemoveContactMenuItem", (WPARAM) g_hContactMenuItems[CMI_REMOVE_CONTACT_FROM_GROUP], (LPARAM) 0);
	}
	
	int chatType = db_get_b(hContact, m_szModuleName, "SimpleChatRoom", 0);

	CLISTMENUITEM mi = {sizeof(mi)};

	if (chatType == 0)
	{
		mi.flags = CMIF_CHILDPOPUP;
		mi.position= -2000006102;
		mi.icolibItem = GetIconHandle("addContactToGroup");
		mi.pszName = GetIconDescription("addContactToGroup");
		mi.pszService = NULL;
		g_hContactMenuItems[CMI_ADD_CONTACT_TO_GROUP] = Menu_AddContactMenuItem(&mi);

		if (!isOnline())
		{
			mi.flags = CMIM_FLAGS | CMIF_GRAYED;
			CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM) g_hContactMenuItems[CMI_ADD_CONTACT_TO_GROUP], (LPARAM) &mi);
			return 0;
		}

		mi.hParentMenu = (HGENMENU) g_hContactMenuItems[CMI_ADD_CONTACT_TO_GROUP];
		mi.flags = CMIF_ROOTHANDLE | CMIF_TCHAR;

		string fullSvcName;
		string svcName = m_szModuleName;
		svcName += "/AddContactToGroup_";
		DBVARIANT dbv;

		for (map<HANDLE, map<HANDLE, bool>>::iterator it = this->isMemberByGroupContact.begin();
			it != this->isMemberByGroupContact.end(); ++it)
		{
			map<HANDLE, bool>::iterator memberIt = it->second.find(hContact);
			// Only, if current contact is not already member of this group
			if ((memberIt == it->second.end() || memberIt->second == false) &&
				!db_get_s(it->first, m_szModuleName, "ID", &dbv, DBVT_ASCIIZ))
			{
				fullSvcName = svcName + dbv.pszVal;
				mi.pszService = (char*) fullSvcName.c_str();
				mi.ptszName = mir_a2t_cp(this->GetContactDisplayName(it->first).c_str(), CP_UTF8);
				CreateServiceFunctionParam(mi.pszService, GlobalServiceParam<&WhatsAppProto::OnAddContactToGroup>, (LPARAM) it->first);
				Menu_AddContactMenuItem(&mi);
				db_free(&dbv);
				mir_free(mi.ptszName);
			}
		}
	}
	else if (chatType == 1)
	{
		mi.flags = CMIM_FLAGS;
		if (!isOnline() || db_get_b(hContact, m_szModuleName, "IsGroupMember", 0) == 0)
		{
			mi.flags |= CMIF_GRAYED;
		}
		CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM) g_hContactMenuItems[CMI_LEAVE_GROUP], (LPARAM) &mi);
	}
	else if (chatType == 2)
	{
		// owning chat/group
		mi.flags = CMIF_CHILDPOPUP;
		mi.position= -2000006102;
		mi.icolibItem = GetIconHandle("removeContactFromGroup");
		mi.pszName = GetIconDescription("removeContactFromGroup");
		mi.pszService = NULL;
		g_hContactMenuItems[CMI_REMOVE_CONTACT_FROM_GROUP] = Menu_AddContactMenuItem(&mi);

		if (isOnline() &&  db_get_b(hContact, m_szModuleName, "IsGroupMember", 0) == 1)
		{
			map<HANDLE, map<HANDLE, bool>>::iterator groupsIt = this->isMemberByGroupContact.find(hContact);
			if (groupsIt == this->isMemberByGroupContact.end())
			{
				LOG("Group exists only on contact list");
			}
			else
			{
				mi.hParentMenu = (HGENMENU) g_hContactMenuItems[CMI_REMOVE_CONTACT_FROM_GROUP];
				mi.flags = CMIF_ROOTHANDLE | CMIF_TCHAR;

				string fullSvcName;
				string svcName = m_szModuleName;
				svcName += "/RemoveContactFromGroup_";
				DBVARIANT dbv;

				for (map<HANDLE, bool>::iterator it = groupsIt->second.begin(); it != groupsIt->second.end(); ++it)
				{
					if (!db_get_s(it->first, m_szModuleName, "ID", &dbv, DBVT_ASCIIZ))
					{
						fullSvcName = svcName + dbv.pszVal;
						mi.pszService = (char*) fullSvcName.c_str();
						mi.ptszName = mir_a2t_cp(this->GetContactDisplayName(it->first).c_str(), CP_UTF8);
						CreateServiceFunctionParam(mi.pszService,
							GlobalServiceParam<&WhatsAppProto::OnRemoveContactFromGroup>, (LPARAM) it->first);
						Menu_AddContactMenuItem(&mi);
						db_free(&dbv);
						mir_free(mi.ptszName);
					}
				}
			}
			mi.flags = CMIM_FLAGS;
		}
		else
		{
			mi.flags = CMIM_FLAGS | CMIF_GRAYED;
			CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM) g_hContactMenuItems[CMI_REMOVE_CONTACT_FROM_GROUP], (LPARAM) &mi);
		}
		CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM) g_hContactMenuItems[CMI_REMOVE_GROUP], (LPARAM) &mi);
		CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM) g_hContactMenuItems[CMI_CHANGE_GROUP_SUBJECT], (LPARAM) &mi);
	}

	return 0;
}

int WhatsAppProto::OnBuildStatusMenu(WPARAM wParam,LPARAM lParam)
{
	LOG("");
	char text[200];
	strcpy(text,m_szModuleName);
	char *tDest = text+strlen(text);

	CLISTMENUITEM mi = {sizeof(mi)};
	mi.pszService = text;

	HGENMENU hRoot = MO_GetProtoRootMenu(m_szModuleName);
	if (hRoot == NULL) {
		mi.popupPosition = 500085000;
		mi.hParentMenu = HGENMENU_ROOT;
		mi.flags = CMIF_ROOTPOPUP | CMIF_TCHAR | CMIF_KEEPUNTRANSLATED | ( this->isOnline() ? 0 : CMIF_GRAYED );
		mi.icolibItem = GetIconHandle( "whatsApp" );
		mi.ptszName = m_tszUserName;
		hRoot = m_hMenuRoot = Menu_AddProtoMenuItem(&mi);
	} else {
		if ( m_hMenuRoot )
			CallService( MS_CLIST_REMOVEMAINMENUITEM, ( WPARAM )m_hMenuRoot, 0 );
		m_hMenuRoot = NULL;
	}

	mi.flags = CMIF_CHILDPOPUP | ( this->isOnline() ? 0 : CMIF_GRAYED );
	mi.position = 201001;

	CreateProtoService(m_szModuleName, "/CreateGroup", &WhatsAppProto::OnCreateGroup, this);
	strcpy(tDest, "/CreateGroup");
	mi.hParentMenu = hRoot;
	mi.pszName = LPGEN("Create Group");
	mi.icolibItem = GetIconHandle("createGroup");
	m_hMenuCreateGroup = Menu_AddProtoMenuItem(&mi);

	return 0;
}

void WhatsAppProto::ToggleStatusMenuItems( BOOL bEnable )
{
	CLISTMENUITEM clmi = {sizeof(clmi)};
	clmi.flags = CMIM_FLAGS | (( bEnable ) ? 0 : CMIF_GRAYED);

	CallService( MS_CLIST_MODIFYMENUITEM, ( WPARAM ) m_hMenuRoot,	( LPARAM )&clmi );
	CallService( MS_CLIST_MODIFYMENUITEM, ( WPARAM ) m_hMenuCreateGroup, ( LPARAM )&clmi );
}