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

/* assistant.vala
 *
 * Copyright (C) Red Hat, Inc
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Felipe Borges <felipeborges@gnome.org>
 *
 */

#include <gtk/gtk.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <libxml/uri.h>

#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

#define CONNECTIONS_TYPE_ASSISTANT (connections_assistant_get_type ())
#define CONNECTIONS_ASSISTANT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONNECTIONS_TYPE_ASSISTANT, ConnectionsAssistant))
#define CONNECTIONS_ASSISTANT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONNECTIONS_TYPE_ASSISTANT, ConnectionsAssistantClass))
#define CONNECTIONS_IS_ASSISTANT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONNECTIONS_TYPE_ASSISTANT))
#define CONNECTIONS_IS_ASSISTANT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONNECTIONS_TYPE_ASSISTANT))
#define CONNECTIONS_ASSISTANT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONNECTIONS_TYPE_ASSISTANT, ConnectionsAssistantClass))

typedef struct _ConnectionsAssistant ConnectionsAssistant;
typedef struct _ConnectionsAssistantClass ConnectionsAssistantClass;
typedef struct _ConnectionsAssistantPrivate ConnectionsAssistantPrivate;
enum  {
	CONNECTIONS_ASSISTANT_0_PROPERTY,
	CONNECTIONS_ASSISTANT_NUM_PROPERTIES
};
static GParamSpec* connections_assistant_properties[CONNECTIONS_ASSISTANT_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _xmlFreeURI0(var) ((var == NULL) ? NULL : (var = (xmlFreeURI (var), NULL)))

#define CONNECTIONS_TYPE_APPLICATION (connections_application_get_type ())
#define CONNECTIONS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONNECTIONS_TYPE_APPLICATION, ConnectionsApplication))
#define CONNECTIONS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONNECTIONS_TYPE_APPLICATION, ConnectionsApplicationClass))
#define CONNECTIONS_IS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONNECTIONS_TYPE_APPLICATION))
#define CONNECTIONS_IS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONNECTIONS_TYPE_APPLICATION))
#define CONNECTIONS_APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONNECTIONS_TYPE_APPLICATION, ConnectionsApplicationClass))

typedef struct _ConnectionsApplication ConnectionsApplication;
typedef struct _ConnectionsApplicationClass ConnectionsApplicationClass;

#define CONNECTIONS_TYPE_WINDOW (connections_window_get_type ())
#define CONNECTIONS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONNECTIONS_TYPE_WINDOW, ConnectionsWindow))
#define CONNECTIONS_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONNECTIONS_TYPE_WINDOW, ConnectionsWindowClass))
#define CONNECTIONS_IS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONNECTIONS_TYPE_WINDOW))
#define CONNECTIONS_IS_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONNECTIONS_TYPE_WINDOW))
#define CONNECTIONS_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONNECTIONS_TYPE_WINDOW, ConnectionsWindowClass))

typedef struct _ConnectionsWindow ConnectionsWindow;
typedef struct _ConnectionsWindowClass ConnectionsWindowClass;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _ConnectionsAssistant {
	GtkPopover parent_instance;
	ConnectionsAssistantPrivate * priv;
};

struct _ConnectionsAssistantClass {
	GtkPopoverClass parent_class;
};

struct _ConnectionsAssistantPrivate {
	GtkEntry* url_entry;
	GtkButton* create_button;
	GtkRadioButton* rdp_radio_button;
	GtkRadioButton* vnc_radio_button;
};

static gint ConnectionsAssistant_private_offset;
static gpointer connections_assistant_parent_class = NULL;
VALA_EXTERN ConnectionsApplication* connections_application_application;

VALA_EXTERN GType connections_assistant_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ConnectionsAssistant, g_object_unref)
static gboolean connections_assistant_uri_has_supported_scheme (ConnectionsAssistant* self,
                                                         const gchar* uri);
static void connections_assistant_on_url_entry_changed (ConnectionsAssistant* self);
static void _connections_assistant_on_url_entry_changed_gtk_toggle_button_toggled (GtkToggleButton* _sender,
                                                                            gpointer self);
