| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "dex-future-list-model.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
} |