1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* foundry-markup.c
*
* Copyright 2024 Christian Hergert <chergert@redhat.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.1 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 General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "foundry-markup.h"
struct _FoundryMarkup
{
GObject parent_instance;
GBytes *contents;
FoundryMarkupKind kind;
};
G_DEFINE_ENUM_TYPE (FoundryMarkupKind, foundry_markup_kind,
G_DEFINE_ENUM_VALUE (FOUNDRY_MARKUP_KIND_PLAINTEXT, "plaintext"),
G_DEFINE_ENUM_VALUE (FOUNDRY_MARKUP_KIND_MARKDOWN, "markdown"),
G_DEFINE_ENUM_VALUE (FOUNDRY_MARKUP_KIND_HTML, "html"),
G_DEFINE_ENUM_VALUE (FOUNDRY_MARKUP_KIND_PANGO, "pango"))
G_DEFINE_FINAL_TYPE (FoundryMarkup, foundry_markup, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_CONTENTS,
PROP_KIND,
N_PROPS
};
static GParamSpec *properties[N_PROPS];
FoundryMarkup *
foundry_markup_new (GBytes *contents,
FoundryMarkupKind kind)
{
g_return_val_if_fail (contents != NULL, NULL);
g_return_val_if_fail (kind <= FOUNDRY_MARKUP_KIND_PANGO, NULL);
return g_object_new (FOUNDRY_TYPE_MARKUP,
"contents", contents,
"kind", kind,
NULL);
}
static void
foundry_markup_finalize (GObject *object)
{
FoundryMarkup *self = (FoundryMarkup *)object;
g_clear_pointer (&self->contents, g_bytes_unref);
G_OBJECT_CLASS (foundry_markup_parent_class)->finalize (object);
}
static void
foundry_markup_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FoundryMarkup *self = FOUNDRY_MARKUP (object);
switch (prop_id)
{
case PROP_CONTENTS:
g_value_take_boxed (value, foundry_markup_dup_contents (self));
break;
case PROP_KIND:
g_value_set_enum (value, foundry_markup_get_kind (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_markup_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FoundryMarkup *self = FOUNDRY_MARKUP (object);
switch (prop_id)
{
case PROP_CONTENTS:
self->contents = g_value_dup_boxed (value);
break;
case PROP_KIND:
self->kind = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_markup_class_init (FoundryMarkupClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = foundry_markup_finalize;
object_class->get_property = foundry_markup_get_property;
object_class->set_property = foundry_markup_set_property;
properties[PROP_CONTENTS] =
g_param_spec_boxed ("contents", NULL, NULL,
G_TYPE_BYTES,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
properties[PROP_KIND] =
g_param_spec_enum ("kind", NULL, NULL,
FOUNDRY_TYPE_MARKUP_KIND,
FOUNDRY_MARKUP_KIND_PLAINTEXT,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
foundry_markup_init (FoundryMarkup *self)
{
}
/**
* foundry_markup_dup_contents:
* @self: a #FoundryMarkup
*
* Gets the contents bytes.
*
* Returns: (transfer full): a #GBytes containing the markup
*/
GBytes *
foundry_markup_dup_contents (FoundryMarkup *self)
{
g_return_val_if_fail (self != NULL, NULL);
return g_bytes_ref (self->contents);
}
FoundryMarkupKind
foundry_markup_get_kind (FoundryMarkup *self)
{
g_return_val_if_fail (self != NULL, 0);
return self->kind;
}
FoundryMarkup *
foundry_markup_new_plaintext (const char *message)
{
g_autoptr(GBytes) contents = NULL;
g_return_val_if_fail (message != NULL, NULL);
contents = g_bytes_new_take (g_strdup (message), strlen (message));
return foundry_markup_new (contents, FOUNDRY_MARKUP_KIND_PLAINTEXT);
}
/**
* foundry_markup_to_string:
* @self: a [class@Foundry.Markup]
* @length: (out): length of resulting string
*
* Returns: (transfer full) (nullable):
*/
char *
foundry_markup_to_string (FoundryMarkup *self,
gsize *length)
{
g_return_val_if_fail (FOUNDRY_IS_MARKUP (self), NULL);
if (self->contents == NULL)
{
if (length != NULL)
*length = 0;
return NULL;
}
else
{
if (length != NULL)
*length = g_bytes_get_size (self->contents);
return g_strndup (g_bytes_get_data (self->contents, NULL),
g_bytes_get_size (self->contents));
}
}
|