# HG changeset patch
# Node ID 5e8ba2bb8acc
# Parent  741992355ead
diff --git a/finch/gntui.c b/finch/gntui.c
--- a/finch/gntui.c
+++ b/finch/gntui.c
@@ -43,8 +43,41 @@
 #include "gntroomlist.h"
 #include "gntstatus.h"
 
+static gboolean
+finch_history_init(GError **error) {
+	PurpleHistoryManager *manager = NULL;
+	PurpleHistoryAdapter *adapter = NULL;
+	gchar *filename = NULL;
+	const gchar *id = NULL;
+
+	manager = purple_history_manager_get_default();
+
+	/* Attempt to create the config directory. */
+	g_mkdir_with_parents(purple_config_dir(), 0700);
+
+	filename = g_build_filename(purple_config_dir(), "history.db", NULL);
+	adapter = purple_sqlite_history_adapter_new(filename);
+	g_free(filename);
+
+	id = purple_history_adapter_get_id(adapter);
+	if(!purple_history_manager_register(manager, adapter, error)) {
+		g_clear_object(&adapter);
+
+		return FALSE;
+	}
+
+	/* The manager adds a ref to the adapter on registration, so we can remove
+	 * our reference.
+	 */
+	g_clear_object(&adapter);
+
+	return purple_history_manager_set_active(manager, id, error);
+}
+
 void finch_ui_init()
 {
+	GError *error = NULL;
+
 #ifdef STANDALONE
 #ifdef _WIN32 /* TODO: don't change it when using FHS under win32 */
 	gnt_set_config_dir(purple_user_dir());
@@ -53,6 +86,12 @@
 	gnt_init();
 #endif /* STANDALONE */
 
+	if(!finch_history_init(&error)) {
+		g_critical("failed initialize the history api: %s",
+		           error != NULL ? error->message : "unknown");
+		g_clear_error(&error);
+	}
+
 	purple_prefs_add_none("/purple/gnt");
 
 	/* Accounts */
diff --git a/libpurple/purplehistorymanager.c b/libpurple/purplehistorymanager.c
--- a/libpurple/purplehistorymanager.c
+++ b/libpurple/purplehistorymanager.c
@@ -148,40 +148,7 @@
 void
 purple_history_manager_startup(void) {
 	if(default_manager == NULL) {
-		PurpleHistoryAdapter *adapter = NULL;
-		GError *error = NULL;
-		gchar *filename = NULL;
-
-		filename = g_build_filename(purple_config_dir(), "history.db", NULL);
-		adapter = purple_sqlite_history_adapter_new(filename);
-		g_free(filename);
-
 		default_manager = g_object_new(PURPLE_TYPE_HISTORY_MANAGER, NULL);
-		if(!purple_history_manager_register(default_manager, adapter, &error)) {
-			if(error != NULL) {
-				g_warning("Failed to register sqlite history adapter: %s", error->message);
-				g_clear_error(&error);
-			} else {
-				g_warning("Failed to register sqlite history adapter: Unknown reason");
-			}
-
-			g_clear_object(&adapter);
-
-			return;
-		}
-
-		purple_history_manager_set_active(default_manager,
-		                                  purple_history_adapter_get_id(adapter),
-		                                  &error);
-
-		if(error != NULL) {
-			g_warning("Failed to activate %s: %s",
-			          purple_history_adapter_get_id(adapter), error->message);
-
-			g_clear_error(&error);
-		}
-
-		g_clear_object(&adapter);
 	}
 }
 
diff --git a/libpurple/tests/test_ui.c b/libpurple/tests/test_ui.c
--- a/libpurple/tests/test_ui.c
+++ b/libpurple/tests/test_ui.c
@@ -65,8 +65,35 @@
 	.ui_init = test_ui_init
 };
 
