File: useractions.cpp

package info (click to toggle)
springlobby 0.267%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,780 kB
  • sloc: cpp: 79,169; ansic: 61,599; python: 927; sh: 735; perl: 238; xml: 52; makefile: 42; sed: 16
file content (329 lines) | stat: -rw-r--r-- 9,556 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* This file is part of the Springlobby (GPL v2 or later), see COPYING */

#include "useractions.h"

#include <lslutils/globalsmanager.h>
#include <wx/colour.h>
#include <wx/intl.h>
#include <wx/log.h>
#include <cmath>

#include "gui/battlelist/battlelisttab.h"
#include "gui/customdialogs.h"
#include "gui/hosting/battleroomtab.h"
#include "gui/hosting/mainjoinbattletab.h"
#include "gui/mainchattab.h"
#include "gui/mainwindow.h"
#include "gui/ui.h"
#include "settings.h"
#include "utils/conversion.h"
#include "utils/slconfig.h"

const wxColour defaultHLcolor(255, 0, 0);

UserActions& useractions()
{
	static LSL::Util::LineInfo<UserActions> m(AT);
	static LSL::Util::GlobalObjectHolder<UserActions, LSL::Util::LineInfo<UserActions> > m_useractions(m);
	return m_useractions;
}

UserActions::UserActions()
{
	Init();
}

UserActions::~UserActions()
{
}

bool UserActions::DoActionOnUser(const UserActions::ActionType action, const wxString& name)
{
	// preventing action on oneself wasn't the best idea, login gets disabled
	//if ( m_knownUsers.Index( name ) == -1 || ui().IsThisMe(name) || action == ActNone )

	if (m_knownUsers.Index(name) == -1 || action == ActNone)
		return false;
	else
		return (m_actionsGroups.find(action) != m_actionsGroups.end() && m_actionsPeople[action].Index(name) != -1);
}

void UserActions::Init()
{
	m_actionNames.clear();
	m_actionNames.push_back(_("none"));
	m_actionNames.push_back(_("highlight"));
	m_actionNames.push_back(_("notify login/out"));
	m_actionNames.push_back(_("ignore chat"));
	m_actionNames.push_back(_("ignore pm"));
	m_actionNames.push_back(_("autokick"));
	m_actionNames.push_back(_("notify hosted battle"));
	m_actionNames.push_back(_("notify status change"));

	m_configActionNames.clear();
	m_configActionNames.push_back(_T("none"));
	m_configActionNames.push_back(_T("highlight"));
	m_configActionNames.push_back(_T("notify_login"));
	m_configActionNames.push_back(_T("ignore_chat"));
	m_configActionNames.push_back(_T("ignore_pm"));
	m_configActionNames.push_back(_T("autokick"));
	m_configActionNames.push_back(_T("notify_hosted"));
	m_configActionNames.push_back(_T("notify_status"));

	m_actionTooltips.clear();
	m_actionTooltips.push_back(_("no action at all"));
	m_actionTooltips.push_back(_("highlight user in nick list and battles he participates in"));
	m_actionTooltips.push_back(_("popup a message box when user logs in/out from  the server"));
	m_actionTooltips.push_back(_("you won't see message by these users in normal channels"));
	m_actionTooltips.push_back(_("ignore private messages of these users, no pm window will open if any of these try to contact you privately"));
	m_actionTooltips.push_back(_("automatically kick users from battles hosted by yourself"));
	m_actionTooltips.push_back(_("popup a message box when user hosts a new battle"));
	m_actionTooltips.push_back(_("popup a message box when user changes away status"));

	// setup if empty
	if (!cfg().Exists(_T( "/Groups"))) {
		AddGroup(_("Default"));
		AddGroup(_("Ignore PM"));
		ChangeAction(_("Ignore PM"), UserActions::ActIgnorePM);
		AddGroup(_("Ignore chat"));
		ChangeAction(_("Ignore chat"), UserActions::ActIgnoreChat);
		AddGroup(_("Battle Autokick"));
		ChangeAction(_("Battle Autokick"), UserActions::ActAutokick);
		AddGroup(_("Friends"));
		ChangeAction(_("Friends"), UserActions::ActNotifBattle);
		ChangeAction(_("Friends"), UserActions::ActHighlight);
		ChangeAction(_("Friends"), UserActions::ActNotifLogin);
		// TODO select better color
		SetGroupColor(_("Friends"), wxColour(0, 0, 255));
	}


	// read
	m_groupNames = GetGroups();
	m_groupMap.clear();
	m_groupActions.clear();
	m_actionsGroups.clear();
	m_actionsPeople.clear();
	m_knownUsers.Clear();
	for (unsigned int i = 0; i < m_groupNames.GetCount(); ++i) {
		wxString name = m_groupNames[i];
		m_groupMap[name] = GetPeopleList(name);
		for (unsigned int k = 0; k < m_groupMap[name].GetCount(); ++k) {
			wxString user = m_groupMap[name][k];
			m_knownUsers.Add(user);
			m_peopleGroup[user] = name;
		}
		m_groupActions[name] = GetGroupActions(name);
	}
	for (size_t i = 0; i < m_actionNames.size(); ++i) {
		UserActions::ActionType cur = (UserActions::ActionType)(1 << i);
		wxArrayString tmp;
		for (unsigned int j = 0; j < m_groupNames.GetCount(); ++j) {
			wxString name = m_groupNames[j];
			if ((m_groupActions[name] & cur) != 0) {
				tmp.Add(name);
				for (unsigned int k = 0; k < m_groupMap[name].GetCount(); ++k) {
					m_actionsPeople[cur].Add((m_groupMap[name])[k]);
				}
			}
		}
		tmp.Sort();
		m_actionsGroups[cur] = tmp;
	}
	m_actionsGroups[ActNone] = m_groupNames;
	m_groupNames.Sort();
	m_knownUsers.Sort();
}

