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

120 lines
3.5 KiB
Vala
Raw Normal View History

2018-04-17 14:01:55 +02:00
using Gtk;
using Gdk;
public class Tootle.NotificationsView : AbstractView {
2018-04-17 14:01:55 +02:00
public NotificationsView () {
2018-05-05 17:38:01 +02:00
base ();
2018-04-18 11:13:22 +02:00
view.remove.connect (on_remove);
accounts.switched.connect(on_account_changed);
app.refresh.connect(on_refresh);
network.notification.connect (prepend);
2018-05-05 17:38:01 +02:00
request ();
2018-04-17 14:01:55 +02:00
}
public override string get_icon () {
2018-06-30 18:19:26 +02:00
return Desktop.fallback_icon ("notification-symbolic", "user-invisible-symbolic");
2018-04-17 14:01:55 +02:00
}
public override string get_name () {
2018-05-09 23:46:24 +02:00
return _("Notifications");
2018-04-17 14:01:55 +02:00
}
2018-05-19 18:01:54 +02:00
public void prepend (ref Notification notification) {
2018-05-24 21:08:28 +02:00
append (ref notification, true);
}
public void append (ref Notification notification, bool reverse = 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-04-17 14:01:55 +02:00
var widget = new NotificationWidget(notification);
2018-04-18 11:13:22 +02:00
widget.separator = separator;
2018-06-30 18:19:26 +02:00
image.icon_name = Desktop.fallback_icon ("notification-new-symbolic", "user-available-symbolic");
2018-04-18 11:13:22 +02:00
view.pack_start(separator, false, false, 0);
2018-04-17 14:01:55 +02:00
view.pack_start(widget, false, false, 0);
2018-05-24 21:08:28 +02:00
if (reverse) {
view.reorder_child (widget, 0);
view.reorder_child (separator, 0);
}
2018-04-17 14:01:55 +02:00
}
2018-05-16 13:23:48 +02:00
public virtual void on_remove (Widget widget) {
2018-04-18 11:13:22 +02:00
if (!(widget is NotificationWidget))
return;
2018-05-19 15:55:41 +02:00
empty_state ();
}
public override bool empty_state () {
var is_empty = base.empty_state ();
if (image != null && is_empty)
image.icon_name = get_icon ();
2018-05-19 15:55:41 +02:00
return is_empty;
2018-04-18 11:13:22 +02:00
}
2018-05-03 10:11:31 +02:00
2018-05-16 13:23:48 +02:00
public virtual void on_refresh () {
2018-05-03 10:11:31 +02:00
clear ();
request ();
}
2018-05-16 13:23:48 +02:00
public virtual void on_account_changed (Account? account) {
2018-04-17 14:01:55 +02:00
if(account == null)
return;
2018-05-05 17:38:01 +02:00
on_refresh ();
2018-05-03 10:11:31 +02:00
}
2018-05-16 13:23:48 +02:00
public void request () {
2018-05-31 14:13:21 +02:00
if (accounts.current == null) {
empty_state ();
return;
}
2018-06-30 18:19:26 +02:00
var url = "%s/api/v1/follow_requests".printf (accounts.formal.instance);
2018-04-17 14:01:55 +02:00
var msg = new Soup.Message("GET", url);
2018-06-30 18:19:26 +02:00
network.queue(msg, (sess, mess) => {
2018-04-17 14:01:55 +02:00
try{
2018-06-30 18:19:26 +02:00
network.parse_array (mess).foreach_element ((array, i, node) => {
2018-05-09 17:32:53 +02:00
var obj = node.get_object ();
if (obj != null){
var notification = Notification.parse_follow_request(obj);
2018-05-24 21:08:28 +02:00
append (ref notification);
2018-05-09 17:32:53 +02:00
}
});
}
catch (GLib.Error e) {
warning ("Can't update follow requests");
warning (e.message);
}
});
2018-06-30 18:19:26 +02:00
var url2 = "%s/api/v1/notifications?limit=30".printf (accounts.formal.instance);
2018-05-09 17:32:53 +02:00
var msg2 = new Soup.Message("GET", url2);
2018-06-30 18:19:26 +02:00
network.queue(msg2, (sess, mess) => {
2018-05-09 17:32:53 +02:00
try{
2018-06-30 18:19:26 +02:00
network.parse_array (mess).foreach_element ((array, i, node) => {
2018-05-09 17:32:53 +02:00
var obj = node.get_object ();
if (obj != null){
var notification = Notification.parse(obj);
2018-05-24 21:08:28 +02:00
append (ref notification);
2018-04-17 14:01:55 +02:00
}
});
}
catch (GLib.Error e) {
2018-05-09 17:32:53 +02:00
warning ("Can't update notifications");
2018-04-17 14:01:55 +02:00
warning (e.message);
}
});
2018-05-19 15:55:41 +02:00
empty_state ();
2018-04-17 14:01:55 +02:00
}
}