/* baobab-connect-server.c generated by valac 0.21.1.1-0d6f3, the Vala compiler
 * generated from baobab-connect-server.vala, do not modify */

/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Baobab - disk usage analyzer
 *
 * Copyright (C) 2012  Paolo Borelli <pborelli@gnome.org>
 *
 * 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.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>


#define BAOBAB_TYPE_CONNECT_SERVER (baobab_connect_server_get_type ())
#define BAOBAB_CONNECT_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BAOBAB_TYPE_CONNECT_SERVER, BaobabConnectServer))
#define BAOBAB_CONNECT_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BAOBAB_TYPE_CONNECT_SERVER, BaobabConnectServerClass))
#define BAOBAB_IS_CONNECT_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BAOBAB_TYPE_CONNECT_SERVER))
#define BAOBAB_IS_CONNECT_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BAOBAB_TYPE_CONNECT_SERVER))
#define BAOBAB_CONNECT_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BAOBAB_TYPE_CONNECT_SERVER, BaobabConnectServerClass))

typedef struct _BaobabConnectServer BaobabConnectServer;
typedef struct _BaobabConnectServerClass BaobabConnectServerClass;
typedef struct _BaobabConnectServerPrivate BaobabConnectServerPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_io_channel_unref0(var) ((var == NULL) ? NULL : (var = (g_io_channel_unref (var), NULL)))

struct _BaobabConnectServer {
	GObject parent_instance;
	BaobabConnectServerPrivate * priv;
};

struct _BaobabConnectServerClass {
	GObjectClass parent_class;
};


static gpointer baobab_connect_server_parent_class = NULL;

GType baobab_connect_server_get_type (void) G_GNUC_CONST;
enum  {
	BAOBAB_CONNECT_SERVER_DUMMY_PROPERTY
};
static void baobab_connect_server_on_child_watch (BaobabConnectServer* self, GPid pid, gint status);
static gboolean baobab_connect_server_on_out_watch (BaobabConnectServer* self, GIOChannel* channel, GIOCondition cond);
void baobab_connect_server_show (BaobabConnectServer* self);
static gboolean _baobab_connect_server_on_out_watch_gio_func (GIOChannel* source, GIOCondition condition, gpointer self);
static void _baobab_connect_server_on_child_watch_gchild_watch_func (GPid pid, gint status, gpointer self);
gboolean baobab_connect_server_available (void);
BaobabConnectServer* baobab_connect_server_new (void);
BaobabConnectServer* baobab_connect_server_construct (GType object_type);

static const gchar* BAOBAB_CONNECT_SERVER_argv[3] = {"nautilus-connect-server", "--print-uri", NULL};

static void baobab_connect_server_on_child_watch (BaobabConnectServer* self, GPid pid, gint status) {
	GPid _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = pid;
	g_spawn_close_pid (_tmp0_);
}


static gchar* string_slice (const gchar* self, glong start, glong end) {
	gchar* result = NULL;
	glong string_length = 0L;
	gint _tmp0_ = 0;
	gint _tmp1_ = 0;
	glong _tmp2_ = 0L;
	glong _tmp5_ = 0L;
	gboolean _tmp8_ = FALSE;
	glong _tmp9_ = 0L;
	gboolean _tmp12_ = FALSE;
	gboolean _tmp13_ = FALSE;
	glong _tmp14_ = 0L;
	gboolean _tmp17_ = FALSE;
	glong _tmp18_ = 0L;
	glong _tmp19_ = 0L;
	glong _tmp20_ = 0L;
	glong _tmp21_ = 0L;
	glong _tmp22_ = 0L;
	gchar* _tmp23_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	_tmp2_ = start;
	if (_tmp2_ < ((glong) 0)) {
		glong _tmp3_ = 0L;
		glong _tmp4_ = 0L;
		_tmp3_ = string_length;
		_tmp4_ = start;
		start = _tmp3_ + _tmp4_;
	}
	_tmp5_ = end;
	if (_tmp5_ < ((glong) 0)) {
		glong _tmp6_ = 0L;
		glong _tmp7_ = 0L;
		_tmp6_ = string_length;
		_tmp7_ = end;
		end = _tmp6_ + _tmp7_;
	}
	_tmp9_ = start;
	if (_tmp9_ >= ((glong) 0)) {
		glong _tmp10_ = 0L;
		glong _tmp11_ = 0L;
		_tmp10_ = start;
		_tmp11_ = string_length;
		_tmp8_ = _tmp10_ <= _tmp11_;
	} else {
		_tmp8_ = FALSE;
	}
	_tmp12_ = _tmp8_;
	g_return_val_if_fail (_tmp12_, NULL);
	_tmp14_ = end;
	if (_tmp14_ >= ((glong) 0)) {
		glong _tmp15_ = 0L;
		glong _tmp16_ = 0L;
		_tmp15_ = end;
		_tmp16_ = string_length;
		_tmp13_ = _tmp15_ <= _tmp16_;
	} else {
		_tmp13_ = FALSE;
	}
	_tmp17_ = _tmp13_;
	g_return_val_if_fail (_tmp17_, NULL);
	_tmp18_ = start;
	_tmp19_ = end;
	g_return_val_if_fail (_tmp18_ <= _tmp19_, NULL);
	_tmp20_ = start;
	_tmp21_ = end;
	_tmp22_ = start;
	_tmp23_ = g_strndup (((gchar*) self) + _tmp20_, (gsize) (_tmp21_ - _tmp22_));
	result = _tmp23_;
	return result;
}


