static void
impl_setLineStatus(PortableServer_Servant servant, GNOME_Evolution_ShellState shell_state, GNOME_Evolution_Listener listener, CORBA_Environment *ev)
{
	struct _setline_data *sd;
	int status = status_check(shell_state);

	/* This will dis/enable further auto-mail-check action. */
	/* FIXME: If send/receive active, wait for it to finish? */
	if (status)
		camel_session_set_online(session, status);

	sd = g_malloc0(sizeof(*sd));
	sd->status = status;
	sd->listener = CORBA_Object_duplicate(listener, ev);
	if (ev->_major == CORBA_NO_EXCEPTION)
		mail_component_stores_foreach(mail_component_peek(), setline_check, sd);
	else
		CORBA_exception_free(ev);

	if (sd->pending == 0) {
		if (sd->listener) {
			CORBA_Object_release(sd->listener, ev);
			CORBA_exception_free(ev);
		}

		g_free(sd);

		if (!status)
			camel_session_set_online(session, status);
		GNOME_Evolution_Listener_complete(listener, ev);
	}
}
static void
setline_done(CamelStore *store, void *data)
{
	struct _setline_data *sd = data;

	g_return_if_fail (sd->pending > 0);

	sd->pending--;
	if (sd->pending == 0) {
		CORBA_Environment ev = { NULL };

		GNOME_Evolution_Listener_complete(sd->listener, &ev);
		CORBA_exception_free(&ev);
		CORBA_Object_release(sd->listener, &ev);
		CORBA_exception_free(&ev);
		if (!sd->status)
			camel_session_set_online(session, sd->status);
		g_free(sd);
	}
}
/* The worker thread function with all the complex
 * logic in it! Big fat beast! Please watch your
 * children ... */
static void
synch_worker (struct account_synch_msg *m)
{
    ScalixAccountSynch *sxas;
    ScalixAccountSynchPrivate *priv;
    CamelException ex;
    CamelStore *store;
    CamelFolderInfo *ftree;
    CamelFolderInfo *sf;
    CamelSession *cs;
    const char *uri;
    gboolean res;
    char *sversion;
    char *markup;

    sxas = SCALIX_ACCOUNT_SYNCH (m->sxas);
    priv = SCALIX_ACCOUNT_SYNCH_GET_PRIVATE (sxas);

    uri = e_account_get_string (priv->account, E_ACCOUNT_SOURCE_URL);

    g_print ("SxAS: starting synch for %s\n", uri);

    signal_progress (sxas, 1, _("Trying to connect to server"));

    camel_exception_init (&ex);
    cs = scalix_camel_session_get_default ();
    camel_session_set_online (cs, TRUE);

    store = camel_session_get_store (cs, uri, &ex);

    if (store == NULL) {
        signal_error (sxas, _("Authentication failed. Check for correct hostname, username and password."));
        return;
    }

    /* Must be online! */
    camel_offline_store_set_network_state (CAMEL_OFFLINE_STORE (store),
                                           CAMEL_OFFLINE_STORE_NETWORK_AVAIL,
                                           &ex);

    camel_service_connect (CAMEL_SERVICE (store), &ex);

    if (camel_exception_is_set (&ex)) {
        signal_error (sxas, _("Error while trying to connect to server."));
        return;
    }

    /* Always check for minimal required server version  */
    signal_progress (sxas, 10, _("Checking version of Scalix server"));
    g_print ("SxAS: Checking for minimal server version\n");

    sversion = NULL;
    camel_object_get (store, NULL,
                      CAMEL_SCALIX_STORE_SERVER_VERSION, &sversion, NULL);

    res = scalix_check_min_server_version (sversion);

    if (res == FALSE) {
        signal_error (sxas, _("Wrong version of Scalix server detected"));
        return;
    }

    m->sversion = g_strdup (sversion);
    g_print ("SxAS: sversion:%s\n", m->sversion);

    markup = g_markup_printf_escaped (MSG_SVER, sversion);
    signal_info (sxas, markup);
    g_free (markup);

    signal_progress (sxas, 20, _("Getting list of folders"));
    ftree = camel_store_get_folder_info (store,
                                         NULL,
                                         CAMEL_STORE_FOLDER_INFO_RECURSIVE |
                                         CAMEL_SCALIX_STORE_SHOW_SFOLDER, &ex);

    if (ftree == NULL) {
        camel_object_unref (store);
        signal_error (sxas, _("Could not obtain folder listening"));
        return;

    }

    /* Calendar  */
    signal_progress (sxas, 30, _("Loading calendar data..."));
    synch_data (sxas, ftree, CAMEL_SCALIX_FOLDER_CALENDAR, 30);

    m->esg_cals = create_group (priv->account, "Calendar");
    sync_source_group (priv->account,
                       m->esg_cals, CAMEL_SCALIX_FOLDER_CALENDAR, ftree);

    /* Contacts */
    signal_progress (sxas, 60, _("Loading contacts..."));
    synch_data (sxas, ftree, CAMEL_SCALIX_FOLDER_CONTACT, 60);

    m->esg_cnts = create_group (priv->account, "Contacts");
    sync_source_group (priv->account,
                       m->esg_cnts, CAMEL_SCALIX_FOLDER_CONTACT, ftree);

    create_ldap_source (priv->account, m->esg_cnts);

    /* Sent Items and Drafts Folder */
    if (priv->synch_dfolder) {
        char *url_str;
	CamelURL *url;

	url = camel_url_new (uri, NULL);

        signal_progress (sxas, 95, _("Synchronizing additional data"));

        sf = folder_tree_find_special (ftree, CAMEL_SCALIX_FOLDER_SENT);

        if (url && sf) {
	    url_str = url_set_path_and_get_string (url, sf->full_name);

            e_account_set_string (priv->account,
                                  E_ACCOUNT_SENT_FOLDER_URI, url_str);
            g_free (url_str);
        }

        sf = folder_tree_find_special (ftree, CAMEL_SCALIX_FOLDER_DRAFTS);

        if (url && sf) {
	    url_str = url_set_path_and_get_string (url, sf->full_name);
            e_account_set_string (priv->account,
                                  E_ACCOUNT_DRAFTS_FOLDER_URI, url_str);

            g_free (url_str);
        }

        if (url) {
            gboolean save_pw;

            camel_url_set_path (url, NULL);

            if (url->authmech == NULL) {
		    camel_url_set_authmech (url, "PLAIN");
            }

	    url_str = camel_url_to_string (url, 0);
            e_account_set_string (priv->account,
                                  E_ACCOUNT_TRANSPORT_URL, url_str);

            save_pw = e_account_get_bool (priv->account,
                                          E_ACCOUNT_SOURCE_SAVE_PASSWD);

            e_account_set_bool (priv->account,
                                E_ACCOUNT_TRANSPORT_SAVE_PASSWD, save_pw);
        }

        if (url) {
		camel_url_free (url);
        }
    }

    m->success = TRUE;

    g_print ("SxAS: DONE!\n");

    camel_object_unref (store);
    signal_progress (sxas, 100, _("Done loading"));
    signal_info (sxas, _("Initial loading complete. Please click Forward."));
    g_print ("SxAS: Freeing DONE\n");
    return;
}