/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 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 Library 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 "xmlparser.h"
#include <common/icore.h>
#include <common/settings.h>
#include <QMessageBox>
#include <QtCore>
#include <QtXml>
using namespace  Common;

static inline QString absolutePath() {return Common::ICore::instance()->settings()->applicationBundlePath();}
static inline QString exchangeFilePath() {return Common::ICore::instance()->settings()->xmlExchangeFilePath();}

XmlParser::XmlParser(){

}

XmlParser::~XmlParser(){}

void XmlParser::purgeXmlFile(){
  qDebug() << "exchangeFilePath() ="+exchangeFilePath();
  QString exchangePath = absolutePath()+"/xmlEchangeFile.xml";
  if(exchangeFilePath() != "absolute path of the exchange xml file" && exchangeFilePath() != ""){
      exchangePath = exchangeFilePath();
  }
  QFile xmlFile(exchangePath);
  if(xmlFile.exists() == false){
      QMessageBox::warning(0,trUtf8("Error"),exchangePath+trUtf8(" does not exist."),QMessageBox::Ok);
  }
  else{
    if(!xmlFile.open(QIODevice::ReadWrite|QIODevice::Truncate)){
      QMessageBox::warning(0,trUtf8("Error"),trUtf8("xmlEchangeFile.xml cannot be purged."),QMessageBox::Ok);
      return;
    }
    QTextStream out(&xmlFile);
              out.setCodec("ISO 8859-1");
              out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
                  << "<programDatas>\n"
                  << "  <programName>\n"
                  << "  </programName>\n"
                  << "  <name>\n"
                  << "  </name>\n"
                  << "  <firstname>\n"
                  << "  </firstname>\n"
                  << "  <dateofbirth>\n"
                  << "  </dateofbirth>\n"
                  << "  <uid>\n"
                  << "  </uid>\n"
                  << "  <price>\n"
                  << "  </price>\n"
                  << "  <priceCode>\n"
                  << "  </priceCode>\n"
                  << "  <user>\n"
                  << "  </user>\n"
                  << "  <otherparams>\n"
                  << "  </otherparams>\n"
                  << "  <version>\n"
                  << "  </version>\n"
                  << "</programDatas>\n";
    }
}

QHash<QString,QString> XmlParser::readXmlFile(){
  QHash<QString,QString> xmlList;
  QDomDocument doc;
  QString exchangePath = absolutePath()+"/xmlEchangeFile.xml";
  if(exchangeFilePath() != "absolute path of the exchange xml file" && exchangeFilePath() != ""){
      exchangePath = exchangeFilePath();
  }
  QFile xmlFile(exchangePath);
  if(xmlFile.exists() == false){
      QMessageBox::warning(0,trUtf8("Error"),exchangePath+trUtf8(" does not exist."),QMessageBox::Ok);
      
  }
  else{
  
    if(!xmlFile.open(QIODevice::ReadOnly)){
      QMessageBox::warning(0,trUtf8("Error"),trUtf8("xmlEchangeFile.xml not found."),QMessageBox::Ok);
    }
    if(!doc.setContent(&xmlFile)){
      xmlFile.close();
      QMessageBox::warning(0,trUtf8("Error"),trUtf8("xmlEchangeFile.xml cannot be analyzed."),QMessageBox::Ok);
      xmlList.insert("error","error");
    }
    xmlFile.close();
    QDomElement rootElement = doc.documentElement();
    for(QDomNode n = rootElement.firstChild() ; !n.isNull() ;  n = n.nextSibling()){
      QString str;
      QString tagName;
      if(n.isElement()){
          qDebug() << "it is element";
          QDomElement e = n.toElement();
          str = e.text();
          tagName = e.tagName();
          qDebug() << "str ="+str+" tag = "+tagName;
          xmlList.insert(tagName,str);
          }
    }
  }
  return xmlList;
  
}