+static gboolean
+test_ui_init_history(GError **error) {
+	PurpleHistoryManager *manager = NULL;
+	PurpleHistoryAdapter *adapter = NULL;
+	const gchar *id = NULL;
+
+	manager = purple_history_manager_get_default();
+
+	adapter = purple_sqlite_history_adapter_new(":memory:");
+	id = purple_history_adapter_get_id(adapter);
+
+	if(!purple_history_manager_register(manager, adapter, error)) {
+		g_clear_object(&adapter);
+
+		return FALSE;
+	}
+
+	/* The manager adds a ref to the adapter on registration, so we can remove
+	 * our reference.
+	 */
+	g_clear_object(&adapter);
+
+	return purple_history_manager_set_active(manager, id, error);
+}
+
 void
 test_ui_purple_init(void) {
+	GError *error = NULL;
+
 #ifndef _WIN32
 	/* libpurple's built-in DNS resolution forks processes to perform
 	 * blocking lookups without blocking the main process.  It does not
@@ -79,9 +106,6 @@
 	/* set the magic PURPLE_PLUGINS_SKIP environment variable */
 	g_setenv("PURPLE_PLUGINS_SKIP", "1", TRUE);
 
-	/* Set a custom user directory (optional) */
-	purple_util_set_user_dir(TEST_DATA_DIR);
-
 	/* We do not want any debugging for now to keep the noise to a minimum. */
 	purple_debug_set_enabled(FALSE);
 
@@ -104,11 +128,11 @@
 		abort();
 	}
 
-	/* Set path to search for plugins. The core (libpurple) takes care of loading the
-	 * core-plugins, which includes the in-tree protocols. So it is not essential to add
-	 * any path here, but it might be desired, especially for ui-specific plugins. */
-	purple_plugins_add_search_path(TEST_DATA_DIR);
-	purple_plugins_refresh();
+	if(!test_ui_init_history(&error)) {
+		g_critical("failed to initialize the history api: %s",
+		           error ? error->message : "unknown");
+		g_clear_error(&error);
+	}
 
 	/* Load the preferences. */
 	purple_prefs_load();
diff --git a/pidgin/libpidgin.c b/pidgin/libpidgin.c
--- a/pidgin/libpidgin.c
+++ b/pidgin/libpidgin.c
@@ -191,11 +191,46 @@
 	purple_debug_set_ui(PURPLE_DEBUG_UI(ui));
 }
 
+static gboolean
+pidgin_history_init(GError **error) {
+	PurpleHistoryManager *manager = NULL;
+	PurpleHistoryAdapter *adapter = NULL;
+	gchar *filename = NULL;
+	const gchar *id = NULL;
+
+	manager = purple_history_manager_get_default();
+
+	/* Attempt to create the config_dir. We don't care about the result as the
+	 * logging adapter will fail with a better error then us failing to create
+	 * the directory.
+	 */
+	g_mkdir_with_parents(purple_config_dir(), 0700);
+
+	filename = g_build_filename(purple_config_dir(), "history.db", NULL);
+	adapter = purple_sqlite_history_adapter_new(filename);
+	g_free(filename);
+
+	id = purple_history_adapter_get_id(adapter);
+	if(!purple_history_manager_register(manager, adapter, error)) {
+		g_clear_object(&adapter);
+
+		return FALSE;
+	}
+
+	/* The manager adds a ref to the adapter on registration, so we can remove
+	 * our reference.
+	 */
+	g_clear_object(&adapter);
+
+	return purple_history_manager_set_active(manager, id, error);
+}
+
 static void
 pidgin_ui_init(void)
 {
 	PurpleProtocolManager *protocol_manager = NULL;
 	GtkIconTheme *theme = NULL;
+	GError *error = NULL;
 	gchar *path;
 
 	theme = gtk_icon_theme_get_default();
@@ -217,6 +252,12 @@
 	purple_protocol_manager_foreach(protocol_manager,
 	                                purple_ui_protocol_foreach_theme_cb, NULL);
 
+	if(!pidgin_history_init(&error)) {
+		g_critical("failed to initialize the history api: %s",
+		           error != NULL ? error->message : "unknown");
+		g_clear_error(&error);
+	}
+
 	pidgin_stock_init();
 
 	/* Set the UI operation structures. */
