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_unref_source (ESource *source)
507 g_signal_handlers_disconnect_matched (
508 source, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
509 source_registry_source_changed_cb, NULL);
511 g_signal_handlers_disconnect_matched (
512 source, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
513 source_registry_source_notify_enabled_cb, NULL);
515 g_object_unref (source);
519 source_registry_add_source (ESourceRegistry *registry,
524 uid = e_source_get_uid (source);
525 g_return_if_fail (uid != NULL);
527 g_mutex_lock (registry->priv->sources_lock);
529 /* Check if we already have this source in the registry. */
530 if (g_hash_table_lookup (registry->priv->sources, uid) != NULL) {
531 g_mutex_unlock (registry->priv->sources_lock);
537 G_CALLBACK (source_registry_source_changed_cb),
541 source, "notify::enabled",
542 G_CALLBACK (source_registry_source_notify_enabled_cb),
545 g_mutex_unlock (registry->priv->sources_lock);
547 source_registry_sources_insert (registry, source);
549 g_signal_emit (registry, signals[SOURCE_ADDED], 0, source);
553 source_registry_remove_source (ESourceRegistry *registry,
556 g_object_ref (source);
558 if (source_registry_sources_remove (registry, source))
559 g_signal_emit (registry, signals[SOURCE_REMOVED], 0, source);
561 g_object_unref (source);
565 source_registry_object_added_idle_cb (gpointer user_data)
567 SourceClosure *closure = user_data;
569 source_registry_add_source (closure->registry, closure->source);
575 source_registry_object_added_cb (GDBusObjectManager *object_manager,
576 GDBusObject *dbus_object,
577 ESourceRegistry *registry)
579 SourceClosure *closure;
580 GMainContext *main_context;
581 GSource *idle_source;
583 const gchar *object_path;
584 GError *error = NULL;
586 g_return_if_fail (E_DBUS_IS_OBJECT (dbus_object));
588 /* We don't want the ESource emitting "changed" signals from
589 * the manager thread, so we pass it the same main context the
590 * registry uses for scheduling signal emissions. */
591 main_context = registry->priv->main_context;
592 source = e_source_new (dbus_object, main_context, &error);
593 object_path = g_dbus_object_get_object_path (dbus_object);
595 /* The likelihood of an error here is slim, so it's
596 * sufficient to just print a warning if one occurs. */
598 g_warn_if_fail (source == NULL);
600 "ESourceRegistry: Failed to create a "
601 "data source object for path '%s': %s",
602 object_path, error->message);
603 g_error_free (error);
607 g_return_if_fail (E_IS_SOURCE (source));
609 /* Add the ESource to the object path table immediately. */
610 source_registry_object_path_table_insert (
611 registry, object_path, source);
613 /* Schedule a callback on the ESourceRegistry's GMainContext. */
615 closure = g_slice_new0 (SourceClosure);
616 closure->registry = g_object_ref (registry);
617 closure->source = g_object_ref (source);
619 idle_source = g_idle_source_new ();
620 g_source_set_callback (
622 source_registry_object_added_idle_cb,
623 closure, (GDestroyNotify) source_closure_free);
624 g_source_attach (idle_source, registry->priv->main_context);
625 g_source_unref (idle_source);
627 g_object_unref (source);
631 source_registry_object_removed_idle_cb (gpointer user_data)
633 SourceClosure *closure = user_data;
635 source_registry_remove_source (closure->registry, closure->source);
641 source_registry_object_removed_cb (GDBusObjectManager *manager,
642 GDBusObject *dbus_object,
643 ESourceRegistry *registry)
645 SourceClosure *closure;
646 GSource *idle_source;
648 const gchar *object_path;
650 /* Find the corresponding ESource in the object path table.
651 * Note that the lookup returns a new ESource reference. */
652 object_path = g_dbus_object_get_object_path (dbus_object);
653 source = source_registry_object_path_table_lookup (
654 registry, object_path);
655 g_return_if_fail (E_IS_SOURCE (source));
657 /* Remove the ESource from the object path table immediately. */
658 source_registry_object_path_table_remove (registry, object_path);
660 /* Schedule a callback on the ESourceRegistry's GMainContext. */
662 closure = g_slice_new0 (SourceClosure);
663 closure->registry = g_object_ref (registry);
664 closure->source = g_object_ref (source);
666 idle_source = g_idle_source_new ();
667 g_source_set_callback (
669 source_registry_object_removed_idle_cb,
670 closure, (GDestroyNotify) source_closure_free);
671 g_source_attach (idle_source, registry->priv->main_context);
672 g_source_unref (idle_source);
674 g_object_unref (source);
678 source_registry_object_manager_running (gpointer data)
680 ThreadClosure *closure = data;
682 g_mutex_lock (closure->main_loop_mutex);
683 g_cond_broadcast (closure->main_loop_cond);
684 g_mutex_unlock (closure->main_loop_mutex);
690 source_registry_object_manager_thread (gpointer data)
692 GDBusObjectManager *object_manager;
693 ThreadClosure *closure = data;
694 GSource *idle_source;
696 gulong object_added_id;
697 gulong object_removed_id;
698 GError *error = NULL;
700 /* GDBusObjectManagerClient grabs the thread-default GMainContext
701 * at creation time and only emits signals from that GMainContext.
702 * Running it in a separate thread prevents its signal emissions
703 * from being inhibited by someone overriding the thread-default
706 /* This becomes the GMainContext that GDBusObjectManagerClient
707 * will emit signals from. Make it the thread-default context
708 * for this thread before creating the client. */
709 g_main_context_push_thread_default (closure->main_context);
711 object_manager = e_dbus_object_manager_client_new_for_bus_sync (
713 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
714 SOURCES_DBUS_SERVICE_NAME,
718 /* If this fails there's really no point in continuing
719 * since we rely on the object manager to populate the
720 * registry. Abort the process with a fatal error. */
722 g_error ("%s", error->message);
723 g_assert_not_reached ();
726 /* Give the registry a handle to the object manager. */
727 closure->registry->priv->dbus_object_manager =
728 g_object_ref (object_manager);
730 /* Now populate the registry with an initial set of ESources. */
732 list = g_dbus_object_manager_get_objects (object_manager);
734 for (link = list; link != NULL; link = g_list_next (link))
735 source_registry_object_added_cb (
737 G_DBUS_OBJECT (link->data),
740 g_list_free_full (list, (GDestroyNotify) g_object_unref);
742 /* Schedule a one-time idle callback to broadcast through a
743 * condition variable that our main loop is up and running. */
745 idle_source = g_idle_source_new ();
746 g_source_set_callback (
748 source_registry_object_manager_running,
749 closure, (GDestroyNotify) NULL);
750 g_source_attach (idle_source, closure->main_context);
751 g_source_unref (idle_source);
753 /* Listen for D-Bus object additions and removals. */
755 object_added_id = g_signal_connect (
756 object_manager, "object-added",
757 G_CALLBACK (source_registry_object_added_cb),
760 object_removed_id = g_signal_connect (
761 object_manager, "object-removed",
762 G_CALLBACK (source_registry_object_removed_cb),
765 /* Now we mostly idle here for the rest of the session. */
767 g_main_loop_run (closure->main_loop);
769 /* Clean up and exit. */
771 g_signal_handler_disconnect (object_manager, object_added_id);
772 g_signal_handler_disconnect (object_manager, object_removed_id);
774 g_object_unref (object_manager);
776 g_main_context_pop_thread_default (closure->main_context);
782 source_registry_set_property (GObject *object,
787 switch (property_id) {
788 case PROP_DEFAULT_ADDRESS_BOOK:
789 e_source_registry_set_default_address_book (
790 E_SOURCE_REGISTRY (object),
791 g_value_get_object (value));
794 case PROP_DEFAULT_CALENDAR:
795 e_source_registry_set_default_calendar (
796 E_SOURCE_REGISTRY (object),
797 g_value_get_object (value));
800 case PROP_DEFAULT_MAIL_ACCOUNT:
801 e_source_registry_set_default_mail_account (
802 E_SOURCE_REGISTRY (object),
803 g_value_get_object (value));
806 case PROP_DEFAULT_MAIL_IDENTITY:
807 e_source_registry_set_default_mail_identity (
808 E_SOURCE_REGISTRY (object),
809 g_value_get_object (value));
812 case PROP_DEFAULT_MEMO_LIST:
813 e_source_registry_set_default_memo_list (
814 E_SOURCE_REGISTRY (object),
815 g_value_get_object (value));
818 case PROP_DEFAULT_TASK_LIST:
819 e_source_registry_set_default_task_list (
820 E_SOURCE_REGISTRY (object),
821 g_value_get_object (value));
825 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
829 source_registry_get_property (GObject *object,
834 switch (property_id) {
835 case PROP_DEFAULT_ADDRESS_BOOK:
836 g_value_take_object (
838 e_source_registry_ref_default_address_book (
839 E_SOURCE_REGISTRY (object)));
842 case PROP_DEFAULT_CALENDAR:
843 g_value_take_object (
845 e_source_registry_ref_default_calendar (
846 E_SOURCE_REGISTRY (object)));
849 case PROP_DEFAULT_MAIL_ACCOUNT:
850 g_value_take_object (
852 e_source_registry_ref_default_mail_account (
853 E_SOURCE_REGISTRY (object)));
856 case PROP_DEFAULT_MAIL_IDENTITY:
857 g_value_take_object (
859 e_source_registry_ref_default_mail_identity (
860 E_SOURCE_REGISTRY (object)));
863 case PROP_DEFAULT_MEMO_LIST:
864 g_value_take_object (
866 e_source_registry_ref_default_memo_list (
867 E_SOURCE_REGISTRY (object)));
870 case PROP_DEFAULT_TASK_LIST:
871 g_value_take_object (
873 e_source_registry_ref_default_task_list (
874 E_SOURCE_REGISTRY (object)));
878 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
882 source_registry_dispose (GObject *object)
884 ESourceRegistryPrivate *priv;
886 priv = E_SOURCE_REGISTRY_GET_PRIVATE (object);
888 /* Terminate the manager thread first. */
889 if (priv->manager_thread != NULL) {
890 g_main_loop_quit (priv->thread_closure->main_loop);
891 g_thread_join (priv->manager_thread);
892 thread_closure_free (priv->thread_closure);
893 priv->manager_thread = NULL;
894 priv->thread_closure = NULL;
897 if (priv->main_context != NULL) {
898 g_main_context_unref (priv->main_context);
899 priv->main_context = NULL;
902 if (priv->dbus_object_manager != NULL) {
903 g_object_unref (priv->dbus_object_manager);
904 priv->dbus_object_manager = NULL;
907 if (priv->dbus_source_manager != NULL) {
908 g_object_unref (priv->dbus_source_manager);
909 priv->dbus_source_manager = NULL;
912 g_hash_table_remove_all (priv->object_path_table);
914 g_hash_table_remove_all (priv->sources);
916 if (priv->settings != NULL) {
917 g_object_unref (priv->settings);
918 priv->settings = NULL;
921 /* Chain up to parent's finalize() method. */
922 G_OBJECT_CLASS (e_source_registry_parent_class)->dispose (object);
926 source_registry_finalize (GObject *object)
928 ESourceRegistryPrivate *priv;
930 priv = E_SOURCE_REGISTRY_GET_PRIVATE (object);
932 g_hash_table_destroy (priv->object_path_table);
933 g_mutex_free (priv->object_path_table_lock);
935 g_hash_table_destroy (priv->sources);
936 g_mutex_free (priv->sources_lock);
938 /* Chain up to parent's finalize() method. */
939 G_OBJECT_CLASS (e_source_registry_parent_class)->finalize (object);
943 source_registry_initable_init (GInitable *initable,
944 GCancellable *cancellable,
947 ESourceRegistry *registry;
948 ThreadClosure *closure;
950 registry = E_SOURCE_REGISTRY (initable);
952 closure = g_slice_new0 (ThreadClosure);
953 closure->registry = registry; /* do not reference */
954 closure->main_context = g_main_context_new ();
955 /* It's important to pass 'is_running=FALSE' here because
956 * we wait for the main loop to start running as a way of
957 * synchronizing with the manager thread. */
958 closure->main_loop = g_main_loop_new (closure->main_context, FALSE);
959 closure->main_loop_cond = g_cond_new ();
960 closure->main_loop_mutex = g_mutex_new ();
962 registry->priv->thread_closure = closure;
964 registry->priv->manager_thread = g_thread_create (
965 source_registry_object_manager_thread,
966 closure, TRUE /* joinable */, error);
968 if (registry->priv->manager_thread == NULL)
971 /* Wait for notification that the manager
972 * thread's main loop has been started. */
973 g_mutex_lock (closure->main_loop_mutex);
974 while (!g_main_loop_is_running (closure->main_loop))
976 closure->main_loop_cond,
977 closure->main_loop_mutex);
978 g_mutex_unlock (closure->main_loop_mutex);
980 /* We should now have a GDBusObjectManagerClient available. */
981 g_return_val_if_fail (
982 G_IS_DBUS_OBJECT_MANAGER_CLIENT (
983 registry->priv->dbus_object_manager), FALSE);
985 /* The manager thread will have queued up a bunch of idle
986 * sources on our GMainContext to populate the registry.
987 * Iterate our GMainContext until they get dispatched. */
988 while (g_hash_table_size (registry->priv->sources) == 0)
989 g_main_context_iteration (registry->priv->main_context, TRUE);
991 /* The EDBusSourceManagerProxy is just another D-Bus interface
992 * that resides at the same object path. It's unrelated to the
993 * GDBusObjectManagerClient and doesn't need its own thread. */
994 registry->priv->dbus_source_manager =
995 e_dbus_source_manager_proxy_new_for_bus_sync (
997 G_DBUS_PROXY_FLAGS_NONE,
998 SOURCES_DBUS_SERVICE_NAME,
1000 cancellable, error);
1002 if (registry->priv->dbus_source_manager == NULL)
1009 e_source_registry_class_init (ESourceRegistryClass *class)
1011 GObjectClass *object_class;
1013 g_type_class_add_private (class, sizeof (ESourceRegistryPrivate));
1015 object_class = G_OBJECT_CLASS (class);
1016 object_class->set_property = source_registry_set_property;
1017 object_class->get_property = source_registry_get_property;
1018 object_class->dispose = source_registry_dispose;
1019 object_class->finalize = source_registry_finalize;
1021 /* The property names correspond to the key names in the
1022 * "org.gnome.Evolution.DefaultSources" GSettings schema. */
1025 * ESourceRegistry:default-address-book:
1027 * The default address book #ESource.
1029 g_object_class_install_property (
1031 PROP_DEFAULT_ADDRESS_BOOK,
1032 g_param_spec_object (
1033 "default-address-book",
1034 "Default Address Book",
1035 "The default address book ESource",
1038 G_PARAM_STATIC_STRINGS));
1041 * ESourceRegistry:default-calendar:
1043 * The default calendar #ESource.
1045 g_object_class_install_property (
1047 PROP_DEFAULT_CALENDAR,
1048 g_param_spec_object (
1051 "The default calendar ESource",
1054 G_PARAM_STATIC_STRINGS));
1057 * ESourceRegistry:default-mail-account:
1059 * The default mail account #ESource.
1061 g_object_class_install_property (
1063 PROP_DEFAULT_MAIL_ACCOUNT,
1064 g_param_spec_object (
1065 "default-mail-account",
1066 "Default Mail Account",
1067 "The default mail account ESource",
1070 G_PARAM_STATIC_STRINGS));
1073 * ESourceRegistry:default-mail-identity:
1075 * The default mail identity #ESource.
1077 g_object_class_install_property (
1079 PROP_DEFAULT_MAIL_IDENTITY,
1080 g_param_spec_object (
1081 "default-mail-identity",
1082 "Default Mail Identity",
1083 "The default mail identity ESource",
1086 G_PARAM_STATIC_STRINGS));
1089 * ESourceRegistry:default-memo-list:
1091 * The default memo list #ESource.
1093 g_object_class_install_property (
1095 PROP_DEFAULT_MEMO_LIST,
1096 g_param_spec_object (
1097 "default-memo-list",
1098 "Default Memo List",
1099 "The default memo list ESource",
1102 G_PARAM_STATIC_STRINGS));
1105 * ESourceRegistry:default-task-list:
1107 * The default task list #ESource.
1109 g_object_class_install_property (
1111 PROP_DEFAULT_TASK_LIST,
1112 g_param_spec_object (
1113 "default-task-list",
1114 "Default Task List",
1115 "The default task list ESource",
1118 G_PARAM_STATIC_STRINGS));
1121 * ESourceRegistry::source-added:
1122 * @registry: the #ESourceRegistry which emitted the signal
1123 * @source: the newly-added #ESource
1125 * Emitted when an #ESource is added to @registry.
1127 signals[SOURCE_ADDED] = g_signal_new (
1129 G_OBJECT_CLASS_TYPE (object_class),
1131 G_STRUCT_OFFSET (ESourceRegistryClass, source_added),
1133 g_cclosure_marshal_VOID__OBJECT,
1138 * ESourceRegistry::source-changed:
1139 * @registry: the #ESourceRegistry which emitted the signal
1140 * @source: the #ESource that changed
1142 * Emitted when an #ESource registered with @registry emits
1143 * its #ESource::changed signal.
1145 signals[SOURCE_CHANGED] = g_signal_new (
1147 G_OBJECT_CLASS_TYPE (object_class),
1149 G_STRUCT_OFFSET (ESourceRegistryClass, source_changed),
1151 g_cclosure_marshal_VOID__OBJECT,
1156 * ESourceRegistry::source-removed:
1157 * @registry: the #ESourceRegistry which emitted the signal
1158 * @source: the #ESource that got removed
1160 * Emitted when an #ESource is removed from @registry.
1162 signals[SOURCE_REMOVED] = g_signal_new (
1164 G_OBJECT_CLASS_TYPE (object_class),
1166 G_STRUCT_OFFSET (ESourceRegistryClass, source_removed),
1168 g_cclosure_marshal_VOID__OBJECT,
1173 * ESourceRegistry::source-enabled:
1174 * @registry: the #ESourceRegistry which emitted the signal
1175 * @source: the #ESource that got enabled
1177 * Emitted when an #ESource #ESource:enabled property becomes %TRUE.
1179 signals[SOURCE_ENABLED] = g_signal_new (
1181 G_OBJECT_CLASS_TYPE (object_class),
1183 G_STRUCT_OFFSET (ESourceRegistryClass, source_enabled),
1185 g_cclosure_marshal_VOID__OBJECT,
1190 * ESourceRegistry::source-disabled:
1191 * @registry: the #ESourceRegistry which emitted the signal
1192 * @source: the #ESource that got disabled
1194 * Emitted when an #ESource #ESource:enabled property becomes %FALSE.
1196 signals[SOURCE_DISABLED] = g_signal_new (
1198 G_OBJECT_CLASS_TYPE (object_class),
1200 G_STRUCT_OFFSET (ESourceRegistryClass, source_disabled),
1202 g_cclosure_marshal_VOID__OBJECT,
1208 e_source_registry_initable_init (GInitableIface *interface)
1210 interface->init = source_registry_initable_init;
1214 e_source_registry_init (ESourceRegistry *registry)
1216 registry->priv = E_SOURCE_REGISTRY_GET_PRIVATE (registry);
1218 /* This is so the object manager thread can schedule signal
1219 * emissions on the thread-default context for this thread. */
1220 registry->priv->main_context = g_main_context_ref_thread_default ();
1222 /* D-Bus object path -> ESource */
1223 registry->priv->object_path_table =
1224 g_hash_table_new_full (
1225 (GHashFunc) g_str_hash,
1226 (GEqualFunc) g_str_equal,
1227 (GDestroyNotify) g_free,
1228 (GDestroyNotify) g_object_unref);
1230 registry->priv->object_path_table_lock = g_mutex_new ();
1232 /* UID string -> ESource */
1233 registry->priv->sources = g_hash_table_new_full (
1234 (GHashFunc) g_str_hash,
1235 (GEqualFunc) g_str_equal,
1236 (GDestroyNotify) g_free,
1237 (GDestroyNotify) source_registry_unref_source);
1239 registry->priv->sources_lock = g_mutex_new ();
1241 registry->priv->settings = g_settings_new (GSETTINGS_SCHEMA);
1244 registry->priv->settings, "changed",
1245 G_CALLBACK (source_registry_settings_changed_cb), registry);
1249 * e_source_registry_new_sync:
1250 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1251 * @error: return location for a #GError, or %NULL
1253 * Creates a new #ESourceRegistry front-end for the registry D-Bus service.
1254 * If an error occurs in connecting to the D-Bus service, the function sets
1255 * @error and returns %NULL.
1257 * Returns: a new #ESourceRegistry, or %NULL
1262 e_source_registry_new_sync (GCancellable *cancellable,
1265 return g_initable_new (
1266 E_TYPE_SOURCE_REGISTRY,
1267 cancellable, error, NULL);
1271 * e_source_registry_new:
1272 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1273 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1275 * @user_data: (closure): data to pass to the callback function
1277 * Asynchronously creates a new #ESourceRegistry front-end for the registry
1280 * When the operation is finished, @callback will be called. You can then
1281 * call e_source_registry_new_finish() to get the result of the operation.
1286 e_source_registry_new (GCancellable *cancellable,
1287 GAsyncReadyCallback callback,
1290 g_async_initable_new_async (
1291 E_TYPE_SOURCE_REGISTRY,
1292 G_PRIORITY_DEFAULT, cancellable,
1293 callback, user_data, NULL);
1297 * e_source_registry_new_finish:
1298 * @result: a #GAsyncResult
1299 * @error: return location for a #GError, or %NULL
1301 * Finishes the operation started with e_source_registry_new_finish().
1302 * If an error occurs in connecting to the D-Bus service, the function
1303 * sets @error and returns %NULL.
1305 * Returns: a new #ESourceRegistry, or %NULL
1310 e_source_registry_new_finish (GAsyncResult *result,
1313 GObject *source_object;
1316 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
1318 source_object = g_async_result_get_source_object (result);
1319 g_return_val_if_fail (source_object != NULL, NULL);
1321 object = g_async_initable_new_finish (
1322 G_ASYNC_INITABLE (source_object), result, error);
1324 g_object_unref (source_object);
1326 return (object != NULL) ? E_SOURCE_REGISTRY (object) : NULL;
1329 /* Helper for e_source_registry_authenticate() */
1331 source_registry_authenticate_thread (GSimpleAsyncResult *simple,
1333 GCancellable *cancellable)
1335 AsyncContext *async_context;
1336 GError *error = NULL;
1338 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1340 e_source_registry_authenticate_sync (
1341 E_SOURCE_REGISTRY (object),
1342 async_context->source,
1343 async_context->auth,
1344 cancellable, &error);
1347 g_simple_async_result_take_error (simple, error);
1350 /* Helper for e_source_registry_authenticate_sync() */
1352 source_registry_authenticate_respond_cb (AuthContext *auth_context)
1354 ESourceAuthenticationResult auth_result;
1355 GError *non_fatal_error = NULL;
1357 g_return_val_if_fail (auth_context->authenticating, FALSE);
1359 auth_result = auth_context->auth_result;
1361 /* Allow the next authentication attempt to proceed. */
1362 auth_context->authenticating = FALSE;
1364 /* Send the server a status update based on the authentication
1365 * result. Note, we don't really care if the D-Bus message gets
1366 * through to the server at this point. If it doesn't, the auth
1367 * session will either time out on its own or the authentication
1368 * dialog will eventually be dismissed by the user. */
1370 /* If an error occurred while attempting to authenticate,
1371 * tell the server to cancel the authentication session. */
1372 if (auth_result == E_SOURCE_AUTHENTICATION_ERROR) {
1373 e_dbus_authenticator_call_cancel_sync (
1374 auth_context->dbus_auth,
1375 auth_context->cancellable,
1377 g_main_loop_quit (auth_context->main_loop);
1378 auth_context->success = FALSE;
1380 /* If the password was accepted, let the server know so it
1381 * can close any authentication dialogs and save the user
1382 * provided password to the keyring. */
1383 } else if (auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
1384 e_dbus_authenticator_call_accepted_sync (
1385 auth_context->dbus_auth,
1386 auth_context->cancellable,
1388 g_main_loop_quit (auth_context->main_loop);
1389 auth_context->success = TRUE;
1391 /* If the password was rejected, let the server know so it can
1392 * indicate failure and request a different password, and then
1393 * wait for the next "response" signal. */
1395 e_dbus_authenticator_call_rejected_sync (
1396 auth_context->dbus_auth,
1397 auth_context->cancellable,
1401 /* Leave breadcrumbs if something went wrong,
1402 * but don't fail the whole operation over it. */
1403 if (non_fatal_error != NULL) {
1404 g_warning ("%s: %s", G_STRFUNC, non_fatal_error->message);
1405 g_error_free (non_fatal_error);
1411 /* Helper for e_source_registry_authenticate_sync() */
1413 source_registry_authenticate_authenticate_cb (EDBusAuthenticator *dbus_auth,
1414 const gchar *encrypted_secret,
1415 AuthContext *auth_context)
1417 GSource *idle_source;
1418 GMainContext *main_context;
1420 gboolean valid_secret;
1422 /* We should only get one secret at a time. */
1423 g_return_if_fail (!auth_context->authenticating);
1425 valid_secret = gcr_secret_exchange_receive (
1426 auth_context->secret_exchange, encrypted_secret);
1427 g_return_if_fail (valid_secret);
1429 auth_context->authenticating = TRUE;
1431 /* This avoids revealing the password in a stack trace. */
1432 password = g_string_new (
1433 gcr_secret_exchange_get_secret (
1434 auth_context->secret_exchange, NULL));
1436 /* Try authenticating with the given password. We have to
1437 * call this synchronously because some authenticators use
1438 * mutexes to serialize I/O operations and are not prepared
1439 * to make authentication attempts from a different thread.
1441 * Unfortunately this means we won't notice server-side
1442 * dismissals while the main loop is blocked. We respond
1443 * to the server from a low-priority idle callback so that
1444 * any pending "dismissed" signals get handled first. */
1446 auth_context->auth_result =
1447 e_source_authenticator_try_password_sync (
1448 auth_context->auth, password,
1449 auth_context->cancellable,
1450 auth_context->error);
1452 idle_source = g_idle_source_new ();
1453 main_context = g_main_context_get_thread_default ();
1454 g_source_set_callback (
1455 idle_source, (GSourceFunc)
1456 source_registry_authenticate_respond_cb,
1457 auth_context, NULL);
1458 g_source_attach (idle_source, main_context);
1459 g_source_unref (idle_source);
1461 g_string_free (password, TRUE);
1464 /* Helper for e_source_registry_authenticate_sync() */
1466 source_registry_authenticate_dismissed_cb (EDBusAuthenticator *dbus_auth,
1467 AuthContext *auth_context)
1469 /* Be careful not to overwrite an existing error in case this
1470 * is called after e_source_authenticator_try_password_sync()
1471 * but prior to the idle callback. */
1472 if (auth_context->auth_result != E_SOURCE_AUTHENTICATION_ERROR) {
1473 /* XXX Use a separate error code for dismissals? */
1474 g_set_error_literal (
1475 auth_context->error,
1476 G_IO_ERROR, G_IO_ERROR_CANCELLED,
1477 _("The user declined to authenticate"));
1478 auth_context->auth_result = E_SOURCE_AUTHENTICATION_ERROR;
1481 g_main_loop_quit (auth_context->main_loop);
1482 auth_context->success = FALSE;
1485 /* Helper for e_source_registry_authenticate_sync() */
1487 source_registry_call_authenticate_for_source (ESourceRegistry *registry,
1488 ESourceAuthenticator *auth,
1490 gchar **out_object_path,
1491 GCancellable *cancellable,
1494 ESource *collection;
1496 gchar *prompt_title = NULL;
1497 gchar *prompt_message = NULL;
1498 gchar *prompt_description = NULL;
1501 /* If the source is a member of a collection, we want to store
1502 * the password under the UID of the "collection" source so it
1503 * will apply to the entire collection.
1505 * XXX This assumes all sources in a collection share a single
1506 * password. If that turns out not to be true in all cases
1507 * we could maybe add a "SharedPassword: true/false" key to
1508 * [Collection] and apply it here.
1510 collection = e_source_registry_find_extension (
1511 registry, source, E_SOURCE_EXTENSION_COLLECTION);
1512 if (collection != NULL)
1513 source = collection;
1515 g_object_ref (source);
1517 uid = e_source_get_uid (source);
1519 e_source_authenticator_get_prompt_strings (
1523 &prompt_description);
1525 success = e_dbus_source_manager_call_authenticate_sync (
1526 registry->priv->dbus_source_manager, uid,
1527 prompt_title, prompt_message, prompt_description,
1528 out_object_path, cancellable, error);
1530 g_free (prompt_title);
1531 g_free (prompt_message);
1532 g_free (prompt_description);
1534 g_object_unref (source);
1540 * e_source_registry_authenticate_sync:
1541 * @registry: an #ESourceRegistry
1542 * @source: an #ESource
1543 * @auth: an #ESourceAuthenticator
1544 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1545 * @error: return location for a #GError, or %NULL
1547 * Authenticates @source, using @auth to handle the authentication
1548 * attempts. The operation loops until authentication is successful or
1549 * the user aborts further authentication attempts. If an error occurs,
1550 * the function will set @error and return %FALSE.
1552 * Note that @source need not have a #GDBusObject, which means this
1553 * function can test authentication on a scratch #ESource.
1555 * Only backend implementations and data source editors should call this
1556 * function. The intent is for basic client applications to not have to
1557 * deal with authentication at all.
1559 * Returns: %TRUE on success, %FALSE on failure
1564 e_source_registry_authenticate_sync (ESourceRegistry *registry,
1566 ESourceAuthenticator *auth,
1567 GCancellable *cancellable,
1570 AuthContext *auth_context;
1571 GMainContext *main_context;
1572 EDBusAuthenticator *dbus_auth;
1573 gchar *encryption_key;
1574 gchar *object_path = NULL;
1577 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1578 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
1579 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth), FALSE);
1581 /* This extracts authentication prompt details for the ESource
1582 * before initiating an authentication session with the server,
1583 * so split it out of the main algorithm for clarity's sake. */
1584 success = source_registry_call_authenticate_for_source (
1585 registry, auth, source, &object_path, cancellable, error);
1588 g_warn_if_fail (object_path == NULL);
1592 g_return_val_if_fail (object_path != NULL, FALSE);
1594 main_context = g_main_context_new ();
1595 g_main_context_push_thread_default (main_context);
1597 dbus_auth = e_dbus_authenticator_proxy_new_for_bus_sync (
1599 G_DBUS_PROXY_FLAGS_NONE,
1600 SOURCES_DBUS_SERVICE_NAME,
1601 object_path, cancellable, error);
1603 g_free (object_path);
1605 if (dbus_auth == NULL) {
1610 auth_context = g_slice_new0 (AuthContext);
1611 auth_context->auth = g_object_ref (auth);
1612 auth_context->dbus_auth = dbus_auth; /* takes ownership */
1613 auth_context->main_loop = g_main_loop_new (main_context, FALSE);
1614 auth_context->error = error;
1616 /* This just needs to be something other than
1617 * E_SOURCE_AUTHENTICATION_ERROR so we don't trip
1618 * up source_registry_authenticate_dismissed_cb(). */
1619 auth_context->auth_result = E_SOURCE_AUTHENTICATION_REJECTED;
1621 if (G_IS_CANCELLABLE (cancellable))
1622 auth_context->cancellable = g_object_ref (cancellable);
1624 auth_context->secret_exchange =
1625 gcr_secret_exchange_new (GCR_SECRET_EXCHANGE_PROTOCOL_1);
1628 dbus_auth, "authenticate",
1629 G_CALLBACK (source_registry_authenticate_authenticate_cb),
1633 dbus_auth, "dismissed",
1634 G_CALLBACK (source_registry_authenticate_dismissed_cb),
1637 encryption_key = gcr_secret_exchange_begin (
1638 auth_context->secret_exchange);
1640 /* Signal the D-Bus server that we're ready to begin the
1641 * authentication session. This must happen AFTER we've
1642 * connected to the response signal since the server may
1643 * already have a response ready and waiting for us. */
1644 success = e_dbus_authenticator_call_ready_sync (
1645 dbus_auth, encryption_key, cancellable, error);
1647 g_free (encryption_key);
1650 g_main_loop_run (auth_context->main_loop);
1651 success = auth_context->success;
1654 auth_context_free (auth_context);
1657 g_main_context_pop_thread_default (main_context);
1658 g_main_context_unref (main_context);
1664 * e_source_registry_authenticate:
1665 * @registry: an #ESourceRegistry
1666 * @source: an #ESource
1667 * @auth: an #ESourceAuthenticator
1668 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1669 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1671 * @user_data: (closure): data to pass to the callback function
1673 * Asynchronously authenticates @source, using @auth to handle the
1674 * authentication attempts. The operation loops until authentication
1675 * is successful or the user aborts further authentication attempts.
1677 * Note that @source need not have a #GDBusObject, which means this
1678 * function can test authentication on a scratch #ESource.
1680 * When the operation is finished, @callback will be called. You can then
1681 * call e_source_registry_authenticate_finish() to get the result of the
1684 * Only backend implementations and data source editors should call this
1685 * function. The intent is for basic client applications to not have to
1686 * deal with authentication at all.
1691 e_source_registry_authenticate (ESourceRegistry *registry,
1693 ESourceAuthenticator *auth,
1694 GCancellable *cancellable,
1695 GAsyncReadyCallback callback,
1698 GSimpleAsyncResult *simple;
1699 AsyncContext *async_context;
1701 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
1702 g_return_if_fail (E_IS_SOURCE (source));
1703 g_return_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth));
1705 async_context = g_slice_new0 (AsyncContext);
1706 async_context->source = g_object_ref (source);
1707 async_context->auth = g_object_ref (auth);
1709 simple = g_simple_async_result_new (
1710 G_OBJECT (registry), callback, user_data,
1711 e_source_registry_authenticate);
1713 g_simple_async_result_set_check_cancellable (simple, cancellable);
1715 g_simple_async_result_set_op_res_gpointer (
1716 simple, async_context, (GDestroyNotify) async_context_free);
1718 g_simple_async_result_run_in_thread (
1719 simple, source_registry_authenticate_thread,
1720 G_PRIORITY_DEFAULT, cancellable);
1722 g_object_unref (simple);
1726 * e_source_registry_authenticate_finish:
1727 * @registry: an #ESourceRegistry
1728 * @result: a #GAsyncResult
1729 * @error: return location for a #GError, or %NULL
1731 * Finishes the operation started with e_source_registry_authenticate().
1732 * If an error occurred, the function will set @error and return %FALSE.
1734 * Returns: %TRUE on success, %FALSE on failure
1739 e_source_registry_authenticate_finish (ESourceRegistry *registry,
1740 GAsyncResult *result,
1743 GSimpleAsyncResult *simple;
1745 g_return_val_if_fail (
1746 g_simple_async_result_is_valid (
1747 result, G_OBJECT (registry),
1748 e_source_registry_authenticate), FALSE);
1750 simple = G_SIMPLE_ASYNC_RESULT (result);
1752 /* Assume success unless a GError is set. */
1753 return !g_simple_async_result_propagate_error (simple, error);
1756 /* Helper for e_source_registry_commit_source() */
1758 source_registry_commit_source_thread (GSimpleAsyncResult *simple,
1760 GCancellable *cancellable)
1762 AsyncContext *async_context;
1763 GError *error = NULL;
1765 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1767 e_source_registry_commit_source_sync (
1768 E_SOURCE_REGISTRY (object),
1769 async_context->source,
1770 cancellable, &error);
1773 g_simple_async_result_take_error (simple, error);
1777 * e_source_registry_commit_source_sync:
1778 * @registry: an #ESourceRegistry
1779 * @source: an #ESource with changes to commit
1780 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1781 * @error: return location for #GError, or %NULL
1783 * This is a convenience function intended for use with graphical
1784 * #ESource editors. Call this function when the user is finished
1785 * making changes to @source.
1787 * If @source has a #GDBusObject, its contents are submitted to the D-Bus
1788 * service through e_source_write_sync().
1790 * If @source does NOT have a #GDBusObject (implying it's a scratch
1791 * #ESource), its contents are submitted to the D-Bus service through
1792 * e_source_registry_create_sources_sync().
1794 * If an error occurs, the function will set @error and return %FALSE.
1796 * Returns: %TRUE on success, %FALSE on failure
1801 e_source_registry_commit_source_sync (ESourceRegistry *registry,
1803 GCancellable *cancellable,
1806 GDBusObject *dbus_object;
1809 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1810 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
1812 dbus_object = e_source_ref_dbus_object (source);
1814 if (dbus_object != NULL) {
1815 success = e_source_write_sync (source, cancellable, error);
1816 g_object_unref (dbus_object);
1818 GList *list = g_list_prepend (NULL, source);
1819 success = e_source_registry_create_sources_sync (
1820 registry, list, cancellable, error);
1828 * e_source_registry_commit_source:
1829 * @registry: an #ESourceRegistry
1830 * @source: an #ESource with changes to commit
1831 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1832 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1834 * @user_data: (closure): data to pass to the callback function
1836 * See e_source_registry_commit_source_sync() for details.
1838 * When the operation is finished, @callback will be called. You can then
1839 * call e_source_registry_commit_source_finish() to get the result of the
1845 e_source_registry_commit_source (ESourceRegistry *registry,
1847 GCancellable *cancellable,
1848 GAsyncReadyCallback callback,
1851 GSimpleAsyncResult *simple;
1852 AsyncContext *async_context;
1854 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
1855 g_return_if_fail (E_IS_SOURCE (source));
1857 async_context = g_slice_new0 (AsyncContext);
1858 async_context->source = g_object_ref (source);
1860 simple = g_simple_async_result_new (
1861 G_OBJECT (registry), callback, user_data,
1862 e_source_registry_commit_source);
1864 g_simple_async_result_set_check_cancellable (simple, cancellable);
1866 g_simple_async_result_set_op_res_gpointer (
1867 simple, async_context, (GDestroyNotify) async_context_free);
1869 g_simple_async_result_run_in_thread (
1870 simple, source_registry_commit_source_thread,
1871 G_PRIORITY_DEFAULT, cancellable);
1873 g_object_unref (simple);
1877 * e_source_registry_commit_source_finish:
1878 * @registry: an #ESourceRegistry
1879 * @result: a #GAsyncResult
1880 * @error: return location for a #GError, or %NULL
1882 * Finishes the operation started with e_source_registry_commit_source().
1884 * If an error occurred, the function will set @error and return %FALSE.
1886 * Returns: %TRUE on success, %FALSE on failure
1891 e_source_registry_commit_source_finish (ESourceRegistry *registry,
1892 GAsyncResult *result,
1895 GSimpleAsyncResult *simple;
1897 g_return_val_if_fail (
1898 g_simple_async_result_is_valid (
1899 result, G_OBJECT (registry),
1900 e_source_registry_commit_source), FALSE);
1902 simple = G_SIMPLE_ASYNC_RESULT (result);
1904 /* Assume success unless a GError is set. */
1905 return !g_simple_async_result_propagate_error (simple, error);
1908 /* Helper for e_source_registry_create_sources() */
1910 source_registry_create_sources_thread (GSimpleAsyncResult *simple,
1912 GCancellable *cancellable)
1914 AsyncContext *async_context;
1915 GError *error = NULL;
1917 async_context = g_simple_async_result_get_op_res_gpointer (simple);
1919 e_source_registry_create_sources_sync (
1920 E_SOURCE_REGISTRY (object),
1921 async_context->list_of_sources,
1922 cancellable, &error);
1925 g_simple_async_result_take_error (simple, error);
1929 * e_source_registry_create_sources_sync:
1930 * @registry: an #ESourceRegistry
1931 * @list_of_sources: (element-type ESource): a list of #ESource instances with
1933 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1934 * @error: return location for a #GError, or %NULL
1936 * Requests the D-Bus service create new key files for each #ESource in
1937 * @list_of_sources. Each list element must be a scratch #ESource with
1940 * If an error occurs, the function will set @error and return %FALSE.
1942 * Returns: %TRUE on success, %FALSE on failure
1947 e_source_registry_create_sources_sync (ESourceRegistry *registry,
1948 GList *list_of_sources,
1949 GCancellable *cancellable,
1952 GVariantBuilder builder;
1957 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
1959 /* Verify the list elements are all ESources. */
1960 for (link = list_of_sources; link != NULL; link = g_list_next (link))
1961 g_return_val_if_fail (E_IS_SOURCE (link->data), FALSE);
1963 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1965 for (link = list_of_sources; link != NULL; link = g_list_next (link)) {
1970 source = E_SOURCE (link->data);
1971 uid = e_source_get_uid (source);
1973 source_data = e_source_to_string (source, NULL);
1974 g_variant_builder_add (&builder, "{ss}", uid, source_data);
1975 g_free (source_data);
1978 variant = g_variant_builder_end (&builder);
1980 /* This function sinks the floating GVariant reference. */
1981 success = e_dbus_source_manager_call_create_sources_sync (
1982 registry->priv->dbus_source_manager,
1983 variant, cancellable, error);
1985 g_variant_builder_clear (&builder);
1991 * e_source_registry_create_sources:
1992 * @registry: an #ESourceRegistry
1993 * @list_of_sources: (element-type ESource): a list of #ESource instances with
1995 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
1996 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
1998 * @user_data: (closure): data to pass to the callback function
2000 * Asynchronously requests the D-Bus service create new key files for each
2001 * #ESource in @list_of_sources. Each list element must be a scratch
2002 * #ESource with no #GDBusObject.
2004 * When the operation is finished, @callback will be called. You can then
2005 * call e_source_registry_create_sources_finish() to get the result of the
2011 e_source_registry_create_sources (ESourceRegistry *registry,
2012 GList *list_of_sources,
2013 GCancellable *cancellable,
2014 GAsyncReadyCallback callback,
2017 GSimpleAsyncResult *simple;
2018 AsyncContext *async_context;
2021 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2023 /* Verify the list elements are all ESources. */
2024 for (link = list_of_sources; link != NULL; link = g_list_next (link))
2025 g_return_if_fail (E_IS_SOURCE (link->data));
2027 async_context = g_slice_new0 (AsyncContext);
2028 async_context->list_of_sources = g_list_copy (list_of_sources);
2031 async_context->list_of_sources,
2032 (GFunc) g_object_ref, NULL);
2034 simple = g_simple_async_result_new (
2035 G_OBJECT (registry), callback, user_data,
2036 e_source_registry_create_sources);
2038 g_simple_async_result_set_check_cancellable (simple, cancellable);
2040 g_simple_async_result_set_op_res_gpointer (
2041 simple, async_context, (GDestroyNotify) async_context_free);
2043 g_simple_async_result_run_in_thread (
2044 simple, source_registry_create_sources_thread,
2045 G_PRIORITY_DEFAULT, cancellable);
2047 g_object_unref (simple);
2051 * e_source_registry_create_sources_finish:
2052 * @registry: an #ESourceRegistry
2053 * @result: a #GAsyncResult
2054 * @error: return location for a #GError, or %NULL
2056 * Finishes the operation started with e_source_registry_create_sources().
2058 * If an error occurred, the function will set @error and return %FALSE.
2060 * Returns: %TRUE on success, %FALSE on failure
2065 e_source_registry_create_sources_finish (ESourceRegistry *registry,
2066 GAsyncResult *result,
2069 GSimpleAsyncResult *simple;
2071 g_return_val_if_fail (
2072 g_simple_async_result_is_valid (
2073 result, G_OBJECT (registry),
2074 e_source_registry_create_sources), FALSE);
2076 simple = G_SIMPLE_ASYNC_RESULT (result);
2078 /* Assume success unless a GError is set. */
2079 return !g_simple_async_result_propagate_error (simple, error);
2083 * e_source_registry_ref_source:
2084 * @registry: an #ESourceRegistry
2085 * @uid: a unique identifier string
2087 * Looks up an #ESource in @registry by its unique identifier string.
2089 * The returned #ESource is referenced for thread-safety and must be
2090 * unreferenced with g_object_unref() when finished with it.
2092 * Returns: (transfer full): an #ESource, or %NULL if no match was found
2097 e_source_registry_ref_source (ESourceRegistry *registry,
2100 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2101 g_return_val_if_fail (uid != NULL, NULL);
2103 return source_registry_sources_lookup (registry, uid);
2107 * e_source_registry_list_sources:
2108 * @registry: an #ESourceRegistry
2109 * @extension_name: (allow-none): an extension name, or %NULL
2111 * Returns a list of registered sources, sorted by display name. If
2112 * @extension_name is given, restrict the list to sources having that
2115 * The sources returned in the list are referenced for thread-safety.
2116 * They must each be unreferenced with g_object_unref() when finished
2117 * when them. Free the returned list itself with g_list_free().
2119 * An easy way to free the list properly in one step is as follows:
2122 * g_list_free_full (list, g_object_unref);
2125 * Returns: (element-type ESource) (transfer full): a sorted list of sources
2130 e_source_registry_list_sources (ESourceRegistry *registry,
2131 const gchar *extension_name)
2134 GQueue trash = G_QUEUE_INIT;
2136 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2138 list = g_list_sort (
2139 source_registry_sources_get_values (registry),
2140 (GCompareFunc) e_source_compare_by_display_name);
2142 if (extension_name == NULL)
2145 for (link = list; link != NULL; link = g_list_next (link)) {
2146 ESource *source = E_SOURCE (link->data);
2148 if (!e_source_has_extension (source, extension_name)) {
2149 g_queue_push_tail (&trash, link);
2150 g_object_unref (source);
2154 /* We do want pop_head() here, not pop_head_link(). */
2155 while ((link = g_queue_pop_head (&trash)) != NULL)
2156 list = g_list_delete_link (list, link);
2162 * e_source_registry_find_extension:
2163 * @registry: an #ESourceRegistry
2164 * @source: an #ESource
2165 * @extension_name: the extension name to find
2167 * Examines @source and its ancestors and returns the "deepest" #ESource
2168 * having an #ESourceExtension with the given @extension_name. If neither
2169 * @source nor any of its ancestors have such an extension, the function
2172 * This function is useful in cases when an #ESourceExtension is meant to
2173 * apply to both the #ESource it belongs to and the #ESource's descendants.
2175 * A common example is the #ESourceCollection extension, where descendants
2176 * of an #ESource having an #ESourceCollection extension are implied to be
2177 * members of that collection. In that example, this function can be used
2178 * to test whether @source is a member of a collection.
2180 * The returned #ESource is referenced for thread-safety and must be
2181 * unreferenced with g_object_unref() when finished with it.
2183 * Note the function returns the #ESource containing the #ESourceExtension
2184 * instead of the #ESourceExtension itself because extension instances are
2185 * not to be referenced directly (see e_source_get_extension()).
2187 * Returns: (transfer full): an #ESource, or %NULL if no match was found
2192 e_source_registry_find_extension (ESourceRegistry *registry,
2194 const gchar *extension_name)
2196 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2197 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2198 g_return_val_if_fail (extension_name != NULL, NULL);
2200 g_object_ref (source);
2202 while (!e_source_has_extension (source, extension_name)) {
2205 uid = e_source_dup_parent (source);
2207 g_object_unref (source);
2211 source = e_source_registry_ref_source (registry, uid);
2222 /* Helper for e_source_registry_build_display_tree() */
2224 source_registry_compare_nodes (GNode *node_a,
2227 ESource *source_a = E_SOURCE (node_a->data);
2228 ESource *source_b = E_SOURCE (node_b->data);
2229 const gchar *uid_a, *uid_b;
2231 uid_a = e_source_get_uid (source_a);
2232 uid_b = e_source_get_uid (source_b);
2234 /* Sanity check, with runtime warnings. */
2235 if (uid_a == NULL) {
2236 g_warn_if_reached ();
2239 if (uid_b == NULL) {
2240 g_warn_if_reached ();
2244 /* The built-in "local-stub" source comes first at depth 1. */
2246 if (g_strcmp0 (uid_a, "local-stub") == 0)
2249 if (g_strcmp0 (uid_b, "local-stub") == 0)
2252 /* The built-in "system-*" sources come first at depth 2. */
2254 if (g_str_has_prefix (uid_a, "system-"))
2257 if (g_str_has_prefix (uid_b, "system-"))
2260 return e_source_compare_by_display_name (source_a, source_b);
2263 /* Helper for e_source_registry_build_display_tree() */
2265 source_registry_prune_nodes (GNode *node,
2266 const gchar *extension_name)
2268 GQueue queue = G_QUEUE_INIT;
2271 /* Unlink all the child nodes and place them in a queue. */
2272 while ((child_node = g_node_first_child (node)) != NULL) {
2273 g_node_unlink (child_node);
2274 g_queue_push_tail (&queue, child_node);
2277 /* Sort the queue by source name. */
2279 &queue, (GCompareDataFunc)
2280 source_registry_compare_nodes, NULL);
2282 /* Pop nodes off the head of the queue until the queue is empty.
2283 * If the node has either its own children or the given extension
2284 * name, put it back under the parent node (preserving the sorted
2285 * order). Otherwise delete the node and its descendants. */
2286 while ((child_node = g_queue_pop_head (&queue)) != NULL) {
2287 ESource *child = E_SOURCE (child_node->data);
2288 gboolean append_child_node = FALSE;
2290 if (extension_name == NULL)
2291 append_child_node = e_source_get_enabled (child);
2293 else if (e_source_has_extension (child, extension_name))
2294 append_child_node = e_source_get_enabled (child);
2296 else if (g_node_first_child (child_node) != NULL)
2297 append_child_node = e_source_get_enabled (child);
2299 if (append_child_node)
2300 g_node_append (node, child_node);
2302 e_source_registry_free_display_tree (child_node);
2309 * e_source_registry_build_display_tree:
2310 * @registry: an #ESourceRegistry
2311 * @extension_name: (allow-none): an extension name, or %NULL
2313 * Returns a single #GNode tree of registered sources that can be used to
2314 * populate a #GtkTreeModel. (The root #GNode is just an empty placeholder.)
2316 * Similar to e_source_registry_list_sources(), an @extension_name can be
2317 * given to restrict the tree to sources having that extension name. Parents
2318 * of matched sources are included in the tree regardless of whether they have
2319 * an extension named @extension_name.
2321 * Disabled leaf nodes are automatically excluded from the #GNode tree.
2323 * The sources returned in the tree are referenced for thread-safety.
2324 * They must each be unreferenced with g_object_unref() when finished
2325 * with them. Free the returned tree itself with g_node_destroy().
2326 * For convenience, e_source_registry_free_display_tree() does all
2329 * Returns: (element-type ESource) (transfer full): a tree of sources,
2330 * arranged for display
2335 e_source_registry_build_display_tree (ESourceRegistry *registry,
2336 const gchar *extension_name)
2340 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2342 /* Assemble all data sources into a tree. */
2343 root = source_registry_sources_build_tree (registry);
2345 /* Prune unwanted nodes from the copied source trees.
2346 * This must be done in "post" order (children first)
2347 * since it reorders and deletes child nodes. */
2349 root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2350 (GNodeTraverseFunc) source_registry_prune_nodes,
2351 (gpointer) extension_name);
2356 /* Helper for e_source_registry_free_display_tree() */
2358 source_registry_unref_nodes (GNode *node)
2360 while (node != NULL) {
2361 if (node->children != NULL)
2362 source_registry_unref_nodes (node->children);
2363 if (node->data != NULL)
2364 g_object_unref (node->data);
2370 * e_source_registry_free_display_tree:
2371 * @display_tree: a tree of sources, arranged for display
2373 * Convenience function to free a #GNode tree of registered
2374 * sources created by e_source_registry_build_display_tree().
2379 e_source_registry_free_display_tree (GNode *display_tree)
2381 g_return_if_fail (display_tree != NULL);
2383 /* XXX This would be easier if GLib had something like
2384 * g_node_destroy_full() which took a GDestroyNotify.
2385 * Then the tree would not have to be traversed twice. */
2387 source_registry_unref_nodes (display_tree);
2388 g_node_destroy (display_tree);
2391 /* Helper for e_source_registry_debug_dump() */
2393 source_registry_debug_dump_cb (GNode *node)
2397 /* Root node is an empty placeholder. */
2398 if (G_NODE_IS_ROOT (node))
2401 depth = g_node_depth (node);
2402 for (ii = 2; ii < depth; ii++)
2405 if (E_IS_SOURCE (node->data)) {
2406 ESource *source = E_SOURCE (node->data);
2407 g_print ("\"%s\" ", e_source_get_display_name (source));
2408 g_print ("(%s)", e_source_get_uid (source));
2417 * e_source_registry_debug_dump:
2418 * @registry: an #ESourceRegistry
2419 * @extension_name: (allow-none): an extension name, or %NULL
2421 * Handy debugging function that uses e_source_registry_build_display_tree()
2422 * to print a tree of registered sources to standard output.
2427 e_source_registry_debug_dump (ESourceRegistry *registry,
2428 const gchar *extension_name)
2432 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2434 root = e_source_registry_build_display_tree (registry, extension_name);
2437 root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2438 (GNodeTraverseFunc) source_registry_debug_dump_cb, NULL);
2440 e_source_registry_free_display_tree (root);
2444 * e_source_registry_ref_builtin_address_book:
2445 * @registry: an #ESourceRegistry
2447 * Returns the built-in address book #ESource.
2449 * This #ESource is always present and makes for a safe fallback.
2451 * The returned #ESource is referenced for thread-safety and must be
2452 * unreferenced with g_object_unref() when finished with it.
2454 * Returns: (transfer full): the built-in address book #ESource
2459 e_source_registry_ref_builtin_address_book (ESourceRegistry *registry)
2464 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2466 uid = E_SOURCE_BUILTIN_ADDRESS_BOOK_UID;
2467 source = e_source_registry_ref_source (registry, uid);
2468 g_return_val_if_fail (source != NULL, NULL);
2474 * e_source_registry_ref_default_address_book:
2475 * @registry: an #ESourceRegistry
2477 * Returns the #ESource most recently passed to
2478 * e_source_registry_set_default_address_book() either in this session
2479 * or a previous session, or else falls back to the built-in address book.
2481 * The returned #ESource is referenced for thread-safety and must be
2482 * unreferenced with g_object_unref() when finished with it.
2484 * Returns: (transfer full): the default address book #ESource
2489 e_source_registry_ref_default_address_book (ESourceRegistry *registry)
2495 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2497 key = E_SETTINGS_DEFAULT_ADDRESS_BOOK_KEY;
2498 uid = g_settings_get_string (registry->priv->settings, key);
2499 source = e_source_registry_ref_source (registry, uid);
2502 /* The built-in source is always present. */
2504 source = e_source_registry_ref_builtin_address_book (registry);
2506 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2512 * e_source_registry_set_default_address_book:
2513 * @registry: an #ESourceRegistry
2514 * @default_source: (allow-none): an address book #ESource, or %NULL
2516 * Sets @default_source as the default address book. If @default_source
2517 * is %NULL, the default address book is reset to the built-in address book.
2518 * This setting will persist across sessions until changed.
2523 e_source_registry_set_default_address_book (ESourceRegistry *registry,
2524 ESource *default_source)
2529 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2531 if (default_source != NULL) {
2532 g_return_if_fail (E_IS_SOURCE (default_source));
2533 uid = e_source_get_uid (default_source);
2535 uid = E_SOURCE_BUILTIN_ADDRESS_BOOK_UID;
2538 key = E_SETTINGS_DEFAULT_ADDRESS_BOOK_KEY;
2539 g_settings_set_string (registry->priv->settings, key, uid);
2541 /* The GSettings::changed signal will trigger a "notify" signal
2542 * from the registry, so no need to call g_object_notify() here. */
2546 * e_source_registry_ref_builtin_calendar:
2547 * @registry: an #ESourceRegistry
2549 * Returns the built-in calendar #ESource.
2551 * This #ESource is always present and makes for a safe fallback.
2553 * The returned #ESource is referenced for thread-safety and must be
2554 * unreferenced with g_object_unref() when finished with it.
2556 * Returns: (transfer full): the built-in calendar #ESource
2561 e_source_registry_ref_builtin_calendar (ESourceRegistry *registry)
2566 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2568 uid = E_SOURCE_BUILTIN_CALENDAR_UID;
2569 source = e_source_registry_ref_source (registry, uid);
2570 g_return_val_if_fail (source != NULL, NULL);
2576 * e_source_registry_ref_default_calendar:
2577 * @registry: an #ESourceRegistry
2579 * Returns the #ESource most recently passed to
2580 * e_source_registry_set_default_calendar() either in this session
2581 * or a previous session, or else falls back to the built-in calendar.
2583 * The returned #ESource is referenced for thread-safety and must be
2584 * unreferenced with g_object_unref() when finished with it.
2586 * Returns: (transfer full): the default calendar #ESource
2591 e_source_registry_ref_default_calendar (ESourceRegistry *registry)
2597 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2599 key = E_SETTINGS_DEFAULT_CALENDAR_KEY;
2600 uid = g_settings_get_string (registry->priv->settings, key);
2601 source = e_source_registry_ref_source (registry, uid);
2604 /* The built-in source is always present. */
2606 source = e_source_registry_ref_builtin_calendar (registry);
2608 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2614 * e_source_registry_set_default_calendar:
2615 * @registry: an #ESourceRegistry
2616 * @default_source: (allow-none): a calendar #ESource, or %NULL
2618 * Sets @default_source as the default calendar. If @default_source
2619 * is %NULL, the default calendar is reset to the built-in calendar.
2620 * This setting will persist across sessions until changed.
2625 e_source_registry_set_default_calendar (ESourceRegistry *registry,
2626 ESource *default_source)
2631 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2633 if (default_source != NULL) {
2634 g_return_if_fail (E_IS_SOURCE (default_source));
2635 uid = e_source_get_uid (default_source);
2637 uid = E_SOURCE_BUILTIN_CALENDAR_UID;
2640 key = E_SETTINGS_DEFAULT_CALENDAR_KEY;
2641 g_settings_set_string (registry->priv->settings, key, uid);
2643 /* The GSettings::changed signal will trigger a "notify" signal
2644 * from the registry, so no need to call g_object_notify() here. */
2648 * e_source_registry_ref_builtin_mail_account:
2649 * @registry: an #ESourceRegistry
2651 * Returns the built-in mail account #ESource.
2653 * This #ESource is always present and makes for a safe fallback.
2655 * The returned #ESource is referenced for thread-safety and must be
2656 * unreferenced with g_object_unref() when finished with it.
2658 * Returns: (transfer full): the built-in mail account #ESource
2663 e_source_registry_ref_builtin_mail_account (ESourceRegistry *registry)
2668 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2670 uid = E_SOURCE_BUILTIN_MAIL_ACCOUNT_UID;
2671 source = e_source_registry_ref_source (registry, uid);
2672 g_return_val_if_fail (source != NULL, NULL);
2678 * e_source_registry_ref_default_mail_account:
2679 * @registry: an #ESourceRegistry
2681 * Returns the #ESource most recently passed to
2682 * e_source_registry_set_default_mail_account() either in this session
2683 * or a previous session, or else falls back to the built-in mail account.
2685 * The returned #ESource is referenced for thread-safety and must be
2686 * unreferenced with g_object_unref() when finished with it.
2688 * Returns: (transfer full): the default mail account #ESource
2693 e_source_registry_ref_default_mail_account (ESourceRegistry *registry)
2699 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2701 key = E_SETTINGS_DEFAULT_MAIL_ACCOUNT_KEY;
2702 uid = g_settings_get_string (registry->priv->settings, key);
2703 source = e_source_registry_ref_source (registry, uid);
2706 /* The built-in source is always present. */
2708 source = e_source_registry_ref_builtin_mail_account (registry);
2710 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2716 * e_source_registry_set_default_mail_account:
2717 * @registry: an #ESourceRegistry
2718 * @default_source: (allow-none): a mail account #ESource, or %NULL
2720 * Sets @default_source as the default mail account. If @default_source
2721 * is %NULL, the default mail account is reset to the built-in mail account.
2722 * This setting will persist across sessions until changed.
2727 e_source_registry_set_default_mail_account (ESourceRegistry *registry,
2728 ESource *default_source)
2733 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2735 if (default_source != NULL) {
2736 g_return_if_fail (E_IS_SOURCE (default_source));
2737 uid = e_source_get_uid (default_source);
2739 uid = E_SOURCE_BUILTIN_MAIL_ACCOUNT_UID;
2742 key = E_SETTINGS_DEFAULT_MAIL_ACCOUNT_KEY;
2743 g_settings_set_string (registry->priv->settings, key, uid);
2745 /* The GSettings::changed signal will trigger a "notify" signal
2746 * from the registry, so no need to call g_object_notify() here. */
2749 /* Helper for e_source_registry_ref_default_mail_identity() */
2751 source_registry_ref_any_mail_identity (ESourceRegistry *registry)
2755 const gchar *extension_name;
2758 /* First fallback: Return the mail identity named
2759 * by the default mail account. */
2761 source = e_source_registry_ref_default_mail_account (registry);
2763 /* This should never be NULL, but just to be safe. */
2764 if (source != NULL) {
2765 ESourceMailAccount *extension;
2767 extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
2768 extension = e_source_get_extension (source, extension_name);
2769 uid = e_source_mail_account_dup_identity_uid (extension);
2771 g_object_unref (source);
2776 source = e_source_registry_ref_source (registry, uid);
2783 /* Second fallback: Pick any available mail identity,
2784 * preferring enabled identities. */
2786 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
2787 list = e_source_registry_list_sources (registry, extension_name);
2789 for (link = list; link != NULL; link = g_list_next (link)) {
2790 ESource *candidate = E_SOURCE (link->data);
2792 if (e_source_get_enabled (candidate)) {
2793 source = g_object_ref (candidate);
2798 if (source == NULL && list != NULL)
2799 source = g_object_ref (link->data);
2801 g_list_free_full (list, (GDestroyNotify) g_object_unref);
2807 * e_source_registry_ref_default_mail_identity:
2808 * @registry: an #ESourceRegistry
2810 * Returns the #ESource most recently passed to
2811 * e_source_registry_set_default_mail_identity() either in this session
2812 * or a previous session, or else falls back to the mail identity named
2813 * by the default mail account. If even that fails it returns any mail
2814 * identity from @registry, or %NULL if there are none.
2816 * The returned #ESource is referenced for thread-safety and must be
2817 * unreferenced with g_object_unref() when finished with it.
2819 * Returns: (transfer full): the default mail identity #ESource, or %NULL
2824 e_source_registry_ref_default_mail_identity (ESourceRegistry *registry)
2830 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2832 key = E_SETTINGS_DEFAULT_MAIL_IDENTITY_KEY;
2833 uid = g_settings_get_string (registry->priv->settings, key);
2834 source = e_source_registry_ref_source (registry, uid);
2838 source = source_registry_ref_any_mail_identity (registry);
2844 * e_source_registry_set_default_mail_identity:
2845 * @registry: an #ESourceRegistry
2846 * @default_source: (allow-none): a mail identity #ESource, or %NULL
2848 * Sets @default_source as the default mail identity. If @default_source
2849 * is %NULL, the next request for the default mail identity will use the
2850 * fallbacks described in e_source_registry_get_default_mail_identity().
2855 e_source_registry_set_default_mail_identity (ESourceRegistry *registry,
2856 ESource *default_source)
2861 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2863 if (default_source != NULL) {
2864 g_return_if_fail (E_IS_SOURCE (default_source));
2865 uid = e_source_get_uid (default_source);
2867 uid = ""; /* no built-in mail identity */
2870 key = E_SETTINGS_DEFAULT_MAIL_IDENTITY_KEY;
2871 g_settings_set_string (registry->priv->settings, key, uid);
2873 /* The GSettings::changed signal will trigger a "notify" signal
2874 * from the registry, so no need to call g_object_notify() here. */
2878 * e_source_registry_ref_builtin_memo_list:
2879 * @registry: an #ESourceRegistry
2881 * Returns the built-in memo list #ESource.
2883 * This #ESource is always present and makes for a safe fallback.
2885 * The returned #ESource is referenced for thread-safety and must be
2886 * unreferenced with g_object_unref() when finished with it.
2888 * Returns: (transfer full): the built-in memo list #ESource
2893 e_source_registry_ref_builtin_memo_list (ESourceRegistry *registry)
2898 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2900 uid = E_SOURCE_BUILTIN_MEMO_LIST_UID;
2901 source = e_source_registry_ref_source (registry, uid);
2902 g_return_val_if_fail (source != NULL, NULL);
2908 * e_source_registry_ref_default_memo_list:
2909 * @registry: an #ESourceRegistry
2911 * Returns the #ESource most recently passed to
2912 * e_source_registry_set_default_memo_list() either in this session
2913 * or a previous session, or else falls back to the built-in memo list.
2915 * The returned #ESource is referenced for thread-safety and must be
2916 * unreferenced with g_object_unref() when finished with it.
2918 * Returns: (transfer full): the default memo list #ESource
2923 e_source_registry_ref_default_memo_list (ESourceRegistry *registry)
2929 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
2931 key = E_SETTINGS_DEFAULT_MEMO_LIST_KEY;
2932 uid = g_settings_get_string (registry->priv->settings, key);
2933 source = e_source_registry_ref_source (registry, uid);
2936 /* The built-in source is always present. */
2938 source = e_source_registry_ref_builtin_memo_list (registry);
2940 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
2946 * e_source_registry_set_default_memo_list:
2947 * @registry: an #ESourceRegistry
2948 * @default_source: (allow-none): a memo list #ESource, or %NULL
2950 * Sets @default_source as the default memo list. If @default_source
2951 * is %NULL, the default memo list is reset to the built-in memo list.
2952 * This setting will persist across sessions until changed.
2957 e_source_registry_set_default_memo_list (ESourceRegistry *registry,
2958 ESource *default_source)
2963 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
2965 if (default_source != NULL) {
2966 g_return_if_fail (E_IS_SOURCE (default_source));
2967 uid = e_source_get_uid (default_source);
2969 uid = E_SOURCE_BUILTIN_MEMO_LIST_UID;
2972 key = E_SETTINGS_DEFAULT_MEMO_LIST_KEY;
2973 g_settings_set_string (registry->priv->settings, key, uid);
2975 /* The GSettings::changed signal will trigger a "notify" signal
2976 * from the registry, so no need to call g_object_notify() here. */
2980 * e_source_registry_ref_builtin_task_list:
2981 * @registry: an #ESourceRegistry
2983 * Returns the built-in task list #ESource.
2985 * This #ESource is always present and makes for a safe fallback.
2987 * The returned #ESource is referenced for thread-safety and must be
2988 * unreferenced with g_object_unref() when finished with it.
2990 * Returns: (transfer full): the built-in task list #ESource
2995 e_source_registry_ref_builtin_task_list (ESourceRegistry *registry)
3000 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3002 uid = E_SOURCE_BUILTIN_TASK_LIST_UID;
3003 source = e_source_registry_ref_source (registry, uid);
3004 g_return_val_if_fail (source != NULL, NULL);
3010 * e_source_registry_ref_default_task_list:
3011 * @registry: an #ESourceRegistry
3013 * Returns the #ESource most recently passed to
3014 * e_source_registry_set_default_task_list() either in this session
3015 * or a previous session, or else falls back to the built-in task list.
3017 * The returned #ESource is referenced for thread-safety and must be
3018 * unreferenced with g_object_unref() when finished with it.
3020 * Returns: (transfer full): the default task list #ESource
3025 e_source_registry_ref_default_task_list (ESourceRegistry *registry)
3031 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3033 key = E_SETTINGS_DEFAULT_TASK_LIST_KEY;
3034 uid = g_settings_get_string (registry->priv->settings, key);
3035 source = e_source_registry_ref_source (registry, uid);
3038 /* The built-in source is always present. */
3040 source = e_source_registry_ref_builtin_task_list (registry);
3042 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
3048 * e_source_registry_set_default_task_list:
3049 * @registry: an #ESourceRegistry
3050 * @default_source: (allow-none): a task list #ESource, or %NULL
3052 * Sets @default_source as the default task list. If @default_source
3053 * is %NULL, the default task list is reset to the built-in task list.
3054 * This setting will persist across sessions until changed.
3059 e_source_registry_set_default_task_list (ESourceRegistry *registry,
3060 ESource *default_source)
3065 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
3067 if (default_source != NULL) {
3068 g_return_if_fail (E_IS_SOURCE (default_source));
3069 uid = e_source_get_uid (default_source);
3071 uid = E_SOURCE_BUILTIN_TASK_LIST_UID;
3074 key = E_SETTINGS_DEFAULT_TASK_LIST_KEY;
3075 g_settings_set_string (registry->priv->settings, key, uid);
3077 /* The GSettings::changed signal will trigger a "notify" signal
3078 * from the registry, so no need to call g_object_notify() here. */
3082 * e_source_registry_ref_default_for_extension_name:
3083 * @registry: an #ESourceRegistry
3084 * @extension_name: an extension_name
3086 * This is a convenience function to return a default #ESource based on
3087 * @extension_name. This only works with a subset of extension names.
3089 * If @extension_name is #E_SOURCE_EXTENSION_ADDRESS_BOOK, the function
3090 * returns the current default address book, or else falls back to the
3091 * built-in address book.
3093 * If @extension_name is #E_SOURCE_EXTENSION_CALENDAR, the function returns
3094 * the current default calendar, or else falls back to the built-in calendar.
3096 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_ACCOUNT, the function
3097 * returns the current default mail account, or else falls back to the
3098 * built-in mail account.
3100 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_IDENTITY, the function
3101 * returns the current default mail identity, or else falls back to the
3102 * mail identity named by the current default mail account.
3104 * If @extension_name is #E_SOURCE_EXTENSION_MEMO_LIST, the function returns
3105 * the current default memo list, or else falls back to the built-in memo list.
3107 * If @extension_name is #E_SOURCE_EXTENSION_TASK_LIST, the function returns
3108 * the current default task list, or else falls back to the built-in task list.
3110 * For all other values of @extension_name, the function returns %NULL.
3112 * The returned #ESource is referenced for thread-safety and must be
3113 * unreferenced with g_object_unref() when finished with it.
3115 * Returns: (transfer full): the default #ESource based on @extension_name
3120 e_source_registry_ref_default_for_extension_name (ESourceRegistry *registry,
3121 const gchar *extension_name)
3123 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
3124 g_return_val_if_fail (extension_name != NULL, NULL);
3126 if (strcmp (extension_name, E_SOURCE_EXTENSION_ADDRESS_BOOK) == 0)
3127 return e_source_registry_ref_default_address_book (registry);
3129 if (strcmp (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0)
3130 return e_source_registry_ref_default_calendar (registry);
3132 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_ACCOUNT) == 0)
3133 return e_source_registry_ref_default_mail_account (registry);
3135 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_IDENTITY) == 0)
3136 return e_source_registry_ref_default_mail_identity (registry);
3138 if (strcmp (extension_name, E_SOURCE_EXTENSION_MEMO_LIST) == 0)
3139 return e_source_registry_ref_default_memo_list (registry);
3141 if (strcmp (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0)
3142 return e_source_registry_ref_default_task_list (registry);
3148 * e_source_registry_set_default_for_extension_name:
3149 * @registry: an #ESourceRegistry
3150 * @extension_name: an extension name
3151 * @default_source: (allow-none): an #ESource, or %NULL
3153 * This is a convenience function to set a default #ESource based on
3154 * @extension_name. This only works with a subset of extension names.
3156 * If @extension_name is #E_SOURCE_EXTENSION_ADDRESS_BOOK, the function
3157 * sets @default_source as the default address book. If @default_source
3158 * is %NULL, the default address book is reset to the built-in address book.
3160 * If @extension_name is #E_SOURCE_EXTENSION_CALENDAR, the function sets
3161 * @default_source as the default calendar. If @default_source is %NULL,
3162 * the default calendar is reset to the built-in calendar.
3164 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_ACCOUNT, the function
3165 * sets @default_source as the default mail account. If @default_source
3166 * is %NULL, the default mail account is reset to the built-in mail account.
3168 * If @extension_name is #E_SOURCE_EXTENSION_MAIL_IDENTITY, the function
3169 * sets @default_source as the default mail identity. If @default_source
3170 * is %NULL, the next request for the default mail identity will return
3171 * the mail identity named by the default mail account.
3173 * If @extension_name is #E_SOURCE_EXTENSION_MEMO_LIST, the function sets
3174 * @default_source as the default memo list. If @default_source is %NULL,
3175 * the default memo list is reset to the built-in memo list.
3177 * If @extension_name is #E_SOURCE_EXTENSION_TASK_LIST, the function sets
3178 * @default_source as the default task list. If @default_source is %NULL,
3179 * the default task list is reset to the built-in task list.
3181 * For all other values of @extension_name, the function does nothing.
3186 e_source_registry_set_default_for_extension_name (ESourceRegistry *registry,
3187 const gchar *extension_name,
3188 ESource *default_source)
3190 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
3191 g_return_if_fail (extension_name != NULL);
3193 if (strcmp (extension_name, E_SOURCE_EXTENSION_ADDRESS_BOOK) == 0)
3194 e_source_registry_set_default_address_book (
3195 registry, default_source);
3197 if (strcmp (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0)
3198 e_source_registry_set_default_calendar (
3199 registry, default_source);
3201 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_ACCOUNT) == 0)
3202 e_source_registry_set_default_mail_account (
3203 registry, default_source);
3205 if (strcmp (extension_name, E_SOURCE_EXTENSION_MAIL_IDENTITY) == 0)
3206 e_source_registry_set_default_mail_identity (
3207 registry, default_source);
3209 if (strcmp (extension_name, E_SOURCE_EXTENSION_MEMO_LIST) == 0)
3210 e_source_registry_set_default_memo_list (
3211 registry, default_source);
3213 if (strcmp (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0)
3214 e_source_registry_set_default_task_list (
3215 registry, default_source);