~psiphon-inc/+junk/psiphon-1.6

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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
////////////////////////////////////////////////////////////////////////////////
//	Copyright (c) CitizenLab, 2006. All Rights Reserved.
//	The latest version of this code is available at http://psiphon.civisec.org/
//
//	This software is open source; 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 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 at:
//	http://psiphon.civisec.org/
//	
//	This General Public License does NOT permit incorporating this software 
//	into proprietary programs.
////////////////////////////////////////////////////////////////////////////////

#include "stdwx.h"
#include "resources.h"
#include "UpdateDlg.h"
#include "GetIP.h"
#include "DownloadFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

wxString wxUpdateDlg::GetUpdateFileName(const wxString& sVersion)
{
	return wxString::Format(_T("psiphon-%s.msi"), sVersion.c_str());
}

wxString wxUpdateDlg::GetUpdatePath(const wxString& sVersion)
{
	return wxString::Format(_T("http://psiphon.ca/download/%s"), GetUpdateFileName(sVersion).c_str());
}

bool wxUpdateDlg::CheckForUpdates(wxWindow* parent, bool bShowNoUpdates)
{
	wxBusyCursor wait;

	wxString sLatestVersion = GetLatestVersion();
	wxString sCurrentVersion = PSIPHON_VERSION;

	if(sLatestVersion == _T(""))
		return false;

	if(sLatestVersion > sCurrentVersion)
	{
		wxUpdateDlg dlg(sLatestVersion, sCurrentVersion, parent);
		
		int nRes = 0;
		nRes = dlg.ShowModal();

#ifdef _WIN32
		if(nRes == wxID_OK)
		{
			wxString sFile = GetUpdateFileName(sLatestVersion);
			::ShellExecute(NULL, _T("open"), sFile, NULL, NULL, SW_SHOWNORMAL);
		}
#endif
	
	}
	else if(bShowNoUpdates)
	{
		wxString sMess = wxString::Format(_("Your software is up to date."));
		wxMessageBox(sMess, _("Checking for updates"));		
	}

	return true;
}

IMPLEMENT_DYNAMIC_CLASS(wxUpdateDlg, wxDialog)

enum
{
	IDC_GET_UPDATE = 10001,
	IDC_PROGRESS,
	IDC_MESSAGE,
};

wxUpdateDlg::wxUpdateDlg()
{
}

wxUpdateDlg::wxUpdateDlg(const wxString& _sLatestVersion, const wxString& _sCurrentVersion, wxWindow* parent)
{
	m_sLatestVersion = _sLatestVersion;
	m_sCurrentVersion = _sCurrentVersion;

	m_sFile = GetUpdateFileName(m_sLatestVersion);
	m_sPath = GetUpdatePath(m_sLatestVersion);

	m_pDownloadFile = NULL;
	m_bDownloading = false;

	Create(parent);
}

wxUpdateDlg::~wxUpdateDlg()
{
}

bool wxUpdateDlg::Create(wxWindow* parent)
{
	SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
	wxDialog::Create(parent, IDD_UPDATE, _("Checking for updates"), wxDefaultPosition, wxSize(400, 300), wxCAPTION|wxSYSTEM_MENU|wxCLOSE_BOX|wxDIALOG_MODAL);

	CreateControls();

	if(GetSizer())
	{
		GetSizer()->SetSizeHints(this);
	}

	Centre();

	return true;
}

#ifdef _WIN32
void wxUpdateDlg::CreateControls()
{
	wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
	SetSizer(itemBoxSizer2);

	wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
	itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);

	wxString sMess = wxString::Format(_("Newer version of psiphon is available.\n"
										"Would you like to download it?"));

	m_stMessage = new wxStaticText(this, IDC_MESSAGE, sMess, wxDefaultPosition, wxDefaultSize, 0);
	itemBoxSizer3->Add(m_stMessage, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);

	wxSize mine = wxDefaultSize;
	mine.SetHeight(20);
	mine.SetWidth(200);

	m_Progress = new wxGauge(this, IDC_PROGRESS, 100, wxDefaultPosition, mine);
	itemBoxSizer3->Add(m_Progress, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);

	wxBoxSizer* itemBoxSizerButtons = new wxBoxSizer(wxHORIZONTAL);
	itemBoxSizer3->Add(itemBoxSizerButtons, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);

	m_btnGetUpdate = new wxButton(this, IDC_GET_UPDATE, _("&Get update"), wxDefaultPosition, wxDefaultSize, 0);
	m_btnGetUpdate->SetDefault();
	itemBoxSizerButtons->Add(m_btnGetUpdate, 0,  wxALIGN_CENTER_VERTICAL | wxALL, 5);

	wxButton* itemButton18 = new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0);
	itemBoxSizerButtons->Add(itemButton18, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
}
#else

