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

/*
 * Copyright 2018 elementary, Inc. (https://elementary.io)
 *
 * 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.
 *
 * Authors: Corentin Noël <corentin@elementary.io>
 */

#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.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 GREETER_TYPE_SESSION_BUTTON (greeter_session_button_get_type ())
#define GREETER_SESSION_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GREETER_TYPE_SESSION_BUTTON, GreeterSessionButton))
#define GREETER_SESSION_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GREETER_TYPE_SESSION_BUTTON, GreeterSessionButtonClass))
#define GREETER_IS_SESSION_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GREETER_TYPE_SESSION_BUTTON))
#define GREETER_IS_SESSION_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GREETER_TYPE_SESSION_BUTTON))
#define GREETER_SESSION_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GREETER_TYPE_SESSION_BUTTON, GreeterSessionButtonClass))

typedef struct _GreeterSessionButton GreeterSessionButton;
typedef struct _GreeterSessionButtonClass GreeterSessionButtonClass;
typedef struct _GreeterSessionButtonPrivate GreeterSessionButtonPrivate;
enum  {
	GREETER_SESSION_BUTTON_0_PROPERTY,
	GREETER_SESSION_BUTTON_NUM_PROPERTIES
};
static GParamSpec* greeter_session_button_properties[GREETER_SESSION_BUTTON_NUM_PROPERTIES];
typedef struct _Block9Data Block9Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define GREETER_TYPE_MAIN_WINDOW (greeter_main_window_get_type ())
#define GREETER_MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GREETER_TYPE_MAIN_WINDOW, GreeterMainWindow))
#define GREETER_MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GREETER_TYPE_MAIN_WINDOW, GreeterMainWindowClass))
#define GREETER_IS_MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GREETER_TYPE_MAIN_WINDOW))
#define GREETER_IS_MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GREETER_TYPE_MAIN_WINDOW))
#define GREETER_MAIN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GREETER_TYPE_MAIN_WINDOW, GreeterMainWindowClass))

typedef struct _GreeterMainWindow GreeterMainWindow;
typedef struct _GreeterMainWindowClass GreeterMainWindowClass;
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_variant_iter_free0(var) ((var == NULL) ? NULL : (var = (g_variant_iter_free (var), NULL)))

struct _GreeterSessionButton {
	GtkMenuButton parent_instance;
	GreeterSessionButtonPrivate * priv;
};

struct _GreeterSessionButtonClass {
	GtkMenuButtonClass parent_class;
};

struct _Block9Data {
	int _ref_count_;
	GreeterSessionButton* self;
	GtkListBox* settings_list;
};

static gpointer greeter_session_button_parent_class = NULL;

VALA_EXTERN GType greeter_session_button_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GreeterSessionButton, g_object_unref)
VALA_EXTERN GreeterSessionButton* greeter_session_button_new (void);
VALA_EXTERN GreeterSessionButton* greeter_session_button_construct (GType object_type);
static GObject * greeter_session_button_constructor (GType type,
                                              guint n_construct_properties,
                                              GObjectConstructParam * construct_properties);
static Block9Data* block9_data_ref (Block9Data* _data9_);
static void block9_data_unref (void * _userdata_);
static gint _greeter_session_button___lambda12_ (GreeterSessionButton* self,
                                          GtkListBoxRow* row1,
                                          GtkListBoxRow* row2);
static gint __greeter_session_button___lambda12__gtk_list_box_sort_func (GtkListBoxRow* row1,
                                                                  GtkListBoxRow* row2,
                                                                  gpointer self);
static void _greeter_session_button___lambda13_ (Block9Data* _data9_,
                                          GtkWidget* previous_toplevel);
VALA_EXTERN GType greeter_main_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GreeterMainWindow, g_object_unref)
static void __greeter_session_button___lambda13__gtk_widget_hierarchy_changed (GtkWidget* _sender,
                                                                        GtkWidget* previous_toplevel,
                                                                        gpointer self);
