/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008 The gstreamermm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gst/gstenumtypes.h>
_PINCLUDE(gstreamermm/private/object_p.h)

namespace Gst
{

static gboolean Index_Filter_gstreamermm_callback(GstIndex* index, GstIndexEntry* entry, void* data)
{
  Index::SlotFilter * the_slot = static_cast<Index::SlotFilter*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (*the_slot)(Glib::wrap(index, true), Glib::wrap(entry, true));
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

static void Index_Filter_gstreamermm_callback_destroy(void* data)
{
  Index::SlotFilter* the_slot = static_cast<Index::SlotFilter*>(data);
  if(the_slot)
    delete the_slot;
}

static gboolean Index_Resolver_gstreamermm_callback(GstIndex *index, GstObject *writer, gchar **writer_string, void* data)
{
  Index::SlotResolver * the_slot = static_cast<Index::SlotResolver*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    Glib::ustring cpp_writer_string;

    const gboolean result =
      (*the_slot)(Glib::wrap(index, true), Glib::wrap(writer, true),
        cpp_writer_string);

    if(writer_string)
      *writer_string = g_strdup(cpp_writer_string.c_str());

    return result;
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

static void Index_Resolver_gstreamermm_callback_destroy(void* data)
{
  delete static_cast<Index::SlotResolver*>(data);
}

bool Index::is_readable() const
{
  return GST_INDEX_IS_READABLE(gobj());
}

bool Index::is_writable() const
{
  return GST_INDEX_IS_WRITABLE(gobj());
}

void Index::set_filter(const SlotFilter& slot)
{
  SlotFilter* slot_copy = new SlotFilter(slot); //Deleted in Index_Filter_gstreamermm_callback_destroy
  gst_index_set_filter_full(gobj(), &Index_Filter_gstreamermm_callback, slot_copy, &Index_Filter_gstreamermm_callback_destroy);
}

void Index::set_resolver(const SlotResolver& slot)
{
  SlotResolver* slot_copy = new SlotResolver(slot); 
  gst_index_set_resolver_full(gobj(), &Index_Resolver_gstreamermm_callback, slot_copy, &Index_Resolver_gstreamermm_callback_destroy);
}

} //namespace Gst