static void connections_assistant_on_create_connection_button_clicked (ConnectionsAssistant* self);
VALA_EXTERN GType connections_application_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ConnectionsApplication, g_object_unref)
VALA_EXTERN void connections_application_add_connection (ConnectionsApplication* self,
                                             const gchar* uri);
static void _connections_assistant_on_create_connection_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                                    gpointer self);
static void connections_assistant_on_url_entry_activated (ConnectionsAssistant* self);
static void _connections_assistant_on_url_entry_activated_gtk_entry_activate (GtkEntry* _sender,
                                                                       gpointer self);
static void connections_assistant_on_help_button_clicked (ConnectionsAssistant* self);
VALA_EXTERN GType connections_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ConnectionsWindow, g_object_unref)
VALA_EXTERN ConnectionsWindow* connections_application_get_main_window (ConnectionsApplication* self);
static void _connections_assistant_on_help_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                       gpointer self);
VALA_EXTERN ConnectionsAssistant* connections_assistant_new (void);
VALA_EXTERN ConnectionsAssistant* connections_assistant_construct (GType object_type);
static void connections_assistant_finalize (GObject * obj);
static GType connections_assistant_get_type_once (void);

static inline gpointer
connections_assistant_get_instance_private (ConnectionsAssistant* self)
{
	return G_STRUCT_MEMBER_P (self, ConnectionsAssistant_private_offset);
}

static gboolean
connections_assistant_uri_has_supported_scheme (ConnectionsAssistant* self,
                                                const gchar* uri)
{
	gboolean _tmp0_ = FALSE;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (uri != NULL, FALSE);
	if (g_str_has_prefix (uri, "rdp://")) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_str_has_prefix (uri, "vnc://");
	}
	result = _tmp0_;
	return result;
}

