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

153 lines
4.1 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
using Gdk;
public class Tootle.TimelineView : AbstractView {
2018-04-14 14:09:06 +02: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;
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;
2018-04-14 19:53:09 +02:00
2018-04-27 20:50:08 +02:00
Tootle.accounts.switched.connect(on_account_changed);
2018-05-05 17:38:01 +02:00
Tootle.app.refresh.connect(on_refresh);
2018-05-19 18:01:54 +02:00
Tootle.network.status_added.connect (on_status_added);
2018-04-14 14:09:06 +02:00
2018-05-05 17:38:01 +02:00
request ();
2018-04-14 14:09:06 +02:00
}
public override string get_icon () {
return "user-home-symbolic";
}
public override string get_name () {
2018-05-09 23:46:24 +02:00
return _("Home");
2018-04-14 14:09:06 +02:00
}
2018-05-19 18:01:54 +02:00
private void on_status_added (ref Status status, string timeline) {
if (timeline != this.timeline)
return;
2018-05-27 19:14:48 +02:00
prepend (ref status);
2018-05-19 18:01:54 +02:00
}
2018-05-21 17:23:31 +02:00
public virtual bool is_status_owned (ref Status status) {
2018-05-31 14:13:21 +02:00
return false;
2018-05-21 17:23:31 +02:00
}
2018-05-27 19:14:48 +02:00
public void prepend (ref Status status) {
append (ref status, true);
}
public void append (ref Status status, bool first = false){
2018-05-19 15:55:41 +02:00
if (empty != null)
empty.destroy ();
2018-04-18 11:13:22 +02:00
var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
separator.show ();
2018-05-18 23:14:12 +02:00
var widget = new StatusWidget (ref status);
2018-04-18 11:13:22 +02:00
widget.separator = separator;
2018-04-26 16:05:03 +02:00
widget.button_press_event.connect(widget.open);
2018-05-21 17:23:31 +02:00
if (!is_status_owned (ref status))
widget.avatar.button_press_event.connect(widget.open_account);
2018-04-18 11:13:22 +02:00
view.pack_start(separator, false, false, 0);
2018-04-16 22:36:57 +02:00
view.pack_start(widget, false, false, 0);
2018-05-19 18:01:54 +02:00
if (first) {
2018-05-19 18:01:54 +02:00
view.reorder_child (widget, 0);
view.reorder_child (separator, 0);
}
2018-04-14 14:09:06 +02:00
}
public override void clear () {
this.page_prev = null;
this.page_next = null;
this.is_last_page = false;
base.clear ();
}
public void get_pages (string? header) {
page_next = page_prev = null;
2018-05-19 15:55:41 +02:00
if (header == null)
return;
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;
}
is_last_page = page_prev != null & page_next == null;
}
2018-05-05 17:38:01 +02:00
public virtual string get_url () {
if (page_next != null)
return page_next;
2018-04-26 16:05:03 +02:00
2018-05-27 18:25:16 +02:00
var url = "%s/api/v1/timelines/%s?limit=%i".printf (Tootle.accounts.formal.instance, this.timeline, this.limit);
url += this.pars;
2018-04-26 16:05:03 +02:00
return url;
}
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;
}
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-04-27 20:50:08 +02:00
Tootle.network.queue(msg, (sess, mess) => {
2018-04-14 14:09:06 +02:00
try{
2018-04-27 20:50:08 +02:00
Tootle.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-05-27 19:14:48 +02:00
append (ref 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);
}
});
}
2018-04-26 16:05:03 +02:00
2018-05-03 10:11:31 +02:00
public virtual void on_refresh (){
clear ();
request ();
}
2018-04-26 16:05:03 +02:00
public virtual void on_account_changed (Account? account){
if(account == null)
return;
2018-05-05 17:38:01 +02:00
on_refresh ();
2018-04-26 16:05:03 +02:00
}
public override void 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
}