/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include <birb.h>

#include "pidginqrcodedialog.h"

struct _PidginQrCodeDialog {
	GtkWindow parent;

	PurpleQrCode *qr_code;
};

enum {
	PROP_0,
	PROP_QR_CODE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
pidgin_qr_code_dialog_set_qr_code(PidginQrCodeDialog *dialog,
                                  PurpleQrCode *qr_code)
{
	g_return_if_fail(PIDGIN_IS_QR_CODE_DIALOG(dialog));

	if(g_set_object(&dialog->qr_code, qr_code)) {
		g_object_notify_by_pspec(G_OBJECT(dialog), properties[PROP_QR_CODE]);
	}
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static gboolean
pidgin_qr_code_dialog_activate_link(GtkLinkButton *self,
                                    G_GNUC_UNUSED gpointer data)
{
	PurpleUi *ui = NULL;
	const char *uri = NULL;

	uri = gtk_link_button_get_uri(self);

	ui = purple_core_get_ui(purple_core_get_default());
	purple_ui_open_uri(ui, uri, NULL, NULL, NULL);

	return TRUE;
}

/******************************************************************************
 * Closures
 *****************************************************************************/
static char *
pidgin_qr_code_visible_child_handler(G_GNUC_UNUSED GtkWidget *self,
                                     const char *text,
                                     G_GNUC_UNUSED gpointer data)
{
	const char *child = "text";

	if(g_uri_is_valid(text, G_URI_FLAGS_PARSE_RELAXED, NULL)) {
		child = "link";
	}

	return g_strdup(child);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginQrCodeDialog, pidgin_qr_code_dialog, GTK_TYPE_WINDOW)

static void
pidgin_qr_code_dialog_finalize(GObject *obj) {
	PidginQrCodeDialog *dialog = PIDGIN_QR_CODE_DIALOG(obj);

	g_clear_object(&dialog->qr_code);

	G_OBJECT_CLASS(pidgin_qr_code_dialog_parent_class)->finalize(obj);
}

static void
pidgin_qr_code_dialog_get_property(GObject *obj,
                                   guint param_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
	PidginQrCodeDialog *dialog = PIDGIN_QR_CODE_DIALOG(obj);

	switch(param_id) {
	case PROP_QR_CODE:
		g_value_set_object(value, pidgin_qr_code_dialog_get_qr_code(dialog));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_qr_code_dialog_set_property(GObject *obj, guint param_id,
                              const GValue *value, GParamSpec *pspec)
{
	PidginQrCodeDialog *dialog = PIDGIN_QR_CODE_DIALOG(obj);

	switch(param_id) {
	case PROP_QR_CODE:
		pidgin_qr_code_dialog_set_qr_code(dialog, g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_qr_code_dialog_init(PidginQrCodeDialog *dialog) {
	gtk_widget_init_template(GTK_WIDGET(dialog));
}

static void
pidgin_qr_code_dialog_class_init(PidginQrCodeDialogClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = pidgin_qr_code_dialog_finalize;
	obj_class->get_property = pidgin_qr_code_dialog_get_property;
	obj_class->set_property = pidgin_qr_code_dialog_set_property;

	/**
	 * PidginQrCodeDialog:qr-code:
	 *
	 * The QR Code being displayed.
	 *
	 * Since: 3.0
	 */
	properties[PROP_QR_CODE] = g_param_spec_object(
	    "qr-code", NULL, NULL,
	    PURPLE_TYPE_QR_CODE,
	    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/* Template stuff */
	gtk_widget_class_set_template_from_resource(widget_class,
	                                            "/im/pidgin/Pidgin3/qrcodedialog.ui");

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_qr_code_dialog_activate_link);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_qr_code_visible_child_handler);

	/* Keybindings. */
	gtk_widget_class_add_binding_action(widget_class,
	                                    GDK_KEY_Escape,
	                                    GDK_NO_MODIFIER_MASK,
	                                    "window.close",
	                                    NULL);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleQrCode *
pidgin_qr_code_dialog_get_qr_code(PidginQrCodeDialog *dialog) {
	g_return_val_if_fail(PIDGIN_IS_QR_CODE_DIALOG(dialog), NULL);

	return dialog->qr_code;
}

GtkWidget *
pidgin_qr_code_dialog_new(PurpleQrCode *qr_code) {
	g_return_val_if_fail(PURPLE_IS_QR_CODE(qr_code), NULL);

	return g_object_new(PIDGIN_TYPE_QR_CODE_DIALOG, "qr-code", qr_code, NULL);
}
