/* contacts-link-suggestion-grid.c generated by valac 0.56.0, the Vala compiler
 * generated from contacts-link-suggestion-grid.vala, do not modify */

/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.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 <glib.h>
#include <folks/folks.h>
#include <adwaita.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "contacts.h"
#include <glib/gi18n-lib.h>
#include <gee.h>

#define CONTACTS_LINK_SUGGESTION_GRID_AVATAR_SIZE 54
#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_LINK_SUGGESTION_GRID (contacts_link_suggestion_grid_get_type ())
#define CONTACTS_LINK_SUGGESTION_GRID(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_LINK_SUGGESTION_GRID, ContactsLinkSuggestionGrid))
#define CONTACTS_LINK_SUGGESTION_GRID_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_LINK_SUGGESTION_GRID, ContactsLinkSuggestionGridClass))
#define CONTACTS_IS_LINK_SUGGESTION_GRID(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_LINK_SUGGESTION_GRID))
#define CONTACTS_IS_LINK_SUGGESTION_GRID_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_LINK_SUGGESTION_GRID))
#define CONTACTS_LINK_SUGGESTION_GRID_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_LINK_SUGGESTION_GRID, ContactsLinkSuggestionGridClass))

typedef struct _ContactsLinkSuggestionGrid ContactsLinkSuggestionGrid;
typedef struct _ContactsLinkSuggestionGridClass ContactsLinkSuggestionGridClass;
typedef struct _ContactsLinkSuggestionGridPrivate ContactsLinkSuggestionGridPrivate;
enum  {
	CONTACTS_LINK_SUGGESTION_GRID_0_PROPERTY,
	CONTACTS_LINK_SUGGESTION_GRID_NUM_PROPERTIES
};
static GParamSpec* contacts_link_suggestion_grid_properties[CONTACTS_LINK_SUGGESTION_GRID_NUM_PROPERTIES];

#define CONTACTS_TYPE_AVATAR (contacts_avatar_get_type ())
#define CONTACTS_AVATAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_AVATAR, ContactsAvatar))
#define CONTACTS_AVATAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_AVATAR, ContactsAvatarClass))
#define CONTACTS_IS_AVATAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_AVATAR))
#define CONTACTS_IS_AVATAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_AVATAR))
#define CONTACTS_AVATAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_AVATAR, ContactsAvatarClass))

typedef struct _ContactsAvatar ContactsAvatar;
typedef struct _ContactsAvatarClass ContactsAvatarClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_ACCEPTED_SIGNAL,
	CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_REJECTED_SIGNAL,
	CONTACTS_LINK_SUGGESTION_GRID_NUM_SIGNALS
};
static guint contacts_link_suggestion_grid_signals[CONTACTS_LINK_SUGGESTION_GRID_NUM_SIGNALS] = {0};

struct _ContactsLinkSuggestionGrid {
	GtkGrid parent_instance;
	ContactsLinkSuggestionGridPrivate * priv;
};

struct _ContactsLinkSuggestionGridClass {
	GtkGridClass parent_class;
};

struct _ContactsLinkSuggestionGridPrivate {
	GtkLabel* description_label;
	GtkLabel* extra_info_label;
	GtkButton* accept_button;
	GtkButton* reject_button;
};

static gint ContactsLinkSuggestionGrid_private_offset;
static gpointer contacts_link_suggestion_grid_parent_class = NULL;

VALA_EXTERN GType contacts_link_suggestion_grid_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsLinkSuggestionGrid, g_object_unref)
VALA_EXTERN ContactsLinkSuggestionGrid* contacts_link_suggestion_grid_new (FolksIndividual* individual);
VALA_EXTERN ContactsLinkSuggestionGrid* contacts_link_suggestion_grid_construct (GType object_type,
                                                                     FolksIndividual* individual);
