1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17 //
18 #if defined(__GNUG__) && !defined(__APPLE__)
19 #pragma implementation "ProjectPropertiesPage.h"
20 #endif
21 
22 #include "stdwx.h"
23 #include "network.h"
24 #include "diagnostics.h"
25 #include "util.h"
26 #include "mfile.h"
27 #include "miofile.h"
28 #include "parse.h"
29 #include "error_numbers.h"
30 #include "wizardex.h"
31 #include "error_numbers.h"
32 #include "BOINCGUIApp.h"
33 #include "SkinManager.h"
34 #include "MainDocument.h"
35 #include "BOINCBaseWizard.h"
36 #include "WizardAttach.h"
37 #include "ProjectPropertiesPage.h"
38 #include "ProjectInfoPage.h"
39 #include "CompletionErrorPage.h"
40 #include "TermsOfUsePage.h"
41 
42 ////@begin XPM images
43 #include "res/wizprogress01.xpm"
44 #include "res/wizprogress02.xpm"
45 #include "res/wizprogress03.xpm"
46 #include "res/wizprogress04.xpm"
47 #include "res/wizprogress05.xpm"
48 #include "res/wizprogress06.xpm"
49 #include "res/wizprogress07.xpm"
50 #include "res/wizprogress08.xpm"
51 #include "res/wizprogress09.xpm"
52 #include "res/wizprogress10.xpm"
53 #include "res/wizprogress11.xpm"
54 #include "res/wizprogress12.xpm"
55 ////@end XPM images
56 
57 /*!
58  * CProjectPropertiesPage custom event definition
59  */
60 
61 DEFINE_EVENT_TYPE(wxEVT_PROJECTPROPERTIES_STATECHANGE)
62 
63 /*!
64  * CProjectPropertiesPage type definition
65  */
66 
IMPLEMENT_DYNAMIC_CLASS(CProjectPropertiesPage,wxWizardPageEx)67 IMPLEMENT_DYNAMIC_CLASS( CProjectPropertiesPage, wxWizardPageEx )
68 
69 /*!
70  * CProjectPropertiesPage event table definition
71  */
72 
73 BEGIN_EVENT_TABLE( CProjectPropertiesPage, wxWizardPageEx )
74 
75     EVT_PROJECTPROPERTIES_STATECHANGE( CProjectPropertiesPage::OnStateChange )
76 
77 ////@begin CProjectPropertiesPage event table entries
78     EVT_WIZARDEX_PAGE_CHANGED( -1, CProjectPropertiesPage::OnPageChanged )
79     EVT_WIZARDEX_CANCEL( -1, CProjectPropertiesPage::OnCancel )
80 
81 ////@end CProjectPropertiesPage event table entries
82 
83 END_EVENT_TABLE()
84 
85 /*!
86  * CProjectPropertiesPage constructors
87  */
88 
89 CProjectPropertiesPage::CProjectPropertiesPage( )
90 {
91 }
92 
CProjectPropertiesPage(CBOINCBaseWizard * parent)93 CProjectPropertiesPage::CProjectPropertiesPage( CBOINCBaseWizard* parent )
94 {
95     Create( parent );
96 }
97 
98 /*!
99  * WizardPage creator
100  */
101 
Create(CBOINCBaseWizard * parent)102 bool CProjectPropertiesPage::Create( CBOINCBaseWizard* parent )
103 {
104 ////@begin CProjectPropertiesPage member initialisation
105     m_pTitleStaticCtrl = NULL;
106     m_pProgressIndicator = NULL;
107 ////@end CProjectPropertiesPage member initialisation
108 
109     m_bProjectPropertiesSucceeded = false;
110     m_bProjectPropertiesURLFailure = false;
111     m_bProjectPropertiesCommunicationFailure = false;
112     m_bProjectAccountCreationDisabled = false;
113     m_bProjectClientAccountCreationDisabled = false;
114     m_bNetworkConnectionNotDetected = false;
115     m_bServerReportedError = false;
116     m_bTermsOfUseRequired = true;
117     m_iBitmapIndex = 0;
118     m_iCurrentState = PROJPROP_INIT;
119 
120 ////@begin CProjectPropertiesPage creation
121     wxWizardPageEx::Create( parent, ID_PROJECTPROPERTIESPAGE );
122 
123     CreateControls();
124     GetSizer()->Fit(this);
125 ////@end CProjectPropertiesPage creation
126 
127     return TRUE;
128 }
129 
130 /*!
131  * Control creation for WizardPage
132  */
133 
CreateControls()134 void CProjectPropertiesPage::CreateControls()
135 {
136 ////@begin CProjectPropertiesPage content construction
137     CProjectPropertiesPage* itemWizardPage36 = this;
138 
139     wxBoxSizer* itemBoxSizer37 = new wxBoxSizer(wxVERTICAL);
140     itemWizardPage36->SetSizer(itemBoxSizer37);
141 
142     m_pTitleStaticCtrl = new wxStaticText;
143     m_pTitleStaticCtrl->Create( itemWizardPage36, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
144     m_pTitleStaticCtrl->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, FALSE, _T("Verdana")));
145     itemBoxSizer37->Add(m_pTitleStaticCtrl, 0, wxALIGN_LEFT|wxALL, 5);
146 
147     itemBoxSizer37->Add(5, 80, 0, wxALIGN_LEFT|wxALL, 5);
148 
149     wxFlexGridSizer* itemFlexGridSizer40 = new wxFlexGridSizer(1, 3, 0, 0);
150     itemFlexGridSizer40->AddGrowableRow(0);
151     itemFlexGridSizer40->AddGrowableCol(0);
152     itemFlexGridSizer40->AddGrowableCol(1);
153     itemFlexGridSizer40->AddGrowableCol(2);
154     itemBoxSizer37->Add(itemFlexGridSizer40, 0, wxGROW|wxALL, 5);
155 
156     itemFlexGridSizer40->Add(5, 5, 0, wxGROW|wxALL, 5);
157 
158     wxBitmap itemBitmap41(GetBitmapResource(wxT("res/wizprogress01.xpm")));
159     m_pProgressIndicator = new wxStaticBitmap;
160     m_pProgressIndicator->Create( itemWizardPage36, ID_PROGRESSCTRL, itemBitmap41, wxDefaultPosition, wxSize(ADJUSTFORXDPI(184), ADJUSTFORYDPI(48)), 0 );
161     itemFlexGridSizer40->Add(m_pProgressIndicator, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
162 
163     itemFlexGridSizer40->Add(5, 5, 0, wxGROW|wxALL, 5);
164 ////@end CProjectPropertiesPage content construction
165 }
166 
167 /*!
168  * Gets the previous page.
169  */
170 
GetPrev() const171 wxWizardPageEx* CProjectPropertiesPage::GetPrev() const
172 {
173     return PAGE_TRANSITION_BACK;
174 }
175 
176 /*!
177  * Gets the next page.
178  */
179 
GetNext() const180 wxWizardPageEx* CProjectPropertiesPage::GetNext() const
181 {
182     if (CHECK_CLOSINGINPROGRESS()) {
183         // Cancel Event Detected
184         return PAGE_TRANSITION_NEXT(ID_COMPLETIONERRORPAGE);
185     } else if (GetProjectPropertiesSucceeded() && GetTermsOfUseRequired()) {
186         // Terms of Use are required before requesting account information
187         return PAGE_TRANSITION_NEXT(ID_TERMSOFUSEPAGE);
188     } else if (GetProjectPropertiesSucceeded() && GetCredentialsAlreadyAvailable()) {
189         // Credentials are already available, do whatever we need to do.
190         return PAGE_TRANSITION_NEXT(ID_PROJECTPROCESSINGPAGE);
191     } else if (GetProjectPropertiesSucceeded()) {
192         // We were successful in retrieving the project properties
193         return PAGE_TRANSITION_NEXT(ID_ACCOUNTINFOPAGE);
194     } else if (GetProjectPropertiesCommunicationFailure() && GetNetworkConnectionNotDetected()) {
195         // No Internet Connection
196         return PAGE_TRANSITION_NEXT(ID_ERRPROXYINFOPAGE);
197     } else if (GetProjectPropertiesURLFailure()) {
198         // Not a BOINC based project
199         return PAGE_TRANSITION_NEXT(ID_ERRNOTDETECTEDPAGE);
200     } else if (GetServerReportedError()) {
201         // Server reported an error, display the error
202         return PAGE_TRANSITION_NEXT(ID_COMPLETIONERRORPAGE);
203     } else {
204         // The project must be down for maintenance
205         return PAGE_TRANSITION_NEXT(ID_ERRUNAVAILABLEPAGE);
206     }
207     return NULL;
208 }
209 
210 /*!
211  * Should we show tooltips?
212  */
213 
ShowToolTips()214 bool CProjectPropertiesPage::ShowToolTips()
215 {
216     return TRUE;
217 }
218 
StartProgress(wxStaticBitmap * pBitmap)219 void CProjectPropertiesPage::StartProgress(wxStaticBitmap* pBitmap) {
220     m_iBitmapIndex = 1;
221     pBitmap->SetBitmap(GetBitmapResource(wxT("res/wizprogress01.xpm")));
222 }
223 
IncrementProgress(wxStaticBitmap * pBitmap)224 void CProjectPropertiesPage::IncrementProgress(wxStaticBitmap* pBitmap) {
225     m_iBitmapIndex += 1;
226     if (12 < m_iBitmapIndex) m_iBitmapIndex = 1;
227 
228     wxString str;
229     str.Printf(wxT("res/wizprogress%02d.xpm"), m_iBitmapIndex);
230 
231     pBitmap->SetBitmap(GetBitmapResource(str));
232     Update();
233 }
234 
FinishProgress(wxStaticBitmap * pBitmap)235 void CProjectPropertiesPage::FinishProgress(wxStaticBitmap* pBitmap) {
236     m_iBitmapIndex = 12;
237     pBitmap->SetBitmap(GetBitmapResource(wxT("res/wizprogress12.xpm")));
238 }
239 
240 /*!
241  * Get bitmap resources
242  */
243 
GetBitmapResource(const wxString & name)244 wxBitmap CProjectPropertiesPage::GetBitmapResource( const wxString& name )
245 {
246 // TODO: Choose from multiple size images if provided, else resize the closest one
247     // Bitmap retrieval
248     if (name == wxT("res/wizprogress01.xpm"))
249     {
250         wxBitmap bitmap(GetScaledBitmapFromXPMData(wizprogress01_xpm));
251         return bitmap;
252     }
253     else if (name == wxT("res/wizprogress02.xpm"))
254     {
255         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress02_xpm));
256         return bitmap;
257     }
258     else if (name == wxT("res/wizprogress03.xpm"))
259     {
260         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress03_xpm));
261         return bitmap;
262     }
263     else if (name == wxT("res/wizprogress04.xpm"))
264     {
265         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress04_xpm));
266         return bitmap;
267     }
268     else if (name == wxT("res/wizprogress05.xpm"))
269     {
270         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress05_xpm));
271         return bitmap;
272     }
273     else if (name == wxT("res/wizprogress06.xpm"))
274     {
275         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress06_xpm));
276         return bitmap;
277     }
278     else if (name == wxT("res/wizprogress07.xpm"))
279     {
280         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress07_xpm));
281         return bitmap;
282     }
283     else if (name == wxT("res/wizprogress08.xpm"))
284     {
285         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress08_xpm));
286         return bitmap;
287     }
288     else if (name == wxT("res/wizprogress09.xpm"))
289     {
290         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress09_xpm));
291         return bitmap;
292     }
293     else if (name == wxT("res/wizprogress10.xpm"))
294     {
295         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress10_xpm));
296         return bitmap;
297     }
298     else if (name == wxT("res/wizprogress11.xpm"))
299     {
300         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress11_xpm));
301         return bitmap;
302     }
303     else if (name == wxT("res/wizprogress12.xpm"))
304     {
305         wxBitmap  bitmap(GetScaledBitmapFromXPMData(wizprogress12_xpm));
306         return bitmap;
307     }
308     return wxNullBitmap;
309 }
310 
311 /*!
312  * Get icon resources
313  */
314 
GetIconResource(const wxString & WXUNUSED (name))315 wxIcon CProjectPropertiesPage::GetIconResource( const wxString& WXUNUSED(name) )
316 {
317     // Icon retrieval
318 ////@begin CProjectPropertiesPage icon retrieval
319     return wxNullIcon;
320 ////@end CProjectPropertiesPage icon retrieval
321 }
322 
323 /*!
324  * wxEVT_WIZARD_PAGE_CHANGED event handler for ID_PROJECTPROPERTIESPAGE
325  */
326 
OnPageChanged(wxWizardExEvent & event)327 void CProjectPropertiesPage::OnPageChanged( wxWizardExEvent& event ) {
328     if (event.GetDirection() == false) return;
329 
330     wxASSERT(m_pTitleStaticCtrl);
331     wxASSERT(m_pProgressIndicator);
332 
333     m_pTitleStaticCtrl->SetLabel(
334         _("Communicating with project\nPlease wait...")
335     );
336 
337     SetProjectPropertiesSucceeded(false);
338     SetProjectPropertiesURLFailure(false);
339     SetProjectPropertiesCommunicationFailure(false);
340     SetProjectAccountCreationDisabled(false);
341     SetProjectClientAccountCreationDisabled(false);
342     SetNetworkConnectionNotDetected(false);
343     SetNextState(PROJPROP_INIT);
344 
345     CProjectPropertiesPageEvent TransitionEvent(wxEVT_PROJECTPROPERTIES_STATECHANGE, this);
346     AddPendingEvent(TransitionEvent);
347 
348     Fit();
349 }
350 
351 /*!
352  * wxEVT_WIZARD_CANCEL event handler for ID_PROJECTPROPERTIESPAGE
353  */
354 
OnCancel(wxWizardExEvent & event)355 void CProjectPropertiesPage::OnCancel( wxWizardExEvent& event ) {
356     PROCESS_CANCELEVENT(event);
357 }
358 
359 /*!
360  * wxEVT_PROJECTPROPERTIES_STATECHANGE event handler for ID_PROJECTPROPERTIESPAGE
361  */
362 
OnStateChange(CProjectPropertiesPageEvent & WXUNUSED (event))363 void CProjectPropertiesPage::OnStateChange( CProjectPropertiesPageEvent& WXUNUSED(event) )
364 {
365     CMainDocument* pDoc = wxGetApp().GetDocument();
366     CWizardAttach* pWAP = ((CWizardAttach*)GetParent());
367     PROJECT_CONFIG* pc  = &pWAP->project_config;
368     CC_STATUS status;
369     wxDateTime dtStartExecutionTime;
370     wxDateTime dtCurrentExecutionTime;
371     wxTimeSpan tsExecutionTime;
372     wxString strBuffer = wxEmptyString;
373     bool bPostNewEvent = true;
374     int  iReturnValue = 0;
375 
376     wxASSERT(pDoc);
377     wxASSERT(wxDynamicCast(pDoc, CMainDocument));
378 
379     switch(GetCurrentState()) {
380         case PROJPROP_INIT:
381             pWAP->DisableNextButton();
382             pWAP->DisableBackButton();
383             StartProgress(m_pProgressIndicator);
384             SetNextState(PROJPROP_RETRPROJECTPROPERTIES_BEGIN);
385             break;
386         case PROJPROP_RETRPROJECTPROPERTIES_BEGIN:
387             SetNextState(PROJPROP_RETRPROJECTPROPERTIES_EXECUTE);
388             break;
389         case PROJPROP_RETRPROJECTPROPERTIES_EXECUTE:
390             // Attempt to retrieve the project's account creation policies
391 
392             // Wait until we are done processing the request.
393             dtStartExecutionTime = wxDateTime::Now();
394             dtCurrentExecutionTime = wxDateTime::Now();
395             tsExecutionTime = dtCurrentExecutionTime - dtStartExecutionTime;
396             iReturnValue = 0;
397             pc->clear();
398             pc->error_num = ERR_RETRY;
399             while (
400                 !iReturnValue &&
401                 ((ERR_IN_PROGRESS == pc->error_num) || (ERR_RETRY == pc->error_num)) &&
402                 tsExecutionTime.GetSeconds() <= 60 &&
403                 !CHECK_CLOSINGINPROGRESS()
404             ) {
405                 if (ERR_RETRY == pc->error_num) {
406                     pDoc->rpc.get_project_config(
407                         (const char*)pWAP->GetProjectURL().mb_str()
408                     );
409                 }
410 
411                 dtCurrentExecutionTime = wxDateTime::Now();
412                 tsExecutionTime = dtCurrentExecutionTime - dtStartExecutionTime;
413                 iReturnValue = pDoc->rpc.get_project_config_poll(*pc);
414                 IncrementProgress(m_pProgressIndicator);
415 
416                 ::wxMilliSleep(500);
417                 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_USER_INPUT);
418             }
419 
420             if (
421                 !iReturnValue
422                 && (!pc->error_num || pc->error_num == ERR_ACCT_CREATION_DISABLED)
423             ) {
424                 // We either successfully retrieved the project's
425                 // account creation policies or we were able to talk
426                 // to the web server and found out they do not support
427                 // account creation through the wizard.
428                 // In either case, claim success and set the correct flags
429                 // to show the correct 'next' page.
430                 //
431                 SetProjectPropertiesSucceeded(true);
432                 SetProjectAccountCreationDisabled(pc->account_creation_disabled);
433                 SetProjectClientAccountCreationDisabled(pc->client_account_creation_disabled);
434                 SetTermsOfUseRequired(!pc->terms_of_use.empty());
435 
436             } else {
437 
438                 SetProjectPropertiesSucceeded(false);
439                 SetProjectPropertiesURLFailure(pc->error_num == ERR_HTTP_PERMANENT);
440 
441                 bool comm_failure = !iReturnValue && (
442                     (ERR_GETHOSTBYNAME == pc->error_num)
443                     || (ERR_CONNECT == pc->error_num)
444                     || (ERR_XML_PARSE == pc->error_num)
445                     || (ERR_PROJECT_DOWN == pc->error_num)
446                 );
447                 SetProjectPropertiesCommunicationFailure(comm_failure);
448 
449                 bool server_reported_error = !iReturnValue && (
450                     (ERR_HTTP_PERMANENT != pc->error_num)
451                     && (ERR_GETHOSTBYNAME != pc->error_num)
452                     && (ERR_CONNECT != pc->error_num)
453                     && (ERR_XML_PARSE != pc->error_num)
454                     && (ERR_PROJECT_DOWN != pc->error_num)
455                 );
456                 SetServerReportedError(server_reported_error);
457 
458                 if (server_reported_error) {
459                     strBuffer = pWAP->m_CompletionErrorPage->m_pServerMessagesCtrl->GetLabel();
460 				    if (pc->error_msg.size()) {
461                         strBuffer += wxString(pc->error_msg.c_str(), wxConvUTF8) + wxString(wxT("\n"));
462                     }
463                     pWAP->m_CompletionErrorPage->m_pServerMessagesCtrl->SetLabel(strBuffer);
464                 }
465             }
466 
467             SetNextState(PROJPROP_DETERMINENETWORKSTATUS_BEGIN);
468             break;
469         case PROJPROP_DETERMINENETWORKSTATUS_BEGIN:
470             SetNextState(PROJPROP_DETERMINENETWORKSTATUS_EXECUTE);
471             break;
472         case PROJPROP_DETERMINENETWORKSTATUS_EXECUTE:
473             // Attempt to determine if we are even connected to a network
474 
475             // Wait until we are done processing the request.
476             dtStartExecutionTime = wxDateTime::Now();
477             dtCurrentExecutionTime = wxDateTime::Now();
478             tsExecutionTime = dtCurrentExecutionTime - dtStartExecutionTime;
479             iReturnValue = 0;
480             status.network_status = NETWORK_STATUS_LOOKUP_PENDING;
481             while ((!iReturnValue && (NETWORK_STATUS_LOOKUP_PENDING == status.network_status)) &&
482                    tsExecutionTime.GetSeconds() <= 60 &&
483                    !CHECK_CLOSINGINPROGRESS()
484                   )
485             {
486                 dtCurrentExecutionTime = wxDateTime::Now();
487                 tsExecutionTime = dtCurrentExecutionTime - dtStartExecutionTime;
488                 iReturnValue = pDoc->GetCoreClientStatus(status);
489                 IncrementProgress(m_pProgressIndicator);
490 
491                 ::wxMilliSleep(500);
492                 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_USER_INPUT);
493             }
494 
495             SetNetworkConnectionNotDetected(NETWORK_STATUS_WANT_CONNECTION == status.network_status);
496 
497             SetNextState(PROJPROP_DETERMINEACCOUNTINFOSTATUS_BEGIN);
498             break;
499         case PROJPROP_DETERMINEACCOUNTINFOSTATUS_BEGIN:
500             SetNextState(PROJPROP_DETERMINEACCOUNTINFOSTATUS_EXECUTE);
501             break;
502         case PROJPROP_DETERMINEACCOUNTINFOSTATUS_EXECUTE:
503             // Determine if the account settings are already pre-populated.
504             //   If so, advance to the Project Processing page.
505             SetCredentialsAlreadyAvailable(pWAP->IsCredentialsCached() || pWAP->IsCredentialsDetected() || pWAP->GetProjectSetupCookie().size());
506 
507             SetNextState(PROJPROP_CLEANUP);
508             break;
509         case PROJPROP_CLEANUP:
510             FinishProgress(m_pProgressIndicator);
511             SetNextState(PROJPROP_END);
512             break;
513         default:
514             // Allow a glimps of what the result was before advancing to the next page.
515             wxSleep(1);
516             pWAP->EnableNextButton();
517             pWAP->EnableBackButton();
518             pWAP->SimulateNextButton();
519             bPostNewEvent = false;
520             break;
521     }
522 
523     Update();
524 
525     if (bPostNewEvent && !CHECK_CLOSINGINPROGRESS()) {
526         CProjectPropertiesPageEvent TransitionEvent(wxEVT_PROJECTPROPERTIES_STATECHANGE, this);
527         AddPendingEvent(TransitionEvent);
528     }
529 }
530 
531