OpenCPN Partial API docs
peer_client_dlg.cpp
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: Peer-peer data sharing.
5  * Author: David Register
6  *
7  ***************************************************************************
8  * Copyright (C) 2022 by David Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  **************************************************************************/
25 
26 #include <cassert>
27 #include <iostream>
28 #include <sstream>
29 
30 #include <wx/fileconf.h>
31 #include <wx/json_defs.h>
32 #include <wx/jsonreader.h>
33 #include <wx/tokenzr.h>
34 
35 #include <curl/curl.h>
36 
37 #include "model/peer_client.h"
38 #include "model/rest_server.h"
39 #include "model/semantic_vers.h"
40 #include "model/config_vars.h"
41 
42 #include "peer_client_dlg.h"
43 #include "ocpn_frame.h"
44 #include "FontMgr.h"
45 #include "gui_lib.h"
46 
47 extern MyFrame* gFrame;
48 
49 struct MemoryStruct {
50  char* memory;
51  size_t size;
52 };
53 
54 PinConfirmDlg::PinConfirmDlg() {
55  m_ok_btn = NULL;
56  m_cancel_btn = NULL;
57  premtext = NULL;
58 }
59 
60 PinConfirmDlg::PinConfirmDlg(wxWindow* parent, wxWindowID id,
61  const wxString& caption, const wxString& hint,
62  const wxPoint& pos, const wxSize& size,
63  long style) {
64  wxFont* pif = FontMgr::Get().GetFont(_T("Dialog"));
65  SetFont(*pif);
66  Create(parent, id, caption, hint, pos, size, style);
67 }
68 
69 PinConfirmDlg::~PinConfirmDlg() {
70  delete m_ok_btn;
71  delete m_cancel_btn;
72 }
73 
75 void PinConfirmDlg::OnTextChange(wxCommandEvent&) {
76  auto txt = m_pin_textctrl->GetValue().ToStdString();
77  int value = -1;
78  if (txt.size() >= 4) {
79  try {
80  value = std::stoi(txt);
81  } catch (std::exception&) {
82  ;
83  }
84  }
85  m_ok_btn->Enable(value >= 0 && txt.size() >= 4);
86 }
87 
88 bool PinConfirmDlg::Create(wxWindow* parent, wxWindowID id,
89  const wxString& caption, const wxString& hint,
90  const wxPoint& pos, const wxSize& size, long style) {
91  SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
92  wxDialog::Create(parent, id, caption, pos, size, style);
93 
94  CreateControls(hint);
95  GetSizer()->Fit(this);
96  GetSizer()->SetSizeHints(this);
97  Centre();
98  return TRUE;
99 }
100 
101 void PinConfirmDlg::CreateControls(const wxString&) {
102  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
103  SetSizer(itemBoxSizer2);
104 
105  // Add a reminder text box
106  itemBoxSizer2->AddSpacer(20);
107 
108  premtext = new wxStaticText(
109  this, -1, "A loooooooooooooooooooooooooooooooooooooooooooooong line\n");
110  itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
111 
112  m_pin_textctrl = new wxTextCtrl(this, wxID_ANY, " ", wxDefaultPosition,
113  wxDefaultSize, wxTE_CENTRE);
114  itemBoxSizer2->Add(m_pin_textctrl, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 10);
115 
116  // OK/Cancel/etc.
117  wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
118  itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
119 
120  m_cancel_btn = new wxButton(this, wxID_CANCEL);
121 
122  m_cancel_btn->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
123  [&](wxCommandEvent e) { OnCancelClick(e); });
124  itemBoxSizer16->Add(m_cancel_btn, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
125 
126  m_ok_btn = new wxButton(this, wxID_OK);
127  itemBoxSizer16->Add(m_ok_btn, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
128  m_ok_btn->SetDefault();
129  m_ok_btn->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
130  [&](wxCommandEvent e) { OnOKClick(e); });
131  m_ok_btn->Enable(false);
132  m_pin_textctrl->Bind(wxEVT_TEXT,
133  [&](wxCommandEvent& ev) { OnTextChange(ev); });
134 }
135 
136 void PinConfirmDlg::SetMessage(const wxString& msg) {
137  if (premtext) {
138  premtext->SetLabel(msg);
139  premtext->Refresh(true);
140  }
141 }
142 
143 void PinConfirmDlg::SetPincodeText(const wxString& message) {
144  m_pin_textctrl->ChangeValue(message);
145  m_pin_textctrl->Show();
146  GetSizer()->Fit(this);
147 }
148 
149 void PinConfirmDlg::OnOKClick(wxCommandEvent&) {
150  EndModal(wxID_OK);
151 }
152 
153 void PinConfirmDlg::OnCancelClick(wxCommandEvent&) { EndModal(wxID_CANCEL); }
General purpose GUI support.