tootle-linux-client/src/Widgets/RichLabel.vala

112 lines
3.4 KiB
Vala
Raw Normal View History

2018-04-28 18:27:10 +02:00
using Gtk;
2020-05-29 14:19:35 +02:00
using Gee;
2018-04-28 18:27:10 +02:00
2019-03-11 15:14:37 +01:00
public class Tootle.Widgets.RichLabel : Label {
2018-04-28 18:27:10 +02:00
2020-05-29 14:19:35 +02:00
public weak ArrayList<API.Mention>? mentions;
2020-06-29 23:43:45 +02:00
public string text {
get {
return this.label;
}
set {
this.label = escape_entities (Html.simplify (value));
}
}
2020-05-29 14:19:35 +02:00
construct {
use_markup = true;
xalign = 0;
wrap_mode = Pango.WrapMode.WORD_CHAR;
justify = Justification.LEFT;
single_line_mode = false;
set_line_wrap (true);
activate_link.connect (open_link);
}
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
}
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 ("&nbsp;", " ")
2018-10-30 18:40:47 +01:00
.replace ("'", "&apos;");
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 ("&amp;", "&")
.replace ("&lt;", "<")
.replace ("&gt;", ">")
.replace ("&apos;", "'")
.replace ("&quot;", "\"");
2018-06-06 16:19:11 +02:00
}
2019-03-09 11:09:36 +01:00
2019-03-11 15:14:37 +01:00
public bool open_link (string url) {
2020-05-29 14:19:35 +02:00
if ("tootle://" in url)
return false;
2018-04-28 18:27:10 +02:00
if (mentions != null){
2020-05-29 14:19:35 +02:00
mentions.@foreach (mention => {
if (url == mention.url)
2019-03-11 15:14:37 +01:00
Views.Profile.open_from_id (mention.id);
2020-05-29 14:19:35 +02:00
return true;
});
2018-04-28 18:27:10 +02:00
}
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
if ("@" in url || "tags" in url) {
2020-05-29 14:19:35 +02:00
new Request.GET ("/api/v2/search")
.with_account (accounts.active)
.with_param ("resolve", "true")
.with_param ("q", Soup.URI.encode (url, null))
.then ((sess, mess) => {
var root = network.parse (mess);
var accounts = root.get_array_member ("accounts");
var statuses = root.get_array_member ("statuses");
var hashtags = root.get_array_member ("hashtags");
2019-03-09 11:09:36 +01:00
2020-05-29 14:19:35 +02:00
if (accounts.get_length () > 0) {
2020-06-20 12:04:58 +02:00
var node = accounts.get_element (0);
var obj = API.Account.from (node);
2020-05-29 14:19:35 +02:00
window.open_view (new Views.Profile (obj));
}
else if (statuses.get_length () > 0) {
2020-06-20 12:04:58 +02:00
var node = accounts.get_element (0);
var obj = API.Status.from (node);
2020-05-29 14:19:35 +02:00
window.open_view (new Views.ExpandedStatus (obj));
}
else if (hashtags.get_length () > 0) {
2020-06-20 12:04:58 +02:00
var node = accounts.get_element (0);
var obj = API.Tag.from (node);
2020-05-29 14:19:35 +02:00
window.open_view (new Views.Hashtag (obj.name));
}
else {
Desktop.open_uri (url);
}
})
.on_error ((status, reason) => open_link_fallback (url, reason))
.exec ();
}
else {
Desktop.open_uri (url);
2018-04-28 18:27:10 +02:00
}
return true;
}
public bool open_link_fallback (string url, string reason) {
2020-05-29 14:19:35 +02:00
warning (@"Can't resolve url: $url");
warning (@"Reason: $reason");
Desktop.open_uri (url);
return true;
2018-04-28 18:27:10 +02:00
}
}