/* Entry point for this file, here we take care of * creating the addressbook if it doesnt exist, * getting an EBookClient, and creating our EBookClientCursor. */ EBookClient * cursor_load_data (const gchar *vcard_path, EBookClientCursor **ret_cursor) { ESourceRegistry *registry; ESource *scratch; ESourceBackend *backend = NULL; GMainLoop *loop; GError *error = NULL; EBookClient *ret_book; g_return_val_if_fail (vcard_path != NULL, NULL); g_return_val_if_fail (ret_cursor != NULL, NULL); g_print ("Cursor loading data from %s\n", vcard_path); loop = g_main_loop_new (NULL, FALSE); registry = e_source_registry_new_sync (NULL, &error); if (!registry) g_error ("Unable to create the registry: %s", error->message); /* Listen to the registry for our added source */ g_signal_connect ( registry, "source-added", G_CALLBACK (cursor_data_source_added), loop); /* Now create a scratch source for our addressbook */ scratch = e_source_new_with_uid (CURSOR_DATA_SOURCE_ID, NULL, &error); /* Ensure the new ESource will be a local addressbook source */ backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK); e_source_backend_set_backend_name (backend, "local"); /* Now is the right time to use the ESourceBackendSummarySetup to configure * your newly created addressbook. This configuration should happen on the * scratch source before calling e_source_registry_commit_source_sync(). */ /* Commit the source to the registry */ if (!e_source_registry_commit_source_sync (registry, scratch, NULL, &error)) { /* It's possible the source already exists if we already ran the example with this data server */ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) { /* If so... then just call our callback early */ ESource *source = e_source_registry_ref_source (registry, CURSOR_DATA_SOURCE_ID); g_clear_error (&error); g_return_val_if_fail (E_IS_SOURCE (source), NULL); /* Run the callback which creates the addressbook client connection */ cursor_data_source_added (registry, source, NULL); g_object_unref (source); } else g_error ("Unable to add new addressbook source to the registry: %s", error->message); } g_object_unref (scratch); /* Give EDS a little time to actually create the ESource remotely and * also have a copy if it cached locally, wait for the "source-added" * signal. */ if (address_book == NULL) { g_timeout_add_seconds (20, cursor_data_source_timeout, NULL); g_main_loop_run (loop); /* By now we aborted or we have an addressbook created */ g_return_val_if_fail (address_book != NULL, NULL); } /********************************************************** * Ok, done with creating an addressbook, let's add data * **********************************************************/ load_contacts (address_book, vcard_path); /* Addressbook should have contacts now, let's create the cursor */ *ret_cursor = get_cursor (address_book); /* Cleanup some resources we used to populate the addressbook */ g_main_loop_unref (loop); g_object_unref (address_book_source); g_object_unref (registry); /* Give the ref through the return value*/ ret_book = address_book; address_book_source = NULL; address_book = NULL; /* Return the addressbook */ return ret_book; }
static gboolean migrate_mbox_to_maildir (EShell *shell, CamelSession *session, ESource *mbox_source) { ESourceRegistry *registry; ESourceExtension *extension; const gchar *extension_name; CamelService *mbox_service = NULL; CamelService *maildir_service = NULL; CamelSettings *settings; const gchar *data_dir; const gchar *mbox_uid; gchar *path; struct MigrateStore ms; GThread *thread; GError *error = NULL; registry = e_shell_get_registry (shell); data_dir = camel_session_get_user_data_dir (session); mbox_uid = e_source_get_uid (mbox_source); e_source_set_display_name (mbox_source, "local_mbox"); extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT; extension = e_source_get_extension (mbox_source, extension_name); e_source_backend_set_backend_name ( E_SOURCE_BACKEND (extension), "mbox"); extension_name = e_source_camel_get_extension_name ("mbox"); extension = e_source_get_extension (mbox_source, extension_name); settings = e_source_camel_get_settings (E_SOURCE_CAMEL (extension)); path = g_build_filename (data_dir, mbox_uid, NULL); g_object_set (settings, "path", path, NULL); g_free (path); e_source_registry_commit_source_sync ( registry, mbox_source, NULL, &error); if (error == NULL) mbox_service = camel_session_add_service ( session, mbox_uid, "mbox", CAMEL_PROVIDER_STORE, &error); if (error == NULL) maildir_service = camel_session_add_service ( session, "local", "maildir", CAMEL_PROVIDER_STORE, &error); if (error != NULL) { if (mbox_service != NULL) g_object_unref (mbox_service); if (maildir_service != NULL) g_object_unref (maildir_service); g_warning ("%s: %s", G_STRFUNC, error->message); g_error_free (error); return FALSE; } g_return_val_if_fail (CAMEL_IS_STORE (mbox_service), FALSE); g_return_val_if_fail (CAMEL_IS_STORE (maildir_service), FALSE); camel_service_set_settings (mbox_service, settings); settings = camel_service_ref_settings (maildir_service); path = g_build_filename (data_dir, "local", NULL); g_object_set (settings, "path", path, NULL); if (g_mkdir (path, 0700) == -1) g_warning ( "%s: Failed to make directory '%s': %s", G_STRFUNC, path, g_strerror (errno)); g_free (path); g_object_unref (settings); ms.mail_store = CAMEL_STORE (mbox_service); ms.maildir_store = CAMEL_STORE (maildir_service); ms.session = session; ms.complete = FALSE; thread = g_thread_new (NULL, (GThreadFunc) migrate_stores, &ms); /* coverity[loop_condition] */ while (!ms.complete) g_main_context_iteration (NULL, TRUE); g_object_unref (mbox_service); g_object_unref (maildir_service); g_thread_unref (thread); return TRUE; }