libpurple/purplecreateconversationdetails.c

changeset 43164
7709065d96eb
parent 43071
071588186662
child 43285
acde304cf24c
--- a/libpurple/purplecreateconversationdetails.c	Wed Jan 29 00:47:33 2025 -0600
+++ b/libpurple/purplecreateconversationdetails.c	Thu Jan 30 22:25:41 2025 -0600
@@ -20,6 +20,8 @@
  * this library; if not, see <https://www.gnu.org/licenses/>.
  */
 
+#include <glib/gi18n-lib.h>
+
 #include <gio/gio.h>
 
 #include "purplecreateconversationdetails.h"
@@ -41,6 +43,9 @@
 	GListModel *participants;
 };
 
+G_DEFINE_QUARK(purple-create-conversation-details-error,
+               purple_create_conversation_details_error)
+
 /******************************************************************************
  * Helpers
  *****************************************************************************/
@@ -194,6 +199,60 @@
 	return details->participants;
 }
 
+gboolean
+purple_create_conversation_details_is_valid(PurpleCreateConversationDetails *details,
+                                            GError **error)
+{
+	guint n_participants = 0;
+
+	g_return_val_if_fail(PURPLE_IS_CREATE_CONVERSATION_DETAILS(details),
+	                     FALSE);
+
+	if(G_IS_LIST_MODEL(details->participants)) {
+		n_participants = g_list_model_get_n_items(details->participants);
+	}
+
+	if(n_participants == 0) {
+		/* TRANSLATORS: This error message is to tell users when they tried to
+		 * create a conversation but did not provide any other participants for
+		 * the conversation.
+		 */
+		g_set_error_literal(error,
+		                    PURPLE_CREATE_CONVERSATION_DETAILS_ERROR,
+		                    PURPLE_CREATE_CONVERSATION_DETAILS_ERROR_NO_PARTICIPANTS,
+		                    _("no participants were provided"));
+
+		return FALSE;
+	}
+
+	/* max-participants with a value of 0 means unlimited, so there is nothing
+	 * to validate in that case.
+	 */
+	if(details->max_participants > 0) {
+		if(n_participants > details->max_participants) {
+			const char *format = NULL;
+
+			/* TRANSLATORS: This error message is to tell users that they tried to
+			 * create a conversation but they have added too many participants to
+			 * it.
+			 */
+			format = g_dngettext(GETTEXT_PACKAGE,
+			                     "a maximum of %u participant is allowed",
+			                     "a maximum of %u participants are allowed",
+			                     details->max_participants);
+
+			g_set_error(error,
+			            PURPLE_CREATE_CONVERSATION_DETAILS_ERROR,
+			            PURPLE_CREATE_CONVERSATION_DETAILS_ERROR_TOO_MANY_PARTICIPANTS,
+			            format, details->max_participants);
+
+			return FALSE;
+		}
+	}
+
+	return TRUE;
+}
+
 void
 purple_create_conversation_details_set_participants(PurpleCreateConversationDetails *details,
                                                     GListModel *participants)

mercurial