/* tracker-statistics.c generated by valac 0.56.1, the Vala compiler
 * generated from tracker-statistics.vala, do not modify */

/*
 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
 *
 * This library 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 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "libtracker-common/tracker-common.h"
#include "libtracker-data/tracker-data-manager.h"
#include "libtracker-data/tracker-ontologies.h"
#include "libtracker-data/tracker-db-interface.h"
#include "libtracker-data/tracker-class.h"
#include "libtracker-sparql/tracker-sparql.h"

#define TRACKER_STATISTICS_PATH "/org/freedesktop/Tracker1/Statistics"
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TRACKER_TYPE_STATISTICS (tracker_statistics_get_type ())
#define TRACKER_STATISTICS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TRACKER_TYPE_STATISTICS, TrackerStatistics))
#define TRACKER_STATISTICS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TRACKER_TYPE_STATISTICS, TrackerStatisticsClass))
#define TRACKER_IS_STATISTICS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TRACKER_TYPE_STATISTICS))
#define TRACKER_IS_STATISTICS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TRACKER_TYPE_STATISTICS))
#define TRACKER_STATISTICS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TRACKER_TYPE_STATISTICS, TrackerStatisticsClass))

typedef struct _TrackerStatistics TrackerStatistics;
typedef struct _TrackerStatisticsClass TrackerStatisticsClass;
typedef struct _TrackerStatisticsPrivate TrackerStatisticsPrivate;
enum  {
	TRACKER_STATISTICS_0_PROPERTY,
	TRACKER_STATISTICS_NUM_PROPERTIES
};
static GParamSpec* tracker_statistics_properties[TRACKER_STATISTICS_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_variant_builder_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_builder_unref (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))

struct _TrackerStatistics {
	GObject parent_instance;
	TrackerStatisticsPrivate * priv;
};

struct _TrackerStatisticsClass {
	GObjectClass parent_class;
};

static gpointer tracker_statistics_parent_class = NULL;
static gboolean tracker_statistics_initialized;
static gboolean tracker_statistics_initialized = FALSE;

VALA_EXTERN GType tracker_statistics_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (TrackerStatistics, g_object_unref)
VALA_EXTERN guint tracker_statistics_register_object (void* object,
                                          GDBusConnection* connection,
                                          const gchar* path,
                                          GError** error);
VALA_EXTERN GVariant* tracker_statistics_get (TrackerStatistics* self,
                                  const char* sender,
                                  GError** error);
VALA_EXTERN TrackerDataManager* tracker_main_get_data_manager (void);
VALA_EXTERN TrackerStatistics* tracker_statistics_new (void);
VALA_EXTERN TrackerStatistics* tracker_statistics_construct (GType object_type);
static void tracker_statistics_finalize (GObject * obj);
static GType tracker_statistics_get_type_once (void);
static void _dbus_tracker_statistics_get (TrackerStatistics* self,
                                   GVariant* _parameters_,
                                   GDBusMethodInvocation* invocation);
static void tracker_statistics_dbus_interface_method_call (GDBusConnection* connection,
                                                    const gchar* sender,
                                                    const gchar* object_path,
                                                    const gchar* interface_name,
                                                    const gchar* method_name,
                                                    GVariant* parameters,
                                                    GDBusMethodInvocation* invocation,
                                                    gpointer user_data);
static GVariant* tracker_statistics_dbus_interface_get_property (GDBusConnection* connection,
                                                          const gchar* sender,
                                                          const gchar* object_path,
                                                          const gchar* interface_name,
                                                          const gchar* property_name,
                                                          GError** error,
                                                          gpointer user_data);
static gboolean tracker_statistics_dbus_interface_set_property (GDBusConnection* connection,
                                                         const gchar* sender,
                                                         const gchar* object_path,
                                                         const gchar* interface_name,
                                                         const gchar* property_name,
                                                         GVariant* value,
                                                         GError** error,
                                                         gpointer user_data);
static void _tracker_statistics_unregister_object (gpointer user_data);

static const GDBusArgInfo _tracker_statistics_dbus_arg_info_get_result = {-1, "result", "aas", NULL};
static const GDBusArgInfo * const _tracker_statistics_dbus_arg_info_get_in[] = {NULL};
static const GDBusArgInfo * const _tracker_statistics_dbus_arg_info_get_out[] = {&_tracker_statistics_dbus_arg_info_get_result, NULL};
static const GDBusMethodInfo _tracker_statistics_dbus_method_info_get = {-1, "Get", (GDBusArgInfo **) (&_tracker_statistics_dbus_arg_info_get_in), (GDBusArgInfo **) (&_tracker_statistics_dbus_arg_info_get_out), NULL};
static const GDBusMethodInfo * const _tracker_statistics_dbus_method_info[] = {&_tracker_statistics_dbus_method_info_get, NULL};
static const GDBusSignalInfo * const _tracker_statistics_dbus_signal_info[] = {NULL};
static const GDBusPropertyInfo * const _tracker_statistics_dbus_property_info[] = {NULL};
static const GDBusInterfaceInfo _tracker_statistics_dbus_interface_info = {-1, "org.freedesktop.Tracker1.Statistics", (GDBusMethodInfo **) (&_tracker_statistics_dbus_method_info), (GDBusSignalInfo **) (&_tracker_statistics_dbus_signal_info), (GDBusPropertyInfo **) (&_tracker_statistics_dbus_property_info), NULL};
static const GDBusInterfaceVTable _tracker_statistics_dbus_interface_vtable = {tracker_statistics_dbus_interface_method_call, tracker_statistics_dbus_interface_get_property, tracker_statistics_dbus_interface_set_property};

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

GVariant*
tracker_statistics_get (TrackerStatistics* self,
                        const char* sender,
                        GError** error)
{
	TrackerDBusRequest* request = NULL;
	TrackerDBusRequest* _tmp0_;
	TrackerDataManager* data_manager = NULL;
	TrackerDataManager* _tmp1_;
	TrackerDataManager* _tmp2_;
	TrackerOntologies* ontologies = NULL;
	TrackerDataManager* _tmp3_;
	TrackerOntologies* _tmp4_;
	TrackerOntologies* _tmp5_;
	GVariantBuilder* builder = NULL;
	GVariantBuilder* _tmp30_;
	TrackerOntologies* _tmp31_;
	gint _tmp32_ = 0;
	TrackerClass** _tmp33_;
	TrackerDBusRequest* _tmp50_;
	GVariantBuilder* _tmp51_;
	GVariant* _tmp52_;
	GError* _inner_error0_ = NULL;
	GVariant* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (sender != NULL, NULL);
	_tmp0_ = tracker_dbus_request_begin ((const gchar*) sender, "Statistics.Get", NULL);
	request = _tmp0_;
	_tmp1_ = tracker_main_get_data_manager ();
	_tmp2_ = _g_object_ref0 (_tmp1_);
	data_manager = _tmp2_;
	_tmp3_ = data_manager;
	_tmp4_ = tracker_data_manager_get_ontologies (_tmp3_);
	_tmp5_ = _g_object_ref0 (_tmp4_);
	ontologies = _tmp5_;
	if (!tracker_statistics_initialized) {
		TrackerDBInterface* iface = NULL;
		TrackerDataManager* _tmp6_;
		TrackerDBInterface* _tmp7_;
		TrackerDBInterface* _tmp8_;
		TrackerOntologies* _tmp9_;
		gint _tmp10_ = 0;
		TrackerClass** _tmp11_;
		_tmp6_ = data_manager;
		_tmp7_ = tracker_data_manager_get_db_interface (_tmp6_);
		_tmp8_ = _g_object_ref0 (_tmp7_);
		iface = _tmp8_;
		_tmp9_ = ontologies;
		_tmp11_ = tracker_ontologies_get_classes (_tmp9_, &_tmp10_);
		{
			TrackerClass** cl_collection = NULL;
			gint cl_collection_length1 = 0;
			gint _cl_collection_size_ = 0;
			gint cl_it = 0;
			cl_collection = _tmp11_;
			cl_collection_length1 = _tmp10_;
			for (cl_it = 0; cl_it < cl_collection_length1; cl_it = cl_it + 1) {
				TrackerClass* _tmp12_;
				TrackerClass* cl = NULL;
				_tmp12_ = _g_object_ref0 (cl_collection[cl_it]);
				cl = _tmp12_;
				{
					TrackerClass* _tmp13_;
					const gchar* _tmp14_;
					const gchar* _tmp15_;
					_tmp13_ = cl;
					_tmp14_ = tracker_class_get_name (_tmp13_);
					_tmp15_ = _tmp14_;
					if (!g_str_has_prefix (_tmp15_, "xsd:")) {
						TrackerDBStatement* stmt = NULL;
						TrackerDBInterface* _tmp16_;
						TrackerClass* _tmp17_;
						const gchar* _tmp18_;
						const gchar* _tmp19_;
						TrackerDBStatement* _tmp20_;
						TrackerDBCursor* stat_cursor = NULL;
						TrackerDBStatement* _tmp21_;
						TrackerDBCursor* _tmp22_;
						gboolean _tmp23_ = FALSE;
						TrackerDBCursor* _tmp24_;
						_tmp16_ = iface;
						_tmp17_ = cl;
						_tmp18_ = tracker_class_get_name (_tmp17_);
						_tmp19_ = _tmp18_;
						_tmp20_ = tracker_db_interface_create_statement (_tmp16_, TRACKER_DB_STATEMENT_CACHE_TYPE_NONE, &_inner_error0_, "SELECT COUNT(1) FROM \"%s\"", _tmp19_);
						stmt = _tmp20_;
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							g_propagate_error (error, _inner_error0_);
							_g_object_unref0 (cl);
							_g_object_unref0 (iface);
							_g_object_unref0 (ontologies);
							_g_object_unref0 (data_manager);
							return NULL;
						}
						_tmp21_ = stmt;
						_tmp22_ = tracker_db_statement_start_cursor (_tmp21_, &_inner_error0_);
						stat_cursor = _tmp22_;
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							g_propagate_error (error, _inner_error0_);
							_g_object_unref0 (stmt);
							_g_object_unref0 (cl);
							_g_object_unref0 (iface);
							_g_object_unref0 (ontologies);
							_g_object_unref0 (data_manager);
							return NULL;
						}
						_tmp24_ = stat_cursor;
						_tmp23_ = tracker_sparql_cursor_next ((TrackerSparqlCursor*) _tmp24_, NULL, &_inner_error0_);
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							g_propagate_error (error, _inner_error0_);
							_g_object_unref0 (stat_cursor);
							_g_object_unref0 (stmt);
							_g_object_unref0 (cl);
							_g_object_unref0 (iface);
							_g_object_unref0 (ontologies);
							_g_object_unref0 (data_manager);
							return NULL;
						}
						if (_tmp23_) {
							TrackerClass* _tmp25_;
							TrackerDBCursor* _tmp26_;
							_tmp25_ = cl;
							_tmp26_ = stat_cursor;
							tracker_class_set_count (_tmp25_, (gint) tracker_sparql_cursor_get_integer ((TrackerSparqlCursor*) _tmp26_, 0));
						} else {
							TrackerClass* _tmp27_;
							const gchar* _tmp28_;
							const gchar* _tmp29_;
							_tmp27_ = cl;
							_tmp28_ = tracker_class_get_name (_tmp27_);
							_tmp29_ = _tmp28_;
							g_warning ("Unable to query instance count for class %s", _tmp29_);
						}
						_g_object_unref0 (stat_cursor);
						_g_object_unref0 (stmt);
					}
					_g_object_unref0 (cl);
				}
			}
		}
		tracker_statistics_initialized = TRUE;
		_g_object_unref0 (iface);
	}
	_tmp30_ = g_variant_builder_new ((const GVariantType*) "aas");
	builder = _tmp30_;
	_tmp31_ = ontologies;
	_tmp33_ = tracker_ontologies_get_classes (_tmp31_, &_tmp32_);
	{
		TrackerClass** cl_collection = NULL;
		gint cl_collection_length1 = 0;
		gint _cl_collection_size_ = 0;
		gint cl_it = 0;
		cl_collection = _tmp33_;
		cl_collection_length1 = _tmp32_;
		for (cl_it = 0; cl_it < cl_collection_length1; cl_it = cl_it + 1) {
			TrackerClass* _tmp34_;
			TrackerClass* cl = NULL;
			_tmp34_ = _g_object_ref0 (cl_collection[cl_it]);
			cl = _tmp34_;
			{
				TrackerClass* _tmp35_;
				gint _tmp36_;
				gint _tmp37_;
				GVariantBuilder* _tmp38_;
				GVariantBuilder* _tmp39_;
				TrackerClass* _tmp40_;
				const gchar* _tmp41_;
				const gchar* _tmp42_;
				GVariantBuilder* _tmp43_;
				TrackerClass* _tmp44_;
				gint _tmp45_;
				gint _tmp46_;
				gchar* _tmp47_;
				gchar* _tmp48_;
				GVariantBuilder* _tmp49_;
				_tmp35_ = cl;
				_tmp36_ = tracker_class_get_count (_tmp35_);
				_tmp37_ = _tmp36_;
				if (_tmp37_ == 0) {
					_g_object_unref0 (cl);
					continue;
				}
				_tmp38_ = builder;
				g_variant_builder_open (_tmp38_, (const GVariantType*) "as");
				_tmp39_ = builder;
				_tmp40_ = cl;
				_tmp41_ = tracker_class_get_name (_tmp40_);
				_tmp42_ = _tmp41_;
				g_variant_builder_add (_tmp39_, "s", _tmp42_, NULL);
				_tmp43_ = builder;
				_tmp44_ = cl;
				_tmp45_ = tracker_class_get_count (_tmp44_);
				_tmp46_ = _tmp45_;
				_tmp47_ = g_strdup_printf ("%i", _tmp46_);
				_tmp48_ = _tmp47_;
				g_variant_builder_add (_tmp43_, "s", _tmp48_, NULL);
				_g_free0 (_tmp48_);
				_tmp49_ = builder;
				g_variant_builder_close (_tmp49_);
				_g_object_unref0 (cl);
			}
		}
	}
	_tmp50_ = request;
	tracker_dbus_request_end (_tmp50_, NULL);
	_tmp51_ = builder;
	_tmp52_ = g_variant_builder_end (_tmp51_);
	g_variant_ref_sink (_tmp52_);
	result = _tmp52_;
	_g_variant_builder_unref0 (builder);
	_g_object_unref0 (ontologies);
	_g_object_unref0 (data_manager);
	return result;
}

TrackerStatistics*
tracker_statistics_construct (GType object_type)
{
	TrackerStatistics * self = NULL;
	self = (TrackerStatistics*) g_object_new (object_type, NULL);
	return self;
}

TrackerStatistics*
tracker_statistics_new (void)
{
	return tracker_statistics_construct (TRACKER_TYPE_STATISTICS);
}

static void
tracker_statistics_class_init (TrackerStatisticsClass * klass,
                               gpointer klass_data)
{
	tracker_statistics_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = tracker_statistics_finalize;
}

static void
tracker_statistics_instance_init (TrackerStatistics * self,
                                  gpointer klass)
{
}

static void
tracker_statistics_finalize (GObject * obj)
{
	TrackerStatistics * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TRACKER_TYPE_STATISTICS, TrackerStatistics);
	G_OBJECT_CLASS (tracker_statistics_parent_class)->finalize (obj);
}

static GType
tracker_statistics_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (TrackerStatisticsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) tracker_statistics_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (TrackerStatistics), 0, (GInstanceInitFunc) tracker_statistics_instance_init, NULL };
	GType tracker_statistics_type_id;
	tracker_statistics_type_id = g_type_register_static (G_TYPE_OBJECT, "TrackerStatistics", &g_define_type_info, 0);
	g_type_set_qdata (tracker_statistics_type_id, g_quark_from_static_string ("vala-dbus-register-object"), (void*) tracker_statistics_register_object);
	return tracker_statistics_type_id;
}

GType
tracker_statistics_get_type (void)
{
	static volatile gsize tracker_statistics_type_id__once = 0;
	if (g_once_init_enter (&tracker_statistics_type_id__once)) {
		GType tracker_statistics_type_id;
		tracker_statistics_type_id = tracker_statistics_get_type_once ();
		g_once_init_leave (&tracker_statistics_type_id__once, tracker_statistics_type_id);
	}
	return tracker_statistics_type_id__once;
}

static void
_dbus_tracker_statistics_get (TrackerStatistics* self,
                              GVariant* _parameters_,
                              GDBusMethodInvocation* invocation)
{
	GError* error = NULL;
	GVariantIter _arguments_iter;
	GDBusMessage* _reply_message = NULL;
	GVariant* _reply;
	GVariantBuilder _reply_builder;
	GVariant* result;
	g_variant_iter_init (&_arguments_iter, _parameters_);
	result = tracker_statistics_get (self, g_dbus_method_invocation_get_sender (invocation), &error);
	if (error) {
		g_dbus_method_invocation_take_error (invocation, error);
		return;
	}
	_reply_message = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
	g_variant_builder_init (&_reply_builder, G_VARIANT_TYPE_TUPLE);
	g_variant_builder_add_value (&_reply_builder, result);
	_g_variant_unref0 (result);
	_reply = g_variant_builder_end (&_reply_builder);
	g_dbus_message_set_body (_reply_message, _reply);
	g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), _reply_message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
	g_object_unref (invocation);
	g_object_unref (_reply_message);
}

static void
tracker_statistics_dbus_interface_method_call (GDBusConnection* connection,
                                               const gchar* sender,
                                               const gchar* object_path,
                                               const gchar* interface_name,
                                               const gchar* method_name,
                                               GVariant* parameters,
                                               GDBusMethodInvocation* invocation,
                                               gpointer user_data)
{
	gpointer* data;
	gpointer object;
	data = user_data;
	object = data[0];
	if (strcmp (method_name, "Get") == 0) {
		_dbus_tracker_statistics_get (object, parameters, invocation);
	} else {
		g_object_unref (invocation);
	}
}

static GVariant*
tracker_statistics_dbus_interface_get_property (GDBusConnection* connection,
                                                const gchar* sender,
                                                const gchar* object_path,
                                                const gchar* interface_name,
                                                const gchar* property_name,
                                                GError** error,
                                                gpointer user_data)
{
	gpointer* data;
	gpointer object;
	data = user_data;
	object = data[0];
	return NULL;
}

static gboolean
tracker_statistics_dbus_interface_set_property (GDBusConnection* connection,
                                                const gchar* sender,
                                                const gchar* object_path,
                                                const gchar* interface_name,
                                                const gchar* property_name,
                                                GVariant* value,
                                                GError** error,
                                                gpointer user_data)
{
	gpointer* data;
	gpointer object;
	data = user_data;
	object = data[0];
	return FALSE;
}

guint
tracker_statistics_register_object (gpointer object,
                                    GDBusConnection* connection,
                                    const gchar* path,
                                    GError** error)
{
	guint result;
	gpointer *data;
	data = g_new (gpointer, 3);
	data[0] = g_object_ref (object);
	data[1] = g_object_ref (connection);
	data[2] = g_strdup (path);
	result = g_dbus_connection_register_object (connection, path, (GDBusInterfaceInfo *) (&_tracker_statistics_dbus_interface_info), &_tracker_statistics_dbus_interface_vtable, data, _tracker_statistics_unregister_object, error);
	if (!result) {
		return 0;
	}
	return result;
}

static void
_tracker_statistics_unregister_object (gpointer user_data)
{
	gpointer* data;
	data = user_data;
	g_object_unref (data[0]);
	g_object_unref (data[1]);
	g_free (data[2]);
	g_free (data);
}

