<commit_before>/*
 * libopensync - A synchronization engine for the opensync framework
 * Copyright (C) 2004-2005  <NAME> <<EMAIL>>
 * Copyright (C) 2007-2008  <NAME> <<EMAIL>>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 * 
 */
 
#include "opensync.h"
#include "opensync_internals.h"

#include "opensync-client.h"
#include "opensync-engine.h"
#include "opensync-group.h"
#include "opensync-format.h"
#include "opensync-data.h"
#include "opensync-plugin.h"
#include "opensync-archive.h"
#include "opensync-merger.h"
#include "opensync-xmlformat.h"

#include "archive/opensync_archive_internals.h"
#include "client/opensync_client_proxy_internals.h"
#include "merger/opensync_merger_internals.h"
#include "group/opensync_group_internals.h"
#include "group/opensync_member_internals.h"

#include "opensync_status_internals.h"
#include "opensync_obj_engine_internals.h"
#include "opensync_sink_engine_internals.h"
#include "opensync_mapping_entry_engine_internals.h"

#include "opensync_engine.h"
#include "opensync_engine_private.h"
#include "opensync_engine_internals.h"


#ifdef OPENSYNC_UNITTESTS
#include "xmlformat/opensync-xmlformat_internals.h"
#endif

void osync_engine_set_error(OSyncEngine *engine, OSyncError *error)
{
	osync_assert(engine);
	if (engine->error) {
		osync_error_stack(&error, &engine->error);
		osync_error_unref(&engine->error);
	}
	
	engine->error = error;
	if (error)
		osync_error_ref(&error);
}
 
osync_bool osync_engine_has_error(OSyncEngine *engine)
{
	osync_assert(engine);
	return engine->error ? TRUE : FALSE;
}

static void _finalize_callback(OSyncClientProxy *proxy, void *userdata, OSyncError *error)
{
	OSyncEngine *engine = userdata;
	osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, proxy, userdata, error);
	
	if (error) {
		osync_engine_set_error(engine, error);
	}
	
	engine->busy = FALSE;
	
	osync_trace(TRACE_EXIT, "%s", __func__);
}

static gboolean _command_prepare(GSource *source, gint *timeout_)
{
	*timeout_ = 1;
	return FALSE;
}

static gboolean _command_check(GSource *source)
{
	OSyncEngine *engine = *((OSyncEngine **)(source + 1));
	if (g_async_queue_length(engine->command_queue) > 0)
		return TRUE;
	
	return FALSE;
}

void osync_engine_command(OSyncEngine *engine, OSyncEngineCommand *command);

static OSyncObjFormat *_osync_engine_get_internal_format(OSyncEngine *engine, const char *objtype)
{
	char *format = g_hash_table_lookup(engine->internalFormats, objtype);
	if (!format)
		return NULL;
	return osync_format_env_find_objformat(engine->formatenv, format);
}

static int _osync_engine_get_proxy_position(OSyncEngine *engine, OSyncClientProxy *proxy)
{
	int ret = 0;
	osync_assert(engine);
	osync_assert(proxy);

	ret = g_list_index(engine->proxies, proxy);

	osync_assert(ret >= 0);

	return ret;
}

static osync_bool _osync_engine_is_proxy_connected(OSyncEngine *engine, OSyncClientProxy *proxy)
{
	osync_assert(engine);
	osync_assert(proxy);

	return !!(engine->proxy_connects & (1 << _osync_engine_get_proxy_position(engine, proxy)));
}

static int _osync_engine_get_objengine_position(OSyncEngine *engine, OSyncObjEngine *objengine)
{
	int ret = 0;
	osync_assert(engine);
	osync_assert(objengine);

	ret = g_list_index(engine->object_engines, objengine);

	osync_assert(ret >= 0);

	return ret;
}

gboolean foreach_schema(void *key, void *value, void *userdata) {
	osync_xmlformat_schema_unref((OSyncXMLFormatSchema *)value);
	return TRUE;
}

static void _osync_engine_finalize_internal_schemas(OSyncEngine *engine)
{

	if ( engine->internalSchemas != NULL ) {
		g_hash_table_foreach_remove(engine->internalSchemas, foreach_schema, NULL);
	} 

}

