Debian Codegraph @

src/dex-future-list-model.c

raw symbols SCIP
1 /* dex-future-list-model.c
2 *
3 * Copyright 2025 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include "dex-future-list-model.h"
24
25 /**
26 * DexFutureListModel:
27 *
28 * This class provides a [[email protected]] implementation that will
29 * expand to the contents of another [[email protected]] from a
30 * [[email protected]].
31 *
32 * Since: 1.1
33 */
34
35 struct _DexFutureListModel
36 {
37 GObject parent_instance;
38 GListModel *model;
39 DexFuture *future;
40 };
41
42 static GType
43 dex_future_list_model_get_item_type (GListModel *model)
44 {
45 DexFutureListModel *self = DEX_FUTURE_LIST_MODEL (model);
46
47 if (self->model != NULL)
48 return g_list_model_get_item_type (self->model);
49
50 return G_TYPE_OBJECT;
51 }
52
53 static guint
54 dex_future_list_model_get_n_items (GListModel *model)
55 {
56 DexFutureListModel *self = DEX_FUTURE_LIST_MODEL (model);
57
58 if (self->model != NULL)
59 return g_list_model_get_n_items (self->model);
60
61 return 0;
62 }
63
64 static gpointer
65 dex_future_list_model_get_item (GListModel *model,
66 guint position)
67 {
68 DexFutureListModel *self = DEX_FUTURE_LIST_MODEL (model);
69
70 if (self->model != NULL)
71 return g_list_model_get_item (self->model, position);
72
73 return NULL;
74 }
75
76 static void
77 list_model_iface_init (GListModelInterface *iface)
78 {
79 iface->get_item_type = dex_future_list_model_get_item_type;
80 iface->get_n_items = dex_future_list_model_get_n_items;
81 iface->get_item = dex_future_list_model_get_item;
82 }
83
84 G_DEFINE_FINAL_TYPE_WITH_CODE (DexFutureListModel, dex_future_list_model, G_TYPE_OBJECT,
85 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
86
87 static void
88 dex_future_list_model_finalize (GObject *object)
89 {
90 DexFutureListModel *self = (DexFutureListModel *)object;
91
92 dex_clear (&self->future);
93 g_clear_object (&self->model);
94
95 G_OBJECT_CLASS (dex_future_list_model_parent_class)->finalize (object);
96 }
97
98 static void
99 dex_future_list_model_class_init (DexFutureListModelClass *klass)
100 {
101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102
103 object_class->finalize = dex_future_list_model_finalize;
104 }
105
106 static void
107 dex_future_list_model_init (DexFutureListModel *self)
108 {
109 }
110
111 static DexFuture *
112 apply_model (DexFuture *future,
113 gpointer user_data)
114 {
115 GWeakRef *weak_ref = user_data;
116 DexFutureListModel *self = g_weak_ref_get (weak_ref);
117 const GValue *value;
118 GListModel *model;
119
120 if (self != NULL)
121 {
122 if ((value = dex_future_get_value (future, NULL)) &&
123 G_VALUE_HOLDS (value, G_TYPE_LIST_MODEL) &&
124 (model = g_value_get_object (value)))
125 {
126 guint n_items = g_list_model_get_n_items (model);
127
128 g_set_object (&self->model, model);
129
130 g_signal_connect_object (model,
131 "items-changed",
132 G_CALLBACK (g_list_model_items_changed),
133 self,
134 G_CONNECT_SWAPPED);
135
136 if (n_items > 0)
137 g_list_model_items_changed (G_LIST_MODEL (self), 0, 0, n_items);
138 }
139
140 g_object_unref (self);
141 }
142
143 return dex_future_new_true ();
144 }
145
146 static GWeakRef *
147 _g_weak_ref_new (gpointer instance)
148 {
149 GWeakRef *wr = g_new0 (GWeakRef, 1);
150 g_weak_ref_init (wr, instance);
151 return wr;
152 }
153
154 static void
155 _g_weak_ref_free (GWeakRef *wr)
156 {
157 g_weak_ref_clear (wr);
158 g_free (wr);
159 }
160
161 /**
162 * dex_future_list_model_new:
163 * @future: (transfer full): a [[email protected]] that resolves to
164 * a [[email protected]].
165 *
166 * Creates a new list model that will initially be empty and after
167 * @future resolves contain the items within it.
168 *
169 * Returns: (transfer full): a new [[email protected]]
170 *
171 * Since: 1.1
172 */
173 GListModel *
174 dex_future_list_model_new (DexFuture *future)
175 {
176 DexFutureListModel *self;
177
178 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
179
180 self = g_object_new (DEX_TYPE_FUTURE_LIST_MODEL, NULL);
181 self->future = g_steal_pointer (&future);
182
183 dex_future_disown (dex_future_then (dex_ref (self->future),
184 apply_model,
185 _g_weak_ref_new (self),
186 (GDestroyNotify) _g_weak_ref_free));
187
188 return G_LIST_MODEL (self);
189 }
190
191 /**
192 * dex_future_list_model_dup_future:
193 * @self: a [[email protected]]
194 *
195 * Gets the future provided when creating the list model.
196 *
197 * Returns: (transfer full): a [[email protected]] that resolves to a
198 * [[email protected]] or rejects with error.
199 *
200 * Since: 1.1
201 */
202 DexFuture *
203 dex_future_list_model_dup_future (DexFutureListModel *self)
204 {
205 dex_return_error_if_fail (DEX_IS_FUTURE_LIST_MODEL (self));
206
207 return dex_ref (self->future);
208 }