diff options
author | Will Thompson <will.thompson@collabora.co.uk> | 2009-08-28 20:18:44 +0800 |
---|---|---|
committer | Will Thompson <will.thompson@collabora.co.uk> | 2009-08-28 20:18:46 +0800 |
commit | 9488760608fb66ac49951fb39741d524cf009c02 (patch) | |
tree | e0e2ee642e84c54c733660a657640637cf8999b1 /libempathy | |
parent | b81c0c48ad7bf68583c08e38f8a2a90c7c7dce4b (diff) | |
parent | c2cbd1de858c0a8a994358fc717054ce32c8b895 (diff) | |
download | gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar.gz gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar.bz2 gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar.lz gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar.xz gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.tar.zst gsoc2013-empathy-9488760608fb66ac49951fb39741d524cf009c02.zip |
Merge branch 'do-say-me-think'
Reviewed-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Diffstat (limited to 'libempathy')
-rw-r--r-- | libempathy/empathy-message.c | 73 | ||||
-rw-r--r-- | libempathy/empathy-message.h | 1 |
2 files changed, 59 insertions, 15 deletions
diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c index a8fe6084c..34a466752 100644 --- a/libempathy/empathy-message.c +++ b/libempathy/empathy-message.c @@ -218,6 +218,62 @@ 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) { @@ -337,28 +393,15 @@ empathy_message_set_body (EmpathyMessage *message, const gchar *body) { EmpathyMessagePriv *priv = GET_PRIV (message); - TpChannelTextMessageType type; g_return_if_fail (EMPATHY_IS_MESSAGE (message)); g_free (priv->body); - priv->body = NULL; - - type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL; - if (g_str_has_prefix (body, "/me")) { - type = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION; - body += 4; - } - else if (g_str_has_prefix (body, "/say")) { - body += 5; - } if (body) { priv->body = g_strdup (body); - } - - if (type != priv->type) { - empathy_message_set_tptype (message, type); + } else { + priv->body = NULL; } g_object_notify (G_OBJECT (message), "body"); diff --git a/libempathy/empathy-message.h b/libempathy/empathy-message.h index 00064df57..f9a488703 100644 --- a/libempathy/empathy-message.h +++ b/libempathy/empathy-message.h @@ -52,6 +52,7 @@ 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, |