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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/* foundry-contextual.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 <json-glib/json-glib.h>
#include "foundry-build-manager.h"
#include "foundry-context.h"
#include "foundry-contextual-private.h"
#include "foundry-inhibitor-private.h"
/**
* FoundryContextual:
*
* Abstract base class for objects that are associated with a Foundry context.
*
* FoundryContextual provides the core interface for objects that need
* access to the Foundry context and its services. It includes context
* management, serialization support, and provides a unified interface
* for context-aware objects throughout the development environment.
*/
typedef struct
{
GWeakRef context_wr;
} FoundryContextualPrivate;
enum {
PROP_0,
PROP_CONTEXT,
N_PROPS
};
G_DEFINE_QUARK (foundry-contextual, foundry_contextual_error)
static GParamSpec **
foundry_contextual_list_properties (JsonSerializable *serializable,
guint *n_pspecs)
{
GParamSpec **pspecs;
guint pos = 0;
g_assert (G_IS_OBJECT (serializable));
g_assert (n_pspecs != NULL);
pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (serializable), n_pspecs);
while (pos < *n_pspecs)
{
if (G_IS_PARAM_SPEC_OBJECT (pspecs[pos]))
{
(*n_pspecs)--;
if (pos < *n_pspecs)
pspecs[pos] = pspecs[*n_pspecs];
}
else
{
pos++;
}
}
return pspecs;
}
static void
serializable_iface_init (JsonSerializableIface *iface)
{
iface->list_properties = foundry_contextual_list_properties;
}
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (FoundryContextual, foundry_contextual, G_TYPE_OBJECT,
G_ADD_PRIVATE (FoundryContextual)
G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE, serializable_iface_init))
static GParamSpec *properties[N_PROPS];
static void
foundry_contextual_finalize (GObject *object)
{
FoundryContextual *self = (FoundryContextual *)object;
FoundryContextualPrivate *priv = foundry_contextual_get_instance_private (self);
g_weak_ref_clear (&priv->context_wr);
G_OBJECT_CLASS (foundry_contextual_parent_class)->finalize (object);
}
static void
foundry_contextual_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FoundryContextual *self = FOUNDRY_CONTEXTUAL (object);
switch (prop_id)
{
case PROP_CONTEXT:
g_value_take_object (value, foundry_contextual_dup_context (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_contextual_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FoundryContextual *self = FOUNDRY_CONTEXTUAL (object);
FoundryContextualPrivate *priv = foundry_contextual_get_instance_private (self);
switch (prop_id)
{
case PROP_CONTEXT:
g_weak_ref_set (&priv->context_wr, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_contextual_class_init (FoundryContextualClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = foundry_contextual_finalize;
object_class->get_property = foundry_contextual_get_property;
object_class->set_property = foundry_contextual_set_property;
properties[PROP_CONTEXT] =
g_param_spec_object ("context", NULL, NULL,
FOUNDRY_TYPE_CONTEXT,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
foundry_contextual_init (FoundryContextual *self)
{
FoundryContextualPrivate *priv = foundry_contextual_get_instance_private (self);
g_weak_ref_init (&priv->context_wr, NULL);
}
/**
* foundry_contextual_dup_context:
* @self: a #FoundryContextual
*
* Gets the #FoundryContext that @self is a part of while safely increasing
* the reference count of the resulting #FoundryContext by 1.
*
* Returns: (transfer full) (nullable): a #FoundryContext or %NULL
*/
FoundryContext *
foundry_contextual_dup_context (FoundryContextual *self)
{
FoundryContextualPrivate *priv = foundry_contextual_get_instance_private (self);
g_return_val_if_fail (FOUNDRY_IS_CONTEXTUAL (self), NULL);
return g_weak_ref_get (&priv->context_wr);
}
void
_foundry_contextual_invalidate_pipeline (FoundryContextual *self)
{
g_autoptr(FoundryContext) context = NULL;
g_return_if_fail (FOUNDRY_IS_CONTEXTUAL (self));
if ((context = foundry_contextual_dup_context (self)))
{
g_autoptr(FoundryBuildManager) build_manager = foundry_context_dup_build_manager (context);
if (build_manager != NULL)
foundry_build_manager_invalidate (build_manager);
}
}
void
foundry_contextual_log (FoundryContextual *self,
const char *domain,
GLogLevelFlags severity,
const char *format,
...)
{
g_autoptr(FoundryContext) context = NULL;
g_autofree char *message = NULL;
FoundryContextualClass *klass;
va_list args;
g_return_if_fail (FOUNDRY_IS_CONTEXTUAL (self));
klass = FOUNDRY_CONTEXTUAL_GET_CLASS (self);
if (klass->log_domain != NULL)
domain = klass->log_domain;
context = foundry_contextual_dup_context (self);
va_start (args, format);
foundry_context_logv (context, domain, severity, format, args);
va_end (args);
}
/**
* foundry_contextual_inhibit:
* @self: a [class@Foundry.Contextual]
*
* Creates a new [class@Foundry.Inhibitor] that will keep the
* [class@Foundry.Context] alive and prevent shutdown until
* [method@Foundry.Inhibitor.uninhibit] is called or the
* [class@Foundry.Inhibitor] is finalized, whichever comes first.
*
* If the context is already in shutdown, then %NULL is returned and
* @error is set.
*
* Returns: (transfer full): a [class@Foundry.Inhibitor] or %NULL and
* @error is set.
*/
FoundryInhibitor *
foundry_contextual_inhibit (FoundryContextual *self,
GError **error)
{
g_autoptr(FoundryContext) context = NULL;
g_return_val_if_fail (FOUNDRY_IS_CONTEXTUAL (self), NULL);
context = foundry_contextual_dup_context (self);
return foundry_inhibitor_new (context, error);
}
/**
* foundry_contextual_acquire:
* @self: a [class@Foundry.Contextual]
*
* This method provides a checked way to get a `context` for the contextual
*
* If the resulting context is %NULL, then the error is set to an
* appropriate error.
*
* Returns: (transfer full): a [class@Foundry.Context] or @error is set.
*
* Since: 1.1
*/
FoundryContext *
foundry_contextual_acquire (FoundryContextual *self,
GError **error)
{
FoundryContext *context;
g_return_val_if_fail (FOUNDRY_IS_CONTEXTUAL (self), NULL);
if (!(context = foundry_contextual_dup_context (self)))
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"Context is disposed");
return context;
}
|