~psiphon-inc/+junk/psiphon-1.6

« back to all changes in this revision

Viewing changes to psiphonPanel/UpdateDlg.cpp

  • Committer: jamyang
  • Date: 2009-05-14 20:59:48 UTC
  • Revision ID: jamyang@jamyangs-mac-mini.local-20090514205948-n3wp8io1e0uqwbp7
psiphon-1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//      Copyright (c) CitizenLab, 2006. All Rights Reserved.
 
3
//      The latest version of this code is available at http://psiphon.civisec.org/
 
4
//
 
5
//      This software is open source; you can redistribute it and/or modify it 
 
6
//      under the terms of the GNU General Public License as published by the 
 
7
//      Free Software Foundation; either version 2 of the License, or (at your 
 
8
//      option) any later version.
 
9
//
 
10
//      This program is distributed WITHOUT ANY WARRANTY; without even the 
 
11
//      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 
12
//      See the GNU General Public License for more details at:
 
13
//      http://psiphon.civisec.org/
 
14
//      
 
15
//      This General Public License does NOT permit incorporating this software 
 
16
//      into proprietary programs.
 
17
////////////////////////////////////////////////////////////////////////////////
 
18
 
 
19
#include "stdwx.h"
 
20
#include "resources.h"
 
21
#include "UpdateDlg.h"
 
22
#include "GetIP.h"
 
23
#include "DownloadFile.h"
 
24
 
 
25
#ifdef _DEBUG
 
26
#define new DEBUG_NEW
 
27
#endif
 
28
 
 
29
wxString wxUpdateDlg::GetUpdateFileName(const wxString& sVersion)
 
30
{
 
31
        return wxString::Format(_T("psiphon-%s.msi"), sVersion.c_str());
 
32
}
 
33
 
 
34
wxString wxUpdateDlg::GetUpdatePath(const wxString& sVersion)
 
35
{
 
36
        return wxString::Format(_T("http://psiphon.ca/download/%s"), GetUpdateFileName(sVersion).c_str());
 
37
}
 
38
 
 
39
bool wxUpdateDlg::CheckForUpdates(wxWindow* parent, bool bShowNoUpdates)
 
40
{
 
41
        wxBusyCursor wait;
 
42
 
 
43
        wxString sLatestVersion = GetLatestVersion();
 
44
        wxString sCurrentVersion = PSIPHON_VERSION;
 
45
 
 
46
        if(sLatestVersion == _T(""))
 
47
                return false;
 
48
 
 
49
        if(sLatestVersion > sCurrentVersion)
 
50
        {
 
51
                wxUpdateDlg dlg(sLatestVersion, sCurrentVersion, parent);
 
52
                
 
53
                int nRes = 0;
 
54
                nRes = dlg.ShowModal();
 
55
 
 
56
#ifdef _WIN32
 
57
                if(nRes == wxID_OK)
 
58
                {
 
59
                        wxString sFile = GetUpdateFileName(sLatestVersion);
 
60
                        ::ShellExecute(NULL, _T("open"), sFile, NULL, NULL, SW_SHOWNORMAL);
 
61
                }
 
62
#endif
 
63
        
 
64
        }
 
65
        else if(bShowNoUpdates)
 
66
        {
 
67
                wxString sMess = wxString::Format(_("Your software is up to date."));
 
68
                wxMessageBox(sMess, _("Checking for updates"));         
 
69
        }
 
70
 
 
71
        return true;
 
72
}
 
73
 
 
74
IMPLEMENT_DYNAMIC_CLASS(wxUpdateDlg, wxDialog)
 
75
 
 
76
enum
 
77
{
 
78
        IDC_GET_UPDATE = 10001,
 
79
        IDC_PROGRESS,
 
80
        IDC_MESSAGE,
 
81
};
 
82
 
 
83
wxUpdateDlg::wxUpdateDlg()
 
84
{
 
85
}
 
86
 
 
87
wxUpdateDlg::wxUpdateDlg(const wxString& _sLatestVersion, const wxString& _sCurrentVersion, wxWindow* parent)
 
88
{
 
89
        m_sLatestVersion = _sLatestVersion;
 
90
        m_sCurrentVersion = _sCurrentVersion;
 
91
 
 
92
        m_sFile = GetUpdateFileName(m_sLatestVersion);
 
93
        m_sPath = GetUpdatePath(m_sLatestVersion);
 
94
 
 
95
        m_pDownloadFile = NULL;
 
96
        m_bDownloading = false;
 
97
 
 
98
        Create(parent);
 
99
}
 
100
 
 
101
wxUpdateDlg::~wxUpdateDlg()
 
