| 1 |
#include "MainFrame.h" |
| 2 |
#include <SocketAPI/clSocketClientAsync.h> |
| 3 |
#include <wx/aboutdlg.h> |
| 4 |
#include <wx/textdlg.h> |
| 5 |
|
| 6 |
MainFrame::MainFrame(wxWindow* parent) |
| 7 |
: MainFrameBaseClass(parent) |
| 8 |
, m_socket(nullptr) |
| 9 |
, m_connected(false) |
| 10 |
{ |
| 11 |
Bind(wxEVT_ASYNC_SOCKET_CONNECTED, &MainFrame::OnConnection, this); |
| 12 |
Bind(wxEVT_ASYNC_SOCKET_INPUT, &MainFrame::OnSocketInput, this); |
| 13 |
Bind(wxEVT_ASYNC_SOCKET_CONNECTION_LOST, &MainFrame::OnConnectionLost, this); |
| 14 |
Bind(wxEVT_ASYNC_SOCKET_ERROR, &MainFrame::OnConnectionLost, this); |
| 15 |
} |
| 16 |
|
| 17 |
MainFrame::~MainFrame() |
| 18 |
{ |
| 19 |
Unbind(wxEVT_ASYNC_SOCKET_CONNECTED, &MainFrame::OnConnection, this); |
| 20 |
Unbind(wxEVT_ASYNC_SOCKET_INPUT, &MainFrame::OnSocketInput, this); |
| 21 |
Unbind(wxEVT_ASYNC_SOCKET_CONNECTION_LOST, &MainFrame::OnConnectionLost, this); |
| 22 |
Unbind(wxEVT_ASYNC_SOCKET_ERROR, &MainFrame::OnConnectionLost, this); |
| 23 |
} |
| 24 |
|
| 25 |
void MainFrame::OnExit(wxCommandEvent& event) |
| 26 |
{ |
| 27 |
wxUnusedVar(event); |
| 28 |
Close(); |
| 29 |
} |
| 30 |
|
| 31 |
void MainFrame::OnAbout(wxCommandEvent& event) |
| 32 |
{ |
| 33 |
wxUnusedVar(event); |
| 34 |
wxAboutDialogInfo info; |
| 35 |
info.SetCopyright(_("My MainFrame")); |
| 36 |
info.SetLicence(_("GPL v2 or later")); |
| 37 |
info.SetDescription(_("Short description goes here")); |
| 38 |
::wxAboutBox(info); |
| 39 |
} |
| 40 |
|
| 41 |
void MainFrame::OnConnect(wxCommandEvent& event) |
| 42 |
{ |
| 43 |
if(m_socket) { |
| 44 |
wxDELETE(m_socket); |
| 45 |
} |
| 46 |
m_socket = new clSocketClientAsync(this, true); |
| 47 |
m_socket->Connect("tcp://127.0.0.1:5050"); |
| 48 |
LogMessage("Connecting to tcp://127.0.0.1:5050"); |
| 49 |
} |
| 50 |
|
| 51 |
void MainFrame::OnConnectUI(wxUpdateUIEvent& event) { event.Enable(!m_connected); } |
| 52 |
|
| 53 |
void MainFrame::OnConnection(clCommandEvent& event) |
| 54 |
{ |
| 55 |
LogMessage("Connection estabilshed successfully"); |
| 56 |
m_connected = true; |
| 57 |
} |
| 58 |
|
| 59 |
void MainFrame::OnDisconnect(wxCommandEvent& event) |
| 60 |
{ |
| 61 |
if(m_connected) { |
| 62 |
m_socket->Disconnect(); |
| 63 |
wxDELETE(m_socket); |
| 64 |
m_connected = false; |
| 65 |
LogMessage("Disconnected"); |
| 66 |
} |
| 67 |
} |
| 68 |
void MainFrame::OnDisconnectUI(wxUpdateUIEvent& event) { event.Enable(m_connected); } |
| 69 |
|
| 70 |
void MainFrame::OnList(wxCommandEvent& event) |
| 71 |
{ |
| 72 |
wxString path = ::wxGetTextFromUser(_("Remote folder path:"), _("Path"), "/"); |
| 73 |
if(path.IsEmpty()) return; |
| 74 |
|
| 75 |
clNetworkMessage message; |
| 76 |
message["command"] = "list"; |
| 77 |
message["folder"] = path; |
| 78 |
message.SendMessage(m_socket); |
| 79 |
} |
| 80 |
|
| 81 |
void MainFrame::OnListUI(wxUpdateUIEvent& event) { event.Enable(m_connected); } |
| 82 |
|
| 83 |
void MainFrame::LogMessage(const wxString& message) |
| 84 |
{ |
| 85 |
m_stc->AppendText(message + "\n"); |
| 86 |
m_stc->ScrollToEnd(); |
| 87 |
} |
| 88 |
|
| 89 |
void MainFrame::OnSocketInput(clCommandEvent& event) { LogMessage(event.GetString()); } |
| 90 |
|
| 91 |
void MainFrame::OnConnectionLost(clCommandEvent& event) |
| 92 |
{ |
| 93 |
if(m_connected) { |
| 94 |
m_socket->Disconnect(); |
| 95 |
wxDELETE(m_socket); |
| 96 |
m_connected = false; |
| 97 |
LogMessage("Connection lost, disconnected"); |
| 98 |
} |
| 99 |
} |