-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwxBupCheck4Update.cpp
More file actions
201 lines (188 loc) · 6.48 KB
/
wxBupCheck4Update.cpp
File metadata and controls
201 lines (188 loc) · 6.48 KB
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
//--------------------------------------------------------------
// Name: wxBupCheck4Update.cpp
// Purpose:
// Author: A. Wiegert
//
// Copyright:
// Licence: wxWidgets licence
//--------------------------------------------------------------
//--------------------------------------------------------------
// Standard wxWidgets headers
//--------------------------------------------------------------
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
# if defined ( _DEBUG )
// this statement NEEDS to go BEFORE all headers
# define _CRTDBG_MAP_ALLOC
# endif
#endif
#include "wxBupPreProcDefsh.h" // needs to be first
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// For all others, include the necessary headers (this file is
// usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wxBupBaseFrame.h"
#include "wxBupFrameh.h"
#include "wxBupCurlh.h"
#include "wxBupCheckUpdateDialogh.h"
// -------------------------------------------------------------
#if defined( _MSC_VER )
// this block needs to go AFTER all headers
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#endif
// ------------------------------------------------------------------
/**
* Check if we have the latest version.
* Get the string from the download site and advise user
* if a more recent version is available.
* This rotine is called from the OnIdle() handler when the application
* first starts up.
* To allow the user to call for a check, we'll return the status
* so we can advise the user if there is no newer version
* NOTE: the version php file MUST NOT be UTF-8 encoded - else
* the server returns a BAD string
* We need to check if the internet is available here, but
* we only care about internet availability if we are running the
* release version.
* When we run the debug version, we only need the local server.
* for HTTP status codes, see:
* https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*/
bool MyFrame::Check4Update(bool bFromUser,
wxString& ar_wsCurrentVersion, wxString& ar_wsLatestVersion)
{
wxString wsT;
wxString wsUrl;
long lHttpCode;
wsUrl = CHECK_4_UPDATE_URL;
if ( wxGetApp().IsInternetAvailable())
{
#if defined( WANT_CURL )
// need to setup the dialog early, so I can get at the URL
MyBupUpdateDlg dlg(this);
wxString wsUrl = dlg.GetHyperLinkBupUpdatePage()->GetURL();
wxString wsVersion;
MyCurlObject MyCurl(wsUrl, wxGetApp().m_wsAppName);
wxString wsVersionHtml = MyCurl.getData();
CURLcode code = MyCurl.GetReturnCode();
MyCurl.GetHttpCode( lHttpCode );
if (code != CURLE_OK)
{
wsT.Printf(
_("Checking for Updates\nGot error %d from libcurl interface!\n%s"),
code, curl_easy_strerror(code));
wxMessageBox(wsT, "Error", wxOK);
return false;
}
if ( !wsVersionHtml.IsEmpty() && ( lHttpCode == 200 ) )
{
// extract & clean up the string
// first find the 'Latest Version" line
int iStart = wsVersionHtml.Find(_T("Latest Version"));
wsVersion = wsVersionHtml.Mid(iStart);
wsVersion = wsVersion.AfterFirst(':');
wsVersion = wsVersion.BeforeFirst('<');
wsVersion = wsVersion.Trim(true);
wsVersion = wsVersion.Trim(false);
ar_wsLatestVersion = wsVersion;
ar_wsCurrentVersion.Printf(_T("%d.%d.%d"),
giMajorVersion, giMinorVersion, giBuildNumber);
// now convert & test against current version
wxString wsMajor = wsVersion.BeforeFirst('.');
wxString wsMinor = wsVersion.AfterFirst('.');
wxString wsBuild = wsMinor.AfterFirst('.');
wsMinor = wsMinor.BeforeFirst('.');
long lMajor;
long lMinor;
long lBuild;
wsMajor.ToLong(&lMajor);
wsMinor.ToLong(&lMinor);
wsBuild.ToLong(&lBuild);
wxString wsTVersion = dlg.GetStaticTextVersion()->GetLabel();
wsT.Printf(wsTVersion, (int)lMajor, (int)lMinor, (int)lBuild);
if ((lMajor > giMajorVersion) ||
((lMajor == giMajorVersion) && (lMinor > giMinorVersion)) ||
((lMajor == giMajorVersion) && (lMinor == giMinorVersion) && (lBuild > giBuildNumber)))
{
dlg.GetStaticTextVersion()->SetLabel(wsT);
if (dlg.ShowModal() == wxID_OK)
return true;
}
}
else if ( lHttpCode == 404 )
{
// we got an empty string, let user know
#if defined( _DEBUG )
wsT.Printf(
_("The Update Check URL \"%s\" was not found"), wsUrl );
wxMessageBox(wsT, "Error", wxOK);
#else
// do nothing for release - don't advertise the URL
#endif
return false;
}
else
{
#if defined( _DEBUG )
// we got an empty string, let user know
wsT.Printf(
_("Checking for Updates\nGot an empty response from libcurl interface!") );
wxMessageBox(wsT, "Error", wxOK);
#else
// do nothing for release - don't advertise the URL
#endif
return false;
}
#endif
}
else if ( bFromUser )
{
wsT.Printf(
_("Cannot connect to the internet to check for updates!\n") +
_("Perhaps the server is down. Please try again later.") );
wxMessageBox(wsT, "Error", wxOK);
// just advise user and pretend there is nothing to do
// to avoid a second error dialog about the version update
return true;
}
return false;
}
// ------------------------------------------------------------------
/**
* Allow user to manually check for a newer version.
* Here we only need to handle the case where the user has
* the latest available version.
* The alternate case, i. e. a newer version is available, is handled by the
* called function.
*/
void MyFrame::OnCheck4Update(wxCommandEvent& event)
{
wxString wsCurrentVersion;
wxString wsLatestVersion;
if( ! MyFrame::Check4Update( true, wsCurrentVersion, wsLatestVersion ) )
{
wxString wsT;
wsT.Printf( _("No newer version found!\n") +
_("Your version is: %s.\n") +
//_("Latest version: %s\n") +
_("\n\nPlease note:\n") +
_("Automatic checking for updates at startup can be\n") +
_("enabled or disabled in the 'Options' dialog"),
wsCurrentVersion /*, wsLatestVersion */);
wxMessageBox(wsT, "Notice", wxOK );;
}
event.Skip();
}
// ------------------------------- eof -------------------------