VALA_EXTERN GType contacts_avatar_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsAvatar, g_object_unref)
VALA_EXTERN ContactsAvatar* contacts_avatar_new (gint size,
                                     FolksIndividual* individual);
VALA_EXTERN ContactsAvatar* contacts_avatar_construct (GType object_type,
                                           gint size,
                                           FolksIndividual* individual);
static gchar* contacts_link_suggestion_grid_find_extra_description (ContactsLinkSuggestionGrid* self,
                                                             FolksIndividual* individual);
static void __lambda45_ (ContactsLinkSuggestionGrid* self);
static void ___lambda45__gtk_button_clicked (GtkButton* _sender,
                                      gpointer self);
static void __lambda46_ (ContactsLinkSuggestionGrid* self);
static void ___lambda46__gtk_button_clicked (GtkButton* _sender,
                                      gpointer self);
static void contacts_link_suggestion_grid_finalize (GObject * obj);
static GType contacts_link_suggestion_grid_get_type_once (void);

static inline gpointer
contacts_link_suggestion_grid_get_instance_private (ContactsLinkSuggestionGrid* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsLinkSuggestionGrid_private_offset);
}

static void
__lambda45_ (ContactsLinkSuggestionGrid* self)
{
	g_signal_emit (self, contacts_link_suggestion_grid_signals[CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_REJECTED_SIGNAL], 0);
}

static void
___lambda45__gtk_button_clicked (GtkButton* _sender,
                                 gpointer self)
{
	__lambda45_ ((ContactsLinkSuggestionGrid*) self);
}

static void
__lambda46_ (ContactsLinkSuggestionGrid* self)
{
	g_signal_emit (self, contacts_link_suggestion_grid_signals[CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_ACCEPTED_SIGNAL], 0);
}

static void
___lambda46__gtk_button_clicked (GtkButton* _sender,
                                 gpointer self)
{
	__lambda46_ ((ContactsLinkSuggestionGrid*) self);
}

ContactsLinkSuggestionGrid*
contacts_link_suggestion_grid_construct (GType object_type,
                                         FolksIndividual* individual)
{
	ContactsLinkSuggestionGrid * self = NULL;
	ContactsAvatar* image_frame = NULL;
	ContactsAvatar* _tmp0_;
	ContactsAvatar* _tmp1_;
	ContactsAvatar* _tmp2_;
	GtkLabel* _tmp3_;
	gchar* _tmp4_ = NULL;
	GtkLabel* _tmp13_;
	gchar* extra_info = NULL;
	gchar* _tmp14_;
	const gchar* _tmp15_;
	GtkButton* _tmp18_;
	GtkButton* _tmp19_;
	g_return_val_if_fail (individual != NULL, NULL);
	self = (ContactsLinkSuggestionGrid*) g_object_new (object_type, NULL);
	_tmp0_ = contacts_avatar_new (CONTACTS_LINK_SUGGESTION_GRID_AVATAR_SIZE, individual);
	g_object_ref_sink (_tmp0_);
	image_frame = _tmp0_;
	_tmp1_ = image_frame;
	gtk_widget_set_hexpand ((GtkWidget*) _tmp1_, FALSE);
	_tmp2_ = image_frame;
	gtk_grid_attach ((GtkGrid*) self, (GtkWidget*) _tmp2_, 0, 0, 1, 2);
	_tmp3_ = self->priv->description_label;
	gtk_label_set_xalign (_tmp3_, (gfloat) 0);
	if (contacts_utils_has_main_persona (individual)) {
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		gchar* _tmp9_;
		_tmp5_ = folks_individual_get_display_name (individual);
		_tmp6_ = _tmp5_;
		_tmp7_ = contacts_utils_format_persona_stores (individual);
		_tmp8_ = _tmp7_;
		_tmp9_ = g_strdup_printf (_ ("Is this the same person as %s from %s?"), _tmp6_, _tmp8_);
		_g_free0 (_tmp4_);
		_tmp4_ = _tmp9_;
		_g_free0 (_tmp8_);
	} else {
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		gchar* _tmp12_;
		_tmp10_ = folks_individual_get_display_name (individual);
		_tmp11_ = _tmp10_;
		_tmp12_ = g_strdup_printf (_ ("Is this the same person as %s?"), _tmp11_);
		_g_free0 (_tmp4_);
		_tmp4_ = _tmp12_;
	}
	_tmp13_ = self->priv->description_label;
	gtk_label_set_label (_tmp13_, _tmp4_);
	_tmp14_ = contacts_link_suggestion_grid_find_extra_description (self, individual);
	extra_info = _tmp14_;
	_tmp15_ = extra_info;
	if (_tmp15_ != NULL) {
		GtkLabel* _tmp16_;
		const gchar* _tmp17_;
		_tmp16_ = self->priv->extra_info_label;
		_tmp17_ = extra_info;
		gtk_label_set_label (_tmp16_, _tmp17_);
	}
	_tmp18_ = self->priv->reject_button;
	g_signal_connect_object (_tmp18_, "clicked", (GCallback) ___lambda45__gtk_button_clicked, self, 0);
	_tmp19_ = self->priv->accept_button;
	g_signal_connect_object (_tmp19_, "clicked", (GCallback) ___lambda46__gtk_button_clicked, self, 0);
	_g_free0 (extra_info);
	_g_free0 (_tmp4_);
	_g_object_unref0 (image_frame);
	return self;
}

