/* -*- 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: OrganiseRecipesForm.ui.h,v 1.7 2003/02/27 18:45:36 rjesse Exp $
 */

void
OrganiseRecipesForm::init()
{
   setMDICaption( caption());
   m_haveSelectionOnLeft = m_haveSelectionOnRight = false;
   
   // Populate combo boxes.
   Repository* repo = Repository::getInstance();
   QStringList cbNames = repo->getCookbookNames();
   m_comboLeft->insertStringList( cbNames);
   m_comboRight->insertStringList( cbNames);
   /*
     Insert an empty item to each combo in order to allow user to
     move recipes into a new cookbook.
   */
   m_comboLeft->insertItem( "");
   m_comboRight->insertItem( "");
   m_leftCB = m_rightCB = 0;
   slotShowLeft( m_comboLeft->currentText());
   slotShowRight( m_comboRight->currentText());
   m_leftList->setSelectionMode( QListBox::Extended);
   m_rightList->setSelectionMode( QListBox::Extended);

   // Disable the tool buttons initially.
   m_btnLeftRight->setEnabled( false);
   m_btnRightLeft->setEnabled( false);
   m_btnRemove->setEnabled( false);
   m_btnSaveLeft->setEnabled( true);
   m_btnSaveRight->setEnabled( true);
   m_btnRenameLeft->setEnabled( true);
   m_btnRenameRight->setEnabled( true);
} // init()

void
OrganiseRecipesForm::slotMoveLeftRight()
{
   // Build StringList containing all selected recipes in left combo.
   QStringList selected;
   for (uint i = 0; i < m_leftList->count(); i++) {
      if (m_leftList->isSelected( i))
         selected.append( m_leftList->text( i));
   }

   // Retrieve both cookbooks from repo.
   Repository* repo = Repository::getInstance();
   RecipeModel* cbLeft = repo->getCookbook( m_comboLeft->currentText());
   RecipeModel* cbRight = repo->getCookbook( m_comboRight->currentText());

   // For each recipe in StringList: add to right cb, remove from left cb.
   for (QStringList::const_iterator it = selected.begin(); it != selected.end(); it++) {
      QString curStr = *it;
      Recipe cur = cbLeft->findByTitle( curStr);
      cbRight->add( cur);
      cbLeft->remove( cur);
   }
   m_btnSaveLeft->setEnabled( true);
   m_btnSaveRight->setEnabled( true);
} // slotMoveLeftRight()

void
OrganiseRecipesForm::slotMoveRightLeft()
{
   // Build StringList containing all selected recipes in left combo.
   QStringList selected;
   for (uint i = 0; i < m_rightList->count(); i++) {
      if (m_rightList->isSelected( i))
         selected.append( m_rightList->text( i));
   }

   // Retrieve both cookbooks from repo.
   Repository* repo = Repository::getInstance();
   RecipeModel* cbLeft = repo->getCookbook( m_comboLeft->currentText());
   RecipeModel* cbRight = repo->getCookbook( m_comboRight->currentText());

   // For each recipe in StringList: add to right cb, remove from left cb.
   for (QStringList::const_iterator it = selected.begin(); it != selected.end(); it++) {
      QString curStr = *it;
      Recipe cur = cbRight->findByTitle( curStr);
      cbLeft->add( cur);
      cbRight->remove( cur);
   }
   m_btnSaveLeft->setEnabled( true);
   m_btnSaveRight->setEnabled( true);
} // slotMoveRightLeft()

void
OrganiseRecipesForm::slotRemoveSelected()
{
   Repository* repo = Repository::getInstance();
   
   // (1) left
   QStringList selected;
   for (uint i = 0; i < m_leftList->count(); i++) {
      if (m_leftList->isSelected( i))
         selected.append( m_leftList->text( i));
   }
   qDebug( "left selection list created: %s", selected.join( " ").latin1());
   for (QStringList::const_iterator it = selected.begin(); it != selected.end(); it++) {
      QString curStr = *it;
      RecipeModel* cbLeft = repo->getCookbook( m_comboLeft->currentText());
      Recipe cur = cbLeft->findByTitle( curStr);
      qDebug( "found the recipe for %s", cur.title().latin1());
      cbLeft->remove( cur);
      qDebug( "%s removed from left.", curStr.latin1());      
      slotNotifyLeft();
      qDebug( "sent notification for left");
   }
   m_btnSaveLeft->setEnabled( true);
   selected.clear();
   
   // (2) right
   for (uint j = 0; j < m_rightList->count(); j++) {
      if (m_rightList->isSelected( j))
         selected.append( m_rightList->text( j));
   }
   qDebug( "right selection list created: %s", selected.join( " ").latin1());
   for (QStringList::const_iterator it2 = selected.begin(); it2 != selected.end(); it2++) {
      QString curStr = *it2;
      RecipeModel* cbRight = repo->getCookbook( m_comboRight->currentText());
      Recipe cur = cbRight->findByTitle( curStr);
      cbRight->remove( cur);
      slotNotifyRight();
   }
   m_btnSaveRight->setEnabled( true);
   qDebug( "slotRemoveSelected() done.");
} // slotRemoveSelected()

/*!
  Check whether something is (still) selected and enable/disable
  buttons accordingly.
*/
void
OrganiseRecipesForm::slotLeftSelectionChanged()
{
   m_haveSelectionOnLeft = false;
   for (uint i = 0; i < m_leftList->count(); i++) {
      if (m_leftList->isSelected( i)) {
         m_haveSelectionOnLeft = true;
         break;
      }
   }
   if (m_haveSelectionOnLeft)
      m_btnLeftRight->setEnabled( true);
   else
      m_btnLeftRight->setEnabled( false);
   if (m_haveSelectionOnLeft || m_haveSelectionOnRight)
      m_btnRemove->setEnabled( true);
   else
      m_btnRemove->setEnabled( false);
} // slotLeftSelectionChanged()

