talkatu/talkatu

Fix a few things found in review

2020-06-29, Gary Kramlich
a831357ccddd
Fix a few things found in review
/*
* talkatu
* Copyright (C) 2017-2020 Gary Kramlich <grim@reaperworld.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "talkatu/talkatuattachment.h"
#include <glib/gi18n-lib.h>
/**
* SECTION:talkatuattachment
* @Title: Attachments
* @Short_description: Message Attachments
*
* Attachments can be images, files, etc that can be attached to a
* #TalkatuMessage.
*/
/**
* TALKATU_TYPE_ATTACHMENT:
*
* The standard _get_type macro for #TalkatuAttachment.
*/
/**
* TalkatuAttachment:
*
* TalkatuAttachment represents an attached file. The files can be any type of
* regular file but only images will be previewed based on their actual
* contents.
*/
struct _TalkatuAttachment {
GObject parent;
guint64 id;
gchar *content_type;
gchar *local_uri;
gchar *remote_uri;
guint64 size;
GdkPixbuf *preview;
gboolean preview_ready;
};
G_DEFINE_TYPE(TalkatuAttachment, talkatu_attachment, G_TYPE_OBJECT);
enum {
PROP_0 = 0,
PROP_ID,
PROP_CONTENT_TYPE,
PROP_LOCAL_URI,
PROP_REMOTE_URI,
PROP_SIZE,
N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES];
/******************************************************************************
* Private Setters
*****************************************************************************/
static void
talkatu_attachment_set_content_type(TalkatuAttachment *attachment,
const gchar *content_type)
{
if(attachment->content_type == content_type) {
return;
}
g_clear_pointer(&attachment->content_type, g_free);
attachment->content_type = g_strdup(content_type);
g_object_notify_by_pspec(G_OBJECT(attachment),
properties[PROP_CONTENT_TYPE]);
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
static void
talkatu_attachment_get_property(GObject *obj, guint prop_id, GValue *value, GParamSpec *pspec) {
TalkatuAttachment *attachment = TALKATU_ATTACHMENT(obj);
switch(prop_id) {
case PROP_ID:
g_value_set_uint64(value, talkatu_attachment_get_id(attachment));
break;
case PROP_CONTENT_TYPE:
g_value_set_string(value, talkatu_attachment_get_content_type(attachment));
break;
case PROP_LOCAL_URI:
g_value_set_string(value, talkatu_attachment_get_local_uri(attachment));
break;
case PROP_REMOTE_URI:
g_value_set_string(value, talkatu_attachment_get_remote_uri(attachment));
break;
case PROP_SIZE:
g_value_set_uint64(value, talkatu_attachment_get_size(attachment));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
talkatu_attachment_set_property(GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec) {
TalkatuAttachment *attachment = TALKATU_ATTACHMENT(obj);
switch(prop_id) {
case PROP_ID:
talkatu_attachment_set_id(attachment, g_value_get_uint64(value));
break;
case PROP_CONTENT_TYPE:
talkatu_attachment_set_content_type(attachment, g_value_get_string(value));
break;
case PROP_LOCAL_URI:
talkatu_attachment_set_local_uri(attachment, g_value_get_string(value));
break;
case PROP_REMOTE_URI:
talkatu_attachment_set_remote_uri(attachment, g_value_get_string(value));
break;
case PROP_SIZE:
talkatu_attachment_set_size(attachment, g_value_get_uint64(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
talkatu_attachment_finalize(GObject *obj) {
TalkatuAttachment *attachment = TALKATU_ATTACHMENT(obj);
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_clear_object(&attachment->preview);
G_OBJECT_CLASS(talkatu_attachment_parent_class)->finalize(obj);
}
static void
talkatu_attachment_init(TalkatuAttachment *attachment) {
}
static void
talkatu_attachment_class_init(TalkatuAttachmentClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
obj_class->get_property = talkatu_attachment_get_property;
obj_class->set_property = talkatu_attachment_set_property;
obj_class->finalize = talkatu_attachment_finalize;
/* add our properties */
properties[PROP_ID] = g_param_spec_uint64(
"id", "id", "The identifier of the attachment",
0, G_MAXUINT64, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
);
properties[PROP_CONTENT_TYPE] = g_param_spec_string(
"content-type", "content-type", "The content type of the attachment",
"application/octet-stream",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
);
properties[PROP_LOCAL_URI] = g_param_spec_string(
"local-uri", "local-uri", "The local URI of the attachment",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
);
properties[PROP_REMOTE_URI] = g_param_spec_string(
"remote-uri", "remote-uri", "The remote URI of the attachment",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
);
properties[PROP_SIZE] = g_param_spec_uint64(
"size", "size", "The file size of the attachment in bytes",
0, G_MAXUINT64, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}
/******************************************************************************
* Public API
*****************************************************************************/
/**
* talkatu_attachment_new:
* @id: The identifier of the attachment.
* @content_type: The content type of the attachment.
*
* Creates a new attachment with @content_type.
*
* Returns: (transfer full): The new #TalkatuAttachment.
*/
TalkatuAttachment *
talkatu_attachment_new(guint64 id, const gchar *content_type) {
g_return_val_if_fail(content_type != NULL, NULL);
return TALKATU_ATTACHMENT(g_object_new(
TALKATU_TYPE_ATTACHMENT,
"id", id,
"content-type", content_type,
NULL
));
}
/**
* talkatu_attachment_get_id:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the ID associated with @attachment.
*
* Returns: The ID of @attachment.
*/
guint64
talkatu_attachment_get_id(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), 0);
return attachment->id;
}
/**
* talkatu_attachment_get_hash_key:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the hash key of @attachment. This should only be used when
* trying to address a #TalkatuAttachment in a #GHashTable that is using
* #g_int64_hash as the key function.
*
* Returns: (transfer none): The hash key of @attachment.
*/
guint64 *
talkatu_attachment_get_hash_key(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
return &attachment->id;
}
/**
* talkatu_attachment_set_id:
* @attachment: The #TalkatuAttachment instance.
* @id: The new ID for @attachment.
*
* Sets the ID of @attachment to @id.
*/
void
talkatu_attachment_set_id(TalkatuAttachment *attachment, guint64 id) {
g_return_if_fail(TALKATU_IS_ATTACHMENT(attachment));
if(attachment->id == id) {
return;
}
attachment->id = id;
g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_ID]);
}
/**
* talkatu_attachment_get_content_type:
* @attachment: The #TalkatuAttachment instance.
*
* Returns the content type of the attachment.
*
* Returns: The content type of @attachment.
*/
const gchar *
talkatu_attachment_get_content_type(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
return attachment->content_type;
}
/**
* talkatu_attachment_get_local_uri:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the local URI if any for @attachment.
*
* Returns: (nullable): The local URI for @attachment.
*/
const gchar *
talkatu_attachment_get_local_uri(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
return attachment->local_uri;
}
/**
* talkatu_attachment_set_local_uri:
* @attachment: The #TalkatuAttachment instance.
* @local_uri: The new local URI.
*
* Sets the local URI of @attachment.
*/
void
talkatu_attachment_set_local_uri(TalkatuAttachment *attachment,
const gchar *local_uri)
{
g_return_if_fail(TALKATU_IS_ATTACHMENT(attachment));
if(attachment->local_uri == local_uri) {
return;
}
g_free(attachment->local_uri);
if(local_uri != NULL) {
gchar *scheme = g_uri_parse_scheme(local_uri);
if(scheme == NULL) {
attachment->local_uri = g_filename_to_uri(local_uri, NULL, NULL);
} else {
g_free(scheme);
attachment->local_uri = g_strdup(local_uri);
}
} else {
attachment->local_uri = NULL;
}
g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_LOCAL_URI]);
}
/**
* talkatu_attachment_get_remote_uri:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the remote URI if any for @attachment.
*
* Returns: (nullable): The remote URI for @attachment.
*/
const gchar *
talkatu_attachment_get_remote_uri(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
return attachment->remote_uri;
}
/**
* talkatu_attachment_set_remote_uri:
* @attachment: The #TalkatuAttachment instance.
* @remote_uri: The new remote URI.
*
* Sets the remote URI of @attachment.
*/
void
talkatu_attachment_set_remote_uri(TalkatuAttachment *attachment,
const gchar *remote_uri)
{
g_return_if_fail(TALKATU_IS_ATTACHMENT(attachment));
if(attachment->remote_uri == remote_uri) {
return;
}
g_free(attachment->remote_uri);
attachment->remote_uri = g_strdup(remote_uri);
g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_REMOTE_URI]);
}
/**
* talkatu_attachment_get_size:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the size of @attachment.
*
* Returns: The size of @attachment.
*/
guint64
talkatu_attachment_get_size(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), 0);
return attachment->size;
}
/**
* talkatu_attachment_set_size:
* @attachment: The #TalkatuAttachment instance.
* @size: The new size of @attachment.
*
* Sets the size of @attachment to @size.
*/
void
talkatu_attachment_set_size(TalkatuAttachment *attachment, guint64 size) {
g_return_if_fail(TALKATU_IS_ATTACHMENT(attachment));
attachment->size = size;
g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_SIZE]);
}
/**
* talkatu_attachment_get_filename:
* @attachment: The #TalkatuAttachment instance.
*
* Gets the base filename for @attachment. Remote URI will be check before
* local URI, but the basename of one of those is what will be returned.
*
* Returns: (transfer full): The filename for @attachment.
*/
gchar *
talkatu_attachment_get_filename(TalkatuAttachment *attachment) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
if(attachment->remote_uri != NULL &&
g_utf8_strlen(attachment->remote_uri, -1) > 0)
{
return g_path_get_basename(attachment->remote_uri);
}
if(attachment->local_uri != NULL &&
g_utf8_strlen(attachment->local_uri, -1) > 0)
{
return g_path_get_basename(attachment->local_uri);
}
return g_strdup("unknown");
}
/**
* talkatu_attachment_get_preview:
* @attachment: The #TalkatuAttachment instance.
*
* Create a #GIcon as a preview for @attachment.
*
* Returns: (transfer full): A preview image of @attachment.
*/
GIcon *
talkatu_attachment_get_preview(TalkatuAttachment *attachment) {
const gchar *name = "text-x-generic-template";
g_return_val_if_fail(TALKATU_IS_ATTACHMENT(attachment), NULL);
if(g_str_has_prefix(attachment->content_type, "image/")) {
if(attachment->local_uri != NULL) {
GFile *file = g_file_new_for_uri(attachment->local_uri);
GIcon *icon = g_file_icon_new(file);
g_object_unref(G_OBJECT(file));
return icon;
}
name = "image-x-generic";
} else if(g_str_has_prefix(attachment->content_type, "text/")) {
name = "text-x-generic";
} else if(g_str_has_prefix(attachment->content_type, "audio/")) {
name = "audio-x-generic";
}
return g_themed_icon_new(name);
}