[empathy: 8/12] Move /me and /say support from EmpathyMessage to EmpathyChat.



commit 018af67a500b8bab6b8b374c32a887f399339834
Author: Xavier Claessens <xclaesse gmail com>
Date:   Tue Oct 27 14:26:14 2009 +0100

    Move /me and /say support from EmpathyMessage to EmpathyChat.
    Also make commands not case sensitive and use g_ascii_isspace to detect spaces.

 libempathy-gtk/empathy-chat.c |   83 ++++++++++++++++++++++++++++++++++-------
 libempathy/empathy-message.c  |   56 ---------------------------
 libempathy/empathy-message.h  |    1 -
 3 files changed, 69 insertions(+), 71 deletions(-)
---
diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c
index df76a02..c2a88b2 100644
--- a/libempathy-gtk/empathy-chat.c
+++ b/libempathy-gtk/empathy-chat.c
@@ -508,6 +508,31 @@ chat_command_msg (EmpathyChat *chat,
 	chat_command_msg_internal (chat, strv[1], strv[2]);
 }
 
+static void
+chat_command_me (EmpathyChat *chat,
+		  GStrv        strv)
+{
+	EmpathyChatPriv *priv = GET_PRIV (chat);
+	EmpathyMessage *message;
+
+	message = empathy_message_new (strv[1]);
+	empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
+	empathy_tp_chat_send (priv->tp_chat, message);
+	g_object_unref (message);
+}
+
+static void
+chat_command_say (EmpathyChat *chat,
+		  GStrv        strv)
+{
+	EmpathyChatPriv *priv = GET_PRIV (chat);
+	EmpathyMessage *message;
+
+	message = empathy_message_new (strv[1]);
+	empathy_tp_chat_send (priv->tp_chat, message);
+	g_object_unref (message);
+}
+
 static void chat_command_help (EmpathyChat *chat, GStrv strv);
 
 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
