
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <dbus/dbus.h>


#define TYPE_FEED_HANDLER (feed_handler_get_type ())
#define FEED_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FEED_HANDLER, FeedHandler))
#define FEED_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FEED_HANDLER, FeedHandlerClass))
#define IS_FEED_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FEED_HANDLER))
#define IS_FEED_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FEED_HANDLER))
#define FEED_HANDLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FEED_HANDLER, FeedHandlerClass))

typedef struct _FeedHandler FeedHandler;
typedef struct _FeedHandlerClass FeedHandlerClass;
typedef struct _FeedHandlerPrivate FeedHandlerPrivate;
typedef struct _DBusObjectVTable _DBusObjectVTable;

struct _FeedHandler {
	GObject parent_instance;
	FeedHandlerPrivate * priv;
};

struct _FeedHandlerClass {
	GObjectClass parent_class;
};

struct _DBusObjectVTable {
	void (*register_object) (DBusConnection*, const char*, void*);
};


static gpointer feed_handler_parent_class = NULL;

GType feed_handler_get_type (void);
enum  {
	FEED_HANDLER_DUMMY_PROPERTY
};
void feed_handler_mime_open (FeedHandler* self, const char* url);
FeedHandler* feed_handler_new (void);
FeedHandler* feed_handler_construct (GType object_type);
void feed_handler_dbus_register_object (DBusConnection* connection, const char* path, void* object);
void _feed_handler_dbus_unregister (DBusConnection* connection, void* user_data);
DBusHandlerResult feed_handler_dbus_message (DBusConnection* connection, DBusMessage* message, void* object);
static DBusMessage* _dbus_feed_handler_introspect (FeedHandler* self, DBusConnection* connection, DBusMessage* message);
static DBusMessage* _dbus_feed_handler_property_get_all (FeedHandler* self, DBusConnection* connection, DBusMessage* message);
static DBusMessage* _dbus_feed_handler_mime_open (FeedHandler* self, DBusConnection* connection, DBusMessage* message);
guint _dynamic_RequestName0 (DBusGProxy* self, const char* param1, guint param2, GError** error);
void _main (void);
static void _vala_dbus_register_object (DBusConnection* connection, const char* path, void* object);
static void _vala_dbus_unregister_object (gpointer connection, GObject* object);

static const DBusObjectPathVTable _feed_handler_dbus_path_vtable = {_feed_handler_dbus_unregister, feed_handler_dbus_message};
static const _DBusObjectVTable _feed_handler_dbus_vtable = {feed_handler_dbus_register_object};


void feed_handler_mime_open (FeedHandler* self, const char* url) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (url != NULL);
	g_message ("feedhandler.vala:7: URL received: %s", url);
}


FeedHandler* feed_handler_construct (GType object_type) {
	FeedHandler * self;
	self = g_object_newv (object_type, 0, NULL);
	return self;
}


FeedHandler* feed_handler_new (void) {
	return feed_handler_construct (TYPE_FEED_HANDLER);
}


void _feed_handler_dbus_unregister (DBusConnection* connection, void* user_data) {
}


