/***************************************************************************
 *   Copyright (C) 2008 by Dr Pierre-Marie DESOMBRE                        *
 *   pm.desombre@medsyn.fr                                                 *
 *                  on a idea from Eric Maeker                             *
 *   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.             *
 ***************************************************************************/
#include "connexion.h"
#include "mdp.h"
	
#include <QProgressDialog>
#include <QMessageBox>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QApplication>
#include <QDebug>
#include <QCursor>

using namespace ConnexionSpace;
enum Warn {WarnDebugMessage = true};

Connexion::Connexion(QObject *parent) :
        QObject(parent)
{}

Connexion::~Connexion(){}

bool Connexion::connectToDrTux(){
    QString log    ;
    QString pass   ;
    QString host   ;
    QString port   ;
    QString driver ;
    QString base = "DrTuxTest";
    Mdp mdp(this);
    if (mdp.exec()==QDialog::Accepted)
    {
          log = mdp.mdpLogin();
          host = mdp.mdpHost();
          pass = mdp.mdpPassword();
          port = mdp.mdpPort();
        }
    QSqlDatabase db;
            db = QSqlDatabase::addDatabase("QMYSQL", base);
            db.setHostName(host);
            db.setDatabaseName(base);
            db.setUserName(log);
            db.setPassword(pass);
            if (!port.isEmpty()&& port != "localhost")
            {
                  db.setPort(port.toInt());
                }            
            if (WarnDebugMessage){
                qWarning() << "Connecting DrTux database" << host << base << log << pass << port;
              }
    bool dbTest = (db.isOpen()) || (db.open());
    if (dbTest) {
        qWarning() << "Connected to : "
                    << db.databaseName()
                    << __FILE__
                    << QString::number(__LINE__);
            }
    else {return false;}
    return true;
}

