File: foundry-documentation-manager.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (684 lines) | stat: -rw-r--r-- 23,361 bytes parent folder | download | duplicates (2)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/* foundry-documentation-manager.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 <glib/gstdio.h>

#include <libpeas.h>

#include "foundry-config.h"
#include "foundry-documentation.h"
#include "foundry-documentation-manager.h"
#include "foundry-documentation-matches-private.h"
#include "foundry-documentation-provider-private.h"
#include "foundry-documentation-provider.h"
#include "foundry-documentation-query.h"
#include "foundry-model-manager.h"
#include "foundry-inhibitor.h"
#include "foundry-service-private.h"
#include "foundry-util-private.h"

/**
 * FoundryDocumentationManager:
 *
 * Service that manages documentation plugins.
 */

struct _FoundryDocumentationManager
{
  FoundryService    parent_instance;
  PeasExtensionSet *addins;
  GListModel       *roots;
  DexFuture        *indexer;
  guint             indexing;
};

struct _FoundryDocumentationManagerClass
{
  FoundryServiceClass parent_class;
};

G_DEFINE_FINAL_TYPE (FoundryDocumentationManager, foundry_documentation_manager, FOUNDRY_TYPE_SERVICE)

enum {
  PROP_0,
  PROP_INDEXING,
  N_PROPS
};

enum {
  CHANGED,
  N_SIGNALS
};

static GParamSpec *properties[N_PROPS];
static guint signals[N_SIGNALS];

static void
foundry_documentation_manager_provider_added (PeasExtensionSet *set,
                                              PeasPluginInfo   *plugin_info,
                                              GObject          *addin,
                                              gpointer          user_data)
{
  FoundryDocumentationManager *self = user_data;

  g_assert (PEAS_IS_EXTENSION_SET (set));
  g_assert (PEAS_IS_PLUGIN_INFO (plugin_info));
  g_assert (FOUNDRY_IS_DOCUMENTATION_PROVIDER (addin));
  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));

  g_debug ("Adding FoundryDocumentationProvider of type %s", G_OBJECT_TYPE_NAME (addin));

  dex_future_disown (foundry_documentation_provider_load (FOUNDRY_DOCUMENTATION_PROVIDER (addin)));
}

static void
foundry_documentation_manager_provider_removed (PeasExtensionSet *set,
                                                PeasPluginInfo   *plugin_info,
                                                GObject          *addin,
                                                gpointer          user_data)
{
  FoundryDocumentationManager *self = user_data;

  g_assert (PEAS_IS_EXTENSION_SET (set));
  g_assert (PEAS_IS_PLUGIN_INFO (plugin_info));
  g_assert (FOUNDRY_IS_DOCUMENTATION_PROVIDER (addin));
  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));

  g_debug ("Removing FoundryDocumentationProvider of type %s", G_OBJECT_TYPE_NAME (addin));

  dex_future_disown (foundry_documentation_provider_unload (FOUNDRY_DOCUMENTATION_PROVIDER (addin)));
}

static DexFuture *
foundry_documentation_manager_index_fiber (gpointer data)
{
  FoundryDocumentationManager *self = data;
  g_autoptr(GPtrArray) futures = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(DexFuture) future = NULL;
  guint n_items;

  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  dex_return_error_if_fail (PEAS_IS_EXTENSION_SET (self->addins));
  dex_return_error_if_fail (G_IS_LIST_MODEL (self->roots));

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));

  if (n_items == 0)
    return dex_future_new_true ();

  if (++self->indexing == 1)
    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INDEXING]);

  futures = g_ptr_array_new_with_free_func (dex_unref);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);
      g_ptr_array_add (futures, foundry_documentation_provider_index (provider, self->roots));
    }

  future = foundry_future_all (futures);
  dex_await (dex_ref (future), NULL);

  if (--self->indexing == 0)
    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INDEXING]);

  return g_steal_pointer (&future);
}

