/************************************** CDrTuxAppCtx.cpp **************************
 *                                                                                *
 *   #include "CDrTuxAppCtx.h"                                                    *
 *   Project: MedinTux                                                            *
 *   Copyright (C) 2003 2004 2005 by Sevin Roland  and Data Medical Design        *
 *   E-Mail: data.medical.design@club-internet.fr                                 *
 *                                                                                *
 *   This program is free software; you can redistribute it and/or modify         *
 *   it under the terms of the CeCILL Version 1 du 21/06/2004                     *
 *   License (GPL compatible)                                                     *
 *                              http://www.cecill.info/                           *
 *   as published by :                                                            *
 *                                                                                *
 *   Commissariat à l'Energie Atomique                                            *
 *   - CEA,                                                                       *
 *                            31-33 rue de la Fédération, 75752 PARIS cedex 15.   *
 *                            FRANCE                                              *
 *   Centre National de la Recherche Scientifique                                 *
 *   - CNRS,                                                                      *
 *                            3 rue Michel-Ange, 75794 Paris cedex 16.            *
 *                            FRANCE                                              *
 *   Institut National de Recherche en Informatique et en Automatique             *
 *   - INRIA,                                                                     *
 *                            Domaine de Voluceau, Rocquencourt, BP 105, 78153    *
 *                            FRANCE                                              *
 *                                                                                *
 *         This program is distributed in the hope that it will be useful,        *
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   *
 *         CeCILL  License (GPL compatible) for more details.                     *
 *                                                                                *
 **********************************************************************************/
#include <qfileinfo.h>
#include <qdir.h>
#include <qapplication.h>
#include <qobject.h>
#include <qtextcodec.h>


#include "CManuAppCtx.h"
#include "../../MedinTuxTools/CGestIni.h"
#include "../../MedinTuxTools/CGenTools.h"

#define TR QObject::tr

//-------------------------------------------- CManuAppCtx --------------------------------------------------
CManuAppCtx::CManuAppCtx(int argc, char ** argv)
{ QString  val1       = "";
  QString in_ini_path = "";
  QTextCodec::setCodecForTr( QTextCodec::codecForName("utf8"));
  m_pDlg_Manu       = 0;
  m_PathAppli       =  CGestIni::Construct_PathBin_Module("manu",QFileInfo (qApp->argv()[0]).dirPath (true));
  m_PathRessources  =  m_PathAppli  + "Ressources/";
  m_PathCache       =  m_PathRessources;
  m_PathManuIni     =  m_PathAppli  + "manu.ini";
  m_StartDoc        = "index.html";
  m_curentPage      = "";
  m_IsQT_page       = 1;
  m_Style           = Qt::WDestructiveClose  |
                      Qt::WType_TopLevel     |
                      Qt::WStyle_Customize   |
                      Qt::WStyle_NormalBorder|
                      Qt::WStyle_Title       |
                      Qt::WStyle_MinMax      |
                      Qt::WStyle_SysMenu     |
                      Qt::WStyle_StaysOnTop;
  m_StaysOnTop = "on top";
  if (argc>=13)  m_PathExchg    = argv[3];

  CGestIni::Param_UpdateFromDisk(m_PathManuIni, m_ManuParam );
  //................... verifier si style defini dans fichier .ini ............................................................
  if (CGestIni::Param_ReadParam(m_ManuParam, "Configuration", "WStyle_StaysOnTop", &val1) == 0)
     {QChar r = val1.lower().at(0);
      if ( r == 'n' ) {m_Style &= ~Qt::WStyle_StaysOnTop; m_StaysOnTop = "a not on top";}
     }
  //......................verifier si style defini dans 14em parametre ........................................................
  if (argc>18)
     {val1     = argv[18];
      m_StaysOnTop = argv[18];
      QChar r = val1.lower().at(0);
      if (r != 'l')
         {if (r == 'n')   {m_Style &= ~Qt::WStyle_StaysOnTop; /* m_StaysOnTop = "b not on top"; */}
          else            {m_Style |=  Qt::WStyle_StaysOnTop; /* m_StaysOnTop = "c on top";*/}
         }
     }
  //................... rechercher le repertoire du cache des chapitres ............................................................
 if (argc > 17)
    {
     if (CGestIni::IsUtf8(argv[17 ])) m_SommaireDoc     =  QString::fromUtf8 (argv[ 17 ]);
     else                             m_SommaireDoc     =  argv[ 17 ];
    }
  //................... verifier si chemin doc dans fichier .ini ............................................................
  //                     y a pas le chemin du document de depart

  if (argc > 14)
    {if (CGestIni::IsUtf8(argv[14 ])) in_ini_path =  QString::fromUtf8 (argv[ 14 ]);
     else                             in_ini_path =  argv[ 14 ];
    }
  else if (CGestIni::Param_ReadParam(m_ManuParam, "Configuration", "Path Doc", &in_ini_path) != 0)
     { in_ini_path = "../../Doc";
     }
  //.......................... si relatif batir le chemin ......................................................................................
  if (in_ini_path.at(0) == '.') val1     =  CGestIni::Construct_Name_File_Ini("manu", QFileInfo (qApp->argv()[0]).dirPath (true), in_ini_path );
  else                          val1     =  in_ini_path;

 //................... rechercher le repertoire du cache des chapitres ............................................................
 if (argc > 15)
    {
     if (CGestIni::IsUtf8(argv[15 ])) m_PathCache     =  QString::fromUtf8 (argv[ 15 ]);
     else                             m_PathCache     =  argv[ 15 ];
     if (m_PathCache.at(0) == '.')    m_PathCache     =  CGestIni::Construct_Name_File_Ini("manu", QFileInfo (qApp->argv()[0]).dirPath (true), m_PathCache );
    }
 if ( (val1 = CGenTools::IsThisFolderExist(val1)) != QString::null )
    {m_PathDoc = val1;
     if (argc < 14)
        {CGestIni::Param_WriteParam(&m_ManuParam, "Configuration", "Path Doc", in_ini_path);    // ecrire le point ini au cas où
         CGestIni::Param_UpdateToDisk(m_PathManuIni, m_ManuParam);
        }
    }
  else
   {QString     mess  = TR("<center><img source=\"./AlertLogoViergeAttention.png\"></center><p>Le chemin de la documentation : <br><b>");
                mess += in_ini_path + " </b><br>";
                mess += TR("n'a pas été trouvé.<br> Vérifiez si celui-ci existe ou,<br>est bien précisé dans le fichier : <br><b>");
                mess += "<font color=\"#e80d0d\">" + m_PathManuIni + TR("</font></b> dans la section : <b>[Configuration]</b> après la variable : <b>Path Doc</b>");
   CGenTools::MyMessageAlert(0, TR("ERREUR : Chemin de la documentation non trouvé"), m_PathRessources, mess,TR("Annu&ler"));
   m_PathDoc = "";
  }

 //................... rechercher liste des pages HTML de l'aide ............................................................
 if (argc > 16)
    {
     if (CGestIni::IsUtf8(argv[16 ])) m_ListDoc =  QString::fromUtf8 (argv[ 16 ]);
     else                             m_ListDoc =  argv[ 16 ];
    }

 //................... rechercher le nom de la page de départ ............................................................
 if (argc > 13)
    {if (CGestIni::IsUtf8(argv[13 ])) m_StartDoc =  QString::fromUtf8 (argv[ 13 ]);
     else                             m_StartDoc =  argv[ 13 ];
    }
 else if (CGestIni::Param_ReadParam(m_ManuParam, "Configuration", "StartDoc", &val1)== 0)
    { qDebug (val1);
      m_StartDoc = val1;
    }

}