static DBusMessage* _dbus_feed_handler_introspect (FeedHandler* self, DBusConnection* connection, DBusMessage* message) {
	DBusMessage* reply;
	DBusMessageIter iter;
	GString* xml_data;
	char** children;
	int i;
	reply = dbus_message_new_method_return (message);
	dbus_message_iter_init_append (reply, &iter);
	xml_data = g_string_new ("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n");
	g_string_append (xml_data, "<node>\n<interface name=\"org.freedesktop.DBus.Introspectable\">\n  <method name=\"Introspect\">\n    <arg name=\"data\" direction=\"out\" type=\"s\"/>\n  </method>\n</interface>\n<interface name=\"org.freedesktop.DBus.Properties\">\n  <method name=\"Get\">\n    <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n    <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n    <arg name=\"value\" direction=\"out\" type=\"v\"/>\n  </method>\n  <method name=\"Set\">\n    <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n    <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n    <arg name=\"value\" direction=\"in\" type=\"v\"/>\n  </method>\n  <method name=\"GetAll\">\n    <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n    <arg name=\"props\" direction=\"out\" type=\"a{sv}\"/>\n  </method>\n</interface>\n<interface name=\"org.maemo.garage.feedhandler\">\n  <method name=\"MimeOpen\">\n    <arg name=\"url\" type=\"s\" direction=\"in\"/>\n  </method>\n</interface>\n");
	dbus_connection_list_registered (connection, g_object_get_data ((GObject *) self, "dbus_object_path"), &children);
	for (i = 0; children[i]; i++) {
		g_string_append_printf (xml_data, "<node name=\"%s\"/>\n", children[i]);
	}
	dbus_free_string_array (children);
	g_string_append (xml_data, "</node>\n");
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &xml_data->str);
	g_string_free (xml_data, TRUE);
	return reply;
}


static DBusMessage* _dbus_feed_handler_property_get_all (FeedHandler* self, DBusConnection* connection, DBusMessage* message) {
	DBusMessage* reply;
	DBusMessageIter iter, reply_iter, subiter;
	char* interface_name;
	const char* _tmp0_;
	if (strcmp (dbus_message_get_signature (message), "s")) {
		return NULL;
	}
	dbus_message_iter_init (message, &iter);
	reply = dbus_message_new_method_return (message);
	dbus_message_iter_init_append (reply, &reply_iter);
	dbus_message_iter_get_basic (&iter, &_tmp0_);
	dbus_message_iter_next (&iter);
	interface_name = g_strdup (_tmp0_);
	if (strcmp (interface_name, "org.maemo.garage.feedhandler") == 0) {
		dbus_message_iter_open_container (&reply_iter, DBUS_TYPE_ARRAY, "{sv}", &subiter);
		dbus_message_iter_close_container (&reply_iter, &subiter);
	} else {
		dbus_message_unref (reply);
		reply = NULL;
	}
	g_free (interface_name);
	return reply;
}


static DBusMessage* _dbus_feed_handler_mime_open (FeedHandler* self, DBusConnection* connection, DBusMessage* message) {
	DBusMessageIter iter;
	GError* error;
	char* url;
	const char* _tmp1_;
	DBusMessage* reply;
	error = NULL;
	if (strcmp (dbus_message_get_signature (message), "s")) {
		return NULL;
	}
	dbus_message_iter_init (message, &iter);
	url = NULL;
	dbus_message_iter_get_basic (&iter, &_tmp1_);
	dbus_message_iter_next (&iter);
	url = g_strdup (_tmp1_);
	feed_handler_mime_open (self, url);
	reply = dbus_message_new_method_return (message);
	dbus_message_iter_init_append (reply, &iter);
	url = (g_free (url), NULL);
	return reply;
}


DBusHandlerResult feed_handler_dbus_message (DBusConnection* connection, DBusMessage* message, void* object) {
	DBusMessage* reply;
	reply = NULL;
	if (dbus_message_is_method_call (message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
		reply = _dbus_feed_handler_introspect (object, connection, message);
	} else if (dbus_message_is_method_call (message, "org.freedesktop.DBus.Properties", "GetAll")) {
		reply = _dbus_feed_handler_property_get_all (object, connection, message);
	} else if (dbus_message_is_method_call (message, "org.maemo.garage.feedhandler", "MimeOpen")) {
		reply = _dbus_feed_handler_mime_open (object, connection, message);
	}
	if (reply) {
		dbus_connection_send (connection, reply, NULL);
		dbus_message_unref (reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	} else {
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}
}


void feed_handler_dbus_register_object (DBusConnection* connection, const char* path, void* object) {
	if (!g_object_get_data (object, "dbus_object_path")) {
		g_object_set_data (object, "dbus_object_path", g_strdup (path));
		dbus_connection_register_object_path (connection, path, &_feed_handler_dbus_path_vtable, object);
		g_object_weak_ref (object, _vala_dbus_unregister_object, connection);
	}
}


static void feed_handler_class_init (FeedHandlerClass * klass) {
	feed_handler_parent_class = g_type_class_peek_parent (klass);
	g_type_set_qdata (TYPE_FEED_HANDLER, g_quark_from_static_string ("DBusObjectVTable"), (void*) (&_feed_handler_dbus_vtable));
}


static void feed_handler_instance_init (FeedHandler * self) {
}


GType feed_handler_get_type (void) {
	static GType feed_handler_type_id = 0;
	if (feed_handler_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (FeedHandlerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) feed_handler_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FeedHandler), 0, (GInstanceInitFunc) feed_handler_instance_init, NULL };
		feed_handler_type_id = g_type_register_static (G_TYPE_OBJECT, "FeedHandler", &g_define_type_info, 0);
	}
	return feed_handler_type_id;
}


