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

/*
* Copyright (c) 2018-2026 (https://github.com/phase1geo/Minder)
*
* 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 2 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: Trevor Williams <phase1geo@gmail.com>
*/

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <archive.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <archive_entry.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <granite-7.h>

#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

typedef enum  {
	UPGRADE_ACTION_OVERRIDE,
	UPGRADE_ACTION_SAVE_ORIG,
	UPGRADE_ACTION_READ_ONLY,
	UPGRADE_ACTION_NUM
} UpgradeAction;

#define TYPE_UPGRADE_ACTION (upgrade_action_get_type ())
#define _g_free0(var) (var = (g_free (var), NULL))
typedef void (*AfterLoadFunc) (gboolean loaded, const gchar* msg, gpointer user_data);

#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;
typedef struct _DocumentPrivate DocumentPrivate;

#define TYPE_MIND_MAP (mind_map_get_type ())
#define MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MIND_MAP, MindMap))
#define MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MIND_MAP, MindMapClass))
#define IS_MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MIND_MAP))
#define IS_MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MIND_MAP))
#define MIND_MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MIND_MAP, MindMapClass))

typedef struct _MindMap MindMap;
typedef struct _MindMapClass MindMapClass;
enum  {
	DOCUMENT_0_PROPERTY,
	DOCUMENT_FILENAME_PROPERTY,
	DOCUMENT_TEMP_DIR_PROPERTY,
	DOCUMENT_LOAD_VERSION_PROPERTY,
	DOCUMENT_LABEL_PROPERTY,
	DOCUMENT_SAVE_NEEDED_PROPERTY,
	DOCUMENT_READ_ONLY_PROPERTY,
	DOCUMENT_NUM_PROPERTIES
};
static GParamSpec* document_properties[DOCUMENT_NUM_PROPERTIES];
#define _mind_map_unref0(var) ((var == NULL) ? NULL : (var = (mind_map_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _xmlFreeDoc0(var) ((var == NULL) ? NULL : (var = (xmlFreeDoc (var), NULL)))
#define _archive_read_free0(var) ((var == NULL) ? NULL : (var = (archive_read_free (var), NULL)))

#define TYPE_MAP_MODEL (map_model_get_type ())
#define MAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAP_MODEL, MapModel))
#define MAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAP_MODEL, MapModelClass))
#define IS_MAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAP_MODEL))
#define IS_MAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAP_MODEL))
#define MAP_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAP_MODEL, MapModelClass))

typedef struct _MapModel MapModel;
typedef struct _MapModelClass MapModelClass;
#define _archive_write_free0(var) ((var == NULL) ? NULL : (var = (archive_write_free (var), NULL)))

#define TYPE_IMAGE_MANAGER (image_manager_get_type ())
#define IMAGE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_IMAGE_MANAGER, ImageManager))
#define IMAGE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_IMAGE_MANAGER, ImageManagerClass))
#define IS_IMAGE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_IMAGE_MANAGER))
#define IS_IMAGE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_IMAGE_MANAGER))
#define IMAGE_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_IMAGE_MANAGER, ImageManagerClass))

typedef struct _ImageManager ImageManager;
typedef struct _ImageManagerClass ImageManagerClass;
typedef struct _Block12Data Block12Data;
typedef struct _Block13Data Block13Data;
typedef struct _Block14Data Block14Data;
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))

#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;
typedef void (*MainWindowOverwriteFunc) (gboolean overwrite, gpointer user_data);
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))
#define _archive_entry_free0(var) ((var == NULL) ? NULL : (var = (archive_entry_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _Block15Data Block15Data;
enum  {
	DOCUMENT_SAVE_CHANGED_SIGNAL,
	DOCUMENT_READ_ONLY_CHANGED_SIGNAL,
	DOCUMENT_NUM_SIGNALS
};
static guint document_signals[DOCUMENT_NUM_SIGNALS] = {0};

struct _Document {
	GObject parent_instance;
	DocumentPrivate * priv;
};

struct _DocumentClass {
	GObjectClass parent_class;
};

struct _DocumentPrivate {
	MindMap* _map;
	gchar* _filename;
	gchar* _temp_dir;
	gboolean _from_user;
	gchar* _etag;
	gboolean _upgrade_ro;
	gboolean _save_needed;
	gboolean _read_only;
	gchar* _load_version;
};

struct _Block12Data {
	int _ref_count_;
	Document* self;
	xmlDoc* doc;
};

struct _Block13Data {
	int _ref_count_;
	Block12Data * _data12_;
	gchar* file_etag;
};

struct _Block14Data {
	int _ref_count_;
	Block13Data * _data13_;
	GDateTime* now;
};

struct _Block15Data {
	int _ref_count_;
	Document* self;
	GraniteMessageDialog* dialog;
	GtkDropDown* options;
	GtkCheckButton* remember;
	AfterLoadFunc func;
	gpointer func_target;
};

static gint Document_private_offset;
static gpointer document_parent_class = NULL;
VALA_EXTERN gchar* minder_version;

VALA_EXTERN GType upgrade_action_get_type (void) G_GNUC_CONST ;
VALA_EXTERN gchar* upgrade_action_label (UpgradeAction self);
VALA_EXTERN gchar* upgrade_action_tab_label (UpgradeAction self);
VALA_EXTERN gchar** upgrade_action_labels (gint* result_length1);
static void _vala_array_add1 (gchar** * array,
                       gint* length,
                       gint* size,
                       gchar* value);
VALA_EXTERN gchar** upgrade_action_tab_labels (gint* result_length1);
static void _vala_array_add2 (gchar** * array,
                       gint* length,
                       gint* size,
                       gchar* value);
VALA_EXTERN GType document_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Document, g_object_unref)
VALA_EXTERN gpointer mind_map_ref (gpointer instance);
VALA_EXTERN void mind_map_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_mind_map (const gchar* name,
                                 const gchar* nick,
                                 const gchar* blurb,
                                 GType object_type,
                                 GParamFlags flags);
VALA_EXTERN void value_set_mind_map (GValue* value,
                         gpointer v_object);
VALA_EXTERN void value_take_mind_map (GValue* value,
                          gpointer v_object);
VALA_EXTERN gpointer value_get_mind_map (const GValue* value);
VALA_EXTERN GType mind_map_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MindMap, mind_map_unref)
VALA_EXTERN Document* document_new (MindMap* map);
VALA_EXTERN Document* document_construct (GType object_type,
                              MindMap* map);
static gchar* document_generate_etag (Document* self);
static void document_make_temp_dir (Document* self);
static void document_canvas_changed (Document* self);
static void _document_canvas_changed_mind_map_changed (MindMap* _sender,
                                                gpointer self);
VALA_EXTERN void document_auto_save (Document* self);
VALA_EXTERN void document_load_filename (Document* self,
                             const gchar* fname,
                             gboolean saved);
VALA_EXTERN void document_set_filename (Document* self,
                            const gchar* value);
static gchar* document_get_bak_file (Document* self);
VALA_EXTERN const gchar* document_get_filename (Document* self);
static gchar* document_get_orig_file (Document* self);
VALA_EXTERN gboolean document_is_saved (Document* self);
VALA_EXTERN gboolean document_xml_find (const gchar* fname,
                            gint id,
                            gchar** name);
VALA_EXTERN gboolean map_model_xml_find (xmlNode* n,
                             gint id,
                             gchar** name);
static xmlDoc* document_load_raw (Document* self);
static gchar* document_get_map_file (Document* self);
static gchar* document_get_etag (Document* self,
                          xmlDoc* doc);
static gboolean document_load_xml (Document* self);
VALA_EXTERN gpointer map_model_ref (gpointer instance);
VALA_EXTERN void map_model_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_map_model (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_map_model (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_map_model (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_map_model (const GValue* value);
VALA_EXTERN GType map_model_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MapModel, map_model_unref)
VALA_EXTERN MapModel* mind_map_get_model (MindMap* self);
VALA_EXTERN void map_model_load (MapModel* self,
                     xmlNode* n);
static gboolean document_save_xml_internal (Document* self,
                                     const gchar* dest_filename,
                                     gboolean bump_etag);
VALA_EXTERN void document_load (Document* self,
                    UpgradeAction force_upgrade_action,
                    AfterLoadFunc func,
                    gpointer func_target);
VALA_EXTERN GSettings* mind_map_get_settings (MindMap* self);
static void document_upgrade (Document* self,
                       UpgradeAction action,
                       AfterLoadFunc func,
                       gpointer func_target);
static void document_request_upgrade_action (Document* self,
                                      AfterLoadFunc func,
                                      gpointer func_target);
static gchar* document_get_image_dir (Document* self);
static guint8* _vala_array_dup1 (guint8* self,
                          gssize length);
VALA_EXTERN gpointer image_manager_ref (gpointer instance);
VALA_EXTERN void image_manager_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_image_manager (const gchar* name,
                                      const gchar* nick,
                                      const gchar* blurb,
                                      GType object_type,
                                      GParamFlags flags);
VALA_EXTERN void value_set_image_manager (GValue* value,
                              gpointer v_object);
VALA_EXTERN void value_take_image_manager (GValue* value,
                               gpointer v_object);
VALA_EXTERN gpointer value_get_image_manager (const GValue* value);
VALA_EXTERN GType image_manager_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ImageManager, image_manager_unref)
VALA_EXTERN ImageManager* mind_map_get_image_manager (MindMap* self);
VALA_EXTERN void image_manager_set_image_dir (ImageManager* self,
                                  const gchar* image_dir);
VALA_EXTERN gboolean document_save_xml (Document* self);
static Block12Data* block12_data_ref (Block12Data* _data12_);
static void block12_data_unref (void * _userdata_);
static Block13Data* block13_data_ref (Block13Data* _data13_);
static void block13_data_unref (void * _userdata_);
static Block14Data* block14_data_ref (Block14Data* _data14_);
static void block14_data_unref (void * _userdata_);
VALA_EXTERN GType main_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MainWindow, g_object_unref)
VALA_EXTERN MainWindow* mind_map_get_win (MindMap* self);
VALA_EXTERN void main_window_ask_modified_overwrite (MainWindow* self,
                                         MindMap* map,
                                         MainWindowOverwriteFunc func,
                                         gpointer func_target);
static void ____lambda12_ (Block14Data* _data14_,
                    gboolean overwrite);
VALA_EXTERN void mind_map_initialize_for_open (MindMap* self);
static void _____lambda12__main_window_overwrite_func (gboolean overwrite,
                                                gpointer self);
VALA_EXTERN gboolean map_model_save (MapModel* self,
                         xmlNode* parent);
VALA_EXTERN gboolean document_copy_as_orig (Document* self);
static gboolean document_copy_file (Document* self,
                             const gchar* from,
                             const gchar* to);
VALA_EXTERN gboolean document_save (Document* self);
VALA_EXTERN gboolean document_archive_file (Document* self,
                                struct archive* archive,
                                const gchar* fname,
                                gint* image_id);
VALA_EXTERN GArray* image_manager_get_ids (ImageManager* self);
VALA_EXTERN gchar* image_manager_get_file (ImageManager* self,
                               gint id);
static gboolean document_move_file (Document* self,
                             const gchar* from,
                             const gchar* to);
static Block15Data* block15_data_ref (Block15Data* _data15_);
static void block15_data_unref (void * _userdata_);
static void __lambda31_ (Block15Data* _data15_,
                  gint id);
static void ___lambda31__gtk_dialog_response (GtkDialog* _sender,
                                       gint response_id,
                                       gpointer self);
