/* -*- mode: c++; 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.h,v 1.6 2003/05/25 19:13:06 rjesse Exp $
 */
 
#ifndef IMPORTER_HPP
#define IMPORTER_HPP

#include "RecipeModel.h"
#include "Error.h"
#include <qobject.h>
#include <qstringlist.h>
#include <qtextstream.h>

/*!
  Abstract base class for importing recipes / cookbooks from files.

  For each supported file format a new subclass needs to be created.
*/
class Importer : public QObject {
      Q_OBJECT

   public:
      enum Type { FILE, STRING };
      
      //! Return recipe file extension we know of.
      static QString getSupportedFileExtensions();

      //! Constructor.
      Importer();
      //! Destructor.
      virtual ~Importer() {}

      /*!
        Set whether a string should be parsed (that could - for example -
        been copy & pasted in beforehand) or whether we need to read in
        from a file first. There should be no need to call this function
        directly as setFileName() and setRawString() already set the type.
      */
      void setType( const Type&);

      /*! 
        Read it in. Make sure to set the input source beforehand.
        \sa setFileName() setRawString()
      */
      virtual RecipeModel& apply() throw(Error) = 0;

   public slots:
      void setFileName( const QString&);
      void setRawString( const QString&);
      void setRawString( const QStringList&);
      //! Stop parsing input.
      void stop();

   signals:
      /*!
        Emitted when the parsing was started.
        \param descr describtion string for the concrete importer.
        \param toatalSteps number of total steps the importer estimates
               to be done.
      */
      void start( const QString& descr, const int totalSteps);
      /*!
        Emitted whenever a step was processed.
        \param current number of steps processed so far.
        \param total total number of steps to be done.
      */
      void progress( const int current, const int total);
      /*!
        Emitted when the parsing is done.
      */
      void done();
      
   protected:
      /*!
        Read in the header information of a cookbook. Read from m_rawString
        and write output to m_cookbook.
      */
      virtual void readCookbookHeader() = 0;
      virtual void readAllRecipes() = 0;
      virtual void readRecipe() = 0;
      virtual void readRecipeHeader() = 0;
      virtual void readRecipeBody() = 0;
      virtual void file2String( QTextStream::Encoding encoding = QTextStream::Locale) throw(Error);

      Type m_type;              //!< Parsing a file or a string?
      QString m_filename;       //!< Name of file to parse.
      QStringList m_rawStringL; //!< The raw recipe text (unparsed).
      RecipeModel m_cookbook;   //!< The parsed recipe(s).

      int m_totalStepsEstimated; //!< Total number of steps required.
      int m_currentStepsDone;   //!< Current number of steps completed.
      bool m_stopProcessing;    //!< Indicate that any parsing should be stopped.
};
#endif // IMPORTER_HPP
