talkatu/talkatu
Clone
Summary
Browse
Changes
Graph
Move all attachment and send functionality from TalkatuView to TalkatuInput
2020-01-28, Gary Kramlich
f9989cac8f7a
Move all attachment and send functionality from TalkatuView to TalkatuInput
/*
* talkatu
* Copyright (C) 2017-2019 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 is a #GtkTextTag subclass that has a few additional properties
* that allows greater control of how text is rendered.
*/
struct
_TalkatuAttachment
{
GObject
parent
;
gchar
*
content_type
;
gchar
*
filename
;
GBytes
*
contents
;
GdkPixbuf
*
preview
;
gboolean
preview_ready
;
};
G_DEFINE_TYPE
(
TalkatuAttachment
,
talkatu_attachment
,
G_TYPE_OBJECT
);
enum
{
PROP_0
=
0
,
PROP_CONTENT_TYPE
,
PROP_FILENAME
,
PROP_CONTENTS
,
N_PROPERTIES
,
};
static
GParamSpec
*
properties
[
N_PROPERTIES
];
/******************************************************************************
* Private Setters
*****************************************************************************/
static
void
talkatu_attachment_set_content_type
(
TalkatuAttachment
*
attachment
,
const
gchar
*
content_type
)
{
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
]);
}
static
void
talkatu_attachment_set_filename
(
TalkatuAttachment
*
attachment
,
const
gchar
*
filename
)
{
g_clear_pointer
(
&
attachment
->
filename
,
g_free
);
attachment
->
filename
=
g_strdup
(
filename
);
g_object_notify_by_pspec
(
G_OBJECT
(
attachment
),
properties
[
PROP_FILENAME
]);
}
static
void
talkatu_attachment_set_contents
(
TalkatuAttachment
*
attachment
,
GBytes
*
contents
)
{
g_clear_pointer
(
&
attachment
->
contents
,
g_bytes_unref
);
if
(
contents
!=
NULL
)
{
attachment
->
contents
=
g_bytes_ref
(
contents
);
}
g_object_notify_by_pspec
(
G_OBJECT
(
attachment
),
properties
[
PROP_CONTENTS
]);
}
/******************************************************************************
* 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_CONTENT_TYPE
:
g_value_set_string
(
value
,
talkatu_attachment_get_content_type
(
attachment
));
break
;
case
PROP_FILENAME
:
g_value_set_string
(
value
,
talkatu_attachment_get_filename
(
attachment
));
break
;
case
PROP_CONTENTS
:
g_value_set_boxed
(
value
,
talkatu_attachment_get_contents
(
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_CONTENT_TYPE
:
talkatu_attachment_set_content_type
(
attachment
,
g_value_get_string
(
value
));
break
;
case
PROP_FILENAME
:
talkatu_attachment_set_filename
(
attachment
,
g_value_get_string
(
value
));
break
;
case
PROP_CONTENTS
:
talkatu_attachment_set_contents
(
attachment
,
g_value_get_boxed
(
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
->
filename
,
g_free
);
g_clear_pointer
(
&
attachment
->
contents
,
g_bytes_unref
);
g_clear_object
(
&
attachment
->
preview
);
G_OBJECT_CLASS
(
talkatu_attachment_parent_class
)
->
finalize
(
obj
);
}
static
void
talkatu_attachment_init
(
TalkatuAttachment
*
tag
)
{
}
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_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_FILENAME
]
=
g_param_spec_string
(
"filename"
,
"filename"
,
"The filename of the attachment"
,
"unknown"
,
G_PARAM_READWRITE
|
G_PARAM_CONSTRUCT_ONLY
|
G_PARAM_STATIC_STRINGS
);
properties
[
PROP_CONTENTS
]
=
g_param_spec_boxed
(
"contents"
,
"contents"
,
"The contents stored in a GBytes"
,
G_TYPE_BYTES
,
G_PARAM_READWRITE
|
G_PARAM_CONSTRUCT_ONLY
|
G_PARAM_STATIC_STRINGS
);
g_object_class_install_properties
(
obj_class
,
N_PROPERTIES
,
properties
);
}
/******************************************************************************
* Public API
*****************************************************************************/
/**
* talkatu_attachment_new:
* @content_type: The content type of the attachment.
* @filename: The filename of the attachment.
* @contents: A #GBytes that contains the data of the attachment.
*
* Creates a new attachment with @content_type and @contents.
*
* Returns: (transfer full): The new #TalkatuAttachment.
*/
TalkatuAttachment
*
talkatu_attachment_new
(
const
gchar
*
content_type
,
const
gchar
*
filename
,
GBytes
*
contents
)
{
g_return_val_if_fail
(
content_type
!=
NULL
,
NULL
);
g_return_val_if_fail
(
filename
!=
NULL
,
NULL
);
g_return_val_if_fail
(
contents
!=
NULL
,
NULL
);
return
TALKATU_ATTACHMENT
(
g_object_new
(
TALKATU_TYPE_ATTACHMENT
,
"content-type"
,
content_type
,
"filename"
,
filename
,
"contents"
,
contents
,
NULL
));
}
/**
* talkatu_attachment_new_from_pixbuf:
* @filename: The filename of the attachment.
* @pixbuf: The #GdkPixbuf to create an attachment for.
*
* Creates a new #TalkatuAttachment containing @pixbuf.
*
* Returns: (transfer full): The new #TalkatuAttachment.
*/
TalkatuAttachment
*
talkatu_attachment_new_from_pixbuf
(
const
gchar
*
filename
,
GdkPixbuf
*
pixbuf
)
{
TalkatuAttachment
*
attachment
=
NULL
;
GBytes
*
contents
=
NULL
;
GError
*
error
=
NULL
;
gchar
*
data
=
NULL
;
gsize
data_sz
;
g_return_val_if_fail
(
filename
!=
NULL
,
NULL
);
g_return_val_if_fail
(
GDK_IS_PIXBUF
(
pixbuf
),
NULL
);
if
(
!
gdk_pixbuf_save_to_buffer
(
pixbuf
,
&
data
,
&
data_sz
,
"png"
,
&
error
,
NULL
))
{
g_warning
(
_
(
"Failed to convert the pixbuf to a png image: %s"
),
error
->
message
?
error
->
message
:
_
(
"unknown error"
));
return
NULL
;
}
/* TODO: adjust filename for the forced png format since we don't know the
* original format of the image.
*/
contents
=
g_bytes_new_take
(
data
,
data_sz
);
attachment
=
talkatu_attachment_new
(
"image/png"
,
filename
,
contents
);
g_bytes_unref
(
contents
);
return
attachment
;
}
/**
* talkatu_attachment_get_content_type:
* @attachment: The #TalkatuAttachment instance.
*
* Returns the content type of the attachment.
*
* Returns: (transfer none): 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_filename:
* @attachment: The #TalkatuAttachment instance.
*
* Returns the filename of the attachment.
*
* Returns: (transfer none): The filename of @attachment.
*/
const
gchar
*
talkatu_attachment_get_filename
(
TalkatuAttachment
*
attachment
)
{
g_return_val_if_fail
(
TALKATU_IS_ATTACHMENT
(
attachment
),
NULL
);
return
attachment
->
filename
;
}
/**
* talkatu_attachment_get_contents:
* @attachment: The #TalkatuAttachment instance.
*
* Returns a new reference to the contents of @attachment.
*
* Returns: (transfer full): The contents of @attachment.
*/
GBytes
*
talkatu_attachment_get_contents
(
TalkatuAttachment
*
attachment
)
{
g_return_val_if_fail
(
TALKATU_IS_ATTACHMENT
(
attachment
),
NULL
);
if
(
attachment
->
contents
!=
NULL
)
{
return
g_bytes_ref
(
attachment
->
contents
);
}
return
NULL
;
}
/**
* talkatu_attachment_get_preview:
* @attachment: The #TalkatuAttachment instance.
*
* Create a #GdkPixbuf 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/"
))
{
return
g_bytes_icon_new
(
attachment
->
contents
);
}
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
);
}