Statistics
| Revision:

svn-gvsig-desktop / trunk / install / launcher / izpack-launcher-1.3 / src / launcher.cpp @ 7293

History | View | Annotate | Download (25.9 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
#ifdef __UNIX__
31
  #include <stdlib.h>
32
  #include <map>
33
  #include <list>
34
#endif
35

    
36

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

    
49
}
50

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

    
56
bool string_to_bool(wxString value, bool defaultValue){
57
 bool returnValue = defaultValue;
58
 if (value != wxEmptyString)
59
    if (
60
       value.CmpNoCase("s")   == 0  ||
61
       value.CmpNoCase("si")  == 0  ||
62
       value.CmpNoCase("1")   == 0  ||
63
       value.CmpNoCase("y")   == 0  ||
64
       value.CmpNoCase("yes") == 0
65
       ) 
66
    {
67
       returnValue = true;
68
    } else {
69
       returnValue = false;
70
    }
71
  return returnValue;
72
}
73

    
74
long LAUNCHAPP_TIMER_ID = wxNewId();
75

    
76
BEGIN_EVENT_TABLE(LauncherApp, wxApp)
77
    EVT_TIMER(LAUNCHAPP_TIMER_ID, LauncherApp::OnTimer)
78
END_EVENT_TABLE()
79

    
80

    
81
/* Main Application $Revision: 7293 $
82
 *
83
 * $Id: launcher.cpp 7293 2006-09-15 07:21:47Z jmvivo $
84
 */
85
LauncherApp::LauncherApp()
86
  : wxApp()
87
{
88
  APPLICATION_NAME = wxString("gvSIG Install-Launcher");
89
  completed = false;
90

    
91
  SetAppName(_( APPLICATION_NAME ));
92
  //loadParams();
93
}
94

    
95
void LauncherApp::echo(const wxString &msg) 
96
{
97
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK);
98
  dlg.ShowModal();
99
}
100

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

    
107

    
108
LauncherApp::~LauncherApp()
109
{
110

    
111
}
112

    
113
void LauncherApp::loadParams()
114
{
115
  //cfgName = wxString( "launcher.ini" );
116
  cfgName =wxString(argv[0]);
117
  wxFileName execFileName(cfgName);
118
  execFileName.SetExt("ini");
119
  cfgName = execFileName.GetFullPath();
120
  if (!wxFileExists(cfgName)) {    
121
    error(cfgName.Format(_("The configuration file '%s' does not exists."),cfgName.c_str()));
122
  }
123

    
124
  wxFileInputStream in( cfgName );
125
  wxFileConfig cfg( in );
126
  wxString downloadEnabled;
127
  wxString launchJarAsync;
128
  wxString askForCheckingProcess;
129
  wxString doChecks;
130

    
131
  cfg.Read( "jar",                    &paramsJar,                wxEmptyString);
132
  cfg.Read( "command",                &paramsCommand,            wxEmptyString);
133
  cfg.Read( "jre",                    &paramsJre,                wxEmptyString);
134
  cfg.Read( "downloadJre",            &paramsJreDownload,        wxEmptyString);
135
  cfg.Read( "jai",                    &paramsJai,                wxEmptyString);
136
  cfg.Read( "downloadJai",            &paramsJaiDownload,        wxEmptyString);
137
  cfg.Read( "jai_io",                 &paramsJaiIo,              wxEmptyString);
138
  cfg.Read( "downloadJai_io",         &paramsJaiIoDownload,      wxEmptyString);
139
  cfg.Read( "jre_version",            &paramsJreVersion,         wxEmptyString);
140
  cfg.Read( "jre_version_prefered",   &paramsJreVersionPrefered, wxEmptyString);
141
  cfg.Read( "downloadEnabled",        &downloadEnabled,          wxEmptyString);
142
  cfg.Read( "launchJarAsync",         &launchJarAsync,           wxEmptyString);
143
  cfg.Read( "askForCheckingProcess",  &askForCheckingProcess,    wxEmptyString);
144
  cfg.Read( "doChecks",               &doChecks,                 wxEmptyString);
145
  cfg.Read( "jre_home",               &paramsJreHome,            wxEmptyString);
146
  
147
  //procesamos el parametro booleano 'downloadEnabled'
148
  paramsEnabledDownload =string_to_bool(downloadEnabled,false);
149
  //procesamos el parametro booleano 'launchJarAsync'
150
  paramsLaunchJarAsync =string_to_bool(launchJarAsync,false);
151
  //procesamos el parametro booleano 'askForCheckingProcess'
152
  paramsAskForCheckingProcess=string_to_bool(askForCheckingProcess,true);
153
  //procesamos el parametro booleano 'askForCheckingProcess'
154
  paramsDoChecks=string_to_bool(doChecks,true);
155
  
156
  if (paramsCommand == wxEmptyString )
157
  {
158
    error(cfgName.Format(_("The configuration file '%s' does not contain a command entry."),cfgName.c_str()));
159
  }
160
  if (paramsJreVersionPrefered == wxEmptyString ) {
161
     paramsJreVersionPrefered = wxString(paramsJreVersion);
162
  }
163
  if ((!paramsDoChecks) && paramsJreHome == wxEmptyString) {
164
     error(cfgName.Format(_("The file entry 'jre_home' can not be empty when 'doChecks = No'."),cfgName.c_str()));
165
  }
166
}
167

    
168
void LauncherApp::error(const wxString &msg)
169
{
170
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK | wxICON_ERROR);
171
  dlg.ShowModal();
