/* vtgvcsbackendsbzr.c generated by valac 0.12.1.1-323029-dirty, the Vala compiler
 * generated from vtgvcsbackendsbzr.vala, do not modify */

/*
 *  vtgvcsbackendsbzr.vala - Vala developer toys for GEdit
 *  
 *  Copyright (C) 2008 - Andrea Del Signore <sejerpz@tin.it>
 *  
 *  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., 59 Temple Place, Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <valagee.h>
#include <sys/wait.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>


#define VTG_VCS_BACKENDS_TYPE_VCS_BASE (vtg_vcs_backends_vcs_base_get_type ())
#define VTG_VCS_BACKENDS_VCS_BASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBase))
#define VTG_VCS_BACKENDS_VCS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBaseClass))
#define VTG_VCS_BACKENDS_IS_VCS_BASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE))
#define VTG_VCS_BACKENDS_IS_VCS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_VCS_BASE))
#define VTG_VCS_BACKENDS_VCS_BASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBaseClass))

typedef struct _VtgVcsBackendsVcsBase VtgVcsBackendsVcsBase;
typedef struct _VtgVcsBackendsVcsBaseClass VtgVcsBackendsVcsBaseClass;
typedef struct _VtgVcsBackendsVcsBasePrivate VtgVcsBackendsVcsBasePrivate;

#define VTG_VCS_BACKENDS_TYPE_ITEM (vtg_vcs_backends_item_get_type ())
#define VTG_VCS_BACKENDS_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItem))
#define VTG_VCS_BACKENDS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItemClass))
#define VTG_VCS_BACKENDS_IS_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_ITEM))
#define VTG_VCS_BACKENDS_IS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_ITEM))
#define VTG_VCS_BACKENDS_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItemClass))

typedef struct _VtgVcsBackendsItem VtgVcsBackendsItem;
typedef struct _VtgVcsBackendsItemClass VtgVcsBackendsItemClass;

#define VTG_VCS_BACKENDS_TYPE_BZR (vtg_vcs_backends_bzr_get_type ())
#define VTG_VCS_BACKENDS_BZR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_BZR, VtgVcsBackendsBzr))
#define VTG_VCS_BACKENDS_BZR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_BZR, VtgVcsBackendsBzrClass))
#define VTG_VCS_BACKENDS_IS_BZR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_BZR))
#define VTG_VCS_BACKENDS_IS_BZR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_BZR))
#define VTG_VCS_BACKENDS_BZR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_BZR, VtgVcsBackendsBzrClass))

typedef struct _VtgVcsBackendsBzr VtgVcsBackendsBzr;
typedef struct _VtgVcsBackendsBzrClass VtgVcsBackendsBzrClass;
typedef struct _VtgVcsBackendsBzrPrivate VtgVcsBackendsBzrPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _vala_iterable_unref0(var) ((var == NULL) ? NULL : (var = (vala_iterable_unref (var), NULL)))

#define VTG_VCS_BACKENDS_TYPE_STATES (vtg_vcs_backends_states_get_type ())
typedef struct _VtgVcsBackendsItemPrivate VtgVcsBackendsItemPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _VtgVcsBackendsVcsBase {
	GObject parent_instance;
	VtgVcsBackendsVcsBasePrivate * priv;
};

struct _VtgVcsBackendsVcsBaseClass {
	GObjectClass parent_class;
	ValaList* (*get_items) (VtgVcsBackendsVcsBase* self, const gchar* path, GError** error);
	gboolean (*test) (VtgVcsBackendsVcsBase* self, const gchar* path);
};

struct _VtgVcsBackendsBzr {
	VtgVcsBackendsVcsBase parent_instance;
	VtgVcsBackendsBzrPrivate * priv;
};

struct _VtgVcsBackendsBzrClass {
	VtgVcsBackendsVcsBaseClass parent_class;
};

typedef enum  {
	VTG_VCS_BACKENDS_STATES_UNTRACKED,
	VTG_VCS_BACKENDS_STATES_ADDED,
	VTG_VCS_BACKENDS_STATES_MODIFIED,
	VTG_VCS_BACKENDS_STATES_DELETED
} VtgVcsBackendsStates;

struct _VtgVcsBackendsItem {
	GObject parent_instance;
	VtgVcsBackendsItemPrivate * priv;
	gchar* name;
	VtgVcsBackendsStates state;
};

struct _VtgVcsBackendsItemClass {
	GObjectClass parent_class;
};

typedef enum  {
	VTG_VCS_BACKENDS_VCS_ERROR_CommandFailed
} VtgVcsBackendsVcsError;
#define VTG_VCS_BACKENDS_VCS_ERROR vtg_vcs_backends_vcs_error_quark ()

static gpointer vtg_vcs_backends_bzr_parent_class = NULL;
static GType vtg_vcs_backends_bzr_type_id = 0;

GType vtg_vcs_backends_vcs_base_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_vcs_base_register_type (GTypeModule * module);
GType vtg_vcs_backends_item_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_item_register_type (GTypeModule * module);
GType vtg_vcs_backends_bzr_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_bzr_register_type (GTypeModule * module);
enum  {
	VTG_VCS_BACKENDS_BZR_DUMMY_PROPERTY
};
VtgVcsBackendsBzr* vtg_vcs_backends_bzr_new (void);
VtgVcsBackendsBzr* vtg_vcs_backends_bzr_construct (GType object_type);
VtgVcsBackendsVcsBase* vtg_vcs_backends_vcs_base_construct (GType object_type);
static ValaList* vtg_vcs_backends_bzr_real_get_items (VtgVcsBackendsVcsBase* base, const gchar* path, GError** error);
VtgVcsBackendsItem* vtg_vcs_backends_item_new (void);
VtgVcsBackendsItem* vtg_vcs_backends_item_construct (GType object_type);
GType vtg_vcs_backends_states_get_type (void) G_GNUC_CONST;
GQuark vtg_vcs_backends_vcs_error_quark (void);
static gboolean vtg_vcs_backends_bzr_real_test (VtgVcsBackendsVcsBase* base, const gchar* path);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);


VtgVcsBackendsBzr* vtg_vcs_backends_bzr_construct (GType object_type) {
	VtgVcsBackendsBzr * self = NULL;
	self = (VtgVcsBackendsBzr*) vtg_vcs_backends_vcs_base_construct (object_type);
	return self;
}


VtgVcsBackendsBzr* vtg_vcs_backends_bzr_new (void) {
	return vtg_vcs_backends_bzr_construct (VTG_VCS_BACKENDS_TYPE_BZR);
}


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


static gchar* string_substring (const gchar* self, glong offset, glong len) {
	gchar* result = NULL;
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= 0) {
		_tmp0_ = len >= 0;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		glong _tmp1_;
		_tmp1_ = string_strnlen ((gchar*) self, offset + len);
		string_length = _tmp1_;
	} else {
		gint _tmp2_;
		_tmp2_ = strlen (self);
		string_length = (glong) _tmp2_;
	}
	if (offset < 0) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= 0, NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < 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 ValaList* vtg_vcs_backends_bzr_real_get_items (VtgVcsBackendsVcsBase* base, const gchar* path, GError** error) {
	VtgVcsBackendsBzr * self;
	ValaList* result = NULL;
	ValaArrayList* _tmp0_ = NULL;
	ValaList* results;
	gchar* stdo = NULL;
	gchar* stde = NULL;
	gint exit_status = 0;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar** _tmp3_ = NULL;
	gchar** _tmp4_;
	gint _tmp4__length1;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_ = NULL;
	gint _tmp7_;
	gboolean _tmp8_;
	gboolean _tmp9_;
	gboolean _tmp10_;
	GError * _inner_error_ = NULL;
	self = (VtgVcsBackendsBzr*) base;
	g_return_val_if_fail (path != NULL, NULL);
	_tmp0_ = vala_array_list_new (VTG_VCS_BACKENDS_TYPE_ITEM, (GBoxedCopyFunc) g_object_ref, g_object_unref, g_direct_equal);
	results = (ValaList*) _tmp0_;
	_tmp1_ = g_strdup ("bzr");
	_tmp2_ = g_strdup ("status");
	_tmp3_ = g_new0 (gchar*, 2 + 1);
	_tmp3_[0] = _tmp1_;
	_tmp3_[1] = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp4__length1 = 2;
	_tmp8_ = g_spawn_sync (path, _tmp4_, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp5_, &_tmp6_, &_tmp7_, &_inner_error_);
	_g_free0 (stdo);
	stdo = _tmp5_;
	_g_free0 (stde);
	stde = _tmp6_;
	exit_status = _tmp7_;
	_tmp9_ = _tmp8_;
	_tmp4_ = (_vala_array_free (_tmp4_, _tmp4__length1, (GDestroyNotify) g_free), NULL);
	_tmp10_ = _tmp9_;
	if (_inner_error_ != NULL) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (stde);
		_g_free0 (stdo);
		_vala_iterable_unref0 (results);
		return NULL;
	}
	if (_tmp10_) {
		gint _tmp11_;
		gint exit;
		_tmp11_ = WEXITSTATUS (exit_status);
		exit = _tmp11_;
		if (exit == 0) {
			gchar** _tmp12_;
			gchar** _tmp13_ = NULL;
			gchar** lines;
			gint lines_length1;
			gint _lines_size_;
			gint idx;
			gint status;
			_tmp13_ = _tmp12_ = g_strsplit (stdo, "\n", 0);
			lines = _tmp13_;
			lines_length1 = _vala_array_length (_tmp12_);
			_lines_size_ = _vala_array_length (_tmp12_);
			idx = 0;
			status = 0;
			while (TRUE) {
				gchar* _tmp14_;
				gchar* line;
				gboolean _tmp15_;
				if (!(lines[idx] != NULL)) {
					break;
				}
				_tmp14_ = g_strdup (lines[idx]);
				line = _tmp14_;
				_tmp15_ = g_str_has_prefix (line, "added:");
				if (_tmp15_) {
					status = 1;
				} else {
					gboolean _tmp16_;
					_tmp16_ = g_str_has_prefix (line, "modified:");
					if (_tmp16_) {
						status = 2;
					} else {
						gboolean _tmp17_;
						_tmp17_ = g_str_has_prefix (line, "  ");
						if (!_tmp17_) {
							status = 0;
						} else {
							gboolean _tmp18_;
							_tmp18_ = g_str_has_prefix (line, "  ");
							if (_tmp18_) {
								switch (status) {
									case 1:
									case 2:
									{
										VtgVcsBackendsItem* _tmp19_ = NULL;
										VtgVcsBackendsItem* item;
										VtgVcsBackendsStates _tmp20_ = 0;
										gint _tmp21_;
										gchar* _tmp22_ = NULL;
										_tmp19_ = vtg_vcs_backends_item_new ();
										item = _tmp19_;
										if (status == 1) {
											_tmp20_ = VTG_VCS_BACKENDS_STATES_ADDED;
										} else {
											_tmp20_ = VTG_VCS_BACKENDS_STATES_MODIFIED;
										}
										item->state = _tmp20_;
										_tmp21_ = strlen (line);
										_tmp22_ = string_substring (line, (glong) 2, (glong) (_tmp21_ - 2));
										_g_free0 (item->name);
										item->name = _tmp22_;
										vala_collection_add ((ValaCollection*) results, item);
										_g_object_unref0 (item);
										break;
									}
									default:
									break;
								}
							}
						}
					}
				}
				idx++;
				_g_free0 (line);
			}
			lines = (_vala_array_free (lines, lines_length1, (GDestroyNotify) g_free), NULL);
		} else {
			const gchar* _tmp23_ = NULL;
			gchar* _tmp24_ = NULL;
			gchar* _tmp25_;
			GError* _tmp26_ = NULL;
			GError* _tmp27_;
			_tmp23_ = _ ("error executing the bzr status command.\n%s");
			_tmp24_ = g_strdup_printf (_tmp23_, stde);
			_tmp25_ = _tmp24_;
			_tmp26_ = g_error_new_literal (VTG_VCS_BACKENDS_VCS_ERROR, VTG_VCS_BACKENDS_VCS_ERROR_CommandFailed, _tmp25_);
			_tmp27_ = _tmp26_;
			_g_free0 (_tmp25_);
			_inner_error_ = _tmp27_;
			g_propagate_error (error, _inner_error_);
			_g_free0 (stde);
			_g_free0 (stdo);
			_vala_iterable_unref0 (results);
			return NULL;
		}
	}
	result = results;
	_g_free0 (stde);
	_g_free0 (stdo);
	return result;
}


static gboolean vtg_vcs_backends_bzr_real_test (VtgVcsBackendsVcsBase* base, const gchar* path) {
	VtgVcsBackendsBzr * self;
	gboolean result = FALSE;
	gchar* _tmp0_ = NULL;
	gchar* bzr_dir;
	gboolean _tmp1_;
	self = (VtgVcsBackendsBzr*) base;
	g_return_val_if_fail (path != NULL, FALSE);
	_tmp0_ = g_build_filename (path, ".bzr", NULL);
	bzr_dir = _tmp0_;
	_tmp1_ = g_file_test (bzr_dir, G_FILE_TEST_IS_DIR);
	if (_tmp1_) {
		result = TRUE;
		_g_free0 (bzr_dir);
		return result;
	}
	result = FALSE;
	_g_free0 (bzr_dir);
	return result;
}


static void vtg_vcs_backends_bzr_class_init (VtgVcsBackendsBzrClass * klass) {
	vtg_vcs_backends_bzr_parent_class = g_type_class_peek_parent (klass);
	VTG_VCS_BACKENDS_VCS_BASE_CLASS (klass)->get_items = vtg_vcs_backends_bzr_real_get_items;
	VTG_VCS_BACKENDS_VCS_BASE_CLASS (klass)->test = vtg_vcs_backends_bzr_real_test;
}


static void vtg_vcs_backends_bzr_instance_init (VtgVcsBackendsBzr * self) {
}


GType vtg_vcs_backends_bzr_get_type (void) {
	return vtg_vcs_backends_bzr_type_id;
}


GType vtg_vcs_backends_bzr_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (VtgVcsBackendsBzrClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vtg_vcs_backends_bzr_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (VtgVcsBackendsBzr), 0, (GInstanceInitFunc) vtg_vcs_backends_bzr_instance_init, NULL };
	vtg_vcs_backends_bzr_type_id = g_type_module_register_type (module, VTG_VCS_BACKENDS_TYPE_VCS_BASE, "VtgVcsBackendsBzr", &g_define_type_info, 0);
	return vtg_vcs_backends_bzr_type_id;
}


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


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


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



