/* finance.c generated by valac 0.36.3, 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 <stdlib.h>
#include <string.h>
#include "config.h"
#include <float.h>
#include <math.h>
#include <gio/gio.h>
#include <gdk/gdk.h>

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (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)))



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


gboolean finance_should_show_dialog_on_startup (void) {
	gboolean result = FALSE;
	GSettings* settings = NULL;
	GSettings* _tmp0_;
	gchar* date = NULL;
	GSettings* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	GSettings* _tmp4_;
	gboolean _tmp5_;
	_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_;
		gchar** _tmp7_;
		gchar** _tmp8_;
		gint ymd_length1;
		gint _ymd_size_;
		gchar** _tmp9_;
		gint _tmp9__length1;
		gint year = 0;
		gchar** _tmp10_;
		gint _tmp10__length1;
		const gchar* _tmp11_;
		gint _tmp12_;
		gint month = 0;
		gchar** _tmp13_;
		gint _tmp13__length1;
		const gchar* _tmp14_;
		gint _tmp15_;
		gint day = 0;
		gchar** _tmp16_;
		gint _tmp16__length1;
		const gchar* _tmp17_;
		gint _tmp18_;
		GDateTime* last_time = NULL;
		gint _tmp19_;
		gint _tmp20_;
		gint _tmp21_;
		GDateTime* _tmp22_;
		GDateTime* cur_time = NULL;
		GDateTime* _tmp23_;
		GDateTime* time_limit = NULL;
		GDateTime* _tmp24_;
		GDateTime* _tmp25_;
		GDateTime* _tmp26_;
		GDateTime* _tmp27_;
		gint _tmp28_;
		_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_;
	GDateTime* time = NULL;
	GDateTime* _tmp1_;
	gchar* date = NULL;
	gint _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	gchar* _tmp5_;
	_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 (GtkWindow* parent_window) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (parent_window != NULL);
	{
		gchar* uri = NULL;
		gchar* _tmp0_;
		GtkWindow* _tmp1_;
		GdkScreen* _tmp2_;
		_tmp0_ = g_strdup ("https://people.gnome.org/~swilmet/latexila/support.html");
		uri = _tmp0_;
		_tmp1_ = parent_window;
		_tmp2_ = gtk_window_get_screen (_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_;
		const gchar* _tmp4_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_warning ("finance.vala:167: 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;
}