static void _osync_engine_set_internal_schema(OSyncEngine *engine, const char *objtype, OSyncError **error) 
{
	OSyncXMLFormat *xmlformat = NULL;
	OSyncXMLFormatSchema *schema = NULL;
	osync_trace(TRACE_INTERNAL, "Setting internal schema for objtype %s", objtype);

	// init OSyncXMLFormatSchemas
	xmlformat = osync_xmlformat_new(objtype, NULL);
#ifdef OPENSYNC_UNITTESTS
	schema = osync_xmlformat_schema_get_instance_with_path(xmlformat, engine->schema_dir, error);
#else
	schema = osync_xmlformat_schema_get_instance(xmlformat, error);
#endif
	osync_xmlformat_unref(xmlformat);
	g_hash_table_insert(engine->internalSchemas, g_strdup(objtype), schema);
}

static void _osync_engine_set_internal_format(OSyncEngine *engine, const char *objtype, OSyncObjFormat *format)
{
	osync_trace(TRACE_INTERNAL, "Setting internal format of %s to %p:%s", objtype, format, osync_objformat_get_name(format));
	if (!format)
		return;
	g_hash_table_insert(engine->internalFormats, g_strdup(objtype), g_strdup(osync_objformat_get_name(format)));
}

static OSyncFormatConverterPath *_osync_engine_get_converter_path(OSyncEngine *engine, const char *member_objtype)
{
	OSyncFormatConverterPath *converter_path = g_hash_table_lookup(engine->converterPathes, member_objtype);
	return converter_path;
}

static void _osync_engine_set_converter_path(OSyncEngine *engine, const char *member_objtype, OSyncFormatConverterPath *converter_path)
{
	osync_trace(TRACE_INTERNAL, "Setting converter_path of %s to %p", member_objtype, converter_path);
	if (!converter_path)
		return;
	g_hash_table_insert(engine->converterPathes, g_strdup(member_objtype), converter_path);<commit_msg>Don't fail if hardcoded-"internal Formats" are installed.<commit_after>/*
 * libopensync - A synchronization engine for the opensync framework
 * Copyright (C) 2004-2005  <NAME> <<EMAIL>>
 * Copyright (C) 2007-2008  <NAME> <<EMAIL>>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 * 
 */
 
#include "opensync.h"
#include "opensync_internals.h"

#include "opensync-client.h"
#include "opensync-engine.h"
#include "opensync-group.h"
#include "opensync-format.h"
#include "opensync-data.h"
#include "opensync-plugin.h"
#include "opensync-archive.h"
#include "opensync-merger.h"
#include "opensync-xmlformat.h"

#include "archive/opensync_archive_internals.h"
#include "client/opensync_client_proxy_internals.h"
#include "merger/opensync_merger_internals.h"
#include "group/opensync_group_internals.h"
#include "group/opensync_member_internals.h"

#include "opensync_status_internals.h"
#include "opensync_obj_engine_internals.h"
#include "opensync_sink_engine_internals.h"
#include "opensync_mapping_entry_engine_internals.h"

#include "opensync_engine.h"
#include "opensync_engine_private.h"
#include "opensync_engine_internals.h"


#ifdef OPENSYNC_UNITTESTS
#include "xmlformat/opensync-xmlformat_internals.h"
#endif

void osync_engine_set_error(OSyncEngine *engine, OSyncError *error)
{
	osync_assert(engine);
	if (engine->error) {
		osync_error_stack(&error, &engine->error);
		osync_error_unref(&engine->error);
	}
	
	engine->error = error;
	if (error)
		osync_error_ref(&error);
}
 
osync_bool osync_engine_has_error(OSyncEngine *engine)
{
	osync_assert(engine);
	return engine->error ? TRUE : FALSE;
}

static void _finalize_callback(OSyncClientProxy *proxy, void *userdata, OSyncError *error)
{
	OSyncEngine *engine = userdata;
	osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, proxy, userdata, error);
	
	if (error) {
		osync_engine_set_error(engine, error);
	}
	
	engine->busy = FALSE;
	
	osync_trace(TRACE_EXIT, "%s", __func__);
}

static gboolean _command_prepare(GSource *source, gint *timeout_)
{
	*timeout_ = 1;
	return FALSE;
}

