

#include <qmime.h>
#include <qimage.h>
#include <qlabel.h>
#include <qdir.h>
#include <qlineedit.h>
#include <qtextedit.h>
#include <qpushbutton.h>
#include <qdatetime.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include "DlgMyMessageBox.h"
#include "DlgGetString.h"
#include "CHtmlTools.h"
#include "CGestIni.h"
#include "CGenTools.h"
//---------------------------------------------- FileToQString ------------------------------------------------------------
 void CGenTools::FileToQString(const char *path, QString  &dst, int mode /* = 1 */, int htmlHeader /* = 0 */)
{QString    qspath = path;
 QString    ext    = GetExtension(qspath, 4).lower();
 CGestIni::Param_UpdateFromDisk(path, dst);

 if (ext !=".htm")
    {if (ext ==".rtf")
        {QString temp = dst;
         CHtmlTools::RtfToQtextEdit_HTML(temp, temp.length(), dst, htmlHeader);     // 0 -> sans le header
        }
     else if (mode & 2 && ext==".txt")
       {QString temp = dst;
        Convert_CRLF_To_BR(temp, dst,1);
       }
    else if (mode)
      {QString temp = dst;
       Convert_CRLF_To_BR(temp, dst);
      }
   }
}

/*------------------------------ Convert_CRLF_To_BR --------------------------------------------------------------------*/
int CGenTools::Convert_CRLF_To_BR(const char *text, QString &dstQStr, int flag /*=0*/)
{int nb_ligne = 0;
 char     *pt = (char*) text;
 //rQTextEdit   = "<html>";
 dstQStr   = "";
 while (*pt)
    { if (*pt=='\r' || *pt=='\n')
         {if (flag==0) dstQStr += "\n";
          else         dstQStr += "<br />";
          if      (*pt=='\r' &&  pt[1]=='\n' ) pt+=2;
          else if (*pt=='\n' &&  pt[1]=='\r' ) pt+=2;
          else                                 ++pt;
          nb_ligne++;
         }
     else
         {dstQStr += pt[0];
          ++pt;
         }
    }
 //rQTextEdit   += "</html>";
 return nb_ligne;
}

/*------------------------------ GetExtension --------------------------------------------------------------------------*/
/*! \brief Get the file name extension, return
 *
 *  - this framework contains on left:
 *         -# Buttons of each windows.
 *         -# and the Glossaire.
 *  - this framework contains on midle:
 *         -# all the specifics window's rubrics.
 */

QString CGenTools::GetExtension(QString &file,  int cut_to /* = -1 */)
{ // int pos = file.findRev('.');
  // if (pos!=-1) return  file.mid(pos, cut_to);
  // else         return(QString(""));
  QString ext = QFileInfo(file).extension();
  if (ext.length() == 0)  return(QString(""));
  ext = ext.prepend(".");
  if (cut_to == -1 ) return ext;
  ext.truncate(cut_to);
  return ext ;
}


//---------------------------- DialogGetString -----------------------------------------------
int CGenTools::DialogGetString(QWidget * parent, const char* title, QString &string, int x/*=0*/, int y /*=0*/)
{
 FormDlgGetString*dlg = new FormDlgGetString(parent,"GetString_Dial",TRUE);
 if (dlg ==0) return QDialog::Rejected;
 if (x && y)  dlg->move (  x, y ); 
 dlg->setCaption(title);
 dlg->lineEditString->setText(string);
 if (dlg->exec()==QDialog::Accepted)
    {string = dlg->lineEditString->text();
     delete dlg;
     return QDialog::Accepted;
    }
 delete dlg;
 return QDialog::Rejected;
}

