//  BMP
//  Copyright (C) 2005-2007 BMP development.
//
//  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.
//
//  --
//
//  The BMPx project hereby grants permission for non GPL-compatible GStreamer
//  plugins to be used and distributed together with GStreamer and BMPx. This
//  permission is above and beyond the permissions granted by the GPL license
//  BMPx is covered by.

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif //HAVE_CONFIG_H

#include <glibmm/ustring.h>
#include "bmp/types/types-bookmarks.hh"

using namespace std;
using namespace Glib;

#include "parser/libxml2-sax-base.hh"
#include "bookmarks-libxml2-sax.hh"

using namespace Bmp;
using Bmp::XPath;

namespace
{
  struct BookmarksParserContext
    : public ParseContextBase
  {
    BookmarksParserContext (Bmp::BookmarkV & bookmarks) : m_bookmarks (bookmarks) {}

    Bookmark    m_bookmark;
    BookmarkV & m_bookmarks;
  };

#define DEFAULT_REFS                                                                  \
  BookmarksParserContext & context (static_cast<BookmarksParserContext&>(_context));  \
  Bmp::BookmarkV &bookmarks G_GNUC_UNUSED (context.m_bookmarks);                      \
  Bmp::Bookmark &bookmark G_GNUC_UNUSED (context.m_bookmark);  

  //////////////////////////////////////////////////////////////////////////////

  namespace Handlers
  {
    HANDLER(bookmark)
    {
      DEFAULT_REFS
      bookmark = Bookmark(); 
      bookmark.m_title = props["title"];
      bookmark.m_url = props["href"];
      bookmark.m_id = g_ascii_strtoull (props["id"].c_str(), NULL, 10);
    }
  }

  namespace HandlersEnd
  {
    HANDLER_END(bookmark)
    {
      DEFAULT_REFS
      bookmarks.push_back (bookmark);
    }
  }
  
  HandlerPair 
  handlers_start[] = 
  {
    HandlerPair(      XPath("bookmarks/bookmark-list/bookmark"), 
                        sigc::ptr_fun (&Handlers::bookmark))
  };
  
  HandlerEndPair
  handlers_end[] =
  {
    HandlerEndPair(   XPath("bookmarks/bookmark-list/bookmark"), 
                        sigc::ptr_fun (&HandlersEnd::bookmark)) 
  };
}

namespace Bmp
{
  namespace Bookmarks
  {
    int parse (Bmp::BookmarkV & bookmarks, std::string const& data)
    {
      BookmarksParserContext context (bookmarks);

      for (unsigned int n = 0; n < G_N_ELEMENTS(handlers_start); context << handlers_start[n++]); 
      for (unsigned int n = 0; n < G_N_ELEMENTS(handlers_end); context << handlers_end[n++]); 
       
      return SaxParserBase::xml_base_parse(data, context);
    }
  }
}
