[Telepathy-commits] [telepathy-idle/master] Extract splitting of long/multiline messages to a function

Will Thompson will.thompson at collabora.co.uk
Wed Sep 24 02:47:09 PDT 2008


---
 src/idle-text.c |   61 +++++++++++++++++++++++++++++++++++++------------------
 src/idle-text.h |    1 +
 2 files changed, 42 insertions(+), 20 deletions(-)

diff --git a/src/idle-text.c b/src/idle-text.c
index a6ae4aa..9bfd0ae 100644
--- a/src/idle-text.c
+++ b/src/idle-text.c
@@ -51,25 +51,15 @@ gboolean idle_text_decode(const gchar *text, TpChannelTextMessageType *type, gch
 	return TRUE;
 }
 
-void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gchar *text, IdleConnection *conn, DBusGMethodInvocation *context) {
+GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, GError **error) {
+	GPtrArray *messages;
 	gchar msg[IRC_MSG_MAXLEN + 1];
-	time_t timestamp;
-	const gchar *final_text = text;
 	gsize len;
 	gchar *part;
 	gsize headerlen;
-	GError *error;
-
-	if ((recipient == NULL) || (strlen(recipient) == 0)) {
-		IDLE_DEBUG("invalid recipient");
-
-		error = g_error_new(TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "invalid recipient");
-		dbus_g_method_return_error(context, error);
-		g_error_free(error);
-
-		return;
-	}
+	const gchar *final_text;
 
+	final_text = text;
 	len = strlen(final_text);
 	part = (gchar*)final_text;
 
@@ -81,14 +71,12 @@ void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gcha
 		g_snprintf(msg, IRC_MSG_MAXLEN + 1, "NOTICE %s :", recipient);
 	else {
 		IDLE_DEBUG("invalid message type %u", type);
+		g_set_error(error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "invalid message type %u", type);
 
-		error = g_error_new(TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "invalid message type %u", type);
-		dbus_g_method_return_error(context, error);
-		g_error_free(error);
-
-		return;
+		return NULL;
 	}
 
+	messages = g_ptr_array_new ();
 	headerlen = strlen(msg);
 
 	while (part < final_text + len) {
@@ -112,9 +100,42 @@ void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gcha
 		if (br)
 			part++;
 
-		idle_connection_send(conn, msg);
+		g_ptr_array_add(messages, g_strdup(msg));
+	}
+
+	g_ptr_array_add(messages, NULL);
+
+	return (GStrv) g_ptr_array_free(messages, FALSE);
+}
+
+void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gchar *text, IdleConnection *conn, DBusGMethodInvocation *context) {
+	time_t timestamp;
+	GError *error = NULL;
+	GStrv messages;
+
+	if ((recipient == NULL) || (strlen(recipient) == 0)) {
+		IDLE_DEBUG("invalid recipient");
+
+		error = g_error_new(TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "invalid recipient");
+		dbus_g_method_return_error(context, error);
+		g_error_free(error);
+
+		return;
+	}
+
+	messages = idle_text_encode_and_split(type, recipient, text, &error);
+	if (messages == NULL) {
+		dbus_g_method_return_error(context, error);
+		g_error_free(error);
+		return;
+	}
+
+	for(GStrv m = messages; *m != NULL; m++) {
+		idle_connection_send(conn, *m);
 	}
 
+	g_strfreev(messages);
+
 	timestamp = time(NULL);
 	tp_svc_channel_type_text_emit_sent(obj, timestamp, type, text);
 
diff --git a/src/idle-text.h b/src/idle-text.h
index 94ee8a1..d965a34 100644
--- a/src/idle-text.h
+++ b/src/idle-text.h
@@ -31,6 +31,7 @@
 G_BEGIN_DECLS
 
 gboolean idle_text_decode(const gchar *text, TpChannelTextMessageType *type, gchar **body);
+GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, GError **error);
 void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gchar *text, IdleConnection *conn, DBusGMethodInvocation *ctx);
 
 G_END_DECLS
-- 
1.5.6.5




More information about the Telepathy-commits mailing list