static void
empathy_webkit_match_newline (const gchar *text,
    gssize len,
    EmpathyStringReplace replace_func,
    EmpathyStringParser *sub_parsers,
    gpointer user_data)
{
  GString *string = user_data;
  gint i;
  gint prev = 0;

  if (len < 0)
    len = G_MAXSSIZE;

  /* Replace \n by <br/> */
  for (i = 0; i < len && text[i] != '\0'; i++)
    {
      if (text[i] == '\n')
        {
          empathy_string_parser_substr (text + prev, i - prev,
              sub_parsers, user_data);
          g_string_append (string, "<br/>");
          prev = i + 1;
        }
    }

  empathy_string_parser_substr (text + prev, i - prev,
              sub_parsers, user_data);
}
示例#2
0
gchar *
empathy_add_link_markup (const gchar *text)
{
	EmpathyStringParser parsers[] = {
		{empathy_string_match_link, empathy_string_replace_link},
		{empathy_string_match_all, empathy_string_replace_escaped},
		{NULL, NULL}
	};
	GString *string;

	g_return_val_if_fail (text != NULL, NULL);

	/* GtkLabel with links could make infinite loop because of
	 * GNOME bug #612066. It is fixed in GTK >= 2.18.8 and GTK >= 2.19.7.
	 * FIXME: Remove this check once we have an hard dep on GTK 2.20 */
	if (gtk_check_version (2, 18, 8) != NULL ||
	    (gtk_minor_version == 19 && gtk_micro_version < 7)) {
		return g_markup_escape_text (text, -1);
	}

	string = g_string_sized_new (strlen (text));
	empathy_string_parser_substr (text, -1, parsers, string);

	return g_string_free (string, FALSE);
}
示例#3
0
void
empathy_string_match_link (const gchar *text,
			   gssize len,
			   EmpathyStringReplace replace_func,
			   EmpathyStringParser *sub_parsers,
			   gpointer user_data)
{
	GRegex     *uri_regex;
	GMatchInfo *match_info;
	gboolean    match;
	gint        last = 0;

	uri_regex = uri_regex_dup_singleton ();
	match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
	if (match) {
		gint s = 0, e = 0;

		do {
			g_match_info_fetch_pos (match_info, 0, &s, &e);

			if (s > last) {
				/* Append the text between last link (or the
				 * start of the message) and this link */
				empathy_string_parser_substr (text + last,
							      s - last,
							      sub_parsers,
							      user_data);
			}

			replace_func (text + s, e - s, NULL, user_data);

			last = e;
		} while (g_match_info_next (match_info, NULL));
	}

	empathy_string_parser_substr (text + last, len - last,
				      sub_parsers, user_data);

	g_match_info_free (match_info);
	g_regex_unref (uri_regex);
}
示例#4
0
void
empathy_string_match_smiley (const gchar *text,
			     gssize len,
			     EmpathyStringReplace replace_func,
			     EmpathyStringParser *sub_parsers,
			     gpointer user_data)
{
	guint last = 0;
	EmpathySmileyManager *smiley_manager;
	GSList *hits, *l;

	smiley_manager = empathy_smiley_manager_dup_singleton ();
	hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);

	for (l = hits; l; l = l->next) {
		EmpathySmileyHit *hit = l->data;

		if (hit->start > last) {
			/* Append the text between last smiley (or the
			 * start of the message) and this smiley */
			empathy_string_parser_substr (text + last,
						      hit->start - last,
						      sub_parsers, user_data);
		}

		replace_func (text + hit->start, hit->end - hit->start,
			      hit, user_data);

		last = hit->end;

		empathy_smiley_hit_free (hit);
	}
	g_slist_free (hits);
	g_object_unref (smiley_manager);

	empathy_string_parser_substr (text + last, len - last,
				      sub_parsers, user_data);
}
示例#5
0
void
empathy_chat_text_view_append_body (EmpathyChatTextView *view,
				    const gchar         *body,
				    const gchar         *tag)
{
	EmpathyChatTextViewPriv *priv = GET_PRIV (view);
	EmpathyStringParser     *parsers;
	gboolean                 use_smileys;
	GtkTextIter              start_iter;
	GtkTextIter              iter;
	GtkTextMark             *mark;

	/* Check if we have to parse smileys */
	use_smileys = g_settings_get_boolean (priv->gsettings_chat,
			EMPATHY_PREFS_CHAT_SHOW_SMILEYS);

	if (use_smileys)
		parsers = string_parsers_with_smiley;
	else
		parsers = string_parsers;

	/* Create a mark at the place we'll start inserting */
	gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
	mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);

	/* Parse text for links/smileys and insert in the buffer */
	empathy_string_parser_substr (body, -1, parsers, priv->buffer);

	/* Insert a newline after the text inserted */
	gtk_text_buffer_get_end_iter (priv->buffer, &iter);
	gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);

	/* Apply the style to the inserted text. */
	gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
	gtk_text_buffer_get_end_iter (priv->buffer, &iter);
	gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
					   &start_iter,
					   &iter);

	gtk_text_buffer_delete_mark (priv->buffer, mark);
}