/* Jungle Monkey
 * Copyright (C) 1999-2001  The Regents of the University of Michigan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "jmmsearch.h"


static JMMSearch* jmmsearch_new (JMMSP* jmmsp, JMMSPCache* jmmsp_cache,
				 JMMSPSearch* jmmsp_search, JMMSPVFS* jmmsp_vfs);


JMMSearch* 
jmmsearch_create_full (BPeer* bpeer, const gchar* name, JMMSPCache* jmmsp_cache,
		       JMMSPSearch* jmmsp_search, JMMSPVFS* jmmsp_vfs)
{
  JMMSP* jmmsp;

  g_return_val_if_fail (bpeer, NULL);
  g_return_val_if_fail (name,  NULL);

  jmmsp = jmmsp_create (bpeer, name);
  if (jmmsp == NULL)
    return NULL;

  return jmmsearch_new (jmmsp, jmmsp_cache, jmmsp_search, jmmsp_vfs);
}


JMMSearch* 
jmmsearch_join_full (BPeer* bpeer, const GURL* url, JMMSPCache* jmmsp_cache,
		     JMMSPSearch* jmmsp_search, JMMSPVFS* jmmsp_vfs)
{
  JMMSP* jmmsp;

  g_return_val_if_fail (bpeer, NULL);
  g_return_val_if_fail (url,   NULL);

  jmmsp = jmmsp_join (bpeer, url);
  if (jmmsp == NULL)
    return NULL;

  return jmmsearch_new (jmmsp, jmmsp_cache, jmmsp_search, jmmsp_vfs);
}


JMMSearch* 
jmmsearch_setup_full (BPeer* bpeer, const GURL* url, JMMSPCache* jmmsp_cache,
		      JMMSPSearch* jmmsp_search, JMMSPVFS* jmmsp_vfs)
{
  JMMSP* jmmsp;

  g_return_val_if_fail (bpeer, NULL);
  g_return_val_if_fail (url,   NULL);

  jmmsp = jmmsp_setup (bpeer, url);
  if (jmmsp == NULL)
    return NULL;

  return jmmsearch_new (jmmsp, jmmsp_cache, jmmsp_search, jmmsp_vfs);
}



static JMMSearch*
jmmsearch_new (JMMSP* jmmsp, JMMSPCache* jmmsp_cache,
	       JMMSPSearch* jmmsp_search, JMMSPVFS* jmmsp_vfs)
{
  JMMSearch*   jmmsearch;


  if (!jmmsp_cache)  jmmsp_cache  = jmmsp_cache_new ();
  g_return_val_if_fail (jmmsp_cache, NULL);
  jmmsp_add_handler (jmmsp, (JMMSPHandler*) jmmsp_cache);

  if (!jmmsp_search) jmmsp_search = jmmsp_search_new ();
  g_return_val_if_fail (jmmsp_search, NULL);
  jmmsp_add_handler (jmmsp, (JMMSPHandler*) jmmsp_search);

  if (!jmmsp_vfs)    jmmsp_vfs    = jmmsp_vfs_new ();
  g_return_val_if_fail (jmmsp_vfs, NULL);
  jmmsp_add_handler (jmmsp, (JMMSPHandler*) jmmsp_vfs);

  jmmsearch               = g_new0 (JMMSearch, 1);
  jmmsearch->jmmsp        = jmmsp;
  jmmsearch->jmmsp_cache  = jmmsp_cache;
  jmmsearch->jmmsp_search = jmmsp_search;
  jmmsearch->jmmsp_vfs    = jmmsp_vfs;
  
  return jmmsearch;
}



void
jmmsearch_delete (JMMSearch* jmmsearch)
{
  if (!jmmsearch)
    return;

  jmmsp_cache_delete  (jmmsearch->jmmsp_cache);
  jmmsp_search_delete (jmmsearch->jmmsp_search);
  jmmsp_vfs_delete    (jmmsearch->jmmsp_vfs);
  jmmsp_delete        (jmmsearch->jmmsp);
  g_free (jmmsearch);
}


syntax highlighted by Code2HTML, v. 0.9.1