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

116 lines
3.8 KiB
Vala
Raw Normal View History

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 ("&lt;", "<")
.replace ("&gt;", ">")
.replace ("&apos;", "'")
.replace ("&quot;", "\"");
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) {
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
if ("@" in url || "tags" in url) {
var query = Soup.URI.encode (url, null);
2019-03-17 11:26:55 +01:00
var msg_url = "%s/api/v1/search?q=%s&resolve=true".printf (accounts.formal.instance, query);
var msg = new Soup.Message("GET", msg_url);
msg.priority = Soup.MessagePriority.HIGH;
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (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
if (accounts.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Account.parse (item);
window.open_view (new Views.Profile (obj));
}
else if (statuses.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Status.parse (item);
window.open_view (new Views.ExpandedStatus (obj));
}
else if (hashtags.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Tag.parse (item);
window.open_view (new Views.Hashtag (obj.name));
}
else {
Desktop.open_uri (url);
}
}, (status, reason) => {
open_link_fallback (url, reason);
});
}
else {
Desktop.open_uri (url);
2018-04-28 18:27:10 +02:00
}
return true;
}
public bool open_link_fallback (string url, string reason) {
warning ("Can't resolve url: " + url);
warning ("Reason: " + reason);
2019-03-09 11:09:36 +01:00
var toast = window.toast;
toast.title = reason;
toast.set_default_action (_("Open in Browser"));
ulong signal_id = 0;
signal_id = toast.default_action.connect (() => {
Desktop.open_uri (url);
toast.disconnect (signal_id);
});
toast.send_notification ();
return true;
2018-04-28 18:27:10 +02:00
}
}