/* 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
{

extern "C"
{

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

  try
  {
    return (*the_slot)(Glib::wrap(index, true), Glib::wrap(entry, true));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return false;
}

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

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

  try
  {
    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;
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return false;
}

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

} // extern "C"

} // anonymous namespace

namespace Gst
{

IndexAssociation::IndexAssociation()
: format(Gst::FORMAT_UNDEFINED),
  value(0)
{}

IndexAssociation::IndexAssociation(const GstIndexAssociation* castitem)
: format((castitem) ? static_cast<Gst::Format>(castitem->format) :
    Gst::FORMAT_UNDEFINED),
  value((castitem) ? castitem->value : 0)
{}

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

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

Gst::IndexEntry Index::get_assoc_entry(int id, gint64 value,
  IndexLookupMethod method, AssocFlags flags, Format format) const
{
  GstIndexEntry* gst_entry =
    gst_index_get_assoc_entry(const_cast<GstIndex*>(gobj()), id,
    (GstIndexLookupMethod) method, (GstAssocFlags) flags, (GstFormat) format,
    value);

  // Return a copy so that the original is not freed by wrapper.
  return Glib::wrap(gst_entry, true);
}

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);
}

void Index_Class::add_entry_vfunc_callback(GstIndex* self, GstIndexEntry* entry)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        Gst::IndexEntry wrappedEntry(entry, true);
        // Call the virtual member method, which derived classes might override.
        obj->add_entry_vfunc(wrappedEntry);
        return;
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
  );

  // Call the original underlying C function:
  if(base && base->add_entry)
    (*base->add_entry)(self, entry);

}
void Gst::Index::add_entry_vfunc(Gst::IndexEntry& entry) 
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
  );

  if(base && base->add_entry)
    (*base->add_entry)(gobj(),(entry).gobj_copy()); // use gobj_copy() so the copy is permanent to the GstIndex.
}

} //namespace Gst
