Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-eds-utils"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <valent.h>
9 : :
10 : : #include "valent-eds-utils.h"
11 : :
12 : :
13 : : static ESourceRegistry *default_registry = NULL;
14 : :
15 : : /**
16 : : * valent_eds_get_registry:
17 : : * @cancellable: (nullable): a `GCancellable`
18 : : * @error: (nullable): a `GError`
19 : : *
20 : : * Get the global `ESourceRegistry` object.
21 : : *
22 : : * Returns: (transfer none): the `ESourceRegistry`
23 : : */
24 : : ESourceRegistry *
25 : 0 : valent_eds_get_registry (GCancellable *cancellable,
26 : : GError **error)
27 : : {
28 : 0 : if (default_registry == NULL)
29 : : {
30 : 0 : default_registry = e_source_registry_new_sync (cancellable, error);
31 : :
32 : 0 : if (default_registry != NULL)
33 : 0 : g_object_add_weak_pointer (G_OBJECT (default_registry), (gpointer) &default_registry);
34 : : else
35 : : return NULL;
36 : : }
37 : :
38 : 0 : return default_registry;
39 : : }
40 : :
41 : : /**
42 : : * valent_eds_register_source:
43 : : * @source: an `ESource`
44 : : * @cancellable: (nullable): a `GCancellable`
45 : : * @error: (nullable): a `GError`
46 : : *
47 : : * Register @scratch with the `ESourceRegistry`. If a source with the same UID as
48 : : * @scratch is already registered, that source will be returned. If the registry
49 : : * is unavailable or registration fails a new reference of @scratch will be
50 : : * returned.
51 : : *
52 : : * Returns: (transfer full): an `ESource`
53 : : */
54 : : ESource *
55 : 0 : valent_eds_register_source (ESource *scratch,
56 : : GCancellable *cancellable,
57 : : GError **error)
58 : : {
59 : 0 : ESourceRegistry *registry;
60 : 0 : g_autoptr (ESource) source = NULL;
61 : :
62 : 0 : g_return_val_if_fail (E_IS_SOURCE (scratch), NULL);
63 : :
64 : : /* Get the source to the registry */
65 : 0 : registry = valent_eds_get_registry (cancellable, error);
66 : :
67 : 0 : if (registry == NULL)
68 : 0 : return g_object_ref (scratch);
69 : :
70 : : /* Check the registry for an existing source */
71 : 0 : source = e_source_registry_ref_source (registry, e_source_get_uid (scratch));
72 : :
73 : 0 : if (source)
74 : : return g_steal_pointer (&source);
75 : :
76 : : /* Commit the scratch source to the registry */
77 : 0 : if (!e_source_registry_commit_source_sync (registry, scratch, cancellable, error))
78 : : return NULL;
79 : :
80 : : /* if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) */
81 : : /* return e_source_registry_ref_source (registry, uid); */
82 : : /* else */
83 : : /* g_warning ("Failed to register ESource: %s", error->message); */
84 : :
85 : 0 : return g_object_ref (scratch);
86 : : }
87 : :
|