/* -*- 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: MangoImporter.cpp,v 1.6 2003/05/11 14:46:11 rjesse Exp $
 */

#include "MangoImporter.h"
#include "Ingredient.h"
#include "Menu.h"
#include "Recipe.h"
#include "Repository.h"
#include "resources.h"
#include <qfile.h>

MangoImporter::MangoImporter()
   : Importer()
{
}

MangoImporter::~MangoImporter()
{
}

/*!
  \see file resources.h for information about file version history.
*/
RecipeModel&
MangoImporter::apply()
   throw(Error)
{
   QFile file( m_filename);
   if (! file.open( IO_ReadOnly)) {
      QString str;
      QTextOStream( &str)
         << "Could not open file: " << m_filename;
      throw Error( str);
   }
   m_stream.setDevice( &file);

   // Check file format
   QString format;
   m_stream >> format;
   if (format != "Mango Cookbook")
      throw Error( tr( "File is not in Mango format."));

   /*
      Retrieve and set QDataStream's format version.
      This is helpful for reading files that were written with 
      a different version of Qt.
    */
   Q_INT32 dsVersion;
   m_stream >> dsVersion;
   m_stream.setVersion( dsVersion);

   // Read Mango's format version
   m_stream >> m_version;
   if (m_version < MANGO_FILE_VERSION_LOWEST) {
      QString msg( tr( "File version is too old."));
      throw Error( msg);
   }
   if (m_version > MANGO_FILE_VERSION_CURRENT) {
      QString msg( tr( "The file was created with a version of Mango more recent than the one you are using. Please update."));
      throw Error( msg);
   }
   Repository::getInstance()->setActiveMangoFormatVersion( m_version);

   readCookbookHeader();
   readAllRecipes();
   file.close();
   m_cookbook.markAsModified( false);

   return m_cookbook;
} // apply()

void
MangoImporter::readCookbookHeader()
{
   QString name;
   m_stream >> name;
   m_cookbook.setName( name);
}

void
MangoImporter::readAllRecipes()
{
   Q_UINT32 numRecipes = 0;
   m_stream >> numRecipes;
   emit start( tr( "Mango import", "String used to label the progress bar."), numRecipes);

   for (Q_UINT32 c = 1; c <= numRecipes; c++) {
      Recipe r;
      m_stream >> r;
      m_cookbook.add( r);
      emit progress( c, numRecipes);
   }

   // New since Mango's format version 2: Menus.
   Q_UINT32 numMenus = 0;
   m_stream >> numMenus;
   for (Q_UINT32 mc = 1; mc <= numMenus; mc++) {
      Menu m;
      m_stream >> m;
      m_cookbook.addMenu( m);
   }
   
   emit done();
}

/*! Not used as the recipe knows itself how to read from a QDataStream. */
void
MangoImporter::readRecipe()
{
}

/*!
  Not used as the Recipe knows how to read itself from a QDataStream.
*/
void
MangoImporter::readRecipeHeader()
{
}

/*!
  Not used as the Recipe knows how to read itself from a QDataStream.
*/
void
MangoImporter::readRecipeBody()
{
}