172
  exit(1);
173
}
174

    
175
void LauncherApp::confirm(const wxString &msg)
176
{
177
  int response;
178
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME), wxOK | wxCANCEL | wxICON_QUESTION );
179
  response = dlg.ShowModal();
180
  if (response != wxID_OK) {
181
    notifyToUser(_("Canceled by user"));
182
    exit(1);
183
  }
184
}
185
bool LauncherApp::confirmYesNoCancel(const wxString &msg,const int yesDefault = true)
186
{
187
  int response;
188
  long maskDefault;
189
  if (yesDefault) {
190
     maskDefault = wxYES_DEFAULT | wxYES_NO | wxCANCEL | wxICON_QUESTION;
191
  } else {
192
     maskDefault = wxNO_DEFAULT | wxYES_NO | wxCANCEL | wxICON_QUESTION;       
193
  }
194
  wxMessageDialog dlg(0, msg, _(APPLICATION_NAME),  maskDefault);
195
  response = dlg.ShowModal();
196
  if (response != wxID_YES && response != wxID_NO) {
197
    notifyToUser(_("Canceled by user"));
198
    exit(1);
199
  }
200
  return (response == wxID_YES);
201
 
202
}
203

    
204

    
205

    
206
bool LauncherApp::OnInit()
207
{
208
  isStatusDialogVisible = false;
209
  locale.Init();
210
  locale.AddCatalog("launcher");
211
  loadParams();  
212
  
213
  if (paramsDoChecks) {
214

    
215
      statusDialog = new StatusDialog( APPLICATION_NAME );
216
    
217
      statusDialog->Centre();
218
      statusDialog->Show(TRUE);
219
      isStatusDialogVisible = true;
220
  }
221

    
222
  showStatusMsg(_("Intializing..."));
223
  
224
  myTimer = new wxTimer(this,LAUNCHAPP_TIMER_ID);
225
  myTimer->Start(150,true);
226
  this->MainLoop();
227
  return true;
228
  /*
229

230
  if (!searchJRE())
231
  {
232
    jreInstall();
233
  }
234

235
  fixSystemJREConfig();
236

237
  jaiInstall();
238

239
  jaiIoInstall();
240

241
  runJRE();
242

243
  exit(0);    
244
  
245
  return false;
246
  */
247
}
248

    
249
void LauncherApp::fixSystemJREConfig(){
250
  showStatusMsg(_("Updating the system..."));
251
  //actualizamos CurrentVersion
252
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
253
                     "\\Java Runtime Environment\\";
254
                     
255
  wxString genericVersion = paramsJreVersion.Left(3);
256
  
257
  wxString baseKeyGeneric = baseKey + genericVersion + "\\";
258
  wxString baseKeyVersion = baseKey + localVersionToUse + "\\";
259

    
260
  wxRegKey *pRegKey = new wxRegKey(baseKey);
261
  if( !pRegKey->Exists() ) {
262
    error(_("JRE not found."));
263
  }
264
  //pRegKey->SetValue("CurrentVersion",genericVersion); // **********
265
  
266
  // compiamos el contenido de la rama de la version
267
  // que queremos a la generica (1.4 o 1.5 o ...)
268
  wxRegKey *pRegKeyGeneric = new wxRegKey(baseKeyGeneric);
269
  wxRegKey *pRegKeyVersion = new wxRegKey(baseKeyVersion);
270
  
271
  if ( !pRegKeyGeneric->Exists() ){
272
    pRegKeyGeneric->Create();  
273
  }
274
  wxString tempKey;
275
  wxString tempValue;
276
  size_t nValues;
277
    
278
  //pRegKeyGeneric->SetValue("hola","generico");
279
  //pRegKeyVersion->SetValue("hola","version");
280
  
281
  pRegKeyVersion->GetKeyInfo(NULL,NULL,&nValues,NULL);
282
  long pos = 1;
283
  pRegKeyVersion->GetFirstValue(tempKey,pos);
284
  for(unsigned i=0;i<nValues;i++)        {
285
      //echo("copy " + tempKey);
286
      pRegKeyVersion->QueryValue(tempKey,tempValue);
287
      pRegKeyGeneric->SetValue(tempKey,tempValue);
288
      pRegKeyVersion->GetNextValue(tempKey,pos);
289
  }
290
    
291
  
292
  //copiamos el java.exe a Windows/System32
293
  //wxString source = javaHome + "\\bin\\java.exe";
294
  //wxString target = getenv("windir"); 
295
  //target = target + "\\system32\\java.exe";
296
  
297
  //bool isOk = wxCopyFile(source, target, true);
298
  //if (!isOk) {
299
  //  error(_("Error perform copy ") + source + _(" to ") + target);
300
  //}
301
  
302
}
303

    
304

    
305
bool LauncherApp::compareVersions(const wxString localVersion, const wxString requireVersion)
306
{
307
    bool result  = (localVersion.Cmp(requireVersion) == 0);
308
    /*
309
        if (requireVersion.Len() > localVersion.Len() )        {   
310
        
311
                result = (localVersion.Find(requireVersion) == 0);
312
        }
313
        else 
314
        {
315
                result = (requireVersion.Find(localVersion) == 0);
316
        }
317
        */
318

    
319
    /*
320
        if (result) {
321
            echo("localversion = '" + localVersion +"' requireVersion = '"+ requireVersion +"' ==> true");
322
    } else {
323
       echo("localversion = '" + localVersion +"' requireVersion = '"+ requireVersion +"' ==> false");
324
    }
325
    */
326
     
327
        return result;
328
}
329

    
330

    
331
bool LauncherApp::searchJRE()
332
{
333
  showStatusMsg(_("Searching JRE's..."));
334
  wxString version("");
335

    
336
  // Windows[tm] registry lookup
337
  wxString baseKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft"
338
                     "\\Java Runtime Environment\\";
339

    
340
  wxRegKey bKey(baseKey);
341
  if (!bKey.Exists())  {
342
     return false;
343
  }
344

    
345
  if (!bKey.QueryValue("CurrentVersion", version)){
346
     return false;
347
  }
348
  
349
  if (version == "1.1") {
350
     return false;
351
  }
352

    
353
  if (!compareVersions(version, paramsJreVersionPrefered)){
354
        //Nos recorremos las versiones instaladas
355
        version = "";
356
        wxString strTemp;
357
        wxRegKey sKey(baseKey);
358
        if (sKey.HasSubKey(paramsJreVersionPrefered)) {
359
      version = wxString(paramsJreVersionPrefered);
360
    } else {              
361
          for(unsigned i=20;i>1;i--) {
362
        strTemp = wxString::Format(paramsJreVersion + "_%02d",i);
363
        if (sKey.HasSubKey(strTemp)) {
364
                        version = strTemp;
365
                        break;
366
                }
367
          }               
368
    }
369
  }
370

    
371
        
372
  if (version == "") {
373
     return false;
374
  }            
375
  localVersionToUse = version;
376
  wxRegKey vKey(baseKey + version);
377

    
378
  if (!vKey.QueryValue("JavaHome", javaHome)){
379
     return false;
380
  }
381
  
382
  
383
  calculateJavaExePath(javaHome);
384
  /*
385
  echo("paramsJreVersion=" + paramsJreVersion);
386
  echo("paramsJreVersionPrefered=" + paramsJreVersionPrefered);
387
  echo("localVersionToUse=" + localVersionToUse);
388
  echo("javaHome=" +javaHome);
389
  echo("javaExecPath=" +javaExecPath);
390
  */
391
  return true;
392
}
393

    
394
void LauncherApp::runJRE()
395
{
396
  showStatusMsg(_("Launching installation..."));
397
  /*
398
  if (!wxFile::Exists(paramsJar))
399
  {
400
    error(_("Could not find '") + paramsJar + _("' file referenced by the configuration file '") + cfgName + _("'"));
401
  }
402
  */
403

    
404
  //wxString cmd = javaExecPath + paramsJarParams + wxString(" -jar ") + paramsJar;
405
  wxString cmd = parseCommand();
406
  echo(cmd);
407
  if (paramsLaunchJarAsync) {
408
      if (!run_external_async(cmd))
409
      {
410
        error(cmd.Format(_("The command\n%s\ncould not be executed."),cmd.c_str()));
411
      }
412
  } else {
413
      if (!run_external(cmd))
414
      {
415
        error(cmd.Format(_("The command\n%s\ncould not be executed."),cmd.c_str()));
416
      }  
417
  }
418

    
419
  completed = true;
420
}
421

    
422
void LauncherApp::jreInstall()
423
{
424
  showStatusMsg(_("Preparing to install JRE..."));
425
  
426
  if (!checkInstallerFile(paramsJre, paramsJreDownload,_("Java JRE version ")+paramsJreVersion))
427
  {
428
    error(_("The JRE could not be setup."));  
429
  }
430

    
431
  showStatusMsg(_("Installing JRE..."));
432
  if (!run_external(paramsJre))
433
  {
434
    error(_("The JRE could not be setup."));
435
  }
436
  if (!searchJRE()) {
437
    error(_("The JRE could not be setup."));
438
  }    
439
}
440

    
441

    
442
// Not used
443
void LauncherApp::netDownload()
444
{
445
  wxString browser;
446

    
447
#ifdef __WINDOWS__
448
  // We use the default browser.
449
  browser = "rundll32 url.dll,FileProtocolHandler ";
450
#endif
451

    
452
#ifdef __UNIX__
453
  // We try some browsers and use the first successful one.
454
  std::list<std::pair<wxString, wxString> > commands;
455
  std::pair<wxString, wxString> cmd;
456
  cmd.first = "konqueror "; cmd.second = "konqueror -v";
457
  commands.push_back(cmd);
458
  cmd.first = "mozilla -splash "; cmd.second = "mozilla -v";
459
  commands.push_back(cmd);
460
  cmd.first = "firefox -splash "; cmd.second = "firefox -v";
461
  commands.push_back(cmd);
462
  cmd.first = "firebird -splash "; cmd.second = "firebird -v";
463
  commands.push_back(cmd);
464
  cmd.first = "opera "; cmd.second = "opera -v";
465
  commands.push_back(cmd);
466
  cmd.first = "netscape "; cmd.second = "netscape -v";
467
  commands.push_back(cmd);
468
  std::list<std::pair<wxString, wxString> >::iterator it;
469
  for (it = commands.begin(); it != commands.end(); ++it)
470
  {
471
    if (wxExecute(it->second, wxEXEC_SYNC) == 0)
472
    {
473
      browser = it->first;
474
      break;
475
    }
476
  }
477
#endif
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
      
511
      showStatusMsg(_("Preparing to install JAI Library..."));
512
      if (!checkInstallerFile(paramsJai, paramsJaiDownload, _("JAI Library")))
513
      {
514
        isOK=false;  
515
      } else {
516
        pRegKey->SetValue("CurrentVersion",localVersionToUse);
517
        showStatusMsg(_("Preparing to install JAI Library..."));
518
        if (run_external(paramsJai))
519
        {
520
          isOK=(checksJai());
521
        } else {
522
          isOK=false;
523
        }  
524
        pRegKey->SetValue("CurrentVersion",currentVersion);
525
      }
526
      
527
  }  
