/* most-recent-events.c generated by valac 0.20.1, the Vala compiler
 * generated from most-recent-events.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include "zeitgeist.h"
#include "zeitgeist-datamodel.h"
#include <gio/gio.h>
#include <stdio.h>

typedef struct _Block1Data Block1Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))

struct _Block1Data {
	int _ref_count_;
	GMainLoop* loop;
	ZeitgeistLog* log;
};



gint _vala_main (void);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void _g_object_unref0_ (gpointer var);
static void __lambda2_ (Block1Data* _data1_, GObject* obj, GAsyncResult* res);
static void ___lambda2__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self);


static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_object_unref0 (_data1_->log);
		_g_main_loop_unref0 (_data1_->loop);
		g_slice_free (Block1Data, _data1_);
	}
}


static void _g_object_unref0_ (gpointer var) {
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}


static void __lambda2_ (Block1Data* _data1_, GObject* obj, GAsyncResult* res) {
	GAsyncResult* _tmp0_;
	ZeitgeistResultSet* _tmp1_ = NULL;
	ZeitgeistResultSet* events;
	FILE* _tmp2_;
	guint _tmp3_ = 0U;
	GError * _inner_error_ = NULL;
	g_return_if_fail (res != NULL);
	_tmp0_ = res;
	_tmp1_ = zeitgeist_log_find_events_finish (_data1_->log, _tmp0_, &_inner_error_);
	events = _tmp1_;
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	_tmp2_ = stdout;
	_tmp3_ = zeitgeist_result_set_size (events);
	fprintf (_tmp2_, "%u most recent subjects:", _tmp3_);
	{
		ZeitgeistResultSet* _tmp4_ = NULL;
		ZeitgeistResultSet* _event_it;
		ZeitgeistEvent* event = NULL;
		_tmp4_ = zeitgeist_result_set_iterator (events);
		_event_it = _tmp4_;
		while (TRUE) {
			ZeitgeistResultSet* _tmp5_;
			ZeitgeistEvent* _tmp6_ = NULL;
			ZeitgeistEvent* _tmp7_;
			FILE* _tmp8_;
			ZeitgeistEvent* _tmp9_;
			GPtrArray* _tmp10_;
			GPtrArray* _tmp11_;
			gconstpointer _tmp12_ = NULL;
			const gchar* _tmp13_;
			const gchar* _tmp14_;
			_tmp5_ = _event_it;
			_tmp6_ = zeitgeist_result_set_next_value (_tmp5_);
			_g_object_unref0 (event);
			event = _tmp6_;
			_tmp7_ = event;
			if (!(_tmp7_ != NULL)) {
				break;
			}
			_tmp8_ = stdout;
			_tmp9_ = event;
			_tmp10_ = zeitgeist_event_get_subjects (_tmp9_);
			_tmp11_ = _tmp10_;
			_tmp12_ = g_ptr_array_index (_tmp11_, (guint) 0);
			_tmp13_ = zeitgeist_subject_get_uri ((ZeitgeistSubject*) _tmp12_);
			_tmp14_ = _tmp13_;
			fprintf (_tmp8_, " - %s\n", _tmp14_);
		}
		_g_object_unref0 (event);
		_g_object_unref0 (_event_it);
	}
	g_main_loop_quit (_data1_->loop);
	_g_object_unref0 (events);
}


static void ___lambda2__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self) {
	__lambda2_ (self, source_object, res);
	block1_data_unref (self);
}


gint _vala_main (void) {
	gint result = 0;
	Block1Data* _data1_;
	GMainLoop* _tmp0_;
	ZeitgeistLog* _tmp1_;
	ZeitgeistTimeRange* _tmp2_;
	ZeitgeistTimeRange* time_range;
	GPtrArray* _tmp3_;
	GPtrArray* templates;
	gint num_events;
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_data1_->loop = _tmp0_;
	_tmp1_ = zeitgeist_log_new ();
	_data1_->log = _tmp1_;
	_tmp2_ = zeitgeist_time_range_new_anytime ();
	time_range = _tmp2_;
	_tmp3_ = g_ptr_array_new_with_free_func (_g_object_unref0_);
	templates = _tmp3_;
	num_events = 20;
	zeitgeist_log_find_events (_data1_->log, time_range, templates, ZEITGEIST_STORAGE_STATE_ANY, (guint32) num_events, ZEITGEIST_RESULT_TYPE_MOST_RECENT_SUBJECTS, NULL, ___lambda2__gasync_ready_callback, block1_data_ref (_data1_));
	g_main_loop_run (_data1_->loop);
	result = 0;
	_g_ptr_array_unref0 (templates);
	_g_object_unref0 (time_range);
	block1_data_unref (_data1_);
	_data1_ = NULL;
	return result;
}


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



