OpenCPN Partial API docs
Loading...
Searching...
No Matches
SendToPeerDlg.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2010 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24#include <memory>
25
26#include "config_vars.h"
27#include "mDNS_query.h"
28#include "OCPNPlatform.h"
29#include "peer_client.h"
30#include "route_gui.h"
31#include "route.h"
32#include "route_point_gui.h"
33#include "route_point.h"
34#include "SendToPeerDlg.h"
35
36#define TIMER_AUTOSCAN 94522
37#define TIMER_SCANTICK 94523
38
39extern OCPNPlatform* g_Platform;
40extern std::vector<std::shared_ptr<ocpn_DNS_record_t>> g_DNS_cache;
41extern wxDateTime g_DNS_cache_time;
42extern bool g_bportable;
43
44IMPLEMENT_DYNAMIC_CLASS(SendToPeerDlg, wxDialog)
45
46BEGIN_EVENT_TABLE(SendToPeerDlg, wxDialog)
47 EVT_BUTTON(ID_STP_CANCEL, SendToPeerDlg::OnCancelClick)
48 EVT_BUTTON(ID_STP_OK, SendToPeerDlg::OnSendClick)
49 EVT_BUTTON(ID_STP_SCAN, SendToPeerDlg::OnScanClick)
50 EVT_TIMER(TIMER_AUTOSCAN, SendToPeerDlg::OnTimerAutoscan)
51 EVT_TIMER(TIMER_SCANTICK, SendToPeerDlg::OnTimerScanTick)
52END_EVENT_TABLE()
53
55 m_PeerListBox = NULL;
56 m_pgauge = NULL;
57 m_SendButton = NULL;
58 m_CancelButton = NULL;
59 premtext = NULL;
60 m_scanTime = 5; //default, seconds
61 m_bScanOnCreate = false;
62}
63
64SendToPeerDlg::SendToPeerDlg(wxWindow* parent, wxWindowID id,
65 const wxString& caption, const wxString& hint,
66 const wxPoint& pos, const wxSize& size, long style) {
67 Create(parent, id, caption, hint, pos, size, style);
68}
69
70SendToPeerDlg::~SendToPeerDlg() {
71 delete m_PeerListBox;
72 delete m_pgauge;
73 delete m_SendButton;
74 delete m_CancelButton;
75}
76
77bool SendToPeerDlg::Create(wxWindow* parent, wxWindowID id,
78 const wxString& caption, const wxString& hint,
79 const wxPoint& pos, const wxSize& size, long style) {
80 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
81 wxDialog::Create(parent, id, caption, pos, size, style);
82
83 CreateControls(hint);
84 GetSizer()->Fit(this);
85 GetSizer()->SetSizeHints(this);
86 Centre();
87 m_pgauge->Hide();
88
89 if (m_bScanOnCreate){
90 m_autoScanTimer.SetOwner(this, TIMER_AUTOSCAN);
91 m_autoScanTimer.Start(500, wxTIMER_ONE_SHOT);
92 }
93
94 m_ScanTickTimer.SetOwner(this, TIMER_SCANTICK);
95
96 return TRUE;
97}
98
99void SendToPeerDlg::CreateControls(const wxString& hint) {
100 SendToPeerDlg* itemDialog1 = this;
101
102 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
103 itemDialog1->SetSizer(itemBoxSizer2);
104
105 // Create the ScrollBox list of available com ports in a labeled static
106 // box
107 wxStaticBox* comm_box =
108 new wxStaticBox(this, wxID_ANY, _("Detected OpenCPN peer instances"));
109
110 wxStaticBoxSizer* comm_box_sizer = new wxStaticBoxSizer(comm_box, wxVERTICAL);
111 itemBoxSizer2->Add(comm_box_sizer, 0, wxEXPAND | wxALL, 5);
112
113 m_PeerListBox = new wxComboBox(this, ID_STP_CHOICE_PEER);
114
115 // Fill in the wxComboBox with all detected peers
116 for (unsigned int i=0; i < g_DNS_cache.size(); i++){
117 wxString item(g_DNS_cache[i]->hostname.c_str());
118
119 //skip "self"
120 if (!g_hostname.IsSameAs(item.BeforeFirst('.'))) {
121 item += " {";
122 item += g_DNS_cache[i]->ip.c_str();
123 item += "}";
124 m_PeerListBox->Append(item);
125 }
126 }
127
128 if (m_PeerListBox->GetCount())
129 m_PeerListBox->SetSelection(0);
130
131 comm_box_sizer->Add(m_PeerListBox, 0, wxEXPAND | wxALL, 5);
132
133 m_RescanButton = new wxButton(itemDialog1, ID_STP_SCAN, _("Scan again"),
134 wxDefaultPosition, wxDefaultSize, 0);
135 itemBoxSizer2->Add(m_RescanButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
136
137 m_pgauge = new wxGauge(this, -1, m_scanTime);
138 itemBoxSizer2->Add(m_pgauge, 0, wxEXPAND | wxALL, 5);
139
140 // Add a reminder text box
141 itemBoxSizer2->AddSpacer(30);
142
143#if 0
144 premtext = new wxStaticText(
145 this, -1, _("Prepare GPS for Route/Waypoint upload and press Send..."));
146 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
147
148 // Create a progress gauge
149 wxStaticBox* prog_box = new wxStaticBox(this, wxID_ANY, _("Progress..."));
150
151 wxStaticBoxSizer* prog_box_sizer = new wxStaticBoxSizer(prog_box, wxVERTICAL);
152 itemBoxSizer2->Add(prog_box_sizer, 0, wxEXPAND | wxALL, 5);
153
154 m_pgauge = new wxGauge(this, -1, 100);
155 prog_box_sizer->Add(m_pgauge, 0, wxEXPAND | wxALL, 5);
156#endif
157
158 // OK/Cancel/etc.
159 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
160 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
161
162 m_CancelButton = new wxButton(itemDialog1, ID_STP_CANCEL, _("Cancel"),
163 wxDefaultPosition, wxDefaultSize, 0);
164 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
165
166 m_SendButton = new wxButton(itemDialog1, ID_STP_OK, _("Send"),
167 wxDefaultPosition, wxDefaultSize, 0);
168 itemBoxSizer16->Add(m_SendButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
169 m_SendButton->SetDefault();
170}
171
172void SendToPeerDlg::SetMessage(wxString msg) {
173 if (premtext) {
174 premtext->SetLabel(msg);
175 premtext->Refresh(true);
176 }
177}
178
179void SendToPeerDlg::OnSendClick(wxCommandEvent& event) {
180 if (m_RouteList.empty() && m_TrackList.empty() && m_RoutePointList.empty())
181 Close();
182
183 // Get the selected peer information
184 wxString peer_ip = m_PeerListBox->GetValue();
185 wxString server_name = peer_ip.BeforeFirst('{').Trim();
186 int tail = peer_ip.Find('{');
187 if (tail != wxNOT_FOUND)
188 peer_ip = peer_ip.Mid(tail+1);
189 peer_ip = peer_ip.BeforeFirst('}');
190 peer_ip += ":";
191
192 // Is the destination a portable? Detect by string inspection.
193 wxString p = "Portable";
194 if (p.IsSameAs(server_name.BeforeFirst('-')))
195 peer_ip += "8444";
196 else
197 peer_ip += "8443";
198
199 std::string server_address("https://");
200 server_address += peer_ip.ToStdString();
201
202
203 //g_uploadConnection = src; // save for persistence
204
205
206 // And send it out
207 if (!m_RouteList.empty() || !m_RoutePointList.empty() || !m_TrackList.empty())
208 {
209 int return_code = SendRoute(server_address, server_name.ToStdString(), m_RouteList, m_RoutePointList, m_TrackList, true);
210 }
211
212 // Show( false );
213 // event.Skip();
214 Close();
215}
216
217void SendToPeerDlg::OnScanClick(wxCommandEvent& event) {
218 DoScan();
219}
220
221void SendToPeerDlg::OnTimerAutoscan(wxTimerEvent &event) {
222 DoScan();
223}
224
225void SendToPeerDlg::OnTimerScanTick(wxTimerEvent &event) {
226 m_tick--;
227 if(m_pgauge) {
228 int v = m_pgauge->GetValue();
229 m_pgauge->SetValue(v+1);
230 }
231
232 if (m_tick == 0){
233 // Housekeeping
234 m_ScanTickTimer.Stop();
235 g_Platform->HideBusySpinner();
236 m_RescanButton->Enable();
237 m_pgauge->Hide();
238 m_bScanOnCreate = false;
239
240 // Clear the combo box
241 m_PeerListBox->Clear();
242
243 // Fill in the wxComboBox with all detected peers
244 for (unsigned int i=0; i < g_DNS_cache.size(); i++){
245 wxString item(g_DNS_cache[i]->hostname.c_str());
246
247 //skip "self"
248 if (!g_hostname.IsSameAs(item.BeforeFirst('.'))) {
249 item += " {";
250 item += g_DNS_cache[i]->ip.c_str();
251 item += "}";
252 m_PeerListBox->Append(item);
253 }
254 }
255 if (m_PeerListBox->GetCount())
256 m_PeerListBox->SetSelection(0);
257
258 g_DNS_cache_time = wxDateTime::Now();
259 }
260}
261
262void SendToPeerDlg::DoScan() {
263 m_RescanButton->Disable();
264 g_Platform->ShowBusySpinner();
265 m_pgauge->SetRange(m_scanTime);
266 m_pgauge->SetValue(0);
267 m_pgauge->Show();
268
269 FindAllOCPNServers(m_scanTime);
270
271 m_tick = m_scanTime;
272 m_ScanTickTimer.Start(1000, wxTIMER_CONTINUOUS);
273}
274
275void SendToPeerDlg::OnCancelClick(wxCommandEvent& event) {
276 // Show( false );
277 // event.Skip();
278 Close();
279}
Route "Send to Peer..." Dialog Definition.
Definition: SendToPeerDlg.h:54