528
  if (!isOK) {
529
    error(_("The JAI could not be setup."));
530
  }
531
}
532

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

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

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

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

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

    
582
bool LauncherApp::downloadFileHttp(const wxString urlOfFile, const wxString filePath, const wxString msg){
583

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

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

    
671
}
672

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

    
694
  }
695
  return true;
696
}
697

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

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

    
740

    
741
}
742

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

    
799
  copyRequiredFiles();
800

    
801
  runJRE();
802

    
803
  exit(0);    
804
     
805
}
806

    
807
void LauncherApp::OnTimer(wxTimerEvent& event) {     
808
     run();
809
}
810

    
811
void LauncherApp::calculateJavaExePath(const wxString aJavaHome){
812
  int osVerMayor;
813
  int osVerMinor;
814

    
815
  wxGetOsVersion(&osVerMayor,&osVerMinor);
816
  if (osVerMayor < 5) {
817
    javaExecPath = aJavaHome + "\\bin\\java";
818
  }else{
819
    javaExecPath = aJavaHome + "\\bin\\javaw";
820
  }
821
    
822
}
823

    
824

    
825
wxString LauncherApp::parseCommand(){
826
  wxString resultCommand(paramsCommand);
827

    
828
  // #JAVA#
829
  resultCommand.Replace("#JAVA#",javaExecPath.c_str());
830
  
831
  // #JAVA_HOME#
832
  resultCommand.Replace("#JAVA_HOME#",javaHome.c_str());
833

    
834
  // #JAR#
835
  resultCommand.Replace("#JAR#",paramsJar.c_str());
836

    
837
  // calculamos la cadena args y sustituimos los #ARG?#
838
  wxString theArgs("");
839
  wxString theArg("");
840
  int i;
841
  for (i=1;i<this->argc;i++) {
842
    theArg = argv[i];
843
    if (i!=1){
844
            theArgs = theArgs + " "  + theArg;
845
    }else{
846
            theArgs = theArg;
847
    }
848

    
849
    resultCommand.Replace(theArg.Format("#ARG%i#",i),theArg.c_str());
850
  }
851
  // Dejamos a blanco los argumento que no hemos recivido y existen en el comando
852
  theArg = "";
853
  for (i=i;i<10;i++) {
854
    resultCommand.Replace(theArg.Format("#ARG%i#",i),theArg.c_str());
855
  }
856

    
857
  // #ARGS#
858
  resultCommand.Replace("#ARGS#",theArgs.c_str());
859
  
860
  // #ARG0#
861
  resultCommand.Replace("#ARG0#",argv[0]);
862
 
863
  return resultCommand;
864
}
865

    
866

    
867

    
868
bool LauncherApp::copyRequiredFiles(){
869
  int i = 0;
870
  wxFileInputStream in( cfgName );
871
  wxFileConfig cfg( in );
872
  cfg.SetPath("/CopyRequiredFiles");
873

    
874
  wxString source;
875
  wxString target;
876
  wxString msg;
877
  wxFileName fileName;
878

    
879
  while (true) {
880
        i++;
881
        cfg.Read(source.Format("source%i",i), &source, wxEmptyString);
882
        cfg.Read(target.Format("target%i",i), &target, wxEmptyString);
883
        if (source == wxEmptyString && target == wxEmptyString) {
884
                return true;
885
        }
886
        if (source == wxEmptyString || target == wxEmptyString) {
887
                error(source.Format(_("Error copying the file number %i:\n missing source or target entry"),i));
888
                return false;
889
        }
890
        if (wxFileExists(target)){
891
                continue;
892
        }
893
        if (!wxFileExists(source)){
894
                error(source.Format(_("Error copying the file number %i:\n source file '%s' does not exists."),i,source.c_str()));
895
                return false;
896
        }
897

    
898
        fileName = wxFileName(source);
899
        msg = msg.Format(_("copying %s..."), fileName.GetFullName().c_str());
900
        if (!copyFile(source,target, msg)){
901
                error(source.Format(_("Error copying the file number %i:\n '%s' --> '%s'."),i,source.c_str(),target.c_str()));
902
                return false;
903
        }
904

    
905
  }
906
}
907

    
908
bool LauncherApp::copyFile(const wxString source, const wxString target, const wxString msg){
909

    
910
  bool isOk;
911
  //echo(urlOfFile);
912
  if (source == wxEmptyString) {
913
     return false;
914
  }
915
  
916
  if (target == wxEmptyString) {
917
     return false;
918
  }
919
  
920
  showStatusMsg(msg);
921
  //wxFile fileSource(source);
922
  //wxFileInputStream in_stream(fileSource);
923
  wxFileInputStream in_stream(source);
924

    
925
  //in_stream = url.GetInputStream();
926
  //echo("in_stream open");
927
  if (!in_stream.IsOk()) {
928
     //echo("in_stream.IsOk == false");
929
     return false;
930
  }
931
  //echo("in_stream.IsOk == true");
932
  //echo("filePath =" + filePath);
933
  wxFileName targetFileName(target);
934
  
935
  
936
  // Preparamos la ruta para el fichero destino
937
  wxArrayString dirs;
938
  
939
  dirs = targetFileName.GetDirs();
940
  wxString dir;
941
  wxFileName curDir(".");
942
  for (size_t i=0; i < dirs.GetCount(); i++) {
943
      dir = dirs.Item(i);
944
      curDir.AppendDir(dir);
945
      //echo("dir " + curDir.GetPath());
946
      if (!curDir.DirExists()) {
947
         //echo("creating dir");
948
         isOk = curDir.Mkdir();
949
         if (!isOk) {
950
            //echo("dir create no ok");
951
            return false;
952
         }
953
         
954
         //echo("dir create ok");
955
      }
956
  }
957
  
958
  
959
  wxFileOutputStream out_stream  = wxFileOutputStream(target);
960
  //echo("out_stream open");
961
  
962
  //in_stream->Read(out_stream);
963
  size_t nbytes = 10240;
964
  char buffer[nbytes];
965
  
966
  
967
  //while (!in_stream->Eof()) {
968
  while (!in_stream.Eof()) {
969
      in_stream.Read(&buffer,nbytes);  
970
      if (in_stream.LastError() != wxSTREAM_NO_ERROR && in_stream.LastError() != wxSTREAM_EOF) {
971
          return false;
972
      }
973
      out_stream.Write(&buffer,in_stream.LastRead());
974
      if (out_stream.LastError() != wxSTREAM_NO_ERROR) {       
975
         return false;                                                                   
976
      }
977
      int totalKb = out_stream.GetSize() / 1024;
978
      wxString totalMsg =msg.Format("%i",totalKb);
979
      showStatusMsg(msg+" "+ totalMsg + " Kb");
980
      
981
  }
982
 
983
  
984
  isOk = true;
985
  /*
986
  if (isOk) {
987
    echo("isOk = true");
988
  } else {
989
    echo("isOk = false");         
990
  }
991
  */
992
  //delete in_stream;
993
  //delete out_stream;
994
  
995
  //echo("end");
996
  return isOk;
997

    
998
}
999