/* -*- mode: c++; indent-tabs-mode: nil; -*-
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2002, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 *
 * $Id: QuickSearchForm.ui.h,v 1.4 2002/12/29 18:09:24 rjesse Exp $
 */

/*!
  Constructor like function.
*/
void
QuickSearchForm::init()
{
   setMDICaption( caption());
   /* Initialise cookbook combo box. */
   cbCookbook->insertItem( tr( "All"));
   QStringList cbNames = Repository::getInstance()->getCookbookNames();
   cbCookbook->insertStringList( cbNames);
   cbCookbook->setCurrentText( tr( "All"));
} // init()

/*!
  Replace this dialog with the advanced search dialog.
*/
void
QuickSearchForm::slotAdvancedSearch()
{
    Mango* mainWin = Mango::getInstance();
    AdvancedSearchForm* win = new AdvancedSearchForm( mainWin);
    mainWin->addWindow( win);
    close();
} // slotAdvancedSearch()

/*!
  Do the search.
*/
void
QuickSearchForm::slotGo()
{
    busy->start();
    QString searchString = editSearchText->text();
    bool cs = checkCase->isChecked();

    bool searchAll = false;
    if (cbCookbook->currentText() == QString( tr( "All"))) {
       searchAll = true;
    }

    QString tmpCookbookName( tr( "Search results"));
    RecipeModel* rm = new RecipeModel( tmpCookbookName, true);
    Repository* repo = Repository::getInstance();
    QStringList cbNames = repo->getCookbookNames();
    QStringList::const_iterator cbIt = cbNames.begin();
    while (cbIt != cbNames.end()) {
       // Check whether or not we really want to search this cookbook:
       if (!searchAll)
          if ( *cbIt != cbCookbook->currentText())
             continue;
       
       RecipeModel::RecipeList curList = repo->getCookbook( *cbIt)->getRecipeList();
       RecipeModel::RecipeList::iterator recIt;
       for (recIt = curList.begin(); recIt != curList.end(); recIt++) {
          if ((*recIt).contains( searchString, cs))
             rm->add( *recIt);  // add current recipe to search result
       }
       cbIt++;                  // next cookbook
    }

    if (! rm->isEmpty()) {
       Mango* mainWin = Mango::getInstance();
       RecipeListDialog* win = new RecipeListDialog( rm, mainWin);
       mainWin->addWindow( win);
    }

    busy->stop();
    if (checkClose->isChecked()) close();
} // slotGo()