static void
connections_assistant_on_url_entry_changed (ConnectionsAssistant* self)
{
	GtkEntry* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* address = NULL;
	GtkEntry* _tmp4_;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp14_;
	xmlURI* uri = NULL;
	const gchar* _tmp15_;
	xmlURI* _tmp16_;
	xmlURI* _tmp17_;
	GtkRadioButton* _tmp20_;
	xmlURI* _tmp21_;
	const gchar* _tmp22_;
	GtkRadioButton* _tmp23_;
	xmlURI* _tmp24_;
	const gchar* _tmp25_;
	GtkButton* _tmp26_;
	xmlURI* _tmp27_;
	const gchar* _tmp28_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->url_entry;
	_tmp1_ = gtk_entry_get_text (_tmp0_);
	_tmp2_ = _tmp1_;
	if (g_strcmp0 (_tmp2_, "") == 0) {
		GtkButton* _tmp3_;
		_tmp3_ = self->priv->create_button;
		gtk_widget_set_sensitive ((GtkWidget*) _tmp3_, FALSE);
		return;
	}
	_tmp4_ = self->priv->url_entry;
	_tmp5_ = gtk_entry_get_text (_tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strdup (_tmp6_);
	address = _tmp7_;
	_tmp8_ = address;
	if (!connections_assistant_uri_has_supported_scheme (self, _tmp8_)) {
		GtkRadioButton* _tmp9_;
		_tmp9_ = self->priv->rdp_radio_button;
		if (gtk_toggle_button_get_active ((GtkToggleButton*) _tmp9_)) {
			const gchar* _tmp10_;
			gchar* _tmp11_;
			_tmp10_ = address;
			_tmp11_ = g_strconcat ("rdp://", _tmp10_, NULL);
			_g_free0 (address);
			address = _tmp11_;
		} else {
			const gchar* _tmp12_;
			gchar* _tmp13_;
			_tmp12_ = address;
			_tmp13_ = g_strconcat ("vnc://", _tmp12_, NULL);
			_g_free0 (address);
			address = _tmp13_;
		}
	}
	_tmp14_ = address;
	g_debug ("assistant.vala:54: Attempting to add \"%s\"", _tmp14_);
	_tmp15_ = address;
	_tmp16_ = xmlParseURI (_tmp15_);
	uri = _tmp16_;
	_tmp17_ = uri;
	if (_tmp17_ == NULL) {
		GtkButton* _tmp18_;
		const gchar* _tmp19_;
		_tmp18_ = self->priv->create_button;
		gtk_widget_set_sensitive ((GtkWidget*) _tmp18_, FALSE);
		_tmp19_ = address;
		g_debug ("assistant.vala:59: Address \"%s\" is invalid", _tmp19_);
		_xmlFreeURI0 (uri);
		_g_free0 (address);
		return;
	}
	_tmp20_ = self->priv->rdp_radio_button;
	_tmp21_ = uri;
	_tmp22_ = _tmp21_->scheme;
	gtk_toggle_button_set_active ((GtkToggleButton*) _tmp20_, g_strcmp0 (_tmp22_, "rdp") == 0);
	_tmp23_ = self->priv->vnc_radio_button;
	_tmp24_ = uri;
	_tmp25_ = _tmp24_->scheme;
	gtk_toggle_button_set_active ((GtkToggleButton*) _tmp23_, g_strcmp0 (_tmp25_, "vnc") == 0);
	_tmp26_ = self->priv->create_button;
	_tmp27_ = uri;
	_tmp28_ = _tmp27_->server;
	gtk_widget_set_sensitive ((GtkWidget*) _tmp26_, _tmp28_ != NULL);
	_xmlFreeURI0 (uri);
	_g_free0 (address);
}

static void
_connections_assistant_on_url_entry_changed_gtk_toggle_button_toggled (GtkToggleButton* _sender,
                                                                       gpointer self)
{
	connections_assistant_on_url_entry_changed ((ConnectionsAssistant*) self);
}

static void
connections_assistant_on_create_connection_button_clicked (ConnectionsAssistant* self)
{
	gchar* uri = NULL;
	GtkEntry* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	ConnectionsApplication* _tmp10_;
	const gchar* _tmp11_;
	GtkEntry* _tmp12_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->url_entry;
	_tmp1_ = gtk_entry_get_text (_tmp0_);
	_tmp2_ = g_strdup (_tmp1_);
	uri = _tmp2_;
	_tmp3_ = uri;
	if (!connections_assistant_uri_has_supported_scheme (self, _tmp3_)) {
		const gchar* _tmp4_ = NULL;
		GtkRadioButton* _tmp5_;
		gchar* scheme = NULL;
		gchar* _tmp6_;
		const gchar* _tmp7_;
		const gchar* _tmp8_;
		gchar* _tmp9_;
		_tmp5_ = self->priv->rdp_radio_button;
		if (gtk_toggle_button_get_active ((GtkToggleButton*) _tmp5_)) {
			_tmp4_ = "rdp://";
		} else {
			_tmp4_ = "vnc://";
		}
		_tmp6_ = g_strdup (_tmp4_);
		scheme = _tmp6_;
		_tmp7_ = scheme;
		_tmp8_ = uri;
		_tmp9_ = g_strconcat (_tmp7_, _tmp8_, NULL);
		_g_free0 (uri);
		uri = _tmp9_;
		_g_free0 (scheme);
	}
	_tmp10_ = connections_application_application;
	_tmp11_ = uri;
	connections_application_add_connection (_tmp10_, _tmp11_);
	_tmp12_ = self->priv->url_entry;
	gtk_entry_set_text (_tmp12_, "");
	gtk_popover_popdown ((GtkPopover*) self);
	_g_free0 (uri);
}

static void
_connections_assistant_on_create_connection_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                               gpointer self)
{
	connections_assistant_on_create_connection_button_clicked ((ConnectionsAssistant*) self);
}

static void
connections_assistant_on_url_entry_activated (ConnectionsAssistant* self)
{
	GtkButton* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->create_button;
	_tmp1_ = gtk_widget_get_sensitive ((GtkWidget*) _tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_) {
		GtkButton* _tmp3_;
		_tmp3_ = self->priv->create_button;
		gtk_button_clicked (_tmp3_);
	}
}

static void
_connections_assistant_on_url_entry_activated_gtk_entry_activate (GtkEntry* _sender,
                                                                  gpointer self)
{
	connections_assistant_on_url_entry_activated ((ConnectionsAssistant*) self);
}