//-------------------------------- MessageAlert -------------------------------------------------------------------------
int CGenTools::MessageAlert(QWidget * parent, const char* title, const char* message, const char* button1, const char* button2,const char* button3,const char* button4)
{FormDlgMyMessageBox *dlg = new FormDlgMyMessageBox(parent,"Alert_Dial",TRUE);   // TRUE Pour modal
 if (dlg ==0) return -1;
 dlg->pushButton1->setShown(FALSE);
 dlg->pushButton2->setShown(FALSE);
 dlg->pushButton3->setShown(FALSE);
 dlg->pushButton4->setShown(FALSE);

 if (title)    dlg->setCaption(title);
 if (message)  dlg->textEditMesage->setText(message);
 if (button1) {dlg->pushButton1->setShown(TRUE);dlg->pushButton1->setText(button1);}
 if (button2) {dlg->pushButton2->setShown(TRUE);dlg->pushButton2->setText(button2);}
 if (button3) {dlg->pushButton3->setShown(TRUE);dlg->pushButton3->setText(button3);}
 if (button4) {dlg->pushButton4->setShown(TRUE);dlg->pushButton4->setText(button4);}

 int ret = dlg->exec();

 delete dlg;
 return ret;
}
//-------------------------------- MyMessageAlert -------------------------------------------------------------------------
int CGenTools::MyMessageAlert(QWidget * parent, const char* title, const QString& mimeSourceFactoryPath, const char* message, const char* button1, const char* button2,const char* button3,const char* button4)
{FormDlgMyMessageBox *dlg = new FormDlgMyMessageBox(parent,"Alert_Dial",TRUE);   // TRUE Pour modal
 if (dlg ==0) return -1;
 dlg->pushButton1->setShown(FALSE);
 dlg->pushButton2->setShown(FALSE);
 dlg->pushButton3->setShown(FALSE);
 dlg->pushButton4->setShown(FALSE);

 if (title)    dlg->setCaption(title);
 if (mimeSourceFactoryPath.length()) dlg->textEditMesage->mimeSourceFactory()->addFilePath(mimeSourceFactoryPath);
 if (message)  dlg->textEditMesage->setText(message);
 if (button1) {dlg->pushButton1->setShown(TRUE);dlg->pushButton1->setText(button1);}
 if (button2) {dlg->pushButton2->setShown(TRUE);dlg->pushButton2->setText(button2);}
 if (button3) {dlg->pushButton3->setShown(TRUE);dlg->pushButton3->setText(button3);}
 if (button4) {dlg->pushButton4->setShown(TRUE);dlg->pushButton4->setText(button4);}

 int ret = dlg->exec();

 delete dlg;
 return ret;
}

//---------------------------- qstringLoad -------------------------------------------------
QString CGenTools::qstringLoad(const char *path, int flag/*=0*/, long *nb_ligne/*=0*/)
{QString string = "";
 QString text   = "";
 CGestIni::Param_UpdateFromDisk(path, text);
 if (text.length() <=0) return text;
 if (flag&CR_TO_HTML)
    {if (text.left(6) == "<html>") return text;
     char *pt=(char*)(const char*)text;
     while (*pt)
           { if (*pt=='\r' || *pt=='\n')
                {string += "\n";
                 if      (*pt=='\r' &&  pt[1]=='\n' ) pt+=2;
                 else if (*pt=='\n' &&  pt[1]=='\r' ) pt+=2;
                 else                                 ++pt;
                 if (nb_ligne) (*nb_ligne) ++;
                 string += "<br />";
                }
             else
                {string += pt[0];
                 ++pt;
                } //end if (*pt=='\r' || *pt=='\n')
           }
     return string;
    }
 else
    {return text;
    }
}

//------------------------------------ IsThisFolderExist --------------------------------------------------
QString CGenTools::IsThisFolderExist(const char *path, const char* folderName /*=0*/)
{QString folder = path;
 QString e ( QChar (QDir::separator()) );
 if (folderName && folderName[0])
    {if       ( folder.endsWith(e)) folder  += folderName;
     else if  ( folder.endsWith("/"))               folder  += folderName;
     else                                           folder  +=  QDir::separator() + folderName;
    }
 if ( (folder.endsWith(e) || folder.endsWith("/")) ) folder  = folder.left(folder.length()-1);
 QDir dir(folder);
 if ( dir.exists() ) return folder;
 else                return QString::null;
}

//------------------------------------ IsThisFileExist --------------------------------------------------
QString CGenTools::IsThisFileExist(const char* path,   const char* fileName   /*=0*/)
{return IsThisFolderExist(path, fileName );
}

//---------------------------- NbLignes -------------------------------------------------
int  CGenTools::NbLignes(const char *txt)
{int nb_lignes = 0;
 char      *pt = (char*)txt;
 while (*pt)
    {if (*pt=='\r' || *pt=='\n')
         {if      (*pt=='\r' &&  pt[1]=='\n' ) pt+=2;
          else if (*pt=='\n' &&  pt[1]=='\r' ) pt+=2;
          else                                 ++pt;
          ++nb_lignes;
         }
     else
         {++pt;
         }
    }
 return nb_lignes;
}

//---------------------------- LoadDataInBuffer -------------------------------------------------
char *CGenTools::LoadDataInBuffer(const char *path)
{QFile qFile(path );
 if (qFile.open( IO_ReadOnly )==FALSE)                                   return 0;
 long file_len = qFile.size();
 char    *text = new char[file_len+1];  // zero de fin
 if (text==0)                           {qFile.close ();                 return 0;}
 qFile.readBlock (text, file_len);
 qFile.close ();
 text[file_len]  = 0;
 return text;
}
