| 1 |
/* font-manager-json-proxy.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2022 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-json-proxy.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-json-proxy |
| 25 |
* @short_description: Wrapper class for JsonObjects |
| 26 |
* @title: JSON Proxy |
| 27 |
* @include: font-manager-json-proxy.h |
| 28 |
* |
| 29 |
* Simple wrapper around a #JsonObject which maps object members to gobject properties. |
| 30 |
*/ |
| 31 |
|
| 32 |
typedef struct |
| 33 |
{ |
| 34 |
JsonObject *source_object; |
| 35 |
} |
| 36 |
FontManagerJsonProxyPrivate; |
| 37 |
|
| 38 |
G_DEFINE_TYPE_WITH_PRIVATE(FontManagerJsonProxy, font_manager_json_proxy, G_TYPE_OBJECT) |
| 39 |
|
| 40 |
static void |
| 41 |
font_manager_json_proxy_dispose (GObject *gobject) |
| 42 |
{ |
| 43 |
g_return_if_fail(gobject != NULL); |
| 44 |
FontManagerJsonProxy *self = FONT_MANAGER_JSON_PROXY(gobject); |
| 45 |
FontManagerJsonProxyPrivate *priv; |
| 46 |
priv = font_manager_json_proxy_get_instance_private(self); |
| 47 |
g_clear_pointer(&priv->source_object, json_object_unref); |
| 48 |
G_OBJECT_CLASS(font_manager_json_proxy_parent_class)->dispose(gobject); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
font_manager_json_proxy_set_property (GObject *gobject, |
| 54 |
guint property_id, |
| 55 |
const GValue *value, |
| 56 |
GParamSpec *pspec) |
| 57 |
{ |
| 58 |
g_return_if_fail(gobject != NULL); |
| 59 |
FontManagerJsonProxy *self = FONT_MANAGER_JSON_PROXY(gobject); |
| 60 |
FontManagerJsonProxyPrivate *priv; |
| 61 |
priv = font_manager_json_proxy_get_instance_private(self); |
| 62 |
GType val_type = G_PARAM_SPEC_VALUE_TYPE(pspec); |
| 63 |
JsonObject *src = priv->source_object; |
| 64 |
|
| 65 |
if (val_type == JSON_TYPE_OBJECT) { |
| 66 |
JsonObject *json_obj = g_value_get_boxed(value); |
| 67 |
if (src == json_obj) |
| 68 |
return; |
| 69 |
if (src != NULL) |
| 70 |
json_object_unref(src); |
| 71 |
priv->source_object = json_obj ? json_object_ref(json_obj) : NULL; |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if (src == NULL) |
| 76 |
return; |
| 77 |
|
| 78 |
if (val_type == G_TYPE_STRING) { |
| 79 |
json_object_set_string_member(src, pspec->name, g_value_get_string(value)); |
| 80 |
} else if (val_type == G_TYPE_INT64) { |
| 81 |
json_object_set_int_member(src, pspec->name, g_value_get_int64(value)); |
| 82 |
} else if (val_type == G_TYPE_DOUBLE) { |
| 83 |
json_object_set_double_member(src, pspec->name, g_value_get_double(value)); |
| 84 |
} else if (val_type == G_TYPE_BOOLEAN) { |
| 85 |
json_object_set_boolean_member(src, pspec->name, g_value_get_boolean(value)); |
| 86 |
} else if (val_type == JSON_TYPE_ARRAY) { |
| 87 |
json_object_set_array_member(src, pspec->name, g_value_get_boxed(value)); |
| 88 |
} else { |
| 89 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 90 |
} |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
font_manager_json_proxy_get_property (GObject *gobject, |
| 97 |
guint property_id, |
| 98 |
GValue *value, |
| 99 |
GParamSpec *pspec) |
| 100 |
{ |
| 101 |
g_return_if_fail(gobject != NULL); |
| 102 |
FontManagerJsonProxy *self = FONT_MANAGER_JSON_PROXY(gobject); |
| 103 |
FontManagerJsonProxyPrivate *priv; |
| 104 |
priv = font_manager_json_proxy_get_instance_private(self); |
| 105 |
GType val_type = G_PARAM_SPEC_VALUE_TYPE(pspec); |
| 106 |
JsonObject *src = priv->source_object; |
| 107 |
|
| 108 |
if (src == NULL) |
| 109 |
return; |
| 110 |
|
| 111 |
if (!json_object_has_member(src, pspec->name) && val_type != JSON_TYPE_OBJECT) |
| 112 |
return; |
| 113 |
|
| 114 |
if (val_type == G_TYPE_STRING) { |
| 115 |
g_value_set_string(value, json_object_get_string_member(src, pspec->name)); |
| 116 |
} else if (val_type == G_TYPE_INT64) { |
| 117 |
g_value_set_int64(value, json_object_get_int_member(src, pspec->name)); |
| 118 |
} else if (val_type == G_TYPE_DOUBLE) { |
| 119 |
g_value_set_double(value, json_object_get_double_member(src, pspec->name)); |
| 120 |
} else if (val_type == G_TYPE_BOOLEAN) { |
| 121 |
g_value_set_boolean(value, json_object_get_boolean_member(src, pspec->name)); |
| 122 |
} else if (val_type == JSON_TYPE_ARRAY) { |
| 123 |
g_value_set_boxed(value, json_object_get_array_member(src, pspec->name)); |
| 124 |
} else if (val_type == JSON_TYPE_OBJECT) { |
| 125 |
g_value_set_boxed(value, src); |
| 126 |
} else { |
| 127 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 128 |
} |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
static void |
| 135 |
font_manager_json_proxy_class_init (FontManagerJsonProxyClass *klass) |
| 136 |
{ |
| 137 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 138 |
object_class->dispose = font_manager_json_proxy_dispose; |
| 139 |
object_class->get_property = font_manager_json_proxy_get_property; |
| 140 |
object_class->set_property = font_manager_json_proxy_set_property; |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
font_manager_json_proxy_init (FontManagerJsonProxy *self) |
| 146 |
{ |
| 147 |
g_return_if_fail(self != NULL); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* font_manager_json_proxy_install_properties: (skip) |
| 152 |
* @klass: #FontManagerJsonProxyClass |
| 153 |
* |
| 154 |
* properties and n_properties MUST be set before calling this method. |
| 155 |
* |
| 156 |
* A property with type G_TYPE_BOXED is assumed to be a #JsonArray. |
| 157 |
* A property with type JSON_TYPE_OBJECT is assumed to be the @source-object. |
| 158 |
*/ |
| 159 |
void |
| 160 |
font_manager_json_proxy_install_properties (FontManagerJsonProxyClass *klass) |
| 161 |
{ |
| 162 |
|
| 163 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 164 |
GParamFlags OBJECT_PARAM_FLAGS = (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 165 |
|
| 166 |
for (gint i = 0; i < klass->n_properties; i++) { |
| 167 |
switch (klass->properties[i].type) { |
| 168 |
case G_TYPE_INT64: |
| 169 |
g_object_class_install_property(object_class, i, |
| 170 |
g_param_spec_int64(klass->properties[i].name, |
| 171 |
NULL, |
| 172 |
klass->properties[i].desc, |
| 173 |
G_MININT, G_MAXINT, 0, |
| 174 |
OBJECT_PARAM_FLAGS)); |
| 175 |
break; |
| 176 |
case G_TYPE_DOUBLE: |
| 177 |
g_object_class_install_property(object_class, i, |
| 178 |
g_param_spec_double(klass->properties[i].name, |
| 179 |
NULL, |
| 180 |
klass->properties[i].desc, |
| 181 |
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, |
| 182 |
OBJECT_PARAM_FLAGS)); |
| 183 |
break; |
| 184 |
case G_TYPE_BOOLEAN: |
| 185 |
g_object_class_install_property(object_class, i, |
| 186 |
g_param_spec_boolean(klass->properties[i].name, |
| 187 |
NULL, |
| 188 |
klass->properties[i].desc, |
| 189 |
FALSE, |
| 190 |
OBJECT_PARAM_FLAGS)); |
| 191 |
break; |
| 192 |
case G_TYPE_STRING: |
| 193 |
g_object_class_install_property(object_class, i, |
| 194 |
g_param_spec_string(klass->properties[i].name, |
| 195 |
NULL, |
| 196 |
klass->properties[i].desc, |
| 197 |
NULL, |
| 198 |
OBJECT_PARAM_FLAGS)); |
| 199 |
break; |
| 200 |
case G_TYPE_BOXED: |
| 201 |
g_object_class_install_property(object_class, i, |
| 202 |
g_param_spec_boxed(klass->properties[i].name, |
| 203 |
NULL, |
| 204 |
klass->properties[i].desc, |
| 205 |
JSON_TYPE_ARRAY, |
| 206 |
OBJECT_PARAM_FLAGS)); |
| 207 |
break; |
| 208 |
case G_TYPE_RESERVED_USER_FIRST: |
| 209 |
g_object_class_install_property(object_class, i, |
| 210 |
g_param_spec_boxed(klass->properties[i].name, |
| 211 |
NULL, |
| 212 |
klass->properties[i].desc, |
| 213 |
JSON_TYPE_OBJECT, |
| 214 |
OBJECT_PARAM_FLAGS)); |
| 215 |
break; |
| 216 |
default: |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* font_manager_json_proxy_new: |
| 225 |
* |
| 226 |
* Returns: (transfer full): A newly created #FontManagerJsonProxy. |
| 227 |
* Free the returned object using #g_object_unref(). |
| 228 |
**/ |
| 229 |
FontManagerJsonProxy * |
| 230 |
font_manager_json_proxy_new (void) |
| 231 |
{ |
| 232 |
return g_object_new(FONT_MANAGER_TYPE_JSON_PROXY, NULL); |
| 233 |
} |