+/*
+ * tmdb_movie.c
+ *
+ * This file is part of maevies
+ * Copyright (C) 2009 Simón Pena <spenap@gmail.com>
+ *
+ * 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 3 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.
+ *
+ */
+
+#include "tmdb_movie.h"
+
+void query_tmdb(const gchar *name);
+
+static void parse_child_element(gpointer key, gpointer element, gpointer data);
+
+RestXmlNode *get_xml(RestProxyCall *call);
+
+TMDBMovie *tmdb_get_info(const gchar *name) {
+
+ TMDBMovie *movie = g_new0(TMDBMovie, 1);
+
+ movie->rating = 10;
+ movie->released = g_date_new_dmy(1, G_DATE_FEBRUARY, 2009);
+
+ query_tmdb(name);
+
+ return movie;
+}
+
+void query_tmdb(const gchar *name) {
+
+ RestProxy *proxy;
+ RestProxyCall *call;
+ gchar *tmdb_function;
+ const gchar *response;
+ RestXmlNode *root;
+ gssize len;
+
+ /* Provider initialization, should be refactored. Maybe it can be reused between calls */
+ proxy = rest_proxy_new(TMDB_SERVICE_URL, FALSE);
+ call = rest_proxy_new_call(proxy);
+
+ /* Generate the function string */
+ tmdb_function = g_strdup_printf("%s/%s/%s/%s/%s", TMDB_SEARCH_METHOD,
+ TMDB_LANGUAGE, TMDB_FORMAT, TMDB_API_KEY, name);
+
+ rest_proxy_call_set_function(call, tmdb_function);
+
+ /* The actual call */
+ rest_proxy_call_run(call, NULL, NULL);
+
+ get_xml(call);
+
+ /* Object disposal, should be refactored. */
+ g_object_unref(call);
+ g_object_unref(proxy);
+}
+
+RestXmlNode *get_xml(RestProxyCall *call) {
+ RestXmlParser *parser;
+ RestXmlNode *root;
+ GError *error = NULL;
+
+ parser = rest_xml_parser_new();
+
+ root = rest_xml_parser_parse_from_data(parser, rest_proxy_call_get_payload(
+ call), rest_proxy_call_get_payload_length(call));
+
+ g_hash_table_foreach(root->children, (GHFunc) parse_child_element, NULL);
+
+ g_object_unref(parser);
+
+ return root;
+}
+
+static void parse_child_element(gpointer key, gpointer element, gpointer data) {
+
+ RestXmlNode *node = (RestXmlNode*) element;
+
+ g_print("%s: ", key);
+ if (g_hash_table_size(node->children) == 0) {
+ g_print("%s", node->content);
+ }
+ g_print("\n");
+ g_hash_table_foreach(node->children, (GHFunc) parse_child_element, NULL);
+}