/***************************************************************************
 *   Copyright (C) 2005 by Sevin Roland                                    *
 *   ro@vaio_ro                                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef  CMOTEUR_AGENDA_H
#define  CMOTEUR_AGENDA_H

#include <qobject.h>
#include <qapplication.h>
#include <qsqldatabase.h>
#include <qstring.h>
#include <qlistview.h>
#include <qlabel.h>
#include <qdatetime.h>
#include <qcstring.h>
#include <qtextedit.h>
#include <qsqlcursor.h>
#include <qsqlrecord.h>
#include <qdatetime.h>
#include <qcursor.h>

#define BASE_AGENDA          "BaseAgenda"
#define SKIP_BLANK_CAR(a)     while( *(a) && (*(a)==' ' || *(a)=='\t' || *(a)== 96) ) (a)++
#define NEXT_LINE(a)          while( *(a) && *(a)!='\r' && *(a)!='\n')(a)++; while( *(a) && (*(a)=='\r'|| *(a)=='\n'))(a)++

 //===================================== RendezVous ========================================================================
 class  RendezVous
 {

  public:
     //RendezVous(){};
     RendezVous(const  QDateTime  &date = QDateTime(),
                const  long       &duree= 0,
                const  QString    &nom=0,
                const  QString    &prenom=0,
                const  QString    &tel=0,
                const  QString    &note=0,
                const  QString    &guid=0,
                const  QString    &prisPar=0,
                const  QString    &prisAvec=0,
                const  QString    &type=0,
                const  QString    &primKey=0
               )
         :m_date (date),
          m_Duree(duree),
          m_Nom(nom),
          m_Prenom(prenom),
          m_Tel(tel),
          m_Note(note),
          m_GUID(guid),
          m_PrisPar(prisPar),
          m_PrisAvec(prisAvec),
          m_Type(type),
          m_PrimKey(primKey)
     {
     };
  QDateTime  m_date;
  long       m_Duree;
  QString    m_Nom;
  QString    m_Prenom;
  QString    m_Tel;
  QString    m_Note;
  QString    m_GUID;
  QString    m_PrisPar;
  QString    m_PrisAvec;
  QString    m_Type;
  QString    m_PrimKey;
 };

 typedef QMap<QString, QString> MAP_COLOR;
 typedef QPtrList <RendezVous>  RDV_LIST;

//===================================== CMoteurAgenda ========================================================================
class CMoteurAgenda
{
 public:
    enum listMode {
        OnlyDateWhithName,
        AllDates,
        DateWhithAnyName
    };
 //............................... METHODES .........................................................................

 public:

  //................................... configuration et connection de la base de données ..............................
  CMoteurAgenda();
  CMoteurAgenda(const char* driver,        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
                const char* DataBaseName,  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
                const char* user,          // = "root"
                const char* password,      // = ""
                const char* hostname,
                const char* confFile,
                const char* baseLabel,
                QString*    errMess);

  ~CMoteurAgenda();
  void initBase   (const char* driver,        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
                   const char* DataBaseName,  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
                   const char* user,          // = "root"
                   const char* password,      // = ""
                   const char* hostname,
                   const char* confFile,
                   const char* baseLabel,
                   QString*    errMess);

  QSqlDatabase*  BaseConnect(const char* driver,        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
                             const char* DataBaseName,  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
                             const char* user,          // = "root"
                             const char* pasword,       // = ""
                             const char* hostname,      // = "localhost"
                             QString*    errMess ,
                             QString     baseLabel);

  int             SetConfBase(const char* confFile, QString *errMess);
  char           *SetConfBase_SetProperties(char *pt, QString &propertie, const char *token, int *line , QString &err);
  static void     GotoDebug();
  int             MASK_Append(const RendezVous &rendezVous, QString *errMess = 0);
  int             MASK_GetDay_RDV_List(const QDate &date, const QString &user, RDV_LIST &rdvList, QString *errMess =0);
  int             MASK_DayDelete( int dayOfWeek, const QString &user, QString *errMess =0);
  int             COL_Create(const QString name, const QString color, QString *errMess =0);
  int             COL_Get_List(MAP_COLOR &colorProfils, QString *errMess =0);
  int             COL_Delete( const QString name, QString *errMess =0);
  QString         RDV_IsThisRendezVousExist(const RendezVous &rendezVous, QString *errMess =0);
  int             RDV_Get_List(QDate date, const QString &user, RDV_LIST &rdvList, int mode = CMoteurAgenda::AllDates, QString *errMess =0 );
  int             RDV_Create(const RendezVous &rendezVous, QString *errMess =0 );
  int             RDV_Update(const RendezVous &rendezVous, QString *errMess =0 );
  int             RDV_Delete( const QString &primKey, QString *errMess =0);
  static QString  dayOfWeek(int dayOfWeek);
  void            OutSQL_error(QSqlQuery &cur, const char *messFunc =0, const char *requete=0);
  void            SetDaysHeight(int = 15);
  void            SetSpaceBetwenDays(int = 1);
  void            SetMinDaysHeight(int = 15);
  int             GetDaysHeight();
  int             GetSpaceBetwenDays();
  int             GetMinDaysHeight();

//............................... DATA .............................................................................
 public:
  int           m_SpaceBetwenDays;
  int           m_DaysHeight;
  int           m_MinDaysHeight;
  //......................... parametres de connexion ...............................................................
  QSqlDatabase* m_DataBase;
  QString       m_DriverName;
  QString       m_BaseName;
  QString       m_UserName;
  QString       m_PassWord;
  QString       m_HostName;

  //......................... configuration des tables ...............................................................
  QString      m_AGENDA_TBL_NAME;         // non de la table DU FICHIER GENERAL D'INDEX DES DOSSIERS
  QString      m_AGENDA_DATETIME;         // date et heure du RDV
  QString      m_AGENDA_DUREE;            // duree (en minutes)  RDV
  QString      m_AGENDA_GUID;             // un champ GUID unique qualifiant le numero de dossier du patient ayant ce RDV (si déjà connu)
  QString      m_AGENDA_NOM;              // un champ NOM   qualifiant le nom du dossier
  QString      m_AGENDA_PRENOM;           // un champ PRENOM   qualifiant le prenom du dossier
  QString      m_AGENDA_TEL;              // téléphone (pour annuler)
  QString      m_AGENDA_PRIS_PAR;         // Utilisateur du programme
  QString      m_AGENDA_PRIS_AVEC;        // Médecin avec lequel le RDV a été pris
  QString      m_AGENDA_TYPE;             // Type de RDV
  QString      m_AGENDA_NOTE;             // Note
  QString      m_AGENDA_PRIM_KEY;         // une clef primaire

  QString      m_COLOR_PROFIL_TBL_NAME;
  QString      m_COLOR_PROFIL_NAME;
  QString      m_COLOR_PROFIL_COLOR;

  QString      m_MASK_TBL_NAME;
  QString      m_MASK_PRIM_KEY;
  QString      m_MASK_MOTIF;
  QString      m_MASK_TYPE;
  QString      m_MASK_HEURE;
  QString      m_MASK_DUREE;
  QString      m_MASK_USER;
  QString      m_MASK_DAYOFWEEK;


private:
  QString      m_BaseLabel;
};


#endif
