/* -*- indent-tabs-mode: nil; -*-
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2000-2003, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 *
 * $Id: Importer.cpp,v 1.16 2003/10/30 14:20:18 rjesse Exp $
 */

#include "Importer.h"
#include <qfile.h>

Importer::Importer()
   : m_totalStepsEstimated(0), m_currentStepsDone(0), m_stopProcessing(false)
{
}

/*!
  Return all file extension associated with recipe formats that can currently be
  imported.
  
  The string is complete and ready to be used in file dialogs.
*/
QString
Importer::getSupportedFileExtensions()
{
   QString str( tr( "CookML (*.cml);;"
                    "Mango (*.mango);;"
                    "Meal-Master (*.mm *.mmf);;"
                    "RecipeML (*.rml *.RecipeML);;"
                    "Rezkonv (*.rk)",
                    "This is a list of all supported extensions for a recipe file."));
   return str;
}

void
Importer::setType( const Type& type) // public
{
   m_type = type;
}

void
Importer::setFileName( const QString& fname) // public slot
{
   m_filename = fname;
   m_cookbook.setFileName( m_filename);
   setType( FILE);
}

void
Importer::setRawString( const QString& str) // public slot
{
   m_rawStringL.append( str);
   m_filename = QString::null;
   m_cookbook.setFileName( QString::null);
   setType( STRING);
}

void
Importer::setRawString( const QStringList& strl) // public slot
{
   m_rawStringL = strl;
   setType( STRING);
}

void
Importer::stop()                // public slot
{
   m_stopProcessing = true;
}   

void
Importer::file2String( QTextStream::Encoding encoding)
   throw(Error)
{
   QFile file( m_filename);
   if (!file.exists()) {
      QString s( tr( "File does not exist: %1") .arg( m_filename));
      throw Error( s);
   }
   if (!file.open( IO_ReadOnly)) {
      QString s( tr( "Could not open file: %1 (permission not set correctly?)")
                 .arg( m_filename));
      throw Error( s);
   }

   QTextStream stream( &file);
   stream.setEncoding( encoding);
   QString s;
   while (!stream.eof()) {
      s = stream.readLine();
	  m_rawStringL.append( s);
   }
   /*
     Add an empty line at the end in order to work around
     the usual DOS file that don't have it.
   */
   m_rawStringL.append( QString());

   file.close();
}
