Statistics
| Revision:

root / branches / v10 / install / launcher / izpack-launcher-1.3 / src / launcher.cpp @ 9314

History | View | Annotate | Download (27.8 KB)

1
/* Copyright (c) 2004 Julien Ponge - All rights reserved.
2
 * Some windows 98 debugging done by Dustin Sacks.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#include <wx/string.h>
23
#include <wx/file.h>
24
#include <wx/filename.h>
25
#include <wx/url.h>
26
#include <wx/process.h>
27

    
28
#include "launcher.h"
29

    
30
#include "myIniReader.h"
31

    
32
#ifdef __UNIX__
33
  #include <stdlib.h>
34
  #include <map>
35
  #include <list>
36
#endif
37

    
38

    
39
/*
40
 * Helper function to run an external program. It takes care of the subtule
41
 * differences between the OS-specific implementations.
42
 *
43
 * @param cmd The command to run.
44
 * @return <code>true</code> in case of a success, <code>false</code> otherwise.
45
 */
46
bool run_external(wxString cmd)
47
{
48
 int code = wxExecute(cmd, wxEXEC_SYNC);
49
 return (code == 0);
50

    
51
}
52

    
53
bool run_external_async(wxString cmd)
54
{
55
 return (wxExecute(cmd, wxEXEC_ASYNC) != 0);
56
}
57

    
58
//wxString escape_backSlash(wxString aStr){
59
//  aStr.Replace("\\","\\\\");
60
//  return aStr;
61
//}
62

    
63
bool string_to_bool(wxString value, bool defaultValue){
64
 bool returnValue = defaultValue;
65
 if (value != wxEmptyString)
66
    if (
67
       value.CmpNoCase("s")   == 0  ||
68
       value.CmpNoCase("si")  == 0  ||
69
       value.CmpNoCase("1")   == 0  ||
70
       value.CmpNoCase("y")   == 0  ||
71
       value.CmpNoCase("yes") == 0
72
       ) 
73
    {
74
       returnValue = true;
75
    } else {
76
       returnValue = false;
77
    }
78
  return returnValue;
79
}
80

    
81
long LAUNCHAPP_TIMER_ID = wxNewId();
82

    
83
BEGIN_EVENT_TABLE(LauncherApp, wxApp)
84
    EVT_TIMER(LAUNCHAPP_TIMER_ID, LauncherApp::OnTimer)
85
END_EVENT_TABLE()
86

    
87

    
88
/* Main Application $Revision: 9314 $
89
 *
90
 * $Id: launcher.cpp 9314 2006-12-14 12:12:36Z jmvivo $
91
 */
92
LauncherApp::LauncherApp()
93
  : wxApp()
94
{
95
  APPLICATION_NAME = wxString(_("Launcher"));
96
  completed = false;
97

    
98
  SetAppName(_(APPLICATION_NAME));
99
  //loadParams();
100
}
101

    
102
void LauncherApp::echo(const wxString &msg) 
103
{
104
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK);
105
  dlg.ShowModal();
106
}
107

    
108
void LauncherApp::notifyToUser(const wxString &msg) 
109
{
110
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK | wxICON_INFORMATION);
111
  dlg.ShowModal();