void UserActions::UpdateUI()
{
	try {
		ui().mw().GetBattleListTab().UpdateHighlights();
	} catch (...) {
	}

	try {
		ui().mw().GetChatTab().UpdateNicklistHighlights();
	} catch (...) {
	}

	try {
		ui().mw().GetJoinTab().GetBattleRoomTab().UpdateHighlights();
	} catch (...) {
	}
}

wxArrayString UserActions::GetGroupNames() const
{
	return m_groupNames;
}

void UserActions::AddUserToGroup(const wxString& group, const wxString& name)
{
	if (IsKnown(name, false) || ui().IsThisMe(name))
		return;
	m_groupMap[group].Add(name);
	SetPeopleList(m_groupMap[group], group);
	Init();
	UpdateUI();
}

void UserActions::DeleteGroup(const wxString& group)
{
	if (cfg().Exists(_T( "/Groups/" ) + group)) {
		cfg().DeleteGroup(_T( "/Groups/" ) + group);
	}
	Init();
	UpdateUI();
}

wxArrayString UserActions::GetPeopleList(const wxString& group) const
{
	wxArrayString list;
	slConfig::PathGuard pathGuard(&cfg(), _T( "/Groups/" ) + group + _T( "/Members/" ));
	unsigned int friendsCount = cfg().GetNumberOfEntries(false);
	for (unsigned int i = 0; i < friendsCount; i++) {
		wxString ToAdd;
		if (cfg().Read(_T( "/Groups/" ) + group + _T( "/Members/" ) + TowxString(i), &ToAdd))
			list.Add(ToAdd);
	}
	return list;
}


void UserActions::AddGroup(const wxString& group)
{
	if (!cfg().Exists(_T( "/Groups/" ) + group)) {
		//set defaults
		SetGroupActions(group, UserActions::ActNone);
		SetGroupHLColor(defaultHLcolor, group);
	}
	Init();
	UpdateUI();
}


void UserActions::ChangeAction(const wxString& group, const ActionType action, bool add)
{
	ActionType old = m_groupActions[group];
	old = (ActionType)(add ? (old | action) : (old & ~action));
	SetGroupActions(group, old);
	Init();
	UpdateUI();
}

