/
ui.cpp
1125 lines (960 loc) · 33.5 KB
/
ui.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of the Springlobby (GPL v2 or later), see COPYING */
//
// Class: Ui
//
#ifdef _MSC_VER
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <winsock2.h>
#endif // _MSC_VER
#include <wx/textdlg.h>
#include <wx/intl.h>
#include <wx/utils.h>
#include <wx/debugrpt.h>
#include <wx/filename.h>
#include <wx/app.h>
#include <wx/log.h>
#include <wx/stdpaths.h>
#include <stdexcept>
#include <lslutils/globalsmanager.h>
#include <lslunitsync/springbundle.h>
#include <lslunitsync/unitsync.h>
#include "ui.h"
#include "tasserver.h"
#include "helper/slconfig.h"
#include "iserver.h"
#include "serverselector.h"
#include "spring.h"
#include "channel/channel.h"
#include "connectwindow.h"
#include "mainwindow.h"
#include "user.h"
#include "utils/conversion.h"
#include "utils/uievents.h"
#include "utils/slpaths.h"
#include "utils/version.h"
#include "uiutils.h"
#include "chatpanel.h"
#include "battlelist/battlelisttab.h"
#include "hosting/battleroomtab.h"
#include "hosting/mainjoinbattletab.h"
#include "hosting/mainsingleplayertab.h"
#include "mainchattab.h"
#include "crashreport.h"
#include "maindownloadtab.h"
#include "downloader/prdownloader.h"
#include "downloader/httpdownloader.h"
#include "agreementdialog.h"
#include "updater/updatehelper.h"
#include "utils/customdialogs.h"
#include "utils/platform.h"
#include "updater/versionchecker.h"
#include "textentrydialog.h"
#include "log.h"
#include "settings.h"
#ifndef DISABLE_SOUND
#include "sound/alsound.h"
#endif
SLCONFIG("/General/AutoUpdate", true, "Determines if springlobby should check for updates on startup");
SLCONFIG("/General/LastUpdateCheck", 0L, "Last time springlobby checked for an update");
SLCONFIG("/GUI/StartTab", (long)MainWindow::PAGE_SINGLE, "which tab to show on startup");
SLCONFIG("/Chat/BroadcastEverywhere",true, "setting to spam the server messages in all channels");
SLCONFIG("/Server/Autoconnect", false, "Connect to server on startup");
static const unsigned int s_reconnect_delay_ms = 6000;
Ui& ui()
{
assert(wxThread::IsMain());
static LSL::Util::LineInfo<Ui> m( AT );
static LSL::Util::GlobalObjectHolder<Ui, LSL::Util::LineInfo<Ui> > m_ui( m );
return m_ui;
}
Ui::Ui() :
m_http_thread(NULL),
m_serv(0),
m_main_win(0),
m_con_win(0),
m_first_update_trigger(true),
m_battle_info_updatedSink( this, &BattleEvents::GetBattleEventSender( ( BattleEvents::BattleInfoUpdate ) ) )
{
m_main_win = new MainWindow( );
CustomMessageBoxBase::setLobbypointer(m_main_win);
m_serv = new TASServer();
serverSelector().SetCurrentServer( m_serv );
ConnectGlobalEvent(this, GlobalEvent::OnSpringTerminated, wxObjectEventFunction(&Ui::OnSpringTerminated));
ConnectGlobalEvent(this, GlobalEvent::OnQuit, wxObjectEventFunction(&Ui::OnQuit));
}
Ui::~Ui()
{
delete m_serv;
m_serv = NULL;
if (m_http_thread != NULL) {
m_http_thread->Wait();
m_http_thread = NULL;
}
}
ChatPanel* Ui::GetActiveChatPanel()
{
return mw().GetActiveChatPanel();
}
MainWindow& Ui::mw()
{
assert(wxThread::IsMain());
ASSERT_LOGIC( m_main_win != 0, _T("m_main_win = 0") );
return *m_main_win;
}
const MainWindow& Ui::mw() const
{
ASSERT_LOGIC( m_main_win != 0, _T("m_main_win = 0") );
return *m_main_win;
}
bool Ui::IsMainWindowCreated() const
{
if ( m_main_win == 0 ) return false;
else return true;
}
//! @brief Shows the main window on screen
void Ui::ShowMainWindow()
{
ASSERT_LOGIC( m_main_win != 0, _T("m_main_win = 0") );
mw().Show(true);
}
//! @brief Show the connect window on screen
//!
//! @note It will create the ConnectWindow if not allready created
void Ui::ShowConnectWindow()
{
if (IsConnected())
return;
if ( m_con_win == 0 ) {
ASSERT_LOGIC( m_main_win != 0, _T("m_main_win = 0") );
m_con_win = new ConnectWindow( m_main_win, *this );
}
m_con_win->CenterOnParent();
m_con_win->Show(true);
m_con_win->Raise();
}
//! @brief Connects to default server or opens the ConnectWindow
//!
//! @todo Fix Auto Connect
//! @see DoConnect
void Ui::Connect()
{
const wxString server_name = sett().GetDefaultServer();
const wxString nick = sett().GetServerAccountNick( server_name );
const wxString pass = sett().GetServerAccountPass( server_name );
bool autoconnect = cfg().ReadBool(_T( "/Server/Autoconnect" ));
if ( !autoconnect || server_name.IsEmpty() || nick.IsEmpty() || pass.IsEmpty() ) {
ShowConnectWindow();
return;
}
m_con_win = 0;
DoConnect( server_name, nick, pass);
}
void Ui::Reconnect()
{
wxString servname = sett().GetDefaultServer();
wxString pass = sett().GetServerAccountPass(servname);
if ( !sett().GetServerAccountSavePass(servname) ) {
ShowConnectWindow();
return;
}
Disconnect();
DoConnect( servname, sett().GetServerAccountNick(servname), pass );
}
void Ui::Disconnect()
{
if ( m_serv != 0 ) {
if ( IsConnected() ) {
m_serv->Disconnect();
}
}
}
//! @brief Opens the accutial connection to a server.
void Ui::DoConnect( const wxString& servername, const wxString& username, const wxString& password )
{
wxString host;
int port;
if ( servername != m_last_used_backup_server ) { // do not save the server as default if it's a backup one
sett().SetDefaultServer( servername );
} else {
m_last_used_backup_server = wxEmptyString;
}
if ( !sett().ServerExists( servername ) ) {
ASSERT_LOGIC( false, _T("Server does not exist in settings") );
return;
}
Disconnect();
m_serv->SetUsername( username );
m_serv->SetPassword( password );
if ( sett().GetServerAccountSavePass( servername ) ) {
if ( m_serv->IsPasswordHash(password) ) sett().SetServerAccountPass( servername, password );
else sett().SetServerAccountPass( servername, m_serv->GetPasswordHash( password ) );
} else {
sett().SetServerAccountPass( servername, wxEmptyString );
}
host = sett().GetServerHost( servername );
port = sett().GetServerPort( servername );
AddServerWindow( servername );
m_serv->uidata.panel->StatusMessage( _T("Connecting to server ") + servername + _T("...") );
// Connect
m_serv->Connect( servername, host, port );
}
void Ui::AddServerWindow( const wxString& servername )
{
if ( !m_serv->uidata.panel ) {
m_serv->uidata.panel = m_main_win->GetChatTab().AddChatPanel( *m_serv, servername );
}
}
void Ui::ReopenServerTab()
{
if ( m_serv->IsOnline() ) {
AddServerWindow( m_serv->GetServerName() );
// re-add all users to the user list
const UserList& list = m_serv->GetUserList();
for ( unsigned int i = 0; i < list.GetNumUsers(); i++ ) {
m_serv->uidata.panel->OnChannelJoin( list.GetUser(i) );
}
}
}
bool Ui::DoRegister( const wxString& servername, const wxString& username, const wxString& password,wxString& reason)
{
wxString host;
int port;
if ( !sett().ServerExists( servername ) ) {
ASSERT_LOGIC( false, _T("Server does not exist in settings") );
return false;
}
host = sett().GetServerHost( servername );
port = sett().GetServerPort( servername );
bool success = m_serv->Register( host, port, username, password,reason );
if ( success ) {
customMessageBox(SL_MAIN_ICON, _("Registration successful,\nyou should now be able to login."), _("Registration successful"), wxOK );
} else {
wxLogWarning( _T("registration failed, reason: %s"), reason.c_str() );
if ( reason == _("Connection timed out") || reason.IsEmpty() ) {
ShowConnectWindow();
} else {
customMessageBox(SL_MAIN_ICON,_("Registration failed, the reason was:\n")+ reason , _("Registration failed."), wxOK );
}
}
return success;
}
bool Ui::IsConnected() const
{
if ( m_serv == 0 ) return false;
return m_serv->IsConnected();
}
void Ui::JoinChannel( const wxString& name, const wxString& password )
{
if ( m_serv != 0 ) m_serv->JoinChannel( name, password );
}
bool Ui::IsSpringRunning() const
{
return spring().IsRunning();
}
//! @brief Quits the entire application
void Ui::Quit()
{
Disconnect();
if ( m_con_win != 0 )
m_con_win->Close();
}
void Ui::Download( const std::string& category, const std::string& name, const std::string& /*hash */)
{
int count = prDownloader().GetDownload(category, name);
assert( count > 0 );
}
//! @brief Display a dialog asking a question with OK and Canel buttons
//!
//! @return true if OK button was pressed
//! @note this does not return until the user pressed any of the buttons or closed the dialog.
bool Ui::Ask( const wxString& heading, const wxString& question ) const
{
int answer = customMessageBox( SL_MAIN_ICON, question, heading, wxYES_NO );
return ( answer == wxYES );
}
//! cannot be const because parent window cannot be const
bool Ui::AskPassword( const wxString& heading, const wxString& message, wxString& password )
{
wxPasswordEntryDialog pw_dlg( &mw(), message, heading, password );
int res = pw_dlg.ShowModal();
password = pw_dlg.GetValue();
return ( res == wxID_OK );
}
bool Ui::AskText( const wxString& heading, const wxString& question, wxString& answer, bool multiline )
{
TextEntryDialog name_dlg( &mw(), question, heading, answer, multiline );
int res = name_dlg.ShowModal();
answer = name_dlg.GetValue();
return ( res == wxID_OK );
}
void Ui::ShowMessage( const wxString& heading, const wxString& message ) const
{
if ( m_main_win == 0 ) return;
serverMessageBox( SL_MAIN_ICON, message, heading, wxOK);
}
bool Ui::ExecuteSayCommand( const wxString& cmd )
{
if ( !IsConnected() ) return false;
if ( (cmd.BeforeFirst(' ').Lower() == _T("/join")) || (cmd.BeforeFirst(' ').Lower() == _T("/j")) ) {
wxString channel = cmd.AfterFirst(' ');
wxString pass = channel.AfterFirst(' ');
if ( !pass.IsEmpty() ) channel = channel.BeforeFirst(' ');
if ( channel.StartsWith(_T("#")) ) channel.Remove( 0, 1 );
m_serv->JoinChannel( channel, pass );
return true;
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/away") ) {
m_serv->GetMe().Status().away = true;
m_serv->GetMe().SendMyUserStatus();
mw().GetJoinTab().GetBattleRoomTab().UpdateMyInfo();
return true;
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/back") ) {
if ( IsConnected() ) {
m_serv->GetMe().Status().away = false;
m_serv->GetMe().SendMyUserStatus();
mw().GetJoinTab().GetBattleRoomTab().UpdateMyInfo();
return true;
}
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/ingame") ) {
wxString nick = cmd.AfterFirst(' ');
m_serv->RequestInGameTime( nick );
return true;
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/help") ) {
wxString topic = cmd.AfterFirst(' ');
ConsoleHelp( topic.Lower() );
return true;
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/msg") ) {
wxString user = cmd.AfterFirst(' ').BeforeFirst(' ');
wxString msg = cmd.AfterFirst(' ').AfterFirst(' ');
m_serv->SayPrivate( user, msg );
return true;
} else if ( cmd.BeforeFirst(' ').Lower() == _T("/channels") ) {
mw().ShowChannelChooser();
return true;
}
return false;
}
void Ui::ConsoleHelp( const wxString& topic )
{
ChatPanel* panel = GetActiveChatPanel();
if ( panel == 0 ) {
ShowMessage( _("Help error"), _("Type /help in a chat box. (A bug currently prevents this from working in battleroom") );
return;
}
if ( topic == wxEmptyString ) {
panel->ClientMessage( IdentityString( _("%s commands help.") ) );
panel->ClientMessage( wxEmptyString );
panel->ClientMessage( _("Global commands:") );
panel->ClientMessage( _(" \"/away\" - Sets your status to away.") );
panel->ClientMessage( _(" \"/back\" - Resets your away status.") );
panel->ClientMessage( _(" \"/changepassword2 newpassword\" - Changes the current active account's password, needs the old password saved in login box") );
panel->ClientMessage( _(" \"/changepassword oldpassword newpassword\" - Changes the current active account's password, password cannot contain spaces") );
panel->ClientMessage( _(" \"/channels\" - Lists currently active channels.") );
panel->ClientMessage( _(" \"/help [topic]\" - Put topic if you want to know more specific information about a command.") );
panel->ClientMessage( _(" \"/join channel [password]\" - Joins a channel.") );
panel->ClientMessage( _(" \"/j\" - Alias to /join.") );
panel->ClientMessage( _(" \"/ingame\" - Shows how much time you have in game.") );
panel->ClientMessage( _(" \"/msg username [text]\" - Sends a private message containing text to username.") );
panel->ClientMessage( _(" \"/part\" - Leaves current channel.") );
panel->ClientMessage( _(" \"/p\" - Alias to /part.") );
panel->ClientMessage( _(" \"/rename newalias\" - Changes your nickname to newalias.") );
panel->ClientMessage( IdentityString( _(" \"/sayver\" - Says what version of %s you have in chat.") ) );
panel->ClientMessage( _(" \"/testmd5 text\" - Returns md5-b64 hash of given text.") );
panel->ClientMessage( IdentityString( _(" \"/ver\" - Displays what version of %s you have.") ) );
panel->ClientMessage( _(" \"/clear\" - Clears all text from current chat panel") );
panel->ClientMessage( wxEmptyString );
panel->ClientMessage( _("Chat commands:") );
panel->ClientMessage( _(" \"/me action\" - Say IRC style action message.") );
panel->ClientMessage( wxEmptyString );
panel->ClientMessage( _("If you are missing any commands, go to #springlobby and try to type it there :)") );
// panel->ClientMessage( _(" \"/\" - .") );
} else if ( topic == _T("topics") ) {
panel->ClientMessage( _("No topics written yet.") );
} else {
panel->ClientMessage( _("The topic \"") + topic + _("\" was not found. Type \"/help topics\" only for available topics.") );
}
}
ChatPanel* Ui::GetChannelChatPanel( const wxString& channel )
{
return mw().GetChannelChatPanel( channel );
}
////////////////////////////////////////////////////////////////////////////////////////////
// EVENTS
////////////////////////////////////////////////////////////////////////////////////////////
//! @brief Called when connected to a server
//!
//! @todo Display in servertab
void Ui::OnConnected( IServer& server, const wxString& server_name, const wxString& /*unused*/, bool /*supported*/ )
{
slLogDebugFunc("");
m_connect_retries = 10;
if ( server.uidata.panel ) server.uidata.panel->StatusMessage( _T("Connected to ") + server_name + _T(".") );
mw().GetBattleListTab().OnConnected();
delete m_con_win;
m_con_win = 0;
}
bool Ui::IsSpringCompatible(const std::string& engine, const std::string& version)
{
assert(engine == "spring");
if ( sett().GetDisableSpringVersionCheck() ) return true;
const std::string ver = SlPaths::GetCompatibleVersion(version);
if (!ver.empty()) {
if ( SlPaths::GetCurrentUsedSpringIndex() != ver ) {
wxLogMessage(_T("server enforce usage of version: %s, switching to profile: %s"), TowxString(ver).c_str(), TowxString(ver).c_str());
SlPaths::SetUsedSpringIndex( ver );
LSL::usync().ReloadUnitSyncLib();
}
return true;
}
return false; // no compatible version found
}
bool Ui::DownloadArchives(const IBattle& battle)
{
const std::string engineVersion = battle.GetBattleOptions().engineVersion;
const std::string engineName = battle.GetBattleOptions().engineName;
if ( !IsSpringCompatible(engineName, engineVersion)) {
wxLogWarning( _T( "trying to join battles with incompatible spring version" ) );
if ( wxYES == customMessageBox( SL_MAIN_ICON,
wxFormat(_("The selected preset requires the engine '%s' version '%s'. Should it be downloaded?")) % engineName % engineVersion,
_("Engine missing"),
wxYES_NO ) ) {
Download(PrDownloader::GetEngineCat(), engineVersion, "");
return true;
}
}
if (battle.MapExists() && battle.ModExists()) {
return true;
}
wxString prompt = wxEmptyString;
if (!battle.ModExists() ) {
prompt += wxFormat(_("the game '%s'")) % battle.GetHostModName();
}
if (!battle.MapExists() ) {
if (!prompt.empty()) {
prompt+= _(" and ");
}
prompt += wxFormat(_("the map '%s'")) % battle.GetHostMapName();
}
if (prDownloader().IsRunning()) {
return true;
}
if ( customMessageBox( SL_MAIN_ICON, wxFormat(_( "You need to download %s to be able to play.\n\n Shall I download it?" )) % prompt,
_( "Content needed to be downloaded" ), wxYES_NO | wxICON_QUESTION ) == wxYES ) {
if (!battle.MapExists()) {
ui().Download("map", battle.GetHostMapName(), battle.GetHostMapHash());
}
if (!battle.ModExists()) {
ui().Download("game", battle.GetHostModName(), battle.GetHostModHash());
}
return true;
}
return false;
}
void Ui::OnLoggedIn( )
{
if ( m_main_win == 0 ) return;
mw().GetChatTab().RejoinChannels();
mw().GetBattleListTab().SortBattleList();
}
void Ui::OnDisconnected( IServer& server, bool wasonline )
{
Start( s_reconnect_delay_ms, true );
if ( m_main_win == 0 ) return;
slLogDebugFunc("");
if (!&server) {
wxLogError(_T("WTF got null reference!!!"));
return;
}
mw().GetJoinTab().LeaveCurrentBattle();
mw().GetBattleListTab().RemoveAllBattles();
mw().GetChatTab().LeaveChannels();
wxString disconnect_msg = wxFormat(_("disconnected from server: %s") ) % server.GetServerName();
UiEvents::GetStatusEventSender( UiEvents::addStatusMessage ).SendEvent(UiEvents::StatusData( disconnect_msg, 1 ) );
if ( !wxTheApp->IsActive() ) {
UiEvents::GetNotificationEventSender().SendEvent(UiEvents::NotficationData( UiEvents::ServerConnection, disconnect_msg ) );
}
if ( server.uidata.panel ) {
server.uidata.panel->StatusMessage( disconnect_msg );
server.uidata.panel->SetServer( 0 );
}
if ((!wasonline) && (m_connect_retries <= 0)) {// couldn't even estabilish a socket, prompt the user to switch to another server
ShowConnectWindow();
}
m_connect_retries--;
}
wxString Ui::GetNextServer()
{
wxString previous_server = m_last_used_backup_server;
if ( previous_server.IsEmpty() ) previous_server = sett().GetDefaultServer();
wxArrayString serverlist = sett().GetServers();
int position = serverlist.Index( previous_server );
if ( position == wxNOT_FOUND ) position = -1;
position = ( position + 1) % serverlist.GetCount(); // switch to next in the list
return serverlist[position];
}
//! @brief Called when client has joined a channel
//!
//! @todo Check if a pannel allready exists for this channel
void Ui::OnJoinedChannelSuccessful( Channel& chan, bool doFocus)
{
slLogDebugFunc("");
if ( m_main_win == NULL ) {
return;
}
mw().OpenChannelChat( chan, doFocus );
}
void Ui::OnChannelMessage(Channel& chan, const wxString& msg )
{
ChatPanel* panel = GetChannelChatPanel( TowxString(chan.GetName()) );
if ( panel != 0 ) {
panel->StatusMessage( msg );
}
}
void Ui::OnChannelList( const wxString& channel, const int& numusers )
{
ChatPanel* panel = GetActiveChatPanel();
if ( panel == 0 ) {
ShowMessage( _("error"), _("no active chat panels open.") );
return;
}
panel->StatusMessage( wxFormat( _("%s (%d users)") ) % channel % numusers );
}
void Ui::OnUserOnline( User& user )
{
if ( m_main_win == 0 ) return;
mw().GetChatTab().OnUserConnected( user );
}
void Ui::OnUserOffline( User& user )
{
if ( m_main_win == 0 ) return;
mw().GetChatTab().OnUserDisconnected( user );
if ( user.uidata.panel ) {
user.uidata.panel->SetUser( 0 );
user.uidata.panel = 0;
}
}
void Ui::OnUserStatusChanged( User& user )
{
if ( m_main_win == 0 ) return;
for ( int i = 0; i < m_serv->GetNumChannels(); i++ ) {
Channel& chan = m_serv->GetChannel( i );
if ( ( chan.UserExists(user.GetNick()) ) && ( chan.uidata.panel != 0 ) ) {
chan.uidata.panel->UserStatusUpdated( user );
}
}
if ( user.uidata.panel ) {
user.uidata.panel->UserStatusUpdated( user );
}
if ( user.GetStatus().in_game ) mw().GetBattleListTab().UserUpdate( user );
try {
ChatPanel& server = mw().GetChatTab().ServerChat();
server.UserStatusUpdated( user );
} catch(...) {}
}
void Ui::OnUnknownCommand( IServer& server, const wxString& command, const wxString& params )
{
if ( server.uidata.panel != 0 ) server.uidata.panel->UnknownCommand( command, params );
}
void Ui::OnMotd( IServer& server, const wxString& message )
{
if ( server.uidata.panel != 0 ) server.uidata.panel->Motd( message );
}
void Ui::OnServerBroadcast( IServer& /*server*/, const wxString& message )
{
if ( m_main_win == 0 ) return;
mw().GetChatTab().BroadcastMessage( message );
try { // send it to battleroom too
mw().GetJoinTab().GetBattleRoomTab().GetChatPanel().StatusMessage(message);
} catch(...) {}
}
void Ui::OnServerMessage( IServer& server, const wxString& message )
{
if ( !cfg().ReadBool(_T("/Chat/BroadcastEverywhere")) ) {
if ( server.uidata.panel != 0 ) server.uidata.panel->StatusMessage( message );
} else {
if ( server.uidata.panel != 0 ) server.uidata.panel->StatusMessage( message );
if ( m_main_win == 0 ) return;
ChatPanel* activepanel = mw().GetChatTab().GetActiveChatPanel();
if ( activepanel != 0 ) {
if (activepanel != server.uidata.panel) activepanel->StatusMessage(message); // don't send it twice to the server tab
}
try { // send it to battleroom too
mw().GetJoinTab().GetBattleRoomTab().GetChatPanel().StatusMessage(message);
} catch(...) {}
}
}
void Ui::OnUserSaid( User& user, const wxString& message, bool fromme )
{
if ( m_main_win == 0 ) return;
if ( user.uidata.panel == 0 ) {
mw().OpenPrivateChat( user );
}
if ( fromme ) user.uidata.panel->Said( TowxString(m_serv->GetMe().GetNick()), message );
else user.uidata.panel->Said(TowxString(user.GetNick()), message);
}
void Ui::OnUserSaidEx( User& user, const wxString& action, bool fromme )
{
if ( m_main_win == 0 ) return;
if ( user.uidata.panel == 0 ) {
mw().OpenPrivateChat( user );
}
if ( fromme ) user.uidata.panel->DidAction ( TowxString(m_serv->GetMe().GetNick()), action );
else user.uidata.panel->DidAction(TowxString(user.GetNick()), action );
}
void Ui::OnBattleOpened( IBattle& battle )
{
if ( m_main_win == 0 ) return;
mw().GetBattleListTab().AddBattle( battle );
try {
User& user = battle.GetFounder();
for ( int i = 0; i < m_serv->GetNumChannels(); i++ ) {
Channel& chan = m_serv->GetChannel( i );
if ( ( chan.UserExists(user.GetNick()) ) && ( chan.uidata.panel != 0 ) ) {
chan.uidata.panel->UserStatusUpdated( user );
}
}
} catch(...) {}
}
void Ui::OnBattleClosed( IBattle& battle )
{
if ( m_main_win == 0 ) return;
mw().GetBattleListTab().RemoveBattle( battle );
try {
if ( mw().GetJoinTab().GetBattleRoomTab().GetBattle() == &battle ) {
if (!battle.IsFounderMe() )
customMessageBoxNoModal(SL_MAIN_ICON,_("The current battle was closed by the host."),_("Battle closed"));
mw().GetJoinTab().LeaveCurrentBattle();
}
} catch (...) {}
for ( unsigned int b = 0; b < battle.GetNumUsers(); b++ ) {
User& user = battle.GetUser( b );
user.SetBattle(0);
for ( int i = 0; i < m_serv->GetNumChannels(); i++ ) {
Channel& chan = m_serv->GetChannel( i );
if ( ( chan.UserExists(user.GetNick()) ) && ( chan.uidata.panel != 0 ) ) {
chan.uidata.panel->UserStatusUpdated( user );
}
}
}
}
void Ui::OnUserJoinedBattle( IBattle& battle, User& user )
{
if ( m_main_win == 0 ) return;
mw().GetBattleListTab().UpdateBattle( battle );
try {
if ( mw().GetJoinTab().GetBattleRoomTab().GetBattle() == &battle ) {
mw().GetJoinTab().GetBattleRoomTab().OnUserJoined( user );
OnBattleInfoUpdated( std::make_pair(&battle,wxString()) );
}
} catch (...) {}
for ( int i = 0; i < m_serv->GetNumChannels(); i++ ) {
Channel& chan = m_serv->GetChannel( i );
if ( ( chan.UserExists(user.GetNick()) ) && ( chan.uidata.panel != 0 ) ) {
chan.uidata.panel->UserStatusUpdated( user );
}
}
}
void Ui::OnUserLeftBattle( IBattle& battle, User& user, bool isbot )
{
assert(wxThread::IsMain());
if ( m_main_win == 0 ) return;
user.SetSideiconIndex( -1 ); //just making sure he's not running around with some icon still set
user.BattleStatus().side = 0; // and reset side, so after rejoin we don't potentially stick with a num higher than avail
mw().GetBattleListTab().UpdateBattle( battle );
try {
if ( mw().GetJoinTab().GetBattleRoomTab().GetBattle() == &battle ) {
mw().GetJoinTab().GetBattleRoomTab().OnUserLeft( user );
OnBattleInfoUpdated( std::make_pair(&battle,wxString()) );
if ( &user == &m_serv->GetMe() ) {
mw().GetJoinTab().LeaveCurrentBattle();
mw().ShowTab(MainWindow::PAGE_LIST);
}
}
} catch (...) {}
if ( isbot ) return;
for ( int i = 0; i < m_serv->GetNumChannels(); i++ ) {
Channel& chan = m_serv->GetChannel( i );
if ( ( chan.UserExists(user.GetNick()) ) && ( chan.uidata.panel != 0 ) ) {
chan.uidata.panel->UserStatusUpdated( user );
}
}
}
void Ui::OnBattleInfoUpdated( BattleEvents::BattleEventData data )
{
IBattle& battle = *data.first;
const wxString& Tag = data.second;
if ( m_main_win == 0 ) return;
mw().GetBattleListTab().UpdateBattle( battle );
if ( mw().GetJoinTab().GetCurrentBattle() == &battle ) {
if ( Tag.IsEmpty() )
mw().GetJoinTab().UpdateCurrentBattle();
else
mw().GetJoinTab().UpdateCurrentBattle( Tag );
}
}
void Ui::OnJoinedBattle( IBattle& battle )
{
if ( m_main_win == 0 ) return;
mw().GetJoinTab().JoinBattle( battle );
mw().ShowTab(MainWindow::PAGE_JOIN);
if ( battle.GetNatType() != NAT_None ) {
wxLogWarning( _T("joining game with NAT transversal") );
}
}
void Ui::OnHostedBattle( IBattle& battle )
{
if ( m_main_win == 0 )
return;
mw().GetJoinTab().HostBattle( battle );
mw().ShowTab(MainWindow::PAGE_JOIN);
}
void Ui::OnUserBattleStatus( IBattle& battle, User& user )
{
if ( m_main_win == 0 ) return;
mw().GetJoinTab().BattleUserUpdated( user );
OnBattleInfoUpdated( std::make_pair(&battle,wxString()) );
}
void Ui::OnRequestBattleStatus( IBattle& battle )
{
if ( m_main_win == 0 ) return;
try {
if ( mw().GetJoinTab().GetBattleRoomTab().GetBattle() == &battle ) {
mw().GetJoinTab().GetBattleRoomTab().GetBattle()->OnRequestBattleStatus();
}
} catch (...) {}
}
void Ui::OnBattleStarted( IBattle& battle )
{
if ( m_main_win == 0 ) return;
slLogDebugFunc("");
mw().GetBattleListTab().UpdateBattle( battle );
}
void Ui::OnSaidBattle( IBattle& /*battle*/, const wxString& nick, const wxString& msg )
{
if ( m_main_win == 0 ) return;
try {
mw().GetJoinTab().GetBattleRoomTab().GetChatPanel().Said( nick, msg );
} catch (...) {}
}
void Ui::OnSpringTerminated( wxCommandEvent& data )
{
const int exit_code = data.GetInt();
if ( !m_serv ) return;
try {
m_serv->GetMe().Status().in_game = false;
m_serv->GetMe().SendMyUserStatus();
IBattle *battle = m_serv->GetCurrentBattle();
if ( !battle )
return;
if( battle->IsFounderMe() && battle->GetAutoLockOnStart() ) {
battle->SetIsLocked( false );
battle->SendHostInfo( IBattle::HI_Locked );
}
} catch ( assert_exception ) {}
if (exit_code != 0) {
SpringDebugReport report;
if ( wxDebugReportPreviewStd().Show( report ) )
report.Process();
}
}
void Ui::OnAcceptAgreement( const wxString& agreement )
{
AgreementDialog dlg( m_main_win, agreement );
if ( dlg.ShowModal() == 1 ) {
m_serv->AcceptAgreement();
m_serv->Login();
} else {
m_connect_retries = 0;
m_serv->Disconnect();
}
}
void Ui::OnRing( const wxString& from )
{
if ( m_main_win == 0 ) return;
m_main_win->RequestUserAttention();
if ( !wxTheApp->IsActive() ) {
const wxString msg = wxFormat(_("%s:\nring!") ) % from;
UiEvents::GetNotificationEventSender().SendEvent(
UiEvents::NotficationData( UiEvents::ServerConnection, msg ) );
}
if ( sett().GetChatPMSoundNotificationEnabled() ) {
slsound().ring();
}
}
bool Ui::IsThisMe(User& other) const
{
return IsThisMe( TowxString(other.GetNick()));
}
bool Ui::IsThisMe(User* other) const
{
return ( ( other != 0 ) && IsThisMe( TowxString(other->GetNick())) );
}
bool Ui::IsThisMe(const wxString& other) const
{
//if i'm not connected i have no identity
if (!IsConnected() || m_serv==0)
return false;
else
return ( STD_STRING(other) == m_serv->GetMe().GetNick() );
}
int Ui::TestHostPort( unsigned int port ) const
{
return m_serv->TestOpenPort( port );
}
void Ui::ReloadPresetList()
{
try {
mw().GetSPTab().ReloadPresetList();
} catch (...) {}
try {
mw().GetJoinTab().ReloadPresetList();
} catch (...) {}
}
bool Ui::OnPresetRequiringMap( const wxString& mapname )
{
if ( wxYES == customMessageBox( SL_MAIN_ICON,
_("The selected preset requires the map ") + mapname + _(". Should it be downloaded? \
\n Selecting \"no\" will remove the missing map from the preset.\n\
Please reselect the preset after download finished"),
_("Map missing"),
wxYES_NO ) ) {
Download("map", STD_STRING(mapname), "");
return true;
}
return false;
}
void Ui::OpenFileInEditor( const wxString& filepath )
{
const wxString editor_path = TowxString(SlPaths::GetEditorPath());
if ( editor_path == wxEmptyString ) {
customMessageBoxNoModal( SL_MAIN_ICON, _T("You have not chosen an external text editor to open files with.\nPlease Select one now."), _T("No editor set") );
mw().ShowConfigure( MainWindow::OPT_PAGE_GENERAL );
return;
}
bool success = ( wxExecute( editor_path + _T(" \"") + filepath + _T("\""), wxEXEC_ASYNC ) != 0 );
if ( !success ) {
customMessageBoxNoModal( SL_MAIN_ICON, _T("There was a problem launching the editor.\nPlease make sure the path is correct and the binary executable for your user.\nNote it's currently not possible to use shell-only editors like ed, vi, etc."), _T("Problem launching editor") );
mw().ShowConfigure( MainWindow::OPT_PAGE_GENERAL );
}
}
void Ui::OnInit()
{
if (sett().IsFirstRun()) {
FirstRunWelcome();
} else {
if (cfg().ReadBool(_T( "/Server/Autoconnect" ))) {
Connect(); // OnConnect changes tab
}
mw().ShowTab(cfg().ReadLong(_T( "/GUI/StartTab" )));
//don't ask for updates on first run, that's a bit much for a newbie
if (cfg().ReadBool(_T("/General/AutoUpdate"))) {