summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/chat/clist.cpp
blob: 644fdf2338f62bd220c0742db48f6eca6113b444 (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
/*
astyle --force-indent=tab=4 --brackets=linux --indent-switches
		--pad=oper --one-line=keep-blocks  --unpad=paren

Chat module plugin for Miranda NG

Copyright (C) 2003 Jörgen Persson

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.

Group chat module: contact list support functions. Add chat rooms as
contacts and handle default action for such contacts.

This code was taken in almost unmodified from from the chat.dll
plugin, originally written by Jörgen Persson

*/

/*
 * handles interaction of the group chat implementation with the contact list plugin
 */

#include "..\commonheaders.h"

char *szChatIconString = "chaticon";

static HANDLE Clist_GroupExists(TCHAR *tszGroup)
{
	for (int i=0; ; i++) {
		char str[10];
		_itoa(i, str, 10);
		ptrT groupName( db_get_tsa(0, "CListGroups", str));
		if (groupName == NULL)
			return NULL;

		if (!_tcscmp(tszGroup, &groupName[1]) && lstrlen(tszGroup) == lstrlen(&groupName[1]))
			return HANDLE(i + 1);
	}
}

HANDLE CList_AddRoom(const char* pszModule, const TCHAR* pszRoom, const TCHAR* pszDisplayName, int iType)
{
	TCHAR pszGroup[50]; *pszGroup = '\0';

	ptrT groupName( db_get_tsa(NULL, "Chat", "AddToGroup"));
	if (groupName)
		_tcsncpy_s(pszGroup, SIZEOF(pszGroup), groupName, _TRUNCATE);
	else
		_tcscpy(pszGroup, _T("Chat rooms"));

	if (pszGroup[0])
		CList_CreateGroup(pszGroup);

	HANDLE hContact = CList_FindRoom(pszModule, pszRoom);
	if (hContact)
		return hContact;

	// here we create a new one since no one is to be found
	if ((hContact = (HANDLE)CallService(MS_DB_CONTACT_ADD, 0, 0)) == NULL)
		return NULL;

	CallService(MS_PROTO_ADDTOCONTACT, (WPARAM)hContact, (LPARAM)pszModule);
	if (pszGroup && lstrlen(pszGroup) > 0)
		CallService(MS_CLIST_CONTACTCHANGEGROUP, (WPARAM)hContact, (LPARAM)g_Settings.hGroup);
	else
		db_unset(hContact, "CList", "Group");

	db_set_ts(hContact, pszModule, "Nick", pszDisplayName);
	db_set_ts(hContact, pszModule, "ChatRoomID", pszRoom);
	db_set_b(hContact, pszModule, "ChatRoom", (BYTE)iType);
	db_set_w(hContact, pszModule, "Status", ID_STATUS_OFFLINE);
	return hContact;
}

BOOL CList_SetOffline(HANDLE hContact, BOOL bHide)
{
	if (hContact) {
		char *szProto = GetContactProto(hContact);
		if (szProto == NULL)
			return FALSE;

		db_set_w(hContact, szProto, "ApparentMode", 0);
		db_set_w(hContact, szProto, "Status", ID_STATUS_OFFLINE);
		return TRUE;
	}
	return FALSE;
}

BOOL CList_SetAllOffline(BOOL bHide, const char *pszModule)
{
	for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
		char *szProto = GetContactProto(hContact);
		if (MM_FindModule(szProto)) {
			if (!pszModule || (pszModule && !strcmp(pszModule, szProto))) {
				int i = db_get_b(hContact, szProto, "ChatRoom", 0);
				if (i != 0) {
					db_set_w(hContact, szProto, "ApparentMode", (LPARAM)(WORD) 0);
					db_set_w(hContact, szProto, "Status", ID_STATUS_OFFLINE);
				}
			}
		}
	}
	return TRUE;
}

int CList_RoomDoubleclicked(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	if (!hContact)
		return 0;

	char *szProto = GetContactProto(hContact);
	if ( !MM_FindModule(szProto) || !db_get_b(hContact, szProto, "ChatRoom", 0))
		return 0;

	ptrT roomid( db_get_tsa(hContact, szProto, "ChatRoomID"));
	if (roomid == NULL)
		return 0;

	SESSION_INFO *si = SM_FindSession(roomid, szProto);
	if (si) {
		// is the "toggle visibility option set, so we need to close the window?
		if (si->hWnd != NULL &&
				M.GetByte("Chat", "ToggleVisibility", 0) == 1 &&
				!CallService(MS_CLIST_GETEVENT, (WPARAM)hContact, 0) &&
				IsWindowVisible(si->hWnd) && !IsIconic(si->pContainer->hwnd))
		{
			PostMessage(si->hWnd, GC_CLOSEWINDOW, 0, 0);
			return 1;
		}

		ShowRoom(si, WINDOW_VISIBLE, TRUE);
		if (lParam) {
			SendMessage(si->hWnd, DM_ACTIVATEME, 0, 0);
			if (si->dat)
				SetForegroundWindow(si->dat->pContainer->hwnd);
		}
	}
	return 1;
}

