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

/*
 * Copyright © 2023 Alain M. (https://github.com/alainm23/planify)
 *
 * 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include <glib-object.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include "planify.h"
#include <stdlib.h>
#include <string.h>
#include <cairo-pdf.h>
#include <cairo-gobject.h>
#include <gee.h>
#include <glib/gi18n-lib.h>

#define SERVICES_EXPORT_SERVICE_PAGE_WIDTH 595.0
#define SERVICES_EXPORT_SERVICE_PAGE_HEIGHT 842.0
#define SERVICES_EXPORT_SERVICE_MARGIN 50.0
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#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 SERVICES_TYPE_EXPORT_SERVICE (services_export_service_get_type ())
#define SERVICES_EXPORT_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SERVICES_TYPE_EXPORT_SERVICE, ServicesExportService))
#define SERVICES_EXPORT_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SERVICES_TYPE_EXPORT_SERVICE, ServicesExportServiceClass))
#define SERVICES_IS_EXPORT_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SERVICES_TYPE_EXPORT_SERVICE))
#define SERVICES_IS_EXPORT_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SERVICES_TYPE_EXPORT_SERVICE))
#define SERVICES_EXPORT_SERVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SERVICES_TYPE_EXPORT_SERVICE, ServicesExportServiceClass))

typedef struct _ServicesExportService ServicesExportService;
typedef struct _ServicesExportServiceClass ServicesExportServiceClass;
typedef struct _ServicesExportServicePrivate ServicesExportServicePrivate;
enum  {
	SERVICES_EXPORT_SERVICE_0_PROPERTY,
	SERVICES_EXPORT_SERVICE_NUM_PROPERTIES
};
static GParamSpec* services_export_service_properties[SERVICES_EXPORT_SERVICE_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_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
#define _cairo_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_destroy (var), NULL)))
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))

struct _ServicesExportService {
	GObject parent_instance;
	ServicesExportServicePrivate * priv;
};

struct _ServicesExportServiceClass {
	GObjectClass parent_class;
};

static gpointer services_export_service_parent_class = NULL;
static ServicesExportService* services_export_service__instance;
static ServicesExportService* services_export_service__instance = NULL;

VALA_EXTERN GType services_export_service_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ServicesExportService, g_object_unref)
VALA_EXTERN ServicesExportService* services_export_service_get_default (void);
VALA_EXTERN ServicesExportService* services_export_service_new (void);
VALA_EXTERN ServicesExportService* services_export_service_construct (GType object_type);
VALA_EXTERN void services_export_service_export_project_pdf (ServicesExportService* self,
                                                 ObjectsProject* project,
                                                 const gchar* output_path);
static GeeArrayList* services_export_service_wrap_text (ServicesExportService* self,
                                                 cairo_t* cr,
                                                 const gchar* text,
                                                 gdouble max_width);
static gdouble services_export_service_draw_items (ServicesExportService* self,
                                            cairo_t* cr,
                                            cairo_surface_t* surface,
                                            GeeArrayList* items,
                                            gdouble start_y,
                                            gdouble x);
static gint __lambda243_ (ServicesExportService* self,
                   ObjectsSection* a,
                   ObjectsSection* b);
static gint ___lambda243__gcompare_data_func (gconstpointer a,
                                       gconstpointer b,
                                       gpointer self);
static void services_export_service_set_priority_color (ServicesExportService* self,
                                                 cairo_t* cr,
                                                 gint priority);
static void services_export_service_finalize (GObject * obj);
static GType services_export_service_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

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

ServicesExportService*
services_export_service_get_default (void)
{
	ServicesExportService* _tmp0_;
	ServicesExportService* _tmp2_;
	ServicesExportService* _tmp3_;
	ServicesExportService* result;
	_tmp0_ = services_export_service__instance;
	if (_tmp0_ == NULL) {
		ServicesExportService* _tmp1_;
		_tmp1_ = services_export_service_new ();
		_g_object_unref0 (services_export_service__instance);
		services_export_service__instance = _tmp1_;
	}
	_tmp2_ = services_export_service__instance;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static gint
__lambda243_ (ServicesExportService* self,
              ObjectsSection* a,
              ObjectsSection* b)
{
	gint _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint result;
	g_return_val_if_fail (a != NULL, -1);
	g_return_val_if_fail (b != NULL, -1);
	_tmp0_ = objects_section_get_section_order (a);
	_tmp1_ = _tmp0_;
	_tmp2_ = objects_section_get_section_order (b);
	_tmp3_ = _tmp2_;
	result = _tmp1_ - _tmp3_;
	return result;
}

static gint
___lambda243__gcompare_data_func (gconstpointer a,
                                  gconstpointer b,
                                  gpointer self)
{
	gint result;
	result = __lambda243_ ((ServicesExportService*) self, (ObjectsSection*) a, (ObjectsSection*) b);
	return result;
}

void
services_export_service_export_project_pdf (ServicesExportService* self,
                                            ObjectsProject* project,
                                            const gchar* output_path)
{
	cairo_surface_t* surface = NULL;
	cairo_surface_t* _tmp0_;
	cairo_t* cr = NULL;
	cairo_surface_t* _tmp1_;
	cairo_t* _tmp2_;
	gdouble y = 0.0;
	cairo_t* _tmp3_;
	cairo_t* _tmp4_;
	cairo_t* _tmp5_;
	cairo_t* _tmp6_;
	cairo_t* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp9_;
	const gchar* _tmp10_;
	const gchar* _tmp11_;
	const gchar* _tmp29_;
	const gchar* _tmp30_;
	GeeArrayList* _tmp45_;
	GeeArrayList* _tmp46_;
	gint _tmp47_;
	gint _tmp48_;
	GeeArrayList* sorted_sections = NULL;
	GeeArrayList* _tmp53_;
	GeeArrayList* _tmp54_;
	GeeArrayList* _tmp55_;
	GeeArrayList* _tmp56_;
	GeeArrayList* _tmp57_;
	gboolean is_first_section = FALSE;
	cairo_surface_t* _tmp90_;
	cairo_surface_t* _tmp91_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (project != NULL);
	g_return_if_fail (output_path != NULL);
	_tmp0_ = cairo_pdf_surface_create (output_path, SERVICES_EXPORT_SERVICE_PAGE_WIDTH, SERVICES_EXPORT_SERVICE_PAGE_HEIGHT);
	surface = _tmp0_;
	_tmp1_ = surface;
	_tmp2_ = cairo_create (_tmp1_);
	cr = _tmp2_;
	y = SERVICES_EXPORT_SERVICE_MARGIN;
	_tmp3_ = cr;
	cairo_select_font_face (_tmp3_, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
	_tmp4_ = cr;
	cairo_set_font_size (_tmp4_, (gdouble) 24);
	_tmp5_ = cr;
	cairo_set_source_rgb (_tmp5_, (gdouble) 0, (gdouble) 0, (gdouble) 0);
	y += (gdouble) 24;
	_tmp6_ = cr;
	cairo_move_to (_tmp6_, SERVICES_EXPORT_SERVICE_MARGIN, y);
	_tmp7_ = cr;
	_tmp8_ = objects_base_object_get_name ((ObjectsBaseObject*) project);
	_tmp9_ = _tmp8_;
	cairo_show_text (_tmp7_, _tmp9_);
	_tmp10_ = objects_project_get_description (project);
	_tmp11_ = _tmp10_;
	if (g_strcmp0 (_tmp11_, "") != 0) {
		cairo_t* _tmp12_;
		cairo_t* _tmp13_;
		cairo_t* _tmp14_;
		y += (gdouble) 24;
		_tmp12_ = cr;
		cairo_select_font_face (_tmp12_, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
		_tmp13_ = cr;
		cairo_set_font_size (_tmp13_, (gdouble) 11);
		_tmp14_ = cr;
		cairo_set_source_rgb (_tmp14_, 0.4, 0.4, 0.4);
		{
			GeeArrayList* _line_list = NULL;
			cairo_t* _tmp15_;
			const gchar* _tmp16_;
			const gchar* _tmp17_;
			GeeArrayList* _tmp18_;
			gint _line_size = 0;
			GeeArrayList* _tmp19_;
			gint _tmp20_;
			gint _tmp21_;
			gint _line_index = 0;
			_tmp15_ = cr;
			_tmp16_ = objects_project_get_description (project);
			_tmp17_ = _tmp16_;
			_tmp18_ = services_export_service_wrap_text (self, _tmp15_, _tmp17_, SERVICES_EXPORT_SERVICE_PAGE_WIDTH - (SERVICES_EXPORT_SERVICE_MARGIN * 2));
			_line_list = _tmp18_;
			_tmp19_ = _line_list;
			_tmp20_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp19_);
			_tmp21_ = _tmp20_;
			_line_size = _tmp21_;
			_line_index = -1;
			while (TRUE) {
				gint _tmp22_;
				gint _tmp23_;
				gchar* line = NULL;
				GeeArrayList* _tmp24_;
				gpointer _tmp25_;
				cairo_t* _tmp26_;
				cairo_t* _tmp27_;
				const gchar* _tmp28_;
				_line_index = _line_index + 1;
				_tmp22_ = _line_index;
				_tmp23_ = _line_size;
				if (!(_tmp22_ < _tmp23_)) {
					break;
				}
				_tmp24_ = _line_list;
				_tmp25_ = gee_abstract_list_get ((GeeAbstractList*) _tmp24_, _line_index);
				line = (gchar*) _tmp25_;
				_tmp26_ = cr;
				cairo_move_to (_tmp26_, SERVICES_EXPORT_SERVICE_MARGIN, y);
				_tmp27_ = cr;
				_tmp28_ = line;
				cairo_show_text (_tmp27_, _tmp28_);
				y += (gdouble) 16;
				_g_free0 (line);
			}
			_g_object_unref0 (_line_list);
		}
	}
	_tmp29_ = objects_project_get_due_date (project);
	_tmp30_ = _tmp29_;
	if (g_strcmp0 (_tmp30_, "") != 0) {
		cairo_t* _tmp31_;
		cairo_t* _tmp32_;
		cairo_t* _tmp33_;
		cairo_t* _tmp34_;
		GDateTime* datetime = NULL;
		const gchar* _tmp35_;
		const gchar* _tmp36_;
		GDateTime* _tmp37_;
		GDateTime* _tmp38_;
		y += (gdouble) 6;
		_tmp31_ = cr;
		cairo_select_font_face (_tmp31_, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
		_tmp32_ = cr;
		cairo_set_font_size (_tmp32_, (gdouble) 10);
		_tmp33_ = cr;
		cairo_set_source_rgb (_tmp33_, 0.3, 0.3, 0.3);
		_tmp34_ = cr;
		cairo_move_to (_tmp34_, SERVICES_EXPORT_SERVICE_MARGIN, y);
		_tmp35_ = objects_project_get_due_date (project);
		_tmp36_ = _tmp35_;
		_tmp37_ = utils_datetime_get_date_from_string (_tmp36_);
		datetime = _tmp37_;
		_tmp38_ = datetime;
		if (_tmp38_ != NULL) {
			cairo_t* _tmp39_;
			GDateTime* _tmp40_;
			gchar* _tmp41_;
			gchar* _tmp42_;
			gchar* _tmp43_;
			gchar* _tmp44_;
			_tmp39_ = cr;
			_tmp40_ = datetime;
			_tmp41_ = utils_datetime_get_short_date_format_from_date (_tmp40_);
			_tmp42_ = _tmp41_;
			_tmp43_ = g_strdup_printf ("%s: %s", _ ("Deadline"), _tmp42_);
			_tmp44_ = _tmp43_;
			cairo_show_text (_tmp39_, _tmp44_);
			_g_free0 (_tmp44_);
			_g_free0 (_tmp42_);
		}
		_g_date_time_unref0 (datetime);
	}
	_tmp45_ = objects_project_get_items (project);
	_tmp46_ = _tmp45_;
	_tmp47_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp46_);
	_tmp48_ = _tmp47_;
	if (_tmp48_ > 0) {
		cairo_t* _tmp49_;
		cairo_surface_t* _tmp50_;
		GeeArrayList* _tmp51_;
		GeeArrayList* _tmp52_;
		y += (gdouble) 12;
		_tmp49_ = cr;
		_tmp50_ = surface;
		_tmp51_ = objects_project_get_items (project);
		_tmp52_ = _tmp51_;
		y = services_export_service_draw_items (self, _tmp49_, _tmp50_, _tmp52_, y, SERVICES_EXPORT_SERVICE_MARGIN);
	}
	_tmp53_ = gee_array_list_new (OBJECTS_TYPE_SECTION, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	sorted_sections = _tmp53_;
	_tmp54_ = sorted_sections;
	_tmp55_ = objects_project_get_sections (project);
	_tmp56_ = _tmp55_;
	gee_array_list_add_all (_tmp54_, (GeeCollection*) _tmp56_);
	_tmp57_ = sorted_sections;
	gee_list_sort ((GeeList*) _tmp57_, ___lambda243__gcompare_data_func, g_object_ref (self), g_object_unref);
	is_first_section = TRUE;
	{
		GeeArrayList* _section_list = NULL;
		GeeArrayList* _tmp58_;
		gint _section_size = 0;
		GeeArrayList* _tmp59_;
		gint _tmp60_;
		gint _tmp61_;
		gint _section_index = 0;
		_tmp58_ = sorted_sections;
		_section_list = _tmp58_;
		_tmp59_ = _section_list;
		_tmp60_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp59_);
		_tmp61_ = _tmp60_;
		_section_size = _tmp61_;
		_section_index = -1;
		while (TRUE) {
			gint _tmp62_;
			gint _tmp63_;
			ObjectsSection* section = NULL;
			GeeArrayList* _tmp64_;
			gpointer _tmp65_;
			gboolean _tmp67_ = FALSE;
			cairo_t* _tmp72_;
			cairo_t* _tmp73_;
			cairo_t* _tmp74_;
			cairo_t* _tmp75_;
			cairo_t* _tmp76_;
			ObjectsSection* _tmp77_;
			const gchar* _tmp78_;
			const gchar* _tmp79_;
			ObjectsSection* _tmp80_;
			GeeArrayList* _tmp81_;
			GeeArrayList* _tmp82_;
			gint _tmp83_;
			gint _tmp84_;
			_section_index = _section_index + 1;
			_tmp62_ = _section_index;
			_tmp63_ = _section_size;
			if (!(_tmp62_ < _tmp63_)) {
				break;
			}
			_tmp64_ = _section_list;
			_tmp65_ = gee_abstract_list_get ((GeeAbstractList*) _tmp64_, _section_index);
			section = (ObjectsSection*) _tmp65_;
			if (y > ((SERVICES_EXPORT_SERVICE_PAGE_HEIGHT - SERVICES_EXPORT_SERVICE_MARGIN) - 30)) {
				cairo_surface_t* _tmp66_;
				_tmp66_ = surface;
				cairo_surface_show_page (_tmp66_);
				y = SERVICES_EXPORT_SERVICE_MARGIN;
			}
			if (is_first_section) {
				GeeArrayList* _tmp68_;
				GeeArrayList* _tmp69_;
				gint _tmp70_;
				gint _tmp71_;
				_tmp68_ = objects_project_get_items (project);
				_tmp69_ = _tmp68_;
				_tmp70_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp69_);
				_tmp71_ = _tmp70_;
				_tmp67_ = _tmp71_ <= 0;
			} else {
				_tmp67_ = FALSE;
			}
			if (_tmp67_) {
				y += (gdouble) 24;
			} else {
				y += (gdouble) 12;
			}
			is_first_section = FALSE;
			_tmp72_ = cr;
			cairo_select_font_face (_tmp72_, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
			_tmp73_ = cr;
			cairo_set_font_size (_tmp73_, (gdouble) 11);
			_tmp74_ = cr;
			cairo_set_source_rgb (_tmp74_, (gdouble) 0, (gdouble) 0, (gdouble) 0);
			_tmp75_ = cr;
			cairo_move_to (_tmp75_, SERVICES_EXPORT_SERVICE_MARGIN, y);
			_tmp76_ = cr;
			_tmp77_ = section;
			_tmp78_ = objects_base_object_get_name ((ObjectsBaseObject*) _tmp77_);
			_tmp79_ = _tmp78_;
			cairo_show_text (_tmp76_, _tmp79_);
			y += (gdouble) 24;
			_tmp80_ = section;
			_tmp81_ = objects_section_get_items (_tmp80_);
			_tmp82_ = _tmp81_;
			_tmp83_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp82_);
			_tmp84_ = _tmp83_;
			if (_tmp84_ > 0) {
				cairo_t* _tmp85_;
				cairo_surface_t* _tmp86_;
				ObjectsSection* _tmp87_;
				GeeArrayList* _tmp88_;
				GeeArrayList* _tmp89_;
				_tmp85_ = cr;
				_tmp86_ = surface;
				_tmp87_ = section;
				_tmp88_ = objects_section_get_items (_tmp87_);
				_tmp89_ = _tmp88_;
				y = services_export_service_draw_items (self, _tmp85_, _tmp86_, _tmp89_, y, SERVICES_EXPORT_SERVICE_MARGIN);
			}
			_g_object_unref0 (section);
		}
	}
	_tmp90_ = surface;
	cairo_surface_show_page (_tmp90_);
	_tmp91_ = surface;
	cairo_surface_finish (_tmp91_);
	_g_object_unref0 (sorted_sections);
	_cairo_destroy0 (cr);
	_cairo_surface_destroy0 (surface);
}

static gdouble
services_export_service_draw_items (ServicesExportService* self,
                                    cairo_t* cr,
                                    cairo_surface_t* surface,
                                    GeeArrayList* items,
                                    gdouble start_y,
                                    gdouble x)
{
	gdouble y = 0.0;
	gdouble result;
	g_return_val_if_fail (self != NULL, -1.0);
	g_return_val_if_fail (cr != NULL, -1.0);
	g_return_val_if_fail (surface != NULL, -1.0);
	g_return_val_if_fail (items != NULL, -1.0);
	y = start_y;
	{
		GeeArrayList* _item_list = NULL;
		gint _item_size = 0;
		GeeArrayList* _tmp0_;
		gint _tmp1_;
		gint _tmp2_;
		gint _item_index = 0;
		_item_list = items;
		_tmp0_ = _item_list;
		_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
		_tmp2_ = _tmp1_;
		_item_size = _tmp2_;
		_item_index = -1;
		while (TRUE) {
			gint _tmp3_;
			gint _tmp4_;
			ObjectsItem* item = NULL;
			GeeArrayList* _tmp5_;
			gpointer _tmp6_;
			const gchar* _tmp7_ = NULL;
			ObjectsItem* _tmp8_;
			gboolean _tmp9_;
			gboolean _tmp10_;
			gchar* checkbox = NULL;
			gchar* _tmp11_;
			const gchar* _tmp12_;
			cairo_text_extents_t extents = {0};
			const gchar* _tmp13_;
			gchar* _tmp14_;
			gchar* _tmp15_;
			cairo_text_extents_t _tmp16_ = {0};
			gdouble text_x = 0.0;
			cairo_text_extents_t _tmp17_;
			ObjectsItem* _tmp18_;
			gboolean _tmp19_;
			gboolean _tmp20_;
			ObjectsItem* _tmp34_;
			gboolean _tmp35_;
			gboolean _tmp36_;
			ObjectsItem* _tmp37_;
			const gchar* _tmp38_;
			const gchar* _tmp39_;
			ObjectsItem* _tmp40_;
			gint _tmp41_;
			gint _tmp42_;
			ObjectsItem* _tmp65_;
			gboolean _tmp66_;
			gboolean _tmp67_;
			ObjectsItem* _tmp76_;
			GeeArrayList* _tmp77_;
			GeeArrayList* _tmp78_;
			gint _tmp79_;
			gint _tmp80_;
			_item_index = _item_index + 1;
			_tmp3_ = _item_index;
			_tmp4_ = _item_size;
			if (!(_tmp3_ < _tmp4_)) {
				break;
			}
			_tmp5_ = _item_list;
			_tmp6_ = gee_abstract_list_get ((GeeAbstractList*) _tmp5_, _item_index);
			item = (ObjectsItem*) _tmp6_;
			if (y > (SERVICES_EXPORT_SERVICE_PAGE_HEIGHT - SERVICES_EXPORT_SERVICE_MARGIN)) {
				cairo_surface_show_page (surface);
				y = SERVICES_EXPORT_SERVICE_MARGIN;
			}
			_tmp8_ = item;
			_tmp9_ = objects_item_get_checked (_tmp8_);
			_tmp10_ = _tmp9_;
			if (_tmp10_) {
				_tmp7_ = "- [x]";
			} else {
				_tmp7_ = "- [ ]";
			}
			_tmp11_ = g_strdup (_tmp7_);
			checkbox = _tmp11_;
			cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
			cairo_set_font_size (cr, (gdouble) 11);
			cairo_set_source_rgb (cr, 0.3, 0.3, 0.3);
			cairo_move_to (cr, x, y);
			_tmp12_ = checkbox;
			cairo_show_text (cr, _tmp12_);
			_tmp13_ = checkbox;
			_tmp14_ = g_strconcat (_tmp13_, " ", NULL);
			_tmp15_ = _tmp14_;
			cairo_text_extents (cr, _tmp15_, &_tmp16_);
			extents = _tmp16_;
			_g_free0 (_tmp15_);
			_tmp17_ = extents;
			text_x = x + _tmp17_.x_advance;
			_tmp18_ = item;
			_tmp19_ = objects_item_get_has_due (_tmp18_);
			_tmp20_ = _tmp19_;
			if (_tmp20_) {
				gchar* date_str = NULL;
				ObjectsItem* _tmp21_;
				ObjectsDueDate* _tmp22_;
				ObjectsDueDate* _tmp23_;
				GDateTime* _tmp24_;
				GDateTime* _tmp25_;
				gchar* _tmp26_;
				gchar* _tmp27_;
				gchar* _tmp28_;
				gchar* _tmp29_;
				const gchar* _tmp30_;
				cairo_text_extents_t date_extents = {0};
				const gchar* _tmp31_;
				cairo_text_extents_t _tmp32_ = {0};
				cairo_text_extents_t _tmp33_;
				_tmp21_ = item;
				_tmp22_ = objects_item_get_due (_tmp21_);
				_tmp23_ = _tmp22_;
				_tmp24_ = objects_due_date_get_datetime (_tmp23_);
				_tmp25_ = _tmp24_;
				_tmp26_ = utils_datetime_get_relative_date_from_date (_tmp25_);
				_tmp27_ = _tmp26_;
				_tmp28_ = g_strdup_printf ("[%s] ", _tmp27_);
				_tmp29_ = _tmp28_;
				_g_free0 (_tmp27_);
				date_str = _tmp29_;
				cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
				cairo_set_font_size (cr, (gdouble) 9);
				cairo_move_to (cr, text_x, y);
				_tmp30_ = date_str;
				cairo_show_text (cr, _tmp30_);
				_tmp31_ = date_str;
				cairo_text_extents (cr, _tmp31_, &_tmp32_);
				date_extents = _tmp32_;
				_tmp33_ = date_extents;
				text_x += _tmp33_.x_advance;
				_g_free0 (date_str);
			}
			cairo_set_font_size (cr, (gdouble) 11);
			_tmp34_ = item;
			_tmp35_ = objects_item_get_checked (_tmp34_);
			_tmp36_ = _tmp35_;
			if (_tmp36_) {
				cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
			} else {
				cairo_set_source_rgb (cr, (gdouble) 0, (gdouble) 0, (gdouble) 0);
			}
			cairo_move_to (cr, text_x, y);
			_tmp37_ = item;
			_tmp38_ = objects_item_get_content (_tmp37_);
			_tmp39_ = _tmp38_;
			cairo_show_text (cr, _tmp39_);
			_tmp40_ = item;
			_tmp41_ = objects_item_get_priority (_tmp40_);
			_tmp42_ = _tmp41_;
			if (_tmp42_ != CONSTANTS_PRIORITY_4) {
				gchar* priority_str = NULL;
				ObjectsItem* _tmp43_;
				gint _tmp44_;
				gint _tmp45_;
				gchar* _tmp46_;
				cairo_text_extents_t content_extents = {0};
				ObjectsItem* _tmp47_;
				const gchar* _tmp48_;
				const gchar* _tmp49_;
				cairo_text_extents_t _tmp50_ = {0};
				ObjectsItem* _tmp51_;
				gint _tmp52_;
				gint _tmp53_;
				cairo_text_extents_t _tmp54_;
				const gchar* _tmp55_;
				cairo_text_extents_t priority_extents = {0};
				const gchar* _tmp56_;
				cairo_text_extents_t _tmp57_ = {0};
				cairo_text_extents_t _tmp58_;
				cairo_text_extents_t _tmp59_;
				_tmp43_ = item;
				_tmp44_ = objects_item_get_priority (_tmp43_);
				_tmp45_ = _tmp44_;
				_tmp46_ = g_strdup_printf (" (P%d)", (4 - _tmp45_) + 1);
				priority_str = _tmp46_;
				_tmp47_ = item;
				_tmp48_ = objects_item_get_content (_tmp47_);
				_tmp49_ = _tmp48_;
				cairo_text_extents (cr, _tmp49_, &_tmp50_);
				content_extents = _tmp50_;
				cairo_set_font_size (cr, (gdouble) 9);
				_tmp51_ = item;
				_tmp52_ = objects_item_get_priority (_tmp51_);
				_tmp53_ = _tmp52_;
				services_export_service_set_priority_color (self, cr, _tmp53_);
				_tmp54_ = content_extents;
				cairo_move_to (cr, text_x + _tmp54_.x_advance, y);
				_tmp55_ = priority_str;
				cairo_show_text (cr, _tmp55_);
				_tmp56_ = priority_str;
				cairo_text_extents (cr, _tmp56_, &_tmp57_);
				priority_extents = _tmp57_;
				_tmp58_ = content_extents;
				_tmp59_ = priority_extents;
				text_x += _tmp58_.x_advance + _tmp59_.x_advance;
				_g_free0 (priority_str);
			} else {
				cairo_text_extents_t content_extents = {0};
				ObjectsItem* _tmp60_;
				const gchar* _tmp61_;
				const gchar* _tmp62_;
				cairo_text_extents_t _tmp63_ = {0};
				cairo_text_extents_t _tmp64_;
				_tmp60_ = item;
				_tmp61_ = objects_item_get_content (_tmp60_);
				_tmp62_ = _tmp61_;
				cairo_text_extents (cr, _tmp62_, &_tmp63_);
				content_extents = _tmp63_;
				_tmp64_ = content_extents;
				text_x += _tmp64_.x_advance;
			}
			_tmp65_ = item;
			_tmp66_ = objects_item_get_has_deadline (_tmp65_);
			_tmp67_ = _tmp66_;
			if (_tmp67_) {
				gchar* deadline_str = NULL;
				ObjectsItem* _tmp68_;
				GDateTime* _tmp69_;
				GDateTime* _tmp70_;
				gchar* _tmp71_;
				gchar* _tmp72_;
				gchar* _tmp73_;
				gchar* _tmp74_;
				const gchar* _tmp75_;
				_tmp68_ = item;
				_tmp69_ = objects_item_get_deadline_datetime (_tmp68_);
				_tmp70_ = _tmp69_;
				_tmp71_ = utils_datetime_get_relative_time_from_date (_tmp70_);
				_tmp72_ = _tmp71_;
				_tmp73_ = g_strdup_printf (" · %s %s", _ ("Deadline:"), _tmp72_);
				_tmp74_ = _tmp73_;
				_g_free0 (_tmp72_);
				deadline_str = _tmp74_;
				cairo_set_font_size (cr, (gdouble) 9);
				cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
				cairo_move_to (cr, text_x, y);
				_tmp75_ = deadline_str;
				cairo_show_text (cr, _tmp75_);
				_g_free0 (deadline_str);
			}
			y += (gdouble) 20;
			_tmp76_ = item;
			_tmp77_ = objects_item_get_items (_tmp76_);
			_tmp78_ = _tmp77_;
			_tmp79_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp78_);
			_tmp80_ = _tmp79_;
			if (_tmp80_ > 0) {
				ObjectsItem* _tmp81_;
				GeeArrayList* _tmp82_;
				GeeArrayList* _tmp83_;
				_tmp81_ = item;
				_tmp82_ = objects_item_get_items (_tmp81_);
				_tmp83_ = _tmp82_;
				y = services_export_service_draw_items (self, cr, surface, _tmp83_, y, x + 20);
			}
			_g_free0 (checkbox);
			_g_object_unref0 (item);
		}
	}
	result = y;
	return result;
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static GeeArrayList*
services_export_service_wrap_text (ServicesExportService* self,
                                   cairo_t* cr,
                                   const gchar* text,
                                   gdouble max_width)
{
	GeeArrayList* lines = NULL;
	GeeArrayList* _tmp0_;
	gchar** paragraphs = NULL;
	gchar** _tmp1_;
	gchar** _tmp2_;
	gint paragraphs_length1;
	gint _paragraphs_size_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (cr != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	lines = _tmp0_;
	_tmp2_ = _tmp1_ = g_strsplit (text, "\n", 0);
	paragraphs = _tmp2_;
	paragraphs_length1 = _vala_array_length (_tmp1_);
	_paragraphs_size_ = paragraphs_length1;
	_tmp3_ = paragraphs;
	_tmp3__length1 = paragraphs_length1;
	{
		gchar** paragraph_collection = NULL;
		gint paragraph_collection_length1 = 0;
		gint _paragraph_collection_size_ = 0;
		gint paragraph_it = 0;
		paragraph_collection = _tmp3_;
		paragraph_collection_length1 = _tmp3__length1;
		for (paragraph_it = 0; paragraph_it < paragraph_collection_length1; paragraph_it = paragraph_it + 1) {
			gchar* _tmp4_;
			gchar* paragraph = NULL;
			_tmp4_ = g_strdup (paragraph_collection[paragraph_it]);
			paragraph = _tmp4_;
			{
				const gchar* _tmp5_;
				gchar* _tmp6_;
				gchar* _tmp7_;
				gboolean _tmp8_;
				gchar** words = NULL;
				const gchar* _tmp10_;
				gchar** _tmp11_;
				gchar** _tmp12_;
				gint words_length1;
				gint _words_size_;
				gchar* current_line = NULL;
				gchar* _tmp13_;
				gchar** _tmp14_;
				gint _tmp14__length1;
				const gchar* _tmp37_;
				_tmp5_ = paragraph;
				_tmp6_ = string_strip (_tmp5_);
				_tmp7_ = _tmp6_;
				_tmp8_ = g_strcmp0 (_tmp7_, "") == 0;
				_g_free0 (_tmp7_);
				if (_tmp8_) {
					GeeArrayList* _tmp9_;
					_tmp9_ = lines;
					gee_abstract_collection_add ((GeeAbstractCollection*) _tmp9_, "");
					_g_free0 (paragraph);
					continue;
				}
				_tmp10_ = paragraph;
				_tmp12_ = _tmp11_ = g_strsplit (_tmp10_, " ", 0);
				words = _tmp12_;
				words_length1 = _vala_array_length (_tmp11_);
				_words_size_ = words_length1;
				_tmp13_ = g_strdup ("");
				current_line = _tmp13_;
				_tmp14_ = words;
				_tmp14__length1 = words_length1;
				{
					gchar** word_collection = NULL;
					gint word_collection_length1 = 0;
					gint _word_collection_size_ = 0;
					gint word_it = 0;
					word_collection = _tmp14_;
					word_collection_length1 = _tmp14__length1;
					for (word_it = 0; word_it < word_collection_length1; word_it = word_it + 1) {
						gchar* _tmp15_;
						gchar* word = NULL;
						_tmp15_ = g_strdup (word_collection[word_it]);
						word = _tmp15_;
						{
							gchar* _tmp16_ = NULL;
							const gchar* _tmp17_;
							gchar* test_line = NULL;
							gchar* _tmp25_;
							cairo_text_extents_t extents = {0};
							const gchar* _tmp26_;
							cairo_text_extents_t _tmp27_ = {0};
							gboolean _tmp28_ = FALSE;
							cairo_text_extents_t _tmp29_;
							_tmp17_ = current_line;
							if (g_strcmp0 (_tmp17_, "") == 0) {
								const gchar* _tmp18_;
								gchar* _tmp19_;
								_tmp18_ = word;
								_tmp19_ = g_strdup (_tmp18_);
								_g_free0 (_tmp16_);
								_tmp16_ = _tmp19_;
							} else {
								const gchar* _tmp20_;
								gchar* _tmp21_;
								gchar* _tmp22_;
								const gchar* _tmp23_;
								gchar* _tmp24_;
								_tmp20_ = current_line;
								_tmp21_ = g_strconcat (_tmp20_, " ", NULL);
								_tmp22_ = _tmp21_;
								_tmp23_ = word;
								_tmp24_ = g_strconcat (_tmp22_, _tmp23_, NULL);
								_g_free0 (_tmp16_);
								_tmp16_ = _tmp24_;
								_g_free0 (_tmp22_);
							}
							_tmp25_ = _tmp16_;
							_tmp16_ = NULL;
							test_line = _tmp25_;
							_tmp26_ = test_line;
							cairo_text_extents (cr, _tmp26_, &_tmp27_);
							extents = _tmp27_;
							_tmp29_ = extents;
							if (_tmp29_.width > max_width) {
								const gchar* _tmp30_;
								_tmp30_ = current_line;
								_tmp28_ = g_strcmp0 (_tmp30_, "") != 0;
							} else {
								_tmp28_ = FALSE;
							}
							if (_tmp28_) {
								GeeArrayList* _tmp31_;
								const gchar* _tmp32_;
								const gchar* _tmp33_;
								gchar* _tmp34_;
								_tmp31_ = lines;
								_tmp32_ = current_line;
								gee_abstract_collection_add ((GeeAbstractCollection*) _tmp31_, _tmp32_);
								_tmp33_ = word;
								_tmp34_ = g_strdup (_tmp33_);
								_g_free0 (current_line);
								current_line = _tmp34_;
							} else {
								const gchar* _tmp35_;
								gchar* _tmp36_;
								_tmp35_ = test_line;
								_tmp36_ = g_strdup (_tmp35_);
								_g_free0 (current_line);
								current_line = _tmp36_;
							}
							_g_free0 (test_line);
							_g_free0 (_tmp16_);
							_g_free0 (word);
						}
					}
				}
				_tmp37_ = current_line;
				if (g_strcmp0 (_tmp37_, "") != 0) {
					GeeArrayList* _tmp38_;
					const gchar* _tmp39_;
					_tmp38_ = lines;
					_tmp39_ = current_line;
					gee_abstract_collection_add ((GeeAbstractCollection*) _tmp38_, _tmp39_);
				}
				_g_free0 (current_line);
				words = (_vala_array_free (words, words_length1, (GDestroyNotify) g_free), NULL);
				_g_free0 (paragraph);
			}
		}
	}
	result = lines;
	paragraphs = (_vala_array_free (paragraphs, paragraphs_length1, (GDestroyNotify) g_free), NULL);
	return result;
}

static void
services_export_service_set_priority_color (ServicesExportService* self,
                                            cairo_t* cr,
                                            gint priority)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (cr != NULL);
	switch (priority) {
		case CONSTANTS_PRIORITY_1:
		{
			cairo_set_source_rgb (cr, 1.0, 0.44, 0.40);
			break;
		}
		case CONSTANTS_PRIORITY_2:
		{
			cairo_set_source_rgb (cr, 1.0, 0.60, 0.08);
			break;
		}
		case CONSTANTS_PRIORITY_3:
		{
			cairo_set_source_rgb (cr, 0.32, 0.59, 1.0);
			break;
		}
		default:
		{
			cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
			break;
		}
	}
}

ServicesExportService*
services_export_service_construct (GType object_type)
{
	ServicesExportService * self = NULL;
	self = (ServicesExportService*) g_object_new (object_type, NULL);
	return self;
}

ServicesExportService*
services_export_service_new (void)
{
	return services_export_service_construct (SERVICES_TYPE_EXPORT_SERVICE);
}

static void
services_export_service_class_init (ServicesExportServiceClass * klass,
                                    gpointer klass_data)
{
	services_export_service_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = services_export_service_finalize;
}

static void
services_export_service_instance_init (ServicesExportService * self,
                                       gpointer klass)
{
}

static void
services_export_service_finalize (GObject * obj)
{
	ServicesExportService * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_EXPORT_SERVICE, ServicesExportService);
	G_OBJECT_CLASS (services_export_service_parent_class)->finalize (obj);
}

static GType
services_export_service_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesExportServiceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_export_service_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesExportService), 0, (GInstanceInitFunc) services_export_service_instance_init, NULL };
	GType services_export_service_type_id;
	services_export_service_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesExportService", &g_define_type_info, 0);
	return services_export_service_type_id;
}

GType
services_export_service_get_type (void)
{
	static volatile gsize services_export_service_type_id__once = 0;
	if (g_once_init_enter (&services_export_service_type_id__once)) {
		GType services_export_service_type_id;
		services_export_service_type_id = services_export_service_get_type_once ();
		g_once_init_leave (&services_export_service_type_id__once, services_export_service_type_id);
	}
	return services_export_service_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize 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,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

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

