static EUri *
ep_keyring_uri_new (const gchar *string,
                    GError **error)
{
	EUri *uri;

	uri = e_uri_new (string);
	g_return_val_if_fail (uri != NULL, NULL);

	/* LDAP URIs do not have usernames, so use the URI as the username. */
	if (uri->user == NULL && uri->protocol != NULL &&
			(strcmp (uri->protocol, "ldap") == 0|| strcmp (uri->protocol, "google") == 0))
		uri->user = g_strdelimit (g_strdup (string), "/=", '_');

	/* Make sure the URI has the required components. */
	if (uri->user == NULL && uri->host == NULL) {
		g_set_error_literal (
			error, EP_KEYRING_ERROR,
			GNOME_KEYRING_RESULT_BAD_ARGUMENTS,
			_("Keyring key is unusable: no user or host name"));
		e_uri_free (uri);
		uri = NULL;
	}

	return uri;
}
static ESourceGroup *
create_group (EAccount * account, const char *type_string)
{
    ESourceGroup *gfake;
    char *uid;
    char *base_uri;
    xmlDocPtr doc;
    xmlNodePtr root;
    EUri *euri;
    const char *surl;

    uid = g_strdup_printf ("%s@%s", type_string, account->uid);

    doc = xmlNewDoc ("1.0");
    root = xmlNewDocNode (doc, NULL, "group", NULL);
    xmlDocSetRootElement (doc, root);

    surl = e_account_get_string (account, E_ACCOUNT_SOURCE_URL);
    euri = e_uri_new (surl);
    base_uri = create_base_uri (euri);
    e_uri_free (euri);

    xmlSetProp (root, "uid", uid);
    xmlSetProp (root, "name", account->name);
    xmlSetProp (root, "base_uri", base_uri);

    gfake = e_source_group_new_from_xmldoc (doc);
    g_free (base_uri);
    g_free (uid);
    xmlFreeDoc (doc);

    return gfake;
}
static void
create_ldap_source (EAccount * account, ESourceGroup * group)
{
    ESource *source;
    EUri *euri;
    char *uri;
    char *uid;
    xmlNodePtr node;

    euri = e_uri_new (account->source->url);

    uri = g_strdup_printf ("ldap://%s:389/o=Scalix?sub", euri->host);
    uid = g_strdup_printf ("system-ldap@%s", account->uid);

    node = xmlNewNode (NULL, "source");
    xmlSetProp (node, "uid", uid);
    xmlSetProp (node, "name", _("System"));
    xmlSetProp (node, "uri", uri);

    source = e_source_new_from_xml_node (node);
    xmlFreeNode (node);
    e_source_set_property (source, "completion", "TRUE");

    if (!e_source_group_add_source (group, source, 1)) {
        g_print ("Could not add ldap source\n");
    }

    g_free (uri);
    g_free (uid);
    e_uri_free (euri);
}
static void
addressbook_authenticate (EBook *book,
			  gpointer data)
{
	gchar *auth;
	gchar *user;
	gchar *passwd;
	gchar *str_uri;
	gchar *pass_key;
	gchar *auth_domain;
	gchar *component_name;
	EUri *e_uri;

	ESource *source = (ESource *)data;

	auth = (gchar *)e_source_get_property (source, "auth");
	auth_domain = (gchar *)e_source_get_property (source, "auth-domain");
	component_name = auth_domain ? auth_domain : "Addressbook";

	if (auth && !strcmp ("plain/password", auth))
		user = (gchar *)e_source_get_property (source, "user");
	else
		user = (gchar *)e_source_get_property (source, "email_addr");
	if (!user)
		user = "";

	str_uri = e_source_get_uri (source);
	e_uri = e_uri_new (str_uri);
	pass_key = e_uri_to_string (e_uri, FALSE);
	e_uri_free (e_uri);

	passwd = e_passwords_get_password (component_name, pass_key);
	if (passwd)
		passwd = "";

	if (book)
		if (!e_book_authenticate_user (book, user, passwd, auth, NULL))
			LOG (g_warning ("Authentication failed"));
	g_free (pass_key);
	g_free (str_uri);

	return;
}
static EPublishUri *
migrateURI (const gchar *xml, xmlDocPtr doc)
{
	GConfClient *client;
	GSList *uris, *l, *events = NULL;
	xmlChar *location, *enabled, *frequency, *username;
	xmlNodePtr root, p;
	EPublishUri *uri;
	gchar *password, *temp;
	EUri *euri;

	client = gconf_client_get_default ();
	uris = gconf_client_get_list (client, "/apps/evolution/calendar/publish/uris", GCONF_VALUE_STRING, NULL);
	l = uris;
	while (l && l->data) {
		gchar *str = l->data;
		if (strcmp (xml, str) == 0) {
			uris = g_slist_remove (uris, str);
			g_free (str);
		}
		l = g_slist_next (l);
	}

	uri = g_new0 (EPublishUri, 1);

	root = doc->children;
	location = xmlGetProp (root, (const unsigned char *)"location");
	enabled = xmlGetProp (root, (const unsigned char *)"enabled");
	frequency = xmlGetProp (root, (const unsigned char *)"frequency");
	username = xmlGetProp (root, (const unsigned char *)"username");

	euri = e_uri_new ((const char *)location);

	if (!euri) {
		g_warning ("Could not form the uri for %s \n", location);
		goto cleanup;
	}

	if (euri->user)
		g_free (euri->user);

	euri->user = g_strdup ((const char *)username);

	temp = e_uri_to_string (euri, FALSE);
	uri->location = g_strdup_printf ("dav://%s", strstr (temp, "//") + 2);
	g_free (temp);
	e_uri_free (euri);

	if (enabled != NULL)
		uri->enabled = atoi ((char *)enabled);
	if (frequency != NULL)
		uri->publish_frequency = atoi ((char *)frequency);
	uri->publish_format = URI_PUBLISH_AS_FB;

	password = e_passwords_get_password ("Calendar", (char *)location);
	if (password) {
		e_passwords_forget_password ("Calendar", (char *)location);
		e_passwords_add_password (uri->location, password);
		e_passwords_remember_password ("Calendar", uri->location);
	}

	for (p = root->children; p != NULL; p = p->next) {
		xmlChar *uid = xmlGetProp (p, (const unsigned char *)"uid");
		if (strcmp ((char *)p->name, "source") == 0) {
			events = g_slist_append (events, uid);
		} else {
			g_free (uid);
		}
	}
	uri->events = events;

	uris = g_slist_prepend (uris, e_publish_uri_to_xml (uri));
	gconf_client_set_list (client, "/apps/evolution/calendar/publish/uris", GCONF_VALUE_STRING, uris, NULL);
	g_slist_foreach (uris, (GFunc) g_free, NULL);
	g_slist_free (uris);
	g_object_unref (client);

cleanup:
	xmlFree (location);
	xmlFree (enabled);
	xmlFree (frequency);
	xmlFree (username);
	xmlFreeDoc (doc);

	return uri;
}