/* finance.c generated by valac 0.30.0.2-88328, the Vala compiler
 * generated from finance.vala, do not modify */

/*
 * This file is part of LaTeXila.
 *
 * Copyright © 2015 - Sébastien Wilmet
 *
 * LaTeXila 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.
 *
 * LaTeXila 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 LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include "config.h"
#include <float.h>
#include <math.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.h>


#define TYPE_MAIN_WINDOW (main_window_get_type ())
#define MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAIN_WINDOW, MainWindow))
#define MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAIN_WINDOW, MainWindowClass))
#define IS_MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAIN_WINDOW))
#define IS_MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAIN_WINDOW))
#define MAIN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAIN_WINDOW, MainWindowClass))

typedef struct _MainWindow MainWindow;
typedef struct _MainWindowClass MainWindowClass;
#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_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))



GType main_window_get_type (void) G_GNUC_CONST;
void finance_show_dialog (MainWindow* window, gboolean startup);
gboolean finance_should_show_dialog_on_startup (void);
void finance_open_donate_page (MainWindow* window);
void finance_save_date (void);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);


void finance_show_dialog (MainWindow* window, gboolean startup) {
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GtkDialog* dialog = NULL;
	MainWindow* _tmp3_ = NULL;
	GtkDialog* _tmp4_ = NULL;
	GtkDialog* _tmp5_ = NULL;
	GtkDialog* _tmp6_ = NULL;
	GtkBox* content_area = NULL;
	GtkDialog* _tmp7_ = NULL;
	GtkBox* _tmp8_ = NULL;
	GtkBox* _tmp9_ = NULL;
	GtkBox* _tmp10_ = NULL;
	GtkBox* _tmp11_ = NULL;
	GtkImage* image = NULL;
	GtkImage* _tmp12_ = NULL;
	GtkBox* _tmp13_ = NULL;
	GtkImage* _tmp14_ = NULL;
	GtkLabel* label = NULL;
	GtkLabel* _tmp15_ = NULL;
	GtkLabel* _tmp16_ = NULL;
	GtkBox* _tmp17_ = NULL;
	GtkLabel* _tmp18_ = NULL;
	GtkLabel* _tmp19_ = NULL;
	GtkBox* _tmp20_ = NULL;
	GtkLabel* _tmp21_ = NULL;
	GtkLabel* _tmp22_ = NULL;
	GtkLabel* _tmp23_ = NULL;
	GtkLabel* _tmp24_ = NULL;
	GtkLabel* _tmp25_ = NULL;
	GtkLabel* _tmp26_ = NULL;
	GtkBox* _tmp27_ = NULL;
	GtkLabel* _tmp28_ = NULL;
	gboolean _tmp29_ = FALSE;
	GtkLabel* _tmp36_ = NULL;
	GtkLabel* _tmp37_ = NULL;
	GtkBox* _tmp38_ = NULL;
	GtkLabel* _tmp39_ = NULL;
	GtkCheckButton* remind_later_checkbutton = NULL;
	GtkCheckButton* _tmp40_ = NULL;
	GtkCheckButton* _tmp41_ = NULL;
	GtkCheckButton* _tmp42_ = NULL;
	GtkCheckButton* _tmp43_ = NULL;
	GtkBox* _tmp44_ = NULL;
	GtkCheckButton* _tmp45_ = NULL;
	GtkBox* _tmp46_ = NULL;
	GtkDialog* _tmp55_ = NULL;
	g_return_if_fail (window != NULL);
	_tmp1_ = startup;
	if (_tmp1_) {
		gboolean _tmp2_ = FALSE;
		_tmp2_ = finance_should_show_dialog_on_startup ();
		_tmp0_ = !_tmp2_;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		return;
	}
	_tmp3_ = window;
	_tmp4_ = (GtkDialog*) gtk_dialog_new_with_buttons ("LaTeXila Finance", (GtkWindow*) _tmp3_, GTK_DIALOG_DESTROY_WITH_PARENT, "_Close", GTK_RESPONSE_CLOSE, "LaTeXila _Fundraiser", GTK_RESPONSE_ACCEPT, NULL, NULL);
	g_object_ref_sink (_tmp4_);
	dialog = _tmp4_;
	_tmp5_ = dialog;
	gtk_window_set_resizable ((GtkWindow*) _tmp5_, FALSE);
	_tmp6_ = dialog;
	gtk_dialog_set_default_response (_tmp6_, (gint) GTK_RESPONSE_ACCEPT);
	_tmp7_ = dialog;
	_tmp8_ = (GtkBox*) gtk_dialog_get_content_area (_tmp7_);
	content_area = _tmp8_;
	_tmp9_ = content_area;
	gtk_box_set_spacing (_tmp9_, 6);
	_tmp10_ = content_area;
	g_object_set ((GtkWidget*) _tmp10_, "margin", 12, NULL);
	_tmp11_ = content_area;
	gtk_widget_set_margin_top ((GtkWidget*) _tmp11_, 6);
	_tmp12_ = (GtkImage*) gtk_image_new_from_file (DATA_DIR "/images/app/logo.png");
	g_object_ref_sink (_tmp12_);
	image = _tmp12_;
	_tmp13_ = content_area;
	_tmp14_ = image;
	gtk_container_add ((GtkContainer*) _tmp13_, (GtkWidget*) _tmp14_);
	_tmp15_ = (GtkLabel*) gtk_label_new (NULL);
	g_object_ref_sink (_tmp15_);
	label = _tmp15_;
	_tmp16_ = label;
	gtk_label_set_markup (_tmp16_, "<big>Did you know...</big>");
	_tmp17_ = content_area;
	_tmp18_ = label;
	gtk_container_add ((GtkContainer*) _tmp17_, (GtkWidget*) _tmp18_);
	_tmp19_ = (GtkLabel*) gtk_label_new ("...that you can support LaTeXila financially?");
	g_object_ref_sink (_tmp19_);
	_g_object_unref0 (label);
	label = _tmp19_;
	_tmp20_ = content_area;
	_tmp21_ = label;
	gtk_container_add ((GtkContainer*) _tmp20_, (GtkWidget*) _tmp21_);
	_tmp22_ = (GtkLabel*) gtk_label_new (NULL);
	g_object_ref_sink (_tmp22_);
	_g_object_unref0 (label);
	label = _tmp22_;
	_tmp23_ = label;
	gtk_label_set_markup (_tmp23_, "LaTeXila is a free/<i>libre</i> LaTeX editor and comes" " free of charge. But if you appreciate this software, you are encourag" \
"ed" " to make a donation to help its future development.");
	_tmp24_ = label;
	gtk_label_set_max_width_chars (_tmp24_, 60);
	_tmp25_ = label;
	gtk_label_set_line_wrap (_tmp25_, TRUE);
	_tmp26_ = label;
	gtk_label_set_xalign (_tmp26_, (gfloat) 0);
	_tmp27_ = content_area;
	_tmp28_ = label;
	gtk_container_add ((GtkContainer*) _tmp27_, (GtkWidget*) _tmp28_);
	_tmp29_ = startup;
	if (_tmp29_) {
		GtkLabel* _tmp30_ = NULL;
		GtkLabel* _tmp31_ = NULL;
		GtkLabel* _tmp32_ = NULL;
		GtkLabel* _tmp33_ = NULL;
		GtkBox* _tmp34_ = NULL;
		GtkLabel* _tmp35_ = NULL;
		_tmp30_ = (GtkLabel*) gtk_label_new ("You can see again this information at any time by going to the Help me" \
"nu.");
		g_object_ref_sink (_tmp30_);
		_g_object_unref0 (label);
		label = _tmp30_;
		_tmp31_ = label;
		gtk_label_set_max_width_chars (_tmp31_, 60);
		_tmp32_ = label;
		gtk_label_set_line_wrap (_tmp32_, TRUE);
		_tmp33_ = label;
		gtk_label_set_xalign (_tmp33_, (gfloat) 0);
		_tmp34_ = content_area;
		_tmp35_ = label;
		gtk_container_add ((GtkContainer*) _tmp34_, (GtkWidget*) _tmp35_);
	}
	_tmp36_ = (GtkLabel*) gtk_label_new ("Thanks!");
	g_object_ref_sink (_tmp36_);
	_g_object_unref0 (label);
	label = _tmp36_;
	_tmp37_ = label;
	gtk_label_set_xalign (_tmp37_, (gfloat) 0);
	_tmp38_ = content_area;
	_tmp39_ = label;
	gtk_container_add ((GtkContainer*) _tmp38_, (GtkWidget*) _tmp39_);
	_tmp40_ = (GtkCheckButton*) gtk_check_button_new_with_mnemonic ("_Remind me later (in one month)");
	g_object_ref_sink (_tmp40_);
	remind_later_checkbutton = _tmp40_;
	_tmp41_ = remind_later_checkbutton;
	gtk_toggle_button_set_active ((GtkToggleButton*) _tmp41_, FALSE);
	_tmp42_ = remind_later_checkbutton;
	gtk_widget_set_margin_top ((GtkWidget*) _tmp42_, 12);
	_tmp43_ = remind_later_checkbutton;
	gtk_widget_set_margin_bottom ((GtkWidget*) _tmp43_, 6);
	_tmp44_ = content_area;
	_tmp45_ = remind_later_checkbutton;
	gtk_container_add ((GtkContainer*) _tmp44_, (GtkWidget*) _tmp45_);
	_tmp46_ = content_area;
	gtk_widget_show_all ((GtkWidget*) _tmp46_);
	while (TRUE) {
		gint response = 0;
		GtkDialog* _tmp47_ = NULL;
		gint _tmp48_ = 0;
		gint _tmp49_ = 0;
		GSettings* settings = NULL;
		GSettings* _tmp51_ = NULL;
		GSettings* _tmp52_ = NULL;
		GtkCheckButton* _tmp53_ = NULL;
		gboolean _tmp54_ = FALSE;
		_tmp47_ = dialog;
		_tmp48_ = gtk_dialog_run (_tmp47_);
		response = _tmp48_;
		_tmp49_ = response;
		if (_tmp49_ == ((gint) GTK_RESPONSE_ACCEPT)) {
			MainWindow* _tmp50_ = NULL;
			_tmp50_ = window;
			finance_open_donate_page (_tmp50_);
			continue;
		}
		_tmp51_ = g_settings_new ("org.gnome.latexila.state.dialogs.finance");
		settings = _tmp51_;
		_tmp52_ = settings;
		_tmp53_ = remind_later_checkbutton;
		_tmp54_ = gtk_toggle_button_get_active ((GtkToggleButton*) _tmp53_);
		g_settings_set_boolean (_tmp52_, "remind-later", _tmp54_);
		_g_object_unref0 (settings);
		break;
	}
	_tmp55_ = dialog;
	gtk_widget_destroy ((GtkWidget*) _tmp55_);
	finance_save_date ();
	_g_object_unref0 (remind_later_checkbutton);
	_g_object_unref0 (label);
	_g_object_unref0 (image);
	_g_object_unref0 (dialog);
}


gboolean finance_should_show_dialog_on_startup (void) {
	gboolean result = FALSE;
	GSettings* settings = NULL;
	GSettings* _tmp0_ = NULL;
	gchar* date = NULL;
	GSettings* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	GSettings* _tmp4_ = NULL;
	gboolean _tmp5_ = FALSE;
	_tmp0_ = g_settings_new ("org.gnome.latexila.state.dialogs.finance");
	settings = _tmp0_;
	_tmp1_ = settings;
	_tmp2_ = g_settings_get_string (_tmp1_, "last-shown-date");
	date = _tmp2_;
	_tmp3_ = date;
	if (g_strcmp0 (_tmp3_, "") == 0) {
		result = TRUE;
		_g_free0 (date);
		_g_object_unref0 (settings);
		return result;
	}
	_tmp4_ = settings;
	_tmp5_ = g_settings_get_boolean (_tmp4_, "remind-later");
	if (_tmp5_) {
		gchar** ymd = NULL;
		const gchar* _tmp6_ = NULL;
		gchar** _tmp7_ = NULL;
		gchar** _tmp8_ = NULL;
		gint ymd_length1 = 0;
		gint _ymd_size_ = 0;
		gchar** _tmp9_ = NULL;
		gint _tmp9__length1 = 0;
		gint year = 0;
		gchar** _tmp10_ = NULL;
		gint _tmp10__length1 = 0;
		const gchar* _tmp11_ = NULL;
		gint _tmp12_ = 0;
		gint month = 0;
		gchar** _tmp13_ = NULL;
		gint _tmp13__length1 = 0;
		const gchar* _tmp14_ = NULL;
		gint _tmp15_ = 0;
		gint day = 0;
		gchar** _tmp16_ = NULL;
		gint _tmp16__length1 = 0;
		const gchar* _tmp17_ = NULL;
		gint _tmp18_ = 0;
		GDateTime* last_time = NULL;
		gint _tmp19_ = 0;
		gint _tmp20_ = 0;
		gint _tmp21_ = 0;
		GDateTime* _tmp22_ = NULL;
		GDateTime* cur_time = NULL;
		GDateTime* _tmp23_ = NULL;
		GDateTime* time_limit = NULL;
		GDateTime* _tmp24_ = NULL;
		GDateTime* _tmp25_ = NULL;
		GDateTime* _tmp26_ = NULL;
		GDateTime* _tmp27_ = NULL;
		gint _tmp28_ = 0;
		_tmp6_ = date;
		_tmp8_ = _tmp7_ = g_strsplit (_tmp6_, "-", 0);
		ymd = _tmp8_;
		ymd_length1 = _vala_array_length (_tmp7_);
		_ymd_size_ = ymd_length1;
		_tmp9_ = ymd;
		_tmp9__length1 = ymd_length1;
		if (_tmp9__length1 != 3) {
			result = FALSE;
			ymd = (_vala_array_free (ymd, ymd_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (date);
			_g_object_unref0 (settings);
			return result;
		}
		_tmp10_ = ymd;
		_tmp10__length1 = ymd_length1;
		_tmp11_ = _tmp10_[0];
		_tmp12_ = atoi (_tmp11_);
		year = _tmp12_;
		_tmp13_ = ymd;
		_tmp13__length1 = ymd_length1;
		_tmp14_ = _tmp13_[1];
		_tmp15_ = atoi (_tmp14_);
		month = _tmp15_;
		_tmp16_ = ymd;
		_tmp16__length1 = ymd_length1;
		_tmp17_ = _tmp16_[2];
		_tmp18_ = atoi (_tmp17_);
		day = _tmp18_;
		_tmp19_ = year;
		_tmp20_ = month;
		_tmp21_ = day;
		_tmp22_ = g_date_time_new_utc (_tmp19_, _tmp20_, _tmp21_, 0, 0, (gdouble) 0);
		last_time = _tmp22_;
		_tmp23_ = g_date_time_new_now_utc ();
		cur_time = _tmp23_;
		_tmp24_ = last_time;
		_tmp25_ = g_date_time_add_months (_tmp24_, 1);
		time_limit = _tmp25_;
		_tmp26_ = time_limit;
		_tmp27_ = cur_time;
		_tmp28_ = g_date_time_compare (_tmp26_, _tmp27_);
		if (_tmp28_ <= 0) {
			result = TRUE;
			_g_date_time_unref0 (time_limit);
			_g_date_time_unref0 (cur_time);
			_g_date_time_unref0 (last_time);
			ymd = (_vala_array_free (ymd, ymd_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (date);
			_g_object_unref0 (settings);
			return result;
		}
		_g_date_time_unref0 (time_limit);
		_g_date_time_unref0 (cur_time);
		_g_date_time_unref0 (last_time);
		ymd = (_vala_array_free (ymd, ymd_length1, (GDestroyNotify) g_free), NULL);
	}
	result = FALSE;
	_g_free0 (date);
	_g_object_unref0 (settings);
	return result;
}


void finance_save_date (void) {
	GSettings* settings = NULL;
	GSettings* _tmp0_ = NULL;
	GDateTime* time = NULL;
	GDateTime* _tmp1_ = NULL;
	gchar* date = NULL;
	gint _tmp2_ = 0;
	gint _tmp3_ = 0;
	gint _tmp4_ = 0;
	gchar* _tmp5_ = NULL;
	_tmp0_ = g_settings_new ("org.gnome.latexila.state.dialogs.finance");
	settings = _tmp0_;
	_tmp1_ = g_date_time_new_now_utc ();
	time = _tmp1_;
	_tmp2_ = g_date_time_get_year (time);
	_tmp3_ = g_date_time_get_month (time);
	_tmp4_ = g_date_time_get_day_of_month (time);
	_tmp5_ = g_strdup_printf ("%d-%d-%d", _tmp2_, _tmp3_, _tmp4_);
	date = _tmp5_;
	g_settings_set_string (settings, "last-shown-date", date);
	_g_free0 (date);
	_g_date_time_unref0 (time);
	_g_object_unref0 (settings);
}


void finance_open_donate_page (MainWindow* window) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (window != NULL);
	{
		gchar* uri = NULL;
		gchar* _tmp0_ = NULL;
		MainWindow* _tmp1_ = NULL;
		GdkScreen* _tmp2_ = NULL;
		_tmp0_ = g_strdup ("https://wiki.gnome.org/Apps/LaTeXila/donate");
		uri = _tmp0_;
		_tmp1_ = window;
		_tmp2_ = gtk_window_get_screen ((GtkWindow*) _tmp1_);
		gtk_show_uri (_tmp2_, uri, (guint32) GDK_CURRENT_TIME, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_free0 (uri);
			goto __catch25_g_error;
		}
		_g_free0 (uri);
	}
	goto __finally25;
	__catch25_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_ = NULL;
		const gchar* _tmp4_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_warning ("finance.vala:161: Impossible to open the donate page: %s", _tmp4_);
		_g_error_free0 (e);
	}
	__finally25:
	if (G_UNLIKELY (_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;
	}
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



