* Talkatu - GTK widgets for chat applications
* 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 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 "talkatu/talkatuattachmentdialog.h"
* SECTION:talkatuattachmentdialog
* @Title: Attachment Dialog
* @Short_description: A GtkDialog for sending message attachments.
* A #GtkDialog that allows the user to customize attachments for a message.
* TALKATU_TYPE_ATTACHMENT_DIALOG:
* The standard _get_type macro for #TalkatuAttachmentDialog.
* TalkatuAttachmentDialog:
* A #GtkDialog that allows the user to customize an image message.
struct _TalkatuAttachmentDialog {
TalkatuAttachment *attachment;
static GParamSpec *properties[N_PROPERTIES] = {NULL, };
/******************************************************************************
*****************************************************************************/
talkatu_attachment_dialog_set_attachment(TalkatuAttachmentDialog *dialog,
TalkatuAttachment *attachment)
if(g_set_object(&dialog->attachment, attachment)) {
if(TALKATU_IS_ATTACHMENT(attachment)) {
GIcon *preview = talkatu_attachment_get_preview(attachment);
gtk_image_set_from_gicon(GTK_IMAGE(dialog->preview), preview,
g_object_unref(G_OBJECT(preview));
filename = talkatu_attachment_get_filename(attachment);
gtk_label_set_text(GTK_LABEL(dialog->filename), filename);
g_object_notify_by_pspec(G_OBJECT(dialog),
properties[PROP_ATTACHMENT]);
talkatu_attachment_dialog_set_comment(TalkatuAttachmentDialog *dialog,
if(GTK_IS_ENTRY(dialog->comment)) {
gtk_entry_set_text(GTK_ENTRY(dialog->comment), comment);
g_object_notify_by_pspec(G_OBJECT(dialog), properties[PROP_COMMENT]);
/******************************************************************************
*****************************************************************************/
G_DEFINE_TYPE(TalkatuAttachmentDialog, talkatu_attachment_dialog, GTK_TYPE_DIALOG)
talkatu_attachment_dialog_get_property(GObject *obj, guint prop_id,
GValue *value, GParamSpec *pspec)
TalkatuAttachmentDialog *dialog = TALKATU_ATTACHMENT_DIALOG(obj);
g_value_take_object(value, talkatu_attachment_dialog_get_attachment(dialog));
g_value_set_string(value, talkatu_attachment_dialog_get_comment(dialog));
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
talkatu_attachment_dialog_set_property(GObject *obj, guint prop_id,
const GValue *value, GParamSpec *pspec)
TalkatuAttachmentDialog *dialog = TALKATU_ATTACHMENT_DIALOG(obj);
talkatu_attachment_dialog_set_attachment(dialog, g_value_get_object(value));
talkatu_attachment_dialog_set_comment(dialog,
g_value_get_string(value));
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
talkatu_attachment_dialog_finalize(GObject *obj) {
TalkatuAttachmentDialog *dialog = TALKATU_ATTACHMENT_DIALOG(obj);
g_clear_object(&dialog->attachment);
G_OBJECT_CLASS(talkatu_attachment_dialog_parent_class)->finalize(obj);
talkatu_attachment_dialog_init(TalkatuAttachmentDialog *dialog) {
gtk_widget_init_template(GTK_WIDGET(dialog));
talkatu_attachment_dialog_class_init(TalkatuAttachmentDialogClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
obj_class->get_property = talkatu_attachment_dialog_get_property;
obj_class->set_property = talkatu_attachment_dialog_set_property;
obj_class->finalize = talkatu_attachment_dialog_finalize;
properties[PROP_ATTACHMENT] = g_param_spec_object(
"attachment", "attachment",
"The attachment that this dialog is customizing",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
properties[PROP_COMMENT] = g_param_spec_string(
"The comment to add to the attachment",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
gtk_widget_class_set_template_from_resource(
"/org/imfreedom/keep/talkatu/talkatu/ui/attachmentdialog.ui"
gtk_widget_class_bind_template_child(widget_class, TalkatuAttachmentDialog, preview);
gtk_widget_class_bind_template_child(widget_class, TalkatuAttachmentDialog, filename);
gtk_widget_class_bind_template_child(widget_class, TalkatuAttachmentDialog, comment);
/******************************************************************************
*****************************************************************************/
* talkatu_attachment_dialog_new:
* @attachment: The #TalkatuAttachment we're customizing.
* @comment: A default comment to display.
* Creates a new #TalkatuAttachmentDialog with the given attachment and comment.
* Returns: (transfer full): The new #TalkatuAttachmentDialog.
talkatu_attachment_dialog_new(TalkatuAttachment *attachment,
return GTK_WIDGET(g_object_new(
TALKATU_TYPE_ATTACHMENT_DIALOG,
"attachment", attachment,
* talkatu_attachment_dialog_get_attachment:
* @dialog: The #TalkatuAttachmentDialog.
* Gets the #TalkatuAttachment from @dialog.
* Returns: (transfer full): The #TalkatuAttachment for the file that the user
talkatu_attachment_dialog_get_attachment(TalkatuAttachmentDialog *dialog) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT_DIALOG(dialog), NULL);
return g_object_ref(dialog->attachment);
* talkatu_attachment_dialog_get_comment:
* @dialog: The #TalkatuAttachmentDialog.
* Get the comment the user entered, or empty string if nothing was entered.
* Returns: The comment that the user entered.
talkatu_attachment_dialog_get_comment(TalkatuAttachmentDialog *dialog) {
g_return_val_if_fail(TALKATU_IS_ATTACHMENT_DIALOG(dialog), "");
if(GTK_IS_ENTRY(dialog->comment)) {
return gtk_entry_get_text(GTK_ENTRY(dialog->comment));