/***************************************************************************
                          custom_rings.cpp  -  custom rings
                             -------------------
    begin                : Fri Aug 30 2002
    copyright            : (C) 2002 by Adam Tenderholt
    email                : atenderholt@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


#include "custom_rings.h"
#include <kstandarddirs.h>
#include <qtextstream.h>
#include <qfile.h>
#include <iostream>

CustomRings::CustomRings() {

//no real need to initialize?

}

CustomRings::~CustomRings() {

//

}

QString CustomRings::getTitle(int x) {

  if((x<0)||(x>titles.size()))
    return QString();

  return titles[x];
}

QString CustomRings::getRingFile(int x) {

  if((x<0)||(x>ringNames.size()))
    return QString();

  return ringNames[x];
}

void CustomRings::load() {

  QString tag;
  int i=0;

  KStandardDirs *dir=KGlobal::dirs();
  QString file=dir->localkdedir();
  file+="share/apps/kdrawchem/customrings";

  QFile f(file);

  if(!f.exists())    //if file DOESN'T exist
    createNewFile();

  if ( f.open(IO_ReadOnly) ) {    // file opened successfully
    QTextStream t( &f );        // use a text stream

    tag=t.readLine();   //should be <rings>

    while(tag=="<rings>") {   //while still reading document

      tag=t.readLine();   //should be <entry>

      while(tag=="<entry>") {    //while still reading entries

        tag=t.readLine();  //first comes <title>Title</title>
        int one=tag.find("<title>");
        int two=tag.find("</title>");

        tag.remove(two,99);
        tag.remove(one,7);

        titles.push_back(tag);

        tag=t.readLine();  //second comes <file>Filename</file>
        one=tag.find("<file>");
        two=tag.find("</file>");

        tag.remove(two,99);
        tag.remove(one,6);

        ringNames.push_back(tag);

        tag=t.readLine();  //should be </entry>
        tag=t.readLine();  //should be <entry> or <rings>


      }
    }
  }
        f.close();
        std::cerr << titles.size() << " " << ringNames.size() << std::endl;
}

void CustomRings::save() {

  KStandardDirs* dir=KGlobal::dirs();

  QString file=dir->localkdedir();
  file+="share/apps/kdrawchem";

  file+="/customrings";

  QFile f(file);

  if(f.open(IO_WriteOnly)) { //make sure file exists before writing

    QTextStream t(&f);

    t << "<rings>\n";

    for(int i=0;i<ringNames.size();i++) {

      std::cerr << "I: " << i << " ,titles.size()" << titles.size() << std::endl;

      t << "<entry>\n";
      t << "<title>";
      t << titles[i];
      t << "</title>\n";
      t << "<file>" << ringNames[i] << "</file>\n";
      t << "</entry>\n";
    }

    t << "</rings>";
  }

  f.close();//write <rings>

}

void CustomRings::createNewFile() {

  KStandardDirs* dir=KGlobal::dirs();

  QString file=dir->localkdedir();
  file+="share/apps/kdrawchem";

  dir->makeDir(file);
  dir->makeDir(file+="/rings");

  file+="/customrings";

  QFile f(file);

  if(f.open(IO_WriteOnly)) { //make sure file exists before writing

    QTextStream t(&f);

    t << "<rings>\n</rings>";

  }

  f.close();

}

void CustomRings::addEntry(QString title,QString filename) {

  std::cerr << size() << " " << title << " " << filename << std::endl;
  titles.push_back(title);
  ringNames.push_back(filename);
  std::cerr << "new size: " << size() << std::endl;

}


//eof