@@ -539,6 +564,14 @@ static ChatCommandItem commands[] = {
 	{"msg", 3, 3, chat_command_msg,
 	 N_("/msg <contact id> <message>, open a private chat")},
 
+	{"me", 2, 2, chat_command_me,
+	 N_("/me <message>, send an ACTION message to the current conversation")},
+
+	{"say", 2, 2, chat_command_say,
+	 N_("/say <message>, send <message> to the current conversation. "
+	    "This is used to send a message starting with a '/'. For example: "
+	    "\"/say /join is used to join a new chatroom\"")},
+
 	{"help", 1, 2, chat_command_help,
 	 N_("/help [<command>], show all supported commands. "
 	    "If <command> is defined, show its usage.")},
@@ -572,7 +605,7 @@ chat_command_help (EmpathyChat *chat,
 	}
 
 	for (i = 0; i < G_N_ELEMENTS (commands); i++) {
-		if (!tp_strdiff (strv[1], commands[i].prefix)) {
+		if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
 			chat_command_show_help (chat, &commands[i]);
 			return;
 		}
@@ -592,12 +625,13 @@ chat_command_parse (const gchar *text, guint max_parts)
 		const gchar *end;
 
 		/* Skip white spaces */
-		while (*text == ' ') {
+		while (g_ascii_isspace (*text)) {
 			text++;
 		}
 
-		end = strchr (text, ' ');
-		if (end == NULL) {
+		/* Search the end of this part, until first space. */
+		for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++);
+		if (*end == '\0') {
 			break;
 		}
 
@@ -624,6 +658,13 @@ chat_command_parse (const gchar *text, guint max_parts)
 	return (GStrv) g_ptr_array_free (array, FALSE);
 }
 
+static gboolean
+has_prefix_case (const gchar *s,
+		  const gchar *prefix)
+{
+	return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
+}
+
 static void
 chat_send (EmpathyChat  *chat,
 	   const gchar *msg)
@@ -641,11 +682,14 @@ chat_send (EmpathyChat  *chat,
 	chat_sent_message_add (chat, msg);
 
 	if (msg[0] == '/') {
+		gboolean second_slash = FALSE;
+		const gchar *iter = msg + 1;
+
 		for (i = 0; i < G_N_ELEMENTS (commands); i++) {
 			GStrv strv;
 			guint strv_len;
 
-			if (!g_str_has_prefix (msg + 1, commands[i].prefix)) {
+			if (!has_prefix_case (msg + 1, commands[i].prefix)) {
 				continue;
 			}
 
@@ -654,7 +698,7 @@ chat_send (EmpathyChat  *chat,
 			 * between args */
 			strv = chat_command_parse (msg + 1, commands[i].max_parts);
 
-			if (tp_strdiff (strv[0], commands[i].prefix)) {
+			if (g_ascii_strcasecmp (strv[0], commands[i].prefix) != 0) {
 				g_strfreev (strv);
 				continue;
 			}
@@ -671,17 +715,28 @@ chat_send (EmpathyChat  *chat,
 			g_strfreev (strv);
 			return;
 		}
-	}
 
-	message = empathy_message_new_from_entry (msg);
+		/* Also allow messages with two slashes before the
+		 * first space, so it is possible to send a /unix/path.
+		 * This heuristic is kind of crap. */
+		while (*iter != '\0' && !g_ascii_isspace (*iter)) {
+			if (*iter == '/') {
+				second_slash = TRUE;
+				break;
+			}
+			iter++;
+		}
 
-	if (message == NULL) {
-		empathy_chat_view_append_event (chat->view,
-			_("Unsupported command"));
-	} else {
-		empathy_tp_chat_send (priv->tp_chat, message);
-		g_object_unref (message);
+		if (!second_slash) {
+			empathy_chat_view_append_event (chat->view,
+				_("Unsupported command"));
+			return;
+		}
 	}
+
+	message = empathy_message_new (msg);
+	empathy_tp_chat_send (priv->tp_chat, message);
+	g_object_unref (message);
 }
 
 static void
diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c
index a403766..705bd22 100644
--- a/libempathy/empathy-message.c
+++ b/libempathy/empathy-message.c
@@ -234,62 +234,6 @@ message_set_property (GObject      *object,
 	};
 }
 
-static gboolean
-has_prefix_case (const gchar *s,
-		 const gchar *prefix)
-{
-	return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
-}
-
-/*
- * Constructs an EmpathyMessage based on user input, which may include "/me"
- * and friends.
- *
- * Returns: an #EmpathyMessage if @message could be parsed, or %NULL if
- *          @message was an unknown command.
- */
-EmpathyMessage *
-empathy_message_new_from_entry (const gchar *message)
-{
-	TpChannelTextMessageType t = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
-
-	g_return_val_if_fail (message != NULL, NULL);
-
-	if (message[0] == '/') {
-		if (g_ascii_strcasecmp (message, "/me") == 0) {
-			message = "";
-			t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
-		} else if (has_prefix_case (message, "/me ")) {
-			message += strlen ("/me ");
-			t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
-		} else if (has_prefix_case (message, "/say ")) {
-			message += strlen ("/say ");
-		} else {
-			/* Also allow messages with two slashes before the
-			 * first space, so it is possible to send a /unix/path.
-			 * This heuristic is kind of crap.
-			 */
-			gboolean second_slash = FALSE;
-			const gchar *m = message + 1;
-
-			while (!second_slash && *m != '\0' && *m != ' ') {
-				if (*m == '/')
-					second_slash = TRUE;
-
-				m++;
-			}
-
-			if (!second_slash)
-				return NULL;
-		}
-	}
-
-	return g_object_new (EMPATHY_TYPE_MESSAGE,
-			     "type", t,
-			     "body", message,
-			     NULL);
-}
-
 EmpathyMessage *
 empathy_message_new (const gchar *body)
 {
diff --git a/libempathy/empathy-message.h b/libempathy/empathy-message.h
index 7ca6240..09175e6 100644
--- a/libempathy/empathy-message.h
+++ b/libempathy/empathy-message.h
@@ -52,7 +52,6 @@ struct _EmpathyMessageClass {
 };
 
 GType                    empathy_message_get_type          (void) G_GNUC_CONST;
-EmpathyMessage *         empathy_message_new_from_entry    (const gchar *message);
 EmpathyMessage *         empathy_message_new               (const gchar              *body);
 TpChannelTextMessageType empathy_message_get_tptype        (EmpathyMessage           *message);
 void                     empathy_message_set_tptype        (EmpathyMessage           *message,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]