INT_PTR CList_EventDoubleclicked(WPARAM wParam, LPARAM lParam)
{
	return CList_RoomDoubleclicked((WPARAM)((CLISTEVENT*)lParam)->hContact, 0);
}

INT_PTR CList_JoinChat(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	if (hContact) {
		char *szProto = GetContactProto(hContact);
		if (szProto) {
			if ( db_get_w(hContact, szProto, "Status", 0) == ID_STATUS_OFFLINE)
				CallProtoService(szProto, PS_JOINCHAT, wParam, lParam);
			else
				CList_RoomDoubleclicked(wParam, 0);
		}
	}

	return 0;
}

INT_PTR CList_LeaveChat(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	if (hContact) {
		char *szProto = GetContactProto(hContact);
		if (szProto)
			CallProtoService( szProto, PS_LEAVECHAT, wParam, lParam );
	}
	return 0;
}

int CList_PrebuildContactMenu(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	if (hContact == NULL)
		return 0;

	bool bEnabled = false;
	char *szProto = GetContactProto(hContact);
	if (szProto) {
		// display this menu item only for chats
		if ( db_get_b(hContact, szProto, "ChatRoom", 0 )) {
			// still hide it for offline protos
			if (CallProtoService( szProto, PS_GETSTATUS, 0, 0 ) != ID_STATUS_OFFLINE) {
				CLISTMENUITEM clmi = { sizeof(clmi) };
				clmi.flags = CMIM_NAME;
				if (db_get_w( hContact, szProto, "Status", 0 ) == ID_STATUS_OFFLINE)
					clmi.pszName = (char*)LPGEN("Join chat");
				else
					clmi.pszName = (char*)LPGEN("Open chat window");
				Menu_ModifyItem(hJoinMenuItem, &clmi);
				bEnabled = true;
	}	}	}

	Menu_ShowItem(hJoinMenuItem, bEnabled);
	Menu_ShowItem(hLeaveMenuItem, bEnabled);
	return 0;
}

INT_PTR CList_PrebuildContactMenuSvc(WPARAM wParam, LPARAM lParam)
{
	return CList_PrebuildContactMenu(wParam, lParam);
}

void CList_CreateGroup(TCHAR* group)
{
	if (!group)
		return;

	g_Settings.hGroup = Clist_GroupExists(group);

	if (g_Settings.hGroup == 0) {
		g_Settings.hGroup = (HANDLE)CallService(MS_CLIST_GROUPCREATE, 0, (LPARAM)group);

		if (g_Settings.hGroup) {
			CallService(MS_CLUI_GROUPADDED, (WPARAM)g_Settings.hGroup, 0);
			CallService(MS_CLIST_GROUPSETEXPANDED, (WPARAM)g_Settings.hGroup, 1);
		}
	}
}

BOOL CList_AddEvent(HANDLE hContact, HICON Icon, HANDLE event, int type, const TCHAR* fmt, ...)
{
	if (!fmt || lstrlen(fmt) < 1 || lstrlen(fmt) > 2000)
		return FALSE;

	TCHAR* szBuf = (TCHAR*)alloca(4096 * sizeof(TCHAR));

	va_list marker;
	va_start(marker, fmt);
	mir_vsntprintf(szBuf, 4096, fmt, marker);
	va_end(marker);

	CLISTEVENT cle;
	cle.cbSize = sizeof(cle);
	cle.hContact = (HANDLE)hContact;
	cle.hDbEvent = (HANDLE)event;
	cle.flags = type + CLEF_TCHAR;
	cle.hIcon = Icon;
	cle.pszService = "GChat/DblClickEvent" ;
	cle.ptszTooltip = TranslateTS(szBuf);
	if (type) {
		if (!CallService(MS_CLIST_GETEVENT, (WPARAM)hContact, 0))
			CallService(MS_CLIST_ADDEVENT, (WPARAM)hContact, (LPARAM)&cle);
	}
	else {
		if (CallService(MS_CLIST_GETEVENT, (WPARAM)hContact, 0))
			CallService(MS_CLIST_REMOVEEVENT, (WPARAM)hContact, (LPARAM)"chaticon");
		CallService(MS_CLIST_ADDEVENT, (WPARAM)hContact, (LPARAM)&cle);
	}
	return TRUE;
}

HANDLE CList_FindRoom(const char* pszModule, const TCHAR* pszRoom)
{
	for (HANDLE hContact = db_find_first(pszModule); hContact; hContact = db_find_next(hContact, pszModule)) {
		if ( !db_get_b(hContact, pszModule, "ChatRoom", 0))
			continue;

		ptrT roomid( db_get_tsa(hContact, pszModule, "ChatRoomID"));
		if (roomid != NULL && !lstrcmpi(roomid, pszRoom))
			return hContact;
	}
	return 0;
}