ContactsLinkSuggestionGrid*
contacts_link_suggestion_grid_new (FolksIndividual* individual)
{
	return contacts_link_suggestion_grid_construct (CONTACTS_TYPE_LINK_SUGGESTION_GRID, individual);
}

static gchar*
contacts_link_suggestion_grid_find_extra_description (ContactsLinkSuggestionGrid* self,
                                                      FolksIndividual* individual)
{
	GeeSet* emails = NULL;
	GeeSet* _tmp0_;
	GeeSet* _tmp1_;
	GeeSet* _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp4_;
	GeeSet* urls = NULL;
	GeeSet* _tmp12_;
	GeeSet* _tmp13_;
	GeeSet* _tmp14_;
	gboolean _tmp15_;
	gboolean _tmp16_;
	GeeSet* phones = NULL;
	GeeSet* _tmp24_;
	GeeSet* _tmp25_;
	GeeSet* _tmp26_;
	gboolean _tmp27_;
	gboolean _tmp28_;
	GeeSet* addresses = NULL;
	GeeSet* _tmp36_;
	GeeSet* _tmp37_;
	GeeSet* _tmp38_;
	gboolean _tmp39_;
	gboolean _tmp40_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (individual != NULL, NULL);
	_tmp0_ = folks_email_details_get_email_addresses ((FolksEmailDetails*) individual);
	_tmp1_ = _tmp0_;
	emails = _tmp1_;
	_tmp2_ = emails;
	_tmp3_ = gee_collection_get_is_empty ((GeeCollection*) _tmp2_);
	_tmp4_ = _tmp3_;
	if (!_tmp4_) {
		GeeSet* _tmp5_;
		gpointer _tmp6_;
		FolksEmailFieldDetails* _tmp7_;
		gconstpointer _tmp8_;
		gconstpointer _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		_tmp5_ = emails;
		_tmp6_ = contacts_utils_get_first (FOLKS_TYPE_EMAIL_FIELD_DETAILS, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, (GeeCollection*) _tmp5_);
		_tmp7_ = (FolksEmailFieldDetails*) _tmp6_;
		_tmp8_ = folks_abstract_field_details_get_value ((FolksAbstractFieldDetails*) _tmp7_);
		_tmp9_ = _tmp8_;
		_tmp10_ = g_strdup ((const gchar*) _tmp9_);
		_tmp11_ = _tmp10_;
		_g_object_unref0 (_tmp7_);
		result = _tmp11_;
		return result;
	}
	_tmp12_ = folks_url_details_get_urls ((FolksUrlDetails*) individual);
	_tmp13_ = _tmp12_;
	urls = _tmp13_;
	_tmp14_ = urls;
	_tmp15_ = gee_collection_get_is_empty ((GeeCollection*) _tmp14_);
	_tmp16_ = _tmp15_;
	if (!_tmp16_) {
		GeeSet* _tmp17_;
		gpointer _tmp18_;
		FolksUrlFieldDetails* _tmp19_;
		gconstpointer _tmp20_;
		gconstpointer _tmp21_;
		gchar* _tmp22_;
		gchar* _tmp23_;
		_tmp17_ = urls;
		_tmp18_ = contacts_utils_get_first (FOLKS_TYPE_URL_FIELD_DETAILS, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, (GeeCollection*) _tmp17_);
		_tmp19_ = (FolksUrlFieldDetails*) _tmp18_;
		_tmp20_ = folks_abstract_field_details_get_value ((FolksAbstractFieldDetails*) _tmp19_);
		_tmp21_ = _tmp20_;
		_tmp22_ = g_strdup ((const gchar*) _tmp21_);
		_tmp23_ = _tmp22_;
		_g_object_unref0 (_tmp19_);
		result = _tmp23_;
		return result;
	}
	_tmp24_ = folks_phone_details_get_phone_numbers ((FolksPhoneDetails*) individual);
	_tmp25_ = _tmp24_;
	phones = _tmp25_;
	_tmp26_ = phones;
	_tmp27_ = gee_collection_get_is_empty ((GeeCollection*) _tmp26_);
	_tmp28_ = _tmp27_;
	if (!_tmp28_) {
		GeeSet* _tmp29_;
		gpointer _tmp30_;
		FolksPhoneFieldDetails* _tmp31_;
		gconstpointer _tmp32_;
		gconstpointer _tmp33_;
		gchar* _tmp34_;
		gchar* _tmp35_;
		_tmp29_ = phones;
		_tmp30_ = contacts_utils_get_first (FOLKS_TYPE_PHONE_FIELD_DETAILS, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, (GeeCollection*) _tmp29_);
		_tmp31_ = (FolksPhoneFieldDetails*) _tmp30_;
		_tmp32_ = folks_abstract_field_details_get_value ((FolksAbstractFieldDetails*) _tmp31_);
		_tmp33_ = _tmp32_;
		_tmp34_ = g_strdup ((const gchar*) _tmp33_);
		_tmp35_ = _tmp34_;
		_g_object_unref0 (_tmp31_);
		result = _tmp35_;
		return result;
	}
	_tmp36_ = folks_postal_address_details_get_postal_addresses ((FolksPostalAddressDetails*) individual);
	_tmp37_ = _tmp36_;
	addresses = _tmp37_;
	_tmp38_ = addresses;
	_tmp39_ = gee_collection_get_is_empty ((GeeCollection*) _tmp38_);
	_tmp40_ = _tmp39_;
	if (!_tmp40_) {
		GeeSet* _tmp41_;
		gpointer _tmp42_;
		FolksPostalAddressFieldDetails* _tmp43_;
		gconstpointer _tmp44_;
		gconstpointer _tmp45_;
		gchar* _tmp46_;
		gchar* _tmp47_;
		_tmp41_ = addresses;
		_tmp42_ = contacts_utils_get_first (FOLKS_TYPE_POSTAL_ADDRESS_FIELD_DETAILS, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, (GeeCollection*) _tmp41_);
		_tmp43_ = (FolksPostalAddressFieldDetails*) _tmp42_;
		_tmp44_ = folks_abstract_field_details_get_value ((FolksAbstractFieldDetails*) _tmp43_);
		_tmp45_ = _tmp44_;
		_tmp46_ = folks_postal_address_to_string ((FolksPostalAddress*) _tmp45_);
		_tmp47_ = _tmp46_;
		_g_object_unref0 (_tmp43_);
		result = _tmp47_;
		return result;
	}
	result = NULL;
	return result;
}

