pidgin/pidgin

755e1554051c
Parents 294d7d9c1cd6
Children cbfcefb3a34d
Make user interfaces setup the default history adapter.

This helps avoid some issues with the unit tests as well as gives us more
flexibility in the future.

Testing Done:
Ran the unit tests without issue. Ran Pidgin 3 with no existing config directory and verified that `history.db` was created properly.

Reviewed at https://reviews.imfreedom.org/r/1033/
--- a/finch/gntui.c Mon Oct 18 01:51:02 2021 -0500
+++ b/finch/gntui.c Mon Oct 18 02:01:37 2021 -0500
@@ -42,8 +42,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());
@@ -52,6 +85,12 @@
gnt_init();
#endif /* STANDALONE */
+ if(!finch_history_init(&error)) {
+ g_critical("failed to initialize the history api: %s",
+ error != NULL ? error->message : "unknown");
+ g_clear_error(&error);
+ }
+
purple_prefs_add_none("/purple/gnt");
/* Accounts */
--- a/libpurple/purplehistorymanager.c Mon Oct 18 01:51:02 2021 -0500
+++ b/libpurple/purplehistorymanager.c Mon Oct 18 02:01:37 2021 -0500
@@ -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);
}
}
--- a/libpurple/tests/test_ui.c Mon Oct 18 01:51:02 2021 -0500
+++ b/libpurple/tests/test_ui.c Mon Oct 18 02:01:37 2021 -0500
@@ -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();
--- a/pidgin/libpidgin.c Mon Oct 18 01:51:02 2021 -0500
+++ b/pidgin/libpidgin.c Mon Oct 18 02:01:37 2021 -0500
@@ -190,11 +190,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 than 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();
@@ -216,6 +251,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. */