/*
 * 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 "purpleaccountsettingstring.h"

struct _PurpleAccountSettingString {
	PurpleAccountSetting parent;

	char *value;
};

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

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleAccountSettingString, purple_account_setting_string,
                    PURPLE_TYPE_ACCOUNT_SETTING)

static void
purple_account_setting_string_finalize(GObject *obj) {
	PurpleAccountSettingString *setting = PURPLE_ACCOUNT_SETTING_STRING(obj);

	g_clear_pointer(&setting->value, g_free);

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

static void
purple_account_setting_string_get_property(GObject *obj, guint param_id,
                                           GValue *value, GParamSpec *pspec)
{
	PurpleAccountSettingString *setting = PURPLE_ACCOUNT_SETTING_STRING(obj);

	switch(param_id) {
	case PROP_VALUE:
		g_value_set_string(value,
		                   purple_account_setting_string_get_value(setting));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_string_set_property(GObject *obj, guint param_id,
                                           const GValue *value,
                                           GParamSpec *pspec)
{
	PurpleAccountSettingString *setting = PURPLE_ACCOUNT_SETTING_STRING(obj);

	switch(param_id) {
	case PROP_VALUE:
		purple_account_setting_string_set_value(setting,
		                                        g_value_get_string(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
purple_account_setting_string_init(G_GNUC_UNUSED PurpleAccountSettingString *setting)
{
}

static void
purple_account_setting_string_class_init(PurpleAccountSettingStringClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_account_setting_string_finalize;
	obj_class->get_property = purple_account_setting_string_get_property;
	obj_class->set_property = purple_account_setting_string_set_property;

	/**
	 * PurpleAccountSettingString:value:
	 *
	 * The string value of the setting.
	 *
	 * Since: 3.0
	 */
	properties[PROP_VALUE] = g_param_spec_string(
		"value", 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_account_setting_string_get_value(PurpleAccountSettingString *setting) {
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_SETTING_STRING(setting), NULL);

	return setting->value;
}

PurpleAccountSetting *
purple_account_setting_string_new(const char *id, const char *label,
                                  const char *default_value)
{
	g_return_val_if_fail(!birb_str_is_empty(id), NULL);
	g_return_val_if_fail(!birb_str_is_empty(label), NULL);

	return g_object_new(
		PURPLE_TYPE_ACCOUNT_SETTING_STRING,
		"id", id,
		"label", label,
		"value", default_value,
		NULL);
}

void
purple_account_setting_string_set_value(PurpleAccountSettingString *setting,
                                        const char *value)
{
	g_return_if_fail(PURPLE_IS_ACCOUNT_SETTING_STRING(setting));

	if(g_set_str(&setting->value, value)) {
		g_object_notify_by_pspec(G_OBJECT(setting), properties[PROP_VALUE]);

		purple_account_setting_changed(PURPLE_ACCOUNT_SETTING(setting));
	}
}