static GType greeter_session_button_get_type_once (void);

GreeterSessionButton*
greeter_session_button_construct (GType object_type)
{
	GreeterSessionButton * self = NULL;
	self = (GreeterSessionButton*) g_object_new (object_type, NULL);
	return self;
}

GreeterSessionButton*
greeter_session_button_new (void)
{
	return greeter_session_button_construct (GREETER_TYPE_SESSION_BUTTON);
}

static Block9Data*
block9_data_ref (Block9Data* _data9_)
{
	g_atomic_int_inc (&_data9_->_ref_count_);
	return _data9_;
}

static void
block9_data_unref (void * _userdata_)
{
	Block9Data* _data9_;
	_data9_ = (Block9Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data9_->_ref_count_)) {
		GreeterSessionButton* self;
		self = _data9_->self;
		_g_object_unref0 (_data9_->settings_list);
		_g_object_unref0 (self);
		g_slice_free (Block9Data, _data9_);
	}
}

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

static gint
_greeter_session_button___lambda12_ (GreeterSessionButton* self,
                                     GtkListBoxRow* row1,
                                     GtkListBoxRow* row2)
{
	GtkModelButton* child1 = NULL;
	GtkWidget* _tmp0_;
	GtkModelButton* _tmp1_;
	GtkModelButton* child2 = NULL;
	GtkWidget* _tmp2_;
	GtkModelButton* _tmp3_;
	GtkModelButton* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GtkModelButton* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gint _tmp12_;
	gint result;
	g_return_val_if_fail (row1 != NULL, 0);
	g_return_val_if_fail (row2 != NULL, 0);
	_tmp0_ = gtk_bin_get_child ((GtkBin*) row1);
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, gtk_model_button_get_type (), GtkModelButton));
	child1 = _tmp1_;
	_tmp2_ = gtk_bin_get_child ((GtkBin*) row2);
	_tmp3_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp2_, gtk_model_button_get_type (), GtkModelButton));
	child2 = _tmp3_;
	_tmp4_ = child1;
	g_object_get (_tmp4_, "text", &_tmp5_, NULL);
	_tmp6_ = _tmp5_;
	_tmp7_ = _tmp6_;
	_tmp8_ = child2;
	g_object_get (_tmp8_, "text", &_tmp9_, NULL);
	_tmp10_ = _tmp9_;
	_tmp11_ = _tmp10_;
	_tmp12_ = g_utf8_collate (_tmp7_, _tmp11_);
	_g_free0 (_tmp11_);
	_g_free0 (_tmp7_);
	result = _tmp12_;
	_g_object_unref0 (child2);
	_g_object_unref0 (child1);
	return result;
}

static gint
__greeter_session_button___lambda12__gtk_list_box_sort_func (GtkListBoxRow* row1,
                                                             GtkListBoxRow* row2,
                                                             gpointer self)
{
	gint result;
	result = _greeter_session_button___lambda12_ ((GreeterSessionButton*) self, row1, row2);
	return result;
}

