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

/*

this is a part of vattery
copyright 2011 jezra lickter http://www.jezra.net

vattery is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

vattery 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 vattery.	If not, see <http://www.gnu.org/licenses/>.

*/

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <glib/gstdio.h>
#include <gobject/gvaluecollector.h>


#define TYPE_BATTERY_DATA_TYPE (battery_data_type_get_type ())

#define TYPE_BATTERY_DATA (battery_data_get_type ())
typedef struct _BatteryData BatteryData;
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_BATTERY (battery_get_type ())
#define BATTERY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_BATTERY, Battery))
#define BATTERY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_BATTERY, BatteryClass))
#define IS_BATTERY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_BATTERY))
#define IS_BATTERY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_BATTERY))
#define BATTERY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_BATTERY, BatteryClass))

typedef struct _Battery Battery;
typedef struct _BatteryClass BatteryClass;
typedef struct _BatteryPrivate BatteryPrivate;
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_dir_close0(var) ((var == NULL) ? NULL : (var = (g_dir_close (var), NULL)))
#define _g_match_info_free0(var) ((var == NULL) ? NULL : (var = (g_match_info_free (var), NULL)))
typedef struct _ParamSpecBattery ParamSpecBattery;

typedef enum  {
	BATTERY_DATA_TYPE_PROC,
	BATTERY_DATA_TYPE_SYS
} BatteryDataType;

struct _BatteryData {
	gint percent;
	gchar* state;
};

struct _Battery {
	GTypeInstance parent_instance;
	volatile int ref_count;
	BatteryPrivate * priv;
	BatteryDataType data_type;
};

struct _BatteryClass {
	GTypeClass parent_class;
	void (*finalize) (Battery *self);
};

struct _BatteryPrivate {
	GRegex* bat_curr_regex_proc;
	GRegex* bat_tot_regex_proc;
	GRegex* ac_regex;
	gint64 bat_percent;
	gchar* ac_state;
};

struct _ParamSpecBattery {
	GParamSpec parent_instance;
};


static gpointer battery_parent_class = NULL;

