/* -*- 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: PasteRawTextForm.ui.h,v 1.6 2003/04/03 17:21:05 rjesse Exp $
 */

/*!
  \todo Add a QDragObject subclass that is capable of encoding recipes
  in all supported formats as outlined in Qt's dnd.html.
*/
void
PasteRawTextForm::init()
{
   setMDICaption( caption());
   // initialise with any possible text content of the clipboard
   mlePastedText->setText( QApplication::clipboard()->text());
   
   setAcceptDrops( true);       // allow for "Drag and Drop"
} // init()

void PasteRawTextForm::slotApply()
{
   if (!mlePastedText->edited())
      return;

   // Copy text into a string list for further processing.
   QStringList lines;
   for (int l = 0; l <= mlePastedText->numLines(); l++)
      lines << mlePastedText->textLine( l);
   parseText( lines);
   close();
} // slotApply()

void PasteRawTextForm::slotClear()
{
   int a = mlePastedText->numLines();
   while (a > 0) {
      mlePastedText->removeLine( 0);
      a--;
   }
} // slotClear()

/*!
  All that can be dragged onto this dialog is text for now
  (be it Meal-Master or Rezkonv).
*/
void
PasteRawTextForm::dragEnterEvent( QDragEnterEvent * event)
{
   event->accept( QTextDrag::canDecode( event));
} // dragEnterEvent()


void PasteRawTextForm::dropEvent( QDropEvent * event )
{
   QString text;
   if (QTextDrag::decode( event, text))
      mlePastedText->setText( text);
   else
      QMessageBox::information( this,
                                tr( "Unknown data."),
                                tr( "Sorry, but as of now, this dialog only accepts text to be dropped onto."));

}

/*!
  \todo: Ask user for name of cookbook to add found recipe(s) to. 
  Open Dialog showing both, list of found recipes as well as all
  available cookbooks. (RecipeToCookbooksDialog)
*/
void
PasteRawTextForm::parseText( const QStringList & lines )
{
   // Check format of the text. We either accept Meal-Master or Rezkonv.
   if (lines.grep( QRegExp( "^(MMMMM|-----)")).size() > 1)
      parseMM( lines);
   else if (lines.grep( QRegExp( "^(=====)")).size() > 1)
      parseRK( lines);
   else
      QMessageBox::warning( this,
                            tr( "Parse error."),
                            tr( "Could not find any recipes in Meal-Master or Rezkonv format."));
} // parseText()

void
PasteRawTextForm::parseMM( const QStringList & lines )
{
   MMImporter mmi;
   mmi.setRawString( lines);
   try {
      RecipeModel* rm = new RecipeModel( mmi.apply(), true);
      Repository::getInstance()->insertCookbook( rm);
      QMessageBox::information( this,
                                tr( "Read Meal-Master recipe(s)."),
                                tr( "%1 Meal-Master recipe(s) found.", "The argument is the number of recipes found.")
                                .arg( rm->getRecipeList().size()));
   }
   catch( Error& e) {
      QMessageBox::warning( this,
                            tr( "Error reading Meal-Master recipe(s)."),
                            e.what());
   }
} // parseMM()

void
PasteRawTextForm::parseRK( const QStringList & lines )
{
   RKImporter rki;
   rki.setRawString( lines);
   try {
      RecipeModel* rm = new RecipeModel( rki.apply(), true);
      Repository::getInstance()->insertCookbook( rm);
      QMessageBox::information( this,
                                tr( "Read Meal-Master recipe(s)."),
                                tr( "%1 Rezkonv recipe(s) found.", "The argument is the number of recipes found.")
                                .arg( rm->getRecipeList().size()));
   }
   catch( Error& e) {
      QMessageBox::warning( this,
                            tr( "Error reading Rezkonv recipe(s)."),
                            e.what());
   }
} // parseRK()