static gboolean baobab_connect_server_on_out_watch (BaobabConnectServer* self, GIOChannel* channel, GIOCondition cond) {
	gboolean result = FALSE;
	gchar* uri = NULL;
	GIOCondition _tmp0_ = 0;
	const gchar* _tmp9_ = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (channel != NULL, FALSE);
	uri = NULL;
	_tmp0_ = cond;
	if ((_tmp0_ & G_IO_IN) == G_IO_IN) {
		{
			gsize len = 0UL;
			gsize lineend = 0UL;
			GIOChannel* _tmp1_ = NULL;
			gchar* _tmp2_ = NULL;
			gsize _tmp3_ = 0UL;
			gsize _tmp4_ = 0UL;
			gsize _tmp5_ = 0UL;
			_tmp1_ = channel;
			g_io_channel_read_line (_tmp1_, &_tmp2_, &_tmp3_, &_tmp4_, &_inner_error_);
			_g_free0 (uri);
			uri = _tmp2_;
			len = _tmp3_;
			lineend = _tmp4_;
			if (_inner_error_ != NULL) {
				goto __catch3_g_error;
			}
			_tmp5_ = len;
			if (_tmp5_ > ((gsize) 0)) {
				const gchar* _tmp6_ = NULL;
				gsize _tmp7_ = 0UL;
				gchar* _tmp8_ = NULL;
				_tmp6_ = uri;
				_tmp7_ = lineend;
				_tmp8_ = string_slice (_tmp6_, (glong) 0, (glong) ((gint) _tmp7_));
				_g_free0 (uri);
				uri = _tmp8_;
			}
		}
		goto __finally3;
		__catch3_g_error:
		{
			g_clear_error (&_inner_error_);
			_inner_error_ = NULL;
		}
		__finally3:
		if (_inner_error_ != NULL) {
			_g_free0 (uri);
			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;
		}
	}
	_tmp9_ = uri;
	g_signal_emit_by_name (self, "selected", _tmp9_);
	result = FALSE;
	_g_free0 (uri);
	return result;
}


static gboolean _baobab_connect_server_on_out_watch_gio_func (GIOChannel* source, GIOCondition condition, gpointer self) {
	gboolean result;
	result = baobab_connect_server_on_out_watch (self, source, condition);
	return result;
}


static void _baobab_connect_server_on_child_watch_gchild_watch_func (GPid pid, gint status, gpointer self) {
	baobab_connect_server_on_child_watch (self, pid, status);
}


void baobab_connect_server_show (BaobabConnectServer* self) {
	GPid pid = 0;
	gint out_fd = 0;
	GIOChannel* out_channel = NULL;
	GIOChannel* _tmp4_ = NULL;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	{
		GPid _tmp0_ = 0;
		gint _tmp1_ = 0;
		g_spawn_async_with_pipes (NULL, BAOBAB_CONNECT_SERVER_argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &_tmp0_, NULL, &_tmp1_, NULL, &_inner_error_);
		pid = _tmp0_;
		out_fd = _tmp1_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_SPAWN_ERROR) {
				goto __catch4_g_spawn_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;
		}
	}
	goto __finally4;
	__catch4_g_spawn_error:
	{
		GError* e = NULL;
		GError* _tmp2_ = NULL;
		const gchar* _tmp3_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp2_ = e;
		_tmp3_ = _tmp2_->message;
		g_warning ("baobab-connect-server.vala:74: Failed to run nautilus-connect-server: " \
"%s", _tmp3_);
		_g_error_free0 (e);
	}
	__finally4:
	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;
	}
	_tmp4_ = g_io_channel_unix_new (out_fd);
	out_channel = _tmp4_;
	g_io_add_watch (out_channel, G_IO_IN | G_IO_HUP, _baobab_connect_server_on_out_watch_gio_func, self);
	g_child_watch_add_full (G_PRIORITY_DEFAULT_IDLE, pid, _baobab_connect_server_on_child_watch_gchild_watch_func, g_object_ref (self), g_object_unref);
	_g_io_channel_unref0 (out_channel);
}


gboolean baobab_connect_server_available (void) {
	gboolean result = FALSE;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	gboolean _tmp3_ = FALSE;
	_tmp0_ = BAOBAB_CONNECT_SERVER_argv[0];
	_tmp1_ = g_find_program_in_path (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_ != NULL;
	_g_free0 (_tmp2_);
	result = _tmp3_;
	return result;
}


BaobabConnectServer* baobab_connect_server_construct (GType object_type) {
	BaobabConnectServer * self = NULL;
	self = (BaobabConnectServer*) g_object_new (object_type, NULL);
	return self;
}


BaobabConnectServer* baobab_connect_server_new (void) {
	return baobab_connect_server_construct (BAOBAB_TYPE_CONNECT_SERVER);
}


static void baobab_connect_server_class_init (BaobabConnectServerClass * klass) {
	baobab_connect_server_parent_class = g_type_class_peek_parent (klass);
	g_signal_new ("selected", BAOBAB_TYPE_CONNECT_SERVER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
}


static void baobab_connect_server_instance_init (BaobabConnectServer * self) {
}


GType baobab_connect_server_get_type (void) {
	static volatile gsize baobab_connect_server_type_id__volatile = 0;
	if (g_once_init_enter (&baobab_connect_server_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (BaobabConnectServerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) baobab_connect_server_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BaobabConnectServer), 0, (GInstanceInitFunc) baobab_connect_server_instance_init, NULL };
		GType baobab_connect_server_type_id;
		baobab_connect_server_type_id = g_type_register_static (G_TYPE_OBJECT, "BaobabConnectServer", &g_define_type_info, 0);
		g_once_init_leave (&baobab_connect_server_type_id__volatile, baobab_connect_server_type_id);
	}
	return baobab_connect_server_type_id__volatile;
}



