-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
update_manager.cpp
266 lines (213 loc) · 8.56 KB
/
update_manager.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
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Mark Roszko <[email protected]>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <update_manager.h>
#include <pgm_base.h>
#include <string>
#include <sstream>
#include "settings/settings_manager.h"
#include "settings/kicad_settings.h"
#include <notifications_manager.h>
#include <kicad_curl/kicad_curl.h>
#include <kicad_curl/kicad_curl_easy.h>
#include <progress_reporter.h>
#include <dialogs/dialog_update_notice.h>
#include <nlohmann/json.hpp>
#include <core/json_serializers.h>
#include <wx/log.h>
#include <wx/event.h>
#include <wx/filefn.h>
#include <wx/translation.h>
#include <wx/notifmsg.h>
#include <background_jobs_monitor.h>
#include <core/thread_pool.h>
#include <build_version.h>
struct UPDATE_REQUEST
{
wxString platform;
wxString arch;
wxString current_version;
wxString lang;
wxString last_check;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( UPDATE_REQUEST, platform, arch, current_version, lang,
last_check )
struct UPDATE_RESPONSE
{
wxString version;
wxString release_date;
wxString details_url;
wxString downloads_url;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( UPDATE_RESPONSE, version, release_date, details_url,
downloads_url )
#define UPDATE_QUERY_ENDPOINT wxS( "https://downloads.kicad.org/api/v1/update" )
UPDATE_MANAGER::UPDATE_MANAGER() : m_working( false )
{
}
int UPDATE_MANAGER::PostRequest( const wxString& aUrl, std::string aRequestBody,
std::ostream* aOutput, PROGRESS_REPORTER* aReporter,
const size_t aSizeLimit )
{
bool size_exceeded = false;
TRANSFER_CALLBACK callback = [&]( size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow )
{
if( aSizeLimit > 0 && ( dltotal > aSizeLimit || dlnow > aSizeLimit ) )
{
size_exceeded = true;
// Non zero return means abort.
return true;
}
if( aReporter )
{
if( dltotal > 1000 )
{
aReporter->SetCurrentProgress( dlnow / (double) dltotal );
aReporter->Report( wxString::Format( _( "Downloading %lld/%lld kB" ), dlnow / 1000,
dltotal / 1000 ) );
}
else
{
if( aReporter )
aReporter->SetCurrentProgress( 0.0 );
}
return !aReporter->KeepRefreshing();
}
else
return false;
};
KICAD_CURL_EASY curl;
curl.SetHeader( "Accept", "application/json" );
curl.SetHeader( "Content-Type", "application/json" );
curl.SetHeader( "charset", "utf-8" );
curl.SetOutputStream( aOutput );
curl.SetURL( aUrl.ToUTF8().data() );
curl.SetPostFields( aRequestBody );
curl.SetFollowRedirects( true );
curl.SetTransferCallback( callback, 250000L );
int code = curl.Perform();
if( aReporter && !aReporter->IsCancelled() )
aReporter->SetCurrentProgress( 1.0 );
if( code != CURLE_OK )
{
if( aReporter )
{
if( code == CURLE_ABORTED_BY_CALLBACK && size_exceeded )
aReporter->Report( _( "Download is too large." ) );
else if( code != CURLE_ABORTED_BY_CALLBACK )
aReporter->Report( wxString( curl.GetErrorText( code ) ) );
}
return 0;
}
return curl.GetResponseStatusCode();
}
void UPDATE_MANAGER::CheckForUpdate( wxWindow* aNoticeParent )
{
if( m_working )
return;
m_working = false;
m_updateBackgroundJob = Pgm().GetBackgroundJobMonitor().Create( _( "Update Check" ) );
auto update_check = [aNoticeParent, this]() -> void
{
std::stringstream update_json_stream;
std::stringstream request_json_stream;
wxString aUrl = UPDATE_QUERY_ENDPOINT;
m_updateBackgroundJob->m_reporter->SetNumPhases( 1 );
m_updateBackgroundJob->m_reporter->Report( _( "Requesting update info" ) );
UPDATE_REQUEST requestContent;
// These platform keys are specific to the downloads site
#if defined( __WXMSW__ )
requestContent.platform = "windows";
#if defined( KICAD_BUILD_ARCH_X64 )
requestContent.arch = "amd64";
#elif defined( KICAD_BUILD_ARCH_X86 )
requestContent.arch = "i686";
#elif defined( KICAD_BUILD_ARCH_ARM )
requestContent.arch = "arm";
#elif defined( KICAD_BUILD_ARCH_ARM64 )
requestContent.arch = "arm64";
#endif
#elif defined( __WXOSX__ )
requestContent.platform = "macos";
requestContent.arch = "unified";
#else
//everything else gets lumped as linux
requestContent.platform = "linux";
requestContent.arch = "";
#endif
wxString verString = GetSemanticVersion();
verString.Replace( "~", "-" ); // make the string valid for semver
requestContent.current_version = verString;
requestContent.lang = Pgm().GetLanguageTag();
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
requestContent.last_check = settings->m_lastUpdateCheckTime;
nlohmann::json requestJson = nlohmann::json( requestContent );
request_json_stream << requestJson;
int responseCode =
PostRequest( aUrl, request_json_stream.str(), &update_json_stream, NULL, 20480 );
// Check that the response is 200 (content provided)
// We can also return 204 for no update
if( responseCode == 200 )
{
nlohmann::json update_json;
UPDATE_RESPONSE response;
try
{
update_json_stream >> update_json;
response = update_json.get<UPDATE_RESPONSE>();
if( response.version != settings->m_lastReceivedUpdate )
{
aNoticeParent->CallAfter(
[aNoticeParent, response]()
{
auto notice = new DIALOG_UPDATE_NOTICE( aNoticeParent,
response.version,
response.details_url,
response.downloads_url );
int retCode = notice->ShowModal();
if( retCode != wxID_RETRY )
{
// basically saving the last received update prevents us from
// prompting again
SETTINGS_MANAGER& set_mgr = Pgm().GetSettingsManager();
KICAD_SETTINGS* cfg = set_mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
cfg->m_lastReceivedUpdate = response.version;
}
} );
}
}
catch( const std::exception& e )
{
wxLogError( wxString::Format( _( "Unable to parse update response: %s" ),
e.what() ) );
}
}
settings->m_lastUpdateCheckTime = wxDateTime::Now().FormatISOCombined();
Pgm().GetBackgroundJobMonitor().Remove( m_updateBackgroundJob );
m_updateBackgroundJob = nullptr;
m_working = false;
};
thread_pool& tp = GetKiCadThreadPool();
tp.push_task( update_check );
}