Add Cancel button to webrequest sample

This commit is contained in:
Tobias Taschner
2018-10-31 22:49:34 +01:00
parent d32e27191d
commit 6bafed4ceb

View File

@@ -144,9 +144,17 @@ public:
mainSizer->Add(m_notebook, wxSizerFlags(1).Expand().Border()); mainSizer->Add(m_notebook, wxSizerFlags(1).Expand().Border());
m_startButton = new wxButton(this, wxID_ANY, "&Start Request"); wxStdDialogButtonSizer* btnSizer = new wxStdDialogButtonSizer();
m_cancelButton = new wxButton(this, wxID_CANCEL, "Cancel");
m_cancelButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnCancelButton, this);
m_cancelButton->Disable();
btnSizer->AddButton(m_cancelButton);
m_startButton = new wxButton(this, wxID_OK, "&Start Request");
m_startButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnStartButton, this); m_startButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnStartButton, this);
mainSizer->Add(m_startButton, wxSizerFlags().Border()); btnSizer->AddButton(m_startButton);
btnSizer->Realize();
mainSizer->Add(btnSizer, wxSizerFlags().Border());
wxCommandEvent evt; wxCommandEvent evt;
OnPostCheckBox(evt); OnPostCheckBox(evt);
@@ -161,7 +169,7 @@ public:
m_downloadProgressTimer.Bind(wxEVT_TIMER, m_downloadProgressTimer.Bind(wxEVT_TIMER,
&WebRequestFrame::OnProgressTimer, this); &WebRequestFrame::OnProgressTimer, this);
m_downloadRequest = NULL; m_currentRequest = NULL;
} }
void OnStartButton(wxCommandEvent& WXUNUSED(evt)) void OnStartButton(wxCommandEvent& WXUNUSED(evt))
@@ -171,6 +179,7 @@ public:
// Create request for the specified URL from the default session // Create request for the specified URL from the default session
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest( wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
m_urlTextCtrl->GetValue())); m_urlTextCtrl->GetValue()));
m_currentRequest = request.get();
// Bind event for state change // Bind event for state change
request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this); request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this);
@@ -187,14 +196,13 @@ public:
m_textResponseTextCtrl->Clear(); m_textResponseTextCtrl->Clear();
// Set postdata if checked // Set postdata if checked
if (m_postCheckBox->IsChecked()) if ( m_postCheckBox->IsChecked() )
{ {
request->SetData(m_postRequestTextCtrl->GetValue(), request->SetData(m_postRequestTextCtrl->GetValue(),
m_postContentTypeTextCtrl->GetValue()); m_postContentTypeTextCtrl->GetValue());
} }
break; break;
case Page_Download: case Page_Download:
m_downloadRequest = request.get();
request->SetStorage(wxWebRequest::Storage_File); request->SetStorage(wxWebRequest::Storage_File);
m_downloadGauge->SetValue(0); m_downloadGauge->SetValue(0);
m_downloadGauge->Pulse(); m_downloadGauge->Pulse();
@@ -218,9 +226,22 @@ public:
request->Start(); request->Start();
} }
void OnCancelButton(wxCommandEvent& WXUNUSED(evt))
{
if ( m_currentRequest )
m_currentRequest->Cancel();
}
void OnWebRequestState(wxWebRequestEvent& evt) void OnWebRequestState(wxWebRequestEvent& evt)
{ {
m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active); m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active);
m_cancelButton->Enable(evt.GetState() == wxWebRequest::State_Active);
if ( evt.GetState() != wxWebRequest::State_Active )
{
m_currentRequest = NULL;
m_downloadProgressTimer.Stop();
}
switch (evt.GetState()) switch (evt.GetState())
{ {
@@ -244,13 +265,8 @@ public:
break; break;
case Page_Download: case Page_Download:
{ {
// Force last progress update m_downloadGauge->SetValue(100);
wxTimerEvent timerEvt; m_downloadStaticText->SetLabel("");
OnProgressTimer(timerEvt);
// Stop updating download progress
m_downloadRequest = NULL;
m_downloadProgressTimer.Stop();
GetStatusBar()->SetStatusText("Download completed"); GetStatusBar()->SetStatusText("Download completed");
@@ -260,7 +276,7 @@ public:
wxFD_SAVE | wxFD_OVERWRITE_PROMPT); wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if ( fileDlg.ShowModal() == wxID_OK ) if ( fileDlg.ShowModal() == wxID_OK )
{ {
if (!wxRenameFile(evt.GetResponseFileName(), fileDlg.GetPath())) if ( !wxRenameFile(evt.GetResponseFileName(), fileDlg.GetPath()) )
wxLogError("Could not move file"); wxLogError("Could not move file");
} }
@@ -274,12 +290,15 @@ public:
break; break;
case wxWebRequest::State_Failed: case wxWebRequest::State_Failed:
// Stop updating download progress
m_downloadRequest = NULL;
m_downloadProgressTimer.Stop();
wxLogError("Web Request failed: %s", evt.GetErrorDescription()); wxLogError("Web Request failed: %s", evt.GetErrorDescription());
GetStatusBar()->SetStatusText(""); GetStatusBar()->SetStatusText("");
break; break;
case wxWebRequest::State_Cancelled:
m_downloadGauge->SetValue(0);
m_downloadStaticText->SetLabel("");
GetStatusBar()->SetStatusText("Cancelled");
break;
} }
} }
@@ -311,15 +330,15 @@ public:
void OnProgressTimer(wxTimerEvent& WXUNUSED(evt)) void OnProgressTimer(wxTimerEvent& WXUNUSED(evt))
{ {
if (!m_downloadRequest || m_downloadRequest->GetBytesExpectedToReceive() <= 0) if ( !m_currentRequest || m_currentRequest->GetBytesExpectedToReceive() <= 0 )
return; return;
m_downloadGauge->SetValue((m_downloadRequest->GetBytesReceived() * 100) / m_downloadGauge->SetValue((m_currentRequest->GetBytesReceived() * 100) /
m_downloadRequest->GetBytesExpectedToReceive()); m_currentRequest->GetBytesExpectedToReceive());
m_downloadStaticText->SetLabelText(wxString::Format("%lld/%lld", m_downloadStaticText->SetLabelText(wxString::Format("%lld/%lld",
m_downloadRequest->GetBytesReceived(), m_downloadRequest->GetBytesExpectedToReceive())); m_currentRequest->GetBytesReceived(), m_currentRequest->GetBytesExpectedToReceive()));
} }
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt)) void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))
@@ -358,7 +377,9 @@ private:
wxNotebook* m_notebook; wxNotebook* m_notebook;
wxTextCtrl* m_urlTextCtrl; wxTextCtrl* m_urlTextCtrl;
wxButton* m_startButton; wxButton* m_startButton;
wxButton* m_cancelButton;
wxStaticBitmap* m_imageStaticBitmap; wxStaticBitmap* m_imageStaticBitmap;
wxWebRequest* m_currentRequest;
wxCheckBox* m_postCheckBox; wxCheckBox* m_postCheckBox;
wxTextCtrl* m_postContentTypeTextCtrl; wxTextCtrl* m_postContentTypeTextCtrl;
@@ -368,7 +389,6 @@ private:
wxGauge* m_downloadGauge; wxGauge* m_downloadGauge;
wxStaticText* m_downloadStaticText; wxStaticText* m_downloadStaticText;
wxTimer m_downloadProgressTimer; wxTimer m_downloadProgressTimer;
wxWebRequest* m_downloadRequest;
wxStaticText* m_advCountStaticText; wxStaticText* m_advCountStaticText;
long long m_advCount; long long m_advCount;