/* 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/gstxml.h>
#include <gstreamermm/object.h>
#include <libxml++/document.h>

_DEFS(gstreamermm,gst)

namespace Gst
{

class Element;

/** Gst::XML - XML save/restore operations of pipelines.
 * GStreamer pipelines can be saved to xml files using write_to_stream(). They
 * can be loaded back using parse_doc() / parse_file() / parse_memory().
 * Additionally one can load saved pipelines into the gst-editor to inspect
 * the graph.
 *
 * Gst::Element implementations need to override Gst::Object::save_vfunc()
 * and Gst::Object::restore_vfunc().
 */
class XML : public Object
{
  _CLASS_GOBJECT(XML, GstXML, GST_XML, Object, GstObject)

protected:
  _CTOR_DEFAULT

public:
  /** Create a new Gst::XML parser object.
   * @return A Glib::RefPtr<> to a new Gst::XML object.
   */
  _WRAP_CREATE()

  /** Converts the given element into an XML presentation.
   * @param element The element to write out.
   * @return A pointer to an XML document.
   */
  static xmlDocPtr write(const Glib::RefPtr<const Gst::Element>& element);

  /** Converts the given element into XML and writes the formatted XML to an
   * open output stream.
   *
   * @param element The element to write out.
   * @param out An open std::ostream, like std::cout.
   */
  static void write_to_stream(const Glib::RefPtr<const Gst::Element>& element, std::ostream& out);

  _WRAP_METHOD(bool parse_doc(xmlpp::Document* doc, Glib::ustring& root), gst_xml_parse_doc)
  _WRAP_METHOD(bool parse_file(const std::string& filename, const Glib::ustring& root), gst_xml_parse_file)

  /** Fills the Gst::XML object with the corresponding elements from an in
   * memory XML buffer.
   *
   * @param buffer The in memory XML buffer.
   * @param root The name of the root objects to build.
   * @return true on success.
   */
  bool parse_memory(const std::string& buffer, const Glib::ustring& root);
  _IGNORE(gst_xml_parse_memory)

  _WRAP_METHOD(Glib::RefPtr<Gst::Element> get_element(const Glib::ustring& name), gst_xml_get_element)
  _WRAP_METHOD(Glib::RefPtr<const Gst::Element> get_element(const Glib::ustring& name) const, gst_xml_get_element, constversion)

#m4 _CONVERSION(`GList*',`Glib::ListHandle< Glib::RefPtr<Gst::Element> >',`$2($3, Glib::OWNERSHIP_NONE)')
  _WRAP_METHOD(Glib::ListHandle< Glib::RefPtr<Gst::Element> > get_topelements(), gst_xml_get_topelements)

#m4 _CONVERSION(`GList*',`Glib::ListHandle< Glib::RefPtr<const Gst::Element> >',`$2($3, Glib::OWNERSHIP_NONE)')
  _WRAP_METHOD(Glib::ListHandle< Glib::RefPtr<const Gst::Element> > get_topelements() const, gst_xml_get_topelements)

  _WRAP_METHOD(static Glib::RefPtr<Gst::Element> make_element(xmlpp::Node* curr, const Glib::RefPtr<Gst::Object>& parent), gst_xml_make_element)

#m4 _CONVERSION(`GstObject*',`const Glib::RefPtr<Gst::Object>&',`Glib::wrap($3, true)')
  /** Signals that a new object has been deserialized.
   */
  //TODO: use xmlpp::Node* for signal:
  _WRAP_SIGNAL(void object_loaded(const Glib::RefPtr<Gst::Object>& object, xmlNode* xml_node), "object-loaded")
};

} //namespace Gst
