/* contacts-preferences-window.c generated by valac 0.56.3, the Vala compiler
 * generated from contacts-preferences-window.vala, do not modify */

/*
 * Copyright (C) 2022 Niels De Graef <nielsdegraef@gmail.com>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <adwaita.h>
#include <glib-object.h>
#include "contacts.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <folks/folks-eds.h>
#include <folks/folks.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.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 CONTACTS_TYPE_PREFERENCES_WINDOW (contacts_preferences_window_get_type ())
#define CONTACTS_PREFERENCES_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_PREFERENCES_WINDOW, ContactsPreferencesWindow))
#define CONTACTS_PREFERENCES_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_PREFERENCES_WINDOW, ContactsPreferencesWindowClass))
#define CONTACTS_IS_PREFERENCES_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_PREFERENCES_WINDOW))
#define CONTACTS_IS_PREFERENCES_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_PREFERENCES_WINDOW))
#define CONTACTS_PREFERENCES_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_PREFERENCES_WINDOW, ContactsPreferencesWindowClass))

typedef struct _ContactsPreferencesWindow ContactsPreferencesWindow;
typedef struct _ContactsPreferencesWindowClass ContactsPreferencesWindowClass;
typedef struct _ContactsPreferencesWindowPrivate ContactsPreferencesWindowPrivate;
enum  {
	CONTACTS_PREFERENCES_WINDOW_0_PROPERTY,
	CONTACTS_PREFERENCES_WINDOW_NUM_PROPERTIES
};
static GParamSpec* contacts_preferences_window_properties[CONTACTS_PREFERENCES_WINDOW_NUM_PROPERTIES];

#define CONTACTS_TYPE_ACCOUNTS_LIST (contacts_accounts_list_get_type ())
#define CONTACTS_ACCOUNTS_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_ACCOUNTS_LIST, ContactsAccountsList))
#define CONTACTS_ACCOUNTS_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_ACCOUNTS_LIST, ContactsAccountsListClass))
#define CONTACTS_IS_ACCOUNTS_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_ACCOUNTS_LIST))
#define CONTACTS_IS_ACCOUNTS_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_ACCOUNTS_LIST))
#define CONTACTS_ACCOUNTS_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_ACCOUNTS_LIST, ContactsAccountsListClass))

typedef struct _ContactsAccountsList ContactsAccountsList;
typedef struct _ContactsAccountsListClass ContactsAccountsListClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block29Data Block29Data;
#define _g_variant_type_free0(var) ((var == NULL) ? NULL : (var = (g_variant_type_free (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_variant_builder_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_builder_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _ContactsPreferencesWindow {
	AdwPreferencesWindow parent_instance;
	ContactsPreferencesWindowPrivate * priv;
};

struct _ContactsPreferencesWindowClass {
	AdwPreferencesWindowClass parent_class;
};

struct _ContactsPreferencesWindowPrivate {
	AdwPreferencesPage* address_books_page;
};

struct _Block29Data {
	int _ref_count_;
	ContactsPreferencesWindow* self;
	ContactsAccountsList* acc_list;
	ContactsStore* contacts_store;
};

static gint ContactsPreferencesWindow_private_offset;
static gpointer contacts_preferences_window_parent_class = NULL;

VALA_EXTERN GType contacts_preferences_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsPreferencesWindow, g_object_unref)
VALA_EXTERN ContactsPreferencesWindow* contacts_preferences_window_new (ContactsStore* contacts_store,
                                                            GtkWindow* transient_for);
VALA_EXTERN ContactsPreferencesWindow* contacts_preferences_window_construct (GType object_type,
                                                                  ContactsStore* contacts_store,
                                                                  GtkWindow* transient_for);
VALA_EXTERN GType contacts_accounts_list_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsAccountsList, g_object_unref)
static Block29Data* block29_data_ref (Block29Data* _data29_);
static void block29_data_unref (void * _userdata_);
VALA_EXTERN ContactsAccountsList* contacts_accounts_list_new (ContactsStore* contacts_store);
VALA_EXTERN ContactsAccountsList* contacts_accounts_list_construct (GType object_type,
                                                        ContactsStore* contacts_store);
static void __lambda42_ (Block29Data* _data29_,
                  GObject* obj,
                  GParamSpec* pspec);
VALA_EXTERN FolksPersonaStore* contacts_accounts_list_get_selected_store (ContactsAccountsList* self);
static void ___lambda42__g_object_notify (GObject* _sender,
                                   GParamSpec* pspec,
                                   gpointer self);
static void contacts_preferences_window_on_goa_button_clicked (ContactsPreferencesWindow* self,
                                                        GtkButton* goa_button);
static void _contacts_preferences_window_on_goa_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                            gpointer self);
static void contacts_preferences_window_finalize (GObject * obj);
static GType contacts_preferences_window_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static inline gpointer
contacts_preferences_window_get_instance_private (ContactsPreferencesWindow* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsPreferencesWindow_private_offset);
}

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

static Block29Data*
block29_data_ref (Block29Data* _data29_)
{
	g_atomic_int_inc (&_data29_->_ref_count_);
	return _data29_;
}

static void
block29_data_unref (void * _userdata_)
{
	Block29Data* _data29_;
	_data29_ = (Block29Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data29_->_ref_count_)) {
		ContactsPreferencesWindow* self;
		self = _data29_->self;
		_g_object_unref0 (_data29_->acc_list);
		_g_object_unref0 (_data29_->contacts_store);
		_g_object_unref0 (self);
		g_slice_free (Block29Data, _data29_);
	}
}

static void
__lambda42_ (Block29Data* _data29_,
             GObject* obj,
             GParamSpec* pspec)
{
	ContactsPreferencesWindow* self;
	EdsfPersonaStore* edsf_store = NULL;
	FolksPersonaStore* _tmp0_;
	FolksPersonaStore* _tmp1_;
	EdsfPersonaStore* _tmp2_;
	self = _data29_->self;
	g_return_if_fail (obj != NULL);
	g_return_if_fail (pspec != NULL);
	_tmp0_ = contacts_accounts_list_get_selected_store (_data29_->acc_list);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp1_, EDSF_TYPE_PERSONA_STORE, EdsfPersonaStore));
	edsf_store = _tmp2_;
	contacts_store_set_primary_address_book (_data29_->contacts_store, edsf_store);
	_g_object_unref0 (edsf_store);
}

static void
___lambda42__g_object_notify (GObject* _sender,
                              GParamSpec* pspec,
                              gpointer self)
{
	__lambda42_ (self, _sender, pspec);
}

static void
_contacts_preferences_window_on_goa_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                       gpointer self)
{
	contacts_preferences_window_on_goa_button_clicked ((ContactsPreferencesWindow*) self, _sender);
}

ContactsPreferencesWindow*
contacts_preferences_window_construct (GType object_type,
                                       ContactsStore* contacts_store,
                                       GtkWindow* transient_for)
{
	ContactsPreferencesWindow * self = NULL;
	Block29Data* _data29_;
	ContactsStore* _tmp0_;
	ContactsAccountsList* _tmp1_;
	AdwPreferencesPage* _tmp2_;
	AdwButtonContent* goa_button_content = NULL;
	AdwButtonContent* _tmp3_;
	GtkButton* goa_button = NULL;
	GtkButton* _tmp4_;
	g_return_val_if_fail (contacts_store != NULL, NULL);
	_data29_ = g_slice_new0 (Block29Data);
	_data29_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (contacts_store);
	_g_object_unref0 (_data29_->contacts_store);
	_data29_->contacts_store = _tmp0_;
	self = (ContactsPreferencesWindow*) g_object_new (object_type, "transient-for", transient_for, "search-enabled", FALSE, NULL);
	_data29_->self = g_object_ref (self);
	_tmp1_ = contacts_accounts_list_new (_data29_->contacts_store);
	g_object_ref_sink (_tmp1_);
	_data29_->acc_list = _tmp1_;
	adw_preferences_group_set_title ((AdwPreferencesGroup*) _data29_->acc_list, _ ("Primary Address Book"));
	adw_preferences_group_set_description ((AdwPreferencesGroup*) _data29_->acc_list, _ ("New contacts will be added to the selected address book. You are able " \
"to view and edit contacts from other address books."));
	_tmp2_ = self->priv->address_books_page;
	adw_preferences_page_add (_tmp2_, (AdwPreferencesGroup*) _data29_->acc_list);
	g_signal_connect_data ((GObject*) _data29_->acc_list, "notify::selected-store", (GCallback) ___lambda42__g_object_notify, block29_data_ref (_data29_), (GClosureNotify) block29_data_unref, 0);
	_tmp3_ = (AdwButtonContent*) adw_button_content_new ();
	g_object_ref_sink (_tmp3_);
	goa_button_content = _tmp3_;
	adw_button_content_set_label (goa_button_content, _ ("_Online Accounts"));
	adw_button_content_set_use_underline (goa_button_content, TRUE);
	adw_button_content_set_icon_name (goa_button_content, "external-link-symbolic");
	_tmp4_ = (GtkButton*) gtk_button_new ();
	g_object_ref_sink (_tmp4_);
	goa_button = _tmp4_;
	gtk_button_set_child (goa_button, (GtkWidget*) goa_button_content);
	gtk_widget_set_tooltip_text ((GtkWidget*) goa_button, _ ("Opens the Online Accounts panel in GNOME Settings"));
	gtk_widget_set_margin_top ((GtkWidget*) goa_button, 36);
	gtk_widget_set_halign ((GtkWidget*) goa_button, GTK_ALIGN_CENTER);
	gtk_widget_add_css_class ((GtkWidget*) goa_button, "pill");
	g_signal_connect_object (goa_button, "clicked", (GCallback) _contacts_preferences_window_on_goa_button_clicked_gtk_button_clicked, self, 0);
	adw_preferences_group_add ((AdwPreferencesGroup*) _data29_->acc_list, (GtkWidget*) goa_button);
	_g_object_unref0 (goa_button);
	_g_object_unref0 (goa_button_content);
	block29_data_unref (_data29_);
	_data29_ = NULL;
	return self;
}

ContactsPreferencesWindow*
contacts_preferences_window_new (ContactsStore* contacts_store,
                                 GtkWindow* transient_for)
{
	return contacts_preferences_window_construct (CONTACTS_TYPE_PREFERENCES_WINDOW, contacts_store, transient_for);
}

static void
contacts_preferences_window_on_goa_button_clicked (ContactsPreferencesWindow* self,
                                                   GtkButton* goa_button)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (goa_button != NULL);
	{
		GDBusProxy* proxy = NULL;
		GDBusProxy* _tmp0_;
		GVariantBuilder* builder = NULL;
		GVariantType* _tmp1_;
		GVariantType* _tmp2_;
		GVariantBuilder* _tmp3_;
		GVariantBuilder* _tmp4_;
		GVariantBuilder* _tmp5_;
		GVariant* _tmp6_;
		GVariant* _tmp7_;
		GVariant* param = NULL;
		GVariant* _tmp8_;
		GVariantType* _tmp9_;
		GVariantType* _tmp10_;
		GVariantBuilder* _tmp11_;
		GVariant* _tmp12_;
		GVariant* _tmp13_;
		GVariant* _tmp14_;
		GVariant** _tmp15_;
		GVariant** _tmp16_;
		gint _tmp16__length1;
		GVariant* _tmp17_;
		GVariantType* _tmp18_;
		GVariantType* _tmp19_;
		GVariant** _tmp20_;
		GVariant** _tmp21_;
		gint _tmp21__length1;
		GVariant* _tmp22_;
		GVariant** _tmp23_;
		GVariant** _tmp24_;
		gint _tmp24__length1;
		GVariant* _tmp25_;
		GVariant* _tmp26_;
		GDBusProxy* _tmp27_;
		GVariant* _tmp28_;
		GVariant* _tmp29_;
		GVariant* _tmp30_;
		_tmp0_ = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.gnome.Settings", "/org/gnome/Settings", "org.gtk.Actions", NULL, &_inner_error0_);
		proxy = _tmp0_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp1_ = g_variant_type_new ("av");
		_tmp2_ = _tmp1_;
		_tmp3_ = g_variant_builder_new (_tmp2_);
		_tmp4_ = _tmp3_;
		_g_variant_type_free0 (_tmp2_);
		builder = _tmp4_;
		_tmp5_ = builder;
		_tmp6_ = g_variant_new_string ("");
		g_variant_ref_sink (_tmp6_);
		_tmp7_ = _tmp6_;
		g_variant_builder_add (_tmp5_, "v", _tmp7_, NULL);
		_g_variant_unref0 (_tmp7_);
		_tmp8_ = g_variant_new_string ("launch-panel");
		g_variant_ref_sink (_tmp8_);
		_tmp9_ = g_variant_type_new ("v");
		_tmp10_ = _tmp9_;
		_tmp11_ = builder;
		_tmp12_ = g_variant_new ("(sav)", "online-accounts", _tmp11_, NULL);
		g_variant_ref_sink (_tmp12_);
		_tmp13_ = _tmp12_;
		_tmp14_ = g_variant_new ("v", _tmp13_, NULL);
		g_variant_ref_sink (_tmp14_);
		_tmp15_ = g_new0 (GVariant*, 1 + 1);
		_tmp15_[0] = _tmp14_;
		_tmp16_ = _tmp15_;
		_tmp16__length1 = 1;
		_tmp17_ = g_variant_new_array (_tmp10_, _tmp16_, 1);
		g_variant_ref_sink (_tmp17_);
		_tmp18_ = g_variant_type_new ("{sv}");
		_tmp19_ = _tmp18_;
		_tmp20_ = g_new0 (GVariant*, 0 + 1);
		_tmp21_ = _tmp20_;
		_tmp21__length1 = 0;
		_tmp22_ = g_variant_new_array (_tmp19_, _tmp21_, 0);
		g_variant_ref_sink (_tmp22_);
		_tmp23_ = g_new0 (GVariant*, 3 + 1);
		_tmp23_[0] = _tmp8_;
		_tmp23_[1] = _tmp17_;
		_tmp23_[2] = _tmp22_;
		_tmp24_ = _tmp23_;
		_tmp24__length1 = 3;
		_tmp25_ = g_variant_new_tuple (_tmp24_, 3);
		g_variant_ref_sink (_tmp25_);
		_tmp26_ = _tmp25_;
		_tmp24_ = (_vala_array_free (_tmp24_, _tmp24__length1, (GDestroyNotify) g_variant_unref), NULL);
		_tmp21_ = (_vala_array_free (_tmp21_, _tmp21__length1, (GDestroyNotify) g_variant_unref), NULL);
		_g_variant_type_free0 (_tmp19_);
		_tmp16_ = (_vala_array_free (_tmp16_, _tmp16__length1, (GDestroyNotify) g_variant_unref), NULL);
		_g_variant_unref0 (_tmp13_);
		_g_variant_type_free0 (_tmp10_);
		param = _tmp26_;
		_tmp27_ = proxy;
		_tmp28_ = param;
		_tmp29_ = g_dbus_proxy_call_sync (_tmp27_, "Activate", _tmp28_, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &_inner_error0_);
		_tmp30_ = _tmp29_;
		_g_variant_unref0 (_tmp30_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_variant_unref0 (param);
			_g_variant_builder_unref0 (builder);
			_g_object_unref0 (proxy);
			goto __catch0_g_error;
		}
		_g_variant_unref0 (param);
		_g_variant_builder_unref0 (builder);
		_g_object_unref0 (proxy);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp31_;
		const gchar* _tmp32_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp31_ = e;
		_tmp32_ = _tmp31_->message;
		g_warning ("contacts-preferences-window.vala:75: Couldn't open online-accounts: %s", _tmp32_);
		_g_error_free0 (e);
	}
	__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
contacts_preferences_window_class_init (ContactsPreferencesWindowClass * klass,
                                        gpointer klass_data)
{
	contacts_preferences_window_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsPreferencesWindow_private_offset);
	G_OBJECT_CLASS (klass)->finalize = contacts_preferences_window_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Contacts/ui/contacts-preferences-window.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "address_books_page", FALSE, ContactsPreferencesWindow_private_offset + G_STRUCT_OFFSET (ContactsPreferencesWindowPrivate, address_books_page));
}

static void
contacts_preferences_window_instance_init (ContactsPreferencesWindow * self,
                                           gpointer klass)
{
	self->priv = contacts_preferences_window_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
contacts_preferences_window_finalize (GObject * obj)
{
	ContactsPreferencesWindow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_PREFERENCES_WINDOW, ContactsPreferencesWindow);
	G_OBJECT_CLASS (contacts_preferences_window_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
contacts_preferences_window_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsPreferencesWindowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_preferences_window_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsPreferencesWindow), 0, (GInstanceInitFunc) contacts_preferences_window_instance_init, NULL };
	GType contacts_preferences_window_type_id;
	contacts_preferences_window_type_id = g_type_register_static (adw_preferences_window_get_type (), "ContactsPreferencesWindow", &g_define_type_info, 0);
	ContactsPreferencesWindow_private_offset = g_type_add_instance_private (contacts_preferences_window_type_id, sizeof (ContactsPreferencesWindowPrivate));
	return contacts_preferences_window_type_id;
}

GType
contacts_preferences_window_get_type (void)
{
	static volatile gsize contacts_preferences_window_type_id__once = 0;
	if (g_once_init_enter (&contacts_preferences_window_type_id__once)) {
		GType contacts_preferences_window_type_id;
		contacts_preferences_window_type_id = contacts_preferences_window_get_type_once ();
		g_once_init_leave (&contacts_preferences_window_type_id__once, contacts_preferences_window_type_id);
	}
	return contacts_preferences_window_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize 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,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

