# HG changeset patch
# Node ID e5643075bea3
# Parent  dcdf8a9a686b
diff --git a/libpurple/purplepresencemanager.c b/libpurple/purplepresencemanager.c
--- a/libpurple/purplepresencemanager.c
+++ b/libpurple/purplepresencemanager.c
@@ -19,6 +19,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
+
+#include <glib/gi18n-lib.h>
+
 #define G_SETTINGS_ENABLE_BACKEND
 #include <gio/gsettingsbackend.h>
 
@@ -170,6 +173,36 @@
 	GStrv ids = NULL;
 
 	ids = g_settings_get_strv(manager->settings, "presences");
+
+	/* If we don't have any existing presences, create an available one. */
+	if(ids[0] == NULL) {
+		PurpleSavedPresence *presence = NULL;
+		const char *id = NULL;
+
+		/* We aren't going to use the ids anymore so free them right away. */
+		g_strfreev(ids);
+
+		/* Create the default available presence and set it as active. */
+		presence = purple_presence_manager_create(manager);
+		purple_saved_presence_set_name(presence, _("Available"));
+		purple_saved_presence_set_primitive(presence,
+		                                    PURPLE_PRESENCE_PRIMITIVE_AVAILABLE);
+
+		id = purple_saved_presence_get_id(presence);
+		purple_presence_manager_set_active(manager, id);
+
+		g_clear_object(&presence);
+
+		/* Create the default offline presence as well. */
+		presence = purple_presence_manager_create(manager);
+		purple_saved_presence_set_name(presence, _("Offline"));
+		purple_saved_presence_set_primitive(presence,
+		                                    PURPLE_PRESENCE_PRIMITIVE_OFFLINE);
+		g_clear_object(&presence);
+
+		return;
+	}
+
 	for(int i = 0; ids[i] != NULL; i++) {
 		PurpleSavedPresence *presence = NULL;
 		GSettings *settings = NULL;
@@ -489,7 +522,11 @@
 	if(g_set_object(&manager->active, presence)) {
 		g_object_notify_by_pspec(G_OBJECT(manager), properties[PROP_ACTIVE]);
 
-		g_settings_set_string(manager->settings, "active", id);
+		/* g_settings_set_string can't handle nulls, so use an empty string if
+		 * it is null.
+		 */
+		g_settings_set_string(manager->settings, "active",
+		                      id == NULL ? "" : id);
 	}
 
 	return TRUE;
diff --git a/libpurple/tests/test_presence_manager.c b/libpurple/tests/test_presence_manager.c
--- a/libpurple/tests/test_presence_manager.c
+++ b/libpurple/tests/test_presence_manager.c
@@ -52,10 +52,18 @@
 static void
 test_purple_presence_manager_new(void) {
 	PurplePresenceManager *manager = NULL;
+	GListModel *model = NULL;
 
 	manager = purple_presence_manager_new(NULL);
 	g_assert_true(PURPLE_IS_PRESENCE_MANAGER(manager));
 
+	/* Make sure we have our 2 default saved presences.
+	 *
+	 * This may change over time, but we should update this for those changes.
+	 */
+	model = G_LIST_MODEL(manager);
+	g_assert_cmpuint(g_list_model_get_n_items(model), ==, 2);
+
 	g_clear_object(&manager);
 }
 
@@ -63,6 +71,7 @@
 test_purple_presence_manager_add_remove(void) {
 	PurplePresenceManager *manager = NULL;
 	PurpleSavedPresence *presence = NULL;
+	GListModel *model = NULL;
 	gboolean success = FALSE;
 	guint len = 0;
 	guint added = 0;
@@ -71,6 +80,28 @@
 	const char *id = NULL;
 
 	manager = purple_presence_manager_new(NULL);
+
+	/* When a presence manager is created, if there are no saved statuses it
+	 * adds some default ones and sets the active presence. We need to clear
+	 * all of that to get to a known state.
+	 *
+	 * The default statuses are checked in test_purple_presence_manager_new.
+	 */
+	model = G_LIST_MODEL(manager);
+	purple_presence_manager_set_active(manager, NULL);
+	for(guint i = 0; i <= g_list_model_get_n_items(model); i++) {
+		PurpleSavedPresence *presence = NULL;
+
+		/* Since we're removing items, the positions change, so we just always
+		 * want to remove the item at position 0.
+		 */
+		presence = g_list_model_get_item(model, 0);
+		purple_presence_manager_remove(manager,
+		                               purple_saved_presence_get_id(presence));
+		g_assert_finalize_object(presence);
+	}
+
+	/* Connect all of our signals to make sure they're being emitted. */
 	g_signal_connect(manager, "added",
 	                 G_CALLBACK(test_purple_presence_manager_add_remove_counter),
 	                 &added);
