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

/*
 * This file is part of LaTeXila.
 *
 * Copyright © 2010-2011 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 <gio/gio.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtksourceview/gtksourceview.h>
#include <gobject/gvaluecollector.h>


#define TYPE_PROJECT (project_get_type ())
typedef struct _Project Project;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_PROJECTS (projects_get_type ())
#define PROJECTS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PROJECTS, Projects))
#define PROJECTS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PROJECTS, ProjectsClass))
#define IS_PROJECTS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PROJECTS))
#define IS_PROJECTS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PROJECTS))
#define PROJECTS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PROJECTS, ProjectsClass))

typedef struct _Projects Projects;
typedef struct _ProjectsClass ProjectsClass;
typedef struct _ProjectsPrivate ProjectsPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_markup_parse_context_free0(var) ((var == NULL) ? NULL : (var = (g_markup_parse_context_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _projects_unref0(var) ((var == NULL) ? NULL : (var = (projects_unref (var), NULL)))

#define TYPE_APPLICATION (application_get_type ())
#define APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_APPLICATION, Application))
#define APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_APPLICATION, ApplicationClass))
#define IS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_APPLICATION))
#define IS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_APPLICATION))
#define APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_APPLICATION, ApplicationClass))

typedef struct _Application Application;
typedef struct _ApplicationClass ApplicationClass;

#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 _project_free0(var) ((var == NULL) ? NULL : (var = (project_free (var), NULL)))

#define TYPE_DOCUMENT (document_get_type ())
#define DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DOCUMENT, Document))
#define DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DOCUMENT, DocumentClass))
#define IS_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DOCUMENT))
#define IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DOCUMENT))
#define DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DOCUMENT, DocumentClass))

typedef struct _Document Document;
typedef struct _DocumentClass DocumentClass;
#define __g_list_free__g_object_unref0_0(var) ((var == NULL) ? NULL : (var = (_g_list_free__g_object_unref0_ (var), NULL)))
typedef struct _ParamSpecProjects ParamSpecProjects;

struct _Project {
	GFile* directory;
	GFile* main_file;
};

struct _Projects {
	GTypeInstance parent_instance;
	volatile int ref_count;
	ProjectsPrivate * priv;
};

struct _ProjectsClass {
	GTypeClass parent_class;
	void (*finalize) (Projects *self);
};

struct _ProjectsPrivate {
	GeeLinkedList* projects;
	gboolean modified;
};

struct _ParamSpecProjects {
	GParamSpec parent_instance;
};


static gpointer projects_parent_class = NULL;
static Projects* projects_instance;
static Projects* projects_instance = NULL;

GType project_get_type (void) G_GNUC_CONST;
Project* project_dup (const Project* self);
void project_free (Project* self);
void project_copy (const Project* self, Project* dest);
void project_destroy (Project* self);
gpointer projects_ref (gpointer instance);
void projects_unref (gpointer instance);
GParamSpec* param_spec_projects (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_projects (GValue* value, gpointer v_object);
void value_take_projects (GValue* value, gpointer v_object);
gpointer value_get_projects (const GValue* value);
GType projects_get_type (void) G_GNUC_CONST;
#define PROJECTS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_PROJECTS, ProjectsPrivate))
enum  {
	PROJECTS_DUMMY_PROPERTY
};
static Projects* projects_new (void);
static Projects* projects_construct (GType object_type);
static GFile* projects_get_xml_file (Projects* self);
static void projects_parser_start (Projects* self, GMarkupParseContext* context, const gchar* name, gchar** attr_names, int attr_names_length1, gchar** attr_values, int attr_values_length1, GError** error);
static void _projects_parser_start_gmarkup_parser_start_element_func (GMarkupParseContext* context, const gchar* element_name, gchar** attribute_names, gchar** attribute_values, gpointer self, GError** error);
static void projects_update_all_documents (Projects* self);
static void projects_update_all_menus (Projects* self);
Projects* projects_get_default (void);
Project* projects_get (Projects* self, gint id);
GeeIterator* projects_iterator (Projects* self);
GType application_get_type (void) G_GNUC_CONST;
Application* application_get_default (void);
GType main_window_get_type (void) G_GNUC_CONST;
GList* application_get_windows (Application* self);
void main_window_update_config_project_sensitivity (MainWindow* self);
gboolean projects_add (Projects* self, Project* new_project, GFile** conflict_file);
static gboolean projects_conflict (Projects* self, GFile* dir1, GFile* dir2);
GType document_get_type (void) G_GNUC_CONST;
GList* application_get_documents (Application* self);
gint document_get_project_id (Document* self);
GFile* document_get_location (Document* self);
void document_set_project_id (Document* self, gint value);
static void _g_object_unref0_ (gpointer var);
static void _g_list_free__g_object_unref0_ (GList* self);
gboolean projects_change_main_file (Projects* self, gint num, GFile* new_main_file);
void projects_delete (Projects* self, gint num);
void projects_clear_all (Projects* self);
void projects_save (Projects* self);
void utils_delete_file (GFile* file);
static void projects_finalize (Projects* obj);
static gint _vala_array_length (gpointer array);


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


void project_copy (const Project* self, Project* dest) {
	dest->directory = _g_object_ref0 (self->directory);
	dest->main_file = _g_object_ref0 (self->main_file);
}


void project_destroy (Project* self) {
	_g_object_unref0 ((*self).directory);
	_g_object_unref0 ((*self).main_file);
}


Project* project_dup (const Project* self) {
	Project* dup;
	dup = g_new0 (Project, 1);
	project_copy (self, dup);
	return dup;
}


void project_free (Project* self) {
	project_destroy (self);
	g_free (self);
}


GType project_get_type (void) {
	static volatile gsize project_type_id__volatile = 0;
	if (g_once_init_enter (&project_type_id__volatile)) {
		GType project_type_id;
		project_type_id = g_boxed_type_register_static ("Project", (GBoxedCopyFunc) project_dup, (GBoxedFreeFunc) project_free);
		g_once_init_leave (&project_type_id__volatile, project_type_id);
	}
	return project_type_id__volatile;
}


static void _projects_parser_start_gmarkup_parser_start_element_func (GMarkupParseContext* context, const gchar* element_name, gchar** attribute_names, gchar** attribute_values, gpointer self, GError** error) {
	projects_parser_start (self, context, element_name, attribute_names, _vala_array_length (attribute_names), attribute_values, _vala_array_length (attribute_values), error);
}


static Projects* projects_construct (GType object_type) {
	Projects* self = NULL;
	GeeLinkedList* _tmp0_ = NULL;
	GFile* _tmp1_ = NULL;
	GFile* file;
	gboolean _tmp2_;
	gchar* contents = NULL;
	gchar* _tmp3_ = NULL;
	GMarkupParser _tmp4_ = {0};
	GMarkupParser parser;
	GMarkupParseContext* _tmp5_ = NULL;
	GMarkupParseContext* context;
	GError * _inner_error_ = NULL;
	self = (Projects*) g_type_create_instance (object_type);
	_tmp0_ = gee_linked_list_new (TYPE_PROJECT, (GBoxedCopyFunc) project_dup, project_free, NULL);
	_g_object_unref0 (self->priv->projects);
	self->priv->projects = _tmp0_;
	_tmp1_ = projects_get_xml_file (self);
	file = _tmp1_;
	_tmp2_ = g_file_query_exists (file, NULL);
	if (!_tmp2_) {
		_g_object_unref0 (file);
		return self;
	}
	g_file_load_contents (file, NULL, &_tmp3_, NULL, NULL, &_inner_error_);
	_g_free0 (contents);
	contents = _tmp3_;
	if (_inner_error_ != NULL) {
		_g_free0 (contents);
		goto __catch58_g_error;
	}
	_tmp4_.start_element = _projects_parser_start_gmarkup_parser_start_element_func;
	_tmp4_.end_element = NULL;
	_tmp4_.text = NULL;
	_tmp4_.passthrough = NULL;
	_tmp4_.error = NULL;
	parser = _tmp4_;
	_tmp5_ = g_markup_parse_context_new (&parser, 0, self, NULL);
	context = _tmp5_;
	g_markup_parse_context_parse (context, contents, (gssize) (-1), &_inner_error_);
	if (_inner_error_ != NULL) {
		_g_markup_parse_context_free0 (context);
		_g_free0 (contents);
		goto __catch58_g_error;
	}
	projects_update_all_documents (self);
	projects_update_all_menus (self);
	_g_markup_parse_context_free0 (context);
	_g_free0 (contents);
	goto __finally58;
	__catch58_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_warning ("projects.vala:58: Impossible to load the projects: %s", e->message);
		_g_error_free0 (e);
	}
	__finally58:
	if (_inner_error_ != NULL) {
		_g_object_unref0 (file);
		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;
	}
	_g_object_unref0 (file);
	return self;
}


static Projects* projects_new (void) {
	return projects_construct (TYPE_PROJECTS);
}


static gpointer _projects_ref0 (gpointer self) {
	return self ? projects_ref (self) : NULL;
}


Projects* projects_get_default (void) {
	Projects* result = NULL;
	Projects* _tmp1_;
	if (projects_instance == NULL) {
		Projects* _tmp0_ = NULL;
		_tmp0_ = projects_new ();
		_projects_unref0 (projects_instance);
		projects_instance = _tmp0_;
	}
	_tmp1_ = _projects_ref0 (projects_instance);
	result = _tmp1_;
	return result;
}


Project* projects_get (Projects* self, gint id) {
	Project* result = NULL;
	gboolean _tmp0_ = FALSE;
	gpointer _tmp2_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	if (id >= 0) {
		gint _tmp1_;
		_tmp1_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
		_tmp0_ = id < _tmp1_;
	} else {
		_tmp0_ = FALSE;
	}
	g_return_val_if_fail (_tmp0_, NULL);
	_tmp2_ = gee_abstract_list_get ((GeeAbstractList*) self->priv->projects, id);
	result = (Project*) _tmp2_;
	return result;
}


GeeIterator* projects_iterator (Projects* self) {
	GeeIterator* result = NULL;
	GeeIterator* _tmp0_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = gee_abstract_collection_iterator ((GeeAbstractCollection*) self->priv->projects);
	result = GEE_ITERATOR (_tmp0_);
	return result;
}


static void projects_update_all_menus (Projects* self) {
	Application* _tmp0_ = NULL;
	Application* _tmp1_;
	GList* _tmp2_ = NULL;
	GList* _tmp3_;
	g_return_if_fail (self != NULL);
	_tmp0_ = application_get_default ();
	_tmp1_ = _tmp0_;
	_tmp2_ = application_get_windows (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_object_unref0 (_tmp1_);
	{
		GList* window_collection;
		GList* window_it;
		window_collection = _tmp3_;
		for (window_it = window_collection; window_it != NULL; window_it = window_it->next) {
			MainWindow* _tmp4_;
			MainWindow* window;
			_tmp4_ = _g_object_ref0 ((MainWindow*) window_it->data);
			window = _tmp4_;
			{
				main_window_update_config_project_sensitivity (window);
				_g_object_unref0 (window);
			}
		}
	}
}


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


static void _g_list_free__g_object_unref0_ (GList* self) {
	g_list_foreach (self, (GFunc) _g_object_unref0_, NULL);
	g_list_free (self);
}


gboolean projects_add (Projects* self, Project* new_project, GFile** conflict_file) {
	GFile* _conflict_file = NULL;
	gboolean result = FALSE;
	Application* _tmp9_ = NULL;
	Application* _tmp10_;
	GList* _tmp11_ = NULL;
	GList* _tmp12_;
	GList* docs;
	g_return_val_if_fail (self != NULL, FALSE);
	{
		GeeLinkedList* _tmp0_;
		GeeLinkedList* _project_list;
		gint _tmp1_;
		gint _project_size;
		gint _project_index;
		_tmp0_ = _g_object_ref0 (self->priv->projects);
		_project_list = _tmp0_;
		_tmp1_ = gee_collection_get_size ((GeeCollection*) _project_list);
		_project_size = _tmp1_;
		_project_index = -1;
		while (TRUE) {
			gpointer _tmp2_ = NULL;
			Project* _tmp3_;
			Project _tmp4_;
			Project _tmp5_ = {0};
			Project _tmp6_;
			Project project;
			gboolean _tmp7_;
			_project_index = _project_index + 1;
			if (!(_project_index < _project_size)) {
				break;
			}
			_tmp2_ = gee_abstract_list_get ((GeeAbstractList*) _project_list, _project_index);
			_tmp3_ = (Project*) _tmp2_;
			project_copy (_tmp3_, &_tmp5_);
			_tmp4_ = _tmp5_;
			_tmp6_ = _tmp4_;
			_project_free0 (_tmp3_);
			project = _tmp6_;
			_tmp7_ = projects_conflict (self, project.directory, (*new_project).directory);
			if (_tmp7_) {
				GFile* _tmp8_;
				_tmp8_ = _g_object_ref0 (project.directory);
				_g_object_unref0 (_conflict_file);
				_conflict_file = _tmp8_;
				result = FALSE;
				project_destroy (&project);
				_g_object_unref0 (_project_list);
				if (conflict_file) {
					*conflict_file = _conflict_file;
				} else {
					_g_object_unref0 (_conflict_file);
				}
				return result;
			}
			project_destroy (&project);
		}
		_g_object_unref0 (_project_list);
	}
	gee_abstract_collection_add ((GeeAbstractCollection*) self->priv->projects, new_project);
	self->priv->modified = TRUE;
	_tmp9_ = application_get_default ();
	_tmp10_ = _tmp9_;
	_tmp11_ = application_get_documents (_tmp10_);
	_tmp12_ = _tmp11_;
	_g_object_unref0 (_tmp10_);
	docs = _tmp12_;
	{
		GList* doc_collection;
		GList* doc_it;
		doc_collection = docs;
		for (doc_it = doc_collection; doc_it != NULL; doc_it = doc_it->next) {
			Document* _tmp13_;
			Document* doc;
			_tmp13_ = _g_object_ref0 ((Document*) doc_it->data);
			doc = _tmp13_;
			{
				gint _tmp14_;
				GFile* _tmp15_ = NULL;
				gboolean _tmp16_;
				_tmp14_ = document_get_project_id (doc);
				if (_tmp14_ != (-1)) {
					_g_object_unref0 (doc);
					continue;
				}
				_tmp15_ = document_get_location (doc);
				_tmp16_ = g_file_has_prefix (_tmp15_, (*new_project).directory);
				if (_tmp16_) {
					gint _tmp17_;
					_tmp17_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
					document_set_project_id (doc, _tmp17_ - 1);
				}
				_g_object_unref0 (doc);
			}
		}
	}
	projects_update_all_menus (self);
	result = TRUE;
	__g_list_free__g_object_unref0_0 (docs);
	if (conflict_file) {
		*conflict_file = _conflict_file;
	} else {
		_g_object_unref0 (_conflict_file);
	}
	return result;
}


gboolean projects_change_main_file (Projects* self, gint num, GFile* new_main_file) {
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	gpointer _tmp2_ = NULL;
	Project* _tmp3_;
	Project _tmp4_;
	Project _tmp5_ = {0};
	Project _tmp6_;
	Project project;
	gboolean _tmp7_;
	gboolean _tmp8_;
	GFile* _tmp9_;
	Application* _tmp10_ = NULL;
	Application* _tmp11_;
	GList* _tmp12_ = NULL;
	GList* _tmp13_;
	GList* docs;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (new_main_file != NULL, FALSE);
	if (num >= 0) {
		gint _tmp1_;
		_tmp1_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
		_tmp0_ = num < _tmp1_;
	} else {
		_tmp0_ = FALSE;
	}
	g_return_val_if_fail (_tmp0_, FALSE);
	_tmp2_ = gee_abstract_list_get ((GeeAbstractList*) self->priv->projects, num);
	_tmp3_ = (Project*) _tmp2_;
	project_copy (_tmp3_, &_tmp5_);
	_tmp4_ = _tmp5_;
	_tmp6_ = _tmp4_;
	_project_free0 (_tmp3_);
	project = _tmp6_;
	_tmp7_ = g_file_equal (new_main_file, project.main_file);
	if (_tmp7_) {
		result = FALSE;
		project_destroy (&project);
		return result;
	}
	_tmp8_ = g_file_has_prefix (new_main_file, project.directory);
	g_return_if_fail (_tmp8_);
	_tmp9_ = _g_object_ref0 (new_main_file);
	_g_object_unref0 (project.main_file);
	project.main_file = _tmp9_;
	gee_abstract_list_set ((GeeAbstractList*) self->priv->projects, num, &project);
	self->priv->modified = TRUE;
	_tmp10_ = application_get_default ();
	_tmp11_ = _tmp10_;
	_tmp12_ = application_get_documents (_tmp11_);
	_tmp13_ = _tmp12_;
	_g_object_unref0 (_tmp11_);
	docs = _tmp13_;
	{
		GList* doc_collection;
		GList* doc_it;
		doc_collection = docs;
		for (doc_it = doc_collection; doc_it != NULL; doc_it = doc_it->next) {
			Document* _tmp14_;
			Document* doc;
			_tmp14_ = _g_object_ref0 ((Document*) doc_it->data);
			doc = _tmp14_;
			{
				gint _tmp15_;
				_tmp15_ = document_get_project_id (doc);
				if (_tmp15_ == num) {
					document_set_project_id (doc, num);
				}
				_g_object_unref0 (doc);
			}
		}
	}
	result = TRUE;
	__g_list_free__g_object_unref0_0 (docs);
	project_destroy (&project);
	return result;
}


void projects_delete (Projects* self, gint num) {
	gboolean _tmp0_ = FALSE;
	gpointer _tmp2_ = NULL;
	Project* _tmp3_;
	Application* _tmp4_ = NULL;
	Application* _tmp5_;
	GList* _tmp6_ = NULL;
	GList* _tmp7_;
	GList* docs;
	g_return_if_fail (self != NULL);
	if (num >= 0) {
		gint _tmp1_;
		_tmp1_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
		_tmp0_ = num < _tmp1_;
	} else {
		_tmp0_ = FALSE;
	}
	g_return_if_fail (_tmp0_);
	_tmp2_ = gee_abstract_list_remove_at ((GeeAbstractList*) self->priv->projects, num);
	_tmp3_ = (Project*) _tmp2_;
	_project_free0 (_tmp3_);
	self->priv->modified = TRUE;
	_tmp4_ = application_get_default ();
	_tmp5_ = _tmp4_;
	_tmp6_ = application_get_documents (_tmp5_);
	_tmp7_ = _tmp6_;
	_g_object_unref0 (_tmp5_);
	docs = _tmp7_;
	{
		GList* doc_collection;
		GList* doc_it;
		doc_collection = docs;
		for (doc_it = doc_collection; doc_it != NULL; doc_it = doc_it->next) {
			Document* _tmp8_;
			Document* doc;
			_tmp8_ = _g_object_ref0 ((Document*) doc_it->data);
			doc = _tmp8_;
			{
				gint _tmp9_;
				_tmp9_ = document_get_project_id (doc);
				if (_tmp9_ == num) {
					document_set_project_id (doc, -1);
				} else {
					gint _tmp10_;
					_tmp10_ = document_get_project_id (doc);
					if (_tmp10_ > num) {
						gint _tmp11_;
						gint _tmp12_;
						_tmp11_ = document_get_project_id (doc);
						_tmp12_ = _tmp11_;
						document_set_project_id (doc, _tmp12_ - 1);
					}
				}
				_g_object_unref0 (doc);
			}
		}
	}
	projects_update_all_menus (self);
	__g_list_free__g_object_unref0_0 (docs);
}


void projects_clear_all (Projects* self) {
	g_return_if_fail (self != NULL);
	gee_abstract_collection_clear ((GeeAbstractCollection*) self->priv->projects);
	self->priv->modified = TRUE;
	projects_update_all_documents (self);
	projects_update_all_menus (self);
}


static void projects_update_all_documents (Projects* self) {
	Application* _tmp0_ = NULL;
	Application* _tmp1_;
	GList* _tmp2_ = NULL;
	GList* _tmp3_;
	GList* docs;
	g_return_if_fail (self != NULL);
	_tmp0_ = application_get_default ();
	_tmp1_ = _tmp0_;
	_tmp2_ = application_get_documents (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_object_unref0 (_tmp1_);
	docs = _tmp3_;
	{
		GList* doc_collection;
		GList* doc_it;
		doc_collection = docs;
		for (doc_it = doc_collection; doc_it != NULL; doc_it = doc_it->next) {
			Document* _tmp4_;
			Document* doc;
			_tmp4_ = _g_object_ref0 ((Document*) doc_it->data);
			doc = _tmp4_;
			{
				document_set_project_id (doc, -1);
				{
					gint i;
					i = 0;
					{
						gboolean _tmp5_;
						_tmp5_ = TRUE;
						while (TRUE) {
							gint _tmp6_;
							GFile* _tmp7_ = NULL;
							gpointer _tmp8_ = NULL;
							Project* _tmp9_;
							gboolean _tmp10_;
							gboolean _tmp11_;
							if (!_tmp5_) {
								i++;
							}
							_tmp5_ = FALSE;
							_tmp6_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
							if (!(i < _tmp6_)) {
								break;
							}
							_tmp7_ = document_get_location (doc);
							_tmp8_ = gee_abstract_list_get ((GeeAbstractList*) self->priv->projects, i);
							_tmp9_ = (Project*) _tmp8_;
							_tmp10_ = g_file_has_prefix (_tmp7_, (*_tmp9_).directory);
							_tmp11_ = _tmp10_;
							_project_free0 (_tmp9_);
							if (_tmp11_) {
								document_set_project_id (doc, i);
								break;
							}
						}
					}
				}
				_g_object_unref0 (doc);
			}
		}
	}
	__g_list_free__g_object_unref0_0 (docs);
}


static void projects_parser_start (Projects* self, GMarkupParseContext* context, const gchar* name, gchar** attr_names, int attr_names_length1, gchar** attr_values, int attr_values_length1, GError** error) {
	const gchar* _tmp0_;
	GQuark _tmp1_;
	static GQuark _tmp1__label0 = 0;
	static GQuark _tmp1__label1 = 0;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (name != NULL);
	_tmp0_ = name;
	_tmp1_ = (NULL == _tmp0_) ? 0 : g_quark_from_string (_tmp0_);
	if (_tmp1_ == ((0 != _tmp1__label0) ? _tmp1__label0 : (_tmp1__label0 = g_quark_from_static_string ("projects")))) {
		switch (0) {
			default:
			{
				return;
			}
		}
	} else if (_tmp1_ == ((0 != _tmp1__label1) ? _tmp1__label1 : (_tmp1__label1 = g_quark_from_static_string ("project")))) {
		switch (0) {
			default:
			{
				Project project = {0};
				memset (&project, 0, sizeof (Project));
				{
					gint i;
					i = 0;
					{
						gboolean _tmp2_;
						_tmp2_ = TRUE;
						while (TRUE) {
							const gchar* _tmp3_;
							GQuark _tmp4_;
							static GQuark _tmp4__label0 = 0;
							static GQuark _tmp4__label1 = 0;
							if (!_tmp2_) {
								i++;
							}
							_tmp2_ = FALSE;
							if (!(i < attr_names_length1)) {
								break;
							}
							_tmp3_ = attr_names[i];
							_tmp4_ = (NULL == _tmp3_) ? 0 : g_quark_from_string (_tmp3_);
							if (_tmp4_ == ((0 != _tmp4__label0) ? _tmp4__label0 : (_tmp4__label0 = g_quark_from_static_string ("directory")))) {
								switch (0) {
									default:
									{
										GFile* _tmp5_ = NULL;
										_tmp5_ = g_file_new_for_uri (attr_values[i]);
										_g_object_unref0 (project.directory);
										project.directory = _tmp5_;
										break;
									}
								}
							} else if (_tmp4_ == ((0 != _tmp4__label1) ? _tmp4__label1 : (_tmp4__label1 = g_quark_from_static_string ("main_file")))) {
								switch (0) {
									default:
									{
										GFile* _tmp6_ = NULL;
										_tmp6_ = g_file_new_for_uri (attr_values[i]);
										_g_object_unref0 (project.main_file);
										project.main_file = _tmp6_;
										break;
									}
								}
							} else {
								switch (0) {
									default:
									{
										gchar* _tmp7_;
										gchar* _tmp8_;
										gchar* _tmp9_;
										gchar* _tmp10_;
										GError* _tmp11_ = NULL;
										GError* _tmp12_;
										_tmp7_ = g_strconcat ("unknown attribute \"", attr_names[i], NULL);
										_tmp8_ = _tmp7_;
										_tmp9_ = g_strconcat (_tmp8_, "\"", NULL);
										_tmp10_ = _tmp9_;
										_tmp11_ = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, _tmp10_);
										_tmp12_ = _tmp11_;
										_g_free0 (_tmp10_);
										_g_free0 (_tmp8_);
										_inner_error_ = _tmp12_;
										if (_inner_error_->domain == G_MARKUP_ERROR) {
											g_propagate_error (error, _inner_error_);
											project_destroy (&project);
											return;
										} else {
											project_destroy (&project);
											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;
										}
									}
								}
							}
						}
					}
				}
				gee_abstract_collection_add ((GeeAbstractCollection*) self->priv->projects, &project);
				project_destroy (&project);
				break;
			}
		}
	} else {
		switch (0) {
			default:
			{
				gchar* _tmp13_;
				gchar* _tmp14_;
				gchar* _tmp15_;
				gchar* _tmp16_;
				GError* _tmp17_ = NULL;
				GError* _tmp18_;
				_tmp13_ = g_strconcat ("unknown element \"", name, NULL);
				_tmp14_ = _tmp13_;
				_tmp15_ = g_strconcat (_tmp14_, "\"", NULL);
				_tmp16_ = _tmp15_;
				_tmp17_ = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, _tmp16_);
				_tmp18_ = _tmp17_;
				_g_free0 (_tmp16_);
				_g_free0 (_tmp14_);
				_inner_error_ = _tmp18_;
				if (_inner_error_->domain == G_MARKUP_ERROR) {
					g_propagate_error (error, _inner_error_);
					return;
				} else {
					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 GFile* projects_get_xml_file (Projects* self) {
	GFile* result = NULL;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	gchar* path;
	GFile* _tmp2_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_get_user_data_dir ();
	_tmp1_ = g_build_filename (_tmp0_, "latexila", "projects.xml", NULL, NULL);
	path = _tmp1_;
	_tmp2_ = g_file_new_for_path (path);
	result = _tmp2_;
	_g_free0 (path);
	return result;
}


void projects_save (Projects* self) {
	GFile* _tmp0_ = NULL;
	GFile* file;
	gint _tmp1_;
	gchar* _tmp2_;
	gchar* content;
	gchar* _tmp17_;
	GFile* _tmp18_ = NULL;
	GFile* parent;
	gboolean _tmp19_ = FALSE;
	gint _tmp21_;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	if (!self->priv->modified) {
		return;
	}
	_tmp0_ = projects_get_xml_file (self);
	file = _tmp0_;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) self->priv->projects);
	if (_tmp1_ == 0) {
		utils_delete_file (file);
		_g_object_unref0 (file);
		return;
	}
	_tmp2_ = g_strdup ("<projects>\n");
	content = _tmp2_;
	{
		GeeLinkedList* _tmp3_;
		GeeLinkedList* _project_list;
		gint _tmp4_;
		gint _project_size;
		gint _project_index;
		_tmp3_ = _g_object_ref0 (self->priv->projects);
		_project_list = _tmp3_;
		_tmp4_ = gee_collection_get_size ((GeeCollection*) _project_list);
		_project_size = _tmp4_;
		_project_index = -1;
		while (TRUE) {
			gpointer _tmp5_ = NULL;
			Project* _tmp6_;
			Project _tmp7_;
			Project _tmp8_ = {0};
			Project _tmp9_;
			Project project;
			gchar* _tmp10_ = NULL;
			gchar* _tmp11_;
			gchar* _tmp12_ = NULL;
			gchar* _tmp13_;
			gchar* _tmp14_ = NULL;
			gchar* _tmp15_;
			gchar* _tmp16_;
			_project_index = _project_index + 1;
			if (!(_project_index < _project_size)) {
				break;
			}
			_tmp5_ = gee_abstract_list_get ((GeeAbstractList*) _project_list, _project_index);
			_tmp6_ = (Project*) _tmp5_;
			project_copy (_tmp6_, &_tmp8_);
			_tmp7_ = _tmp8_;
			_tmp9_ = _tmp7_;
			_project_free0 (_tmp6_);
			project = _tmp9_;
			_tmp10_ = g_file_get_uri (project.directory);
			_tmp11_ = _tmp10_;
			_tmp12_ = g_file_get_uri (project.main_file);
			_tmp13_ = _tmp12_;
			_tmp14_ = g_strdup_printf ("  <project directory=\"%s\" main_file=\"%s\" />\n", _tmp11_, _tmp13_);
			_tmp15_ = _tmp14_;
			_tmp16_ = g_strconcat (content, _tmp15_, NULL);
			_g_free0 (content);
			content = _tmp16_;
			_g_free0 (_tmp15_);
			_g_free0 (_tmp13_);
			_g_free0 (_tmp11_);
			project_destroy (&project);
		}
		_g_object_unref0 (_project_list);
	}
	_tmp17_ = g_strconcat (content, "</projects>\n", NULL);
	_g_free0 (content);
	content = _tmp17_;
	_tmp18_ = g_file_get_parent (file);
	parent = _tmp18_;
	if (parent != NULL) {
		gboolean _tmp20_;
		_tmp20_ = g_file_query_exists (parent, NULL);
		_tmp19_ = !_tmp20_;
	} else {
		_tmp19_ = FALSE;
	}
	if (_tmp19_) {
		g_file_make_directory_with_parents (parent, NULL, &_inner_error_);
		if (_inner_error_ != NULL) {
			_g_object_unref0 (parent);
			goto __catch59_g_error;
		}
	}
	_tmp21_ = strlen (content);
	g_file_replace_contents (file, content, (gsize) _tmp21_, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &_inner_error_);
	if (_inner_error_ != NULL) {
		_g_object_unref0 (parent);
		goto __catch59_g_error;
	}
	_g_object_unref0 (parent);
	goto __finally59;
	__catch59_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_warning ("projects.vala:263: Impossible to save the projects: %s", e->message);
		_g_error_free0 (e);
	}
	__finally59:
	if (_inner_error_ != NULL) {
		_g_free0 (content);
		_g_object_unref0 (file);
		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;
	}
	_g_free0 (content);
	_g_object_unref0 (file);
}


static gboolean projects_conflict (Projects* self, GFile* dir1, GFile* dir2) {
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	gboolean _tmp2_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (dir1 != NULL, FALSE);
	g_return_val_if_fail (dir2 != NULL, FALSE);
	_tmp2_ = g_file_has_prefix (dir1, dir2);
	if (_tmp2_) {
		_tmp1_ = TRUE;
	} else {
		gboolean _tmp3_;
		_tmp3_ = g_file_has_prefix (dir2, dir1);
		_tmp1_ = _tmp3_;
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		gboolean _tmp4_;
		_tmp4_ = g_file_equal (dir1, dir2);
		_tmp0_ = _tmp4_;
	}
	result = _tmp0_;
	return result;
}


static void value_projects_init (GValue* value) {
	value->data[0].v_pointer = NULL;
}


static void value_projects_free_value (GValue* value) {
	if (value->data[0].v_pointer) {
		projects_unref (value->data[0].v_pointer);
	}
}


static void value_projects_copy_value (const GValue* src_value, GValue* dest_value) {
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = projects_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}


static gpointer value_projects_peek_pointer (const GValue* value) {
	return value->data[0].v_pointer;
}


static gchar* value_projects_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		Projects* object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = projects_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* value_projects_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	Projects** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = projects_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* param_spec_projects (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	ParamSpecProjects* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_PROJECTS), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}


gpointer value_get_projects (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_PROJECTS), NULL);
	return value->data[0].v_pointer;
}


void value_set_projects (GValue* value, gpointer v_object) {
	Projects* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_PROJECTS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_PROJECTS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		projects_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		projects_unref (old);
	}
}


void value_take_projects (GValue* value, gpointer v_object) {
	Projects* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_PROJECTS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_PROJECTS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		projects_unref (old);
	}
}


static void projects_class_init (ProjectsClass * klass) {
	projects_parent_class = g_type_class_peek_parent (klass);
	PROJECTS_CLASS (klass)->finalize = projects_finalize;
	g_type_class_add_private (klass, sizeof (ProjectsPrivate));
}


static void projects_instance_init (Projects * self) {
	self->priv = PROJECTS_GET_PRIVATE (self);
	self->priv->modified = FALSE;
	self->ref_count = 1;
}


static void projects_finalize (Projects* obj) {
	Projects * self;
	self = PROJECTS (obj);
	_g_object_unref0 (self->priv->projects);
}


GType projects_get_type (void) {
	static volatile gsize projects_type_id__volatile = 0;
	if (g_once_init_enter (&projects_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_projects_init, value_projects_free_value, value_projects_copy_value, value_projects_peek_pointer, "p", value_projects_collect_value, "p", value_projects_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (ProjectsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) projects_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Projects), 0, (GInstanceInitFunc) projects_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType projects_type_id;
		projects_type_id = g_type_register_fundamental (g_type_fundamental_next (), "Projects", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&projects_type_id__volatile, projects_type_id);
	}
	return projects_type_id__volatile;
}


gpointer projects_ref (gpointer instance) {
	Projects* self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}


void projects_unref (gpointer instance) {
	Projects* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		PROJECTS_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}


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