112
}
113

    
114

    
115
LauncherApp::~LauncherApp()
116
{
117

    
118
}
119

    
120
void LauncherApp::loadParams()
121
{
122
  //cfgName = wxString( "launcher.ini" );
123
  cfgName =wxString(argv[0]);
124
  wxFileName execFileName(cfgName);
125
  execFileName.SetExt("ini");
126
  cfgName = execFileName.GetFullPath();
127
  if (!wxFileExists(cfgName)) {    
128
    error(cfgName.Format(_("The configuration file '%s' does not exists."),cfgName.c_str()));
129
  }
130

    
131
  //wxFileInputStream in( cfgName );
132
  //wxFileConfig cfg( in );
133
  wxString downloadEnabled;
134
  wxString launchJarAsync;
135
  wxString askForCheckingProcess;
136
  wxString doChecks;
137
  wxString checksJREExecutionTerminationCode;
138

    
139
  paramsApplicationName = readPathFromINI( "appname","gvSIG");
140
  paramsJar = readPathFromINI( "jar",wxEmptyString);
141
  paramsCommand = readPathFromINI( "command",wxEmptyString);
142
  paramsJre = readPathFromINI( "jre",   wxEmptyString);
143
  paramsJreDownload = readFromINI( "downloadJre", wxEmptyString);
144
  paramsJai = readPathFromINI( "jai",              wxEmptyString);
145
  paramsJaiDownload = readFromINI( "downloadJai",        wxEmptyString);
146
  paramsJaiIo = readPathFromINI( "jai_io",wxEmptyString);
147
  paramsJaiIoDownload = readFromINI( "downloadJai_io",wxEmptyString);
148
  paramsJreVersion = readFromINI( "jre_version",wxEmptyString);
149
  paramsJreVersionPrefered = readFromINI( "jre_version_prefered",wxEmptyString);
150
  downloadEnabled = readFromINI( "downloadEnabled",wxEmptyString);
151
  launchJarAsync = readFromINI( "launchJarAsync", wxEmptyString);
152
  askForCheckingProcess = readFromINI( "askForCheckingProcess", wxEmptyString);
153
  doChecks = readFromINI( "doChecks",wxEmptyString);
154
  checksJREExecutionTerminationCode = readFromINI( "checksJREExecutionTerminationCode",wxEmptyString);
155
  paramsJreHome = readPathFromINI( "jre_home", wxEmptyString);
156
  paramsLaunchMode = readPathFromINI( "launchMode", wxEmptyString);
157

    
158
  
159
  //echo(paramsCommand);
160

    
161
  //procesamos el parametro booleano 'downloadEnabled'
162
  paramsEnabledDownload =string_to_bool(downloadEnabled,false);
163
  //procesamos el parametro booleano 'launchJarAsync'
164
  paramsLaunchJarAsync =string_to_bool(launchJarAsync,false);
165
  //procesamos el parametro booleano 'askForCheckingProcess'
166
  paramsAskForCheckingProcess=string_to_bool(askForCheckingProcess,true);
167
  //procesamos el parametro booleano 'askForCheckingProcess'
168
  paramsDoChecks=string_to_bool(doChecks,true);
169
  //procesamos el parametro booleano 'checksJREExecutionTerminationCode'
170
  paramsChecksJREExecutionTerminationCode=string_to_bool(checksJREExecutionTerminationCode,true);
171
  
172
  if (paramsCommand == wxEmptyString )
173
  {
174
    error(cfgName.Format(_("The configuration file '%s' does not contain a command entry."),cfgName.c_str()));
175
  }
176
  if (paramsLaunchMode == wxEmptyString )
177
  {
178
    error(cfgName.Format(_("The configuration file '%s' does not contain a mode entry."),cfgName.c_str()));
179
  }
180
  paramsLaunchMode = paramsLaunchMode.MakeUpper();
181
  if ((!paramsLaunchMode.IsSameAs("APPLICATION")) && (!paramsLaunchMode.IsSameAs("INSTALL"))) {
182
        error(cfgName.Format(_("The configuration file '%s' contains a invalid mode entry."),cfgName.c_str()));
183
  }
184
  
185
  if (paramsJreVersionPrefered == wxEmptyString ) {
186
     paramsJreVersionPrefered = wxString(paramsJreVersion);
187
  }
188
  if ((!paramsDoChecks) && paramsJreHome == wxEmptyString) {
189
     error(cfgName.Format(_("The file entry 'jre_home' can not be empty when 'doChecks = No'."),cfgName.c_str()));
190
  }
191

    
192

    
193

    
194
  variables = sectionKeysFromINI("Variables");
195

    
196
  variablesValues = sectionKeysValuesFromINI("Variables",variables);
197
}
198

    
199
void LauncherApp::error(const wxString &msg)
200
{
201
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK | wxICON_ERROR);
202
  dlg.ShowModal();
203
  printf(msg.c_str());
204
  exit(1);
205
}
206

    
207
void LauncherApp::confirm(const wxString &msg)
208
{
209
  int response;
210
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK | wxCANCEL | wxICON_QUESTION );
211
  response = dlg.ShowModal();
212
  if (response != wxID_OK) {
213
    notifyToUser(_("Canceled by user"));
214
    exit(1);
215
  }
216
}
217
bool LauncherApp::confirmYesNoCancel(const wxString &msg,const int yesDefault = true)
218
{
219
  int response;
220
  long maskDefault;
221
  if (yesDefault) {
222
     maskDefault = wxYES_DEFAULT | wxYES_NO | wxCANCEL | wxICON_QUESTION;
223
  } else {
224
     maskDefault = wxNO_DEFAULT | wxYES_NO | wxCANCEL | wxICON_QUESTION;       
225
  }
226
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME),  maskDefault);
227
  response = dlg.ShowModal();
228
  if (response != wxID_YES && response != wxID_NO) {
229
    notifyToUser(_("Canceled by user"));
230
    exit(1);
231
  }
232
  return (response == wxID_YES);
233
 
