/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple 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 library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <birb.h>

#include "purpleqrcode.h"

struct _PurpleQrCode {
	GObject parent;

	char *alt_text;
	char *description;
	char *text;
	char *title;
};

enum {
	PROP_0,
	PROP_ALT_TEXT,
	PROP_DESCRIPTION,
	PROP_TEXT,
	PROP_TITLE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/*******************************************************************************
 * GObject Implementation
 ******************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleQrCode, purple_qr_code, G_TYPE_OBJECT)

static void
purple_qr_code_finalize(GObject *obj) {
	PurpleQrCode *qr_code = PURPLE_QR_CODE(obj);

	g_clear_pointer(&qr_code->alt_text, g_free);
	g_clear_pointer(&qr_code->description, g_free);
	g_clear_pointer(&qr_code->text, g_free);
	g_clear_pointer(&qr_code->title, g_free);

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

static void
purple_qr_code_get_property(GObject *obj,
                            guint param_id,
                            GValue *value,
                            GParamSpec *pspec)
{
	PurpleQrCode *qr_code = PURPLE_QR_CODE(obj);

	switch(param_id) {
	case PROP_ALT_TEXT:
		g_value_set_string(value, purple_qr_code_get_alt_text(qr_code));
		break;
	case PROP_DESCRIPTION:
		g_value_set_string(value, purple_qr_code_get_description(qr_code));
		break;
	case PROP_TEXT:
		g_value_set_string(value, purple_qr_code_get_text(qr_code));
		break;
	case PROP_TITLE:
		g_value_set_string(value, purple_qr_code_get_title(qr_code));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_qr_code_set_property(GObject *obj,
                            guint param_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
	PurpleQrCode *qr_code = PURPLE_QR_CODE(obj);

	switch(param_id) {
	case PROP_ALT_TEXT:
		purple_qr_code_set_alt_text(qr_code, g_value_get_string(value));
		break;
	case PROP_DESCRIPTION:
		purple_qr_code_set_description(qr_code, g_value_get_string(value));
		break;
	case PROP_TEXT:
		purple_qr_code_set_text(qr_code, g_value_get_string(value));
		break;
	case PROP_TITLE:
		purple_qr_code_set_title(qr_code, g_value_get_string(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_qr_code_init(G_GNUC_UNUSED PurpleQrCode *qr_code) {
}

static void
purple_qr_code_class_init(PurpleQrCodeClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_qr_code_finalize;
	obj_class->get_property = purple_qr_code_get_property;
	obj_class->set_property = purple_qr_code_set_property;

	/**
	 * PurpleQrCode:alt-text:
	 *
	 * Optional alternative text.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ALT_TEXT] = g_param_spec_string(
		"alt-text", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleQrCode:description:
	 *
	 * An optional description.
	 *
	 * Since: 3.0
	 */
	properties[PROP_DESCRIPTION] = g_param_spec_string(
		"description", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleQrCode:text:
	 *
	 * The text to encode as a QR Code.
	 *
	 * Since: 3.0
	 */
	properties[PROP_TEXT] = g_param_spec_string(
		"text", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * PurpleQrCode:title:
	 *
	 * The title.
	 *
	 * Since: 3.0
	 */
	properties[PROP_TITLE] = g_param_spec_string(
		"title", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/*******************************************************************************
 * Public API
 ******************************************************************************/
const char *
purple_qr_code_get_alt_text(PurpleQrCode *qr_code) {
	g_return_val_if_fail(PURPLE_IS_QR_CODE(qr_code), NULL);

	return qr_code->alt_text;
}

const char *
purple_qr_code_get_description(PurpleQrCode *qr_code) {
	g_return_val_if_fail(PURPLE_IS_QR_CODE(qr_code), NULL);

	return qr_code->description;
}

const char *
purple_qr_code_get_text(PurpleQrCode *qr_code) {
	g_return_val_if_fail(PURPLE_IS_QR_CODE(qr_code), NULL);

	return qr_code->text;
}

const char *
purple_qr_code_get_title(PurpleQrCode *qr_code) {
	g_return_val_if_fail(PURPLE_IS_QR_CODE(qr_code), NULL);

	return qr_code->title;
}

PurpleQrCode *
purple_qr_code_new(const char *title,
                   const char *description,
                   const char *text,
                   const char *alt_text)
{
	g_return_val_if_fail(!birb_str_is_empty(title), NULL);
	g_return_val_if_fail(!birb_str_is_empty(text), NULL);

	return g_object_new(PURPLE_TYPE_QR_CODE,
	                    "alt-text", alt_text,
	                    "description", description,
	                    "text", text,
	                    "title", title,
	                    NULL);
}

void
purple_qr_code_set_alt_text(PurpleQrCode *qr_code, const char *alt_text) {
	g_return_if_fail(PURPLE_IS_QR_CODE(qr_code));

	if(g_set_str(&qr_code->alt_text, alt_text)) {
		g_object_notify_by_pspec(G_OBJECT(qr_code), properties[PROP_ALT_TEXT]);
	}
}

void
purple_qr_code_set_description(PurpleQrCode *qr_code, const char *description) {
	g_return_if_fail(PURPLE_IS_QR_CODE(qr_code));

	if(g_set_str(&qr_code->description, description)) {
		g_object_notify_by_pspec(G_OBJECT(qr_code),
		                         properties[PROP_DESCRIPTION]);
	}
}

void
purple_qr_code_set_text(PurpleQrCode *qr_code, const char *text) {
	g_return_if_fail(PURPLE_IS_QR_CODE(qr_code));
	g_return_if_fail(!birb_str_is_empty(text));

	if(g_set_str(&qr_code->text, text)) {
		g_object_notify_by_pspec(G_OBJECT(qr_code), properties[PROP_TEXT]);
	}
}

void
purple_qr_code_set_title(PurpleQrCode *qr_code, const char *title) {
	g_return_if_fail(PURPLE_IS_QR_CODE(qr_code));
	g_return_if_fail(!birb_str_is_empty(title));

	if(g_set_str(&qr_code->title, title)) {
		g_object_notify_by_pspec(G_OBJECT(qr_code), properties[PROP_TITLE]);
	}
}
