2018-04-17 14:01:55 +02:00
|
|
|
using Gtk;
|
|
|
|
using Gdk;
|
|
|
|
|
2018-05-19 14:21:03 +02:00
|
|
|
public class Tootle.NotificationsView : AbstractView {
|
2018-04-17 14:01:55 +02:00
|
|
|
|
2018-10-29 16:43:34 +01:00
|
|
|
private int64 last_id = 0;
|
|
|
|
private bool force_dot = false;
|
|
|
|
|
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);
|
2018-10-23 12:05:24 +02:00
|
|
|
accounts.switched.connect (on_account_changed);
|
|
|
|
app.refresh.connect (on_refresh);
|
2018-06-17 10:40:24 +02:00
|
|
|
network.notification.connect (prepend);
|
2018-05-05 17:38:01 +02:00
|
|
|
|
|
|
|
request ();
|
2018-04-17 14:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 16:43:34 +01:00
|
|
|
private bool has_unread () {
|
|
|
|
if (accounts.formal == null)
|
|
|
|
return false;
|
2018-10-28 13:54:09 +01:00
|
|
|
else
|
2018-10-29 16:43:34 +01:00
|
|
|
return last_id > accounts.formal.last_seen_notification || force_dot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string get_icon () {
|
|
|
|
if (has_unread ())
|
2018-10-28 13:54:09 +01:00
|
|
|
return Desktop.fallback_icon ("notification-new-symbolic", "user-available-symbolic");
|
2018-10-29 16:43:34 +01:00
|
|
|
else
|
|
|
|
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-10-23 12:05:24 +02:00
|
|
|
public void prepend (Notification notification) {
|
|
|
|
append (notification, true);
|
2018-05-24 21:08:28 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:24 +02:00
|
|
|
public void append (Notification notification, bool reverse = false) {
|
2018-05-19 15:55:41 +02:00
|
|
|
if (empty != null)
|
|
|
|
empty.destroy ();
|
|
|
|
|
2018-10-27 10:55:40 +02:00
|
|
|
var separator = new Gtk.Separator (Orientation.HORIZONTAL);
|
2018-04-18 11:13:22 +02:00
|
|
|
separator.show ();
|
|
|
|
|
2018-10-23 12:05:24 +02:00
|
|
|
var widget = new NotificationWidget (notification);
|
2018-04-18 11:13:22 +02:00
|
|
|
widget.separator = separator;
|
2018-10-27 10:55:40 +02:00
|
|
|
view.pack_start (separator, false, false, 0);
|
|
|
|
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-10-29 16:43:34 +01:00
|
|
|
force_dot = true;
|
|
|
|
accounts.formal.has_unread_notifications = force_dot;
|
2018-05-24 21:08:28 +02:00
|
|
|
}
|
2018-10-28 13:54:09 +01:00
|
|
|
|
2018-10-29 16:43:34 +01:00
|
|
|
if (notification.id > last_id)
|
|
|
|
last_id = notification.id;
|
|
|
|
|
|
|
|
if (has_unread ()) {
|
2018-10-28 13:54:09 +01:00
|
|
|
accounts.save ();
|
2018-10-29 16:43:34 +01:00
|
|
|
image.icon_name = get_icon ();
|
2018-10-28 13:54:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void on_set_current () {
|
|
|
|
var account = accounts.formal;
|
2018-10-29 16:43:34 +01:00
|
|
|
if (has_unread ()) {
|
|
|
|
force_dot = false;
|
|
|
|
account.has_unread_notifications = force_dot;
|
|
|
|
account.last_seen_notification = last_id;
|
2018-10-28 13:54:09 +01:00
|
|
|
accounts.save ();
|
2018-10-29 16:43:34 +01:00
|
|
|
image.icon_name = get_icon ();
|
2018-10-28 13:54:09 +01:00
|
|
|
}
|
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-10-29 16:43:34 +01:00
|
|
|
|
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)
|
2018-04-22 12:33:19 +02:00
|
|
|
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-10-23 12:05:24 +02:00
|
|
|
if (account == null)
|
2018-04-17 14:01:55 +02:00
|
|
|
return;
|
2018-05-05 17:38:01 +02:00
|
|
|
|
2018-10-29 16:43:34 +01:00
|
|
|
last_id = accounts.formal.last_seen_notification;
|
|
|
|
force_dot = accounts.formal.has_unread_notifications;
|
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-10-23 12:05:24 +02:00
|
|
|
var msg = new Soup.Message ("GET", url);
|
|
|
|
network.queue (msg, (sess, mess) => {
|
|
|
|
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){
|
2018-10-23 12:05:24 +02:00
|
|
|
var notification = Notification.parse_follow_request (obj);
|
|
|
|
append (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-10-23 12:05:24 +02:00
|
|
|
var msg2 = new Soup.Message ("GET", url2);
|
|
|
|
network.queue (msg2, (sess, mess) => {
|
|
|
|
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){
|
2018-10-23 12:05:24 +02:00
|
|
|
var notification = Notification.parse (obj);
|
|
|
|
append (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
|
|
|
}
|
|
|
|
|
|
|
|
}
|