/*!
  Check whether something is (still) selected and enable/disable
  buttons accordingly.
*/
void
OrganiseRecipesForm::slotRightSelectionChanged()
{
   m_haveSelectionOnRight = false;
   for (uint i = 0; i < m_rightList->count(); i++) {
      if (m_rightList->isSelected( i)) {
         m_haveSelectionOnRight = true;
         break;
      }
   }
   if (m_haveSelectionOnRight)
      m_btnRightLeft->setEnabled( true);
   else
      m_btnRightLeft->setEnabled( false);
   if (m_haveSelectionOnLeft || m_haveSelectionOnRight)
      m_btnRemove->setEnabled( true);
   else
      m_btnRemove->setEnabled( false);
}

void
OrganiseRecipesForm::slotSaveLeft()
{
   QString cbName = m_comboLeft->currentText();
   RecipeModel cb = Repository::getInstance()->getCookbook( cbName);
   cb.saveToFile();

   // Disable save button until something has changed.
   m_btnSaveLeft->setEnabled( false);
} // slotSaveLeft()

void
OrganiseRecipesForm::slotSaveRight()
{
   QString cbName = m_comboRight->currentText();
   RecipeModel cb = Repository::getInstance()->getCookbook( cbName);
   cb.saveToFile();

   // Disable save button until something has changed.
   m_btnSaveRight->setEnabled( false);
} // slotSaveRight()

void
OrganiseRecipesForm::slotRenameLeft()
{
   QString oldName = m_comboLeft->currentText();
   int oldIdx = m_comboLeft->currentItem();
   bool ok = false;
   QString newName = QInputDialog::getText(
      tr( "Mango"),
      tr( "Please enter the new name of the cookbook:"),
      QLineEdit::Normal, oldName, &ok, this);
   Repository* repo = Repository::getInstance();
   RecipeModel* cb = repo->getCookbook( oldName);
   cb->setName( newName);
   m_comboLeft->changeItem( newName, oldIdx);
   repo->removeCookbook( oldName);
   repo->insertCookbook( cb);
   
   /*
     Check whether the name was initially empty. If so, reinsert
     an empty name into the combo box.
   */
   if (oldName.isEmpty())
      m_comboLeft->insertItem( "");
} // slotRenameLeft()

void
OrganiseRecipesForm::slotRenameRight()
{
   QString oldName = m_comboRight->currentText();
   int oldIdx = m_comboRight->currentItem();
   bool ok = false;
   QString newName = QInputDialog::getText(
      tr( "Mango"),
      tr( "Please enter the new name of the cookbook:"),
      QLineEdit::Normal, oldName, &ok, this);
   Repository* repo = Repository::getInstance();
   RecipeModel* cb = repo->getCookbook( oldName);
   cb->setName( newName);
   m_comboLeft->changeItem( newName, oldIdx);
   repo->removeCookbook( oldName);
   repo->insertCookbook( cb);
   
   /*
     Check whether the name was initially empty. If so, reinsert
     an empty name into the combo box.
   */
   if (oldName.isEmpty())
      m_comboRight->insertItem( "");
} // slotRenameRight()

/*!
  Update the recipe list for the left cookbook. This updates the m_leftCB pointer, too.
*/
void
OrganiseRecipesForm::slotShowLeft( const QString& cbName)
{
   // disconnect any possibly obsoleted MVC chain
   if (m_leftCB) disconnect( m_leftCB, 0, this, 0);
   
   m_leftList->clear();
   
   // Retrieve cb from repo.
   try {
      m_leftCB = Repository::getInstance()->getCookbook( cbName);
   }
   catch (Error& e) {
      QMessageBox::critical( this,
                             tr( "Error retrieving recipe list."),
                             e.what());
      return;
   }

   // For each recipe in cb: Add name to list box.
   RecipeModel::RecipeList rl = m_leftCB->getRecipeList();
   for (RecipeModel::RecipeList::const_iterator it = rl.begin();
        it != rl.end();
        it++)
   {
      m_leftList->insertItem( (*it).title());
   }

   // MVC pattern.
   connect( m_leftCB, SIGNAL( notify()), this, SLOT( slotNotifyLeft()));
} // slotShowLeft()

void
OrganiseRecipesForm::slotNotifyLeft()
{
   slotShowLeft( m_comboLeft->currentText());
}

/*!
  Update the recipe list for the right cookbook. This updates the m_rightCB pointer, too.
*/
void
OrganiseRecipesForm::slotShowRight( const QString& cbName)
{
   // disconnect any possibly obsoleted MVC chain
   if (m_rightCB) disconnect( m_rightCB, 0, this, 0);
   
   m_rightList->clear();
   
   // Retrieve cb from repo.
   try {
      m_rightCB = Repository::getInstance()->getCookbook( cbName);
   }
   catch (Error& e) {
      QMessageBox::critical( this,
                             tr( "Error retrieving recipe list."),
                             e.what());
      return;
   }

   // For each recipe in cb: Add name to list box.
   RecipeModel::RecipeList rl = m_rightCB->getRecipeList();
   for (RecipeModel::RecipeList::const_iterator it = rl.begin();
        it != rl.end();
        it++)
   {
      m_rightList->insertItem( (*it).title());
   }

   // MVC pattern.
   connect( m_rightCB, SIGNAL( notify()), this, SLOT( slotNotifyRight()));
} // slotShowRight()

void
OrganiseRecipesForm::slotNotifyRight()
{
   slotShowRight( m_comboRight->currentText());
}