GType battery_data_type_get_type (void) G_GNUC_CONST;
GType battery_data_get_type (void) G_GNUC_CONST;
BatteryData* battery_data_dup (const BatteryData* self);
void battery_data_free (BatteryData* self);
void battery_data_copy (const BatteryData* self, BatteryData* dest);
void battery_data_destroy (BatteryData* self);
#define DIR_PROC "/proc/acpi/battery"
#define DIR_SYS "/sys/class/power_supply"
gpointer battery_ref (gpointer instance);
void battery_unref (gpointer instance);
GParamSpec* param_spec_battery (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_battery (GValue* value, gpointer v_object);
void value_take_battery (GValue* value, gpointer v_object);
gpointer value_get_battery (const GValue* value);
GType battery_get_type (void) G_GNUC_CONST;
#define BATTERY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_BATTERY, BatteryPrivate))
enum  {
	BATTERY_DUMMY_PROPERTY
};
Battery* battery_new (void);
Battery* battery_construct (GType object_type);
void battery_check_data_type (Battery* self);
void battery_get_status (Battery* self, BatteryData* result);
static gboolean battery_get_sys_status (Battery* self);
static gboolean battery_get_proc_status (Battery* self);
static void battery_finalize (Battery* obj);


GType battery_data_type_get_type (void) {
	static volatile gsize battery_data_type_type_id__volatile = 0;
	if (g_once_init_enter (&battery_data_type_type_id__volatile)) {
		static const GEnumValue values[] = {{BATTERY_DATA_TYPE_PROC, "BATTERY_DATA_TYPE_PROC", "proc"}, {BATTERY_DATA_TYPE_SYS, "BATTERY_DATA_TYPE_SYS", "sys"}, {0, NULL, NULL}};
		GType battery_data_type_type_id;
		battery_data_type_type_id = g_enum_register_static ("BatteryDataType", values);
		g_once_init_leave (&battery_data_type_type_id__volatile, battery_data_type_type_id);
	}
	return battery_data_type_type_id__volatile;
}


void battery_data_copy (const BatteryData* self, BatteryData* dest) {
	gint _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	_tmp0_ = (*self).percent;
	(*dest).percent = _tmp0_;
	_tmp1_ = (*self).state;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 ((*dest).state);
	(*dest).state = _tmp2_;
}


void battery_data_destroy (BatteryData* self) {
	_g_free0 ((*self).state);
}


BatteryData* battery_data_dup (const BatteryData* self) {
	BatteryData* dup;
	dup = g_new0 (BatteryData, 1);
	battery_data_copy (self, dup);
	return dup;
}


void battery_data_free (BatteryData* self) {
	battery_data_destroy (self);
	g_free (self);
}


GType battery_data_get_type (void) {
	static volatile gsize battery_data_type_id__volatile = 0;
	if (g_once_init_enter (&battery_data_type_id__volatile)) {
		GType battery_data_type_id;
		battery_data_type_id = g_boxed_type_register_static ("BatteryData", (GBoxedCopyFunc) battery_data_dup, (GBoxedFreeFunc) battery_data_free);
		g_once_init_leave (&battery_data_type_id__volatile, battery_data_type_id);
	}
	return battery_data_type_id__volatile;
}


Battery* battery_construct (GType object_type) {
	Battery* self = NULL;
	GError * _inner_error_ = NULL;
	self = (Battery*) g_type_create_instance (object_type);
	{
		GRegex* _tmp0_;
		GRegex* _tmp1_;
		_tmp0_ = g_regex_new (".*remaining.*:[ \t\n\r\f]*([0-9]*).*", G_REGEX_CASELESS, G_REGEX_MATCH_NEWLINE_ANY, &_inner_error_);
		_tmp1_ = _tmp0_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch10_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_g_regex_unref0 (self->priv->bat_curr_regex_proc);
		self->priv->bat_curr_regex_proc = _tmp1_;
	}
	goto __finally10;
	__catch10_g_regex_error:
	{
		GError* _error_ = NULL;
		FILE* _tmp2_;
		GError* _tmp3_;
		const gchar* _tmp4_;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_tmp2_ = stdout;
		_tmp3_ = _error_;
		_tmp4_ = _tmp3_->message;
		fprintf (_tmp2_, "%s", _tmp4_);
		_g_error_free0 (_error_);
	}
	__finally10:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	{
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		_tmp5_ = g_regex_new (".*last full capacity:[ \t\n\r\f]*([0-9]*).*", G_REGEX_CASELESS, G_REGEX_MATCH_NEWLINE_ANY, &_inner_error_);
		_tmp6_ = _tmp5_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch11_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_g_regex_unref0 (self->priv->bat_tot_regex_proc);
		self->priv->bat_tot_regex_proc = _tmp6_;
	}
	goto __finally11;
	__catch11_g_regex_error:
	{
		GError* _error_ = NULL;
		FILE* _tmp7_;
		GError* _tmp8_;
		const gchar* _tmp9_;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_tmp7_ = stdout;
		_tmp8_ = _error_;
		_tmp9_ = _tmp8_->message;
		fprintf (_tmp7_, "%s", _tmp9_);
		_g_error_free0 (_error_);
	}
	__finally11:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	{
		GRegex* _tmp10_;
		GRegex* _tmp11_;
		_tmp10_ = g_regex_new (".*charging.*:[ \t\n\r\f]*([a-z]*).*", 0, 0, &_inner_error_);
		_tmp11_ = _tmp10_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch12_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_g_regex_unref0 (self->priv->ac_regex);
		self->priv->ac_regex = _tmp11_;
	}
	goto __finally12;
	__catch12_g_regex_error:
	{
		GError* _error_ = NULL;
		FILE* _tmp12_;
		GError* _tmp13_;
		const gchar* _tmp14_;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_tmp12_ = stdout;
		_tmp13_ = _error_;
		_tmp14_ = _tmp13_->message;
		fprintf (_tmp12_, "%s", _tmp14_);
		_g_error_free0 (_error_);
	}
	__finally12:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	battery_check_data_type (self);
	return self;
}


Battery* battery_new (void) {
	return battery_construct (TYPE_BATTERY);
}


void battery_get_status (Battery* self, BatteryData* result) {
	BatteryData ret = {0};
	BatteryDataType _tmp0_;
	gint64 _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	memset (&ret, 0, sizeof (BatteryData));
	_tmp0_ = self->data_type;
	switch (_tmp0_) {
		case BATTERY_DATA_TYPE_SYS:
		{
			battery_get_sys_status (self);
			break;
		}
		case BATTERY_DATA_TYPE_PROC:
		{
			battery_get_proc_status (self);
			break;
		}
		default:
		{
			break;
		}
	}
	_tmp1_ = self->priv->bat_percent;
	ret.percent = (gint) _tmp1_;
	_tmp2_ = self->priv->ac_state;
	_tmp3_ = g_strdup (_tmp2_);
	_g_free0 (ret.state);
	ret.state = _tmp3_;
	*result = ret;
	return;
}


void battery_check_data_type (Battery* self) {
	gchar* t = NULL;
	gboolean _tmp0_ = FALSE;
	FILE* _tmp5_;
	const gchar* _tmp6_;
	g_return_if_fail (self != NULL);
	_tmp0_ = battery_get_sys_status (self);
	if (_tmp0_) {
		gchar* _tmp1_;
		self->data_type = BATTERY_DATA_TYPE_SYS;
		_tmp1_ = g_strdup ("SYS");
		_g_free0 (t);
		t = _tmp1_;
	} else {
		gboolean _tmp2_ = FALSE;
		_tmp2_ = battery_get_proc_status (self);
		if (_tmp2_) {
			gchar* _tmp3_;
			self->data_type = BATTERY_DATA_TYPE_PROC;
			_tmp3_ = g_strdup ("PROC");
			_g_free0 (t);
			t = _tmp3_;
		} else {
			gchar* _tmp4_;
			_tmp4_ = g_strdup ("None");
			_g_free0 (t);
			t = _tmp4_;
		}
	}
	_tmp5_ = stdout;
	_tmp6_ = t;
	fprintf (_tmp5_, "using data type: %s\n", _tmp6_);
	_g_free0 (t);
}


static gboolean battery_get_proc_status (Battery* self) {
	gboolean result = FALSE;
	gchar* contents = NULL;
	GMatchInfo* match_info = NULL;
	gchar* _tmp0_;
	gchar* bat_curr;
	gchar* _tmp1_;
	gchar* bat_total;
	gchar* _tmp2_;
	gchar* ac_state;
	gint bats_current;
	gint bats_total;
	gint bats_percent = 0;
	gchar* state_file = NULL;
	gchar* info_file = NULL;
	gboolean battery_data_found;
	gboolean tloop;
	gboolean _tmp53_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = g_strdup ("");
	bat_curr = _tmp0_;
	_tmp1_ = g_strdup ("");
	bat_total = _tmp1_;
	_tmp2_ = g_strdup ("");
	ac_state = _tmp2_;
	bats_current = 0;
	bats_total = 0;
	battery_data_found = FALSE;
	tloop = TRUE;
	{
		GDir* _tmp3_ = NULL;
		GDir* dir;
		GDir* _tmp4_;
		_tmp3_ = g_dir_open (DIR_PROC, (guint) 0, &_inner_error_);
		dir = _tmp3_;
		if (_inner_error_ != NULL) {
			goto __catch13_g_error;
		}
		_tmp4_ = dir;
		g_dir_rewind (_tmp4_);
		while (TRUE) {
			gboolean _tmp5_;
			GDir* _tmp6_;
			const gchar* _tmp7_ = NULL;
			gchar* _tmp8_;
			gchar* name;
			const gchar* _tmp9_;
			_tmp5_ = tloop;
			if (!_tmp5_) {
				break;
			}
			_tmp6_ = dir;
			_tmp7_ = g_dir_read_name (_tmp6_);
			_tmp8_ = g_strdup (_tmp7_);
			name = _tmp8_;
			_tmp9_ = name;
			if (_tmp9_ == NULL) {
				tloop = FALSE;
			} else {
				const gchar* _tmp10_;
				gchar* _tmp11_ = NULL;
				gchar* check_path;
				const gchar* _tmp12_;
				gboolean _tmp13_ = FALSE;
				_tmp10_ = name;
				_tmp11_ = g_strdup_printf ("%s/%s", DIR_PROC, _tmp10_);
				check_path = _tmp11_;
				_tmp12_ = check_path;
				_tmp13_ = g_file_test (_tmp12_, G_FILE_TEST_IS_DIR);
				if (_tmp13_) {
					const gchar* _tmp14_;
					gchar* _tmp15_ = NULL;
					const gchar* _tmp16_;
					gchar* _tmp17_ = NULL;
					const gchar* _tmp18_;
					gboolean _tmp19_ = FALSE;
					_tmp14_ = check_path;
					_tmp15_ = g_strdup_printf ("%s/state", _tmp14_);
					_g_free0 (state_file);
					state_file = _tmp15_;
					_tmp16_ = check_path;
					_tmp17_ = g_strdup_printf ("%s/info", _tmp16_);
					_g_free0 (info_file);
					info_file = _tmp17_;
					_tmp18_ = state_file;
					_tmp19_ = g_file_test (_tmp18_, G_FILE_TEST_EXISTS);
					if (_tmp19_) {
						{
							const gchar* _tmp20_;
							gchar* _tmp21_ = NULL;
							gboolean _tmp22_ = FALSE;
							gboolean _tmp23_;
							_tmp20_ = state_file;
							_tmp22_ = g_file_get_contents (_tmp20_, &_tmp21_, NULL, &_inner_error_);
							_g_free0 (contents);
							contents = _tmp21_;
							_tmp23_ = _tmp22_;
							if (_inner_error_ != NULL) {
								if (_inner_error_->domain == G_FILE_ERROR) {
									goto __catch14_g_file_error;
								}
								_g_free0 (check_path);
								_g_free0 (name);
								_g_dir_close0 (dir);
								_g_free0 (info_file);
								_g_free0 (state_file);
								_g_free0 (ac_state);
								_g_free0 (bat_total);
								_g_free0 (bat_curr);
								_g_match_info_free0 (match_info);
								_g_free0 (contents);
								g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
								g_clear_error (&_inner_error_);
								return FALSE;
							}
							if (_tmp23_) {
								GRegex* _tmp24_;
								const gchar* _tmp25_;
								GMatchInfo* _tmp26_ = NULL;
								GMatchInfo* _tmp27_;
								gchar* _tmp28_ = NULL;
								const gchar* _tmp29_;
								const gchar* _tmp35_;
								gchar* _tmp36_ = NULL;
								gboolean _tmp37_ = FALSE;
								gboolean _tmp38_;
								_tmp24_ = self->priv->bat_curr_regex_proc;
								_tmp25_ = contents;
								g_regex_match (_tmp24_, _tmp25_, G_REGEX_MATCH_NEWLINE_ANY, &_tmp26_);
								_g_match_info_free0 (match_info);
								match_info = _tmp26_;
								_tmp27_ = match_info;
								_tmp28_ = g_match_info_fetch (_tmp27_, 1);
								_g_free0 (bat_curr);
								bat_curr = _tmp28_;
								_tmp29_ = ac_state;
								if (g_strcmp0 (_tmp29_, "") == 0) {
									GRegex* _tmp30_;
									const gchar* _tmp31_;
									GMatchInfo* _tmp32_ = NULL;
									GMatchInfo* _tmp33_;
									gchar* _tmp34_ = NULL;
									_tmp30_ = self->priv->ac_regex;
									_tmp31_ = contents;
									g_regex_match (_tmp30_, _tmp31_, G_REGEX_MATCH_NEWLINE_ANY, &_tmp32_);
									_g_match_info_free0 (match_info);
									match_info = _tmp32_;
									_tmp33_ = match_info;
									_tmp34_ = g_match_info_fetch (_tmp33_, 1);
									_g_free0 (ac_state);
									ac_state = _tmp34_;
								}
								_tmp35_ = info_file;
								_tmp37_ = g_file_get_contents (_tmp35_, &_tmp36_, NULL, &_inner_error_);
								_g_free0 (contents);
								contents = _tmp36_;
								_tmp38_ = _tmp37_;
								if (_inner_error_ != NULL) {
									if (_inner_error_->domain == G_FILE_ERROR) {
										goto __catch14_g_file_error;
									}
									_g_free0 (check_path);
									_g_free0 (name);
									_g_dir_close0 (dir);
									_g_free0 (info_file);
									_g_free0 (state_file);
									_g_free0 (ac_state);
									_g_free0 (bat_total);
									_g_free0 (bat_curr);
									_g_match_info_free0 (match_info);
									_g_free0 (contents);
									g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
									g_clear_error (&_inner_error_);
									return FALSE;
								}
								if (_tmp38_) {
									GRegex* _tmp39_;
									const gchar* _tmp40_;
									GMatchInfo* _tmp41_ = NULL;
									GMatchInfo* _tmp42_;
									gchar* _tmp43_ = NULL;
									gint _tmp44_;
									const gchar* _tmp45_;
									gint _tmp46_ = 0;
									gint _tmp47_;
									const gchar* _tmp48_;
									gint _tmp49_ = 0;
									_tmp39_ = self->priv->bat_tot_regex_proc;
									_tmp40_ = contents;
									g_regex_match (_tmp39_, _tmp40_, G_REGEX_MATCH_NEWLINE_ANY, &_tmp41_);
									_g_match_info_free0 (match_info);
									match_info = _tmp41_;
									_tmp42_ = match_info;
									_tmp43_ = g_match_info_fetch (_tmp42_, 1);
									_g_free0 (bat_total);
									bat_total = _tmp43_;
									battery_data_found = TRUE;
									_tmp44_ = bats_current;
									_tmp45_ = bat_curr;
									_tmp46_ = atoi (_tmp45_);
									bats_current = _tmp44_ + _tmp46_;
									_tmp47_ = bats_total;
									_tmp48_ = bat_total;
									_tmp49_ = atoi (_tmp48_);
									bats_total = _tmp47_ + _tmp49_;
								}
							}
						}
						goto __finally14;
						__catch14_g_file_error:
						{
							GError* _error_ = NULL;
							_error_ = _inner_error_;
							_inner_error_ = NULL;
							_g_error_free0 (_error_);
						}
						__finally14:
						if (_inner_error_ != NULL) {
							_g_free0 (check_path);
							_g_free0 (name);
							_g_dir_close0 (dir);
							goto __catch13_g_error;
						}
					}
				}
				_g_free0 (check_path);
			}
			_g_free0 (name);
		}
		_g_dir_close0 (dir);
	}
	goto __finally13;
	__catch13_g_error:
	{
		GError* e = NULL;
		FILE* _tmp50_;
		GError* _tmp51_;
		const gchar* _tmp52_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp50_ = stderr;
		_tmp51_ = e;
		_tmp52_ = _tmp51_->message;
		fprintf (_tmp50_, "%s", _tmp52_);
		_g_error_free0 (e);
	}
	__finally13:
	if (_inner_error_ != NULL) {
		_g_free0 (info_file);
		_g_free0 (state_file);
		_g_free0 (ac_state);
		_g_free0 (bat_total);
		_g_free0 (bat_curr);
		_g_match_info_free0 (match_info);
		_g_free0 (contents);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return FALSE;
	}
	_tmp53_ = battery_data_found;
	if (_tmp53_) {
		gint _tmp54_;
		gint _tmp55_;
		gint _tmp56_;
		const gchar* _tmp57_;
		gchar* _tmp58_;
		_tmp54_ = bats_current;
		_tmp55_ = bats_total;
		bats_percent = (_tmp54_ * 100) / _tmp55_;
		_tmp56_ = bats_percent;
		self->priv->bat_percent = (gint64) _tmp56_;
		_tmp57_ = ac_state;
		_tmp58_ = g_strdup (_tmp57_);
		_g_free0 (self->priv->ac_state);
		self->priv->ac_state = _tmp58_;
	} else {
		gchar* _tmp59_;
		FILE* _tmp60_;
		self->priv->bat_percent = (gint64) 0;
		_tmp59_ = g_strdup ("NONE");
		_g_free0 (self->priv->ac_state);
		self->priv->ac_state = _tmp59_;
		_tmp60_ = stdout;
		fprintf (_tmp60_, "No battery info found in %s\n", DIR_PROC);
		result = FALSE;
		_g_free0 (info_file);
		_g_free0 (state_file);
		_g_free0 (ac_state);
		_g_free0 (bat_total);
		_g_free0 (bat_curr);
		_g_match_info_free0 (match_info);
		_g_free0 (contents);
		return result;
	}
	result = TRUE;
	_g_free0 (info_file);
	_g_free0 (state_file);
	_g_free0 (ac_state);
	_g_free0 (bat_total);
	_g_free0 (bat_curr);
	_g_match_info_free0 (match_info);
	_g_free0 (contents);
	return result;
}


static gint64 int64_parse (const gchar* str) {
	gint64 result = 0LL;
	const gchar* _tmp0_;
	gint64 _tmp1_ = 0LL;
	g_return_val_if_fail (str != NULL, 0LL);
	_tmp0_ = str;
	_tmp1_ = g_ascii_strtoll (_tmp0_, NULL, (guint) 0);
	result = _tmp1_;
	return result;
}


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


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


static gboolean battery_get_sys_status (Battery* self) {
	gboolean result = FALSE;
	gchar* contents = NULL;
	gchar* _tmp0_;
	gchar* bat_curr;
	gchar* _tmp1_;
	gchar* bat_total;
	gchar* _tmp2_;
	gchar* ac_state;
	gint64 bats_current;
	gint64 bats_total;
	gint64 bats_percent = 0LL;
	gchar* status_file = NULL;
	gchar* charge_full_file = NULL;
	gchar* charge_now_file = NULL;
	gboolean battery_data_found;
	gboolean tloop;
	gboolean _tmp68_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = g_strdup ("");
	bat_curr = _tmp0_;
	_tmp1_ = g_strdup ("");
	bat_total = _tmp1_;
	_tmp2_ = g_strdup ("");
	ac_state = _tmp2_;
	bats_current = (gint64) 0;
	bats_total = (gint64) 0;
	battery_data_found = FALSE;
	tloop = TRUE;
	{
		GDir* _tmp3_ = NULL;
		GDir* dir;
		GDir* _tmp4_;
		_tmp3_ = g_dir_open (DIR_SYS, (guint) 0, &_inner_error_);
		dir = _tmp3_;
		if (_inner_error_ != NULL) {
			goto __catch15_g_error;
		}
		_tmp4_ = dir;
		g_dir_rewind (_tmp4_);
		while (TRUE) {
			gboolean _tmp5_;
			GDir* _tmp6_;
			const gchar* _tmp7_ = NULL;
			gchar* _tmp8_;
			gchar* name;
			const gchar* _tmp9_;
			_tmp5_ = tloop;
			if (!_tmp5_) {
				break;
			}
			_tmp6_ = dir;
			_tmp7_ = g_dir_read_name (_tmp6_);
			_tmp8_ = g_strdup (_tmp7_);
			name = _tmp8_;
			_tmp9_ = name;
			if (_tmp9_ == NULL) {
				tloop = FALSE;
			} else {
				const gchar* _tmp10_;
				gboolean _tmp11_ = FALSE;
				const gchar* _tmp12_;
				gchar* _tmp13_ = NULL;
				gchar* check_path;
				const gchar* _tmp14_;
				gboolean _tmp15_ = FALSE;
				_tmp10_ = name;
				_tmp11_ = g_str_has_prefix (_tmp10_, "BAT");
				if (!_tmp11_) {
					_g_free0 (name);
					continue;
				}
				_tmp12_ = name;
				_tmp13_ = g_strdup_printf ("%s/%s", DIR_SYS, _tmp12_);
				check_path = _tmp13_;
				_tmp14_ = check_path;
				_tmp15_ = g_file_test (_tmp14_, G_FILE_TEST_IS_DIR);
				if (_tmp15_) {
					const gchar* _tmp16_;
					gchar* _tmp17_ = NULL;
					gchar* _tmp18_;
					gchar* file_prefix;
					gboolean use_energy;
					const gchar* _tmp19_;
					const gchar* _tmp20_;
					gchar* _tmp21_ = NULL;
					gchar* _tmp22_;
					gboolean _tmp23_ = FALSE;
					gboolean _tmp24_;
					const gchar* _tmp26_;
					const gchar* _tmp27_;
					gchar* _tmp28_ = NULL;
					const gchar* _tmp29_;
					const gchar* _tmp30_;
					gchar* _tmp31_ = NULL;
					const gchar* _tmp32_;
					gboolean _tmp33_ = FALSE;
					_tmp16_ = check_path;
					_tmp17_ = g_strdup_printf ("%s/status", _tmp16_);
					_g_free0 (status_file);
					status_file = _tmp17_;
					_tmp18_ = g_strdup ("energy");
					file_prefix = _tmp18_;
					use_energy = TRUE;
					_tmp19_ = check_path;
					_tmp20_ = file_prefix;
					_tmp21_ = g_strdup_printf ("%s/%s_full", _tmp19_, _tmp20_);
					_tmp22_ = _tmp21_;
					_tmp23_ = g_file_test (_tmp22_, G_FILE_TEST_EXISTS);
					_tmp24_ = !_tmp23_;
					_g_free0 (_tmp22_);
					if (_tmp24_) {
						gchar* _tmp25_;
						_tmp25_ = g_strdup ("charge");
						_g_free0 (file_prefix);
						file_prefix = _tmp25_;
						use_energy = FALSE;
					}
					_tmp26_ = check_path;
					_tmp27_ = file_prefix;
					_tmp28_ = g_strdup_printf ("%s/%s_full", _tmp26_, _tmp27_);
					_g_free0 (charge_full_file);
					charge_full_file = _tmp28_;
					_tmp29_ = check_path;
					_tmp30_ = file_prefix;
					_tmp31_ = g_strdup_printf ("%s/%s_now", _tmp29_, _tmp30_);
					_g_free0 (charge_now_file);
					charge_now_file = _tmp31_;
					_tmp32_ = charge_now_file;
					_tmp33_ = g_file_test (_tmp32_, G_FILE_TEST_EXISTS);
					if (_tmp33_) {
						{
							const gchar* _tmp34_;
							gchar* _tmp35_ = NULL;
							gboolean _tmp36_ = FALSE;
							gboolean _tmp37_;
							_tmp34_ = charge_now_file;
							_tmp36_ = g_file_get_contents (_tmp34_, &_tmp35_, NULL, &_inner_error_);
							_g_free0 (contents);
							contents = _tmp35_;
							_tmp37_ = _tmp36_;
							if (_inner_error_ != NULL) {
								if (_inner_error_->domain == G_FILE_ERROR) {
									goto __catch16_g_file_error;
								}
								_g_free0 (file_prefix);
								_g_free0 (check_path);
								_g_free0 (name);
								_g_dir_close0 (dir);
								_g_free0 (charge_now_file);
								_g_free0 (charge_full_file);
								_g_free0 (status_file);
								_g_free0 (ac_state);
								_g_free0 (bat_total);
								_g_free0 (bat_curr);
								_g_free0 (contents);
								g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
								g_clear_error (&_inner_error_);
								return FALSE;
							}
							if (_tmp37_) {
								const gchar* _tmp38_;
								gchar* _tmp39_;
								const gchar* _tmp40_;
								gchar* _tmp41_ = NULL;
								gboolean _tmp42_ = FALSE;
								gboolean _tmp43_;
								const gchar* _tmp52_;
								gchar* _tmp53_ = NULL;
								gboolean _tmp54_ = FALSE;
								gboolean _tmp55_;
								_tmp38_ = contents;
								_tmp39_ = g_strdup (_tmp38_);
								_g_free0 (bat_curr);
								bat_curr = _tmp39_;
								_tmp40_ = charge_full_file;
								_tmp42_ = g_file_get_contents (_tmp40_, &_tmp41_, NULL, &_inner_error_);
								_g_free0 (contents);
								contents = _tmp41_;
								_tmp43_ = _tmp42_;
								if (_inner_error_ != NULL) {
									if (_inner_error_->domain == G_FILE_ERROR) {
										goto __catch16_g_file_error;
									}
									_g_free0 (file_prefix);
									_g_free0 (check_path);
									_g_free0 (name);
									_g_dir_close0 (dir);
									_g_free0 (charge_now_file);
									_g_free0 (charge_full_file);
									_g_free0 (status_file);
									_g_free0 (ac_state);
									_g_free0 (bat_total);
									_g_free0 (bat_curr);
									_g_free0 (contents);
									g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
									g_clear_error (&_inner_error_);
									return FALSE;
								}
								if (_tmp43_) {
									const gchar* _tmp44_;
									gchar* _tmp45_;
									gint64 _tmp46_;
									const gchar* _tmp47_;
									gint64 _tmp48_ = 0LL;
									gint64 _tmp49_;
									const gchar* _tmp50_;
									gint64 _tmp51_ = 0LL;
									_tmp44_ = contents;
									_tmp45_ = g_strdup (_tmp44_);
									_g_free0 (bat_total);
									bat_total = _tmp45_;
									battery_data_found = TRUE;
									_tmp46_ = bats_current;
									_tmp47_ = bat_curr;
									_tmp48_ = int64_parse (_tmp47_);
									bats_current = _tmp46_ + _tmp48_;
									_tmp49_ = bats_total;
									_tmp50_ = bat_total;
									_tmp51_ = int64_parse (_tmp50_);
									bats_total = _tmp49_ + _tmp51_;
								}
								_tmp52_ = status_file;
								_tmp54_ = g_file_get_contents (_tmp52_, &_tmp53_, NULL, &_inner_error_);
								_g_free0 (contents);
								contents = _tmp53_;
								_tmp55_ = _tmp54_;
								if (_inner_error_ != NULL) {
									if (_inner_error_->domain == G_FILE_ERROR) {
										goto __catch16_g_file_error;
									}
									_g_free0 (file_prefix);
									_g_free0 (check_path);
									_g_free0 (name);
									_g_dir_close0 (dir);
									_g_free0 (charge_now_file);
									_g_free0 (charge_full_file);
									_g_free0 (status_file);
									_g_free0 (ac_state);
									_g_free0 (bat_total);
									_g_free0 (bat_curr);
									_g_free0 (contents);
									g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
									g_clear_error (&_inner_error_);
									return FALSE;
								}
								if (_tmp55_) {
									const gchar* _tmp56_;
									gchar* _tmp57_ = NULL;
									gchar* _tmp58_;
									gchar* _tmp59_ = NULL;
									_tmp56_ = contents;
									_tmp57_ = string_strip (_tmp56_);
									_tmp58_ = _tmp57_;
									_tmp59_ = g_utf8_strdown (_tmp58_, (gssize) (-1));
									_g_free0 (ac_state);
									ac_state = _tmp59_;
									_g_free0 (_tmp58_);
								}
							}
						}
						goto __finally16;
						__catch16_g_file_error:
						{
							GError* _error_ = NULL;
							FILE* _tmp60_;
							GError* _tmp61_;
							const gchar* _tmp62_;
							gchar* _tmp63_;
							gchar* _tmp64_;
							_error_ = _inner_error_;
							_inner_error_ = NULL;
							_tmp60_ = stderr;
							_tmp61_ = _error_;
							_tmp62_ = _tmp61_->message;
							_tmp63_ = g_strconcat (_tmp62_, "\n", NULL);
							_tmp64_ = _tmp63_;
							fprintf (_tmp60_, "%s", _tmp64_);
							_g_free0 (_tmp64_);
							_g_error_free0 (_error_);
						}
						__finally16:
						if (_inner_error_ != NULL) {
							_g_free0 (file_prefix);
							_g_free0 (check_path);
							_g_free0 (name);
							_g_dir_close0 (dir);
							goto __catch15_g_error;
						}
					}
					_g_free0 (file_prefix);
				}
				_g_free0 (check_path);
			}
			_g_free0 (name);
		}
		_g_dir_close0 (dir);
	}
	goto __finally15;
	__catch15_g_error:
	{
		GError* e = NULL;
		FILE* _tmp65_;
		GError* _tmp66_;
		const gchar* _tmp67_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp65_ = stderr;
		_tmp66_ = e;
		_tmp67_ = _tmp66_->message;
		fprintf (_tmp65_, "%s", _tmp67_);
		_g_error_free0 (e);
	}
	__finally15:
	if (_inner_error_ != NULL) {
		_g_free0 (charge_now_file);
		_g_free0 (charge_full_file);
		_g_free0 (status_file);
		_g_free0 (ac_state);
		_g_free0 (bat_total);
		_g_free0 (bat_curr);
		_g_free0 (contents);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return FALSE;
	}
	_tmp68_ = battery_data_found;
	if (_tmp68_) {
		gint64 _tmp69_;
		gint64 _tmp70_;
		gint64 _tmp71_;
		const gchar* _tmp72_;
		gchar* _tmp73_;
		_tmp69_ = bats_current;
		_tmp70_ = bats_total;
		bats_percent = (_tmp69_ * 100) / _tmp70_;
		_tmp71_ = bats_percent;
		self->priv->bat_percent = _tmp71_;
		_tmp72_ = ac_state;
		_tmp73_ = g_strdup (_tmp72_);
		_g_free0 (self->priv->ac_state);
		self->priv->ac_state = _tmp73_;
	} else {
		gchar* _tmp74_;
		FILE* _tmp75_;
		const gchar* _tmp76_ = NULL;
		gchar* _tmp77_ = NULL;
		gchar* _tmp78_;
		self->priv->bat_percent = (gint64) 0;
		_tmp74_ = g_strdup ("NONE");
		_g_free0 (self->priv->ac_state);
		self->priv->ac_state = _tmp74_;
		_tmp75_ = stdout;
		_tmp76_ = string_to_string (DIR_SYS);
		_tmp77_ = g_strconcat ("No battery info found in ", _tmp76_, "\n", NULL);
		_tmp78_ = _tmp77_;
		fprintf (_tmp75_, "%s", _tmp78_);
		_g_free0 (_tmp78_);
		result = FALSE;
		_g_free0 (charge_now_file);
		_g_free0 (charge_full_file);
		_g_free0 (status_file);
		_g_free0 (ac_state);
		_g_free0 (bat_total);
		_g_free0 (bat_curr);
		_g_free0 (contents);
		return result;
	}
	result = TRUE;
	_g_free0 (charge_now_file);
	_g_free0 (charge_full_file);
	_g_free0 (status_file);
	_g_free0 (ac_state);
	_g_free0 (bat_total);
	_g_free0 (bat_curr);
	_g_free0 (contents);
	return result;
}


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


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


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


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


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


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


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


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


void value_set_battery (GValue* value, gpointer v_object) {
	Battery* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_BATTERY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_BATTERY));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		battery_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		battery_unref (old);
	}
}