static void
_greeter_session_button___lambda13_ (Block9Data* _data9_,
                                     GtkWidget* previous_toplevel)
{
	GreeterSessionButton* self;
	GtkWidget* main_window = NULL;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	GtkWidget* _tmp2_;
	self = _data9_->self;
	_tmp0_ = gtk_widget_get_ancestor ((GtkWidget*) self, GREETER_TYPE_MAIN_WINDOW);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	main_window = _tmp1_;
	_tmp2_ = main_window;
	if (_tmp2_ != NULL) {
		GActionGroup* session_action_group = NULL;
		GtkWidget* _tmp3_;
		GActionGroup* _tmp4_;
		GActionGroup* _tmp5_;
		GVariant* hint = NULL;
		GActionGroup* _tmp6_;
		GVariant* _tmp7_;
		GVariantIter* iter = NULL;
		GVariant* _tmp8_;
		GVariantIter* _tmp9_;
		GVariant* val = NULL;
		gchar* key = NULL;
		GtkListBox* _tmp23_;
		GtkListBoxRow* _tmp24_;
		_tmp3_ = main_window;
		_tmp4_ = gtk_widget_get_action_group (_tmp3_, "session");
		_tmp5_ = _g_object_ref0 (_tmp4_);
		session_action_group = _tmp5_;
		_tmp6_ = session_action_group;
		_tmp7_ = g_action_group_get_action_state_hint (_tmp6_, "select");
		hint = _tmp7_;
		_tmp8_ = hint;
		_tmp9_ = g_variant_iter_new (_tmp8_);
		iter = _tmp9_;
		val = NULL;
		key = NULL;
		while (TRUE) {
			GVariantIter* _tmp10_;
			gchar* _tmp11_ = NULL;
			GVariant* _tmp12_ = NULL;
			gboolean _tmp13_;
			GtkModelButton* radio = NULL;
			const gchar* _tmp14_;
			gchar* _tmp15_;
			GtkModelButton* _tmp16_ = NULL;
			GtkModelButton* _tmp17_;
			GVariant* _tmp18_;
			gchar* _tmp19_;
			gchar* _tmp20_;
			GtkListBox* _tmp21_;
			GtkModelButton* _tmp22_;
			_tmp10_ = iter;
			_tmp13_ = g_variant_iter_next (_tmp10_, "{sv}", &_tmp11_, &_tmp12_, NULL);
			_g_free0 (key);
			key = _tmp11_;
			_g_variant_unref0 (val);
			val = _tmp12_;
			if (!_tmp13_) {
				break;
			}
			_tmp14_ = key;
			_tmp15_ = g_strdup (_tmp14_);
			_tmp16_ = (GtkModelButton*) gtk_model_button_new ();
			g_object_set (_tmp16_, "text", _tmp15_, NULL);
			_g_free0 (_tmp15_);
			g_object_ref_sink (_tmp16_);
			radio = _tmp16_;
			_tmp17_ = radio;
			_tmp18_ = val;
			_tmp19_ = g_action_print_detailed_name ("session.select", _tmp18_);
			_tmp20_ = _tmp19_;
			gtk_actionable_set_detailed_action_name ((GtkActionable*) _tmp17_, _tmp20_);
			_g_free0 (_tmp20_);
			_tmp21_ = _data9_->settings_list;
			_tmp22_ = radio;
			gtk_container_add ((GtkContainer*) _tmp21_, (GtkWidget*) _tmp22_);
			_g_object_unref0 (radio);
		}
		_tmp23_ = _data9_->settings_list;
		_tmp24_ = gtk_list_box_get_row_at_index (_tmp23_, 1);
		if (_tmp24_ == NULL) {
			gtk_widget_destroy ((GtkWidget*) self);
		} else {
			GtkListBox* _tmp25_;
			GtkListBox* _tmp26_;
			_tmp25_ = _data9_->settings_list;
			gtk_widget_show_all ((GtkWidget*) _tmp25_);
			_tmp26_ = _data9_->settings_list;
			gtk_list_box_invalidate_sort (_tmp26_);
		}
		_g_free0 (key);
		_g_variant_unref0 (val);
		_g_variant_iter_free0 (iter);
		_g_variant_unref0 (hint);
		_g_object_unref0 (session_action_group);
	}
	_g_object_unref0 (main_window);
}

static void
__greeter_session_button___lambda13__gtk_widget_hierarchy_changed (GtkWidget* _sender,
                                                                   GtkWidget* previous_toplevel,
                                                                   gpointer self)
{
	_greeter_session_button___lambda13_ (self, previous_toplevel);
}

