ews_initial_setup_with_connection_sync (CamelStore *store,
					GHashTable *save_setup,
					EEwsConnection *connection,
					GCancellable *cancellable,
					GError **error)
{
	CamelEwsStore *ews_store;
	GSList *folders = NULL, *folder_ids = NULL;
	gint nn;
	GError *local_error = NULL;

	g_return_val_if_fail (CAMEL_IS_EWS_STORE (store), FALSE);

	if (g_cancellable_set_error_if_cancelled (cancellable, error))
		return FALSE;

	ews_store = CAMEL_EWS_STORE (store);
	if (connection) {
		g_object_ref (connection);
	} else {
		if (!camel_ews_store_connected (ews_store, cancellable, error))
			return FALSE;

		connection = camel_ews_store_ref_connection (ews_store);
		g_return_val_if_fail (connection != NULL, FALSE);
	}

	for (nn = 0; nn < G_N_ELEMENTS (system_folder); nn++) {
		EwsFolderId *fid = NULL;

		fid = g_new0 (EwsFolderId, 1);
		fid->id = g_strdup (system_folder[nn].dist_folder_id);
		fid->is_distinguished_id = TRUE;

		folder_ids = g_slist_append (folder_ids, fid);
	}

	/* fetch system folders first using getfolder operation */
	if (!e_ews_connection_get_folder_sync (
		connection, EWS_PRIORITY_MEDIUM, "IdOnly",
		NULL, folder_ids, &folders,
		cancellable, &local_error)) {
		g_clear_object (&connection);
		g_propagate_error (error, local_error);
		return FALSE;
	}

	if (folders && (g_slist_length (folders) != G_N_ELEMENTS (system_folder)))
		d (printf ("Error : not all folders are returned by getfolder operation"));
	else if (!local_error && folders)
		ews_store_set_flags (ews_store, folders);
	else if (local_error) {
		/* report error and make sure we are not leaking anything */
		g_warn_if_fail (folders == NULL);
	} else
		d (printf ("folders for respective distinguished ids don't exist"));

	if (save_setup) {
		gchar *folder_id;

		folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (ews_store->summary, CAMEL_FOLDER_TYPE_SENT);
		if (folder_id) {
			gchar *fullname;

			fullname = camel_ews_store_summary_get_folder_full_name (ews_store->summary, folder_id, NULL);
			if (fullname && *fullname) {
				g_hash_table_insert (save_setup,
					g_strdup (CAMEL_STORE_SETUP_SENT_FOLDER),
					g_strdup (fullname));
			}

			g_free (fullname);
			g_free (folder_id);
		}

		if (g_slist_length (folders) == G_N_ELEMENTS (system_folder)) {
			gint ii;

			for (ii = 0; ii < G_N_ELEMENTS (system_folder); ii++) {
				if (g_str_equal ("drafts", system_folder[ii].dist_folder_id)) {
					break;
				}
			}

			if (ii < G_N_ELEMENTS (system_folder)) {
				EEwsFolder *drafts = g_slist_nth (folders, ii)->data;
				if (drafts && !e_ews_folder_is_error (drafts)) {
					const EwsFolderId *fid = e_ews_folder_get_id (drafts);

					if (fid && fid->id) {
						gchar *fullname;

						fullname = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid->id, NULL);
						if (fullname && *fullname) {
							g_hash_table_insert (save_setup,
								g_strdup (CAMEL_STORE_SETUP_DRAFTS_FOLDER),
								g_strdup (fullname));
						}

						g_free (fullname);
					}
				}
			}
		}
	}

	g_slist_free_full (folders, g_object_unref);
	g_slist_free_full (folder_ids, (GDestroyNotify) e_ews_folder_id_free);
	g_clear_object (&connection);
	g_clear_error (&local_error);

	return TRUE;
}