4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
20 * SECTION: e-source-registry
21 * @include: libedataserver/libedataserver.h
22 * @short_description: A central repository for data sources
24 * The #ESourceRegistry is a global singleton store for all #ESource
25 * instances. It uses file monitors to react to key file creation and
26 * deletion events, either constructing an #ESource instance from the
27 * newly created key file, or removing from the logical #ESource
28 * hierarchy the instance corresponding to the deleted key file.
30 * The #ESourceRegistry can be queried for individual #ESource instances
31 * by their unique identifier string or key file path, for collections of
32 * #ESource instances having a particular extension, or for all available
35 * The #ESourceRegistry API also provides a front-end for the
36 * "org.gnome.Evolution.DefaultSources" #GSettings schema which tracks
37 * which #ESource instances are designated to be the user's default address
38 * book, calendar, memo list and task list for desktop integration.
41 #include "e-source-registry.h"
44 #include <glib/gstdio.h>
45 #include <glib/gi18n-lib.h>
47 /* XXX Yeah, yeah... */
48 #define GCR_API_SUBJECT_TO_CHANGE
50 #include <gcr/gcr-base.h>
52 /* Private D-Bus classes. */
53 #include <e-dbus-source.h>
54 #include <e-dbus-source-manager.h>
56 #include <libedataserver/e-marshal.h>
57 #include <libedataserver/e-data-server-util.h>
58 #include <libedataserver/e-source-collection.h>
60 /* Needed for the defaults API. */
61 #include <libedataserver/e-source-address-book.h>
62 #include <libedataserver/e-source-calendar.h>
63 #include <libedataserver/e-source-mail-account.h>
64 #include <libedataserver/e-source-mail-identity.h>
66 #include "e-dbus-authenticator.h"
68 #define E_SOURCE_REGISTRY_GET_PRIVATE(obj) \
69 (G_TYPE_INSTANCE_GET_PRIVATE \
70 ((obj), E_TYPE_SOURCE_REGISTRY, ESourceRegistryPrivate))
72 #define DBUS_OBJECT_PATH "/org/gnome/evolution/dataserver/SourceManager"
73 #define GSETTINGS_SCHEMA "org.gnome.Evolution.DefaultSources"
75 /* Built-in data source UIDs. */
76 #define E_SOURCE_BUILTIN_ADDRESS_BOOK_UID "system-address-book"
77 #define E_SOURCE_BUILTIN_CALENDAR_UID "system-calendar"
78 #define E_SOURCE_BUILTIN_MAIL_ACCOUNT_UID "local"
79 #define E_SOURCE_BUILTIN_MEMO_LIST_UID "system-memo-list"
80 #define E_SOURCE_BUILTIN_TASK_LIST_UID "system-task-list"
82 /* GSettings keys for default data sources. */
83 #define E_SETTINGS_DEFAULT_ADDRESS_BOOK_KEY "default-address-book"
84 #define E_SETTINGS_DEFAULT_CALENDAR_KEY "default-calendar"
85 #define E_SETTINGS_DEFAULT_MAIL_ACCOUNT_KEY "default-mail-account"
86 #define E_SETTINGS_DEFAULT_MAIL_IDENTITY_KEY "default-mail-identity"
87 #define E_SETTINGS_DEFAULT_MEMO_LIST_KEY "default-memo-list"
88 #define E_SETTINGS_DEFAULT_TASK_LIST_KEY "default-task-list"
90 typedef struct _AsyncContext AsyncContext;
91 typedef struct _AuthContext AuthContext;
92 typedef struct _SourceClosure SourceClosure;
93 typedef struct _ThreadClosure ThreadClosure;
95 struct _ESourceRegistryPrivate {
96 GMainContext *main_context;
98 GThread *manager_thread;
99 ThreadClosure *thread_closure;
101 GDBusObjectManager *dbus_object_manager;
102 EDBusSourceManager *dbus_source_manager;
104 GHashTable *object_path_table;
105 GMutex *object_path_table_lock;
108 GMutex *sources_lock;
113 struct _AsyncContext {
115 GList *list_of_sources;
116 ESourceAuthenticator *auth;
119 /* Used in e_source_registry_authenticate_sync() */
120 struct _AuthContext {
121 ESourceAuthenticator *auth;
122 EDBusAuthenticator *dbus_auth;
123 GCancellable *cancellable;
124 GMainLoop *main_loop;
125 ESourceAuthenticationResult auth_result;
126 GcrSecretExchange *secret_exchange;
127 gboolean authenticating;
132 struct _SourceClosure {
133 ESourceRegistry *registry;
137 struct _ThreadClosure {
138 ESourceRegistry *registry;
139 GMainContext *main_context;
140 GMainLoop *main_loop;
141 GCond *main_loop_cond;
142 GMutex *main_loop_mutex;
147 PROP_DEFAULT_ADDRESS_BOOK,
148 PROP_DEFAULT_CALENDAR,
149 PROP_DEFAULT_MAIL_ACCOUNT,
150 PROP_DEFAULT_MAIL_IDENTITY,
151 PROP_DEFAULT_MEMO_LIST,
152 PROP_DEFAULT_TASK_LIST
164 /* Forward Declarations */
165 static void source_registry_add_source (ESourceRegistry *registry,
167 static void e_source_registry_initable_init (GInitableIface *interface);
169 static guint signals[LAST_SIGNAL];
171 /* By default, the GAsyncInitable interface calls GInitable.init()
172 * from a separate thread, so we only have to override GInitable. */
173 G_DEFINE_TYPE_WITH_CODE (
177 G_IMPLEMENT_INTERFACE (
178 G_TYPE_INITABLE, e_source_registry_initable_init)
179 G_IMPLEMENT_INTERFACE (
180 G_TYPE_ASYNC_INITABLE, NULL))
183 async_context_free (AsyncContext *async_context)
185 if (async_context->source != NULL)
186 g_object_unref (async_context->source);
189 async_context->list_of_sources,
190 (GDestroyNotify) g_object_unref);
192 if (async_context->auth != NULL)
193 g_object_unref (async_context->auth);
195 g_slice_free (AsyncContext, async_context);
199 auth_context_free (AuthContext *auth_context)
201 if (auth_context->auth != NULL)
202 g_object_unref (auth_context->auth);
204 if (auth_context->dbus_auth != NULL)
205 g_object_unref (auth_context->dbus_auth);
207 if (auth_context->cancellable != NULL)
208 g_object_unref (auth_context->cancellable);
210 if (auth_context->main_loop != NULL)
211 g_main_loop_unref (auth_context->main_loop);
213 if (auth_context->secret_exchange != NULL)
214 g_object_unref (auth_context->secret_exchange);
216 g_slice_free (AuthContext, auth_context);
220 source_closure_free (SourceClosure *closure)
222 g_object_unref (closure->registry);
223 g_object_unref (closure->source);
225 g_slice_free (SourceClosure, closure);
229 thread_closure_free (ThreadClosure *closure)
231 /* The registry member is not referenced. */
233 g_main_context_unref (closure->main_context);
234 g_main_loop_unref (closure->main_loop);
235 g_cond_free (closure->main_loop_cond);
236 g_mutex_free (closure->main_loop_mutex);
238 g_slice_free (ThreadClosure, closure);
242 source_registry_object_path_table_insert (ESourceRegistry *registry,
243 const gchar *object_path,
246 g_return_if_fail (object_path != NULL);
247 g_return_if_fail (E_IS_SOURCE (source));
249 g_mutex_lock (registry->priv->object_path_table_lock);
251 g_hash_table_insert (
252 registry->priv->object_path_table,
253 g_strdup (object_path),
254 g_object_ref (source));
256 g_mutex_unlock (registry->priv->object_path_table_lock);
260 source_registry_object_path_table_lookup (ESourceRegistry *registry,
261 const gchar *object_path)
265 g_return_val_if_fail (object_path != NULL, NULL);
267 g_mutex_lock (registry->priv->object_path_table_lock);
269 source = g_hash_table_lookup (
270 registry->priv->object_path_table, object_path);
272 g_object_ref (source);
274 g_mutex_unlock (registry->priv->object_path_table_lock);
280 source_registry_object_path_table_remove (ESourceRegistry *registry,
281 const gchar *object_path)
285 g_return_val_if_fail (object_path != NULL, FALSE);
287 g_mutex_lock (registry->priv->object_path_table_lock);
289 removed = g_hash_table_remove (
290 registry->priv->object_path_table, object_path);
292 g_mutex_unlock (registry->priv->object_path_table_lock);
298 source_registry_sources_insert (ESourceRegistry *registry,
303 uid = e_source_get_uid (source);
304 g_return_if_fail (uid != NULL);
306 g_mutex_lock (registry->priv->sources_lock);
308 g_hash_table_insert (
309 registry->priv->sources,
310 g_strdup (uid), g_object_ref (source));
312 g_mutex_unlock (registry->priv->sources_lock);
316 source_registry_sources_remove (ESourceRegistry *registry,
322 uid = e_source_get_uid (source);
323 g_return_val_if_fail (uid != NULL, FALSE);
325 g_mutex_lock (registry->priv->sources_lock);
327 removed = g_hash_table_remove (registry->priv->sources, uid);
329 g_mutex_unlock (registry->priv->sources_lock);
335 source_registry_sources_lookup (ESourceRegistry *registry,
340 g_return_val_if_fail (uid != NULL, NULL);
342 g_mutex_lock (registry->priv->sources_lock);
344 source = g_hash_table_lookup (registry->priv->sources, uid);
347 g_object_ref (source);
349 g_mutex_unlock (registry->priv->sources_lock);
355 source_registry_sources_get_values (ESourceRegistry *registry)
359 g_mutex_lock (registry->priv->sources_lock);
361 values = g_hash_table_get_values (registry->priv->sources);
363 g_list_foreach (values, (GFunc) g_object_ref, NULL);
365 g_mutex_unlock (registry->priv->sources_lock);
371 source_registry_sources_build_tree (ESourceRegistry *registry)
378 g_mutex_lock (registry->priv->sources_lock);
380 root = g_node_new (NULL);
381 index = g_hash_table_new (g_str_hash, g_str_equal);
383 /* Add a GNode for each ESource to the index. */
384 g_hash_table_iter_init (&iter, registry->priv->sources);
385 while (g_hash_table_iter_next (&iter, &key, &value)) {
386 ESource *source = g_object_ref (value);
387 g_hash_table_insert (index, key, g_node_new (source));
390 /* Traverse the index and link the nodes together. */
391 g_hash_table_iter_init (&iter, index);
392 while (g_hash_table_iter_next (&iter, NULL, &value)) {
396 const gchar *parent_uid;
398 source_node = (GNode *) value;
399 source = E_SOURCE (source_node->data);
400 parent_uid = e_source_get_parent (source);
402 if (parent_uid == NULL || *parent_uid == '\0') {
405 parent_node = g_hash_table_lookup (index, parent_uid);
406 g_warn_if_fail (parent_node != NULL);
409 /* Should never be NULL, but just to be safe. */
410 if (parent_node != NULL)
411 g_node_append (parent_node, source_node);
414 g_hash_table_destroy (index);
416 g_mutex_unlock (registry->priv->sources_lock);
422 source_registry_settings_changed_cb (GSettings *settings,
424 ESourceRegistry *registry)
426 /* We define a property name that matches every key in
427 * the "org.gnome.Evolution.DefaultSources" schema. */
428 g_object_notify (G_OBJECT (registry), key);
432 source_registry_source_changed_idle_cb (gpointer user_data)
434 SourceClosure *closure = user_data;
438 signals[SOURCE_CHANGED], 0,
445 source_registry_source_notify_enabled_idle_cb (gpointer user_data)
447 SourceClosure *closure = user_data;
449 if (e_source_get_enabled (closure->source))
452 signals[SOURCE_ENABLED], 0,
457 signals[SOURCE_DISABLED], 0,
464 source_registry_source_changed_cb (ESource *source,
465 ESourceRegistry *registry)
467 GSource *idle_source;
468 SourceClosure *closure;
470 closure = g_slice_new0 (SourceClosure);
471 closure->registry = g_object_ref (registry);
472 closure->source = g_object_ref (source);
474 idle_source = g_idle_source_new ();
475 g_source_set_callback (
477 source_registry_source_changed_idle_cb,
478 closure, (GDestroyNotify) source_closure_free);
479 g_source_attach (idle_source, registry->priv->main_context);
480 g_source_unref (idle_source);
484 source_registry_source_notify_enabled_cb (ESource *source,
486 ESourceRegistry *registry)
488 GSource *idle_source;
489 SourceClosure *closure;
491 closure = g_slice_new0 (SourceClosure);
492 closure->registry = g_object_ref (registry);
493 closure->source = g_object_ref (source);
495 idle_source = g_idle_source_new ();
496 g_source_set_callback (
498 source_registry_source_notify_enabled_idle_cb,
499 closure, (GDestroyNotify) source_closure_free);
500 g_source_attach (idle_source, registry->priv->main_context);
501 g_source_unref (idle_source);
505 source_registry_new_source (ESourceRegistry *registry,
506 GDBusObject *dbus_object)
508 GMainContext *main_context;
510 const gchar *object_path;
511 GError *error = NULL;
513 /* We don't want the ESource emitting "changed" signals from
514 * the manager thread, so we pass it the same main context the
515 * registry uses for scheduling signal emissions. */
516 main_context = registry->priv->main_context;
517 source = e_source_new (dbus_object, main_context, &error);
518 object_path = g_dbus_object_get_object_path (dbus_object);
520 /* The likelihood of an error here is slim, so it's
521 * sufficient to just print a warning if one occurs. */
523 g_warn_if_fail (source == NULL);
525 "ESourceRegistry: Failed to create a "
526 "data source object for path '%s': %s",
527 object_path, error->message);
528 g_error_free (error);
532 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
534 /* Add the ESource to the object path table immediately. */
535 source_registry_object_path_table_insert (
536 registry, object_path, source);
542 source_registry_unref_source (ESource *source)
544 g_signal_handlers_disconnect_matched (
545 source, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
546 source_registry_source_changed_cb, NULL);
548 g_signal_handlers_disconnect_matched (
549 source, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
550 source_registry_source_notify_enabled_cb, NULL);
552 g_object_unref (source);
556 source_registry_add_source (ESourceRegistry *registry,
561 uid = e_source_get_uid (source);
562 g_return_if_fail (uid != NULL);
564 g_mutex_lock (registry->priv->sources_lock);
566 /* Check if we already have this source in the registry. */
567 if (g_hash_table_lookup (registry->priv->sources, uid) != NULL) {
568 g_mutex_unlock (registry->priv->sources_lock);
574 G_CALLBACK (source_registry_source_changed_cb),
578 source, "notify::enabled",
579 G_CALLBACK (source_registry_source_notify_enabled_cb),
582 g_mutex_unlock (registry->priv->sources_lock);
584 source_registry_sources_insert (registry, source);
586 g_signal_emit (registry, signals[SOURCE_ADDED], 0, source);
590 source_registry_remove_source (ESourceRegistry *registry,
593 g_object_ref (source);
595 if (source_registry_sources_remove (registry, source))
596 g_signal_emit (registry, signals[SOURCE_REMOVED], 0, source);
598 g_object_unref (source);
602 source_registry_object_added_idle_cb (gpointer user_data)
604 SourceClosure *closure = user_data;
606 source_registry_add_source (closure->registry, closure->source);
612 source_registry_object_added_cb (GDBusObjectManager *object_manager,
613 GDBusObject *dbus_object,
614 ESourceRegistry *registry)
616 SourceClosure *closure;
617 GSource *idle_source;
620 g_return_if_fail (E_DBUS_IS_OBJECT (dbus_object));
622 source = source_registry_new_source (registry, dbus_object);
623 g_return_if_fail (source != NULL);
625 /* Schedule a callback on the ESourceRegistry's GMainContext. */
627 closure = g_slice_new0 (SourceClosure);
628 closure->registry = g_object_ref (registry);
629 closure->source = g_object_ref (source);
631 idle_source = g_idle_source_new ();
632 g_source_set_callback (
634 source_registry_object_added_idle_cb,
635 closure, (GDestroyNotify) source_closure_free);
636 g_source_attach (idle_source, registry->priv->main_context);
637 g_source_unref (idle_source);
639 g_object_unref (source);
643 source_registry_object_removed_idle_cb (gpointer user_data)
645 SourceClosure *closure = user_data;
647 source_registry_remove_source (closure->registry, closure->source);
653 source_registry_object_removed_cb (GDBusObjectManager *manager,
654 GDBusObject *dbus_object,
655 ESourceRegistry *registry)
657 SourceClosure *closure;
658 GSource *idle_source;
660 const gchar *object_path;
662 /* Find the corresponding ESource in the object path table.
663 * Note that the lookup returns a new ESource reference. */
664 object_path = g_dbus_object_get_object_path (dbus_object);
665 source = source_registry_object_path_table_lookup (
666 registry, object_path);
667 g_return_if_fail (E_IS_SOURCE (source));
669 /* Remove the ESource from the object path table immediately. */
670 source_registry_object_path_table_remove (registry, object_path);
672 /* Schedule a callback on the ESourceRegistry's GMainContext. */
674 closure = g_slice_new0 (SourceClosure);
675 closure->registry = g_object_ref (registry);
676 closure->source = g_object_ref (source);
678 idle_source = g_idle_source_new ();
679 g_source_set_callback (
681 source_registry_object_removed_idle_cb,
682 closure, (GDestroyNotify) source_closure_free);
683 g_source_attach (idle_source, registry->priv->main_context);
684 g_source_unref (idle_source);
686 g_object_unref (source);
690 source_registry_object_manager_running (gpointer data)
692 ThreadClosure *closure = data;
694 g_mutex_lock (closure->main_loop_mutex);
695 g_cond_broadcast (closure->main_loop_cond);
696 g_mutex_unlock (closure->main_loop_mutex);
702 source_registry_object_manager_thread (gpointer data)
704 GDBusObjectManager *object_manager;
705 ThreadClosure *closure = data;
706 GSource *idle_source;
708 gulong object_added_id;
709 gulong object_removed_id;
710 GError *error = NULL;
712 /* GDBusObjectManagerClient grabs the thread-default GMainContext
713 * at creation time and only emits signals from that GMainContext.
714 * Running it in a separate thread prevents its signal emissions
715 * from being inhibited by someone overriding the thread-default
718 /* This becomes the GMainContext that GDBusObjectManagerClient
719 * will emit signals from. Make it the thread-default context
720 * for this thread before creating the client. */
721 g_main_context_push_thread_default (closure->main_context);
723 object_manager = e_dbus_object_manager_client_new_for_bus_sync (
725 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
726 SOURCES_DBUS_SERVICE_NAME,
730 /* If this fails there's really no point in continuing
731 * since we rely on the object manager to populate the
732 * registry. Abort the process with a fatal error. */
734 g_error ("%s", error->message);
735 g_assert_not_reached ();
738 /* Give the registry a handle to the object manager. */
739 closure->registry->priv->dbus_object_manager =
740 g_object_ref (object_manager);
742 /* Now populate the registry with an initial set of ESources. */
744 list = g_dbus_object_manager_get_objects (object_manager);
746 for (link = list; link != NULL; link = g_list_next (link))
747 source_registry_object_added_cb (
749 G_DBUS_OBJECT (link->data),
752 g_list_free_full (list, (GDestroyNotify) g_object_unref);
754 /* Schedule a one-time idle callback to broadcast through a
755 * condition variable that our main loop is up and running. */
757 idle_source = g_idle_source_new ();
758 g_source_set_callback (
760 source_registry_object_manager_running,
761 closure, (GDestroyNotify) NULL);
762 g_source_attach (idle_source, closure->main_context);
763 g_source_unref (idle_source);
765 /* Listen for D-Bus object additions and removals. */
767 object_added_id = g_signal_connect (
768 object_manager, "object-added",
769 G_CALLBACK (source_registry_object_added_cb),
772 object_removed_id = g_signal_connect (
773 object_manager, "object-removed",
774 G_CALLBACK (source_registry_object_removed_cb),
777 /* Now we mostly idle here for the rest of the session. */
779 g_main_loop_run (closure->main_loop);
781 /* Clean up and exit. */
783 g_signal_handler_disconnect (object_manager, object_added_id);
784 g_signal_handler_disconnect (object_manager, object_removed_id);
786 g_object_unref (object_manager);
788 g_main_context_pop_thread_default (closure->main_context);
794 source_registry_set_property (GObject *object,
799 switch (property_id) {
800 case PROP_DEFAULT_ADDRESS_BOOK:
801 e_source_registry_set_default_address_book (
802 E_SOURCE_REGISTRY (object),
803 g_value_get_object (value));
806 case PROP_DEFAULT_CALENDAR:
807 e_source_registry_set_default_calendar (
808 E_SOURCE_REGISTRY (object),
809 g_value_get_object (value));
812 case PROP_DEFAULT_MAIL_ACCOUNT:
813 e_source_registry_set_default_mail_account (
814 E_SOURCE_REGISTRY (object),
815 g_value_get_object (value));
818 case PROP_DEFAULT_MAIL_IDENTITY:
819 e_source_registry_set_default_mail_identity (
820 E_SOURCE_REGISTRY (object),
821 g_value_get_object (value));
824 case PROP_DEFAULT_MEMO_LIST:
825 e_source_registry_set_default_memo_list (
826 E_SOURCE_REGISTRY (object),
827 g_value_get_object (value));
830 case PROP_DEFAULT_TASK_LIST:
831 e_source_registry_set_default_task_list (
832 E_SOURCE_REGISTRY (object),
833 g_value_get_object (value));
837 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
841 source_registry_get_property (GObject *object,
846 switch (property_id) {
847 case PROP_DEFAULT_ADDRESS_BOOK:
848 g_value_take_object (
850 e_source_registry_ref_default_address_book (
851 E_SOURCE_REGISTRY (object)));
854 case PROP_DEFAULT_CALENDAR:
855 g_value_take_object (
857 e_source_registry_ref_default_calendar (
858 E_SOURCE_REGISTRY (object)));
861 case PROP_DEFAULT_MAIL_ACCOUNT:
862 g_value_take_object (
864 e_source_registry_ref_default_mail_account (
865 E_SOURCE_REGISTRY (object)));
868 case PROP_DEFAULT_MAIL_IDENTITY:
869 g_value_take_object (
871 e_source_registry_ref_default_mail_identity (
872 E_SOURCE_REGISTRY (object)));
875 case PROP_DEFAULT_MEMO_LIST:
876 g_value_take_object (
878 e_source_registry_ref_default_memo_list (
879 E_SOURCE_REGISTRY (object)));
882 case PROP_DEFAULT_TASK_LIST:
883 g_value_take_object (
885 e_source_registry_ref_default_task_list (
886 E_SOURCE_REGISTRY (object)));
890 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
894 source_registry_dispose (GObject *object)
896 ESourceRegistryPrivate *priv;
898 priv = E_SOURCE_REGISTRY_GET_PRIVATE (object);
900 /* Terminate the manager thread first. */
901 if (priv->manager_thread != NULL) {
902 g_main_loop_quit (priv->thread_closure->main_loop);
903 g_thread_join (priv->manager_thread);
904 thread_closure_free (priv->thread_closure);
905 priv->manager_thread = NULL;
906 priv->thread_closure = NULL;
909 if (priv->main_context != NULL) {
910 g_main_context_unref (priv->main_context);
911 priv->main_context = NULL;
914 if (priv->dbus_object_manager != NULL) {
915 g_object_unref (priv->dbus_object_manager);
916 priv->dbus_object_manager = NULL;
919 if (priv->dbus_source_manager != NULL) {
920 g_object_unref (priv->dbus_source_manager);
921 priv->dbus_source_manager = NULL;
924 g_hash_table_remove_all (priv->object_path_table);
926 g_hash_table_remove_all (priv->sources);
928 if (priv->settings != NULL) {
929 g_object_unref (priv->settings);
930 priv->settings = NULL;
933 /* Chain up to parent's finalize() method. */
934 G_OBJECT_CLASS (e_source_registry_parent_class)->dispose (object);
938 source_registry_finalize (GObject *object)
940 ESourceRegistryPrivate *priv;
942 priv = E_SOURCE_REGISTRY_GET_PRIVATE (object);
944 g_hash_table_destroy (priv->object_path_table);
945 g_mutex_free (priv->object_path_table_lock);
947 g_hash_table_destroy (priv->sources);
948 g_mutex_free (priv->sources_lock);
950 /* Chain up to parent's finalize() method. */
951 G_OBJECT_CLASS (e_source_registry_parent_class)->finalize (object);
955 source_registry_initable_init (GInitable *initable,
956 GCancellable *cancellable,
959 ESourceRegistry *registry;
960 ThreadClosure *closure;
962 registry = E_SOURCE_REGISTRY (initable);
964 closure = g_slice_new0 (ThreadClosure);
965 closure->registry = registry; /* do not reference */
966 closure->main_context = g_main_context_new ();
967 /* It's important to pass 'is_running=FALSE' here because
968 * we wait for the main loop to start running as a way of
969 * synchronizing with the manager thread. */
970 closure->main_loop = g_main_loop_new (closure->main_context, FALSE);
971 closure->main_loop_cond = g_cond_new ();
972 closure->main_loop_mutex = g_mutex_new ();
974 registry->priv->thread_closure = closure;
976 registry->priv->manager_thread = g_thread_create (
977 source_registry_object_manager_thread,
978 closure, TRUE /* joinable */, error);
980 if (registry->priv->manager_thread == NULL)
983 /* Wait for notification that the manager
984 * thread's main loop has been started. */
985 g_mutex_lock (closure->main_loop_mutex);
986 while (!g_main_loop_is_running (closure->main_loop))
988 closure->main_loop_cond,
989 closure->main_loop_mutex);
990 g_mutex_unlock (closure->main_loop_mutex);
992 /* We should now have a GDBusObjectManagerClient available. */
993 g_return_val_if_fail (
994 G_IS_DBUS_OBJECT_MANAGER_CLIENT (
995 registry->priv->dbus_object_manager), FALSE);
997 /* The manager thread will have queued up a bunch of idle
998 * sources on our GMainContext to populate the registry.
999 * Iterate our GMainContext until they get dispatched. */
1000 while (g_hash_table_size (registry->priv->sources) == 0)
1001 g_main_context_iteration (registry->priv->main_context, TRUE);
1003 /* The EDBusSourceManagerProxy is just another D-Bus interface
1004 * that resides at the same object path. It's unrelated to the
1005 * GDBusObjectManagerClient and doesn't need its own thread. */
1006 registry->priv->dbus_source_manager =
1007 e_dbus_source_manager_proxy_new_for_bus_sync (
1009 G_DBUS_PROXY_FLAGS_NONE,
1010 SOURCES_DBUS_SERVICE_NAME,
1012 cancellable, error);
1014 if (registry->priv->dbus_source_manager == NULL)
1021 e_source_registry_class_init (ESourceRegistryClass *class)
1023 GObjectClass *object_class;
1025 g_type_class_add_private (class, sizeof (ESourceRegistryPrivate));
1027 object_class = G_OBJECT_CLASS (class);
1028 object_class->set_property = source_registry_set_property;
1029 object_class->get_property = source_registry_get_property;
1030 object_class->dispose = source_registry_dispose;
1031 object_class->finalize = source_registry_finalize;
1033 /* The property names correspond to the key names in the
1034 * "org.gnome.Evolution.DefaultSources" GSettings schema. */
1037 * ESourceRegistry:default-address-book:
1039 * The default address book #ESource.
1041 g_object_class_install_property (
1043 PROP_DEFAULT_ADDRESS_BOOK,
1044 g_param_spec_object (
1045 "default-address-book",
1046 "Default Address Book",
1047 "The default address book ESource",
1050 G_PARAM_STATIC_STRINGS));
1053 * ESourceRegistry:default-calendar:
1055 * The default calendar #ESource.
1057 g_object_class_install_property (
1059 PROP_DEFAULT_CALENDAR,
1060 g_param_spec_object (
1063 "The default calendar ESource",
1066 G_PARAM_STATIC_STRINGS));
1069 * ESourceRegistry:default-mail-account:
1071 * The default mail account #ESource.
1073 g_object_class_install_property (
1075 PROP_DEFAULT_MAIL_ACCOUNT,
1076 g_param_spec_object (
1077 "default-mail-account",
1078 "Default Mail Account",
1079 "The default mail account ESource",
1082 G_PARAM_STATIC_STRINGS));
1085 * ESourceRegistry:default-mail-identity:
1087 * The default mail identity #ESource.
1089 g_object_class_install_property (
1091 PROP_DEFAULT_MAIL_IDENTITY,
1092 g_param_spec_object (
1093 "default-mail-identity",
1094 "Default Mail Identity",
1095 "The default mail identity ESource",
1098 G_PARAM_STATIC_STRINGS));
1101 * ESourceRegistry:default-memo-list:
1103 * The default memo list #ESource.
1105 g_object_class_install_property (
1107 PROP_DEFAULT_MEMO_LIST,
1108 g_param_spec_object (
1109 "default-memo-list",
1110 "Default Memo List",
1111 "The default memo list ESource",
1114 G_PARAM_STATIC_STRINGS));
1117 * ESourceRegistry:default-task-list:
1119 * The default task list #ESource.
1121 g_object_class_install_property (
1123 PROP_DEFAULT_TASK_LIST,
1124 g_param_spec_object (
1125 "default-task-list",
1126 "Default Task List",
1127 "The default task list ESource",
1130 G_PARAM_STATIC_STRINGS));
1133 * ESourceRegistry::source-added:
1134 * @registry: the #ESourceRegistry which emitted the signal
1135 * @source: the newly-added #ESource
1137 * Emitted when an #ESource is added to @registry.
1139 signals[SOURCE_ADDED] = g_signal_new (
1141 G_OBJECT_CLASS_TYPE (object_class),
1143 G_STRUCT_OFFSET (ESourceRegistryClass, source_added),
1145 g_cclosure_marshal_VOID__OBJECT,
1150 * ESourceRegistry::source-changed:
1151 * @registry: the #ESourceRegistry which emitted the signal
1152 * @source: the #ESource that changed
1154 * Emitted when an #ESource registered with @registry emits
1155 * its #ESource::changed signal.
1157 signals[SOURCE_CHANGED] = g_signal_new (
1159 G_OBJECT_CLASS_TYPE (object_class),
1161 G_STRUCT_OFFSET (ESourceRegistryClass, source_changed),
1163 g_cclosure_marshal_VOID__OBJECT,
1168 * ESourceRegistry::source-removed:
1169 * @registry: the #ESourceRegistry which emitted the signal
1170 * @source: the #ESource that got removed
1172 * Emitted when an #ESource is removed from @registry.
1174 signals[SOURCE_REMOVED] = g_signal_new (
1176 G_OBJECT_CLASS_TYPE (object_class),
1178 G_STRUCT_OFFSET (ESourceRegistryClass, source_removed),
1180 g_cclosure_marshal_VOID__OBJECT,
1185 * ESourceRegistry::source-enabled:
1186 * @registry: the #ESourceRegistry which emitted the signal
1187 * @source: the #ESource that got enabled
1189 * Emitted when an #ESource #ESource:enabled property becomes %TRUE.
1191 signals[SOURCE_ENABLED] = g_signal_new (
1193 G_OBJECT_CLASS_TYPE (object_class),
1195 G_STRUCT_OFFSET (ESourceRegistryClass, source_enabled),
1197 g_cclosure_marshal_VOID__OBJECT,
1202 * ESourceRegistry::source-disabled:
1203 * @registry: the #ESourceRegistry which emitted the signal
1204 * @source: the #ESource that got disabled
1206 * Emitted when an #ESource #ESource:enabled property becomes %FALSE.
1208 signals[SOURCE_DISABLED] = g_signal_new (
1210 G_OBJECT_CLASS_TYPE (object_class),
1212 G_STRUCT_OFFSET (ESourceRegistryClass, source_disabled),
1214 g_cclosure_marshal_VOID__OBJECT,
1220 e_source_registry_initable_init (GInitableIface *interface)
1222 interface->init = source_registry_initable_init;
1226 e_source_registry_init (ESourceRegistry *registry)
1228 registry->priv = E_SOURCE_REGISTRY_GET_PRIVATE (registry);
1230 /* This is so the object manager thread can schedule signal
1231 * emissions on the thread-default context for this thread. */
1232 registry->priv->main_context = g_main_context_ref_thread_default ();
1234 /* D-Bus object path -> ESource */
1235 registry->priv->object_path_table =
1236 g_hash_table_new_full (
1237 (GHashFunc) g_str_hash,
1238 (GEqualFunc) g_str_equal,
1239 (GDestroyNotify) g_free,
1240 (GDestroyNotify) g_object_unref);
1242 registry->priv->object_path_table_lock = g_mutex_new ();
1244 /* UID string -> ESource */
1245 registry->priv->sources = g_hash_table_new_full (
1246 (GHashFunc) g_str_hash,
1247 (GEqualFunc) g_str_equal,
1248 (GDestroyNotify) g_free,
1249 (GDestroyNotify) source_registry_unref_source);
1251 registry->priv->sources_lock = g_mutex_new ();
1253 registry->priv->settings = g_settings_new (GSETTINGS_SCHEMA);
1256 registry->priv->settings, "changed",
1257 G_CALLBACK (source_registry_settings_changed_cb), registry);
1261 * e_source_registry_new_sync:
1262 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1263 * @error: return location for a #GError, or %NULL
1265 * Creates a new #ESourceRegistry front-end for the registry D-Bus service.
1266 * If an error occurs in connecting to the D-Bus service, the function sets
1267 * @error and returns %NULL.
1269 * Returns: a new #ESourceRegistry, or %NULL
1274 e_source_registry_new_sync (GCancellable *cancellable,
1277 return g_initable_new (
1278 E_TYPE_SOURCE_REGISTRY,
1279 cancellable, error, NULL);
1283 * e_source_registry_new:
1284 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1285 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1287 * @user_data: (closure): data to pass to the callback function
1289 * Asynchronously creates a new #ESourceRegistry front-end for the registry
1292 * When the operation is finished, @callback will be called. You can then
1293 * call e_source_registry_new_finish() to get the result of the operation.
1298 e_source_registry_new (GCancellable *cancellable,
1299 GAsyncReadyCallback callback,
1302 g_async_initable_new_async (
1303 E_TYPE_SOURCE_REGISTRY,
1304 G_PRIORITY_DEFAULT, cancellable,
1305 callback, user_data, NULL);
1309 * e_source_registry_new_finish:
1310 * @result: a #GAsyncResult
1311 * @error: return location for a #GError, or %NULL
1313 * Finishes the operation started with e_source_registry_new_finish().
1314 * If an error occurs in connecting to the D-Bus service, the function
1315 * sets @error and returns %NULL.
1317 * Returns: a new #ESourceRegistry, or %NULL
1322 e_source_registry_new_finish (GAsyncResult *result,
1325 GObject *source_object;
1328 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
1330 source_object = g_async_result_get_source_object (result);
1331 g_return_val_if_fail (source_object != NULL, NULL);
1333 object = g_async_initable_new_finish (
1334 G_ASYNC_INITABLE (source_object), result, error);
1336 g_object_unref (source_object);
1338 return (object != NULL) ? E_SOURCE_REGISTRY (object) : NULL;
1341 /* Helper for e_source_registry_authenticate() */
1343 source_registry_authenticate_thread (GSimpleAsyncResult *simple,
1345 GCancellable *cancellable)
1347 AsyncContext *async_context;
1348 GError *error = NULL;
1350 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1352 e_source_registry_authenticate_sync (
1353 E_SOURCE_REGISTRY (object),
1354 async_context->source,
1355 async_context->auth,
1356 cancellable, &error);
1359 g_simple_async_result_take_error (simple, error);
1362 /* Helper for e_source_registry_authenticate_sync() */
1364 source_registry_authenticate_respond_cb (AuthContext *auth_context)
1366 ESourceAuthenticationResult auth_result;
1367 GError *non_fatal_error = NULL;
1369 g_return_val_if_fail (auth_context->authenticating, FALSE);
1371 auth_result = auth_context->auth_result;
1373 /* Allow the next authentication attempt to proceed. */
1374 auth_context->authenticating = FALSE;
1376 /* Send the server a status update based on the authentication
1377 * result. Note, we don't really care if the D-Bus message gets
1378 * through to the server at this point. If it doesn't, the auth
1379 * session will either time out on its own or the authentication
1380 * dialog will eventually be dismissed by the user. */
1382 /* If an error occurred while attempting to authenticate,
1383 * tell the server to cancel the authentication session. */
1384 if (auth_result == E_SOURCE_AUTHENTICATION_ERROR) {
1385 e_dbus_authenticator_call_cancel_sync (
1386 auth_context->dbus_auth,
1387 auth_context->cancellable,
1389 g_main_loop_quit (auth_context->main_loop);
1390 auth_context->success = FALSE;
1392 /* If the password was accepted, let the server know so it
1393 * can close any authentication dialogs and save the user
1394 * provided password to the keyring. */
1395 } else if (auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
1396 e_dbus_authenticator_call_accepted_sync (
1397 auth_context->dbus_auth,
1398 auth_context->cancellable,
1400 g_main_loop_quit (auth_context->main_loop);
1401 auth_context->success = TRUE;
1403 /* If the password was rejected, let the server know so it can
1404 * indicate failure and request a different password, and then
1405 * wait for the next "response" signal. */
1407 e_dbus_authenticator_call_rejected_sync (
1408 auth_context->dbus_auth,
1409 auth_context->cancellable,
1413 /* Leave breadcrumbs if something went wrong,
1414 * but don't fail the whole operation over it. */
1415 if (non_fatal_error != NULL) {
1416 g_warning ("%s: %s", G_STRFUNC, non_fatal_error->message);
1417 g_error_free (non_fatal_error);
1423 /* Helper for e_source_registry_authenticate_sync() */
1425 source_registry_authenticate_authenticate_cb (EDBusAuthenticator *dbus_auth,
1426 const gchar *encrypted_secret,
1427 AuthContext *auth_context)
1429 GSource *idle_source;
1430 GMainContext *main_context;
1432 gboolean valid_secret;
1434 /* We should only get one secret at a time. */
1435 g_return_if_fail (!auth_context->authenticating);
1437 valid_secret = gcr_secret_exchange_receive (
1438 auth_context->secret_exchange, encrypted_secret);
1439 g_return_if_fail (valid_secret);
1441 auth_context->authenticating = TRUE;
1443 /* This avoids revealing the password in a stack trace. */
1444 password = g_string_new (
1445 gcr_secret_exchange_get_secret (
1446 auth_context->secret_exchange, NULL));
1448 /* Try authenticating with the given password. We have to
1449 * call this synchronously because some authenticators use
1450 * mutexes to serialize I/O operations and are not prepared
1451 * to make authentication attempts from a different thread.
1453 * Unfortunately this means we won't notice server-side
1454 * dismissals while the main loop is blocked. We respond
1455 * to the server from a low-priority idle callback so that
1456 * any pending "dismissed" signals get handled first. */
1458 auth_context->auth_result =
1459 e_source_authenticator_try_password_sync (
1460 auth_context->auth, password,
1461 auth_context->cancellable,
1462 auth_context->error);
1464 idle_source = g_idle_source_new ();
1465 main_context = g_main_context_get_thread_default ();
1466 g_source_set_callback (
1467 idle_source, (GSourceFunc)
1468 source_registry_authenticate_respond_cb,
1469 auth_context, NULL);
1470 g_source_attach (idle_source, main_context);
1471 g_source_unref (idle_source);
1473 g_string_free (password, TRUE);
1476 /* Helper for e_source_registry_authenticate_sync() */
1478 source_registry_authenticate_dismissed_cb (EDBusAuthenticator *dbus_auth,
1479 AuthContext *auth_context)
1481 /* Be careful not to overwrite an existing error in case this
1482 * is called after e_source_authenticator_try_password_sync()
1483 * but prior to the idle callback. */
1484 if (auth_context->auth_result != E_SOURCE_AUTHENTICATION_ERROR) {
1485 /* XXX Use a separate error code for dismissals? */
1486 g_set_error_literal (
1487 auth_context->error,
1488 G_IO_ERROR, G_IO_ERROR_CANCELLED,
1489 _("The user declined to authenticate"));
1490 auth_context->auth_result = E_SOURCE_AUTHENTICATION_ERROR;
1493 g_main_loop_quit (auth_context->main_loop);
1494 auth_context->success = FALSE;
1497 /* Helper for e_source_registry_authenticate_sync() */
1499 source_registry_call_authenticate_for_source (ESourceRegistry *registry,
1500 ESourceAuthenticator *auth,
1502 gchar **out_object_path,
1503 GCancellable *cancellable,
1506 ESource *collection;
1508 gchar *prompt_title = NULL;
1509 gchar *prompt_message = NULL;
1510 gchar *prompt_description = NULL;
1513 /* If the source is a member of a collection, we want to store
1514 * the password under the UID of the "collection" source so it
1515 * will apply to the entire collection.
1517 * XXX This assumes all sources in a collection share a single
1518 * password. If that turns out not to be true in all cases
1519 * we could maybe add a "SharedPassword: true/false" key to
1520 * [Collection] and apply it here.
1522 collection = e_source_registry_find_extension (
1523 registry, source, E_SOURCE_EXTENSION_COLLECTION);
1524 if (collection != NULL)
1525 source = collection;
1527 g_object_ref (source);
1529 uid = e_source_get_uid (source);
1531 e_source_authenticator_get_prompt_strings (
1535 &prompt_description);
1537 success = e_dbus_source_manager_call_authenticate_sync (
1538 registry->priv->dbus_source_manager, uid,
1539 prompt_title, prompt_message, prompt_description,
1540 out_object_path, cancellable, error);
1542 g_free (prompt_title);
1543 g_free (prompt_message);
1544 g_free (prompt_description);
1546 g_object_unref (source);
1552 * e_source_registry_authenticate_sync:
1553 * @registry: an #ESourceRegistry
1554 * @source: an #ESource
1555 * @auth: an #ESourceAuthenticator
1556 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1557 * @error: return location for a #GError, or %NULL
1559 * Authenticates @source, using @auth to handle the authentication
1560 * attempts. The operation loops until authentication is successful or
1561 * the user aborts further authentication attempts. If an error occurs,
1562 * the function will set @error and return %FALSE.
1564 * Note that @source need not have a #GDBusObject, which means this
1565 * function can test authentication on a scratch #ESource.
1567 * Only backend implementations and data source editors should call this
1568 * function. The intent is for basic client applications to not have to
1569 * deal with authentication at all.
1571 * Returns: %TRUE on success, %FALSE on failure
1576 e_source_registry_authenticate_sync (ESourceRegistry *registry,
1578 ESourceAuthenticator *auth,
1579 GCancellable *cancellable,
1582 AuthContext *auth_context;
1583 GMainContext *main_context;
1584 EDBusAuthenticator *dbus_auth;
1585 gchar *encryption_key;
1586 gchar *object_path = NULL;
1589 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1590 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
1591 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth), FALSE);
1593 /* This extracts authentication prompt details for the ESource
1594 * before initiating an authentication session with the server,
1595 * so split it out of the main algorithm for clarity's sake. */
1596 success = source_registry_call_authenticate_for_source (
1597 registry, auth, source, &object_path, cancellable, error);
1600 g_warn_if_fail (object_path == NULL);
1604 g_return_val_if_fail (object_path != NULL, FALSE);
1606 main_context = g_main_context_new ();
1607 g_main_context_push_thread_default (main_context);
1609 dbus_auth = e_dbus_authenticator_proxy_new_for_bus_sync (
1611 G_DBUS_PROXY_FLAGS_NONE,
1612 SOURCES_DBUS_SERVICE_NAME,
1613 object_path, cancellable, error);
1615 g_free (object_path);
1617 if (dbus_auth == NULL) {
1622 auth_context = g_slice_new0 (AuthContext);
1623 auth_context->auth = g_object_ref (auth);
1624 auth_context->dbus_auth = dbus_auth; /* takes ownership */
1625 auth_context->main_loop = g_main_loop_new (main_context, FALSE);
1626 auth_context->error = error;
1628 /* This just needs to be something other than
1629 * E_SOURCE_AUTHENTICATION_ERROR so we don't trip
1630 * up source_registry_authenticate_dismissed_cb(). */
1631 auth_context->auth_result = E_SOURCE_AUTHENTICATION_REJECTED;
1633 if (G_IS_CANCELLABLE (cancellable))
1634 auth_context->cancellable = g_object_ref (cancellable);
1636 auth_context->secret_exchange =
1637 gcr_secret_exchange_new (GCR_SECRET_EXCHANGE_PROTOCOL_1);
1640 dbus_auth, "authenticate",
1641 G_CALLBACK (source_registry_authenticate_authenticate_cb),
1645 dbus_auth, "dismissed",
1646 G_CALLBACK (source_registry_authenticate_dismissed_cb),
1649 encryption_key = gcr_secret_exchange_begin (
1650 auth_context->secret_exchange);
1652 /* Signal the D-Bus server that we're ready to begin the
1653 * authentication session. This must happen AFTER we've
1654 * connected to the response signal since the server may
1655 * already have a response ready and waiting for us. */
1656 success = e_dbus_authenticator_call_ready_sync (
1657 dbus_auth, encryption_key, cancellable, error);
1659 g_free (encryption_key);
1662 g_main_loop_run (auth_context->main_loop);
1663 success = auth_context->success;
1666 auth_context_free (auth_context);
1669 g_main_context_pop_thread_default (main_context);
1670 g_main_context_unref (main_context);
1676 * e_source_registry_authenticate:
1677 * @registry: an #ESourceRegistry
1678 * @source: an #ESource
1679 * @auth: an #ESourceAuthenticator
1680 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1681 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1683 * @user_data: (closure): data to pass to the callback function
1685 * Asynchronously authenticates @source, using @auth to handle the
1686 * authentication attempts. The operation loops until authentication
1687 * is successful or the user aborts further authentication attempts.
1689 * Note that @source need not have a #GDBusObject, which means this
1690 * function can test authentication on a scratch #ESource.
1692 * When the operation is finished, @callback will be called. You can then
1693 * call e_source_registry_authenticate_finish() to get the result of the
1696 * Only backend implementations and data source editors should call this
1697 * function. The intent is for basic client applications to not have to
1698 * deal with authentication at all.
1703 e_source_registry_authenticate (ESourceRegistry *registry,
1705 ESourceAuthenticator *auth,
1706 GCancellable *cancellable,
1707 GAsyncReadyCallback callback,
1710 GSimpleAsyncResult *simple;
1711 AsyncContext *async_context;
1713 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
1714 g_return_if_fail (E_IS_SOURCE (source));
1715 g_return_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth));
1717 async_context = g_slice_new0 (AsyncContext);
1718 async_context->source = g_object_ref (source);
1719 async_context->auth = g_object_ref (auth);
1721 simple = g_simple_async_result_new (
1722 G_OBJECT (registry), callback, user_data,
1723 e_source_registry_authenticate);
1725 g_simple_async_result_set_check_cancellable (simple, cancellable);
1727 g_simple_async_result_set_op_res_gpointer (
1728 simple, async_context, (GDestroyNotify) async_context_free);
1730 g_simple_async_result_run_in_thread (
1731 simple, source_registry_authenticate_thread,
1732 G_PRIORITY_DEFAULT, cancellable);
1734 g_object_unref (simple);
1738 * e_source_registry_authenticate_finish:
1739 * @registry: an #ESourceRegistry
1740 * @result: a #GAsyncResult
1741 * @error: return location for a #GError, or %NULL
1743 * Finishes the operation started with e_source_registry_authenticate().
1744 * If an error occurred, the function will set @error and return %FALSE.
1746 * Returns: %TRUE on success, %FALSE on failure
1751 e_source_registry_authenticate_finish (ESourceRegistry *registry,
1752 GAsyncResult *result,
1755 GSimpleAsyncResult *simple;
1757 g_return_val_if_fail (
1758 g_simple_async_result_is_valid (
1759 result, G_OBJECT (registry),
1760 e_source_registry_authenticate), FALSE);
1762 simple = G_SIMPLE_ASYNC_RESULT (result);
1764 /* Assume success unless a GError is set. */
1765 return !g_simple_async_result_propagate_error (simple, error);
1768 /* Helper for e_source_registry_commit_source() */
1770 source_registry_commit_source_thread (GSimpleAsyncResult *simple,
1772 GCancellable *cancellable)
1774 AsyncContext *async_context;
1775 GError *error = NULL;
1777 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1779 e_source_registry_commit_source_sync (
1780 E_SOURCE_REGISTRY (object),
1781 async_context->source,
1782 cancellable, &error);
1785 g_simple_async_result_take_error (simple, error);
1789 * e_source_registry_commit_source_sync:
1790 * @registry: an #ESourceRegistry
1791 * @source: an #ESource with changes to commit
1792 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1793 * @error: return location for #GError, or %NULL
1795 * This is a convenience function intended for use with graphical
1796 * #ESource editors. Call this function when the user is finished
1797 * making changes to @source.
1799 * If @source has a #GDBusObject, its contents are submitted to the D-Bus
1800 * service through e_source_write_sync().
1802 * If @source does NOT have a #GDBusObject (implying it's a scratch
1803 * #ESource), its contents are submitted to the D-Bus service through
1804 * e_source_registry_create_sources_sync().
1806 * If an error occurs, the function will set @error and return %FALSE.
1808 * Returns: %TRUE on success, %FALSE on failure
1813 e_source_registry_commit_source_sync (ESourceRegistry *registry,
1815 GCancellable *cancellable,
1818 GDBusObject *dbus_object;
1821 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1822 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
1824 dbus_object = e_source_ref_dbus_object (source);
1826 if (dbus_object != NULL) {
1827 success = e_source_write_sync (source, cancellable, error);
1828 g_object_unref (dbus_object);
1830 GList *list = g_list_prepend (NULL, source);
1831 success = e_source_registry_create_sources_sync (
1832 registry, list, cancellable, error);
1840 * e_source_registry_commit_source:
1841 * @registry: an #ESourceRegistry
1842 * @source: an #ESource with changes to commit
1843 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1844 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1846 * @user_data: (closure): data to pass to the callback function
1848 * See e_source_registry_commit_source_sync() for details.
1850 * When the operation is finished, @callback will be called. You can then
1851 * call e_source_registry_commit_source_finish() to get the result of the
1857 e_source_registry_commit_source (ESourceRegistry *registry,
1859 GCancellable *cancellable,
1860 GAsyncReadyCallback callback,
1863 GSimpleAsyncResult *simple;
1864 AsyncContext *async_context;
1866 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
1867 g_return_if_fail (E_IS_SOURCE (source));
1869 async_context = g_slice_new0 (AsyncContext);
1870 async_context->source = g_object_ref (source);
1872 simple = g_simple_async_result_new (
1873 G_OBJECT (registry), callback, user_data,
1874 e_source_registry_commit_source);
1876 g_simple_async_result_set_check_cancellable (simple, cancellable);
1878 g_simple_async_result_set_op_res_gpointer (
1879 simple, async_context, (GDestroyNotify) async_context_free);
1881 g_simple_async_result_run_in_thread (
1882 simple, source_registry_commit_source_thread,
1883 G_PRIORITY_DEFAULT, cancellable);
1885 g_object_unref (simple);
1889 * e_source_registry_commit_source_finish:
1890 * @registry: an #ESourceRegistry
1891 * @result: a #GAsyncResult
1892 * @error: return location for a #GError, or %NULL
1894 * Finishes the operation started with e_source_registry_commit_source().
1896 * If an error occurred, the function will set @error and return %FALSE.
1898 * Returns: %TRUE on success, %FALSE on failure
1903 e_source_registry_commit_source_finish (ESourceRegistry *registry,
1904 GAsyncResult *result,
1907 GSimpleAsyncResult *simple;
1909 g_return_val_if_fail (
1910 g_simple_async_result_is_valid (
1911 result, G_OBJECT (registry),
1912 e_source_registry_commit_source), FALSE);
1914 simple = G_SIMPLE_ASYNC_RESULT (result);
1916 /* Assume success unless a GError is set. */
1917 return !g_simple_async_result_propagate_error (simple, error);
1920 /* Helper for e_source_registry_create_sources() */
1922 source_registry_create_sources_thread (GSimpleAsyncResult *simple,
1924 GCancellable *cancellable)
1926 AsyncContext *async_context;
1927 GError *error = NULL;
1929 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1931 e_source_registry_create_sources_sync (
1932 E_SOURCE_REGISTRY (object),
1933 async_context->list_of_sources,
1934 cancellable, &error);
1937 g_simple_async_result_take_error (simple, error);
1941 * e_source_registry_create_sources_sync:
1942 * @registry: an #ESourceRegistry
1943 * @list_of_sources: (element-type ESource): a list of #ESource instances with
1945 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1946 * @error: return location for a #GError, or %NULL
1948 * Requests the D-Bus service create new key files for each #ESource in
1949 * @list_of_sources. Each list element must be a scratch #ESource with
1952 * If an error occurs, the function will set @error and return %FALSE.
1954 * Returns: %TRUE on success, %FALSE on failure
1959 e_source_registry_create_sources_sync (ESourceRegistry *registry,
1960 GList *list_of_sources,
1961 GCancellable *cancellable,
1964 GVariantBuilder builder;
1969 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1971 /* Verify the list elements are all ESources. */
1972 for (link = list_of_sources; link != NULL; link = g_list_next (link))
1973 g_return_val_if_fail (E_IS_SOURCE (link->data), FALSE);
1975 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1977 for (link = list_of_sources; link != NULL; link = g_list_next (link)) {
1982 source = E_SOURCE (link->data);
1983 uid = e_source_get_uid (source);
1985 source_data = e_source_to_string (source, NULL);
1986 g_variant_builder_add (&builder, "{ss}", uid, source_data);
1987 g_free (source_data);
1990 variant = g_variant_builder_end (&builder);
1992 /* This function sinks the floating GVariant reference. */
1993 success = e_dbus_source_manager_call_create_sources_sync (
1994 registry->priv->dbus_source_manager,
1995 variant, cancellable, error);
1997 g_variant_builder_clear (&builder);
2003 * e_source_registry_create_sources:
2004 * @registry: an #ESourceRegistry
2005 * @list_of_sources: (element-type ESource): a list of #ESource instances with
2007 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
2008 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
2010 * @user_data: (closure): data to pass to the callback function
2012 * Asynchronously requests the D-Bus service create new key files for each
2013 * #ESource in @list_of_sources. Each list element must be a scratch
2014 * #ESource with no #GDBusObject.
2016 * When the operation is finished, @callback will be called. You can then
2017 * call e_source_registry_create_sources_finish() to get the result of the
2023 e_source_registry_create_sources (ESourceRegistry *registry,
2024 GList *list_of_sources,
2025 GCancellable *cancellable,
2026 GAsyncReadyCallback callback,
2029 GSimpleAsyncResult *simple;
2030 AsyncContext *async_context;
2033 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2035 /* Verify the list elements are all ESources. */
2036 for (link = list_of_sources; link != NULL; link = g_list_next (link))
2037 g_return_if_fail (E_IS_SOURCE (link->data));
2039 async_context = g_slice_new0 (AsyncContext);
2040 async_context->list_of_sources = g_list_copy (list_of_sources);
2043 async_context->list_of_sources,
2044 (GFunc) g_object_ref, NULL);
2046 simple = g_simple_async_result_new (
2047 G_OBJECT (registry), callback, user_data,
2048 e_source_registry_create_sources);
2050 g_simple_async_result_set_check_cancellable (simple, cancellable);
2052 g_simple_async_result_set_op_res_gpointer (
2053 simple, async_context, (GDestroyNotify) async_context_free);
2055 g_simple_async_result_run_in_thread (
2056 simple, source_registry_create_sources_thread,
2057 G_PRIORITY_DEFAULT, cancellable);
2059 g_object_unref (simple);
2063 * e_source_registry_create_sources_finish:
2064 * @registry: an #ESourceRegistry
2065 * @result: a #GAsyncResult
2066 * @error: return location for a #GError, or %NULL
2068 * Finishes the operation started with e_source_registry_create_sources().
2070 * If an error occurred, the function will set @error and return %FALSE.
2072 * Returns: %TRUE on success, %FALSE on failure
2077 e_source_registry_create_sources_finish (ESourceRegistry *registry,
2078 GAsyncResult *result,
2081 GSimpleAsyncResult *simple;
2083 g_return_val_if_fail (
2084 g_simple_async_result_is_valid (
2085 result, G_OBJECT (registry),
2086 e_source_registry_create_sources), FALSE);
2088 simple = G_SIMPLE_ASYNC_RESULT (result);
2090 /* Assume success unless a GError is set. */
2091 return !g_simple_async_result_propagate_error (simple, error);
2095 * e_source_registry_ref_source:
2096 * @registry: an #ESourceRegistry
2097 * @uid: a unique identifier string
2099 * Looks up an #ESource in @registry by its unique identifier string.
2101 * The returned #ESource is referenced for thread-safety and must be
2102 * unreferenced with g_object_unref() when finished with it.
2104 * Returns: (transfer full): an #ESource, or %NULL if no match was found
2109 e_source_registry_ref_source (ESourceRegistry *registry,
2112 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2113 g_return_val_if_fail (uid != NULL, NULL);
2115 return source_registry_sources_lookup (registry, uid);
2119 * e_source_registry_list_sources:
2120 * @registry: an #ESourceRegistry
2121 * @extension_name: (allow-none): an extension name, or %NULL
2123 * Returns a list of registered sources, sorted by display name. If
2124 * @extension_name is given, restrict the list to sources having that
2127 * The sources returned in the list are referenced for thread-safety.
2128 * They must each be unreferenced with g_object_unref() when finished
2129 * when them. Free the returned list itself with g_list_free().
2131 * An easy way to free the list properly in one step is as follows:
2134 * g_list_free_full (list, g_object_unref);
2137 * Returns: (element-type ESource) (transfer full): a sorted list of sources
2142 e_source_registry_list_sources (ESourceRegistry *registry,
2143 const gchar *extension_name)
2146 GQueue trash = G_QUEUE_INIT;
2148 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2150 list = g_list_sort (
2151 source_registry_sources_get_values (registry),
2152 (GCompareFunc) e_source_compare_by_display_name);
2154 if (extension_name == NULL)
2157 for (link = list; link != NULL; link = g_list_next (link)) {
2158 ESource *source = E_SOURCE (link->data);
2160 if (!e_source_has_extension (source, extension_name)) {
2161 g_queue_push_tail (&trash, link);
2162 g_object_unref (source);
2166 /* We do want pop_head() here, not pop_head_link(). */
2167 while ((link = g_queue_pop_head (&trash)) != NULL)
2168 list = g_list_delete_link (list, link);
2174 * e_source_registry_find_extension:
2175 * @registry: an #ESourceRegistry
2176 * @source: an #ESource
2177 * @extension_name: the extension name to find
2179 * Examines @source and its ancestors and returns the "deepest" #ESource
2180 * having an #ESourceExtension with the given @extension_name. If neither
2181 * @source nor any of its ancestors have such an extension, the function
2184 * This function is useful in cases when an #ESourceExtension is meant to
2185 * apply to both the #ESource it belongs to and the #ESource's descendants.
2187 * A common example is the #ESourceCollection extension, where descendants
2188 * of an #ESource having an #ESourceCollection extension are implied to be
2189 * members of that collection. In that example, this function can be used
2190 * to test whether @source is a member of a collection.
2192 * The returned #ESource is referenced for thread-safety and must be
2193 * unreferenced with g_object_unref() when finished with it.
2195 * Note the function returns the #ESource containing the #ESourceExtension
2196 * instead of the #ESourceExtension itself because extension instances are
2197 * not to be referenced directly (see e_source_get_extension()).
2199 * Returns: (transfer full): an #ESource, or %NULL if no match was found
2204 e_source_registry_find_extension (ESourceRegistry *registry,
2206 const gchar *extension_name)
2208 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2209 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2210 g_return_val_if_fail (extension_name != NULL, NULL);
2212 g_object_ref (source);
2214 while (!e_source_has_extension (source, extension_name)) {
2217 uid = e_source_dup_parent (source);
2219 g_object_unref (source);
2223 source = e_source_registry_ref_source (registry, uid);
2234 /* Helper for e_source_registry_build_display_tree() */
2236 source_registry_compare_nodes (GNode *node_a,
2239 ESource *source_a = E_SOURCE (node_a->data);
2240 ESource *source_b = E_SOURCE (node_b->data);
2241 const gchar *uid_a, *uid_b;
2243 uid_a = e_source_get_uid (source_a);
2244 uid_b = e_source_get_uid (source_b);
2246 /* Sanity check, with runtime warnings. */
2247 if (uid_a == NULL) {
2248 g_warn_if_reached ();
2251 if (uid_b == NULL) {
2252 g_warn_if_reached ();
2256 /* The built-in "local-stub" source comes first at depth 1. */
2258 if (g_strcmp0 (uid_a, "local-stub") == 0)
2261 if (g_strcmp0 (uid_b, "local-stub") == 0)
2264 /* The built-in "system-*" sources come first at depth 2. */
2266 if (g_str_has_prefix (uid_a, "system-"))
2269 if (g_str_has_prefix (uid_b, "system-"))
2272 return e_source_compare_by_display_name (source_a, source_b);
2275 /* Helper for e_source_registry_build_display_tree() */
2277 source_registry_prune_nodes (GNode *node,
2278 const gchar *extension_name)
2280 GQueue queue = G_QUEUE_INIT;
2283 /* Unlink all the child nodes and place them in a queue. */
2284 while ((child_node = g_node_first_child (node)) != NULL) {
2285 g_node_unlink (child_node);
2286 g_queue_push_tail (&queue, child_node);
2289 /* Sort the queue by source name. */
2291 &queue, (GCompareDataFunc)
2292 source_registry_compare_nodes, NULL);
2294 /* Pop nodes off the head of the queue until the queue is empty.
2295 * If the node has either its own children or the given extension
2296 * name, put it back under the parent node (preserving the sorted
2297 * order). Otherwise delete the node and its descendants. */
2298 while ((child_node = g_queue_pop_head (&queue)) != NULL) {
2299 ESource *child = E_SOURCE (child_node->data);
2300 gboolean append_child_node = FALSE;
2302 if (extension_name == NULL)
2303 append_child_node = e_source_get_enabled (child);
2305 else if (e_source_has_extension (child, extension_name))
2306 append_child_node = e_source_get_enabled (child);
2308 else if (g_node_first_child (child_node) != NULL)
2309 append_child_node = e_source_get_enabled (child);
2311 if (append_child_node)
2312 g_node_append (node, child_node);
2314 e_source_registry_free_display_tree (child_node);
2321 * e_source_registry_build_display_tree:
2322 * @registry: an #ESourceRegistry
2323 * @extension_name: (allow-none): an extension name, or %NULL
2325 * Returns a single #GNode tree of registered sources that can be used to
2326 * populate a #GtkTreeModel. (The root #GNode is just an empty placeholder.)
2328 * Similar to e_source_registry_list_sources(), an @extension_name can be
2329 * given to restrict the tree to sources having that extension name. Parents
2330 * of matched sources are included in the tree regardless of whether they have
2331 * an extension named @extension_name.
2333 * Disabled leaf nodes are automatically excluded from the #GNode tree.
2335 * The sources returned in the tree are referenced for thread-safety.
2336 * They must each be unreferenced with g_object_unref() when finished
2337 * with them. Free the returned tree itself with g_node_destroy().
2338 * For convenience, e_source_registry_free_display_tree() does all
2341 * Returns: (element-type ESource) (transfer full): a tree of sources,
2342 * arranged for display
2347 e_source_registry_build_display_tree (ESourceRegistry *registry,
2348 const gchar *extension_name)
2352 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2354 /* Assemble all data sources into a tree. */
2355 root = source_registry_sources_build_tree (registry);
2357 /* Prune unwanted nodes from the copied source trees.
2358 * This must be done in "post" order (children first)
2359 * since it reorders and deletes child nodes. */
2361 root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2362 (GNodeTraverseFunc) source_registry_prune_nodes,
2363 (gpointer) extension_name);
2368 /* Helper for e_source_registry_free_display_tree() */
2370 source_registry_unref_nodes (GNode *node)
2372 while (node != NULL) {
2373 if (node->children != NULL)
2374 source_registry_unref_nodes (node->children);
2375 if (node->data != NULL)
2376 g_object_unref (node->data);
2382 * e_source_registry_free_display_tree:
2383 * @display_tree: a tree of sources, arranged for display
2385 * Convenience function to free a #GNode tree of registered
2386 * sources created by e_source_registry_build_display_tree().
2391 e_source_registry_free_display_tree (GNode *display_tree)
2393 g_return_if_fail (display_tree != NULL);
2395 /* XXX This would be easier if GLib had something like
2396 * g_node_destroy_full() which took a GDestroyNotify.
2397 * Then the tree would not have to be traversed twice. */
2399 source_registry_unref_nodes (display_tree);
2400 g_node_destroy (display_tree);
2403 /* Helper for e_source_registry_debug_dump() */
2405 source_registry_debug_dump_cb (GNode *node)
2409 /* Root node is an empty placeholder. */
2410 if (G_NODE_IS_ROOT (node))
2413 depth = g_node_depth (node);
2414 for (ii = 2; ii < depth; ii++)
2417 if (E_IS_SOURCE (node->data)) {
2418 ESource *source = E_SOURCE (node->data);
2419 g_print ("\"%s\" ", e_source_get_display_name (source));
2420 g_print ("(%s)", e_source_get_uid (source));
2429 * e_source_registry_debug_dump:
2430 * @registry: an #ESourceRegistry
2431 * @extension_name: (allow-none): an extension name, or %NULL
2433 * Handy debugging function that uses e_source_registry_build_display_tree()
2434 * to print a tree of registered sources to standard output.
2439 e_source_registry_debug_dump (ESourceRegistry *registry,
2440 const gchar *extension_name)
2444 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2446 root = e_source_registry_build_display_tree (registry, extension_name);
2449 root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2450 (GNodeTraverseFunc) source_registry_debug_dump_cb, NULL);
2452 e_source_registry_free_display_tree (root);
2456 * e_source_registry_ref_builtin_address_book:
2457 * @registry: an #ESourceRegistry
2459 * Returns the built-in address book #ESource.
2461 * This #ESource is always present and makes for a safe fallback.
2463 * The returned #ESource is referenced for thread-safety and must be
2464 * unreferenced with g_object_unref() when finished with it.
2466 * Returns: (transfer full): the built-in address book #ESource
2471 e_source_registry_ref_builtin_address_book (ESourceRegistry *registry)
2476 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2478 uid = E_SOURCE_BUILTIN_ADDRESS_BOOK_UID;
2479 source = e_source_registry_ref_source (registry, uid);
2480 g_return_val_if_fail (source != NULL, NULL);
2486 * e_source_registry_ref_default_address_book:
2487 * @registry: an #ESourceRegistry
2489 * Returns the #ESource most recently passed to
2490 * e_source_registry_set_default_address_book() either in this session
2491 * or a previous session, or else falls back to the built-in address book.
2493 * The returned #ESource is referenced for thread-safety and must be
2494 * unreferenced with g_object_unref() when finished with it.
2496 * Returns: (transfer full): the default address book #ESource
2501 e_source_registry_ref_default_address_book (ESourceRegistry *registry)
2507 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2509 key = E_SETTINGS_DEFAULT_ADDRESS_BOOK_KEY;
2510 uid = g_settings_get_string (registry->priv->settings, key);
2511 source = e_source_registry_ref_source (registry, uid);
2514 /* The built-in source is always present. */
2516 source = e_source_registry_ref_builtin_address_book (registry);
2518 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2524 * e_source_registry_set_default_address_book:
2525 * @registry: an #ESourceRegistry
2526 * @default_source: (allow-none): an address book #ESource, or %NULL
2528 * Sets @default_source as the default address book. If @default_source
2529 * is %NULL, the default address book is reset to the built-in address book.
2530 * This setting will persist across sessions until changed.
2535 e_source_registry_set_default_address_book (ESourceRegistry *registry,
2536 ESource *default_source)
2541 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2543 if (default_source != NULL) {
2544 g_return_if_fail (E_IS_SOURCE (default_source));
2545 uid = e_source_get_uid (default_source);
2547 uid = E_SOURCE_BUILTIN_ADDRESS_BOOK_UID;
2550 key = E_SETTINGS_DEFAULT_ADDRESS_BOOK_KEY;
2551 g_settings_set_string (registry->priv->settings, key, uid);
2553 /* The GSettings::changed signal will trigger a "notify" signal
2554 * from the registry, so no need to call g_object_notify() here. */
2558 * e_source_registry_ref_builtin_calendar:
2559 * @registry: an #ESourceRegistry
2561 * Returns the built-in calendar #ESource.
2563 * This #ESource is always present and makes for a safe fallback.
2565 * The returned #ESource is referenced for thread-safety and must be
2566 * unreferenced with g_object_unref() when finished with it.
2568 * Returns: (transfer full): the built-in calendar #ESource
2573 e_source_registry_ref_builtin_calendar (ESourceRegistry *registry)
2578 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2580 uid = E_SOURCE_BUILTIN_CALENDAR_UID;
2581 source = e_source_registry_ref_source (registry, uid);
2582 g_return_val_if_fail (source != NULL, NULL);
2588 * e_source_registry_ref_default_calendar:
2589 * @registry: an #ESourceRegistry
2591 * Returns the #ESource most recently passed to
2592 * e_source_registry_set_default_calendar() either in this session
2593 * or a previous session, or else falls back to the built-in calendar.
2595 * The returned #ESource is referenced for thread-safety and must be
2596 * unreferenced with g_object_unref() when finished with it.
2598 * Returns: (transfer full): the default calendar #ESource
2603 e_source_registry_ref_default_calendar (ESourceRegistry *registry)
2609 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2611 key = E_SETTINGS_DEFAULT_CALENDAR_KEY;
2612 uid = g_settings_get_string (registry->priv->settings, key);
2613 source = e_source_registry_ref_source (registry, uid);
2616 /* The built-in source is always present. */
2618 source = e_source_registry_ref_builtin_calendar (registry);
2620 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2626 * e_source_registry_set_default_calendar:
2627 * @registry: an #ESourceRegistry
2628 * @default_source: (allow-none): a calendar #ESource, or %NULL
2630 * Sets @default_source as the default calendar. If @default_source
2631 * is %NULL, the default calendar is reset to the built-in calendar.
2632 * This setting will persist across sessions until changed.
2637 e_source_registry_set_default_calendar (ESourceRegistry *registry,
2638 ESource *default_source)
2643 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2645 if (default_source != NULL) {
2646 g_return_if_fail (E_IS_SOURCE (default_source));
2647 uid = e_source_get_uid (default_source);
2649 uid = E_SOURCE_BUILTIN_CALENDAR_UID;
2652 key = E_SETTINGS_DEFAULT_CALENDAR_KEY;
2653 g_settings_set_string (registry->priv->settings, key, uid);
2655 /* The GSettings::changed signal will trigger a "notify" signal
2656 * from the registry, so no need to call g_object_notify() here. */
2660 * e_source_registry_ref_builtin_mail_account:
2661 * @registry: an #ESourceRegistry
2663 * Returns the built-in mail account #ESource.
2665 * This #ESource is always present and makes for a safe fallback.
2667 * The returned #ESource is referenced for thread-safety and must be
2668 * unreferenced with g_object_unref() when finished with it.
2670 * Returns: (transfer full): the built-in mail account #ESource
2675 e_source_registry_ref_builtin_mail_account (ESourceRegistry *registry)
2680 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2682 uid = E_SOURCE_BUILTIN_MAIL_ACCOUNT_UID;
2683 source = e_source_registry_ref_source (registry, uid);
2684 g_return_val_if_fail (source != NULL, NULL);
2690 * e_source_registry_ref_default_mail_account:
2691 * @registry: an #ESourceRegistry
2693 * Returns the #ESource most recently passed to
2694 * e_source_registry_set_default_mail_account() either in this session
2695 * or a previous session, or else falls back to the built-in mail account.
2697 * The returned #ESource is referenced for thread-safety and must be
2698 * unreferenced with g_object_unref() when finished with it.
2700 * Returns: (transfer full): the default mail account #ESource
2705 e_source_registry_ref_default_mail_account (ESourceRegistry *registry)
2711 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2713 key = E_SETTINGS_DEFAULT_MAIL_ACCOUNT_KEY;
2714 uid = g_settings_get_string (registry->priv->settings, key);
2715 source = e_source_registry_ref_source (registry, uid);
2718 /* The built-in source is always present. */
2720 source = e_source_registry_ref_builtin_mail_account (registry);
2722 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2728 * e_source_registry_set_default_mail_account:
2729 * @registry: an #ESourceRegistry
2730 * @default_source: (allow-none): a mail account #ESource, or %NULL
2732 * Sets @default_source as the default mail account. If @default_source
2733 * is %NULL, the default mail account is reset to the built-in mail account.
2734 * This setting will persist across sessions until changed.
2739 e_source_registry_set_default_mail_account (ESourceRegistry *registry,
2740 ESource *default_source)
2745 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2747 if (default_source != NULL) {
2748 g_return_if_fail (E_IS_SOURCE (default_source));
2749 uid = e_source_get_uid (default_source);
2751 uid = E_SOURCE_BUILTIN_MAIL_ACCOUNT_UID;
2754 key = E_SETTINGS_DEFAULT_MAIL_ACCOUNT_KEY;
2755 g_settings_set_string (registry->priv->settings, key, uid);
2757 /* The GSettings::changed signal will trigger a "notify" signal
2758 * from the registry, so no need to call g_object_notify() here. */
2761 /* Helper for e_source_registry_ref_default_mail_identity() */
2763 source_registry_ref_any_mail_identity (ESourceRegistry *registry)
2767 const gchar *extension_name;
2770 /* First fallback: Return the mail identity named
2771 * by the default mail account. */
2773 source = e_source_registry_ref_default_mail_account (registry);
2775 /* This should never be NULL, but just to be safe. */
2776 if (source != NULL) {
2777 ESourceMailAccount *extension;
2779 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
2780 extension = e_source_get_extension (source, extension_name);
2781 uid = e_source_mail_account_dup_identity_uid (extension);
2783 g_object_unref (source);
2788 source = e_source_registry_ref_source (registry, uid);
2795 /* Second fallback: Pick any available mail identity,
2796 * preferring enabled identities. */
2798 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
2799 list = e_source_registry_list_sources (registry, extension_name);
2801 for (link = list; link != NULL; link = g_list_next (link)) {
2802 ESource *candidate = E_SOURCE (link->data);
2804 if (e_source_get_enabled (candidate)) {
2805 source = g_object_ref (candidate);
2810 if (source == NULL && list != NULL)
2811 source = g_object_ref (link->data);
2813 g_list_free_full (list, (GDestroyNotify) g_object_unref);
2819 * e_source_registry_ref_default_mail_identity:
2820 * @registry: an #ESourceRegistry
2822 * Returns the #ESource most recently passed to
2823 * e_source_registry_set_default_mail_identity() either in this session
2824 * or a previous session, or else falls back to the mail identity named
2825 * by the default mail account. If even that fails it returns any mail
2826 * identity from @registry, or %NULL if there are none.
2828 * The returned #ESource is referenced for thread-safety and must be
2829 * unreferenced with g_object_unref() when finished with it.
2831 * Returns: (transfer full): the default mail identity #ESource, or %NULL
2836 e_source_registry_ref_default_mail_identity (ESourceRegistry *registry)
2842 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2844 key = E_SETTINGS_DEFAULT_MAIL_IDENTITY_KEY;
2845 uid = g_settings_get_string (registry->priv->settings, key);
2846 source = e_source_registry_ref_source (registry, uid);
2850 source = source_registry_ref_any_mail_identity (registry);
2856 * e_source_registry_set_default_mail_identity:
2857 * @registry: an #ESourceRegistry
2858 * @default_source: (allow-none): a mail identity #ESource, or %NULL
2860 * Sets @default_source as the default mail identity. If @default_source
2861 * is %NULL, the next request for the default mail identity will use the
2862 * fallbacks described in e_source_registry_get_default_mail_identity().
2867 e_source_registry_set_default_mail_identity (ESourceRegistry *registry,
2868 ESource *default_source)
2873 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2875 if (default_source != NULL) {
2876 g_return_if_fail (E_IS_SOURCE (default_source));
2877 uid = e_source_get_uid (default_source);
2879 uid = ""; /* no built-in mail identity */
2882 key = E_SETTINGS_DEFAULT_MAIL_IDENTITY_KEY;
2883 g_settings_set_string (registry->priv->settings, key, uid);
2885 /* The GSettings::changed signal will trigger a "notify" signal
2886 * from the registry, so no need to call g_object_notify() here. */
2890 * e_source_registry_ref_builtin_memo_list:
2891 * @registry: an #ESourceRegistry
2893 * Returns the built-in memo list #ESource.
2895 * This #ESource is always present and makes for a safe fallback.
2897 * The returned #ESource is referenced for thread-safety and must be
2898 * unreferenced with g_object_unref() when finished with it.
2900 * Returns: (transfer full): the built-in memo list #ESource
2905 e_source_registry_ref_builtin_memo_list (ESourceRegistry *registry)
2910 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2912 uid = E_SOURCE_BUILTIN_MEMO_LIST_UID;
2913 source = e_source_registry_ref_source (registry, uid);
2914 g_return_val_if_fail (source != NULL, NULL);
2920 * e_source_registry_ref_default_memo_list:
2921 * @registry: an #ESourceRegistry
2923 * Returns the #ESource most recently passed to
2924 * e_source_registry_set_default_memo_list() either in this session
2925 * or a previous session, or else falls back to the built-in memo list.
2927 * The returned #ESource is referenced for thread-safety and must be
2928 * unreferenced with g_object_unref() when finished with it.
2930 * Returns: (transfer full): the default memo list #ESource
2935 e_source_registry_ref_default_memo_list (ESourceRegistry *registry)
2941 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2943 key = E_SETTINGS_DEFAULT_MEMO_LIST_KEY;
2944 uid = g_settings_get_string (registry->priv->settings, key);
2945 source = e_source_registry_ref_source (registry, uid);
2948 /* The built-in source is always present. */
2950 source = e_source_registry_ref_builtin_memo_list (registry);
2952 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2958 * e_source_registry_set_default_memo_list:
2959 * @registry: an #ESourceRegistry
2960 * @default_source: (allow-none): a memo list #ESource, or %NULL
2962 * Sets @default_source as the default memo list. If @default_source
2963 * is %NULL, the default memo list is reset to the built-in memo list.
2964 * This setting will persist across sessions until changed.
2969 e_source_registry_set_default_memo_list (ESourceRegistry *registry,
2970 ESource *default_source)
2975 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2977 if (default_source != NULL) {
2978 g_return_if_fail (E_IS_SOURCE (default_source));
2979 uid = e_source_get_uid (default_source);
2981 uid = E_SOURCE_BUILTIN_MEMO_LIST_UID;
2984 key = E_SETTINGS_DEFAULT_MEMO_LIST_KEY;
2985 g_settings_set_string (registry->priv->settings, key, uid);
2987 /* The GSettings::changed signal will trigger a "notify" signal
2988 * from the registry, so no need to call g_object_notify() here. */
2992 * e_source_registry_ref_builtin_task_list:
2993 * @registry: an #ESourceRegistry
2995 * Returns the built-in task list #ESource.
2997 * This #ESource is always present and makes for a safe fallback.
2999 * The returned #ESource is referenced for thread-safety and must be
3000 * unreferenced with g_object_unref() when finished with it.
3002 * Returns: (transfer full): the built-in task list #ESource
3007 e_source_registry_ref_builtin_task_list (ESourceRegistry *registry)
3012 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3014 uid = E_SOURCE_BUILTIN_TASK_LIST_UID;
3015 source = e_source_registry_ref_source (registry, uid);
3016 g_return_val_if_fail (source != NULL, NULL);
3022 * e_source_registry_ref_default_task_list:
3023 * @registry: an #ESourceRegistry
3025 * Returns the #ESource most recently passed to
3026 * e_source_registry_set_default_task_list() either in this session
3027 * or a previous session, or else falls back to the built-in task list.
3029 * The returned #ESource is referenced for thread-safety and must be
3030 * unreferenced with g_object_unref() when finished with it.
3032 * Returns: (transfer full): the default task list #ESource
3037 e_source_registry_ref_default_task_list (ESourceRegistry *registry)
3043 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3045 key = E_SETTINGS_DEFAULT_TASK_LIST_KEY;
3046 uid = g_settings_get_string (registry->priv->settings, key);
3047 source = e_source_registry_ref_source (registry, uid);
3050 /* The built-in source is always present. */
3052 source = e_source_registry_ref_builtin_task_list (registry);
3054 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
3060 * e_source_registry_set_default_task_list:
3061 * @registry: an #ESourceRegistry
3062 * @default_source: (allow-none): a task list #ESource, or %NULL
3064 * Sets @default_source as the default task list. If @default_source
3065 * is %NULL, the default task list is reset to the built-in task list.
3066 * This setting will persist across sessions until changed.
3071 e_source_registry_set_default_task_list (ESourceRegistry *registry,
3072 ESource *default_source)
3077 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
3079 if (default_source != NULL) {
3080 g_return_if_fail (E_IS_SOURCE (default_source));
3081 uid = e_source_get_uid (default_source);
3083 uid = E_SOURCE_BUILTIN_TASK_LIST_UID;
3086 key = E_SETTINGS_DEFAULT_TASK_LIST_KEY;
3087 g_settings_set_string (registry->priv->settings, key, uid);
3089 /* The GSettings::changed signal will trigger a "notify" signal
3090 * from the registry, so no need to call g_object_notify() here. */
3094 * e_source_registry_ref_default_for_extension_name:
3095 * @registry: an #ESourceRegistry
3096 * @extension_name: an extension_name
3098 * This is a convenience function to return a default #ESource based on
3099 * @extension_name. This only works with a subset of extension names.
3101 * If @extension_name is #E_SOURCE_EXTENSION_ADDRESS_BOOK, the function
3102 * returns the current default address book, or else falls back to the
3103 * built-in address book.
3105 * If @extension_name is #E_SOURCE_EXTENSION_CALENDAR, the function returns
3106 * the current default calendar, or else falls back to the built-in calendar.
3108 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_ACCOUNT, the function
3109 * returns the current default mail account, or else falls back to the
3110 * built-in mail account.
3112 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_IDENTITY, the function
3113 * returns the current default mail identity, or else falls back to the
3114 * mail identity named by the current default mail account.
3116 * If @extension_name is #E_SOURCE_EXTENSION_MEMO_LIST, the function returns
3117 * the current default memo list, or else falls back to the built-in memo list.
3119 * If @extension_name is #E_SOURCE_EXTENSION_TASK_LIST, the function returns
3120 * the current default task list, or else falls back to the built-in task list.
3122 * For all other values of @extension_name, the function returns %NULL.
3124 * The returned #ESource is referenced for thread-safety and must be
3125 * unreferenced with g_object_unref() when finished with it.
3127 * Returns: (transfer full): the default #ESource based on @extension_name
3132 e_source_registry_ref_default_for_extension_name (ESourceRegistry *registry,
3133 const gchar *extension_name)
3135 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3136 g_return_val_if_fail (extension_name != NULL, NULL);
3138 if (strcmp (extension_name, E_SOURCE_EXTENSION_ADDRESS_BOOK) == 0)
3139 return e_source_registry_ref_default_address_book (registry);
3141 if (strcmp (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0)
3142 return e_source_registry_ref_default_calendar (registry);
3144 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_ACCOUNT) == 0)
3145 return e_source_registry_ref_default_mail_account (registry);
3147 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_IDENTITY) == 0)
3148 return e_source_registry_ref_default_mail_identity (registry);
3150 if (strcmp (extension_name, E_SOURCE_EXTENSION_MEMO_LIST) == 0)
3151 return e_source_registry_ref_default_memo_list (registry);
3153 if (strcmp (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0)
3154 return e_source_registry_ref_default_task_list (registry);
3160 * e_source_registry_set_default_for_extension_name:
3161 * @registry: an #ESourceRegistry
3162 * @extension_name: an extension name
3163 * @default_source: (allow-none): an #ESource, or %NULL
3165 * This is a convenience function to set a default #ESource based on
3166 * @extension_name. This only works with a subset of extension names.
3168 * If @extension_name is #E_SOURCE_EXTENSION_ADDRESS_BOOK, the function
3169 * sets @default_source as the default address book. If @default_source
3170 * is %NULL, the default address book is reset to the built-in address book.
3172 * If @extension_name is #E_SOURCE_EXTENSION_CALENDAR, the function sets
3173 * @default_source as the default calendar. If @default_source is %NULL,
3174 * the default calendar is reset to the built-in calendar.
3176 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_ACCOUNT, the function
3177 * sets @default_source as the default mail account. If @default_source
3178 * is %NULL, the default mail account is reset to the built-in mail account.
3180 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_IDENTITY, the function
3181 * sets @default_source as the default mail identity. If @default_source
3182 * is %NULL, the next request for the default mail identity will return
3183 * the mail identity named by the default mail account.
3185 * If @extension_name is #E_SOURCE_EXTENSION_MEMO_LIST, the function sets
3186 * @default_source as the default memo list. If @default_source is %NULL,
3187 * the default memo list is reset to the built-in memo list.
3189 * If @extension_name is #E_SOURCE_EXTENSION_TASK_LIST, the function sets
3190 * @default_source as the default task list. If @default_source is %NULL,
3191 * the default task list is reset to the built-in task list.
3193 * For all other values of @extension_name, the function does nothing.
3198 e_source_registry_set_default_for_extension_name (ESourceRegistry *registry,
3199 const gchar *extension_name,
3200 ESource *default_source)
3202 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
3203 g_return_if_fail (extension_name != NULL);
3205 if (strcmp (extension_name, E_SOURCE_EXTENSION_ADDRESS_BOOK) == 0)
3206 e_source_registry_set_default_address_book (
3207 registry, default_source);
3209 if (strcmp (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0)
3210 e_source_registry_set_default_calendar (
3211 registry, default_source);
3213 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_ACCOUNT) == 0)
3214 e_source_registry_set_default_mail_account (
3215 registry, default_source);
3217 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_IDENTITY) == 0)
3218 e_source_registry_set_default_mail_identity (
3219 registry, default_source);
3221 if (strcmp (extension_name, E_SOURCE_EXTENSION_MEMO_LIST) == 0)
3222 e_source_registry_set_default_memo_list (
3223 registry, default_source);
3225 if (strcmp (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0)
3226 e_source_registry_set_default_task_list (
3227 registry, default_source);