/**
 * foundry_documentation_manager_index:
 * @self: a [class@Foundry.DocumentationManager]
 *
 * Ensures the documentation manager has indexed.
 *
 * This happens on demand but can be forced to start earlier by
 * applications which may want to delay operations until this
 * part has completed. Such an example is to now show a window
 * until the initial indexing has completed.
 *
 * Returns: (transfer full):
 */
DexFuture *
foundry_documentation_manager_index (FoundryDocumentationManager *self)
{
  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));

  if (self->indexer == NULL)
    {
      self->indexer = dex_scheduler_spawn (NULL, 0,
                                           foundry_documentation_manager_index_fiber,
                                           g_object_ref (self),
                                           g_object_unref);
      dex_future_disown (dex_ref (self->indexer));
    }

  return dex_ref (self->indexer);
}

static void
foundry_documentation_manager_roots_changed_cb (FoundryDocumentationManager *self,
                                                guint                        position,
                                                guint                        removed,
                                                guint                        added,
                                                GListModel                  *model)
{
  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  g_assert (G_IS_LIST_MODEL (model));

  dex_clear (&self->indexer);

  g_signal_emit (self, signals[CHANGED], 0);
}

static DexFuture *
foundry_documentation_manager_start_fiber (gpointer user_data)
{
  FoundryDocumentationManager *self = user_data;
  g_autoptr(FoundryDocumentation) documentation = NULL;
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GListModel) flatten_roots = NULL;
  g_autoptr(GListStore) all_roots = NULL;
  g_autoptr(GPtrArray) futures = NULL;
  g_autofree char *documentation_id = NULL;
  guint n_items;

  g_assert (FOUNDRY_IS_MAIN_THREAD ());
  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  g_assert (PEAS_IS_EXTENSION_SET (self->addins));

  context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (self));

  g_signal_connect_object (self->addins,
                           "extension-added",
                           G_CALLBACK (foundry_documentation_manager_provider_added),
                           self,
                           0);
  g_signal_connect_object (self->addins,
                           "extension-removed",
                           G_CALLBACK (foundry_documentation_manager_provider_removed),
                           self,
                           0);

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));
  futures = g_ptr_array_new_with_free_func (dex_unref);

  /* First request that all of the providers pass the load phase */
  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);

      g_ptr_array_add (futures, foundry_documentation_provider_load (provider));
    }

  if (futures->len > 0)
    dex_await (foundry_future_all (futures), NULL);

  /* Now collect all of the roots from various providers */
  all_roots = g_list_store_new (G_TYPE_LIST_MODEL);
  flatten_roots = foundry_flatten_list_model_new (g_object_ref (G_LIST_MODEL (all_roots)));
  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);
      g_autoptr(GListModel) roots = foundry_documentation_provider_list_roots (provider);

      g_list_store_append (all_roots, roots);
    }

  g_signal_connect_object (flatten_roots,
                           "items-changed",
                           G_CALLBACK (foundry_documentation_manager_roots_changed_cb),
                           self,
                           G_CONNECT_SWAPPED);

  g_set_object (&self->roots, flatten_roots);

  return dex_future_new_true ();
}

static DexFuture *
foundry_documentation_manager_start (FoundryService *service)
{
  FoundryDocumentationManager *self = (FoundryDocumentationManager *)service;

  g_assert (FOUNDRY_IS_MAIN_THREAD ());
  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  g_assert (PEAS_IS_EXTENSION_SET (self->addins));

  return dex_scheduler_spawn (NULL, 0,
                              foundry_documentation_manager_start_fiber,
                              g_object_ref (self),
                              g_object_unref);
}

static DexFuture *
foundry_documentation_manager_stop (FoundryService *service)
{
  FoundryDocumentationManager *self = (FoundryDocumentationManager *)service;
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GPtrArray) futures = NULL;
  guint n_items;

  g_assert (FOUNDRY_IS_MAIN_THREAD ());
  g_assert (FOUNDRY_IS_SERVICE (service));

  context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (self));

  g_clear_object (&self->roots);

  g_signal_handlers_disconnect_by_func (self->addins,
                                        G_CALLBACK (foundry_documentation_manager_provider_added),
                                        self);
  g_signal_handlers_disconnect_by_func (self->addins,
                                        G_CALLBACK (foundry_documentation_manager_provider_removed),
                                        self);

  dex_clear (&self->indexer);

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->addins));
  futures = g_ptr_array_new_with_free_func (dex_unref);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (G_LIST_MODEL (self->addins), i);

      g_ptr_array_add (futures, foundry_documentation_provider_unload (provider));
    }

  g_clear_object (&self->addins);

  if (futures->len > 0)
    return foundry_future_all (futures);

  return dex_future_new_true ();
}