static void
contacts_link_suggestion_grid_class_init (ContactsLinkSuggestionGridClass * klass,
                                          gpointer klass_data)
{
	contacts_link_suggestion_grid_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsLinkSuggestionGrid_private_offset);
	G_OBJECT_CLASS (klass)->finalize = contacts_link_suggestion_grid_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Contacts/ui/contacts-link-suggestion-grid.ui");
	contacts_link_suggestion_grid_signals[CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_ACCEPTED_SIGNAL] = g_signal_new ("suggestion-accepted", CONTACTS_TYPE_LINK_SUGGESTION_GRID, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	contacts_link_suggestion_grid_signals[CONTACTS_LINK_SUGGESTION_GRID_SUGGESTION_REJECTED_SIGNAL] = g_signal_new ("suggestion-rejected", CONTACTS_TYPE_LINK_SUGGESTION_GRID, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "description_label", FALSE, ContactsLinkSuggestionGrid_private_offset + G_STRUCT_OFFSET (ContactsLinkSuggestionGridPrivate, description_label));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "extra_info_label", FALSE, ContactsLinkSuggestionGrid_private_offset + G_STRUCT_OFFSET (ContactsLinkSuggestionGridPrivate, extra_info_label));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "accept_button", FALSE, ContactsLinkSuggestionGrid_private_offset + G_STRUCT_OFFSET (ContactsLinkSuggestionGridPrivate, accept_button));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "reject_button", FALSE, ContactsLinkSuggestionGrid_private_offset + G_STRUCT_OFFSET (ContactsLinkSuggestionGridPrivate, reject_button));
}