VALA_EXTERN gboolean document_remove (Document* self);
static void document_delete_recursively (Document* self,
                                  GFile* file,
                                  GError** error);
VALA_EXTERN void document_cleanup (Document* self);
VALA_EXTERN const gchar* document_get_temp_dir (Document* self);
VALA_EXTERN const gchar* document_get_load_version (Document* self);
VALA_EXTERN gchar* document_get_label (Document* self);
VALA_EXTERN gboolean document_get_save_needed (Document* self);
VALA_EXTERN gboolean document_get_read_only (Document* self);
VALA_EXTERN gboolean utils_is_read_only (const gchar* fname);
static void document_finalize (GObject * obj);
static GType document_get_type_once (void);
static void _vala_document_get_property (GObject * object,
                                  guint property_id,
                                  GValue * value,
                                  GParamSpec * pspec);
static void _vala_document_set_property (GObject * object,
                                  guint property_id,
                                  const GValue * value,
                                  GParamSpec * pspec);
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 inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

gchar*
upgrade_action_label (UpgradeAction self)
{
	gchar* result;
	switch (self) {
		case UPGRADE_ACTION_OVERRIDE:
		{
			gchar* _tmp0_;
			_tmp0_ = g_strdup (_ ("Upgrade Minder file to new version"));
			result = _tmp0_;
			return result;
		}
		case UPGRADE_ACTION_SAVE_ORIG:
		{
			gchar* _tmp1_;
			_tmp1_ = g_strdup (_ ("Upgrade Minder file to new version and keep a copy of the original"));
			result = _tmp1_;
			return result;
		}
		case UPGRADE_ACTION_READ_ONLY:
		{
			gchar* _tmp2_;
			_tmp2_ = g_strdup (_ ("Do not upgrade Minder file but view it as read-only"));
			result = _tmp2_;
			return result;
		}
		default:
		{
			result = NULL;
			return result;
		}
	}
}

gchar*
upgrade_action_tab_label (UpgradeAction self)
{
	gchar* result;
	switch (self) {
		case UPGRADE_ACTION_OVERRIDE:
		{
			gchar* _tmp0_;
			_tmp0_ = g_strdup (_ ("Upgrade Minder files to new version"));
			result = _tmp0_;
			return result;
		}
		case UPGRADE_ACTION_SAVE_ORIG:
		{
			gchar* _tmp1_;
			_tmp1_ = g_strdup (_ ("Upgrade Minder files to new version but keep a copy of the original if" \
" previously saved"));
			result = _tmp1_;
			return result;
		}
		case UPGRADE_ACTION_READ_ONLY:
		{
			gchar* _tmp2_;
			_tmp2_ = g_strdup (_ ("Do not upgrade Minder files but view them as read-only"));
			result = _tmp2_;
			return result;
		}
		default:
		{
			result = NULL;
			return result;
		}
	}
}

static void
_vala_array_add1 (gchar** * array,
                  gint* length,
                  gint* size,
                  gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

gchar**
upgrade_action_labels (gint* result_length1)
{
	gchar** lbls = NULL;
	gchar** _tmp0_;
	gint lbls_length1;
	gint _lbls_size_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gchar** result;
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	lbls = _tmp0_;
	lbls_length1 = 0;
	_lbls_size_ = lbls_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				UpgradeAction action = 0;
				gchar* _tmp3_;
				gchar* _tmp4_;
				gboolean _tmp5_;
				if (!_tmp1_) {
					gint _tmp2_;
					_tmp2_ = i;
					i = _tmp2_ + 1;
				}
				_tmp1_ = FALSE;
				if (!(i < ((gint) UPGRADE_ACTION_NUM))) {
					break;
				}
				action = (UpgradeAction) i;
				_tmp3_ = upgrade_action_label (action);
				_tmp4_ = _tmp3_;
				_tmp5_ = _tmp4_ != NULL;
				_g_free0 (_tmp4_);
				if (_tmp5_) {
					gchar* _tmp6_;
					_tmp6_ = upgrade_action_label (action);
					_vala_array_add1 (&lbls, &lbls_length1, &_lbls_size_, _tmp6_);
				}
			}
		}
	}
	_tmp7_ = lbls;
	_tmp7__length1 = lbls_length1;
	if (result_length1) {
		*result_length1 = _tmp7__length1;
	}
	result = _tmp7_;
	return result;
}

static void
_vala_array_add2 (gchar** * array,
                  gint* length,
                  gint* size,
                  gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

gchar**
upgrade_action_tab_labels (gint* result_length1)
{
	gchar** lbls = NULL;
	gchar** _tmp0_;
	gint lbls_length1;
	gint _lbls_size_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gchar** result;
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	lbls = _tmp0_;
	lbls_length1 = 0;
	_lbls_size_ = lbls_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				UpgradeAction action = 0;
				gchar* _tmp3_;
				gchar* _tmp4_;
				gboolean _tmp5_;
				if (!_tmp1_) {
					gint _tmp2_;
					_tmp2_ = i;
					i = _tmp2_ + 1;
				}
				_tmp1_ = FALSE;
				if (!(i < ((gint) UPGRADE_ACTION_NUM))) {
					break;
				}
				action = (UpgradeAction) i;
				_tmp3_ = upgrade_action_tab_label (action);
				_tmp4_ = _tmp3_;
				_tmp5_ = _tmp4_ != NULL;
				_g_free0 (_tmp4_);
				if (_tmp5_) {
					gchar* _tmp6_;
					_tmp6_ = upgrade_action_tab_label (action);
					_vala_array_add2 (&lbls, &lbls_length1, &_lbls_size_, _tmp6_);
				}
			}
		}
	}
	_tmp7_ = lbls;
	_tmp7__length1 = lbls_length1;
	if (result_length1) {
		*result_length1 = _tmp7__length1;
	}
	result = _tmp7_;
	return result;
}

static GType
upgrade_action_get_type_once (void)
{
	static const GEnumValue values[] = {{UPGRADE_ACTION_OVERRIDE, "UPGRADE_ACTION_OVERRIDE", "override"}, {UPGRADE_ACTION_SAVE_ORIG, "UPGRADE_ACTION_SAVE_ORIG", "save-orig"}, {UPGRADE_ACTION_READ_ONLY, "UPGRADE_ACTION_READ_ONLY", "read-only"}, {UPGRADE_ACTION_NUM, "UPGRADE_ACTION_NUM", "num"}, {0, NULL, NULL}};
	GType upgrade_action_type_id;
	upgrade_action_type_id = g_enum_register_static ("UpgradeAction", values);
	return upgrade_action_type_id;
}

GType
upgrade_action_get_type (void)
{
	static volatile gsize upgrade_action_type_id__once = 0;
	if (g_once_init_enter (&upgrade_action_type_id__once)) {
		GType upgrade_action_type_id;
		upgrade_action_type_id = upgrade_action_get_type_once ();
		g_once_init_leave (&upgrade_action_type_id__once, upgrade_action_type_id);
	}
	return upgrade_action_type_id__once;
}

static inline gpointer
document_get_instance_private (Document* self)
{
	return G_STRUCT_MEMBER_P (self, Document_private_offset);
}

static gpointer
_mind_map_ref0 (gpointer self)
{
	return self ? mind_map_ref (self) : NULL;
}

static void
_document_canvas_changed_mind_map_changed (MindMap* _sender,
                                           gpointer self)
{
	document_canvas_changed ((Document*) self);
}

Document*
document_construct (GType object_type,
                    MindMap* map)
{
	Document * self = NULL;
	MindMap* _tmp0_;
	gchar* _tmp1_;
	gchar* dir = NULL;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	const gchar* _tmp4_;
	g_return_val_if_fail (map != NULL, NULL);
	self = (Document*) g_object_new (object_type, NULL);
	_tmp0_ = _mind_map_ref0 (map);
	_mind_map_unref0 (self->priv->_map);
	self->priv->_map = _tmp0_;
	_tmp1_ = document_generate_etag (self);
	_g_free0 (self->priv->_etag);
	self->priv->_etag = _tmp1_;
	_tmp2_ = g_get_user_data_dir ();
	_tmp3_ = g_build_filename (_tmp2_, "minder", NULL);
	dir = _tmp3_;
	_tmp4_ = dir;
	if (g_mkdir_with_parents (_tmp4_, 0775) == 0) {
		gint i = 0;
		i = 1;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				const gchar* _tmp7_;
				gint _tmp8_;
				gchar* _tmp9_;
				gchar* _tmp10_;
				gchar* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_;
				if (!_tmp5_) {
					const gchar* _tmp6_;
					_tmp6_ = self->priv->_filename;
					if (!g_file_test (_tmp6_, G_FILE_TEST_EXISTS)) {
						break;
					}
				}
				_tmp5_ = FALSE;
				_tmp7_ = dir;
				_tmp8_ = i;
				i = _tmp8_ + 1;
				_tmp9_ = g_strdup_printf ("%d.minder", _tmp8_);
				_tmp10_ = _tmp9_;
				_tmp11_ = g_strconcat (_ ("unnamed"), _tmp10_, NULL);
				_tmp12_ = _tmp11_;
				_tmp13_ = g_build_filename (_tmp7_, _tmp12_, NULL);
				_g_free0 (self->priv->_filename);
				self->priv->_filename = _tmp13_;
				_g_free0 (_tmp12_);
				_g_free0 (_tmp10_);
			}
		}
		self->priv->_from_user = FALSE;
	}
	document_make_temp_dir (self);
	g_signal_connect_object (map, "changed", (GCallback) _document_canvas_changed_mind_map_changed, self, 0);
	_g_free0 (dir);
	return self;
}

Document*
document_new (MindMap* map)
{
	return document_construct (TYPE_DOCUMENT, map);
}

static gchar*
document_generate_etag (Document* self)
{
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup_printf ("%u", g_random_int ());
	result = _tmp0_;
	return result;
}

static void
document_canvas_changed (Document* self)
{
	gboolean call_save_changed = FALSE;
	g_return_if_fail (self != NULL);
	call_save_changed = !self->priv->_save_needed;
	self->priv->_save_needed = TRUE;
	document_auto_save (self);
	if (call_save_changed) {
		g_signal_emit (self, document_signals[DOCUMENT_SAVE_CHANGED_SIGNAL], 0);
	}
}

void
document_load_filename (Document* self,
                        const gchar* fname,
                        gboolean saved)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (fname != NULL);
	document_set_filename (self, fname);
	self->priv->_from_user = saved;
}