void value_take_battery (GValue* value, gpointer v_object) {
	Battery* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_BATTERY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_BATTERY));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		battery_unref (old);
	}
}


static void battery_class_init (BatteryClass * klass) {
	battery_parent_class = g_type_class_peek_parent (klass);
	BATTERY_CLASS (klass)->finalize = battery_finalize;
	g_type_class_add_private (klass, sizeof (BatteryPrivate));
}


static void battery_instance_init (Battery * self) {
	self->priv = BATTERY_GET_PRIVATE (self);
	self->ref_count = 1;
}


static void battery_finalize (Battery* obj) {
	Battery * self;
	self = BATTERY (obj);
	_g_regex_unref0 (self->priv->bat_curr_regex_proc);
	_g_regex_unref0 (self->priv->bat_tot_regex_proc);
	_g_regex_unref0 (self->priv->ac_regex);
	_g_free0 (self->priv->ac_state);
}


GType battery_get_type (void) {
	static volatile gsize battery_type_id__volatile = 0;
	if (g_once_init_enter (&battery_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_battery_init, value_battery_free_value, value_battery_copy_value, value_battery_peek_pointer, "p", value_battery_collect_value, "p", value_battery_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (BatteryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) battery_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Battery), 0, (GInstanceInitFunc) battery_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType battery_type_id;
		battery_type_id = g_type_register_fundamental (g_type_fundamental_next (), "Battery", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&battery_type_id__volatile, battery_type_id);
	}
	return battery_type_id__volatile;
}


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


void battery_unref (gpointer instance) {
	Battery* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		BATTERY_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}



