/* contacts-addressbook-dialog.c generated by valac 0.56.0, the Vala compiler
 * generated from contacts-addressbook-dialog.vala, do not modify */

/*
 * Copyright (C) 2020 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 <gtk/gtk.h>
#include <glib-object.h>
#include <adwaita.h>
#include <glib.h>
#include "contacts.h"
#include <glib/gi18n-lib.h>
#include <float.h>
#include <math.h>
#include <folks/folks.h>
#include <folks/folks-eds.h>
#include <libedataserver/libedataserver.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_ADDRESSBOOK_DIALOG (contacts_addressbook_dialog_get_type ())
#define CONTACTS_ADDRESSBOOK_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_ADDRESSBOOK_DIALOG, ContactsAddressbookDialog))
#define CONTACTS_ADDRESSBOOK_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_ADDRESSBOOK_DIALOG, ContactsAddressbookDialogClass))
#define CONTACTS_IS_ADDRESSBOOK_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_ADDRESSBOOK_DIALOG))
#define CONTACTS_IS_ADDRESSBOOK_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_ADDRESSBOOK_DIALOG))
#define CONTACTS_ADDRESSBOOK_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_ADDRESSBOOK_DIALOG, ContactsAddressbookDialogClass))

typedef struct _ContactsAddressbookDialog ContactsAddressbookDialog;
typedef struct _ContactsAddressbookDialogClass ContactsAddressbookDialogClass;
typedef struct _ContactsAddressbookDialogPrivate ContactsAddressbookDialogPrivate;

#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;
enum  {
	CONTACTS_ADDRESSBOOK_DIALOG_0_PROPERTY,
	CONTACTS_ADDRESSBOOK_DIALOG_NUM_PROPERTIES
};
static GParamSpec* contacts_addressbook_dialog_properties[CONTACTS_ADDRESSBOOK_DIALOG_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block1Data Block1Data;
typedef struct _ContactsAccountsListPrivate ContactsAccountsListPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _ContactsAddressbookDialog {
	GtkDialog parent_instance;
	ContactsAddressbookDialogPrivate * priv;
};

struct _ContactsAddressbookDialogClass {
	GtkDialogClass parent_class;
};

struct _ContactsAddressbookDialogPrivate {
	ContactsAccountsList* accounts_list;
};

struct _Block1Data {
	int _ref_count_;
	ContactsAddressbookDialog* self;
	GtkWidget* ok_button;
	gulong active_button_once;
};

struct _ContactsAccountsList {
	AdwBin parent_instance;
	ContactsAccountsListPrivate * priv;
	FolksPersonaStore* selected_store;
};

struct _ContactsAccountsListClass {
	AdwBinClass parent_class;
};

static gint ContactsAddressbookDialog_private_offset;
static gpointer contacts_addressbook_dialog_parent_class = NULL;

VALA_EXTERN GType contacts_addressbook_dialog_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsAddressbookDialog, g_object_unref)
VALA_EXTERN GType contacts_accounts_list_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsAccountsList, g_object_unref)
VALA_EXTERN ContactsAddressbookDialog* contacts_addressbook_dialog_new (ContactsStore* contacts_store,
                                                            GtkWindow* window);
VALA_EXTERN ContactsAddressbookDialog* contacts_addressbook_dialog_construct (GType object_type,
                                                                  ContactsStore* contacts_store,
                                                                  GtkWindow* window);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_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);
VALA_EXTERN void contacts_accounts_list_update_contents (ContactsAccountsList* self,
                                             gboolean select_active);
static void __lambda4_ (Block1Data* _data1_);
static void ___lambda4__contacts_accounts_list_account_selected (ContactsAccountsList* _sender,
                                                          gpointer self);
static void __lambda5_ (ContactsAddressbookDialog* self);
static void ___lambda5__folks_backend_store_backend_available (FolksBackendStore* _sender,
                                                        FolksBackend* backend,
                                                        gpointer self);
static void contacts_addressbook_dialog_real_response (GtkDialog* base,
                                                gint response);
static void contacts_addressbook_dialog_finalize (GObject * obj);
static GType contacts_addressbook_dialog_get_type_once (void);

static inline gpointer
contacts_addressbook_dialog_get_instance_private (ContactsAddressbookDialog* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsAddressbookDialog_private_offset);
}

static Block1Data*
block1_data_ref (Block1Data* _data1_)
{
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}

static void
block1_data_unref (void * _userdata_)
{
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		ContactsAddressbookDialog* self;
		self = _data1_->self;
		_g_object_unref0 (_data1_->ok_button);
		_g_object_unref0 (self);
		g_slice_free (Block1Data, _data1_);
	}
}

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

static void
__lambda4_ (Block1Data* _data1_)
{
	ContactsAddressbookDialog* self;
	ContactsAccountsList* _tmp0_;
	self = _data1_->self;
	gtk_widget_set_sensitive (_data1_->ok_button, TRUE);
	_tmp0_ = self->priv->accounts_list;
	g_signal_handler_disconnect ((GObject*) _tmp0_, _data1_->active_button_once);
}

static void
___lambda4__contacts_accounts_list_account_selected (ContactsAccountsList* _sender,
                                                     gpointer self)
{
	__lambda4_ (self);
}

static void
__lambda5_ (ContactsAddressbookDialog* self)
{
	ContactsAccountsList* _tmp0_;
	_tmp0_ = self->priv->accounts_list;
	contacts_accounts_list_update_contents (_tmp0_, TRUE);
}

static void
___lambda5__folks_backend_store_backend_available (FolksBackendStore* _sender,
                                                   FolksBackend* backend,
                                                   gpointer self)
{
	__lambda5_ ((ContactsAddressbookDialog*) self);
}

ContactsAddressbookDialog*
contacts_addressbook_dialog_construct (GType object_type,
                                       ContactsStore* contacts_store,
                                       GtkWindow* window)
{
	ContactsAddressbookDialog * self = NULL;
	Block1Data* _data1_;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	GtkStyleContext* _tmp2_;
	GtkScrolledWindow* scrolled_window = NULL;
	GtkScrolledWindow* _tmp3_;
	GtkBox* _tmp4_;
	AdwClamp* clamp = NULL;
	AdwClamp* _tmp5_;
	GtkBox* box = NULL;
	GtkBox* _tmp6_;
	GtkLabel* explanation_label = NULL;
	GtkLabel* _tmp7_;
	ContactsAccountsList* _tmp8_;
	ContactsAccountsList* _tmp9_;
	ContactsAccountsList* _tmp10_;
	gulong _tmp11_;
	FolksBackendStore* _tmp12_;
	FolksBackendStore* _tmp13_;
	ContactsAccountsList* _tmp14_;
	g_return_val_if_fail (contacts_store != NULL, NULL);
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	self = (ContactsAddressbookDialog*) g_object_new (object_type, "modal", TRUE, "transient-for", window, "title", _ ("Change Address Book"), "use-header-bar", 1, NULL);
	_data1_->self = g_object_ref (self);
	gtk_dialog_add_buttons ((GtkDialog*) self, _ ("Change"), GTK_RESPONSE_OK, _ ("Cancel"), GTK_RESPONSE_CANCEL, NULL);
	_tmp0_ = gtk_dialog_get_widget_for_response ((GtkDialog*) self, (gint) GTK_RESPONSE_OK);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_data1_->ok_button = _tmp1_;
	gtk_widget_set_sensitive (_data1_->ok_button, FALSE);
	_tmp2_ = gtk_widget_get_style_context (_data1_->ok_button);
	gtk_style_context_add_class (_tmp2_, "suggested-action");
	_tmp3_ = (GtkScrolledWindow*) gtk_scrolled_window_new ();
	g_object_ref_sink (_tmp3_);
	scrolled_window = _tmp3_;
	gtk_widget_set_hexpand ((GtkWidget*) scrolled_window, TRUE);
	gtk_widget_set_vexpand ((GtkWidget*) scrolled_window, TRUE);
	g_object_set ((GtkWidget*) scrolled_window, "height-request", 300, NULL);
	g_object_set (scrolled_window, "hscrollbar-policy", GTK_POLICY_NEVER, NULL);
	gtk_scrolled_window_set_propagate_natural_height (scrolled_window, TRUE);
	_tmp4_ = gtk_dialog_get_content_area ((GtkDialog*) self);
	gtk_box_append (G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, gtk_box_get_type (), GtkBox), (GtkWidget*) scrolled_window);
	_tmp5_ = (AdwClamp*) adw_clamp_new ();
	g_object_ref_sink (_tmp5_);
	clamp = _tmp5_;
	gtk_widget_set_margin_top ((GtkWidget*) clamp, 32);
	gtk_widget_set_margin_bottom ((GtkWidget*) clamp, 32);
	gtk_widget_set_margin_start ((GtkWidget*) clamp, 12);
	gtk_widget_set_margin_end ((GtkWidget*) clamp, 12);
	adw_clamp_set_maximum_size (clamp, 400);
	gtk_scrolled_window_set_child (scrolled_window, (GtkWidget*) clamp);
	_tmp6_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
	g_object_ref_sink (_tmp6_);
	box = _tmp6_;
	gtk_widget_set_valign ((GtkWidget*) box, GTK_ALIGN_START);
	adw_clamp_set_child (clamp, (GtkWidget*) box);
	_tmp7_ = (GtkLabel*) gtk_label_new (_ ("New contacts will be added to the selected address book.\n" \
"You are able to view and edit contacts from other address books."));
	g_object_ref_sink (_tmp7_);
	explanation_label = _tmp7_;
	gtk_label_set_xalign (explanation_label, (gfloat) 0);
	gtk_label_set_wrap (explanation_label, TRUE);
	gtk_box_append (box, (GtkWidget*) explanation_label);
	_tmp8_ = contacts_accounts_list_new (contacts_store);
	g_object_ref_sink (_tmp8_);
	_g_object_unref0 (self->priv->accounts_list);
	self->priv->accounts_list = _tmp8_;
	_tmp9_ = self->priv->accounts_list;
	contacts_accounts_list_update_contents (_tmp9_, TRUE);
	_data1_->active_button_once = (gulong) 0;
	_tmp10_ = self->priv->accounts_list;
	_tmp11_ = g_signal_connect_data (_tmp10_, "account-selected", (GCallback) ___lambda4__contacts_accounts_list_account_selected, block1_data_ref (_data1_), (GClosureNotify) block1_data_unref, 0);
	_data1_->active_button_once = _tmp11_;
	_tmp12_ = contacts_store_get_backend_store (contacts_store);
	_tmp13_ = _tmp12_;
	g_signal_connect_object (_tmp13_, "backend-available", (GCallback) ___lambda5__folks_backend_store_backend_available, self, 0);
	_tmp14_ = self->priv->accounts_list;
	gtk_box_append (box, (GtkWidget*) _tmp14_);
	_g_object_unref0 (explanation_label);
	_g_object_unref0 (box);
	_g_object_unref0 (clamp);
	_g_object_unref0 (scrolled_window);
	block1_data_unref (_data1_);
	_data1_ = NULL;
	return self;
}

ContactsAddressbookDialog*
contacts_addressbook_dialog_new (ContactsStore* contacts_store,
                                 GtkWindow* window)
{
	return contacts_addressbook_dialog_construct (CONTACTS_TYPE_ADDRESSBOOK_DIALOG, contacts_store, window);
}

static void
contacts_addressbook_dialog_real_response (GtkDialog* base,
                                           gint response)
{
	ContactsAddressbookDialog * self;
	EdsfPersonaStore* e_store = NULL;
	ContactsAccountsList* _tmp0_;
	FolksPersonaStore* _tmp1_;
	EdsfPersonaStore* _tmp2_;
	EdsfPersonaStore* _tmp3_;
	self = (ContactsAddressbookDialog*) base;
	if (response != ((gint) GTK_RESPONSE_OK)) {
		return;
	}
	_tmp0_ = self->priv->accounts_list;
	_tmp1_ = _tmp0_->selected_store;
	_tmp2_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (_tmp1_, EDSF_TYPE_PERSONA_STORE) ? ((EdsfPersonaStore*) _tmp1_) : NULL);
	e_store = _tmp2_;
	_tmp3_ = e_store;
	if (_tmp3_ != NULL) {
		ESourceRegistry* _tmp4_;
		EdsfPersonaStore* _tmp5_;
		ESource* _tmp6_;
		ESource* _tmp7_;
		GSettings* settings = NULL;
		GSettings* _tmp8_;
		GSettings* _tmp9_;
		EdsfPersonaStore* _tmp10_;
		const gchar* _tmp11_;
		const gchar* _tmp12_;
		gchar* _tmp13_;
		gchar* _tmp14_;
		_tmp4_ = contacts_eds_source_registry;
		_tmp5_ = e_store;
		_tmp6_ = edsf_persona_store_get_source (_tmp5_);
		_tmp7_ = _tmp6_;
		e_source_registry_set_default_address_book (_tmp4_, _tmp7_);
		_tmp8_ = g_settings_new ("org.freedesktop.folks");
		settings = _tmp8_;
		_tmp9_ = settings;
		_tmp10_ = e_store;
		_tmp11_ = folks_persona_store_get_id ((FolksPersonaStore*) _tmp10_);
		_tmp12_ = _tmp11_;
		_tmp13_ = g_strdup_printf ("eds:%s", _tmp12_);
		_tmp14_ = _tmp13_;
		g_settings_set_string (_tmp9_, "primary-store", _tmp14_);
		_g_free0 (_tmp14_);
		_g_object_unref0 (settings);
	}
	_g_object_unref0 (e_store);
}

static void
contacts_addressbook_dialog_class_init (ContactsAddressbookDialogClass * klass,
                                        gpointer klass_data)
{
	contacts_addressbook_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsAddressbookDialog_private_offset);
	((GtkDialogClass *) klass)->response = (void (*) (GtkDialog*, gint)) contacts_addressbook_dialog_real_response;
	G_OBJECT_CLASS (klass)->finalize = contacts_addressbook_dialog_finalize;
}

static void
contacts_addressbook_dialog_instance_init (ContactsAddressbookDialog * self,
                                           gpointer klass)
{
	self->priv = contacts_addressbook_dialog_get_instance_private (self);
}

static void
contacts_addressbook_dialog_finalize (GObject * obj)
{
	ContactsAddressbookDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_ADDRESSBOOK_DIALOG, ContactsAddressbookDialog);
	_g_object_unref0 (self->priv->accounts_list);
	G_OBJECT_CLASS (contacts_addressbook_dialog_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
contacts_addressbook_dialog_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsAddressbookDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_addressbook_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsAddressbookDialog), 0, (GInstanceInitFunc) contacts_addressbook_dialog_instance_init, NULL };
	GType contacts_addressbook_dialog_type_id;
	contacts_addressbook_dialog_type_id = g_type_register_static (gtk_dialog_get_type (), "ContactsAddressbookDialog", &g_define_type_info, 0);
	ContactsAddressbookDialog_private_offset = g_type_add_instance_private (contacts_addressbook_dialog_type_id, sizeof (ContactsAddressbookDialogPrivate));
	return contacts_addressbook_dialog_type_id;
}

GType
contacts_addressbook_dialog_get_type (void)
{
	static volatile gsize contacts_addressbook_dialog_type_id__once = 0;
	if (g_once_init_enter (&contacts_addressbook_dialog_type_id__once)) {
		GType contacts_addressbook_dialog_type_id;
		contacts_addressbook_dialog_type_id = contacts_addressbook_dialog_get_type_once ();
		g_once_init_leave (&contacts_addressbook_dialog_type_id__once, contacts_addressbook_dialog_type_id);
	}
	return contacts_addressbook_dialog_type_id__once;
}

