diff options
author | Alan Fitton <ajf@eth0.org.uk> | 2012-03-02 10:04:41 +0000 |
---|---|---|
committer | Alan Fitton <ajf@eth0.org.uk> | 2012-03-02 10:04:41 +0000 |
commit | 9e8fed99f309cdc1381e56f7d02915aa27e9da50 (patch) | |
tree | 163d7cb148b9cf9277ed0a9aa20d7de6396dfdd0 /src | |
parent | f77e1360b5204eb3b04ac230cac7827b731470cc (diff) |
contributed improvement to the url comment patch - use a GRegex now so only the URL is linked
Diffstat (limited to 'src')
-rw-r--r-- | src/trg-general-panel.c | 16 | ||||
-rw-r--r-- | src/util.c | 17 | ||||
-rw-r--r-- | src/util.h | 1 |
3 files changed, 22 insertions, 12 deletions
diff --git a/src/trg-general-panel.c b/src/trg-general-panel.c index 88c0545..827d9ef 100644 --- a/src/trg-general-panel.c +++ b/src/trg-general-panel.c @@ -113,8 +113,7 @@ trg_general_panel_update(TrgGeneralPanel * panel, JsonObject * t, TrgGeneralPanelPrivate *priv; gchar buf[32]; gint sizeOfBuf; - gchar *statusString, *fullStatusString, *completedAtString; - const gchar *comment; + gchar *statusString, *fullStatusString, *completedAtString, *comment; const gchar *errorStr; gint64 eta, uploaded, downloaded, completedAt; GtkLabel *keyLabel; @@ -180,16 +179,9 @@ trg_general_panel_update(TrgGeneralPanel * panel, JsonObject * t, gtk_label_set_text(GTK_LABEL(priv->gen_downloaddir_label), torrent_get_download_dir(t)); - comment = torrent_get_comment(t); - if(g_str_has_prefix(comment, "http")) { - /* starts with http -> url, converting to markup */ - gchar *commentMarkup = g_markup_printf_escaped("<a href='%s'>%s</a>", - comment, comment); - gtk_label_set_markup(GTK_LABEL(priv->gen_comment_label), commentMarkup); - g_free(commentMarkup); - } else { - gtk_label_set_markup(GTK_LABEL(priv->gen_comment_label), comment); - } + comment = add_links_to_text(torrent_get_comment(t)); + gtk_label_set_markup(GTK_LABEL(priv->gen_comment_label), comment); + g_free(comment); errorStr = torrent_get_errorstr(t); keyLabel = @@ -506,6 +506,23 @@ gchar *epoch_to_string(gint64 epoch) #endif } +/* wrap a link in text with a hyperlink, for use in pango markup. + * with or without any links - a newly allocated string is returned. */ + +gchar *add_links_to_text(const gchar *original) { + /* only perform replacement if string doesn't contains links */ + if (!g_regex_match_simple("<a\\s.*>", original, 0, 0)) { + GRegex *regex = g_regex_new("(https?://[a-zA-Z0-9_\\-\\./?=]+)", 0, 0, + NULL); + gchar *newText = g_regex_replace(regex, original, -1, 0, + "<a href='\\1'>\\1</a>", 0, NULL); + g_regex_unref(regex); + return newText; + } else { + return g_strdup(original); + } +} + size_t tr_strlcpy(char *dst, const void *src, size_t siz) { #ifdef HAVE_STRLCPY @@ -55,6 +55,7 @@ GRegex *trg_uri_host_regex_new(void); gchar *trg_gregex_get_first(GRegex * rx, const gchar * uri); gchar *make_error_message(JsonObject * response, int status); void trg_error_dialog(GtkWindow * parent, trg_response * response); +gchar *add_links_to_text(const gchar *original); void tr_formatter_size_init(unsigned int kilo, |