//-------------------------------------------- PlaceWindows --------------------------------------------------
void CManuAppCtx::PlaceWindows()
{QString x,y,w,h;
 if (CGestIni::Param_ReadParam(m_ManuParam, "Configuration", "Windows_Pos", &x,&y,&w,&h)==0) m_pDlg_Manu->setGeometry (  x.toInt(),  y.toInt(),  w.toInt(),  h.toInt() );
}

//-------------------------------------------- GetListDoc --------------------------------------------------
int CManuAppCtx::GetListDoc(QStringList &lst)
{QString text     = "";
 QString val1     = "";
 if (CGestIni::Param_UpdateFromDisk(m_PathCache + "/" + m_ListDoc, text )!=0)
    { CGestIni::Param_GetList(text, "Fichiers Doc", "",  lst );
      if (CGestIni::Param_ReadParam(text, "Configuration", "IsQT_Doc", &val1)== 0)
         {if (val1.lower().at(0)=='n') m_IsQT_page = 0;
         }
    }
 return lst.count();
}

//-------------------------------------------- AfficherContexte --------------------------------------------------
void CManuAppCtx::AfficherContexte()
{QString        mess  = TR("<center><img source=\"./InfoCtx.png\"><p><hr><font color=\"#2497d8\"> <span style=\"font-size:18pt;font-weight:600\"><u>Variables et contexte de manu </u></span></font></center>");
                mess += "<b>m_PathAppli : </b>"      + m_PathAppli + "<br>";
                mess += "<b>m_PathCache : </b>"      + m_PathCache + "<br>";
                mess += "<b>m_PathDoc : </b>"        + m_PathDoc + "<br>";
                mess += "<b>m_StartDoc : </b>"       + m_StartDoc + "<br>";
                mess += "<b>m_ListDoc : </b>"        + m_ListDoc + "<br>";
                mess += "<b>m_SommaireDoc : </b>"    + m_SommaireDoc + "<br>";
                mess += "<b>m_IsQT_page : </b>"      + QString::number(m_IsQT_page) + "<br>";
                mess += "<b>m_StaysOnTop : </b>"     + m_StaysOnTop + "<br>";
               //mess += "<b>m_DocList : </b><br>"   + G_pCManuApp->m_DocList + "<br>";
                //mess += "<b>m_PathAppli : </b><br>" + G_pCManuApp->m_PathAppli + "<br>";

   CGenTools::MyMessageAlert(0, TR("A propos de manu : Gestion d'aide en ligne pour Med'in Tux"), m_PathRessources, mess, TR("&Quitter"));
}
