/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "ftd4linux.h"
#include "ftd_app_core.h"

/**
 * Leest de gegevens van een event die door de server werden gegeven en zet deze om naar bruikbare informatie.
 *
 * @arg     tekst       De gegevens zoals deze direct van de FTD servers werden ontvangen.
 *
 * @return  Een nieuw event met daarin de gegevens zoals die uit de gegeven tekst zijn gehaald.
 */
FtdEvent *
ftd_eventinfo_parse(FtdString *tekst)
{
    FtdEvent *event = NULL;
    xmlDocPtr doc;
    xmlNodePtr cur;

    g_assert(tekst);

    doc = xmlParseMemory(ftd_string_value(tekst), ftd_string_length(tekst));
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"ftd_eventinfo_parse(): empty document!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    if (strcasecmp(cur->name, "PostInfo")) {
        fprintf(stderr,"ftd_eventinfo_parse(): document of the wrong type, root node != PostInfo");
        xmlFreeDoc(doc);
        return NULL;
    }

    cur = cur->xmlChildrenNode;
    if (!cur) {
        g_warning("ftd_eventinfo_parse(): no children of root-node!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    while (cur) {
        if (!strcasecmp(cur->name, "Post")) {
            event = ftd_events_parse_event(doc, cur->xmlChildrenNode);
        }
        if (!strcasecmp(cur->name, "MP3")) {
            if (!event) {
                g_warning("ftd_eventinfo_parse(): event == NULL\n");
                cur = cur->next;
                continue;
            }
            event->mp3Tracks = ftd_events_parse_mp3_tracks(doc, cur->xmlChildrenNode);
        }

        cur = cur->next;
    }
    xmlFreeDoc(doc);

    return event;
}

/**
 * Leest de gegevens voor het vinden van een event zoals die direct van de FTD servers werden ontvangen en maakt deze bruikbaar.
 *
 * @arg     buffer      De gegevens zoals die direct van de FTD servers gekregen zijn.
 *
 * @return  Een nieuw event met de gegevens uit de buffer in een bruikbare vorm.
 */
FtdEvent *
ftd_eventinfo_parse_findpost(FtdString *buffer)
{
    xmlDocPtr doc;
    xmlNodePtr cur;

    FtdEvent *event = NULL;

    g_assert(core);
    g_assert(buffer);

    doc = xmlParseMemory(ftd_string_value(buffer), ftd_string_length(buffer));
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        g_warning("ftd_eventinfo_parse_findpost(): empty document!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    if (strcasecmp(cur->name, "Post")) {
        g_warning("ftd_eventinfo_parse_findpost(): document of the wrong type, root node != Post");
        xmlFreeDoc(doc);
        return NULL;
    }

    event = ftd_events_parse_event(doc, cur->xmlChildrenNode);

    ftd_category_update(event);

    xmlFreeDoc(doc);

    return event;
}
