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

static void TagList_foreach_gstreamermm_callback(const GstTagList* list, const gchar *tag, void* data)
{
  Gst::TagList::SlotForeach* slot = static_cast<Gst::TagList::SlotForeach*>(data);

  const Glib::ustring tag_str = Glib::convert_const_gchar_ptr_to_ustring(tag);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    (*slot)(tag_str);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

namespace Gst
{

void TagList::add(const Glib::ustring& tag, const Glib::ValueBase& value, TagMergeMode mode)
{
  gst_tag_list_add_values(gobj(), (GstTagMergeMode) mode, tag.c_str(), value.gobj(), NULL);
}

void TagList::add(const Glib::ustring& tag, const char* data, TagMergeMode mode)
{
  gst_tag_list_add(gobj(), (GstTagMergeMode) mode, tag.c_str(), data, NULL);
}

void TagList::foreach(const SlotForeach& slot)
{
  SlotForeach* slot_copy = new SlotForeach(slot);
  gst_tag_list_foreach(gobj(), &TagList_foreach_gstreamermm_callback, slot_copy);
  delete slot_copy;
}

bool TagList::get(const Glib::ustring& tag, Glib::ValueBase& dest) const
{
  return gst_tag_list_copy_value(dest.gobj(), const_cast<GstTagList*>(gobj()),
    tag.c_str());
}

bool TagList::get(const Glib::ustring& tag, guint index,
  Glib::ValueBase& value) const
{
  const GValue* gst_value =
    gst_tag_list_get_value_index(const_cast<GstTagList*>(gobj()),
    tag.c_str(), index);
  if (gst_value)
  {
    value.init(gst_value);
    return true;
  }
  return false;
}

} //namespace Gst