void UserActions::SetGroupActions(const wxString& group, ActionType action) const
{
	wxString key = _T( "/Groups/" ) + group + _T( "/Opts/ActionsList" );
	cfg().DeleteGroup(key);
	key += _T( "/" );
	unsigned int tmp = action & ((UserActions::ActLast << 1) - 1);
	for (auto config : m_configActionNames) {
		if (tmp & 1)
			cfg().Write(key + config, true);
		tmp >>= 1;
	}
}

UserActions::ActionType UserActions::GetGroupActions(const wxString& group) const
{
	wxString key = _T( "/Groups/" ) + group + _T( "/Opts/Actions" );
	if (cfg().HasEntry(key)) // Backward compatibility.
	{
		wxLogMessage(_T( "loading deprecated group actions and updating config" ));
		UserActions::ActionType action = (UserActions::ActionType)cfg().Read(key, (long)UserActions::ActNone);
		cfg().DeleteEntry(key);

		SetGroupActions(group, action);

		return action;
	}
	key = _T( "/Groups/" ) + group + _T( "/Opts/ActionsList" );
	if (!cfg().Exists(key))
		return UserActions::ActNone;
	key += _T( "/" );
	int mask = 1;
	int result = 0;
	for (auto config : m_configActionNames) {
		if (cfg().Read(key + config, 0l)) {
			result |= mask;
		}
		mask <<= 1;
	}
	if (result == 0)
		return UserActions::ActNone;
	return (UserActions::ActionType)result;
}

UserActions::ActionType UserActions::GetGroupAction(const wxString& group) const
{
	const GroupActionMap::const_iterator res = m_groupActions.find(group);
	return res->second;
}

wxString UserActions::GetGroupOfUser(const wxString& user) const
{
	const PeopleGroupMap::const_iterator res = m_peopleGroup.find(user);
	if (res == m_peopleGroup.end()) {
		return wxEmptyString;
	} else {
		return res->second;
	}
}

void UserActions::SetGroupColor(const wxString& group, const wxColour& color)
{
	SetGroupHLColor(color, group);
	Init();
	UpdateUI();
}

wxColour UserActions::GetGroupHLColor(const wxString& group) const
{
	return wxColour(cfg().Read(_T( "/Groups/" ) + group + _T( "/Opts/HLColor" ), _T( "#64648C" )));
}

void UserActions::SetGroupHLColor(const wxColour& color, const wxString& group)
{
	cfg().Write(_T( "/Groups/" ) + group + _T( "/Opts/HLColor" ), color.GetAsString(wxC2S_HTML_SYNTAX));
}

wxArrayString UserActions::GetGroups()
{
	return cfg().GetGroupList(_T( "/Groups/" ));
}


wxColour UserActions::GetGroupColor(const wxString& group) const
{
	return GetGroupHLColor(group);
}

bool UserActions::IsKnown(const wxString& name, bool outputWarning) const
{
	bool ret = m_knownUsers.Index(name) != -1;
	if (outputWarning) {
		customMessageBoxModal(SL_MAIN_ICON, _("To prevent logical inconsistencies, adding a user to more than one group is not allowed"),
				      _("Cannot add user to group"));
	}

	return ret;
}

void UserActions::RemoveUser(const wxString& name)
{
	wxString group = m_peopleGroup[name];
	m_groupMap[group].Remove(name);
	SetPeopleList(m_groupMap[group], group);
	Init();
	UpdateUI();
}

void UserActions::SetPeopleList(const wxArrayString& friends, const wxString& group)
{
	unsigned int friendsCount = friends.GetCount();
	cfg().DeleteGroup(_T( "/Groups/" ) + group + _T( "/Members/" ));
	for (unsigned int i = 0; i < friendsCount; i++) {
		cfg().Write(_T( "/Groups/" ) + group + _T( "/Members/" ) + TowxString(i), friends[i]);
	}
}