static void
contacts_link_suggestion_grid_instance_init (ContactsLinkSuggestionGrid * self,
                                             gpointer klass)
{
	self->priv = contacts_link_suggestion_grid_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
contacts_link_suggestion_grid_finalize (GObject * obj)
{
	ContactsLinkSuggestionGrid * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_LINK_SUGGESTION_GRID, ContactsLinkSuggestionGrid);
	G_OBJECT_CLASS (contacts_link_suggestion_grid_parent_class)->finalize (obj);
}

/**
 * The LinkSuggestionGrid is show at the bottom of the ContactPane.
 * It offers the user the sugugestion of linking the currently shown contact
 * and another (hopefully) similar contact.
 */
 G_GNUC_NO_INLINE static GType
contacts_link_suggestion_grid_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsLinkSuggestionGridClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_link_suggestion_grid_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsLinkSuggestionGrid), 0, (GInstanceInitFunc) contacts_link_suggestion_grid_instance_init, NULL };
	GType contacts_link_suggestion_grid_type_id;
	contacts_link_suggestion_grid_type_id = g_type_register_static (gtk_grid_get_type (), "ContactsLinkSuggestionGrid", &g_define_type_info, 0);
	ContactsLinkSuggestionGrid_private_offset = g_type_add_instance_private (contacts_link_suggestion_grid_type_id, sizeof (ContactsLinkSuggestionGridPrivate));
	return contacts_link_suggestion_grid_type_id;
}

GType
contacts_link_suggestion_grid_get_type (void)
{
	static volatile gsize contacts_link_suggestion_grid_type_id__once = 0;
	if (g_once_init_enter (&contacts_link_suggestion_grid_type_id__once)) {
		GType contacts_link_suggestion_grid_type_id;
		contacts_link_suggestion_grid_type_id = contacts_link_suggestion_grid_get_type_once ();
		g_once_init_leave (&contacts_link_suggestion_grid_type_id__once, contacts_link_suggestion_grid_type_id);
	}
	return contacts_link_suggestion_grid_type_id__once;
}