static GObject *
greeter_session_button_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GreeterSessionButton * self;
	Block9Data* _data9_;
	GtkListBox* _tmp0_ = NULL;
	GtkListBox* _tmp1_;
	GtkPopover* settings_popover = NULL;
	GtkPopover* _tmp2_ = NULL;
	GtkPopover* _tmp3_;
	GtkListBox* _tmp4_;
	GtkImage* cog_image = NULL;
	GtkImage* _tmp5_;
	GtkPopover* _tmp6_;
	GtkStyleContext* _tmp7_;
	GtkImage* _tmp8_;
	parent_class = G_OBJECT_CLASS (greeter_session_button_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GREETER_TYPE_SESSION_BUTTON, GreeterSessionButton);
	_data9_ = g_slice_new0 (Block9Data);
	_data9_->_ref_count_ = 1;
	_data9_->self = g_object_ref (self);
	_tmp0_ = (GtkListBox*) gtk_list_box_new ();
	gtk_widget_set_margin_bottom ((GtkWidget*) _tmp0_, 3);
	gtk_widget_set_margin_top ((GtkWidget*) _tmp0_, 3);
	g_object_ref_sink (_tmp0_);
	_data9_->settings_list = _tmp0_;
	_tmp1_ = _data9_->settings_list;
	gtk_list_box_set_sort_func (_tmp1_, __greeter_session_button___lambda12__gtk_list_box_sort_func, g_object_ref (self), g_object_unref);
	_tmp2_ = (GtkPopover*) gtk_popover_new ((GtkWidget*) self);
	gtk_popover_set_position (_tmp2_, GTK_POS_BOTTOM);
	g_object_ref_sink (_tmp2_);
	settings_popover = _tmp2_;
	_tmp3_ = settings_popover;
	_tmp4_ = _data9_->settings_list;
	gtk_container_add ((GtkContainer*) _tmp3_, (GtkWidget*) _tmp4_);
	_tmp5_ = (GtkImage*) gtk_image_new_from_icon_name ("open-menu-symbolic", (GtkIconSize) GTK_ICON_SIZE_BUTTON);
	g_object_ref_sink (_tmp5_);
	cog_image = _tmp5_;
	gtk_menu_button_set_direction ((GtkMenuButton*) self, GTK_ARROW_DOWN);
	_tmp6_ = settings_popover;
	gtk_menu_button_set_popover ((GtkMenuButton*) self, _tmp6_);
	_tmp7_ = gtk_widget_get_style_context ((GtkWidget*) self);
	gtk_style_context_add_class (_tmp7_, GTK_STYLE_CLASS_FLAT);
	_tmp8_ = cog_image;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp8_);
	g_signal_connect_data ((GtkWidget*) self, "hierarchy-changed", (GCallback) __greeter_session_button___lambda13__gtk_widget_hierarchy_changed, block9_data_ref (_data9_), (GClosureNotify) block9_data_unref, 0);
	_g_object_unref0 (cog_image);
	_g_object_unref0 (settings_popover);
	block9_data_unref (_data9_);
	_data9_ = NULL;
	return obj;
}

static void
greeter_session_button_class_init (GreeterSessionButtonClass * klass,
                                   gpointer klass_data)
{
	greeter_session_button_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->constructor = greeter_session_button_constructor;
}

static void
greeter_session_button_instance_init (GreeterSessionButton * self,
                                      gpointer klass)
{
}

static GType
greeter_session_button_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GreeterSessionButtonClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) greeter_session_button_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GreeterSessionButton), 0, (GInstanceInitFunc) greeter_session_button_instance_init, NULL };
	GType greeter_session_button_type_id;
	greeter_session_button_type_id = g_type_register_static (gtk_menu_button_get_type (), "GreeterSessionButton", &g_define_type_info, 0);
	return greeter_session_button_type_id;
}

GType
greeter_session_button_get_type (void)
{
	static volatile gsize greeter_session_button_type_id__once = 0;
	if (g_once_init_enter (&greeter_session_button_type_id__once)) {
		GType greeter_session_button_type_id;
		greeter_session_button_type_id = greeter_session_button_get_type_once ();
		g_once_init_leave (&greeter_session_button_type_id__once, greeter_session_button_type_id);
	}
	return greeter_session_button_type_id__once;
}