234
}
235

    
236
void LauncherApp::showStatusWindow(){
237
  if (isStatusDialogVisible) {
238
    return;
239
  }
240
  
241
  statusDialog = new StatusDialog( APPLICATION_NAME );
242

    
243
  statusDialog->Centre();
244
  statusDialog->Show(TRUE);
245
  isStatusDialogVisible = true;
246
}
247

    
248

    
249
bool LauncherApp::OnInit()
250
{
251
  isStatusDialogVisible = false;
252
  wxFileName exeFileName(argv[0]);
253
  wxFileName exePath = exeFileName.GetPath();
254
  
255
  exePath.SetCwd();
256
  
257
  locale.Init();
258
  locale.AddCatalog("launcher");
259
  loadParams();  
260
  
261
  
262
  if (paramsLaunchMode.IsSameAs("INSTALL")){  
263
        APPLICATION_NAME = wxString(APPLICATION_NAME.Format(_("%s Install-Launcher"),paramsApplicationName.c_str()));
264
        SetAppName(_(APPLICATION_NAME));
265
        showStatusWindow();
266
  } else {
267
        APPLICATION_NAME = wxString(APPLICATION_NAME.Format(_("%s Launcher"),paramsApplicationName.c_str()));
268
        SetAppName(_(APPLICATION_NAME));
269
  }
270
  
271
  showStatusMsg(_("Intializing..."));
272
  
273
  myTimer = new wxTimer(this,LAUNCHAPP_TIMER_ID);
274
  myTimer->Start(150,true);
275
  this->MainLoop();
276
  return true;
277
}
278

    
279
void LauncherApp::fixSystemJREConfig(){
280
  showStatusMsg(_("Updating the system..."));
281
  //actualizamos CurrentVersion
282
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
283
                     "\\Java Runtime Environment\\";
284
                     
285
  wxString genericVersion = paramsJreVersion.Left(3);
286
  
287
  wxString baseKeyGeneric = baseKey + genericVersion + "\\";
288
  wxString baseKeyVersion = baseKey + localVersionToUse + "\\";
289

    
290
  wxRegKey *pRegKey = new wxRegKey(baseKey);
291
  if( !pRegKey->Exists() ) {
292
    error(_("JRE not found."));
293
  }
294
  
295
  // compiamos el contenido de la rama de la version
296
  // que queremos a la generica (1.4 o 1.5 o ...)
297
  wxRegKey *pRegKeyGeneric = new wxRegKey(baseKeyGeneric);
298
  wxRegKey *pRegKeyVersion = new wxRegKey(baseKeyVersion);
299
  
300
  if ( !pRegKeyGeneric->Exists() ){
301
    pRegKeyGeneric->Create();  
302
  }
303
  wxString tempKey;
304
  wxString tempValue;
305
  size_t nValues;
306
  
307
  pRegKeyVersion->GetKeyInfo(NULL,NULL,&nValues,NULL);
308
  long pos = 1;
309
  pRegKeyVersion->GetFirstValue(tempKey,pos);
310
  for(unsigned i=0;i<nValues;i++)        {
311
      //echo("copy " + tempKey);
312
      pRegKeyVersion->QueryValue(tempKey,tempValue);
313
      pRegKeyGeneric->SetValue(tempKey,tempValue);
314
      pRegKeyVersion->GetNextValue(tempKey,pos);
315
  }
316
    
317
  
318
}
319

    
320

    
321
bool LauncherApp::compareVersions(const wxString localVersion, const wxString requireVersion)
322
{
323
    bool result  = (localVersion.Cmp(requireVersion) == 0);
324
    /*
325
        if (requireVersion.Len() > localVersion.Len() )        {   
326
        
327
                result = (localVersion.Find(requireVersion) == 0);
328
        }
329
        else 
330
        {
331
                result = (requireVersion.Find(localVersion) == 0);
332
        }
333
        */
334

    
335
    /*
336
        if (result) {
337
            echo("localversion = '" + localVersion +"' requireVersion = '"+ requireVersion +"' ==> true");
338
    } else {
339
       echo("localversion = '" + localVersion +"' requireVersion = '"+ requireVersion +"' ==> false");
340
    }
341
    */
342
     
343
        return result;
344
}
345

    
346

    
347
bool LauncherApp::searchJRE()
348
{
349
  showStatusMsg(_("Searching JRE's..."));
350
  wxString version("");
351

    
352
  // Windows[tm] registry lookup
353
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
354
                     "\\Java Runtime Environment\\";
355

    
356
  wxRegKey bKey(baseKey);
357
  if (!bKey.Exists())  {
358
     return false;
359
  }
360

    
361
  if (!bKey.QueryValue("CurrentVersion", version)){
362
     return false;
363
  }
364
  
365
  if (version == "1.1") {
366
     return false;
367
  }
368

    
369
  if (!compareVersions(version, paramsJreVersionPrefered)){
370
        //Nos recorremos las versiones instaladas
371
        version = "";
372
        wxString strTemp;
373
        wxRegKey sKey(baseKey);
374
        if (sKey.HasSubKey(paramsJreVersionPrefered)) {
375
      version = wxString(paramsJreVersionPrefered);
376
    } else {              
377
          for(unsigned i=20;i>1;i--) {
378
        strTemp = wxString::Format(paramsJreVersion + "_%02d",i);
379
        if (sKey.HasSubKey(strTemp)) {
380
                        version = strTemp;
381
                        break;
382
                }
383
          }               
384
    }
385
  }
386

    
387
        
388
  if (version == "") {
389
     return false;
390
  }            
391
  localVersionToUse = version;
392
  wxRegKey vKey(baseKey + version);
393

    
394
  if (!vKey.QueryValue("JavaHome", javaHome)){
395
     return false;
396
  }
397
  
398
  
399
  calculateJavaExePath(javaHome);
400
  /*
401
  echo("paramsJreVersion=" + paramsJreVersion);
402
  echo("paramsJreVersionPrefered=" + paramsJreVersionPrefered);
403
  echo("localVersionToUse=" + localVersionToUse);
404
  echo("javaHome=" +javaHome);
405
  echo("javaExecPath=" +javaExecPath);
406
  */
407
  return true;
408
}
409

    
410
void LauncherApp::runJRE()
411
{
412
  if (paramsLaunchMode.IsSameAs("INSTALL"))        {
413
    showStatusMsg(_("Launching installation..."));
414
  } else {
415
    showStatusMsg(_("Launching application..."));
416
  }
417
  wxString cmd = parseCommand();
418
  //echo(cmd);
419
  bool isOk=true;
420
  if (paramsLaunchJarAsync) {
421
      if (!run_external_async(cmd))
422
      {
423
        isOk=false;
424
      }
425
  } else {
426
      if (!run_external(cmd))
427
      {
428
        isOk=false;
429
      }  
430
  }
431
  
432
  if (!isOk) {
433
      if (paramsChecksJREExecutionTerminationCode) {
434
        error(
435
          cmd.Format(
436
            _("The command\n%s\ncould not be executed."),
437
            cmd.c_str()
438
          )
439
        );
440
      }
441
  } 
442

    
443
  completed = true;
444
}
445

    
446
void LauncherApp::jreInstall()
447
{
448
  showStatusWindow();
449
  
450
  showStatusMsg(_("Preparing to install JRE..."));
451
  
452
  if (!checkInstallerFile(paramsJre, paramsJreDownload,_("Java JRE version ")+paramsJreVersion))
453
  {
454
    error(_("The JRE could not be setup."));  
455
  }
456

    
457
  showStatusMsg(_("Installing JRE..."));
458
  if (!run_external(paramsJre))
459
  {
460
    error(_("The JRE could not be setup."));
461
  }
462
  if (!searchJRE()) {
463
    error(_("The JRE could not be setup."));
464
  }    
465
}
466

    
467

    
468
// Not used
469
void LauncherApp::netDownload()
470
{
471
  wxString browser;
472

    
473
#ifdef __WINDOWS__
474
  // We use the default browser.
475
  browser = "rundll32 url.dll,FileProtocolHandler ";
476
#endif
477

    
478
/*
479
  if (run_external(browser + paramsDownload))
480
  {
481
    completed = true;
482
  }
483
  else
484
  {
485
    error(_("Could not find a web browser."));
486
  }
487
  */
488
}
489

    
490
IMPLEMENT_APP(LauncherApp)
491

    
492

    
493
void LauncherApp::jaiInstall()
494
{
495
  bool isOK;
496
  showStatusMsg(_("Checking JAI Library..."));
497
  
498
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
499
                     "\\Java Runtime Environment\\";
500

    
501
  wxString currentVersion;
502
  wxRegKey *pRegKey = new wxRegKey(baseKey);
503
  if( !pRegKey->Exists() )
504
    error(_("JRE not found."));
505
  if (!pRegKey->QueryValue("CurrentVersion", currentVersion)) error(_("JRE not found."));
506
  
507
  isOK=true;  
508
  if (!checksJai()) {
509
      //confirm(_("JAI library is required, Install it?"));
510
      showStatusWindow();
511
      
512
      showStatusMsg(_("Preparing to install JAI Library..."));
513
      if (!checkInstallerFile(paramsJai, paramsJaiDownload, _("JAI Library")))
514
      {
515
        isOK=false;  
516
      } else {
517
        pRegKey->SetValue("CurrentVersion",localVersionToUse);
518
        showStatusMsg(_("Preparing to install JAI Library..."));
519
        if (run_external(paramsJai))
520
        {
521
          isOK=(checksJai());
522
        } else {
523
          isOK=false;
524
        }  
525
        pRegKey->SetValue("CurrentVersion",currentVersion);
526
      }
527
      
528
  }  
529
  if (!isOK) {
530
    error(_("The JAI could not be setup."));
531
  }
532
}
533

    
534
void LauncherApp::jaiIoInstall()
535
{
536
  bool isOK;
537
  showStatusMsg(_("Checking JAI imageIO Library..."));
538
  
539
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
540
                     "\\Java Runtime Environment\\";
541

    
542
  wxString currentVersion;
543
  wxRegKey *pRegKey = new wxRegKey(baseKey);
544
  if( !pRegKey->Exists() )
545
    error(_("JRE not found."));
546
  if (!pRegKey->QueryValue("CurrentVersion", currentVersion)) error(_("JRE not found."));
547
  
548

    
549
  isOK=true;  
550
  if (!checksJaiIo()) {
551
      //confirm(_("JAI ImageIO library is required, Install it?"));
552
      showStatusWindow();
553
      
554
      showStatusMsg(_("Preparing to install JAI imageIO Library..."));
555
      if (!checkInstallerFile(paramsJaiIo, paramsJaiIoDownload,"JAI ImageIO Library"))
556
      {
557
        isOK=false;  
558
      } else {
559
        pRegKey->SetValue("CurrentVersion",localVersionToUse);
560
        showStatusMsg(_("Installing JAI imageIO Library..."));
561
        if (run_external(paramsJaiIo))
562
        {                                    
563
          isOK=(checksJaiIo());
564
        } else {
565
          isOK=false;        
566
        }
567
         pRegKey->SetValue("CurrentVersion",currentVersion);
568
      }
569
     
570
  }  
571
  if (!isOK) {
572
             error(_("The JAI imageIO could not be setup."));
573
  }
574
}
575

    
576
bool LauncherApp::checksJai(){
577
   return (wxFileExists(javaHome + "\\lib\\ext\\jai_core.jar"));
578
}
579

    
580
bool LauncherApp::checksJaiIo(){
581
   return (wxFileExists(javaHome + "\\lib\\ext\\jai_imageio.jar"));     
582
}
583

    
584
bool LauncherApp::downloadFileHttp(const wxString urlOfFile, const wxString filePath, const wxString msg){
585

    
586
  bool isOk;
587
  //echo(urlOfFile);
588
  if (urlOfFile == wxEmptyString) {
589
     return false;
590
  }
591
  
592
  if (filePath == wxEmptyString) {
593
     return false;
594
  }
595
  
596
  showStatusMsg(msg);
597
  wxURL url(urlOfFile);
598
  //echo("url open");
599
  wxInputStream *in_stream;
600

    
601
  in_stream = url.GetInputStream();
602
  //echo("in_stream open");
603
  if (!in_stream->IsOk()) {
604
     //echo("in_stream.IsOk == false");
605
     return false;
606
  }
607
  //echo("in_stream.IsOk == true");
608
  //echo("filePath =" + filePath);
609
  wxFileName fileName(filePath);
610
  
611
  
612
  // Preparamos la ruta para el fichero destino
613
  wxArrayString dirs;
614
  
615
  dirs = fileName.GetDirs();
616
  wxString dir;
617
  wxFileName curDir(".");
618
  for (size_t i=0; i < dirs.GetCount(); i++) {
619
      dir = dirs.Item(i);
620
      curDir.AppendDir(dir);
621
      //echo("dir " + curDir.GetPath());
622
      if (!curDir.DirExists()) {
623
         //echo("creating dir");
624
         isOk = curDir.Mkdir();
625
         if (!isOk) {
626
            //echo("dir create no ok");
627
            return false;
628
         }
629
         
630
         //echo("dir create ok");
631
      }
632
  }
633
  
634
  
635
  wxFileOutputStream out_stream  = wxFileOutputStream(filePath);
636
  //echo("out_stream open");
637
  
638
  //in_stream->Read(out_stream);
639
  size_t nbytes = 10240;
640
  char buffer[nbytes];
641
  
642
  
643
  //while (!in_stream->Eof()) {
644
  while (!in_stream->Eof()) {
645
      in_stream->Read(&buffer,nbytes);  
646
      if (in_stream->LastError() != wxSTREAM_NO_ERROR && in_stream->LastError() != wxSTREAM_EOF) {
647
          return false;
648
      }
649
      out_stream.Write(&buffer,in_stream->LastRead());
650
      if (out_stream.LastError() != wxSTREAM_NO_ERROR) {       
651
         return false;                                                                   
652
      }
653
      int totalKb = out_stream.GetSize() / 1024;
654
      wxString totalMsg =msg.Format("%i",totalKb);
655
      showStatusMsg(msg+" "+ totalMsg + " Kb");
656
      
657
  }
658
 
659
  
660
  isOk = true;
661
  /*
662
  if (isOk) {
663
    echo("isOk = true");
664
  } else {
665
    echo("isOk = false");         
666
  }
667
  */
668
  delete in_stream;
669
  
670
  //echo("end");
671
  return isOk;
672

    
673
}
674

    
675
bool LauncherApp::checkInstallerFile(const wxString filePath, const wxString urlDownload, const wxString fileDescription) {
676
  if (!wxFile::Exists(filePath))
677
  {
678
    if (paramsEnabledDownload) 
679
    {
680
      wxString msg = _("Installation requires to download this file:\n") + 
681
                     fileDescription + "\n" +
682
                     _("Do you want to continue?");
683
      confirm(msg);
684
      //showStatusMsg(_("Downloading ") + fileDescription + "...");      
685
      if (!downloadFileHttp(urlDownload,filePath,_("Downloading ") + fileDescription + "..." )) 
686
      {
687
        //FIXME: Falta msgError
688
        return false;  
689
      }       
690
    }
691
    else
692
    {
693
      return false;  
694
    }
695

    
696
  }
697
  return true;
698
}
699

    
700
void LauncherApp::showStatusMsg(const wxString msg) {
701
  if (isStatusDialogVisible) {
702
     while (this->Pending()) {
703
           this->Dispatch();
704
     }
705
     if (statusDialog->IsCanceled()) {
706
        int response;
707
        wxMessageDialog dlg(0, _("Do you really want to cancel the installation process?"), _(APPLICATION_NAME), wxYES_NO | wxICON_QUESTION );
708
        response = dlg.ShowModal();
709
        if (response == wxID_YES) {
710
            notifyToUser(_("Canceled by user"));
711
            exit(1);
712
        }
713
        statusDialog->DoNotCancel();
714
     }
715
     statusDialog->showStatus(msg);
716
  }
717
}
718

    
719
bool LauncherApp::checkVersion(const wxString javaexe)
720
{
721
     wxString cmd;
722
     wxArrayString out;
723
     wxArrayString err;
724
     
725
     cmd = cmd.Format("%s -version", javaexe.c_str());
726
     if( wxExecute(cmd,out,err)!= 0 ) {
727
         notifyToUser(_("Failed checking JVM version."));      
728
         return false;
729
     }
730
     
731
     if (err.Count() == 0) {
732
         notifyToUser(_("Failed checking JVM version."));      
733
         return false;        
734
     }
735
     
736
     if (err[0].Contains(paramsJreVersion.c_str()) && err[0].Contains("version")) {
737
        return true; 
738
     }
739
     notifyToUser(cmd.Format(_("%s\nJava %s recommended"), err[0].c_str(),paramsJreVersion.c_str()));
740
     return false;
741

    
742

    
743
}
744

    
745
void LauncherApp::run(){
746
  bool doChecks =true;
747
                  
748
  if (!paramsDoChecks) {
749
    javaHome = paramsJreHome;
750
    calculateJavaExePath(javaHome);
751
  } else {
752
      if (paramsAskForCheckingProcess) {
753
         wxString msg = _("Do you want to check the application requirements? \nThis will install missing components.");
754
         doChecks = confirmYesNoCancel(msg,true);
755
         if (!doChecks) {
756
                // No quiere comprobacion, por lo que solicitamos 
757
                wxString msgDir = _("Please, select the Java VM.");   
758
                wxFileDialog dlg(
759
                    0,
760
                    msgDir,
761
                    wxEmptyString,
762
                    wxString("java.exe"),
763
                    wxString("Java VM executable file (java.exe)|java.exe"),
764
                    wxOPEN | wxFILE_MUST_EXIST,
765
                    wxDefaultPosition
766
                );
767
                int response = dlg.ShowModal();  
768
                if (response != wxID_OK) {
769
                   notifyToUser(_("Canceled by user"));
770
                   exit(1);
771
                }
772
                //caragamos la variable con el eljecutable
773
                javaExecPath = dlg.GetPath();
774
                
775
                checkVersion(javaExecPath);
776
                
777
                //generamos el path para el JavaHome (por si acaso)
778
                wxFileName fileName(javaExecPath);
779
                fileName.SetFullName("");
780
                fileName.AppendDir("..");
781
                fileName.Normalize();
782
                javaHome = fileName.GetPath();                 
783
                 
784
         }
785
      }
786
         
787
    
788
      if (doChecks) {
789
    
790
          if (!searchJRE())
791
          {
792
            jreInstall();
793
          }
794
        
795
          fixSystemJREConfig();
796
        
797
          jaiInstall();
798
        
799
          jaiIoInstall();
800
      }
801
  }
802

    
803
  copyRequiredFiles();
804

    
805
  runJRE();
806

    
807
  exit(0);    
808
     
809
}
810

    
811
void LauncherApp::OnTimer(wxTimerEvent& event) {     
812
     run();
813
}
814

    
815
void LauncherApp::calculateJavaExePath(const wxString aJavaHome){
816
  int osVerMayor;
817
  int osVerMinor;
818

    
819
  wxGetOsVersion(&osVerMayor,&osVerMinor);
820
  if (osVerMayor < 5) {
821
    javaExecPath = aJavaHome + "\\bin\\java";
822
  }else{
823
    javaExecPath = aJavaHome + "\\bin\\javaw";
824
  }
825
    
826
}
827

    
828

    
829
wxString LauncherApp::parseCommand(){
830

    
831
  return parseString(paramsCommand);
832
}
833

    
834
wxString LauncherApp::parseString(const wxString theString){
835
  wxString resultCommand(theString);
836

    
837
  // #JAVA#
838
  resultCommand.Replace("#JAVA#",javaExecPath.c_str());
839
  
840
  // #JAVA_HOME#
841
  resultCommand.Replace("#JAVA_HOME#",javaHome.c_str());
842

    
843
  // #JAR#
844
  resultCommand.Replace("#JAR#",paramsJar.c_str());
845

    
846
  // calculamos la cadena args y sustituimos los #ARG?#
847
  wxString theArgs("");
848
  wxString theArg("");
849
  int i;
850
  for (i=1;i<this->argc;i++) {
851
    theArg = argv[i];
852
    if (i!=1){
853
            theArgs = theArgs + " \"" + theArg + "\"";
854
    }else{
855
            theArgs = "\"" + theArg + "\"";
856
    }
857

    
858
    resultCommand.Replace(theArg.Format("\"#ARG%i#\"",i),theArg.c_str());
859
  }
860
  // Dejamos a blanco los argumento que no hemos recivido y existen en el comando
861
  theArg = "";
862
  for (i=i;i<10;i++) {
863
    resultCommand.Replace(theArg.Format("#ARG%i#",i),theArg.c_str());
864
  }
865
  //echo(theArgs);
866

    
867
  // #ARGS#
868
  resultCommand.Replace("#ARGS#",theArgs.c_str());
869
  
870
  // #ARG0#
871
  resultCommand.Replace("#ARG0#",argv[0]);
872

    
873
  // variables de la seccion variables
874

    
875
  //echo(resultCommand);
876
  
877
//  
878
  if (!variables.IsEmpty()){
879
          //echo(theArg.Format("No empty: count =%i",keys.GetCount()));
880
          unsigned int i;
881
          for (i=0;i<variables.GetCount();i++){
882
                  //echo("#"+variables[i]+"#="+variablesValues[i]);
883
                  resultCommand.Replace("#"+variables[i]+"#",variablesValues[i]);
884
          }
885

    
886
  //echo(resultCommand);
887

    
888
  }
889

    
890
 
891

    
892
  return resultCommand;
893
}
894

    
895

    
896

    
897
bool LauncherApp::copyRequiredFiles(){
898
  int i = 0;
899
  //wxFileInputStream in( cfgName );
900
  //wxFileConfig cfg( in );
901
  //cfg.SetPath("/CopyRequiredFiles");
902

    
903
  wxString source;
904
  wxString target;
905
  wxString msg;
906
  wxFileName fileName;
907

    
908
  while (true) {
909
        i++;
910

    
911
        source = readPathFromINI("CopyRequiredFiles",source.Format("source%i",i),wxEmptyString);
912
        target = readPathFromINI("CopyRequiredFiles",target.Format("target%i",i),wxEmptyString);
913
        if (source == wxEmptyString && target == wxEmptyString) {
914
                return true;
915
        }
916
        if (source == wxEmptyString || target == wxEmptyString) {
917
                error(source.Format(_("Error copying the file number %i:\n missing source or target entry"),i));
918
                return false;
919
        }
920
        source = parseString(source);
921
        target = parseString(target);
922
        if (wxFileExists(target)){
923
                continue;
924
        } else {
925
            if (wxDirExists(target)) {
926
                fileName = wxFileName(target);
927
                wxFileName tempFileName(source);
928
                fileName.SetFullName(tempFileName.GetFullName());
929
                if (fileName.FileExists()) {
930
                        continue;
931
                }
932
            }
933
                                
934
        }
935
        if (!wxFileExists(source)){
936
                error(source.Format(_("Error copying the file number %i:\n source file '%s' does not exists."),i,source.c_str()));
937
                return false;
938
        }
939

    
940
        fileName = wxFileName(source);
941
        msg = msg.Format(_("copying %s..."), fileName.GetFullName().c_str());
942
        //echo(msg);
943
        //echo(msg.Format("%s --> %s",source.c_str(),target.c_str()));
944
        if (!copyFile(source,target, msg)){
945
                error(source.Format(_("Error copying the file number %i:\n '%s' --> '%s'."),i,source.c_str(),target.c_str()));
946
                return false;
947
        }
948

    
949
  }
950
}
951

    
952
bool LauncherApp::copyFile(const wxString source, const wxString target, const wxString msg){
953

    
954
  bool isOk;
955
  //echo(source);
956
  if (source == wxEmptyString) {
957
     return false;
958
  }
959
  
960
  //echo(target);
961
  if (target == wxEmptyString) {
962
     return false;
963
  }
964
  
965
  showStatusMsg(msg);
966

    
967
  wxFileName targetFileName(target);
968
  
969
  
970
  // Preparamos la ruta para el fichero destino
971
  wxArrayString dirs;
972
  
973
  dirs = targetFileName.GetDirs();
974
  wxString dir;
975
  wxFileName curDir;
976
  if (targetFileName.IsAbsolute()) {
977
        curDir.Assign("\\");
978
        curDir.SetVolume(targetFileName.GetVolume());
979
  }else{
980
        curDir.Assign(".");
981
  }
982
  for (size_t i=0; i < dirs.GetCount(); i++) {
983
      dir = dirs.Item(i);
984
      curDir.AppendDir(dir);
985
      //echo("dir " + curDir.GetPath());
986
      if (!curDir.DirExists()) {
987
         //echo("creating dir");
988
         isOk = curDir.Mkdir();
989
         if (!isOk) {
990
            //echo("dir create no ok");
991
            return false;
992
         }
993
         
994
         //echo("dir create ok");
995
      }
996
  }
997

    
998
  wxString finalTarget;
999
  if (targetFileName.IsDir() || targetFileName.DirExists()) { 
1000
      //echo("targetFileName.IsDir -> true  " + targetFileName.GetFullPath());
1001
      wxFileName sourceFileName(source);
1002
      targetFileName.SetFullName(sourceFileName.GetFullName());
1003
      finalTarget = targetFileName.GetFullPath();
1004
  } else {
1005
      //echo("targetFileName.IsDir -> false  " + targetFileName.GetFullPath());
1006
      finalTarget = target;
1007
  }
1008

    
1009

    
1010
  //echo(msg.Format("%s --> %s",source.c_str(),finalTarget.c_str()));
1011
  isOk = wxCopyFile(source,finalTarget,false);
1012
        
1013

    
1014
  return isOk;
1015

    
1016
}
1017

    
1018
wxString LauncherApp::readPathFromINI(wxString section, wxString key, const wxString defaultValue){
1019
        char* charResult;
1020

    
1021
        charResult =myGetPrivateProfileString(section.c_str(),key.c_str(),defaultValue.c_str(),cfgName.c_str()); 
1022

    
1023
        //Remplazamos \ por /
1024
        
1025
        register char* s = charResult;
1026
        for (;*s;s++) {
1027
                if (*s == '\\') {
1028
                        *s = '/';
1029
                }
1030
        }
1031

    
1032
        wxString result(charResult);
1033
        return result;
1034

    
1035

    
1036
}
1037

    
1038

    
1039

    
1040
wxString LauncherApp::readPathFromINI(wxString key, const wxString defaultValue){
1041
        return readPathFromINI("default",key,defaultValue);
1042

    
1043
}
1044

    
1045
wxString LauncherApp::readFromINI(wxString section, wxString key, const wxString defaultValue){
1046
        char* charResult;
1047

    
1048
        charResult =myGetPrivateProfileString(section.c_str(),key.c_str(),defaultValue.c_str(),cfgName.c_str()); 
1049

    
1050
        wxString result(charResult);
1051
        return result;
1052
}
1053

    
1054
wxString LauncherApp::readFromINI(wxString key, const wxString defaultValue){
1055
        return readFromINI("default",key,defaultValue);
1056
}
1057

    
1058
wxArrayString LauncherApp::sectionKeysFromINI(const wxString section){
1059
        char* charResult;
1060

    
1061
        charResult =myGetPrivateProfileString(section.c_str(),NULL,NULL,cfgName.c_str()); 
1062
        //echo(section +" = "+charResult);
1063

    
1064
        wxArrayString rvalue;
1065
        char* token;
1066
        
1067
        for (
1068
                token = strtok(charResult,"="); 
1069
                token;
1070
                token = strtok(NULL,"=") 
1071
                
1072
            ) {
1073
                rvalue.Add(token);
1074
        }
1075
        return rvalue;
1076
}
1077

    
1078

    
1079
wxArrayString LauncherApp::sectionKeysValuesFromINI(const wxString section,wxArrayString keys) {
1080
        wxArrayString rvalue;
1081
        unsigned int i;
1082
        wxString key;
1083
        wxString value;
1084
        for (i=0;i < keys.GetCount(); i++){
1085
                rvalue.Add(readPathFromINI( section, keys[i], "")); 
1086
        }        
1087
        return rvalue;
1088
}
1089