tootle-linux-client/src/Views/TimelineView.vala

199 lines
5.2 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
using Gdk;
public class Tootle.TimelineView : AbstractView {
2019-03-07 17:16:52 +01:00
protected string timeline;
protected string pars;
protected int limit = 25;
protected bool is_last_page = false;
protected string? page_next;
protected string? page_prev;
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
protected Notificator? notificator;
2018-04-14 14:09:06 +02:00
public TimelineView (string timeline, string pars = "") {
2018-05-05 17:38:01 +02:00
base ();
2018-04-14 14:09:06 +02:00
this.timeline = timeline;
this.pars = pars;
2019-03-07 17:16:52 +01:00
2018-06-13 15:13:41 +02:00
accounts.switched.connect (on_account_changed);
app.refresh.connect (on_refresh);
destroy.connect (() => {
if (notificator != null)
notificator.close ();
});
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
setup_notificator ();
2018-05-05 17:38:01 +02:00
request ();
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
2018-04-14 14:09:06 +02:00
public override string get_icon () {
return "user-home-symbolic";
}
2019-03-07 17:16:52 +01:00
2018-04-14 14:09:06 +02:00
public override string get_name () {
2018-05-09 23:46:24 +02:00
return _("Home");
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
public virtual void on_status_added (Status status) {
prepend (status);
2018-05-19 18:01:54 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
public virtual bool is_status_owned (Status status) {
2018-05-31 14:13:21 +02:00
return false;
2018-05-21 17:23:31 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
public void prepend (Status status) {
append (status, true);
2018-05-27 19:14:48 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
public void append (Status status, bool first = false){
2018-05-19 15:55:41 +02:00
if (empty != null)
empty.destroy ();
2019-03-07 17:16:52 +01:00
2018-10-24 14:01:32 +02:00
var separator = new Separator (Orientation.HORIZONTAL);
2018-04-18 11:13:22 +02:00
separator.show ();
2018-05-18 23:14:12 +02:00
2018-10-23 12:05:24 +02:00
var widget = new StatusWidget (status);
2018-04-18 11:13:22 +02:00
widget.separator = separator;
2018-10-24 12:50:40 +02:00
widget.button_press_event.connect (widget.open);
2018-10-23 12:05:24 +02:00
if (!is_status_owned (status))
2019-03-11 13:28:51 +01:00
widget.avatar.button_press_event.connect (widget.on_avatar_clicked);
2018-10-24 12:50:40 +02:00
view.pack_start (separator, false, false, 0);
view.pack_start (widget, false, false, 0);
2019-03-07 17:16:52 +01:00
2018-10-24 11:29:36 +02:00
if (first || status.pinned) {
2018-10-24 12:50:40 +02:00
var new_index = header == null ? 1 : 0;
view.reorder_child (separator, new_index);
2018-10-24 14:01:32 +02:00
view.reorder_child (widget, new_index);
}
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
public override void clear () {
this.page_prev = null;
this.page_next = null;
this.is_last_page = false;
base.clear ();
}
2019-03-07 17:16:52 +01:00
public void get_pages (string? header) {
page_next = page_prev = null;
2018-05-19 15:55:41 +02:00
if (header == null)
return;
2019-03-07 17:16:52 +01:00
var pages = header.split (",");
foreach (var page in pages) {
var sanitized = page
.replace ("<","")
.replace (">", "")
.split (";")[0];
2018-05-19 15:55:41 +02:00
if ("rel=\"prev\"" in page)
page_prev = sanitized;
2018-05-19 15:55:41 +02:00
else
page_next = sanitized;
}
2019-03-07 17:16:52 +01:00
is_last_page = page_prev != null & page_next == null;
}
2019-03-07 17:16:52 +01:00
2018-05-05 17:38:01 +02:00
public virtual string get_url () {
if (page_next != null)
return page_next;
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
var url = "%s/api/v1/timelines/%s?limit=%i".printf (accounts.formal.instance, this.timeline, this.limit);
url += this.pars;
2018-04-26 16:05:03 +02:00
return url;
}
2019-03-07 17:16:52 +01:00
2018-05-05 17:38:01 +02:00
public virtual void request (){
2018-05-29 14:25:56 +02:00
if (accounts.current == null) {
empty_state ();
return;
}
2019-03-07 17:16:52 +01:00
2018-04-26 16:05:03 +02:00
var msg = new Soup.Message("GET", get_url ());
2018-05-19 15:55:41 +02:00
msg.finished.connect (() => empty_state ());
2018-06-20 17:50:42 +02:00
network.queue(msg, (sess, mess) => {
2018-10-23 12:05:24 +02:00
try {
network.parse_array (mess).foreach_element ((array, i, node) => {
2018-04-14 14:09:06 +02:00
var object = node.get_object ();
if (object != null){
var status = Status.parse(object);
2018-10-23 12:05:24 +02:00
append (status);
2018-04-14 14:09:06 +02:00
}
});
get_pages (mess.response_headers.get_one ("Link"));
2018-04-14 14:09:06 +02:00
}
catch (GLib.Error e) {
warning ("Can't update feed");
warning (e.message);
}
});
}
2019-03-07 17:16:52 +01:00
2018-05-03 10:11:31 +02:00
public virtual void on_refresh (){
clear ();
request ();
}
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
public virtual Soup.Message? get_stream (){
return null;
}
2019-03-07 17:16:52 +01:00
2018-04-26 16:05:03 +02:00
public virtual void on_account_changed (Account? account){
if(account == null)
return;
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
var stream = get_stream ();
if (notificator != null && stream != null) {
var old_url = notificator.get_url ();
var new_url = stream.get_uri ().to_string (false);
if (old_url != new_url) {
info ("Updating notificator %s", notificator.get_name ());
setup_notificator ();
}
2018-06-13 15:13:41 +02:00
}
2019-03-07 17:16:52 +01:00
2018-05-05 17:38:01 +02:00
on_refresh ();
2018-04-26 16:05:03 +02:00
}
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
protected void setup_notificator () {
if (notificator != null)
notificator.close ();
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
var stream = get_stream ();
if (stream == null)
return;
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
notificator = new Notificator (stream);
2018-10-23 12:05:24 +02:00
notificator.status_added.connect ((status) => {
2018-06-20 17:50:42 +02:00
if (can_stream ())
2018-10-23 12:05:24 +02:00
on_status_added (status);
2018-06-20 17:50:42 +02:00
});
notificator.start ();
}
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
protected virtual bool is_public () {
return false;
}
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
protected virtual bool can_stream () {
var allowed_public = true;
if (is_public ())
allowed_public = settings.live_updates_public;
2019-03-07 17:16:52 +01:00
2018-06-20 17:50:42 +02:00
return settings.live_updates && allowed_public;
}
2019-03-07 17:16:52 +01:00
2018-10-28 13:54:09 +01:00
protected override void on_bottom_reached () {
if (is_last_page) {
debug ("Last page reached");
return;
}
2018-04-26 16:05:03 +02:00
request ();
}
2018-04-14 14:09:06 +02:00
}