2018-04-28 18:27:10 +02:00
|
|
|
using Gtk;
|
|
|
|
|
2019-03-11 15:14:37 +01:00
|
|
|
public class Tootle.Widgets.RichLabel : Label {
|
2018-04-28 18:27:10 +02:00
|
|
|
|
2019-03-11 15:14:37 +01:00
|
|
|
public weak API.Mention[]? mentions;
|
2018-04-28 18:27:10 +02:00
|
|
|
|
2018-05-18 23:14:12 +02:00
|
|
|
public RichLabel (string text) {
|
2018-06-06 16:19:11 +02:00
|
|
|
set_label (text);
|
2018-04-28 18:27:10 +02:00
|
|
|
set_use_markup (true);
|
2018-05-18 23:14:12 +02:00
|
|
|
activate_link.connect (open_link);
|
2018-04-28 18:27:10 +02:00
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2018-06-06 16:19:11 +02:00
|
|
|
public static string escape_entities (string content) {
|
|
|
|
return content
|
2019-03-09 11:09:36 +01:00
|
|
|
.replace (" ", " ")
|
2018-10-30 18:40:47 +01:00
|
|
|
.replace ("'", "'");
|
2018-06-06 16:19:11 +02:00
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2018-06-06 16:19:11 +02:00
|
|
|
public static string restore_entities (string content) {
|
|
|
|
return content
|
2018-10-30 18:40:47 +01:00
|
|
|
.replace ("&", "&")
|
|
|
|
.replace ("<", "<")
|
|
|
|
.replace (">", ">")
|
|
|
|
.replace ("'", "'")
|
|
|
|
.replace (""", "\"");
|
2018-06-06 16:19:11 +02:00
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2018-06-06 16:19:11 +02:00
|
|
|
public new void set_label (string text) {
|
2019-03-09 11:48:58 +01:00
|
|
|
base.set_markup (Html.simplify(escape_entities (text)));
|
2018-06-06 16:19:11 +02:00
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2018-05-04 22:57:31 +02:00
|
|
|
public void wrap_words () {
|
2019-03-11 15:14:37 +01:00
|
|
|
halign = Align.START;
|
2018-05-04 22:57:31 +02:00
|
|
|
single_line_mode = false;
|
|
|
|
set_line_wrap (true);
|
|
|
|
wrap_mode = Pango.WrapMode.WORD_CHAR;
|
2019-03-11 15:14:37 +01:00
|
|
|
justify = Justification.LEFT;
|
2018-05-04 22:57:31 +02:00
|
|
|
xalign = 0;
|
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2019-03-11 15:14:37 +01:00
|
|
|
public bool open_link (string url) {
|
2018-04-28 18:27:10 +02:00
|
|
|
if (mentions != null){
|
2019-03-11 15:14:37 +01:00
|
|
|
foreach (API.Mention mention in mentions) {
|
2018-04-28 18:27:10 +02:00
|
|
|
if (url == mention.url){
|
2019-03-11 15:14:37 +01:00
|
|
|
Views.Profile.open_from_id (mention.id);
|
2018-04-28 18:27:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2019-03-11 15:14:37 +01:00
|
|
|
if ("/tags/" in url) {
|
2018-05-14 16:43:10 +02:00
|
|
|
var encoded = url.split("/tags/")[1];
|
|
|
|
var hashtag = Soup.URI.decode (encoded);
|
2019-03-11 15:14:37 +01:00
|
|
|
window.open_view (new Views.Hashtag (hashtag));
|
2018-05-14 16:43:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2019-03-11 15:14:37 +01:00
|
|
|
if ("/@" in url) {
|
2018-05-16 11:11:54 +02:00
|
|
|
var uri = new Soup.URI (url);
|
|
|
|
var username = url.split("/@")[1];
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2018-06-02 10:37:28 +02:00
|
|
|
if ("/" in username)
|
2019-03-11 15:14:37 +01:00
|
|
|
Views.ExpandedStatus.open_from_link (url);
|
2018-06-02 10:37:28 +02:00
|
|
|
else
|
2019-03-11 15:14:37 +01:00
|
|
|
Views.Profile.open_from_name ("@" + username + "@" + uri.get_host ());
|
2018-04-28 18:27:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-09 11:09:36 +01:00
|
|
|
|
2019-03-11 13:28:51 +01:00
|
|
|
return Desktop.open_uri (url);
|
2018-04-28 18:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|