static gchar*
document_get_bak_file (Document* self)
{
	GFile* file = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* parent = NULL;
	GFile* _tmp3_;
	GFile* _tmp4_;
	gchar* _tmp5_ = NULL;
	GFile* _tmp6_;
	gchar* dir = NULL;
	gchar* _tmp10_;
	gchar* bak_basename = NULL;
	GFile* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = document_get_filename (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	file = _tmp2_;
	_tmp3_ = file;
	_tmp4_ = g_file_get_parent (_tmp3_);
	parent = _tmp4_;
	_tmp6_ = parent;
	if (_tmp6_ != NULL) {
		GFile* _tmp7_;
		gchar* _tmp8_;
		_tmp7_ = parent;
		_tmp8_ = g_file_get_path (_tmp7_);
		_g_free0 (_tmp5_);
		_tmp5_ = _tmp8_;
	} else {
		gchar* _tmp9_;
		_tmp9_ = g_strdup (".");
		_g_free0 (_tmp5_);
		_tmp5_ = _tmp9_;
	}
	_tmp10_ = _tmp5_;
	_tmp5_ = NULL;
	dir = _tmp10_;
	_tmp11_ = file;
	_tmp12_ = g_file_get_basename (_tmp11_);
	_tmp13_ = _tmp12_;
	_tmp14_ = g_strconcat (".", _tmp13_, NULL);
	_tmp15_ = _tmp14_;
	_tmp16_ = g_strconcat (_tmp15_, ".bak", NULL);
	_tmp17_ = _tmp16_;
	_g_free0 (_tmp15_);
	_g_free0 (_tmp13_);
	bak_basename = _tmp17_;
	_tmp18_ = g_build_filename (dir, bak_basename, NULL);
	result = _tmp18_;
	_g_free0 (bak_basename);
	_g_free0 (dir);
	_g_free0 (_tmp5_);
	_g_object_unref0 (parent);
	_g_object_unref0 (file);
	return result;
}

static gint
string_last_index_of_char (const gchar* self,
                           gunichar c,
                           gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = g_utf8_strrchr (((gchar*) self) + start_index, (gssize) -1, c);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

static gchar*
document_get_orig_file (Document* self)
{
	GFile* file = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* parent = NULL;
	GFile* _tmp3_;
	GFile* _tmp4_;
	gchar* _tmp5_ = NULL;
	GFile* _tmp6_;
	gchar* dir = NULL;
	gchar* _tmp10_;
	gchar* basename = NULL;
	GFile* _tmp11_;
	gchar* _tmp12_;
	gint index = 0;
	const gchar* _tmp13_;
	gchar* _tmp14_ = NULL;
	gchar* _tmp26_;
	const gchar* _tmp27_;
	const gchar* _tmp28_;
	gchar* _tmp29_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = document_get_filename (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	file = _tmp2_;
	_tmp3_ = file;
	_tmp4_ = g_file_get_parent (_tmp3_);
	parent = _tmp4_;
	_tmp6_ = parent;
	if (_tmp6_ != NULL) {
		GFile* _tmp7_;
		gchar* _tmp8_;
		_tmp7_ = parent;
		_tmp8_ = g_file_get_path (_tmp7_);
		_g_free0 (_tmp5_);
		_tmp5_ = _tmp8_;
	} else {
		gchar* _tmp9_;
		_tmp9_ = g_strdup (".");
		_g_free0 (_tmp5_);
		_tmp5_ = _tmp9_;
	}
	_tmp10_ = _tmp5_;
	_tmp5_ = NULL;
	dir = _tmp10_;
	_tmp11_ = file;
	_tmp12_ = g_file_get_basename (_tmp11_);
	basename = _tmp12_;
	_tmp13_ = basename;
	index = string_last_index_of_char (_tmp13_, (gunichar) '.', 0);
	if (index > 0) {
		const gchar* _tmp15_;
		gchar* _tmp16_;
		gchar* _tmp17_;
		gchar* _tmp18_;
		gchar* _tmp19_;
		const gchar* _tmp20_;
		gchar* _tmp21_;
		gchar* _tmp22_;
		gchar* _tmp23_;
		_tmp15_ = basename;
		_tmp16_ = string_substring (_tmp15_, (glong) 0, (glong) index);
		_tmp17_ = _tmp16_;
		_tmp18_ = g_strconcat (_tmp17_, ".orig", NULL);
		_tmp19_ = _tmp18_;
		_tmp20_ = basename;
		_tmp21_ = string_substring (_tmp20_, (glong) index, (glong) -1);
		_tmp22_ = _tmp21_;
		_tmp23_ = g_strconcat (_tmp19_, _tmp22_, NULL);
		_g_free0 (_tmp14_);
		_tmp14_ = _tmp23_;
		_g_free0 (_tmp22_);
		_g_free0 (_tmp19_);
		_g_free0 (_tmp17_);
	} else {
		const gchar* _tmp24_;
		gchar* _tmp25_;
		_tmp24_ = basename;
		_tmp25_ = g_strconcat (_tmp24_, ".orig", NULL);
		_g_free0 (_tmp14_);
		_tmp14_ = _tmp25_;
	}
	_tmp26_ = _tmp14_;
	_tmp14_ = NULL;
	_g_free0 (basename);
	basename = _tmp26_;
	_tmp27_ = dir;
	_tmp28_ = basename;
	_tmp29_ = g_build_filename (_tmp27_, _tmp28_, NULL);
	result = _tmp29_;
	_g_free0 (_tmp14_);
	_g_free0 (basename);
	_g_free0 (dir);
	_g_free0 (_tmp5_);
	_g_object_unref0 (parent);
	_g_object_unref0 (file);
	return result;
}

gboolean
document_is_saved (Document* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_from_user;
	return result;
}

gboolean
document_xml_find (const gchar* fname,
                   gint id,
                   gchar** name)
{
	gboolean found = FALSE;
	struct archive* archive = NULL;
	struct archive* _tmp0_;
	struct archive* _tmp1_;
	struct archive* _tmp2_;
	struct archive* _tmp3_;
	struct archive_entry* entry = NULL;
	struct archive* _tmp31_;
	gboolean result;
	g_return_val_if_fail (fname != NULL, FALSE);
	g_return_val_if_fail (*name != NULL, FALSE);
	found = FALSE;
	_tmp0_ = archive_read_new ();
	archive = _tmp0_;
	_tmp1_ = archive;
	archive_read_support_filter_gzip (_tmp1_);
	_tmp2_ = archive;
	archive_read_support_format_all (_tmp2_);
	_tmp3_ = archive;
	if (archive_read_open_filename (_tmp3_, fname, (gsize) 16384) != ARCHIVE_OK) {
		xmlDoc* doc = NULL;
		xmlDoc* _tmp4_;
		xmlDoc* _tmp5_;
		_tmp4_ = xmlReadFile (fname, NULL, (gint) XML_PARSE_HUGE);
		doc = _tmp4_;
		_tmp5_ = doc;
		if (_tmp5_ != NULL) {
			xmlDoc* _tmp6_;
			xmlNode* _tmp7_;
			gboolean _tmp8_;
			xmlDoc* _tmp9_;
			_tmp6_ = doc;
			_tmp7_ = xmlDocGetRootElement (_tmp6_);
			_tmp8_ = map_model_xml_find (_tmp7_, id, name);
			found = _tmp8_;
			_tmp9_ = doc;
			_xmlFreeDoc0 (_tmp9_);
		}
		result = found;
		_archive_read_free0 (archive);
		return result;
	}
	while (TRUE) {
		struct archive* _tmp10_;
		struct archive_entry* _tmp11_ = NULL;
		int _tmp12_;
		struct archive_entry* _tmp13_;
		const gchar* _tmp14_;
		_tmp10_ = archive;
		_tmp12_ = archive_read_next_header (_tmp10_, &_tmp11_);
		entry = _tmp11_;
		if (!(_tmp12_ == ARCHIVE_OK)) {
			break;
		}
		_tmp13_ = entry;
		_tmp14_ = archive_entry_pathname (_tmp13_);
		if (g_strcmp0 (_tmp14_, "map.xml") == 0) {
			guint8* data = NULL;
			struct archive_entry* _tmp15_;
			guint8* _tmp16_;
			gint data_length1;
			gint _data_size_;
			struct archive* _tmp17_;
			guint8* _tmp18_;
			gint _tmp18__length1;
			_tmp15_ = entry;
			_tmp16_ = g_new0 (guint8, archive_entry_size (_tmp15_));
			data = _tmp16_;
			data_length1 = archive_entry_size (_tmp15_);
			_data_size_ = data_length1;
			_tmp17_ = archive;
			_tmp18_ = data;
			_tmp18__length1 = data_length1;
			if (archive_read_data (_tmp17_, _tmp18_, (size_t) _tmp18__length1) > ((gssize) 0)) {
				gchar* memory = NULL;
				guint8* _tmp19_;
				gint _tmp19__length1;
				gchar* _tmp20_;
				xmlDoc* doc = NULL;
				const gchar* _tmp21_;
				const gchar* _tmp22_;
				gint _tmp23_;
				gint _tmp24_;
				xmlDoc* _tmp25_;
				xmlDoc* _tmp26_;
				_tmp19_ = data;
				_tmp19__length1 = data_length1;
				_tmp20_ = g_strdup ((const gchar*) _tmp19_);
				memory = _tmp20_;
				_tmp21_ = memory;
				_tmp22_ = memory;
				_tmp23_ = strlen (_tmp22_);
				_tmp24_ = _tmp23_;
				_tmp25_ = xmlReadMemory (_tmp21_, _tmp24_, NULL, NULL, (gint) XML_PARSE_HUGE);
				doc = _tmp25_;
				_tmp26_ = doc;
				if (_tmp26_ != NULL) {
					xmlDoc* _tmp27_;
					xmlNode* _tmp28_;
					gboolean _tmp29_;
					xmlDoc* _tmp30_;
					_tmp27_ = doc;
					_tmp28_ = xmlDocGetRootElement (_tmp27_);
					_tmp29_ = map_model_xml_find (_tmp28_, id, name);
					found = _tmp29_;
					_tmp30_ = doc;
					_xmlFreeDoc0 (_tmp30_);
					_g_free0 (memory);
					data = (g_free (data), NULL);
					break;
				}
				_g_free0 (memory);
			}
			data = (g_free (data), NULL);
		}
	}
	_tmp31_ = archive;
	archive_read_close (_tmp31_);
	result = found;
	_archive_read_free0 (archive);
	return result;
}

static xmlDoc*
document_load_raw (Document* self)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	xmlDoc* _tmp2_;
	xmlDoc* _tmp3_;
	xmlDoc* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = document_get_map_file (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = xmlReadFile (_tmp1_, NULL, (gint) (XML_PARSE_HUGE | XML_PARSE_NOWARNING));
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	result = _tmp3_;
	return result;
}

static gchar*
document_get_etag (Document* self,
                   xmlDoc* doc)
{
	gchar* e = NULL;
	xmlNode* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_ = NULL;
	const gchar* _tmp3_;
	gchar* _tmp5_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = xmlDocGetRootElement (doc);
	_tmp1_ = (gchar*) xmlGetProp (_tmp0_, (xmlChar*) "etag");
	e = _tmp1_;
	_tmp3_ = e;
	if (_tmp3_ == NULL) {
		_tmp2_ = "";
	} else {
		const gchar* _tmp4_;
		_tmp4_ = e;
		_tmp2_ = _tmp4_;
	}
	_tmp5_ = g_strdup (_tmp2_);
	result = _tmp5_;
	_g_free0 (e);
	return result;
}

static gboolean
document_load_xml (Document* self)
{
	xmlDoc* doc = NULL;
	xmlDoc* _tmp0_;
	xmlDoc* _tmp1_;
	xmlDoc* _tmp2_;
	gchar* _tmp3_;
	gchar* v = NULL;
	xmlDoc* _tmp4_;
	xmlNode* _tmp5_;
	gchar* _tmp6_;
	const gchar* _tmp7_;
	MindMap* _tmp10_;
	MapModel* _tmp11_;
	MapModel* _tmp12_;
	xmlDoc* _tmp13_;
	xmlNode* _tmp14_;
	xmlDoc* _tmp15_;
	const gchar* _tmp16_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = document_load_raw (self);
	doc = _tmp0_;
	_tmp1_ = doc;
	if (_tmp1_ == NULL) {
		result = FALSE;
		return result;
	}
	_tmp2_ = doc;
	_tmp3_ = document_get_etag (self, _tmp2_);
	_g_free0 (self->priv->_etag);
	self->priv->_etag = _tmp3_;
	_tmp4_ = doc;
	_tmp5_ = xmlDocGetRootElement (_tmp4_);
	_tmp6_ = (gchar*) xmlGetProp (_tmp5_, (xmlChar*) "version");
	v = _tmp6_;
	_tmp7_ = v;
	if (_tmp7_ != NULL) {
		const gchar* _tmp8_;
		gchar* _tmp9_;
		_tmp8_ = v;
		_tmp9_ = g_strdup (_tmp8_);
		_g_free0 (self->priv->_load_version);
		self->priv->_load_version = _tmp9_;
	}
	_tmp10_ = self->priv->_map;
	_tmp11_ = mind_map_get_model (_tmp10_);
	_tmp12_ = _tmp11_;
	_tmp13_ = doc;
	_tmp14_ = xmlDocGetRootElement (_tmp13_);
	map_model_load (_tmp12_, _tmp14_);
	_tmp15_ = doc;
	_xmlFreeDoc0 (_tmp15_);
	_tmp16_ = self->priv->_etag;
	if (g_strcmp0 (_tmp16_, "") == 0) {
		gchar* _tmp17_;
		const gchar* _tmp18_;
		const gchar* _tmp19_;
		_tmp17_ = document_generate_etag (self);
		_g_free0 (self->priv->_etag);
		self->priv->_etag = _tmp17_;
		_tmp18_ = document_get_filename (self);
		_tmp19_ = _tmp18_;
		document_save_xml_internal (self, _tmp19_, FALSE);
	}
	result = TRUE;
	_g_free0 (v);
	return result;
}

static guint8*
_vala_array_dup1 (guint8* self,
                  gssize length)
{
	if (length > 0) {
		return _vala_memdup2 (self, length * sizeof (guint8));
	}
	return NULL;
}

void
document_load (Document* self,
               UpgradeAction force_upgrade_action,
               AfterLoadFunc func,
               gpointer func_target)
{
	gchar* bak_file = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_ = NULL;
	gboolean _tmp2_ = FALSE;
	const gchar* _tmp3_;
	gchar* fname = NULL;
	gchar* _tmp10_;
	struct archive* archive = NULL;
	struct archive* _tmp11_;
	struct archive* _tmp12_;
	struct archive* _tmp13_;
	int flags = 0;
	struct archive* extractor = NULL;
	struct archive* _tmp14_;
	struct archive* _tmp15_;
	struct archive* _tmp16_;
	struct archive* _tmp17_;
	const gchar* _tmp18_;
	struct archive_entry* entry = NULL;
	struct archive* _tmp53_;
	MindMap* _tmp57_;
	ImageManager* _tmp58_;
	ImageManager* _tmp59_;
	gchar* _tmp60_;
	gchar* _tmp61_;
	gboolean loaded = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = document_get_bak_file (self);
	bak_file = _tmp0_;
	_tmp3_ = bak_file;
	if (g_file_test (_tmp3_, G_FILE_TEST_EXISTS)) {
		MindMap* _tmp4_;
		GSettings* _tmp5_;
		GSettings* _tmp6_;
		_tmp4_ = self->priv->_map;
		_tmp5_ = mind_map_get_settings (_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp2_ = !g_settings_get_boolean (_tmp6_, "keep-backup-after-save");
	} else {
		_tmp2_ = FALSE;
	}
	if (_tmp2_) {
		const gchar* _tmp7_;
		_tmp7_ = bak_file;
		_tmp1_ = _tmp7_;
	} else {
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		_tmp8_ = document_get_filename (self);
		_tmp9_ = _tmp8_;
		_tmp1_ = _tmp9_;
	}
	_tmp10_ = g_strdup (_tmp1_);
	fname = _tmp10_;
	_tmp11_ = archive_read_new ();
	archive = _tmp11_;
	_tmp12_ = archive;
	archive_read_support_filter_gzip (_tmp12_);
	_tmp13_ = archive;
	archive_read_support_format_all (_tmp13_);
	flags = ARCHIVE_EXTRACT_TIME;
	flags |= ARCHIVE_EXTRACT_PERM;
	flags |= ARCHIVE_EXTRACT_ACL;
	flags |= ARCHIVE_EXTRACT_FFLAGS;
	_tmp14_ = archive_write_disk_new ();
	extractor = _tmp14_;
	_tmp15_ = extractor;
	archive_write_disk_set_options (_tmp15_, flags);
	_tmp16_ = extractor;
	archive_write_disk_set_standard_lookup (_tmp16_);
	_tmp17_ = archive;
	_tmp18_ = fname;
	if (archive_read_open_filename (_tmp17_, _tmp18_, (gsize) 16384) != ARCHIVE_OK) {
		if (force_upgrade_action != UPGRADE_ACTION_NUM) {
			document_upgrade (self, force_upgrade_action, func, func_target);
		} else {
			MindMap* _tmp19_;
			GSettings* _tmp20_;
			GSettings* _tmp21_;
			_tmp19_ = self->priv->_map;
			_tmp20_ = mind_map_get_settings (_tmp19_);
			_tmp21_ = _tmp20_;
			if (g_settings_get_boolean (_tmp21_, "ask-for-upgrade-action")) {
				document_request_upgrade_action (self, func, func_target);
			} else {
				MindMap* _tmp22_;
				GSettings* _tmp23_;
				GSettings* _tmp24_;
				_tmp22_ = self->priv->_map;
				_tmp23_ = mind_map_get_settings (_tmp22_);
				_tmp24_ = _tmp23_;
				document_upgrade (self, (UpgradeAction) g_settings_get_int (_tmp24_, "upgrade-action"), func, func_target);
			}
		}
		_archive_write_free0 (extractor);
		_archive_read_free0 (archive);
		_g_free0 (fname);
		_g_free0 (bak_file);
		return;
	}
	while (TRUE) {
		struct archive* _tmp25_;
		struct archive_entry* _tmp26_ = NULL;
		int _tmp27_;
		struct archive_entry* _tmp28_;
		const gchar* _tmp29_;
		struct archive* _tmp43_;
		struct archive_entry* _tmp44_;
		guint8* buffer = NULL;
		gint buffer_length1 = 0;
		gint _buffer_size_ = 0;
		la_int64_t offset = {0};
		_tmp25_ = archive;
		_tmp27_ = archive_read_next_header (_tmp25_, &_tmp26_);
		entry = _tmp26_;
		if (!(_tmp27_ == ARCHIVE_OK)) {
			break;
		}
		_tmp28_ = entry;
		_tmp29_ = archive_entry_pathname (_tmp28_);
		if (g_strcmp0 (_tmp29_, "map.xml") == 0) {
			struct archive_entry* _tmp30_;
			const gchar* _tmp31_;
			struct archive_entry* _tmp32_;
			const gchar* _tmp33_;
			gchar* _tmp34_;
			gchar* _tmp35_;
			_tmp30_ = entry;
			_tmp31_ = self->priv->_temp_dir;
			_tmp32_ = entry;
			_tmp33_ = archive_entry_pathname (_tmp32_);
			_tmp34_ = g_build_filename (_tmp31_, _tmp33_, NULL);
			_tmp35_ = _tmp34_;
			archive_entry_set_pathname (_tmp30_, _tmp35_);
			_g_free0 (_tmp35_);
		} else {
			struct archive_entry* _tmp36_;
			gchar* _tmp37_;
			gchar* _tmp38_;
			struct archive_entry* _tmp39_;
			const gchar* _tmp40_;
			gchar* _tmp41_;
			gchar* _tmp42_;
			_tmp36_ = entry;
			_tmp37_ = document_get_image_dir (self);
			_tmp38_ = _tmp37_;
			_tmp39_ = entry;
			_tmp40_ = archive_entry_pathname (_tmp39_);
			_tmp41_ = g_build_filename (_tmp38_, _tmp40_, NULL);
			_tmp42_ = _tmp41_;
			archive_entry_set_pathname (_tmp36_, _tmp42_);
			_g_free0 (_tmp42_);
			_g_free0 (_tmp38_);
		}
		_tmp43_ = extractor;
		_tmp44_ = entry;
		if (archive_write_header (_tmp43_, _tmp44_) != ARCHIVE_OK) {
			continue;
		}
		while (TRUE) {
			struct archive* _tmp45_;
			guint8* _tmp46_ = NULL;
			size_t _tmp47_ = 0;
			la_int64_t _tmp48_ = {0};
			int _tmp49_;
			guint8* _tmp50_;
			gint _tmp50__length1;
			struct archive* _tmp51_;
			guint8* _tmp52_;
			gint _tmp52__length1;
			_tmp45_ = archive;
			_tmp49_ = archive_read_data_block (_tmp45_, &_tmp46_, &_tmp47_, &_tmp48_);
			buffer = (g_free (buffer), NULL);
			_tmp50_ = (_tmp46_ != NULL) ? _vala_array_dup1 (_tmp46_, _tmp47_) : _tmp46_;
			_tmp50__length1 = _tmp47_;
			buffer = _tmp50_;
			buffer_length1 = _tmp50__length1;
			_buffer_size_ = buffer_length1;
			offset = _tmp48_;
			if (!(_tmp49_ == ARCHIVE_OK)) {
				break;
			}
			_tmp51_ = extractor;
			_tmp52_ = buffer;
			_tmp52__length1 = buffer_length1;
			if (archive_write_data_block (_tmp51_, _tmp52_, (size_t) _tmp52__length1, offset) != ((gssize) ARCHIVE_OK)) {
				break;
			}
		}
		buffer = (g_free (buffer), NULL);
	}
	_tmp53_ = archive;
	if (archive_read_close (_tmp53_) != ARCHIVE_OK) {
		struct archive* _tmp54_;
		const gchar* _tmp55_;
		struct archive* _tmp56_;
		_tmp54_ = archive;
		_tmp55_ = archive_error_string (_tmp54_);
		_tmp56_ = archive;
		g_error ("Document.vala:396: Error: %s (%d)", _tmp55_, archive_errno (_tmp56_));
	}
	_tmp57_ = self->priv->_map;
	_tmp58_ = mind_map_get_image_manager (_tmp57_);
	_tmp59_ = _tmp58_;
	_tmp60_ = document_get_image_dir (self);
	_tmp61_ = _tmp60_;
	image_manager_set_image_dir (_tmp59_, _tmp61_);
	_g_free0 (_tmp61_);
	loaded = document_load_xml (self);
	if (func != NULL) {
		func (loaded, "load", func_target);
	}
	_archive_write_free0 (extractor);
	_archive_read_free0 (archive);
	_g_free0 (fname);
	_g_free0 (bak_file);
}

static Block12Data*
block12_data_ref (Block12Data* _data12_)
{
	g_atomic_int_inc (&_data12_->_ref_count_);
	return _data12_;
}

static void
block12_data_unref (void * _userdata_)
{
	Block12Data* _data12_;
	_data12_ = (Block12Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data12_->_ref_count_)) {
		Document* self;
		self = _data12_->self;
		_g_object_unref0 (self);
		g_slice_free (Block12Data, _data12_);
	}
}

static Block13Data*
block13_data_ref (Block13Data* _data13_)
{
	g_atomic_int_inc (&_data13_->_ref_count_);
	return _data13_;
}

static void
block13_data_unref (void * _userdata_)
{
	Block13Data* _data13_;
	_data13_ = (Block13Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data13_->_ref_count_)) {
		Document* self;
		self = _data13_->_data12_->self;
		_g_free0 (_data13_->file_etag);
		block12_data_unref (_data13_->_data12_);
		_data13_->_data12_ = NULL;
		g_slice_free (Block13Data, _data13_);
	}
}

static Block14Data*
block14_data_ref (Block14Data* _data14_)
{
	g_atomic_int_inc (&_data14_->_ref_count_);
	return _data14_;
}

static void
block14_data_unref (void * _userdata_)
{
	Block14Data* _data14_;
	_data14_ = (Block14Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data14_->_ref_count_)) {
		Document* self;
		self = _data14_->_data13_->_data12_->self;
		_g_date_time_unref0 (_data14_->now);
		block13_data_unref (_data14_->_data13_);
		_data14_->_data13_ = NULL;
		g_slice_free (Block14Data, _data14_);
	}
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static gchar*
g_date_time_to_string (GDateTime* self)
{
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_date_time_format (self, "%FT%H:%M:%S%z");
	result = _tmp0_;
	return result;
}

static void
____lambda12_ (Block14Data* _data14_,
               gboolean overwrite)
{
	Block13Data* _data13_;
	Block12Data* _data12_;
	Document* self;
	xmlDoc* _tmp24_;
	_data13_ = _data14_->_data13_;
	_data12_ = _data13_->_data12_;
	self = _data12_->self;
	if (overwrite) {
		gchar* fname = NULL;
		const gchar* _tmp0_;
		const gchar* _tmp1_;
		GDateTime* _tmp2_;
		gchar* _tmp3_;
		gchar* _tmp4_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		gchar* _tmp9_;
		xmlDoc* _tmp10_;
		const gchar* _tmp11_;
		_tmp0_ = document_get_filename (self);
		_tmp1_ = _tmp0_;
		_tmp2_ = _data14_->now;
		_tmp3_ = g_date_time_to_string (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = _data13_->file_etag;
		_tmp6_ = g_strdup_printf ("-backup-%s-%s.mind", _tmp4_, _tmp5_);
		_tmp7_ = _tmp6_;
		_tmp8_ = string_replace (_tmp1_, ".mind", _tmp7_);
		_tmp9_ = _tmp8_;
		_g_free0 (_tmp7_);
		_g_free0 (_tmp4_);
		fname = _tmp9_;
		_tmp10_ = _data12_->doc;
		_tmp11_ = fname;
		xmlSaveFormatFile (_tmp11_, _tmp10_, 1);
		_g_free0 (fname);
	} else {
		gchar* fname = NULL;
		const gchar* _tmp12_;
		const gchar* _tmp13_;
		GDateTime* _tmp14_;
		gchar* _tmp15_;
		gchar* _tmp16_;
		const gchar* _tmp17_;
		gchar* _tmp18_;
		gchar* _tmp19_;
		gchar* _tmp20_;
		gchar* _tmp21_;
		const gchar* _tmp22_;
		MindMap* _tmp23_;
		_tmp12_ = document_get_filename (self);
		_tmp13_ = _tmp12_;
		_tmp14_ = _data14_->now;
		_tmp15_ = g_date_time_to_string (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = self->priv->_etag;
		_tmp18_ = g_strdup_printf ("-backup-%s-%s.mind", _tmp16_, _tmp17_);
		_tmp19_ = _tmp18_;
		_tmp20_ = string_replace (_tmp13_, ".mind", _tmp19_);
		_tmp21_ = _tmp20_;
		_g_free0 (_tmp19_);
		_g_free0 (_tmp16_);
		fname = _tmp21_;
		_tmp22_ = fname;
		document_save_xml_internal (self, _tmp22_, FALSE);
		_tmp23_ = self->priv->_map;
		mind_map_initialize_for_open (_tmp23_);
		document_load (self, UPGRADE_ACTION_NUM, NULL, NULL);
		_g_free0 (fname);
	}
	_tmp24_ = _data12_->doc;
	_xmlFreeDoc0 (_tmp24_);
}

static void
_____lambda12__main_window_overwrite_func (gboolean overwrite,
                                           gpointer self)
{
	____lambda12_ (self, overwrite);
}

gboolean
document_save_xml (Document* self)
{
	Block12Data* _data12_;
	xmlDoc* _tmp0_;
	xmlDoc* _tmp1_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gboolean _tmp13_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_data12_ = g_slice_new0 (Block12Data);
	_data12_->_ref_count_ = 1;
	_data12_->self = g_object_ref (self);
	_tmp0_ = document_load_raw (self);
	_data12_->doc = _tmp0_;
	_tmp1_ = _data12_->doc;
	if (_tmp1_ != NULL) {
		Block13Data* _data13_;
		xmlDoc* _tmp2_;
		gchar* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		_data13_ = g_slice_new0 (Block13Data);
		_data13_->_ref_count_ = 1;
		_data13_->_data12_ = block12_data_ref (_data12_);
		_tmp2_ = _data12_->doc;
		_tmp3_ = document_get_etag (self, _tmp2_);
		_data13_->file_etag = _tmp3_;
		_tmp4_ = self->priv->_etag;
		_tmp5_ = _data13_->file_etag;
		if (g_strcmp0 (_tmp4_, _tmp5_) != 0) {
			Block14Data* _data14_;
			GDateTime* _tmp6_;
			MindMap* _tmp7_;
			MainWindow* _tmp8_;
			MainWindow* _tmp9_;
			MindMap* _tmp10_;
			_data14_ = g_slice_new0 (Block14Data);
			_data14_->_ref_count_ = 1;
			_data14_->_data13_ = block13_data_ref (_data13_);
			_tmp6_ = g_date_time_new_now_local ();
			_data14_->now = _tmp6_;
			_tmp7_ = self->priv->_map;
			_tmp8_ = mind_map_get_win (_tmp7_);
			_tmp9_ = _tmp8_;
			_tmp10_ = self->priv->_map;
			main_window_ask_modified_overwrite (_tmp9_, _tmp10_, _____lambda12__main_window_overwrite_func, _data14_);
			block14_data_unref (_data14_);
			_data14_ = NULL;
		}
		block13_data_unref (_data13_);
		_data13_ = NULL;
	}
	_tmp11_ = document_get_map_file (self);
	_tmp12_ = _tmp11_;
	_tmp13_ = document_save_xml_internal (self, _tmp12_, TRUE);
	_g_free0 (_tmp12_);
	result = _tmp13_;
	block12_data_unref (_data12_);
	_data12_ = NULL;
	return result;
}

static gboolean
document_save_xml_internal (Document* self,
                            const gchar* dest_filename,
                            gboolean bump_etag)
{
	xmlDoc* doc = NULL;
	xmlDoc* _tmp0_;
	xmlNode* root = NULL;
	xmlNode* _tmp1_;
	gchar* orig_etag = NULL;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	xmlNode* _tmp4_;
	const gchar* _tmp5_;
	xmlNode* _tmp9_;
	const gchar* _tmp10_;
	xmlDoc* _tmp11_;
	xmlNode* _tmp12_;
	MindMap* _tmp13_;
	MapModel* _tmp14_;
	MapModel* _tmp15_;
	xmlNode* _tmp16_;
	gint res = 0;
	xmlDoc* _tmp17_;
	xmlDoc* _tmp18_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (dest_filename != NULL, FALSE);
	_tmp0_ = xmlNewDoc ((xmlChar*) "1.0");
	doc = _tmp0_;
	_tmp1_ = xmlNewNode (NULL, (xmlChar*) "minder");
	root = _tmp1_;
	_tmp2_ = self->priv->_etag;
	_tmp3_ = g_strdup (_tmp2_);
	orig_etag = _tmp3_;
	_tmp4_ = root;
	_tmp5_ = minder_version;
	xmlSetProp (_tmp4_, (xmlChar*) "version", (xmlChar*) _tmp5_);
	if (bump_etag) {
		xmlNode* _tmp6_;
		const gchar* _tmp7_;
		gchar* _tmp8_;
		_tmp6_ = root;
		_tmp7_ = self->priv->_etag;
		xmlSetProp (_tmp6_, (xmlChar*) "parent-etag", (xmlChar*) _tmp7_);
		_tmp8_ = document_generate_etag (self);
		_g_free0 (self->priv->_etag);
		self->priv->_etag = _tmp8_;
	}
	_tmp9_ = root;
	_tmp10_ = self->priv->_etag;
	xmlSetProp (_tmp9_, (xmlChar*) "etag", (xmlChar*) _tmp10_);
	_tmp11_ = doc;
	_tmp12_ = root;
	xmlDocSetRootElement (_tmp11_, _tmp12_);
	_tmp13_ = self->priv->_map;
	_tmp14_ = mind_map_get_model (_tmp13_);
	_tmp15_ = _tmp14_;
	_tmp16_ = root;
	map_model_save (_tmp15_, _tmp16_);
	_tmp17_ = doc;
	res = xmlSaveFormatFile (dest_filename, _tmp17_, 1);
	_tmp18_ = doc;
	_xmlFreeDoc0 (_tmp18_);
	if (res < 0) {
		const gchar* _tmp19_;
		gchar* _tmp20_;
		_tmp19_ = orig_etag;
		_tmp20_ = g_strdup (_tmp19_);
		_g_free0 (self->priv->_etag);
		self->priv->_etag = _tmp20_;
		result = FALSE;
		_g_free0 (orig_etag);
		return result;
	}
	result = TRUE;
	_g_free0 (orig_etag);
	return result;
}

gboolean
document_copy_as_orig (Document* self)
{
	gchar* orig_file = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = document_get_orig_file (self);
	orig_file = _tmp0_;
	_tmp1_ = document_get_filename (self);
	_tmp2_ = _tmp1_;
	result = document_copy_file (self, _tmp2_, orig_file);
	_g_free0 (orig_file);
	return result;
}

gboolean
document_save (Document* self)
{
	gchar* bak_file = NULL;
	gchar* _tmp0_;
	gboolean backed = FALSE;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	struct archive* archive = NULL;
	struct archive* _tmp6_;
	struct archive* _tmp7_;
	struct archive* _tmp8_;
	struct archive* _tmp9_;
	const gchar* _tmp10_;
	const gchar* _tmp11_;
	struct archive* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	GArray* image_ids = NULL;
	MindMap* _tmp15_;
	ImageManager* _tmp16_;
	ImageManager* _tmp17_;
	GArray* _tmp18_;
	struct archive* _tmp29_;
	gboolean _tmp33_ = FALSE;
	gboolean upgrade_ro = FALSE;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = document_get_bak_file (self);
	bak_file = _tmp0_;
	backed = FALSE;
	_tmp1_ = document_get_filename (self);
	_tmp2_ = _tmp1_;
	if (g_file_test (_tmp2_, G_FILE_TEST_EXISTS)) {
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		_tmp3_ = document_get_filename (self);
		_tmp4_ = _tmp3_;
		_tmp5_ = bak_file;
		document_copy_file (self, _tmp4_, _tmp5_);
		backed = TRUE;
	}
	document_save_xml (self);
	_tmp6_ = archive_write_new ();
	archive = _tmp6_;
	_tmp7_ = archive;
	archive_write_add_filter_gzip (_tmp7_);
	_tmp8_ = archive;
	archive_write_set_format_pax_restricted (_tmp8_);
	_tmp9_ = archive;
	_tmp10_ = document_get_filename (self);
	_tmp11_ = _tmp10_;
	archive_write_open_filename (_tmp9_, _tmp11_);
	_tmp12_ = archive;
	_tmp13_ = document_get_map_file (self);
	_tmp14_ = _tmp13_;
	document_archive_file (self, _tmp12_, _tmp14_, NULL);
	_g_free0 (_tmp14_);
	_tmp15_ = self->priv->_map;
	_tmp16_ = mind_map_get_image_manager (_tmp15_);
	_tmp17_ = _tmp16_;
	_tmp18_ = image_manager_get_ids (_tmp17_);
	image_ids = _tmp18_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp19_ = FALSE;
			_tmp19_ = TRUE;
			while (TRUE) {
				GArray* _tmp21_;
				gint id = 0;
				GArray* _tmp22_;
				struct archive* _tmp23_;
				MindMap* _tmp24_;
				ImageManager* _tmp25_;
				ImageManager* _tmp26_;
				gchar* _tmp27_;
				gchar* _tmp28_;
				if (!_tmp19_) {
					gint _tmp20_;
					_tmp20_ = i;
					i = _tmp20_ + 1;
				}
				_tmp19_ = FALSE;
				_tmp21_ = image_ids;
				if (!(((guint) i) < _tmp21_->len)) {
					break;
				}
				_tmp22_ = image_ids;
				id = g_array_index (_tmp22_, gint, (guint) i);
				_tmp23_ = archive;
				_tmp24_ = self->priv->_map;
				_tmp25_ = mind_map_get_image_manager (_tmp24_);
				_tmp26_ = _tmp25_;
				_tmp27_ = image_manager_get_file (_tmp26_, id);
				_tmp28_ = _tmp27_;
				document_archive_file (self, _tmp23_, _tmp28_, &id);
				_g_free0 (_tmp28_);
			}
		}
	}
	_tmp29_ = archive;
	if (archive_write_close (_tmp29_) != ARCHIVE_OK) {
		struct archive* _tmp30_;
		const gchar* _tmp31_;
		struct archive* _tmp32_;
		_tmp30_ = archive;
		_tmp31_ = archive_error_string (_tmp30_);
		_tmp32_ = archive;
		g_error ("Document.vala:522: Error : %s (%d)", _tmp31_, archive_errno (_tmp32_));
	}
	if (backed) {
		MindMap* _tmp34_;
		GSettings* _tmp35_;
		GSettings* _tmp36_;
		_tmp34_ = self->priv->_map;
		_tmp35_ = mind_map_get_settings (_tmp34_);
		_tmp36_ = _tmp35_;
		_tmp33_ = !g_settings_get_boolean (_tmp36_, "keep-backup-after-save");
	} else {
		_tmp33_ = FALSE;
	}
	if (_tmp33_) {
		const gchar* _tmp37_;
		_tmp37_ = bak_file;
		g_unlink (_tmp37_);
	}
	upgrade_ro = self->priv->_upgrade_ro;
	self->priv->_save_needed = FALSE;
	self->priv->_upgrade_ro = FALSE;
	g_signal_emit (self, document_signals[DOCUMENT_READ_ONLY_CHANGED_SIGNAL], 0);
	g_signal_emit (self, document_signals[DOCUMENT_SAVE_CHANGED_SIGNAL], 0);
	result = TRUE;
	_g_array_unref0 (image_ids);
	_archive_write_free0 (archive);
	_g_free0 (bak_file);
	return result;
}

gboolean
document_archive_file (Document* self,
                       struct archive* archive,
                       const gchar* fname,
                       gint* image_id)
{
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (archive != NULL, FALSE);
	g_return_val_if_fail (fname != NULL, FALSE);
	{
		GFile* file = NULL;
		GFile* _tmp0_;
		GFileInfo* file_info = NULL;
		GFile* _tmp1_;
		GFileInfo* _tmp2_;
		GFileInputStream* input_stream = NULL;
		GFile* _tmp3_;
		GFileInputStream* _tmp4_;
		GDataInputStream* data_input_stream = NULL;
		GFileInputStream* _tmp5_;
		GDataInputStream* _tmp6_;
		struct archive_entry* entry = NULL;
		struct archive_entry* _tmp7_;
		struct archive_entry* _tmp8_;
		GFile* _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		struct archive_entry* _tmp12_;
		GFileInfo* _tmp13_;
		struct archive_entry* _tmp14_;
		struct archive_entry* _tmp15_;
		struct archive_entry* _tmp17_;
		gsize bytes_read = 0UL;
		guint8* buffer = NULL;
		guint8* _tmp22_;
		gint buffer_length1;
		gint _buffer_size_;
		_tmp0_ = g_file_new_for_path (fname);
		file = _tmp0_;
		_tmp1_ = file;
		_tmp2_ = g_file_query_info (_tmp1_, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
		file_info = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp3_ = file;
		_tmp4_ = g_file_read (_tmp3_, NULL, &_inner_error0_);
		input_stream = _tmp4_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file_info);
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp5_ = input_stream;
		_tmp6_ = g_data_input_stream_new ((GInputStream*) _tmp5_);
		data_input_stream = _tmp6_;
		_tmp7_ = archive_entry_new2 (NULL);
		entry = _tmp7_;
		_tmp8_ = entry;
		_tmp9_ = file;
		_tmp10_ = g_file_get_basename (_tmp9_);
		_tmp11_ = _tmp10_;
		archive_entry_set_pathname (_tmp8_, _tmp11_);
		_g_free0 (_tmp11_);
		_tmp12_ = entry;
		_tmp13_ = file_info;
		archive_entry_set_size (_tmp12_, (la_int64_t) g_file_info_get_size (_tmp13_));
		_tmp14_ = entry;
		archive_entry_set_filetype (_tmp14_, AE_IFREG);
		_tmp15_ = entry;
		archive_entry_set_perm (_tmp15_, (__LA_MODE_T) 0644);
		if (image_id != NULL) {
			struct archive_entry* _tmp16_;
			_tmp16_ = entry;
			archive_entry_xattr_add_entry (_tmp16_, "image_id", (void*) image_id, (gsize) sizeof (gint));
		}
		_tmp17_ = entry;
		if (archive_write_header (archive, _tmp17_) != ARCHIVE_OK) {
			GFile* _tmp18_;
			gchar* _tmp19_;
			gchar* _tmp20_;
			const gchar* _tmp21_;
			_tmp18_ = file;
			_tmp19_ = g_file_get_path (_tmp18_);
			_tmp20_ = _tmp19_;
			_tmp21_ = archive_error_string (archive);
			g_critical ("Document.vala:565: Error writing '%s': %s (%d)", _tmp20_, _tmp21_, archive_errno (archive));
			_g_free0 (_tmp20_);
			result = FALSE;
			_archive_entry_free0 (entry);
			_g_object_unref0 (data_input_stream);
			_g_object_unref0 (input_stream);
			_g_object_unref0 (file_info);
			_g_object_unref0 (file);
			return result;
		}
		_tmp22_ = g_new0 (guint8, 64);
		buffer = _tmp22_;
		buffer_length1 = 64;
		_buffer_size_ = buffer_length1;
		while (TRUE) {
			gboolean _tmp23_ = FALSE;
			GDataInputStream* _tmp24_;
			guint8* _tmp25_;
			gint _tmp25__length1;
			gsize _tmp26_ = 0UL;
			gboolean _tmp27_;
			guint8* _tmp28_;
			gint _tmp28__length1;
			_tmp24_ = data_input_stream;
			_tmp25_ = buffer;
			_tmp25__length1 = buffer_length1;
			_tmp27_ = g_input_stream_read_all ((GInputStream*) _tmp24_, _tmp25_, (gsize) _tmp25__length1, &_tmp26_, NULL, &_inner_error0_);
			bytes_read = _tmp26_;
			_tmp23_ = _tmp27_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				buffer = (g_free (buffer), NULL);
				_archive_entry_free0 (entry);
				_g_object_unref0 (data_input_stream);
				_g_object_unref0 (input_stream);
				_g_object_unref0 (file_info);
				_g_object_unref0 (file);
				goto __catch0_g_error;
			}
			if (!_tmp23_) {
				break;
			}
			if (bytes_read <= ((gsize) 0)) {
				break;
			}
			_tmp28_ = buffer;
			_tmp28__length1 = buffer_length1;
			archive_write_data (archive, _tmp28_, (size_t) _tmp28__length1);
		}
		buffer = (g_free (buffer), NULL);
		_archive_entry_free0 (entry);
		_g_object_unref0 (data_input_stream);
		_g_object_unref0 (input_stream);
		_g_object_unref0 (file_info);
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		FILE* _tmp29_;
		GError* _tmp30_;
		const gchar* _tmp31_;
		GError* _tmp32_;
		const gchar* _tmp33_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp29_ = stdout;
		_tmp30_ = e;
		_tmp31_ = _tmp30_->message;
		fprintf (_tmp29_, "ERROR archiving: %s\n", _tmp31_);
		_tmp32_ = e;
		_tmp33_ = _tmp32_->message;
		g_critical ("Document.vala:581: %s", _tmp33_);
		result = FALSE;
		_g_error_free0 (e);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp34_ = FALSE;
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return _tmp34_;
	}
	result = TRUE;
	return result;
}

static void
document_make_temp_dir (Document* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		gchar* _tmp0_ = NULL;
		gchar* _tmp1_;
		gchar* _tmp2_;
		gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp1_ = g_dir_make_tmp ("minder-XXXXXX", &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_free0 (self->priv->_temp_dir);
		self->priv->_temp_dir = _tmp2_;
		_tmp3_ = document_get_image_dir (self);
		_tmp4_ = _tmp3_;
		g_mkdir (_tmp4_, 0755);
		_g_free0 (_tmp4_);
		_g_free0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp5_;
		const gchar* _tmp6_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		g_critical ("Document.vala:597: %s", _tmp6_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static gboolean
document_move_file (Document* self,
                    const gchar* from,
                    const gchar* to)
{
	GFile* from_file = NULL;
	GFile* _tmp0_;
	GFile* to_file = NULL;
	GFile* _tmp1_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (from != NULL, FALSE);
	g_return_val_if_fail (to != NULL, FALSE);
	_tmp0_ = g_file_new_for_path (from);
	from_file = _tmp0_;
	_tmp1_ = g_file_new_for_path (to);
	to_file = _tmp1_;
	{
		GFile* _tmp2_;
		GFile* _tmp3_;
		_tmp2_ = from_file;
		_tmp3_ = to_file;
		g_file_move (_tmp2_, _tmp3_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_critical ("Document.vala:609: %s", _tmp5_);
		result = FALSE;
		_g_error_free0 (e);
		_g_object_unref0 (to_file);
		_g_object_unref0 (from_file);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp6_ = FALSE;
		_g_object_unref0 (to_file);
		_g_object_unref0 (from_file);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return _tmp6_;
	}
	result = TRUE;
	_g_object_unref0 (to_file);
	_g_object_unref0 (from_file);
	return result;
}

static gboolean
document_copy_file (Document* self,
                    const gchar* from,
                    const gchar* to)
{
	GFile* from_file = NULL;
	GFile* _tmp0_;
	GFile* to_file = NULL;
	GFile* _tmp1_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (from != NULL, FALSE);
	g_return_val_if_fail (to != NULL, FALSE);
	_tmp0_ = g_file_new_for_path (from);
	from_file = _tmp0_;
	_tmp1_ = g_file_new_for_path (to);
	to_file = _tmp1_;
	{
		GFile* _tmp2_;
		GFile* _tmp3_;
		_tmp2_ = from_file;
		_tmp3_ = to_file;
		g_file_copy (_tmp2_, _tmp3_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_critical ("Document.vala:623: %s", _tmp5_);
		result = FALSE;
		_g_error_free0 (e);
		_g_object_unref0 (to_file);
		_g_object_unref0 (from_file);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp6_ = FALSE;
		_g_object_unref0 (to_file);
		_g_object_unref0 (from_file);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return _tmp6_;
	}
	result = TRUE;
	_g_object_unref0 (to_file);
	_g_object_unref0 (from_file);
	return result;
}

static gchar*
document_get_map_file (Document* self)
{
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_temp_dir;
	_tmp1_ = g_build_filename (_tmp0_, "map.xml", NULL);
	result = _tmp1_;
	return result;
}

static gchar*
document_get_image_dir (Document* self)
{
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_temp_dir;
	_tmp1_ = g_build_filename (_tmp0_, "images", NULL);
	result = _tmp1_;
	return result;
}

static void
document_upgrade (Document* self,
                  UpgradeAction action,
                  AfterLoadFunc func,
                  gpointer func_target)
{
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gboolean _tmp4_;
	GArray* image_ids = NULL;
	MindMap* _tmp5_;
	ImageManager* _tmp6_;
	ImageManager* _tmp7_;
	GArray* _tmp8_;
	MindMap* _tmp25_;
	ImageManager* _tmp26_;
	ImageManager* _tmp27_;
	gchar* _tmp28_;
	gchar* _tmp29_;
	g_return_if_fail (self != NULL);
	_tmp0_ = document_get_filename (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = document_get_map_file (self);
	_tmp3_ = _tmp2_;
	_tmp4_ = !document_copy_file (self, _tmp1_, _tmp3_);
	_g_free0 (_tmp3_);
	if (_tmp4_) {
		if (func != NULL) {
			func (FALSE, "upgrade A", func_target);
		}
		return;
	}
	if (!document_load_xml (self)) {
		if (func != NULL) {
			func (FALSE, "upgrade B", func_target);
		}
		return;
	}
	_tmp5_ = self->priv->_map;
	_tmp6_ = mind_map_get_image_manager (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = image_manager_get_ids (_tmp7_);
	image_ids = _tmp8_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp9_ = FALSE;
			_tmp9_ = TRUE;
			while (TRUE) {
				GArray* _tmp11_;
				gint id = 0;
				GArray* _tmp12_;
				gchar* img_file = NULL;
				MindMap* _tmp13_;
				ImageManager* _tmp14_;
				ImageManager* _tmp15_;
				gchar* _tmp16_;
				const gchar* _tmp17_;
				gchar* _tmp18_;
				gchar* _tmp19_;
				const gchar* _tmp20_;
				gchar* _tmp21_;
				gchar* _tmp22_;
				gchar* _tmp23_;
				gchar* _tmp24_;
				if (!_tmp9_) {
					gint _tmp10_;
					_tmp10_ = i;
					i = _tmp10_ + 1;
				}
				_tmp9_ = FALSE;
				_tmp11_ = image_ids;
				if (!(((guint) i) < _tmp11_->len)) {
					break;
				}
				_tmp12_ = image_ids;
				id = g_array_index (_tmp12_, gint, (guint) i);
				_tmp13_ = self->priv->_map;
				_tmp14_ = mind_map_get_image_manager (_tmp13_);
				_tmp15_ = _tmp14_;
				_tmp16_ = image_manager_get_file (_tmp15_, id);
				img_file = _tmp16_;
				_tmp17_ = img_file;
				_tmp18_ = document_get_image_dir (self);
				_tmp19_ = _tmp18_;
				_tmp20_ = img_file;
				_tmp21_ = g_path_get_basename (_tmp20_);
				_tmp22_ = _tmp21_;
				_tmp23_ = g_build_filename (_tmp19_, _tmp22_, NULL);
				_tmp24_ = _tmp23_;
				document_copy_file (self, _tmp17_, _tmp24_);
				_g_free0 (_tmp24_);
				_g_free0 (_tmp22_);
				_g_free0 (_tmp19_);
				_g_free0 (img_file);
			}
		}
	}
	_tmp25_ = self->priv->_map;
	_tmp26_ = mind_map_get_image_manager (_tmp25_);
	_tmp27_ = _tmp26_;
	_tmp28_ = document_get_image_dir (self);
	_tmp29_ = _tmp28_;
	image_manager_set_image_dir (_tmp27_, _tmp29_);
	_g_free0 (_tmp29_);
	switch (action) {
		case UPGRADE_ACTION_OVERRIDE:
		{
			document_save (self);
			break;
		}
		case UPGRADE_ACTION_SAVE_ORIG:
		{
			document_copy_as_orig (self);
			document_save (self);
			break;
		}
		case UPGRADE_ACTION_READ_ONLY:
		{
			MindMap* _tmp30_;
			MindMap* _tmp31_;
			self->priv->_upgrade_ro = TRUE;
			_tmp30_ = self->priv->_map;
			_tmp31_ = self->priv->_map;
			g_signal_emit_by_name (_tmp30_, "editable-changed", _tmp31_);
			break;
		}
		default:
		break;
	}
	if (func != NULL) {
		func (TRUE, "upgrade C", func_target);
	}
	_g_array_unref0 (image_ids);
}

static Block15Data*
block15_data_ref (Block15Data* _data15_)
{
	g_atomic_int_inc (&_data15_->_ref_count_);
	return _data15_;
}

static void
block15_data_unref (void * _userdata_)
{
	Block15Data* _data15_;
	_data15_ = (Block15Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data15_->_ref_count_)) {
		Document* self;
		self = _data15_->self;
		_g_object_unref0 (_data15_->remember);
		_g_object_unref0 (_data15_->options);
		_g_object_unref0 (_data15_->dialog);
		_g_object_unref0 (self);
		g_slice_free (Block15Data, _data15_);
	}
}

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

static void
__lambda31_ (Block15Data* _data15_,
             gint id)
{
	Document* self;
	self = _data15_->self;
	if (id == ((gint) GTK_RESPONSE_APPLY)) {
		UpgradeAction action = 0;
		guint _tmp0_;
		guint _tmp1_;
		MindMap* _tmp2_;
		GSettings* _tmp3_;
		GSettings* _tmp4_;
		MindMap* _tmp5_;
		GSettings* _tmp6_;
		GSettings* _tmp7_;
		gboolean _tmp8_;
		gboolean _tmp9_;
		_tmp0_ = gtk_drop_down_get_selected (_data15_->options);
		_tmp1_ = _tmp0_;
		action = (UpgradeAction) _tmp1_;
		_tmp2_ = self->priv->_map;
		_tmp3_ = mind_map_get_settings (_tmp2_);
		_tmp4_ = _tmp3_;
		g_settings_set_int (_tmp4_, "upgrade-action", (gint) action);
		_tmp5_ = self->priv->_map;
		_tmp6_ = mind_map_get_settings (_tmp5_);
		_tmp7_ = _tmp6_;
		_tmp8_ = gtk_check_button_get_active (_data15_->remember);
		_tmp9_ = _tmp8_;
		g_settings_set_boolean (_tmp7_, "ask-for-upgrade-action", !_tmp9_);
		document_upgrade (self, action, _data15_->func, _data15_->func_target);
	} else {
		gboolean _tmp10_ = FALSE;
		if (id == ((gint) GTK_RESPONSE_CANCEL)) {
			_tmp10_ = _data15_->func != NULL;
		} else {
			_tmp10_ = FALSE;
		}
		if (_tmp10_) {
			_data15_->func (FALSE, "request_upgrade_action", _data15_->func_target);
		}
	}
	g_signal_emit_by_name ((GtkDialog*) _data15_->dialog, "close");
}

static void
___lambda31__gtk_dialog_response (GtkDialog* _sender,
                                  gint response_id,
                                  gpointer self)
{
	__lambda31_ (self, response_id);
}

static void
document_request_upgrade_action (Document* self,
                                 AfterLoadFunc func,
                                 gpointer func_target)
{
	Block15Data* _data15_;
	GraniteMessageDialog* _tmp0_;
	GtkButton* cancel = NULL;
	GtkButton* _tmp1_;
	GtkButton* apply = NULL;
	GtkButton* _tmp2_;
	gint _tmp3_ = 0;
	gchar** _tmp4_;
	gchar** _tmp5_;
	gint _tmp5__length1;
	MindMap* _tmp6_;
	GSettings* _tmp7_;
	GSettings* _tmp8_;
	GtkDropDown* _tmp9_ = NULL;
	GtkDropDown* _tmp10_;
	GtkCheckButton* _tmp11_;
	GtkLabel* rem_description = NULL;
	GtkLabel* _tmp12_ = NULL;
	GtkLabel* rem_info = NULL;
	GtkLabel* _tmp13_ = NULL;
	GtkGrid* rem_grid = NULL;
	GtkGrid* _tmp14_ = NULL;
	GtkBox* box = NULL;
	GtkBox* _tmp15_;
	GtkBox* _tmp16_;
	MindMap* _tmp17_;
	MainWindow* _tmp18_;
	MainWindow* _tmp19_;
	g_return_if_fail (self != NULL);
	_data15_ = g_slice_new0 (Block15Data);
	_data15_->_ref_count_ = 1;
	_data15_->self = g_object_ref (self);
	_data15_->func = func;
	_data15_->func_target = func_target;
	_tmp0_ = granite_message_dialog_new_with_image_from_icon_name (_ ("Upgrade?"), _ ("This file is from an older version Minder and needs to be upgraded to " \
"edit.\n" \
"\n" \
"Note: Upgraded files cannot be viewed/edited with older versions of Mi" \
"nder."), "system-software-update", GTK_BUTTONS_NONE);
	g_object_ref_sink (_tmp0_);
	_data15_->dialog = _tmp0_;
	_tmp1_ = (GtkButton*) gtk_button_new_with_label (_ ("Cancel"));
	g_object_ref_sink (_tmp1_);
	cancel = _tmp1_;
	gtk_dialog_add_action_widget ((GtkDialog*) _data15_->dialog, (GtkWidget*) cancel, (gint) GTK_RESPONSE_CANCEL);
	_tmp2_ = (GtkButton*) gtk_button_new_with_label (_ ("Apply"));
	g_object_ref_sink (_tmp2_);
	apply = _tmp2_;
	gtk_dialog_add_action_widget ((GtkDialog*) _data15_->dialog, (GtkWidget*) apply, (gint) GTK_RESPONSE_APPLY);
	_tmp4_ = upgrade_action_labels (&_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp5__length1 = _tmp3_;
	_tmp6_ = self->priv->_map;
	_tmp7_ = mind_map_get_settings (_tmp6_);
	_tmp8_ = _tmp7_;
	_tmp9_ = (GtkDropDown*) gtk_drop_down_new_from_strings (_tmp5_);
	gtk_widget_set_halign ((GtkWidget*) _tmp9_, GTK_ALIGN_START);
	gtk_widget_set_margin_top ((GtkWidget*) _tmp9_, 10);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp9_, 20);
	gtk_drop_down_set_selected (_tmp9_, (guint) g_settings_get_int (_tmp8_, "upgrade-action"));
	g_object_ref_sink (_tmp9_);
	_tmp10_ = _tmp9_;
	_tmp5_ = (_vala_array_free (_tmp5_, _tmp5__length1, (GDestroyNotify) g_free), NULL);
	_data15_->options = _tmp10_;
	_tmp11_ = (GtkCheckButton*) gtk_check_button_new ();
	g_object_ref_sink (_tmp11_);
	_data15_->remember = _tmp11_;
	_tmp12_ = (GtkLabel*) gtk_label_new (_ ("Don't show this dialog again"));
	gtk_widget_set_halign ((GtkWidget*) _tmp12_, GTK_ALIGN_START);
	g_object_ref_sink (_tmp12_);
	rem_description = _tmp12_;
	_tmp13_ = (GtkLabel*) gtk_label_new (_ ("<small>This can be changed in preferences</small>"));
	gtk_widget_set_halign ((GtkWidget*) _tmp13_, GTK_ALIGN_START);
	gtk_label_set_use_markup (_tmp13_, TRUE);
	g_object_ref_sink (_tmp13_);
	rem_info = _tmp13_;
	_tmp14_ = (GtkGrid*) gtk_grid_new ();
	gtk_grid_set_row_spacing (_tmp14_, 5);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp14_, 20);
	gtk_widget_set_margin_top ((GtkWidget*) _tmp14_, 20);
	g_object_ref_sink (_tmp14_);
	rem_grid = _tmp14_;
	gtk_grid_attach (rem_grid, (GtkWidget*) _data15_->remember, 0, 0, 1, 1);
	gtk_grid_attach (rem_grid, (GtkWidget*) rem_description, 1, 0, 1, 1);
	gtk_grid_attach (rem_grid, (GtkWidget*) rem_info, 1, 1, 1, 1);
	_tmp15_ = gtk_dialog_get_content_area ((GtkDialog*) _data15_->dialog);
	_tmp16_ = _g_object_ref0 (_tmp15_);
	box = _tmp16_;
	gtk_box_append (box, (GtkWidget*) _data15_->options);
	gtk_box_append (box, (GtkWidget*) rem_grid);
	_tmp17_ = self->priv->_map;
	_tmp18_ = mind_map_get_win (_tmp17_);
	_tmp19_ = _tmp18_;
	gtk_window_set_transient_for ((GtkWindow*) _data15_->dialog, (GtkWindow*) _tmp19_);
	gtk_window_set_modal ((GtkWindow*) _data15_->dialog, TRUE);
	gtk_dialog_set_default_response ((GtkDialog*) _data15_->dialog, (gint) GTK_RESPONSE_APPLY);
	gtk_window_set_title ((GtkWindow*) _data15_->dialog, _ ("Upgrade Needed"));
	g_signal_connect_data ((GtkDialog*) _data15_->dialog, "response", (GCallback) ___lambda31__gtk_dialog_response, block15_data_ref (_data15_), (GClosureNotify) block15_data_unref, 0);
	gtk_window_present ((GtkWindow*) _data15_->dialog);
	_g_object_unref0 (box);
	_g_object_unref0 (rem_grid);
	_g_object_unref0 (rem_info);
	_g_object_unref0 (rem_description);
	_g_object_unref0 (apply);
	_g_object_unref0 (cancel);
	block15_data_unref (_data15_);
	_data15_ = NULL;
}

void
document_auto_save (Document* self)
{
	g_return_if_fail (self != NULL);
}

gboolean
document_remove (Document* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	if (!self->priv->_from_user) {
		const gchar* _tmp0_;
		_tmp0_ = self->priv->_filename;
		g_unlink (_tmp0_);
	}
	result = TRUE;
	return result;
}

static void
document_delete_recursively (Document* self,
                             GFile* file,
                             GError** error)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (file != NULL);
	{
		GFileEnumerator* enumerator = NULL;
		GFileEnumerator* _tmp0_;
		GFileInfo* info = NULL;
		_tmp0_ = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &_inner_error0_);
		enumerator = _tmp0_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		while (TRUE) {
			GFileInfo* _tmp1_ = NULL;
			GFileEnumerator* _tmp2_;
			GFileInfo* _tmp3_;
			GFileInfo* _tmp4_;
			GFileInfo* _tmp5_;
			GFile* child = NULL;
			GFileInfo* _tmp6_;
			const gchar* _tmp7_;
			GFile* _tmp8_;
			GFile* _tmp9_;
			_tmp2_ = enumerator;
			_tmp3_ = g_file_enumerator_next_file (_tmp2_, NULL, &_inner_error0_);
			_tmp1_ = _tmp3_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (info);
				_g_object_unref0 (enumerator);
				goto __catch0_g_error;
			}
			_tmp4_ = _tmp1_;
			_tmp1_ = NULL;
			_g_object_unref0 (info);
			info = _tmp4_;
			_tmp5_ = info;
			if (!(_tmp5_ != NULL)) {
				_g_object_unref0 (_tmp1_);
				break;
			}
			_tmp6_ = info;
			_tmp7_ = g_file_info_get_name (_tmp6_);
			_tmp8_ = g_file_get_child (file, _tmp7_);
			child = _tmp8_;
			_tmp9_ = child;
			document_delete_recursively (self, _tmp9_, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (child);
				_g_object_unref0 (_tmp1_);
				_g_object_unref0 (info);
				_g_object_unref0 (enumerator);
				goto __catch0_g_error;
			}
			_g_object_unref0 (child);
			_g_object_unref0 (_tmp1_);
		}
		_g_object_unref0 (info);
		_g_object_unref0 (enumerator);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	g_file_delete (file, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
}

void
document_cleanup (Document* self)
{
	GFile* dir = NULL;
	const gchar* _tmp0_;
	GFile* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	if (self->priv->_save_needed) {
		document_save (self);
	}
	_tmp0_ = self->priv->_temp_dir;
	_tmp1_ = g_file_new_for_path (_tmp0_);
	dir = _tmp1_;
	document_delete_recursively (self, dir, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (dir);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (dir);
}

const gchar*
document_get_filename (Document* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_filename;
	result = _tmp0_;
	return result;
}

void
document_set_filename (Document* self,
                       const gchar* value)
{
	const gchar* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_filename;
	if (g_strcmp0 (_tmp0_, value) != 0) {
		gchar* _tmp2_;
		if (!self->priv->_from_user) {
			const gchar* _tmp1_;
			_tmp1_ = self->priv->_filename;
			g_unlink (_tmp1_);
		}
		_tmp2_ = g_strdup (value);
		_g_free0 (self->priv->_filename);
		self->priv->_filename = _tmp2_;
		self->priv->_from_user = TRUE;
	}
	g_object_notify_by_pspec ((GObject *) self, document_properties[DOCUMENT_FILENAME_PROPERTY]);
}

const gchar*
document_get_temp_dir (Document* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_temp_dir;
	result = _tmp0_;
	return result;
}

const gchar*
document_get_load_version (Document* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_load_version;
	result = _tmp0_;
	return result;
}

gchar*
document_get_label (Document* self)
{
	gchar* result;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_filename;
	_tmp1_ = g_path_get_basename (_tmp0_);
	result = _tmp1_;
	return result;
}

gboolean
document_get_save_needed (Document* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_save_needed;
	return result;
}

gboolean
document_get_read_only (Document* self)
{
	gboolean result;
	gboolean prev_read_only = FALSE;
	const gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gboolean _tmp4_ = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	prev_read_only = self->priv->_read_only;
	_tmp0_ = self->priv->_filename;
	self->priv->_read_only = utils_is_read_only (_tmp0_);
	if (self->priv->_save_needed) {
		_tmp3_ = prev_read_only;
	} else {
		_tmp3_ = FALSE;
	}
	if (_tmp3_) {
		_tmp2_ = !self->priv->_read_only;
	} else {
		_tmp2_ = FALSE;
	}
	if (_tmp2_) {
		_tmp1_ = !self->priv->_upgrade_ro;
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		document_save (self);
	}
	if (self->priv->_read_only) {
		_tmp4_ = TRUE;
	} else {
		_tmp4_ = self->priv->_upgrade_ro;
	}
	result = _tmp4_;
	return result;
}

static void
document_class_init (DocumentClass * klass,
                     gpointer klass_data)
{
	document_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &Document_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_document_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_document_set_property;
	G_OBJECT_CLASS (klass)->finalize = document_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_FILENAME_PROPERTY, document_properties[DOCUMENT_FILENAME_PROPERTY] = g_param_spec_string ("filename", "filename", "filename", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_TEMP_DIR_PROPERTY, document_properties[DOCUMENT_TEMP_DIR_PROPERTY] = g_param_spec_string ("temp-dir", "temp-dir", "temp-dir", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_LOAD_VERSION_PROPERTY, document_properties[DOCUMENT_LOAD_VERSION_PROPERTY] = g_param_spec_string ("load-version", "load-version", "load-version", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_LABEL_PROPERTY, document_properties[DOCUMENT_LABEL_PROPERTY] = g_param_spec_string ("label", "label", "label", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_SAVE_NEEDED_PROPERTY, document_properties[DOCUMENT_SAVE_NEEDED_PROPERTY] = g_param_spec_boolean ("save-needed", "save-needed", "save-needed", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_READ_ONLY_PROPERTY, document_properties[DOCUMENT_READ_ONLY_PROPERTY] = g_param_spec_boolean ("read-only", "read-only", "read-only", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	document_signals[DOCUMENT_SAVE_CHANGED_SIGNAL] = g_signal_new ("save-changed", TYPE_DOCUMENT, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	document_signals[DOCUMENT_READ_ONLY_CHANGED_SIGNAL] = g_signal_new ("read-only-changed", TYPE_DOCUMENT, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
document_instance_init (Document * self,
                        gpointer klass)
{
	gchar* _tmp0_;
	self->priv = document_get_instance_private (self);
	self->priv->_upgrade_ro = FALSE;
	self->priv->_save_needed = FALSE;
	self->priv->_read_only = FALSE;
	_tmp0_ = g_strdup ("");
	self->priv->_load_version = _tmp0_;
}

static void
document_finalize (GObject * obj)
{
	Document * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_DOCUMENT, Document);
	_mind_map_unref0 (self->priv->_map);
	_g_free0 (self->priv->_filename);
	_g_free0 (self->priv->_temp_dir);
	_g_free0 (self->priv->_etag);
	_g_free0 (self->priv->_load_version);
	G_OBJECT_CLASS (document_parent_class)->finalize (obj);
}

static GType
document_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DocumentClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) document_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Document), 0, (GInstanceInitFunc) document_instance_init, NULL };
	GType document_type_id;
	document_type_id = g_type_register_static (G_TYPE_OBJECT, "Document", &g_define_type_info, 0);
	Document_private_offset = g_type_add_instance_private (document_type_id, sizeof (DocumentPrivate));
	return document_type_id;
}

GType
document_get_type (void)
{
	static volatile gsize document_type_id__once = 0;
	if (g_once_init_enter (&document_type_id__once)) {
		GType document_type_id;
		document_type_id = document_get_type_once ();
		g_once_init_leave (&document_type_id__once, document_type_id);
	}
	return document_type_id__once;
}

static void
_vala_document_get_property (GObject * object,
                             guint property_id,
                             GValue * value,
                             GParamSpec * pspec)
{
	Document * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_DOCUMENT, Document);
	switch (property_id) {
		case DOCUMENT_FILENAME_PROPERTY:
		g_value_set_string (value, document_get_filename (self));
		break;
		case DOCUMENT_TEMP_DIR_PROPERTY:
		g_value_set_string (value, document_get_temp_dir (self));
		break;
		case DOCUMENT_LOAD_VERSION_PROPERTY:
		g_value_set_string (value, document_get_load_version (self));
		break;
		case DOCUMENT_LABEL_PROPERTY:
		g_value_take_string (value, document_get_label (self));
		break;
		case DOCUMENT_SAVE_NEEDED_PROPERTY:
		g_value_set_boolean (value, document_get_save_needed (self));
		break;
		case DOCUMENT_READ_ONLY_PROPERTY:
		g_value_set_boolean (value, document_get_read_only (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_document_set_property (GObject * object,
                             guint property_id,
                             const GValue * value,
                             GParamSpec * pspec)
{
	Document * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_DOCUMENT, Document);
	switch (property_id) {
		case DOCUMENT_FILENAME_PROPERTY:
		document_set_filename (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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 inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