102
{
 
103
}
 
104
 
 
105
bool wxUpdateDlg::Create(wxWindow* parent)
 
106
{
 
107
        SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
 
108
        wxDialog::Create(parent, IDD_UPDATE, _("Checking for updates"), wxDefaultPosition, wxSize(400, 300), wxCAPTION|wxSYSTEM_MENU|wxCLOSE_BOX|wxDIALOG_MODAL);
 
109
 
 
110
        CreateControls();
 
111
 
 
112
        if(GetSizer())
 
113
        {
 
114
                GetSizer()->SetSizeHints(this);
 
115
        }
 
116
 
 
117
        Centre();
 
118
 
 
119
        return true;
 
120
}
 
121
 
 
122
#ifdef _WIN32
 
123
void wxUpdateDlg::CreateControls()
 
124
{
 
125
        wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
 
126
        SetSizer(itemBoxSizer2);
 
127
 
 
128
        wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
 
129
        itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
130
 
 
131
        wxString sMess = wxString::Format(_("Newer version of psiphon is available.\n"
 
132
                                                                                "Would you like to download it?"));
 
133
 
 
134
        m_stMessage = new wxStaticText(this, IDC_MESSAGE, sMess, wxDefaultPosition, wxDefaultSize, 0);
 
135
        itemBoxSizer3->Add(m_stMessage, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
136
 
 
137
        wxSize mine = wxDefaultSize;
 
138
        mine.SetHeight(20);
 
139
        mine.SetWidth(200);
 
140
 
 
141
        m_Progress = new wxGauge(this, IDC_PROGRESS, 100, wxDefaultPosition, mine);
 
142
        itemBoxSizer3->Add(m_Progress, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
143
 
 
144
        wxBoxSizer* itemBoxSizerButtons = new wxBoxSizer(wxHORIZONTAL);
 
145
        itemBoxSizer3->Add(itemBoxSizerButtons, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
146
 
 
147
        m_btnGetUpdate = new wxButton(this, IDC_GET_UPDATE, _("&Get update"), wxDefaultPosition, wxDefaultSize, 0);
 
148
        m_btnGetUpdate->SetDefault();
 
149
        itemBoxSizerButtons->Add(m_btnGetUpdate, 0,  wxALIGN_CENTER_VERTICAL | wxALL, 5);
 
150
 
 
151
        wxButton* itemButton18 = new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0);
 
152
        itemBoxSizerButtons->Add(itemButton18, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
153
}
 
154
#else
 
155
 
 
156
void wxUpdateDlg::CreateControls()
 
157
{
 
158
        wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
 
159
        SetSizer(itemBoxSizer2);
 
160
 
 
161
        wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
 
162
        itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
163
 
 
164
        wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
 
165
        itemBoxSizer3->Add(itemBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
166
//
 
167
 
 
168
        wxString sMess = wxString::Format(
 
169
        _("Checking if a newer version of psiphon is available...\n\n"
 
170
        "Your current version is %s\n"
 
171
        "Latest version is %s."), m_sCurrentVersion.c_str(), m_sLatestVersion.c_str());
 
172
 
 
173
        wxStaticText* itemStaticText11 = new wxStaticText(this, wxID_STATIC, sMess, wxDefaultPosition, wxDefaultSize, 0);
 
174
        itemBoxSizer4->Add(itemStaticText11, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
175
 
 
176
        wxBoxSizer* itemBoxSizerLink = new wxBoxSizer(wxHORIZONTAL);
 
177
        itemBoxSizer4->Add(itemBoxSizerLink, 0, wxALIGN_LEFT | wxALL, 5);
 
178
        
 
179
        wxStaticText* itemStaticText12 = new wxStaticText(this, wxID_STATIC, _("Please go to"), wxDefaultPosition, wxDefaultSize, 0);
 
180
        itemBoxSizerLink->Add(itemStaticText12, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
181
 
 
182
        wxString sLink = _T("http://psiphon.civisec.org/");
 
183
        wxHyperlinkCtrl* pHyperlink = new wxHyperlinkCtrl(this, wxID_STATIC, sLink, sLink, wxDefaultPosition, wxDefaultSize, wxHL_CONTEXTMENU | wxNO_BORDER | wxHL_ALIGN_LEFT);
 
184
        itemBoxSizerLink->Add(pHyperlink, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
185
        
 
186
        wxStaticText* itemStaticText13 = new wxStaticText(this, wxID_STATIC, _("to download the current version."), wxDefaultPosition, wxDefaultSize, 0);
 
187
        itemBoxSizer4->Add(itemStaticText13, 0, wxALIGN_CENTER_VERTICAL | wxALL | wxADJUST_MINSIZE, 5);
 
188
 
 
189
        wxButton* itemButton17 = new wxButton(this, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0);
 
190
        itemButton17->SetDefault();
 
191
        itemBoxSizer3->Add(itemButton17, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
 
192
}
 
193
#endif
 
194
 
 
195
BEGIN_EVENT_TABLE(wxUpdateDlg, wxDialog)
 
196
        EVT_BUTTON(IDC_GET_UPDATE, wxUpdateDlg::OnGetUpdate)
 
197
        EVT_BUTTON(wxID_CANCEL, wxUpdateDlg::OnCancel)
 
198
    EVT_DOWNLOAD(wxUpdateDlg::OnDownloadEvent)
 
199
END_EVENT_TABLE()
 
200
 
 
201
void wxUpdateDlg::OnGetUpdate(wxCommandEvent&)
 
202
{
 
203
        m_btnGetUpdate->Enable(false);
 
204
        
 
205
        wxRemoveFile(m_sFile);
 
206
 
 
207
        m_pDownloadFile = new wxDownloadFile(this, m_sPath, m_sFile, true, 1024*100); 
 
208
    wxString sMessage;
 
209
 
 
210
        if(m_pDownloadFile)
 
211
        {
 
212
                sMessage = wxString::Format(_("downloading '%s'..."), m_sFile.c_str()); 
 
213
                m_bDownloading = true;
 
214
        }
 
215
        else
 
216
        {
 
217
                sMessage = wxString::Format(_("downloading '%s' failed."),  m_sFile.c_str());
 
218
                m_bDownloading = false;
 
219
                m_btnGetUpdate->Enable(true);
 
220
        }
 
221
 
 
222
        m_stMessage->SetLabel(sMessage);        
 
223
}
 
224
 
 
225
 
 
226
void wxUpdateDlg::OnCancel(wxCommandEvent&)
 
227
{
 
228
        if(m_pDownloadFile && m_bDownloading)
 
229
        {
 
230
                m_bDownloading = false;
 
231
                m_pDownloadFile->CancelDownload();
 
232
        }
 
233
        else
 
234
        {
 
235
                EndModal(wxID_CANCEL);
 
236
        }
 
237
}
 
238
 
 
239
void wxUpdateDlg::OnDownloadEvent(wxDownloadEvent &event)
 
240
{
 
241
        if(event.GetDownLoadStatus() == DOWNLOAD_COMPLETE)
 
242
        {
 
243
                m_bDownloading = false;
 
244
                EndModal(wxID_OK);
 
245
                return;
 
246
        }
 
247
        
 
248
        if(event.GetDownLoadStatus() == DOWNLOAD_CANCELLED)
 
249
        {
 
250
                wxRemoveFile(m_sFile);
 
251
                m_bDownloading = false;
 
252
                EndModal(wxID_CANCEL);
 
253
                return;
 
254
        }
 
255
 
 
256
        wxString sMessage;
 
257
        
 
258
        if(event.GetDownLoadStatus() == DOWNLOAD_FAIL)
 
259
        {
 
260
                wxRemoveFile(m_sFile);
 
261
                m_bDownloading = false;
 
262
                m_btnGetUpdate->Enable(true);
 
263
                sMessage = wxString::Format(_("downloading '%s' failed."));
 
264
        }
 
265
        
 
266
        if(event.GetDownLoadStatus() == DOWNLOAD_INPROGRESS)
 
267
        {
 
268
                wxInt64 nFileSize = event.GetFileSize();
 
269
                wxInt64 nDownloaded = event.GetDownLoadedBytesCount();
 
270
 
 
271
                if(nFileSize != 0)
 
272
                {
 
273
                        int percent = nDownloaded*100/nFileSize;
 
274
                        m_Progress->SetValue(percent);
 
275
 
 
276
                        double nFileSizeMB = nFileSize/(1024.0*1024.0);
 
277
                        double nDownloadedMB = nDownloaded/(1024.0*1024.0);
 
278
 
 
279
                        sMessage = wxString::Format(_("downloading '%s'...\n%.1f of %.1f MB."),  m_sFile.c_str(), nDownloadedMB, nFileSizeMB);
 
280
                }
 
281
                else
 
282
                {
 
283
                        int percent = m_Progress->GetValue();
 
284
                        percent+=10;
 
285
                        m_Progress->SetValue(percent%100);
 
286
                        sMessage = wxString::Format(_("downloading '%s'..."),  m_sFile.c_str());
 
287
                }
 
288
        }
 
289
 
 
290
        m_stMessage->SetLabel(sMessage);        
 
291
 
292