guint _dynamic_RequestName0 (DBusGProxy* self, const char* param1, guint param2, GError** error) {
	guint result;
	dbus_g_proxy_call (self, "RequestName", error, G_TYPE_STRING, param1, G_TYPE_UINT, param2, G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID);
	if (*error) {
		return 0U;
	}
	return result;
}


void _main (void) {
	GError * _inner_error_;
	GMainLoop* loop;
	_inner_error_ = NULL;
	loop = g_main_loop_new (NULL, FALSE);
	{
		DBusGConnection* conn;
		DBusGProxy* bus;
		guint request_name_result;
		conn = dbus_g_bus_get (DBUS_BUS_SESSION, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch0_g_error;
			goto __finally0;
		}
		bus = dbus_g_proxy_new_for_name (conn, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
		request_name_result = _dynamic_RequestName0 (bus, "org.maemo.garage.feedhandler", (guint) 0, &_inner_error_);
		if (_inner_error_ != NULL) {
			(conn == NULL) ? NULL : (conn = (dbus_g_connection_unref (conn), NULL));
			(bus == NULL) ? NULL : (bus = (g_object_unref (bus), NULL));
			goto __catch0_g_error;
			goto __finally0;
		}
		if (request_name_result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
			FeedHandler* server;
			server = feed_handler_new ();
			_vala_dbus_register_object (dbus_g_connection_get_connection (conn), "/org/maemo/garge/feedhandler", (GObject*) server);
			g_main_loop_run (loop);
			(server == NULL) ? NULL : (server = (g_object_unref (server), NULL));
		}
		(conn == NULL) ? NULL : (conn = (dbus_g_connection_unref (conn), NULL));
		(bus == NULL) ? NULL : (bus = (g_object_unref (bus), NULL));
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			fprintf (stderr, "OOps: %s\n", e->message);
			(e == NULL) ? NULL : (e = (g_error_free (e), NULL));
		}
	}
	__finally0:
	if (_inner_error_ != NULL) {
		(loop == NULL) ? NULL : (loop = (g_main_loop_unref (loop), NULL));
		g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, _inner_error_->message);
		g_clear_error (&_inner_error_);
		return;
	}
	(loop == NULL) ? NULL : (loop = (g_main_loop_unref (loop), NULL));
}


int main (int argc, char ** argv) {
	g_type_init ();
	_main ();
	return 0;
}


static void _vala_dbus_register_object (DBusConnection* connection, const char* path, void* object) {
	const _DBusObjectVTable * vtable;
	vtable = g_type_get_qdata (G_TYPE_FROM_INSTANCE (object), g_quark_from_static_string ("DBusObjectVTable"));
	if (vtable) {
		vtable->register_object (connection, path, object);
	} else {
		g_warning ("Object does not implement any D-Bus interface");
	}
}


static void _vala_dbus_unregister_object (gpointer connection, GObject* object) {
	char* path;
	path = g_object_steal_data ((GObject*) object, "dbus_object_path");
	dbus_connection_unregister_object_path (connection, path);
	g_free (path);
}