static void
foundry_documentation_manager_constructed (GObject *object)
{
  FoundryDocumentationManager *self = (FoundryDocumentationManager *)object;
  g_autoptr(FoundryContext) context = NULL;

  G_OBJECT_CLASS (foundry_documentation_manager_parent_class)->constructed (object);

  context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (self));

  self->addins = peas_extension_set_new (NULL,
                                         FOUNDRY_TYPE_DOCUMENTATION_PROVIDER,
                                         "context", context,
                                         NULL);
}

static void
foundry_documentation_manager_finalize (GObject *object)
{
  FoundryDocumentationManager *self = (FoundryDocumentationManager *)object;

  g_clear_object (&self->addins);
  g_clear_object (&self->roots);

  G_OBJECT_CLASS (foundry_documentation_manager_parent_class)->finalize (object);
}

static void
foundry_documentation_manager_get_property (GObject    *object,
                                            guint       prop_id,
                                            GValue     *value,
                                            GParamSpec *pspec)
{
  FoundryDocumentationManager *self = FOUNDRY_DOCUMENTATION_MANAGER (object);

  switch (prop_id)
    {
    case PROP_INDEXING:
      g_value_set_boolean (value, foundry_documentation_manager_is_indexing (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
foundry_documentation_manager_class_init (FoundryDocumentationManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FoundryServiceClass *service_class = FOUNDRY_SERVICE_CLASS (klass);

  object_class->constructed = foundry_documentation_manager_constructed;
  object_class->finalize = foundry_documentation_manager_finalize;
  object_class->get_property = foundry_documentation_manager_get_property;

  service_class->start = foundry_documentation_manager_start;
  service_class->stop = foundry_documentation_manager_stop;

  /**
   * FoundryDocumentationManager:indexing: (getter is_indexing)
   *
   * If the documentation is currently indexing documentation.
   */
  properties[PROP_INDEXING] =
    g_param_spec_boolean ("indexing", NULL, NULL,
                          FALSE,
                          (G_PARAM_READABLE |
                           G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, N_PROPS, properties);

  /**
   * FoundryDocumentationManager:changed:
   *
   * The "changed" signal is emitted when the manager discovers that
   * there is documentation which has changed and needs to be re-indexed.
   *
   * Applications that care about this can respond by calling
   * [method@Foundry.DocumentationManager.index] to re-index the
   * documentation at their point of convenience.
   */
  signals[CHANGED] =
    g_signal_new ("changed",
                 G_TYPE_FROM_CLASS (klass),
                 G_SIGNAL_RUN_LAST,
                 0,
                 NULL, NULL,
                 NULL,
                 G_TYPE_NONE, 0);
}

static void
foundry_documentation_manager_init (FoundryDocumentationManager *self)
{
}

static DexFuture *
foundry_documentation_manager_query_fiber (FoundryDocumentationManager *self,
                                           FoundryDocumentationQuery   *query)
{
  g_autoptr(FoundryDocumentationMatches) matches = NULL;
  g_autoptr(GError) error = NULL;
  DexFuture *everything = NULL;

  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  g_assert (FOUNDRY_IS_DOCUMENTATION_QUERY (query));

  /* Wait for startup to complete */
  if (!dex_await (foundry_service_when_ready (FOUNDRY_SERVICE (self)), &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  /* Wait for indexing to complete */
  if (!dex_await (foundry_documentation_manager_index (self), &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  matches = foundry_documentation_matches_new (query);

  /* Query providers and await the creation of the immediate
   * GListModel. If it has a future associated with it, then
   * swap out our future for the future that will complete when
   * the model has completed so we can provide the same feature
   * to our caller (a future list model with immediate results plus
   * ability to await on full result set).
   */
  if (self->addins != NULL)
    {
      g_autoptr(GListModel) addins = g_object_ref (G_LIST_MODEL (self->addins));
      g_autoptr(GPtrArray) futures = g_ptr_array_new_with_free_func (dex_unref);
      guint n_items = g_list_model_get_n_items (addins);

      for (guint i = 0; i < n_items; i++)
        {
          g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (addins, i);

          g_ptr_array_add (futures,
                           foundry_documentation_provider_query (provider, query, matches));
        }

      if (futures->len > 0)
        everything = foundry_future_all (futures);
    }

  foundry_documentation_matches_set_future (matches, everything);

  return dex_future_new_take_object (g_steal_pointer (&matches));
}

/**
 * foundry_documentation_manager_query:
 * @self: a [class@Foundry.DocumentationManager]
 * @query: a [class@Foundry.DocumentationQuery]
 *
 * Consumers can call [method@Foundry.DocumentationMatches.await] to get a
 * future that will complete when all matches have been populated.
 *
 * Returns: (transfer full) (not nullable): a [class@Dex.Future] that resolves
 *    to [Foundry.DocumentationMatches].
 */
DexFuture *
foundry_documentation_manager_query (FoundryDocumentationManager *self,
                                     FoundryDocumentationQuery   *query)
{
  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));

  return foundry_scheduler_spawn (NULL, 0,
                                  G_CALLBACK (foundry_documentation_manager_query_fiber),
                                  2,
                                  FOUNDRY_TYPE_DOCUMENTATION_MANAGER, self,
                                  FOUNDRY_TYPE_DOCUMENTATION_QUERY, query);
}

/**
 * foundry_documentation_manager_is_indexing:
 * @self: a [class@Foundry.DocumentationManager]
 *
 * If the documentation manager is currently indexing.
 */
gboolean
foundry_documentation_manager_is_indexing (FoundryDocumentationManager *self)
{
  g_return_val_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self), FALSE);

  return self->indexing > 0;
}

/**
 * foundry_documentation_manager_find_by_uri:
 * @self: a [class@Foundry.DocumentationManager]
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to a
 *   [class@Foundry.Documentation] or rejects with error.
 */
DexFuture *
foundry_documentation_manager_find_by_uri (FoundryDocumentationManager *self,
                                           const char                  *uri)
{
  g_autoptr(GPtrArray) futures = NULL;
  GListModel *model;
  guint n_items;

  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  dex_return_error_if_fail (uri != NULL);

  model = G_LIST_MODEL (self->addins);

  if (!(n_items = g_list_model_get_n_items (model)))
    return dex_future_new_reject (G_IO_ERROR,
                                  G_IO_ERROR_NOT_FOUND,
                                  "Not found");

  futures = g_ptr_array_new_with_free_func (dex_unref);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (model, i);

      g_ptr_array_add (futures, foundry_documentation_provider_find_by_uri (provider, uri));
    }

  return dex_future_anyv ((DexFuture **)futures->pdata, futures->len);
}

static DexFuture *
foundry_documentation_manager_list_children_cb (DexFuture *completed,
                                                gpointer   user_data)
{
  g_autoptr(GListStore) store = NULL;
  guint size;

  g_assert (DEX_IS_FUTURE_SET (completed));

  size = dex_future_set_get_size (DEX_FUTURE_SET (completed));
  store = g_list_store_new (G_TYPE_LIST_MODEL);

  for (guint i = 0; i < size; i++)
    {
      const GValue *value;

      if ((value = dex_future_set_get_value_at (DEX_FUTURE_SET (completed), i, NULL)) &&
          G_VALUE_HOLDS (value, G_TYPE_LIST_MODEL))
        g_list_store_append (store, g_value_get_object (value));
    }

  return dex_future_new_take_object (foundry_flatten_list_model_new (g_object_ref (G_LIST_MODEL (store))));
}

static DexFuture *
foundry_documentation_manager_list_children_fiber (FoundryDocumentationManager *self,
                                                   FoundryDocumentation        *parent)
{
  g_autoptr(GPtrArray) futures = NULL;
  g_autoptr(GError) error = NULL;
  GListModel *model;
  guint n_items;

  g_assert (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  g_assert (!parent || FOUNDRY_IS_DOCUMENTATION (parent));

  if (!dex_await (foundry_service_when_ready (FOUNDRY_SERVICE (self)), &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  dex_await (foundry_documentation_manager_index (self), NULL);

  model = G_LIST_MODEL (self->addins);

  if (!(n_items = g_list_model_get_n_items (model)))
    return dex_future_new_reject (G_IO_ERROR,
                                  G_IO_ERROR_NOT_FOUND,
                                  "Not found");


  futures = g_ptr_array_new_with_free_func (dex_unref);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (model, i);

      g_ptr_array_add (futures, foundry_documentation_provider_list_children (provider, parent));
    }

  return dex_future_finally (dex_future_anyv ((DexFuture **)futures->pdata, futures->len),
                             foundry_documentation_manager_list_children_cb,
                             NULL, NULL);
}

/**
 * foundry_documentation_manager_list_children:
 * @self: a [class@Foundry.DocumentationManager]
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to
 *   a [iface@Gio.ListModel] or rejects with error.
 */
DexFuture *
foundry_documentation_manager_list_children (FoundryDocumentationManager *self,
                                             FoundryDocumentation        *parent)
{
  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));
  dex_return_error_if_fail (!parent || FOUNDRY_IS_DOCUMENTATION (parent));

  return foundry_scheduler_spawn (NULL, 0,
                                  G_CALLBACK (foundry_documentation_manager_list_children_fiber),
                                  2,
                                  FOUNDRY_TYPE_DOCUMENTATION_MANAGER, self,
                                  FOUNDRY_TYPE_DOCUMENTATION, parent);
}

static DexFuture *
foundry_documentation_manager_list_bundles_cb (DexFuture *completed,
                                               gpointer   user_data)
{
  g_autoptr(GListStore) store = NULL;
  guint size;

  g_assert (DEX_IS_FUTURE_SET (completed));

  size = dex_future_set_get_size (DEX_FUTURE_SET (completed));
  store = g_list_store_new (G_TYPE_LIST_MODEL);

  for (guint i = 0; i < size; i++)
    {
      const GValue *value;

      if ((value = dex_future_set_get_value_at (DEX_FUTURE_SET (completed), i, NULL)) &&
          G_VALUE_HOLDS (value, G_TYPE_LIST_MODEL))
        g_list_store_append (store, g_value_get_object (value));

    }

  return dex_future_new_take_object (foundry_flatten_list_model_new (g_object_ref (G_LIST_MODEL (store))));
}

/**
 * foundry_documentation_manager_list_bundles:
 * @self: a [class@Foundry.DocumentationManager]
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to
 *   a [iface@Gio.ListModel] of [class@Foundry.DocumentationBundle]
 *   or rejects with error.
 */
DexFuture *
foundry_documentation_manager_list_bundles (FoundryDocumentationManager *self)
{
  g_autoptr(GPtrArray) futures = NULL;
  GListModel *model;
  guint n_items;

  dex_return_error_if_fail (FOUNDRY_IS_DOCUMENTATION_MANAGER (self));

  model = G_LIST_MODEL (self->addins);

  if (!(n_items = g_list_model_get_n_items (model)))
    return dex_future_new_reject (G_IO_ERROR,
                                  G_IO_ERROR_NOT_FOUND,
                                  "Not found");

  futures = g_ptr_array_new_with_free_func (dex_unref);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(FoundryDocumentationProvider) provider = g_list_model_get_item (model, i);

      g_ptr_array_add (futures, foundry_documentation_provider_list_bundles (provider));
    }

  return dex_future_finally (dex_future_anyv ((DexFuture **)futures->pdata, futures->len),
                             foundry_documentation_manager_list_bundles_cb,
                             NULL, NULL);
}