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

#include <glib/gi18n-lib.h>

struct _PurpleAttachment {
	GObject parent;

	char *id;
	char *content_type;
	gboolean is_inline;

	char *local_uri;
	char *remote_uri;

	guint64 size;
};

enum {
	PROP_0,
	PROP_ID,
	PROP_CONTENT_TYPE,
	PROP_INLINE,
	PROP_LOCAL_URI,
	PROP_REMOTE_URI,
	PROP_SIZE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleAttachment, purple_attachment, G_TYPE_OBJECT);

static void
purple_attachment_get_property(GObject *obj, guint prop_id, GValue *value,
                               GParamSpec *pspec)
{
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	switch(prop_id) {
	case PROP_ID:
		g_value_set_string(value, purple_attachment_get_id(attachment));
		break;
	case PROP_CONTENT_TYPE:
		g_value_set_string(value,
		                   purple_attachment_get_content_type(attachment));
		break;
	case PROP_INLINE:
		g_value_set_boolean(value, purple_attachment_get_is_inline(attachment));
		break;
	case PROP_LOCAL_URI:
		g_value_set_string(value, purple_attachment_get_local_uri(attachment));
		break;
	case PROP_REMOTE_URI:
		g_value_set_string(value,
		                   purple_attachment_get_remote_uri(attachment));
		break;
	case PROP_SIZE:
		g_value_set_uint64(value, purple_attachment_get_size(attachment));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
		break;
	}
}

static void
purple_attachment_set_property(GObject *obj, guint prop_id,
                               const GValue *value, GParamSpec *pspec)
{
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	switch(prop_id) {
	case PROP_ID:
		purple_attachment_set_id(attachment, g_value_get_string(value));
		break;
	case PROP_CONTENT_TYPE:
		purple_attachment_set_content_type(attachment,
		                                   g_value_get_string(value));
		break;
	case PROP_INLINE:
		purple_attachment_set_is_inline(attachment, g_value_get_boolean(value));
		break;
	case PROP_LOCAL_URI:
		purple_attachment_set_local_uri(attachment, g_value_get_string(value));
		break;
	case PROP_REMOTE_URI:
		purple_attachment_set_remote_uri(attachment,
		                                 g_value_get_string(value));
		break;
	case PROP_SIZE:
		purple_attachment_set_size(attachment, g_value_get_uint64(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
		break;
	}
}

static void
purple_attachment_finalize(GObject *obj) {
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	g_clear_pointer(&attachment->id, g_free);
	g_clear_pointer(&attachment->content_type, g_free);
	g_clear_pointer(&attachment->local_uri, g_free);
	g_clear_pointer(&attachment->remote_uri, g_free);

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

static void
purple_attachment_init(G_GNUC_UNUSED PurpleAttachment *attachment) {
}

static void
purple_attachment_class_init(PurpleAttachmentClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_attachment_get_property;
	obj_class->set_property = purple_attachment_set_property;
	obj_class->finalize = purple_attachment_finalize;

	/**
	 * PurpleAttachment:id:
	 *
	 * The identifier of this attachment.
	 *
	 * Since: 3.0
	 */
	properties[PROP_ID] = g_param_spec_string(
		"id", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAttachment:content-type:
	 *
	 * The content type of this attachment. This should be a commonly used
	 * mime type.
	 *
	 * Since: 3.0
	 */
	properties[PROP_CONTENT_TYPE] = g_param_spec_string(
		"content-type", NULL, NULL,
		"application/octet-stream",
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAttachment:is-inline:
	 *
	 * Whether or not the attachment should be shown inline.
	 *
	 * This property is a hint that user interfaces may use to only show this
	 * attachment inline with message content instead of as an external
	 * attachment.
	 *
	 * Since: 3.0
	 */
	properties[PROP_INLINE] = g_param_spec_boolean(
		"is-inline", NULL, NULL,
		FALSE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAttachment:local-uri:
	 *
	 * The local URI of the attachment. That is where the file can be found on
	 * disk.
	 *
	 * Since: 3.0
	 */
	properties[PROP_LOCAL_URI] = g_param_spec_string(
		"local-uri", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAttachment:remote-uri:
	 *
	 * The remote URI of the attachment. In most cases this is just a base
	 * filename.
	 *
	 * Since: 3.0
	 */
	properties[PROP_REMOTE_URI] = g_param_spec_string(
		"remote-uri", NULL, NULL,
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAttachment:size:
	 *
	 * The size of the attachment in bytes.
	 *
	 * Since: 3.0
	 */
	properties[PROP_SIZE] = g_param_spec_uint64(
		"size", NULL, NULL,
		0, G_MAXUINT64, 0,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleAttachment *
purple_attachment_new(const char *id, const char *content_type) {
	g_return_val_if_fail(content_type != NULL, NULL);

	return g_object_new(
		PURPLE_TYPE_ATTACHMENT,
		"id", id,
		"content-type", content_type,
		NULL);
}

gboolean
purple_attachment_equal(PurpleAttachment *attachment1,
                        PurpleAttachment *attachment2)
{
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment1), FALSE);
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment2), FALSE);

	return birb_str_equal(attachment1->id, attachment2->id);
}

const char *
purple_attachment_get_id(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);

	return attachment->id;
}

void
purple_attachment_set_id(PurpleAttachment *attachment, const char *id) {
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(g_set_str(&attachment->id, id)) {
		g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_ID]);
	}
}

gboolean
purple_attachment_get_is_inline(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), FALSE);

	return attachment->is_inline;
}

void
purple_attachment_set_is_inline(PurpleAttachment *attachment,
                                gboolean is_inline)
{
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(attachment->is_inline != is_inline) {
		attachment->is_inline = is_inline;

		g_object_notify_by_pspec(G_OBJECT(attachment),
		                         properties[PROP_INLINE]);
	}
}

const char *
purple_attachment_get_content_type(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->content_type;
}

void
purple_attachment_set_content_type(PurpleAttachment *attachment,
                                   const char *content_type)
{
	if(g_set_str(&attachment->content_type, content_type)) {
		g_object_notify_by_pspec(G_OBJECT(attachment),
		                         properties[PROP_CONTENT_TYPE]);
	}
}

const char *
purple_attachment_get_local_uri(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->local_uri;
}

void
purple_attachment_set_local_uri(PurpleAttachment *attachment,
                                const char *local_uri)
{
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(g_set_str(&attachment->local_uri, local_uri)) {
		g_object_notify_by_pspec(G_OBJECT(attachment),
		                         properties[PROP_LOCAL_URI]);
	}
}

const char *
purple_attachment_get_remote_uri(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->remote_uri;
}

void
purple_attachment_set_remote_uri(PurpleAttachment *attachment,
                                 const char *remote_uri)
{
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(g_set_str(&attachment->remote_uri, remote_uri)) {
		g_object_notify_by_pspec(G_OBJECT(attachment),
		                         properties[PROP_REMOTE_URI]);
	}
}

guint64
purple_attachment_get_size(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);

	return attachment->size;
}

void
purple_attachment_set_size(PurpleAttachment *attachment, guint64 size) {
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	attachment->size = size;

	g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_SIZE]);
}

char *
purple_attachment_get_filename(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	if(attachment->remote_uri != NULL && attachment->remote_uri[0] != '\0') {
		return g_path_get_basename(attachment->remote_uri);
	}

	if(attachment->local_uri != NULL && attachment->local_uri[0] != '\0') {
		return g_path_get_basename(attachment->local_uri);
	}

	return g_strdup("unknown");
}