static gboolean _command_check(GSource *source)
{
	OSyncEngine *engine = *((OSyncEngine **)(source + 1));
	if (g_async_queue_length(engine->command_queue) > 0)
		return TRUE;
	
	return FALSE;
}

void osync_engine_command(OSyncEngine *engine, OSyncEngineCommand *command);

static OSyncObjFormat *_osync_engine_get_internal_format(OSyncEngine *engine, const char *objtype)
{
	char *format = g_hash_table_lookup(engine->internalFormats, objtype);
	if (!format)
		return NULL;
	return osync_format_env_find_objformat(engine->formatenv, format);
}

static int _osync_engine_get_proxy_position(OSyncEngine *engine, OSyncClientProxy *proxy)
{
	int ret = 0;
	osync_assert(engine);
	osync_assert(proxy);

	ret = g_list_index(engine->proxies, proxy);

	osync_assert(ret >= 0);

	return ret;
}

static osync_bool _osync_engine_is_proxy_connected(OSyncEngine *engine, OSyncClientProxy *proxy)
{
	osync_assert(engine);
	osync_assert(proxy);

	return !!(engine->proxy_connects & (1 << _osync_engine_get_proxy_position(engine, proxy)));
}

static int _osync_engine_get_objengine_position(OSyncEngine *engine, OSyncObjEngine *objengine)
{
	int ret = 0;
	osync_assert(engine);
	osync_assert(objengine);

	ret = g_list_index(engine->object_engines, objengine);

	osync_assert(ret >= 0);

	return ret;
}

gboolean foreach_schema(void *key, void *value, void *userdata) {
	osync_xmlformat_schema_unref((OSyncXMLFormatSchema *)value);
	return TRUE;
}

static void _osync_engine_finalize_internal_schemas(OSyncEngine *engine)
{

	if ( engine->internalSchemas != NULL ) {
		g_hash_table_foreach_remove(engine->internalSchemas, foreach_schema, NULL);
	} 

}

static void _osync_engine_set_internal_schema(OSyncEngine *engine, const char *objtype, OSyncError **error) 
{
	OSyncXMLFormat *xmlformat = NULL;
	OSyncXMLFormatSchema *schema = NULL;

	// init OSyncXMLFormatSchemas
	xmlformat = osync_xmlformat_new(objtype, NULL);
#ifdef OPENSYNC_UNITTESTS
	schema = osync_xmlformat_schema_get_instance_with_path(xmlformat, engine->schema_dir, error);
#else
	schema = osync_xmlformat_schema_get_instance(xmlformat, error);
#endif
	osync_xmlformat_unref(xmlformat);

	if (!schema)
		return;

	osync_trace(TRACE_INTERNAL, "Setting internal schema for objtype %s", objtype);

	g_hash_table_insert(engine->internalSchemas, g_strdup(objtype), schema);
}

static void _osync_engine_set_internal_format(OSyncEngine *engine, const char *objtype, OSyncObjFormat *format)
{
	if (!format)
		return;

	osync_trace(TRACE_INTERNAL, "Setting internal format of %s to %p:%s", objtype, format, osync_objformat_get_name(format));
	g_hash_table_insert(engine->internalFormats, g_strdup(objtype), g_strdup(osync_objformat_get_name(format)));
}

static OSyncFormatConverterPath *_osync_engine_get_converter_path(OSyncEngine *engine, const char *member_objtype)
{
	OSyncFormatConverterPath *converter_path = g_hash_table_lookup(engine->converterPathes, member_objtype);
	return converter_path;
}

static void _osync_engine_set_converter_path(OSyncEngine *engine, const char *member_objtype, OSyncFormatConverterPath *converter_path)
{
	osync_trace(TRACE_INTERNAL, "Setting converter_path of %s to %p", member_objtype, converter_path);
	if (!converter_path)
		return;
	g_hash_table_insert(engine->converterPathes, g_strdup(member_objtype), converter_path);
{'fraction_non_alphanumeric': 0.10396204428167138, 'fraction_numerical': 0.00665889795238888, 'mean_word_length': 6.736638763683194, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '799244', 'n_tokens_mistral': 4227, 'n_tokens_neox': 4068, 'n_words': 934}