/* gcc -g -O2 -Wall `pkg-config --cflags --libs webkitgtk-3.0` test2.c -o test2 && ./test2 */

#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>

static gchar *
replace_string (gchar *text,
                const gchar *find,
                const gchar *replace)
{
	const gchar *p, *next;
	GString *str;
	gint find_len;

	g_return_val_if_fail (text != NULL, NULL);
	g_return_val_if_fail (find != NULL, NULL);
	g_return_val_if_fail (*find, NULL);

	find_len = strlen (find);
	str = g_string_new ("");

	p = text;
	while (next = strstr (p, find), next) {
		if (p < next)
			g_string_append_len (str, p, next - p);

		if (replace && *replace)
			g_string_append (str, replace);

		p = next + find_len;
	}

	g_string_append (str, p);
	g_free (text);

	return g_string_free (str, FALSE);
}

static gchar *
get_ui_definition (void)
{
	gchar *contents = NULL;

	g_assert (g_file_get_contents ("/usr/share/evolution/3.8/ui/eab-contact-duplicate-detected.ui", &contents, NULL, NULL));

	contents = replace_string (contents, "eab_contact_display_get_type", "webkit_web_view_get_type");
	contents = replace_string (contents, "EABContactDisplay", "WebkitWebView");

	return contents;
}

int main (int argc, char *argv[])
{
	GtkBuilder *builder;
	gchar *ui;
	GtkWidget *widget, *container;

	gtk_init (&argc, &argv);

	builder = gtk_builder_new ();

	ui = get_ui_definition ();
	gtk_builder_add_from_string (builder, ui, -1, NULL);
	g_free (ui);

	/* destroy/hide the second webview */
	widget = GTK_WIDGET (gtk_builder_get_object (builder, "custom-new-contact"));
	gtk_widget_destroy (widget);

	/* destroy/hide the first webview */
	/*widget = GTK_WIDGET (gtk_builder_get_object (builder, "custom-old-contact"));
	gtk_widget_destroy (widget);*/

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "dialog-duplicate-contact"));
	gtk_widget_ensure_style (widget);

	container = gtk_dialog_get_action_area (GTK_DIALOG (widget));
	gtk_container_set_border_width (GTK_CONTAINER (container), 12);

	container = gtk_dialog_get_content_area (GTK_DIALOG (widget));
	gtk_container_set_border_width (GTK_CONTAINER (container), 0);

	gtk_widget_show_all (widget);
	gtk_dialog_run (GTK_DIALOG (widget));
	
	g_object_unref (builder);

	return 0;
}
