1 
2 #include "groupoptionspanel.h"
3 
4 #include <wx/textdlg.h>
5 #include <wx/colour.h>
6 #include <wx/textctrl.h>
7 #include <wx/gdicmn.h>
8 #include <wx/font.h>
9 #include <wx/settings.h>
10 #include <wx/sizer.h>
11 #include <wx/listbox.h>
12 #include <wx/checkbox.h>
13 #include <wx/stattext.h>
14 #include <wx/statbox.h>
15 
16 #include "useractions.h"
17 #include "settings.h"
18 #include "uiutils.h"
19 #include "utils/controls.h"
20 #include "helper/colorbutton.h"
21 #include "selectusersdialog.h"
22 
BEGIN_EVENT_TABLE(GroupOptionsPanel,wxPanel)23 BEGIN_EVENT_TABLE( GroupOptionsPanel, wxPanel )
24 	EVT_BUTTON( REMOVE_GROUP, GroupOptionsPanel::OnRemoveGroup )
25 	EVT_BUTTON( RENAME_GROUP, GroupOptionsPanel::OnRenameGroup )
26 	EVT_BUTTON( ADD_GROUP, GroupOptionsPanel::OnAddNewGroup )
27 	EVT_LISTBOX( GROUPS_LIST, GroupOptionsPanel::OnGroupListSelectionChange )
28 	EVT_CHECKBOX( NOTIFY_LOGIN, GroupOptionsPanel::OnGroupActionsChange )
29 	EVT_CHECKBOX( IGNORE_CHAT, GroupOptionsPanel::OnGroupActionsChange )
30 	EVT_CHECKBOX( NOTIFY_HOST, GroupOptionsPanel::OnGroupActionsChange )
31 	EVT_CHECKBOX( IGNORE_PM, GroupOptionsPanel::OnGroupActionsChange )
32 	EVT_CHECKBOX( NOTIFY_STATUS, GroupOptionsPanel::OnGroupActionsChange )
33 	EVT_CHECKBOX( AUTOCKICK, GroupOptionsPanel::OnGroupActionsChange )
34 	EVT_CHECKBOX( NOTIFY_HIGHLIGHT, GroupOptionsPanel::OnGroupActionsChange )
35 	EVT_BUTTON( HIGHLIGHT_COLOR, GroupOptionsPanel::OnHighlightColorClick )
36 	EVT_LISTBOX( USERS_LIST, GroupOptionsPanel::OnUsersListSelectionChange )
37 	EVT_BUTTON( ADD_USER, GroupOptionsPanel::OnAddUsers )
38 	EVT_BUTTON( REMOVE_USER, GroupOptionsPanel::OnRemoveUser )
39 END_EVENT_TABLE()
40 
41 GroupOptionsPanel::GroupOptionsPanel( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style ) : wxPanel( parent, id, pos, size, style )
42 {
43 	wxBoxSizer* mainSizer;
44 	mainSizer = new wxBoxSizer( wxHORIZONTAL );
45 
46 	wxBoxSizer* groupListSizer;
47 	groupListSizer = new wxBoxSizer( wxVERTICAL );
48 
49 	m_group_list = new wxListBox( this, GROUPS_LIST, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_ALWAYS_SB|wxLB_SINGLE|wxLB_SORT );
50 	groupListSizer->Add( m_group_list, 1, wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 5 );
51 
52 	wxBoxSizer* groupListButtonsSizer;
53 	groupListButtonsSizer = new wxBoxSizer( wxHORIZONTAL );
54 
55 	m_remove_group_button = new wxButton( this, REMOVE_GROUP, _("Remove"), wxDefaultPosition, wxSize( -1,-1 ), wxBU_EXACTFIT );
56 	m_remove_group_button->Enable( false );
57 	m_remove_group_button->SetToolTip( _("Remove an existing group") );
58 
59 	groupListButtonsSizer->Add( m_remove_group_button, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
60 
61 	m_rename_group_button = new wxButton( this, RENAME_GROUP, _("Rename.."), wxDefaultPosition, wxSize( -1,-1 ), wxBU_EXACTFIT );
62 	m_rename_group_button->Enable( false );
63 	m_rename_group_button->SetToolTip( _("Rename an existing group") );
64 
65 	groupListButtonsSizer->Add( m_rename_group_button, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
66 
67 
68 	groupListButtonsSizer->Add( 0, 0, 1, wxEXPAND, 5 );
69 
70 	m_add_group_button = new wxButton( this, ADD_GROUP, _("Add New.."), wxDefaultPosition, wxSize( -1,-1 ), wxBU_EXACTFIT );
71 	m_add_group_button->SetToolTip( _("Add new group") );
72 
73 	groupListButtonsSizer->Add( m_add_group_button, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
74 
75 	groupListSizer->Add( groupListButtonsSizer, 0, wxEXPAND|wxBOTTOM, 5 );
76 
77 	mainSizer->Add( groupListSizer, 2, wxEXPAND, 5 );
78 
79 	m_group_panel = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
80 	wxBoxSizer* mainGroupPanelSizer;
81 	mainGroupPanelSizer = new wxBoxSizer( wxVERTICAL );
82 
83 	wxStaticBoxSizer* actionsSizer;
84 	actionsSizer = new wxStaticBoxSizer( new wxStaticBox( m_group_panel, wxID_ANY, _("Group Actions") ), wxVERTICAL );
85 
86 	wxGridSizer* actionCheckSizer;
87 	actionCheckSizer = new wxGridSizer( 2, 2, 0, 0 );
88 
89 	m_login_notify_check = new wxCheckBox( m_group_panel, NOTIFY_LOGIN, _("Notify login/logout"), wxDefaultPosition, wxDefaultSize, 0 );
90 
91 	m_login_notify_check->SetToolTip( _("Notify when users of this group go online or offline") );
92 
93 	actionCheckSizer->Add( m_login_notify_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
94 
95 	m_ignore_chat_check = new wxCheckBox( m_group_panel, IGNORE_CHAT, _("Ignore Chat"), wxDefaultPosition, wxDefaultSize, 0 );
96 
97 	m_ignore_chat_check->SetToolTip( _("Ignore anything said in channel by any of the users in this group") );
98 
99 	actionCheckSizer->Add( m_ignore_chat_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
100 
101 	m_notify_host_check = new wxCheckBox( m_group_panel, NOTIFY_HOST, _("Notify Hosted Battles"), wxDefaultPosition, wxDefaultSize, 0 );
102 
103 	m_notify_host_check->SetToolTip( _("Notify when users of this group hosts a battle") );
104 
105 	actionCheckSizer->Add( m_notify_host_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
106 
107 	m_ignore_pm_check = new wxCheckBox( m_group_panel, IGNORE_PM, _("Ignore PM"), wxDefaultPosition, wxDefaultSize, 0 );
108 
109 	m_ignore_pm_check->SetToolTip( _("Ignore anything said in private chat by any of the users in this group") );
110 
111 	actionCheckSizer->Add( m_ignore_pm_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
112 
113 	m_notify_status_check = new wxCheckBox( m_group_panel, NOTIFY_STATUS, _("Notify Status Change"), wxDefaultPosition, wxDefaultSize, 0 );
114 
115 	m_notify_status_check->SetToolTip( _("Notify when the status of a users in this group changes") );
116 
117 	actionCheckSizer->Add( m_notify_status_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
118 
119 	m_autokick_check = new wxCheckBox( m_group_panel, AUTOCKICK, _("Autokick"), wxDefaultPosition, wxDefaultSize, 0 );
120 
121 	m_autokick_check->SetToolTip( _("Auto kick any of the users in this group from battles hosted") );
122 
123 	actionCheckSizer->Add( m_autokick_check, 0, wxALL, 5 );
124 
125 	m_highlight_check = new wxCheckBox( m_group_panel, NOTIFY_HIGHLIGHT, _("Highlight"), wxDefaultPosition, wxDefaultSize, 0 );
126 
127 	m_highlight_check->SetToolTip( _("Highlight battles and the names of users in this group") );
128 
129 	actionCheckSizer->Add( m_highlight_check, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
130 
131 	wxBoxSizer* colorSizer;
132 	colorSizer = new wxBoxSizer( wxHORIZONTAL );
133 
134 	m_highlight_colorstaticText = new wxStaticText( m_group_panel, wxID_ANY, _("Highlight Color"), wxDefaultPosition, wxDefaultSize, 0 );
135 	m_highlight_colorstaticText->Wrap( -1 );
136 	colorSizer->Add( m_highlight_colorstaticText, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
137 
138 	m_highlight_color_button = new ColorButton( m_group_panel, HIGHLIGHT_COLOR, wxColour(0,0,0), wxDefaultPosition, wxSize( 20,20 ) );
139 	m_highlight_color_button->SetToolTip( _("Select highlight color") );
140 
141 	m_highlight_color_button->SetToolTip( _("Select highlight color") );
142 
143 	colorSizer->Add( m_highlight_color_button, 0, wxALL, 5 );
144 
145 	actionCheckSizer->Add( colorSizer, 1, wxEXPAND, 5 );
146 
147 	actionsSizer->Add( actionCheckSizer, 1, wxEXPAND, 5 );
148 
149 	mainGroupPanelSizer->Add( actionsSizer, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
150 
151 	wxStaticBoxSizer* usersSizer;
152 	usersSizer = new wxStaticBoxSizer( new wxStaticBox( m_group_panel, wxID_ANY, _("Users in group") ), wxVERTICAL );
153 
154 	wxBoxSizer* userListSizer;
155 	userListSizer = new wxBoxSizer( wxHORIZONTAL );
156 
157 	m_user_list = new wxListBox( m_group_panel, USERS_LIST, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_MULTIPLE );
158 	userListSizer->Add( m_user_list, 1, wxALL|wxEXPAND, 5 );
159 
160 	wxBoxSizer* userButtonSizer;
161 	userButtonSizer = new wxBoxSizer( wxVERTICAL );
162 
163 	m_add_user_button = new wxButton( m_group_panel, ADD_USER, _("Add.."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
164 	m_add_user_button->SetToolTip( _("Add users to group") );
165 
166 	userButtonSizer->Add( m_add_user_button, 0, wxEXPAND|wxTOP|wxRIGHT, 5 );
167 
168 	m_remove_user_button = new wxButton( m_group_panel, REMOVE_USER, _("Remove"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
169 	m_remove_user_button->Enable( false );
170 	m_remove_user_button->SetToolTip( _("Remove users from group") );
171 
172 	userButtonSizer->Add( m_remove_user_button, 0, wxEXPAND|wxTOP|wxRIGHT, 5 );
173 
174 	userListSizer->Add( userButtonSizer, 0, wxEXPAND, 5 );
175 
176 	usersSizer->Add( userListSizer, 1, wxEXPAND, 5 );
177 
178 	mainGroupPanelSizer->Add( usersSizer, 1, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
179 
180 	m_group_panel->SetSizer( mainGroupPanelSizer );
181 	m_group_panel->Layout();
182 	mainGroupPanelSizer->Fit( m_group_panel );
183 	mainSizer->Add( m_group_panel, 3, wxALL|wxEXPAND, 5 );
184 
185 	this->SetSizer( mainSizer );
186 	this->Layout();
187 
188 	Initialize();
189 }
190 
Initialize()191 void GroupOptionsPanel::Initialize()
192 {
193   m_user_dialog = 0;
194   ReloadGroupsList();
195   ShowGroup( wxEmptyString );
196 }
197 
~GroupOptionsPanel()198 GroupOptionsPanel::~GroupOptionsPanel()
199 {
200 }
201 
ShowGroup(const wxString & group)202 void GroupOptionsPanel::ShowGroup( const wxString& group )
203 {
204   if ( group == wxEmptyString ) m_current_group = GetFirstGroupName();
205   else m_current_group = group;
206 
207   m_group_list->SetStringSelection(m_current_group);
208 
209   UserActions::ActionType act = useractions().GetGroupAction( m_current_group );
210 
211   m_login_notify_check->SetValue( (act & UserActions::ActNotifLogin) != 0 );
212   m_ignore_chat_check->SetValue( (act & UserActions::ActIgnoreChat) != 0 );
213   m_notify_host_check->SetValue( (act & UserActions::ActNotifBattle) != 0 );
214   m_ignore_pm_check->SetValue( (act & UserActions::ActIgnorePM) != 0 );
215   m_notify_status_check->SetValue( (act & UserActions::ActNotifStatus) != 0 );
216   m_autokick_check->SetValue( (act & UserActions::ActAutokick) != 0 );
217   m_highlight_check->SetValue( (act & UserActions::ActHighlight) != 0 );
218 
219   m_highlight_color_button->SetColor( useractions().GetGroupColor(m_current_group) );
220 
221   ReloadUsersList();
222   m_remove_group_button->Enable( m_group_list->GetStringSelection() != wxEmptyString );
223 }
224 
ReloadUsersList()225 void GroupOptionsPanel::ReloadUsersList()
226 {
227     wxArrayString groupuser = useractions().GetPeopleList( m_current_group );
228     m_user_list->Clear();
229     m_user_list->InsertItems(groupuser, 0);
230     m_remove_user_button->Enable( false );
231 }
232 
ReloadGroupsList()233 void GroupOptionsPanel::ReloadGroupsList()
234 {
235   wxArrayString groupnames = useractions().GetGroupNames();
236   m_group_list->Clear();
237   m_group_list->InsertItems(groupnames, 0);
238   m_group_list->SetStringSelection(m_current_group);
239 }
240 
GetFirstGroupName()241 wxString GroupOptionsPanel::GetFirstGroupName()
242 {
243   wxSortedArrayString groupnames = useractions().GetGroupNames();
244   if (groupnames.Count() <= 0) return wxEmptyString;
245   return groupnames[0];
246 }
247 
OnRemoveGroup(wxCommandEvent &)248 void GroupOptionsPanel::OnRemoveGroup( wxCommandEvent& /*unused*/ )
249 {
250   if (m_current_group == _T("Default")) return;
251   useractions().DeleteGroup( m_current_group );
252   ReloadGroupsList();
253   ShowGroup(wxEmptyString);
254 }
255 
256 
OnRenameGroup(wxCommandEvent &)257 void GroupOptionsPanel::OnRenameGroup( wxCommandEvent& /*unused*/ )
258 {
259 }
260 
261 
OnAddNewGroup(wxCommandEvent &)262 void GroupOptionsPanel::OnAddNewGroup( wxCommandEvent& /*unused*/ )
263 {
264   wxTextEntryDialog* ted = new wxTextEntryDialog(this, _("Name of new group:"), _("Add New Group"));
265   if ( ted->ShowModal() == wxID_OK ) {
266     wxString newgroup = ted->GetValue();
267     //!TODO: Check if group exists already.
268     if ( newgroup != wxEmptyString ) useractions().AddGroup( newgroup );
269     ReloadGroupsList();
270     ShowGroup(newgroup);
271   }
272   delete ted;
273 }
274 
275 
OnGroupListSelectionChange(wxCommandEvent &)276 void GroupOptionsPanel::OnGroupListSelectionChange( wxCommandEvent& /*unused*/ )
277 {
278   wxString newgroup = m_group_list->GetStringSelection();
279   wxSortedArrayString groupnames = useractions().GetGroupNames();
280   if ( groupnames.Index(newgroup) == wxNOT_FOUND ) {
281     return;
282   }
283   ShowGroup(newgroup);
284 }
285 
286 
OnGroupActionsChange(wxCommandEvent &)287 void GroupOptionsPanel::OnGroupActionsChange( wxCommandEvent&  )
288 {
289   useractions().ChangeAction( m_current_group, UserActions::ActNotifLogin, m_login_notify_check->GetValue() );
290   useractions().ChangeAction( m_current_group, UserActions::ActIgnoreChat, m_ignore_chat_check->GetValue() );
291   useractions().ChangeAction( m_current_group, UserActions::ActNotifBattle, m_notify_host_check->GetValue() );
292   useractions().ChangeAction( m_current_group, UserActions::ActIgnorePM, m_ignore_pm_check->GetValue() );
293   useractions().ChangeAction( m_current_group, UserActions::ActNotifStatus, m_notify_status_check->GetValue() );
294   useractions().ChangeAction( m_current_group, UserActions::ActAutokick, m_autokick_check->GetValue() );
295   useractions().ChangeAction( m_current_group, UserActions::ActHighlight, m_highlight_check->GetValue() );
296 }
297 
298 
OnHighlightColorClick(wxCommandEvent & event)299 void GroupOptionsPanel::OnHighlightColorClick( wxCommandEvent& event )
300 {
301     ColorButton* origin = (ColorButton*) event.GetEventObject();
302     wxColour c = GetColourFromUser( this, origin->GetColor(), m_current_group );
303     if ( c.IsOk() )
304     {
305         origin->SetColor( c );
306         useractions().SetGroupColor( m_current_group, c );
307     }
308 }
309 
310 
OnUsersListSelectionChange(wxCommandEvent &)311 void GroupOptionsPanel::OnUsersListSelectionChange( wxCommandEvent& /*unused*/ )
312 {
313   wxArrayInt sel;
314   m_user_list->GetSelections( sel );
315   m_remove_user_button->Enable( sel.Count() > 0 );
316 }
317 
318 
OnAddUsers(wxCommandEvent &)319 void GroupOptionsPanel::OnAddUsers( wxCommandEvent& /*unused*/ )
320 {
321   wxSortedArrayString users = SelectUsersDialog::GetUsers(this);
322   for ( unsigned int i = 0; i < users.Count(); i++ ) {
323     useractions().AddUserToGroup( m_current_group, users[i] );
324   }
325   if ( users.Count() > 0 ) ReloadUsersList();
326 }
327 
328 
OnRemoveUser(wxCommandEvent &)329 void GroupOptionsPanel::OnRemoveUser( wxCommandEvent& /*unused*/ )
330 {
331   wxArrayInt sel;
332   int num = m_user_list->GetSelections( sel );
333   for ( int i = 0; i < num; i++ ) {
334     wxString name = m_user_list->GetString(sel[i]);
335     useractions().RemoveUser( name );
336   }
337   ReloadUsersList();
338 }
339 
340 
Update()341 void GroupOptionsPanel::Update()
342 {
343   ReloadGroupsList();
344   ReloadUsersList();
345 }
346