static void
connections_assistant_on_help_button_clicked (ConnectionsAssistant* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		ConnectionsApplication* _tmp0_;
		ConnectionsWindow* _tmp1_;
		ConnectionsWindow* _tmp2_;
		_tmp0_ = connections_application_application;
		_tmp1_ = connections_application_get_main_window (_tmp0_);
		_tmp2_ = _tmp1_;
		gtk_show_uri_on_window ((GtkWindow*) _tmp2_, "help:gnome-connections/connect", gtk_get_current_event_time (), &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = _error_;
		_tmp4_ = _tmp3_->message;
		g_warning ("assistant.vala:99: Failed to display help: %s", _tmp4_);
		_g_error_free0 (_error_);
	}
	__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 void
_connections_assistant_on_help_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                  gpointer self)
{
	connections_assistant_on_help_button_clicked ((ConnectionsAssistant*) self);
}

ConnectionsAssistant*
connections_assistant_construct (GType object_type)
{
	ConnectionsAssistant * self = NULL;
	self = (ConnectionsAssistant*) g_object_new (object_type, NULL);
	return self;
}

ConnectionsAssistant*
connections_assistant_new (void)
{
	return connections_assistant_construct (CONNECTIONS_TYPE_ASSISTANT);
}

static void
connections_assistant_class_init (ConnectionsAssistantClass * klass,
                                  gpointer klass_data)
{
	connections_assistant_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ConnectionsAssistant_private_offset);
	G_OBJECT_CLASS (klass)->finalize = connections_assistant_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Connections/ui/assistant.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "url_entry", FALSE, ConnectionsAssistant_private_offset + G_STRUCT_OFFSET (ConnectionsAssistantPrivate, url_entry));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "create_button", FALSE, ConnectionsAssistant_private_offset + G_STRUCT_OFFSET (ConnectionsAssistantPrivate, create_button));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "rdp_radio_button", FALSE, ConnectionsAssistant_private_offset + G_STRUCT_OFFSET (ConnectionsAssistantPrivate, rdp_radio_button));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "vnc_radio_button", FALSE, ConnectionsAssistant_private_offset + G_STRUCT_OFFSET (ConnectionsAssistantPrivate, vnc_radio_button));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_url_entry_changed", G_CALLBACK(_connections_assistant_on_url_entry_changed_gtk_toggle_button_toggled));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_create_connection_button_clicked", G_CALLBACK(_connections_assistant_on_create_connection_button_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_url_entry_activated", G_CALLBACK(_connections_assistant_on_url_entry_activated_gtk_entry_activate));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_help_button_clicked", G_CALLBACK(_connections_assistant_on_help_button_clicked_gtk_button_clicked));
}

static void
connections_assistant_instance_init (ConnectionsAssistant * self,
                                     gpointer klass)
{
	self->priv = connections_assistant_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
connections_assistant_finalize (GObject * obj)
{
	ConnectionsAssistant * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONNECTIONS_TYPE_ASSISTANT, ConnectionsAssistant);
	G_OBJECT_CLASS (connections_assistant_parent_class)->finalize (obj);
}

static GType
connections_assistant_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ConnectionsAssistantClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) connections_assistant_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ConnectionsAssistant), 0, (GInstanceInitFunc) connections_assistant_instance_init, NULL };
	GType connections_assistant_type_id;
	connections_assistant_type_id = g_type_register_static (gtk_popover_get_type (), "ConnectionsAssistant", &g_define_type_info, 0);
	ConnectionsAssistant_private_offset = g_type_add_instance_private (connections_assistant_type_id, sizeof (ConnectionsAssistantPrivate));
	return connections_assistant_type_id;
}

GType
connections_assistant_get_type (void)
{
	static volatile gsize connections_assistant_type_id__once = 0;
	if (g_once_init_enter (&connections_assistant_type_id__once)) {
		GType connections_assistant_type_id;
		connections_assistant_type_id = connections_assistant_get_type_once ();
		g_once_init_leave (&connections_assistant_type_id__once, connections_assistant_type_id);
	}
	return connections_assistant_type_id__once;
}

