/* 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 "jmmsp_vfs.h"
#include "jmmsp_debug.h"


typedef struct _Entry
{
  JMMSPReply reply;
  /* TODO: Statistics */

} Entry;


static void vfs_query (JMMSPHandler* handler, JMMSP* jmmsp, 
		       const gchar* keyword);

static JMMSPHandlerFuncs vfs_vtbl = { vfs_query, NULL, NULL, NULL};



/* **************************************** */

JMMSPVFS* 
jmmsp_vfs_new (void)
{
  JMMSPVFS* vfs;

  vfs = g_new0 (JMMSPVFS, 1);
  jmmsp_handler_init (&vfs->handler, &vfs_vtbl);
  vfs->handler.loopback = 1;
  
  return vfs;
}


void
jmmsp_vfs_delete (JMMSPVFS* vfs) 
{
  GSList* i;

  if (!vfs)
    return;

  /* Delete files */
  for (i = vfs->files; i != NULL; i = i->next)
    {
      Entry* entry = (Entry*) i->data;
      g_free (entry->reply.name);
      gnet_url_delete (entry->reply.url);
    }

  /* Remove handlers */
  jmmsp_handler_uninit (&vfs->handler);

  g_free (vfs);
}


/* **************************************** */


JMMSPVFSID
jmmsp_vfs_add (JMMSPVFS* vfs, const gchar* name, 
	       const GURL* url, gint length)
{
  Entry* entry;

  g_return_val_if_fail (vfs,  NULL);
  g_return_val_if_fail (name, NULL);
  g_return_val_if_fail (url,  NULL);

  entry = g_new0 (Entry, 1);
  entry->reply.name   = g_strdup (name);
  entry->reply.url    = gnet_url_clone (url);
  entry->reply.length = length;
  vfs->files = g_slist_prepend (vfs->files, entry);

  return entry;
}


void
jmmsp_vfs_remove (JMMSPVFS* vfs, JMMSPVFSID id)
{
  Entry* entry = (Entry*) id;

  g_return_if_fail (vfs);
  g_return_if_fail (entry);
  g_return_if_fail (g_slist_find (vfs->files, entry));

  vfs->files = g_slist_remove (vfs->files, entry);
  g_free (entry->reply.name);
  gnet_url_delete (entry->reply.url);
  g_free (entry);
}


void
jmmsp_vfs_update (JMMSPVFS* vfs, JMMSPVFSID id,
		  const gchar* name, const GURL* url, 
		  gint length)
{
  Entry* entry = (Entry*) id;

  g_return_if_fail (vfs);
  g_return_if_fail (entry);
  g_return_if_fail (name);
  g_return_if_fail (url);

  if (strcmp(entry->reply.name, name))
    {
      g_free (entry->reply.name);
      entry->reply.name = g_strdup (name);
    }

  if (!gnet_url_equal(entry->reply.url, url))
    {
      gnet_url_delete (entry->reply.url);
      entry->reply.url = gnet_url_clone (url);
    }

  entry->reply.length = length;
}




/* **************************************** */

static void
vfs_query (JMMSPHandler* handler, JMMSP* jmmsp, const gchar* keyword)
{
  JMMSPVFS* vfs = (JMMSPVFS*) handler;
  GSList* i;
  int num = 0;

  JMMSPP (0x08, "vfs_query: %s\n", keyword);

  for (i = vfs->files; i != NULL; i = i->next)
    {
      Entry* entry = (Entry*) i->data;

      if (jmmsp_reply_matches(&entry->reply, keyword))
	{
	  /* Send REPLY */
	  jmmsp_send_reply (jmmsp, keyword, &entry->reply, -1);

	  /* Count it.  If sent enough, quit */
	  ++num;
	  if (num >= JMMSP_VFS_MAX_REPLIES)
	    return;
	}
    }
}


syntax highlighted by Code2HTML, v. 0.9.1