void wxUpdateDlg::CreateControls()
{
	wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
	SetSizer(itemBoxSizer2);

	wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
	itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);

	wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
	itemBoxSizer3->Add(itemBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
//

	wxString sMess = wxString::Format(
	_("Checking if a newer version of psiphon is available...\n\n"
	"Your current version is %s\n"
	"Latest version is %s."), m_sCurrentVersion.c_str(), m_sLatestVersion.c_str());

	wxStaticText* itemStaticText11 = new wxStaticText(this, wxID_STATIC, sMess, wxDefaultPosition, wxDefaultSize, 0);
	itemBoxSizer4->Add(itemStaticText11, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);

	wxBoxSizer* itemBoxSizerLink = new wxBoxSizer(wxHORIZONTAL);
	itemBoxSizer4->Add(itemBoxSizerLink, 0, wxALIGN_LEFT | wxALL, 5);
	
	wxStaticText* itemStaticText12 = new wxStaticText(this, wxID_STATIC, _("Please go to"), wxDefaultPosition, wxDefaultSize, 0);
	itemBoxSizerLink->Add(itemStaticText12, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);

	wxString sLink = _T("http://psiphon.civisec.org/");
	wxHyperlinkCtrl* pHyperlink = new wxHyperlinkCtrl(this, wxID_STATIC, sLink, sLink, wxDefaultPosition, wxDefaultSize, wxHL_CONTEXTMENU | wxNO_BORDER | wxHL_ALIGN_LEFT);
	itemBoxSizerLink->Add(pHyperlink, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
	
	wxStaticText* itemStaticText13 = new wxStaticText(this, wxID_STATIC, _("to download the current version."), wxDefaultPosition, wxDefaultSize, 0);
	itemBoxSizer4->Add(itemStaticText13, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);

	wxButton* itemButton17 = new wxButton(this, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0);
	itemButton17->SetDefault();
	itemBoxSizer3->Add(itemButton17, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
}
#endif

BEGIN_EVENT_TABLE(wxUpdateDlg, wxDialog)
	EVT_BUTTON(IDC_GET_UPDATE, wxUpdateDlg::OnGetUpdate)
	EVT_BUTTON(wxID_CANCEL, wxUpdateDlg::OnCancel)
    EVT_DOWNLOAD(wxUpdateDlg::OnDownloadEvent)
END_EVENT_TABLE()

void wxUpdateDlg::OnGetUpdate(wxCommandEvent&)
{
	m_btnGetUpdate->Enable(false);
	
	wxRemoveFile(m_sFile);

	m_pDownloadFile = new wxDownloadFile(this, m_sPath, m_sFile, true, 1024*100); 
    wxString sMessage;

	if(m_pDownloadFile)
	{
		sMessage = wxString::Format(_("downloading '%s'..."), m_sFile.c_str());	
		m_bDownloading = true;
	}
	else
	{
		sMessage = wxString::Format(_("downloading '%s' failed."),  m_sFile.c_str());
		m_bDownloading = false;
		m_btnGetUpdate->Enable(true);
	}

	m_stMessage->SetLabel(sMessage);	
}


void wxUpdateDlg::OnCancel(wxCommandEvent&)
{
	if(m_pDownloadFile && m_bDownloading)
	{
		m_bDownloading = false;
		m_pDownloadFile->CancelDownload();
	}
	else
	{
		EndModal(wxID_CANCEL);
	}
}

void wxUpdateDlg::OnDownloadEvent(wxDownloadEvent &event)
{
	if(event.GetDownLoadStatus() == DOWNLOAD_COMPLETE)
	{
		m_bDownloading = false;
		EndModal(wxID_OK);
		return;
	}
	
	if(event.GetDownLoadStatus() == DOWNLOAD_CANCELLED)
	{
		wxRemoveFile(m_sFile);
		m_bDownloading = false;
		EndModal(wxID_CANCEL);
		return;
	}

	wxString sMessage;
	
	if(event.GetDownLoadStatus() == DOWNLOAD_FAIL)
	{
		wxRemoveFile(m_sFile);
		m_bDownloading = false;
		m_btnGetUpdate->Enable(true);
		sMessage = wxString::Format(_("downloading '%s' failed."));
	}
	
	if(event.GetDownLoadStatus() == DOWNLOAD_INPROGRESS)
	{
		wxInt64 nFileSize = event.GetFileSize();
		wxInt64 nDownloaded = event.GetDownLoadedBytesCount();

		if(nFileSize != 0)
		{
			int percent = nDownloaded*100/nFileSize;
			m_Progress->SetValue(percent);

			double nFileSizeMB = nFileSize/(1024.0*1024.0);
			double nDownloadedMB = nDownloaded/(1024.0*1024.0);

			sMessage = wxString::Format(_("downloading '%s'...\n%.1f of %.1f MB."),  m_sFile.c_str(), nDownloadedMB, nFileSizeMB);
		}
		else
		{
			int percent = m_Progress->GetValue();
			percent+=10;
			m_Progress->SetValue(percent%100);
			sMessage = wxString::Format(_("downloading '%s'..."),  m_sFile.c_str());
		}
	}

	m_stMessage->SetLabel(sMessage);	
}