/* notify_gernerated.c generated by valac, the Vala compiler
 * generated from notify_gernerated.vala, do not modify */

/*
    Copyright 2010 by BoscoWitch

    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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <gtk/gtk.h>
#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <glib/gstdio.h>
#include <gdk/gdk.h>
#include <float.h>
#include <math.h>


#define TYPE_WADOKU_NOTIFY (wadoku_notify_get_type ())
#define WADOKU_NOTIFY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_WADOKU_NOTIFY, WadokuNotify))
#define WADOKU_NOTIFY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_WADOKU_NOTIFY, WadokuNotifyClass))
#define IS_WADOKU_NOTIFY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_WADOKU_NOTIFY))
#define IS_WADOKU_NOTIFY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_WADOKU_NOTIFY))
#define WADOKU_NOTIFY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_WADOKU_NOTIFY, WadokuNotifyClass))

typedef struct _WadokuNotify WadokuNotify;
typedef struct _WadokuNotifyClass WadokuNotifyClass;
typedef struct _WadokuNotifyPrivate WadokuNotifyPrivate;

#define WADOKU_NOTIFY_TYPE_OUTPUT_TYPE (wadoku_notify_output_type_get_type ())
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _sqlite3_close0(var) ((var == NULL) ? NULL : (var = (sqlite3_close (var), NULL)))

struct _WadokuNotify {
	GObject parent_instance;
	WadokuNotifyPrivate * priv;
};

struct _WadokuNotifyClass {
	GObjectClass parent_class;
};

typedef enum  {
	WADOKU_NOTIFY_OUTPUT_TYPE_stdout,
	WADOKU_NOTIFY_OUTPUT_TYPE_Gtk,
	WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify
} WadokuNotifyOutputType;


extern char* wadoku_notify_Path;
char* wadoku_notify_Path = NULL;
extern gint wadoku_notify_rc;
gint wadoku_notify_rc = 0;
extern sqlite3* wadoku_notify_db;
sqlite3* wadoku_notify_db = NULL;
extern sqlite3_callback wadoku_notify_CallbackFunc;
extern gpointer wadoku_notify_CallbackFunc_target;
extern GDestroyNotify wadoku_notify_CallbackFunc_target_destroy_notify;
sqlite3_callback wadoku_notify_CallbackFunc = NULL;
gpointer wadoku_notify_CallbackFunc_target = NULL;
GDestroyNotify wadoku_notify_CallbackFunc_target_destroy_notify = NULL;
extern gint wadoku_notify_output;
gint wadoku_notify_output = 0;
extern gint wadoku_notify_reading;
gint wadoku_notify_reading = 0;
extern gboolean wadoku_notify_japanese_only;
gboolean wadoku_notify_japanese_only = FALSE;
extern gint wadoku_notify_time;
gint wadoku_notify_time = 0;
extern gint wadoku_notify_res_nr;
gint wadoku_notify_res_nr = 0;
extern char* wadoku_notify_result_nr;
char* wadoku_notify_result_nr = NULL;
extern GtkClipboard* wadoku_notify_clip;
GtkClipboard* wadoku_notify_clip = NULL;
extern char* wadoku_notify_text;
char* wadoku_notify_text = NULL;
extern char* wadoku_notify_old_text;
char* wadoku_notify_old_text = NULL;
extern NotifyNotification* wadoku_notify_not;
NotifyNotification* wadoku_notify_not = NULL;
extern GtkWindow* wadoku_notify_window;
GtkWindow* wadoku_notify_window = NULL;
extern GtkLabel* wadoku_notify_label;
GtkLabel* wadoku_notify_label = NULL;
extern guint wadoku_notify_Timer;
guint wadoku_notify_Timer = 0U;
static gpointer wadoku_notify_parent_class = NULL;

GType wadoku_notify_get_type (void);
enum  {
	WADOKU_NOTIFY_DUMMY_PROPERTY
};
GType wadoku_notify_output_type_get_type (void);
#define WADOKU_NOTIFY_version "0.2"
void wadoku_notify_clipboard_changed (void);
gint wadoku_notify_callback_notify (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1);
gboolean wadoku_notify_HideTimer (void);
static gboolean _wadoku_notify_HideTimer_gsource_func (gpointer self);
gint wadoku_notify_callback_ownerdrawn (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1);
void wadoku_notify_EnterWindow (void);
void wadoku_notify_Next (void);
gint wadoku_notify_callback_stdout (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1);
static gint _wadoku_notify_callback_notify_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names);
static gint _wadoku_notify_callback_stdout_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names);
static gint _wadoku_notify_callback_ownerdrawn_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names);
static void _gtk_main_quit_gtk_object_destroy (GtkObject* _sender, gpointer self);
static void _wadoku_notify_EnterWindow_gcallback (void);
static void _wadoku_notify_clipboard_changed_gcallback (void);
gint wadoku_notify_main (char** args, int args_length1);
WadokuNotify* wadoku_notify_new (void);
WadokuNotify* wadoku_notify_construct (GType object_type);
static void wadoku_notify_finalize (GObject* obj);
static int _vala_strcmp0 (const char * str1, const char * str2);



GType wadoku_notify_output_type_get_type (void) {
	static volatile gsize wadoku_notify_output_type_type_id__volatile = 0;
	if (g_once_init_enter (&wadoku_notify_output_type_type_id__volatile)) {
		static const GEnumValue values[] = {{WADOKU_NOTIFY_OUTPUT_TYPE_stdout, "WADOKU_NOTIFY_OUTPUT_TYPE_stdout", "stdout"}, {WADOKU_NOTIFY_OUTPUT_TYPE_Gtk, "WADOKU_NOTIFY_OUTPUT_TYPE_Gtk", "gtk"}, {WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify, "WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify", "libnotify"}, {0, NULL, NULL}};
		GType wadoku_notify_output_type_type_id;
		wadoku_notify_output_type_type_id = g_enum_register_static ("WadokuNotifyOutputType", values);
		g_once_init_leave (&wadoku_notify_output_type_type_id__volatile, wadoku_notify_output_type_type_id);
	}
	return wadoku_notify_output_type_type_id__volatile;
}


static glong string_get_length (const char* self) {
	glong result;
	g_return_val_if_fail (self != NULL, 0L);
	result = g_utf8_strlen (self, -1);
	return result;
}


static char* string_replace (const char* self, const char* old, const char* replacement) {
	char* result = NULL;
	GError * _inner_error_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	_inner_error_ = NULL;
	{
		char* _tmp0_;
		GRegex* _tmp1_;
		GRegex* regex;
		char* _tmp2_;
		regex = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (old, -1), 0, 0, &_inner_error_), _g_free0 (_tmp0_), _tmp1_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp2_ = g_regex_replace_literal (regex, self, (gssize) (-1), 0, replacement, 0, &_inner_error_);
		if (_inner_error_ != NULL) {
			_g_regex_unref0 (regex);
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			_g_regex_unref0 (regex);
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		result = _tmp2_;
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_assert_not_reached ();
			_g_error_free0 (e);
		}
	}
	__finally0:
	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 NULL;
	}
}


void wadoku_notify_clipboard_changed (void) {
	char* _tmp0_;
	gboolean _tmp1_ = FALSE;
	gboolean _tmp2_ = FALSE;
	wadoku_notify_text = (_tmp0_ = g_strdup (gtk_clipboard_wait_for_text (wadoku_notify_clip)), _g_free0 (wadoku_notify_text), _tmp0_);
	if (wadoku_notify_text != NULL) {
		_tmp2_ = _vala_strcmp0 (wadoku_notify_text, wadoku_notify_old_text) != 0;
	} else {
		_tmp2_ = FALSE;
	}
	if (_tmp2_) {
		_tmp1_ = string_get_length (wadoku_notify_text) >= 1;
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		char* _tmp3_;
		gboolean _tmp4_ = FALSE;
		gboolean _tmp5_ = FALSE;
		wadoku_notify_text = (_tmp3_ = string_replace (wadoku_notify_text, "\"", "`"), _g_free0 (wadoku_notify_text), _tmp3_);
		if (g_utf8_get_char (wadoku_notify_text) >= 0x4E00) {
			_tmp5_ = g_utf8_get_char (wadoku_notify_text) <= 0x9FAF;
		} else {
			_tmp5_ = FALSE;
		}
		if (_tmp5_) {
			_tmp4_ = TRUE;
		} else {
			gboolean _tmp6_ = FALSE;
			if (g_utf8_get_char (wadoku_notify_text) >= 0x30A0) {
				_tmp6_ = g_utf8_get_char (wadoku_notify_text) <= 0x30FF;
			} else {
				_tmp6_ = FALSE;
			}
			_tmp4_ = _tmp6_;
		}
		if (_tmp4_) {
			char* _tmp17_;
			char* _tmp16_;
			char* _tmp15_;
			char* _tmp14_;
			char* _tmp13_;
			char* _tmp12_;
			char* _tmp11_;
			char* _tmp10_;
			char* _tmp9_;
			char* _tmp8_;
			char* _tmp7_;
			wadoku_notify_rc = sqlite3_exec (wadoku_notify_db, _tmp17_ = g_strconcat (_tmp16_ = g_strconcat (_tmp14_ = g_strconcat (_tmp13_ = g_strconcat (_tmp12_ = g_strconcat (_tmp11_ = g_strconcat (_tmp10_ = g_strconcat (_tmp9_ = g_strconcat (_tmp8_ = g_strconcat (_tmp7_ = g_strconcat ("select japanese,reading,german,romanjireading from entries where\n" \
"    \t\t\t\t\t\tjapanese like \"", wadoku_notify_text, NULL), "\" or\n    \t\t\t\t\t\tjapanese like \"", NULL), wadoku_notify_text, NULL), " %\" or\n    \t\t\t\t\t\tjapanese like \"", NULL), wadoku_notify_text, NULL), "%\" or\n    \t\t\t\t\t\tjapanese like \"%", NULL), wadoku_notify_text, NULL), "%\" \n    \t\t\t\t\t\torder by LENGTH(japanese_stripped) asc limit ", NULL), _tmp15_ = g_strdup_printf ("%i", wadoku_notify_res_nr), NULL), ",1;", NULL), wadoku_notify_CallbackFunc, wadoku_notify_CallbackFunc_target, NULL);
			_g_free0 (_tmp17_);
			_g_free0 (_tmp16_);
			_g_free0 (_tmp15_);
			_g_free0 (_tmp14_);
			_g_free0 (_tmp13_);
			_g_free0 (_tmp12_);
			_g_free0 (_tmp11_);
			_g_free0 (_tmp10_);
			_g_free0 (_tmp9_);
			_g_free0 (_tmp8_);
			_g_free0 (_tmp7_);
		} else {
			gboolean _tmp18_ = FALSE;
			if (g_utf8_get_char (wadoku_notify_text) >= 0x3040) {
				_tmp18_ = g_utf8_get_char (wadoku_notify_text) <= 0x309F;
			} else {
				_tmp18_ = FALSE;
			}
			if (_tmp18_) {
				char* _tmp27_;
				char* _tmp26_;
				char* _tmp25_;
				char* _tmp24_;
				char* _tmp23_;
				char* _tmp22_;
				char* _tmp21_;
				char* _tmp20_;
				char* _tmp19_;
				wadoku_notify_rc = sqlite3_exec (wadoku_notify_db, _tmp27_ = g_strconcat (_tmp26_ = g_strconcat (_tmp24_ = g_strconcat (_tmp23_ = g_strconcat (_tmp22_ = g_strconcat (_tmp21_ = g_strconcat (_tmp20_ = g_strconcat (_tmp19_ = g_strconcat ("select japanese,reading,german,romanjireading from entries where\n" \
"    \t\t\t\t\t\treading like \"", wadoku_notify_text, NULL), "\" or  \n    \t\t\t\t\t\treading like \"", NULL), wadoku_notify_text, NULL), "%\" or \n    \t\t\t\t\t\treading like \"%", NULL), wadoku_notify_text, NULL), "%\" order by LENGTH(reading_stripped) asc limit ", NULL), _tmp25_ = g_strdup_printf ("%i", wadoku_notify_res_nr), NULL), ",1;", NULL), wadoku_notify_CallbackFunc, wadoku_notify_CallbackFunc_target, NULL);
				_g_free0 (_tmp27_);
				_g_free0 (_tmp26_);
				_g_free0 (_tmp25_);
				_g_free0 (_tmp24_);
				_g_free0 (_tmp23_);
				_g_free0 (_tmp22_);
				_g_free0 (_tmp21_);
				_g_free0 (_tmp20_);
				_g_free0 (_tmp19_);
			} else {
				if (!wadoku_notify_japanese_only) {
					char* _tmp44_;
					char* _tmp43_;
					char* _tmp42_;
					char* _tmp41_;
					char* _tmp40_;
					char* _tmp39_;
					char* _tmp38_;
					char* _tmp37_;
					char* _tmp36_;
					char* _tmp35_;
					char* _tmp34_;
					char* _tmp33_;
					char* _tmp32_;
					char* _tmp31_;
					char* _tmp30_;
					char* _tmp29_;
					char* _tmp28_;
					wadoku_notify_rc = sqlite3_exec (wadoku_notify_db, _tmp44_ = g_strconcat (_tmp43_ = g_strconcat (_tmp41_ = g_strconcat (_tmp40_ = g_strconcat (_tmp39_ = g_strconcat (_tmp38_ = g_strconcat (_tmp37_ = g_strconcat (_tmp36_ = g_strconcat (_tmp35_ = g_strconcat (_tmp34_ = g_strconcat (_tmp33_ = g_strconcat (_tmp32_ = g_strconcat (_tmp31_ = g_strconcat (_tmp30_ = g_strconcat (_tmp29_ = g_strconcat (_tmp28_ = g_strconcat ("select japanese,reading,german,romanjireading from entries where\n" \
"    \t\t\t\t\t\tjapanese like \"", wadoku_notify_text, NULL), "\" or\n    \t\t\t\t\t\treading like \"", NULL), wadoku_notify_text, NULL), "\" or\n    \t\t\t\t\t\tjapanese like \"", NULL), wadoku_notify_text, NULL), "%\" or\n    \t\t\t\t\t\tjapanese like \"%", NULL), wadoku_notify_text, NULL), "%\" or  \n    \t\t\t\t\t\treading like \"", NULL), wadoku_notify_text, NULL), "%\" or \n    \t\t\t\t\t\treading like \"%", NULL), wadoku_notify_text, NULL), "%\" or \n    \t\t\t\t\t\tgerman like \"%", NULL), wadoku_notify_text, NULL), "%\" order by LENGTH(reading_stripped) asc limit ", NULL), _tmp42_ = g_strdup_printf ("%i", wadoku_notify_res_nr), NULL), ",1;", NULL), wadoku_notify_CallbackFunc, wadoku_notify_CallbackFunc_target, NULL);
					_g_free0 (_tmp44_);
					_g_free0 (_tmp43_);
					_g_free0 (_tmp42_);
					_g_free0 (_tmp41_);
					_g_free0 (_tmp40_);
					_g_free0 (_tmp39_);
					_g_free0 (_tmp38_);
					_g_free0 (_tmp37_);
					_g_free0 (_tmp36_);
					_g_free0 (_tmp35_);
					_g_free0 (_tmp34_);
					_g_free0 (_tmp33_);
					_g_free0 (_tmp32_);
					_g_free0 (_tmp31_);
					_g_free0 (_tmp30_);
					_g_free0 (_tmp29_);
					_g_free0 (_tmp28_);
				}
			}
		}
		if (wadoku_notify_rc != SQLITE_OK) {
			fprintf (stderr, "SQL error: %d, %s\n", wadoku_notify_rc, sqlite3_errmsg (wadoku_notify_db));
		}
	}
}


static char* string_substring (const char* self, glong offset, glong len) {
	char* result = NULL;
	glong string_length;
	const char* start;
	g_return_val_if_fail (self != NULL, NULL);
	string_length = g_utf8_strlen (self, -1);
	if (offset < 0) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= 0, NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < 0) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	start = g_utf8_offset_to_pointer (self, offset);
	result = g_strndup (start, ((gchar*) g_utf8_offset_to_pointer (start, len)) - ((gchar*) start));
	return result;
}


gint wadoku_notify_callback_notify (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1) {
	gint result = 0;
	GError * _inner_error_;
	char* _tmp0_;
	char* temp;
	gboolean _tmp1_ = FALSE;
	_inner_error_ = NULL;
	if (_vala_strcmp0 (wadoku_notify_text, wadoku_notify_old_text) == 0) {
		result = 0;
		return result;
	}
	wadoku_notify_old_text = (_tmp0_ = g_strdup (wadoku_notify_text), _g_free0 (wadoku_notify_old_text), _tmp0_);
	temp = g_strdup_printf ("%s\n%s", values[wadoku_notify_reading], values[2]);
	if (wadoku_notify_output == WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify) {
		_tmp1_ = string_get_length (temp) > 300;
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		char* _tmp3_;
		char* _tmp2_;
		temp = (_tmp3_ = g_strconcat (_tmp2_ = string_substring (temp, (glong) 0, (glong) 300), "...", NULL), _g_free0 (temp), _tmp3_);
		_g_free0 (_tmp2_);
	}
	if (wadoku_notify_not != NULL) {
		char* _tmp4_;
		notify_notification_update (wadoku_notify_not, values[0], temp, _tmp4_ = g_strdup_printf ("%s/wadoku.png", wadoku_notify_Path));
		_g_free0 (_tmp4_);
	} else {
		NotifyNotification* _tmp6_;
		char* _tmp5_;
		wadoku_notify_not = (_tmp6_ = notify_notification_new (values[0], temp, _tmp5_ = g_strdup_printf ("%s/wadoku.png", wadoku_notify_Path), NULL), _g_object_unref0 (wadoku_notify_not), _tmp6_);
		_g_free0 (_tmp5_);
	}
	notify_notification_set_timeout (wadoku_notify_not, wadoku_notify_time);
	notify_notification_set_urgency (wadoku_notify_not, NOTIFY_URGENCY_CRITICAL);
	notify_notification_show (wadoku_notify_not, &_inner_error_);
	if (_inner_error_ != NULL) {
		_g_free0 (temp);
		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 0;
	}
	result = 0;
	_g_free0 (temp);
	return result;
}


static gboolean _wadoku_notify_HideTimer_gsource_func (gpointer self) {
	return wadoku_notify_HideTimer ();
}


gint wadoku_notify_callback_ownerdrawn (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1) {
	gint result = 0;
	char* _tmp0_;
	char* temp;
	char* _tmp1_;
	char* _tmp2_;
	char* _tmp3_;
	if (_vala_strcmp0 (wadoku_notify_text, wadoku_notify_old_text) == 0) {
		result = 0;
		return result;
	}
	wadoku_notify_old_text = (_tmp0_ = g_strdup (wadoku_notify_text), _g_free0 (wadoku_notify_old_text), _tmp0_);
	temp = string_replace (values[2], "<", "&#60;");
	temp = (_tmp1_ = string_replace (temp, ">", "&#62;"), _g_free0 (temp), _tmp1_);
	temp = (_tmp2_ = string_replace (temp, "&", "&amp"), _g_free0 (temp), _tmp2_);
	gtk_label_set_markup (wadoku_notify_label, _tmp3_ = g_strdup_printf ("<span font=\"Meiryo 14\">%s</span>\n%s\n%s", values[0], values[wadoku_notify_reading], temp));
	_g_free0 (_tmp3_);
	gtk_window_resize (wadoku_notify_window, 1, 1);
	gtk_widget_show_all ((GtkWidget*) wadoku_notify_window);
	g_source_remove (wadoku_notify_Timer);
	wadoku_notify_Timer = g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) wadoku_notify_time, _wadoku_notify_HideTimer_gsource_func, NULL, NULL);
	result = 0;
	_g_free0 (temp);
	return result;
}


gboolean wadoku_notify_HideTimer (void) {
	gboolean result = FALSE;
	gtk_widget_hide_all ((GtkWidget*) wadoku_notify_window);
	result = FALSE;
	return result;
}


void wadoku_notify_EnterWindow (void) {
	gtk_widget_hide_all ((GtkWidget*) wadoku_notify_window);
	g_source_remove (wadoku_notify_Timer);
}


void wadoku_notify_Next (void) {
	wadoku_notify_res_nr++;
}


gint wadoku_notify_callback_stdout (gint n_columns, char** values, int values_length1, char** column_names, int column_names_length1) {
	gint result = 0;
	char* _tmp0_;
	if (_vala_strcmp0 (wadoku_notify_text, wadoku_notify_old_text) == 0) {
		result = 0;
		return result;
	}
	wadoku_notify_old_text = (_tmp0_ = g_strdup (wadoku_notify_text), _g_free0 (wadoku_notify_old_text), _tmp0_);
	fprintf (stdout, "\n %s\n %s\n%s\n", values[0], values[wadoku_notify_reading], values[2]);
	fflush (stdout);
	result = 0;
	return result;
}


static char* string_slice (const char* self, glong start, glong end) {
	char* result = NULL;
	glong string_length;
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	const char* start_string;
	g_return_val_if_fail (self != NULL, NULL);
	string_length = g_utf8_strlen (self, -1);
	if (start < 0) {
		start = string_length + start;
	}
	if (end < 0) {
		end = string_length + end;
	}
	if (start >= 0) {
		_tmp0_ = start <= string_length;
	} else {
		_tmp0_ = FALSE;
	}
	g_return_val_if_fail (_tmp0_, NULL);
	if (end >= 0) {
		_tmp1_ = end <= string_length;
	} else {
		_tmp1_ = FALSE;
	}
	g_return_val_if_fail (_tmp1_, NULL);
	g_return_val_if_fail (start <= end, NULL);
	start_string = g_utf8_offset_to_pointer (self, start);
	result = g_strndup (start_string, ((gchar*) g_utf8_offset_to_pointer (start_string, end - start)) - ((gchar*) start_string));
	return result;
}


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


static gint _wadoku_notify_callback_notify_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names) {
	return wadoku_notify_callback_notify (n_columns, values, -1, column_names, -1);
}


static gint _wadoku_notify_callback_stdout_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names) {
	return wadoku_notify_callback_stdout (n_columns, values, -1, column_names, -1);
}


static gint _wadoku_notify_callback_ownerdrawn_sqlite3_callback (gpointer self, gint n_columns, char** values, char** column_names) {
	return wadoku_notify_callback_ownerdrawn (n_columns, values, -1, column_names, -1);
}


static void _gtk_main_quit_gtk_object_destroy (GtkObject* _sender, gpointer self) {
	gtk_main_quit ();
}


static void _wadoku_notify_EnterWindow_gcallback (void) {
	wadoku_notify_EnterWindow ();
}


static void _wadoku_notify_clipboard_changed_gcallback (void) {
	wadoku_notify_clipboard_changed ();
}


gint wadoku_notify_main (char** args, int args_length1) {
	gint result = 0;
	gint rc = 0;
	char* exelink;
	gchar exepath[1024] = {0};
	char* _tmp2_;
	char* _tmp3_;
	char* _tmp4_;
	char* _tmp5_;
	gint _tmp6_;
	gint fd;
	char* _tmp8_;
	gboolean _tmp9_;
	sqlite3* _tmp13_;
	gint _tmp12_;
	sqlite3* _tmp11_ = NULL;
	char* _tmp10_;
	GtkClipboard* _tmp14_;
	wadoku_notify_res_nr = 0;
	wadoku_notify_output = (gint) WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify;
	wadoku_notify_reading = 1;
	wadoku_notify_japanese_only = FALSE;
	wadoku_notify_time = 5000;
	{
		gint i;
		i = 1;
		{
			gboolean _tmp0_;
			_tmp0_ = TRUE;
			while (TRUE) {
				gboolean _tmp1_ = FALSE;
				if (!_tmp0_) {
					i++;
				}
				_tmp0_ = FALSE;
				if (!(i < args_length1)) {
					break;
				}
				if (_vala_strcmp0 (args[i], "-v") == 0) {
					_tmp1_ = TRUE;
				} else {
					_tmp1_ = _vala_strcmp0 (args[i], "--version") == 0;
				}
				if (_tmp1_) {
					fprintf (stdout, "%s\n", WADOKU_NOTIFY_version);
					result = 0;
					return result;
				} else {
					if (_vala_strcmp0 (args[i], "-romanji") == 0) {
						wadoku_notify_reading = 3;
						continue;
					} else {
						if (_vala_strcmp0 (args[i], "-gtk") == 0) {
							wadoku_notify_output = (gint) WADOKU_NOTIFY_OUTPUT_TYPE_Gtk;
							continue;
						} else {
							if (_vala_strcmp0 (args[i], "-stdout") == 0) {
								wadoku_notify_output = (gint) WADOKU_NOTIFY_OUTPUT_TYPE_stdout;
								continue;
							} else {
								if (_vala_strcmp0 (args[i], "-jap-only") == 0) {
									wadoku_notify_japanese_only = TRUE;
									continue;
								} else {
									if (_vala_strcmp0 (args[i], "-time") == 0) {
										i++;
										if (args[i] == NULL) {
											fprintf (stderr, "Please give a timespan in milli seconds to show the notify box\n");
											result = -1;
											return result;
										}
										wadoku_notify_time = atoi (args[i]);
										continue;
									} else {
										fprintf (stderr, "Usage:\n" \
"\t-time TIMEINMS sets to time how long to show the notify bos in milli" \
" seconds default is 5000\n" \
"\t-jap-only only search if japanese characters are selected\n" \
"\t-romanji for reading on roman letters instead of japanese hiragana.\n" \
"\t-gtk for gtk dialog output or\n" \
"\t-stdout for shell standart out stream.\n" \
"\tdefault is LibNotify System Notification output\n");
										result = -1;
										return result;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	exelink = NULL;
	exelink = (_tmp2_ = g_strdup_printf ("/proc/%d/exe", (gint) getpid ()), _g_free0 (exelink), _tmp2_);
	readlink (exelink, exepath, 1024);
	wadoku_notify_Path = (_tmp3_ = g_strdup ((const char*) exepath), _g_free0 (wadoku_notify_Path), _tmp3_);
	wadoku_notify_Path = (_tmp4_ = string_slice (wadoku_notify_Path, (glong) 0, g_utf8_pointer_to_offset (wadoku_notify_Path, g_utf8_strrchr (wadoku_notify_Path, (gssize) (-1), (gunichar) '/'))), _g_free0 (wadoku_notify_Path), _tmp4_);
	fd = (_tmp6_ = open (_tmp5_ = g_strdup_printf ("%s/wadoku_Database_notify-mod2.sqlite3", wadoku_notify_Path), O_RDONLY, 0), _g_free0 (_tmp5_), _tmp6_);
	if (posix_fadvise (fd, (glong) 0, (glong) 0, POSIX_FADV_RANDOM | POSIX_FADV_WILLNEED) == (-1)) {
		char* _tmp7_;
		fprintf (stderr, "Could not preload Database file %s", _tmp7_ = g_strdup_printf ("%s/wadoku_Database_notify-mod2.sqlite3", wadoku_notify_Path));
		_g_free0 (_tmp7_);
	}
	if ((_tmp9_ = !g_file_test (_tmp8_ = g_strdup_printf ("%s/wadoku_Database_notify-mod2.sqlite3", wadoku_notify_Path), G_FILE_TEST_IS_REGULAR), _g_free0 (_tmp8_), _tmp9_)) {
		fprintf (stderr, "Database %s does not exist or is a directory\n", args[1]);
		close (fd);
		result = -1;
		_g_free0 (exelink);
		return result;
	}
	rc = (_tmp12_ = sqlite3_open (_tmp10_ = g_strdup_printf ("%s/wadoku_Database_notify-mod2.sqlite3", wadoku_notify_Path), &_tmp11_), wadoku_notify_db = (_tmp13_ = _tmp11_, _sqlite3_close0 (wadoku_notify_db), _tmp13_), _tmp12_);
	_g_free0 (_tmp10_);
	if (rc != SQLITE_OK) {
		fprintf (stderr, "Can't open database: %d, %s\n", rc, sqlite3_errmsg (wadoku_notify_db));
		close (fd);
		result = -1;
		_g_free0 (exelink);
		return result;
	}
	gtk_init (&args_length1, &args);
	wadoku_notify_clip = (_tmp14_ = _g_object_ref0 (gtk_clipboard_get (gdk_atom_intern ("PRIMARY", FALSE))), _g_object_unref0 (wadoku_notify_clip), _tmp14_);
	if (wadoku_notify_output == WADOKU_NOTIFY_OUTPUT_TYPE_LibNotify) {
		sqlite3_callback _tmp15_;
		notify_init ("wadoku-notify");
		wadoku_notify_CallbackFunc = (_tmp15_ = _wadoku_notify_callback_notify_sqlite3_callback, ((wadoku_notify_CallbackFunc_target_destroy_notify == NULL) ? NULL : (wadoku_notify_CallbackFunc_target_destroy_notify (wadoku_notify_CallbackFunc_target), NULL), wadoku_notify_CallbackFunc = NULL, wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL), wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL, _tmp15_);
	} else {
		if (wadoku_notify_output == WADOKU_NOTIFY_OUTPUT_TYPE_stdout) {
			sqlite3_callback _tmp16_;
			wadoku_notify_CallbackFunc = (_tmp16_ = _wadoku_notify_callback_stdout_sqlite3_callback, ((wadoku_notify_CallbackFunc_target_destroy_notify == NULL) ? NULL : (wadoku_notify_CallbackFunc_target_destroy_notify (wadoku_notify_CallbackFunc_target), NULL), wadoku_notify_CallbackFunc = NULL, wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL), wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL, _tmp16_);
		} else {
			sqlite3_callback _tmp17_;
			GtkWindow* _tmp18_;
			GdkColor color = {0};
			GtkLabel* _tmp19_;
			GdkColor fg_color = {0};
			wadoku_notify_CallbackFunc = (_tmp17_ = _wadoku_notify_callback_ownerdrawn_sqlite3_callback, ((wadoku_notify_CallbackFunc_target_destroy_notify == NULL) ? NULL : (wadoku_notify_CallbackFunc_target_destroy_notify (wadoku_notify_CallbackFunc_target), NULL), wadoku_notify_CallbackFunc = NULL, wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL), wadoku_notify_CallbackFunc_target = NULL, wadoku_notify_CallbackFunc_target_destroy_notify = NULL, _tmp17_);
			wadoku_notify_window = (_tmp18_ = g_object_ref_sink ((GtkWindow*) gtk_window_new (GTK_WINDOW_POPUP)), _g_object_unref0 (wadoku_notify_window), _tmp18_);
			gtk_window_set_skip_taskbar_hint (wadoku_notify_window, TRUE);
			gtk_window_set_skip_pager_hint (wadoku_notify_window, TRUE);
			g_object_set ((GtkWidget*) wadoku_notify_window, "can-focus", FALSE, NULL);
			gtk_window_move (wadoku_notify_window, gdk_screen_width () - 310, gdk_screen_height () - 250);
			gdk_color_parse ("black", &color);
			gtk_widget_modify_bg ((GtkWidget*) wadoku_notify_window, GTK_STATE_NORMAL, &color);
			gtk_container_set_border_width ((GtkContainer*) wadoku_notify_window, (guint) 2);
			gtk_window_set_opacity (wadoku_notify_window, (double) 0.75f);
			gtk_window_set_default_size (wadoku_notify_window, 300, -1);
			g_signal_connect ((GtkObject*) wadoku_notify_window, "destroy", (GCallback) _gtk_main_quit_gtk_object_destroy, NULL);
			wadoku_notify_label = (_tmp19_ = g_object_ref_sink ((GtkLabel*) gtk_label_new ("")), _g_object_unref0 (wadoku_notify_label), _tmp19_);
			gtk_label_set_selectable (wadoku_notify_label, FALSE);
			gtk_label_set_line_wrap (wadoku_notify_label, TRUE);
			gtk_widget_set_size_request ((GtkWidget*) wadoku_notify_label, 300, -1);
			gdk_color_parse ("white", &fg_color);
			gtk_widget_modify_fg ((GtkWidget*) wadoku_notify_label, GTK_STATE_NORMAL, &fg_color);
			gtk_container_add ((GtkContainer*) wadoku_notify_window, (GtkWidget*) wadoku_notify_label);
			wadoku_notify_Timer = g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 5000, _wadoku_notify_HideTimer_gsource_func, NULL, NULL);
			g_signal_connect (wadoku_notify_window, "enter_notify_event", _wadoku_notify_EnterWindow_gcallback, NULL);
		}
	}
	g_signal_connect (wadoku_notify_clip, "owner_change", _wadoku_notify_clipboard_changed_gcallback, NULL);
	gtk_main ();
	close (fd);
	result = 0;
	_g_free0 (exelink);
	return result;
}


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


WadokuNotify* wadoku_notify_construct (GType object_type) {
	WadokuNotify * self;
	self = (WadokuNotify*) g_object_new (object_type, NULL);
	return self;
}


WadokuNotify* wadoku_notify_new (void) {
	return wadoku_notify_construct (TYPE_WADOKU_NOTIFY);
}


static void wadoku_notify_class_init (WadokuNotifyClass * klass) {
	wadoku_notify_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = wadoku_notify_finalize;
}


static void wadoku_notify_instance_init (WadokuNotify * self) {
}


static void wadoku_notify_finalize (GObject* obj) {
	WadokuNotify * self;
	self = WADOKU_NOTIFY (obj);
	G_OBJECT_CLASS (wadoku_notify_parent_class)->finalize (obj);
}


GType wadoku_notify_get_type (void) {
	static volatile gsize wadoku_notify_type_id__volatile = 0;
	if (g_once_init_enter (&wadoku_notify_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (WadokuNotifyClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) wadoku_notify_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WadokuNotify), 0, (GInstanceInitFunc) wadoku_notify_instance_init, NULL };
		GType wadoku_notify_type_id;
		wadoku_notify_type_id = g_type_register_static (G_TYPE_OBJECT, "WadokuNotify", &g_define_type_info, 0);
		g_once_init_leave (&wadoku_notify_type_id__volatile, wadoku_notify_type_id);
	}
	return wadoku_notify_type_id__volatile;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}




