From 387433ebb9bab442502f812e0364111f37270bcb Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 30 May 2017 22:31:05 +0200 Subject: [PATCH] Notifications + typing notifications + message marker settings per conversation --- libdino/data/dino.gschema.xml | 14 ++++++-- libdino/src/entity/conversation.vala | 44 ++++++++++++++++++++++- libdino/src/service/chat_interaction.vala | 4 +-- libdino/src/service/database.vala | 7 ++-- libdino/src/settings.vala | 16 +++++++-- main/data/settings_dialog.ui | 30 ++++++++++++++-- main/src/ui/notifications.vala | 12 ++++++- main/src/ui/settings_dialog.vala | 10 ++++-- 8 files changed, 121 insertions(+), 16 deletions(-) diff --git a/libdino/data/dino.gschema.xml b/libdino/data/dino.gschema.xml index f3d342cf..19cad0e4 100644 --- a/libdino/data/dino.gschema.xml +++ b/libdino/data/dino.gschema.xml @@ -1,9 +1,19 @@ - + true - Whether to confirm that a message was received per default + Whether to send typing notifications + + + + true + Whether to confirm that a message was received or read + + + + true + Whether to get notifications diff --git a/libdino/src/entity/conversation.vala b/libdino/src/entity/conversation.vala index e820af4d..d4f2f7a7 100644 --- a/libdino/src/entity/conversation.vala +++ b/libdino/src/entity/conversation.vala @@ -28,6 +28,14 @@ public class Conversation : Object { public Type type_ { get; set; } public Message read_up_to { get; set; } + public enum NotifySetting { DEFAULT, ON, OFF, HIGHLIGHT } + public NotifySetting notify_setting { get; set; default = NotifySetting.DEFAULT; } + + public enum Setting { DEFAULT, ON, OFF } + public Setting send_typing { get; set; default = Setting.DEFAULT; } + + public Setting send_marker { get; set; default = Setting.DEFAULT; } + private Database? db; public Conversation(Jid jid, Account account, Type type) { @@ -51,6 +59,9 @@ public class Conversation : Object { encryption = (Encryption) row[db.conversation.encryption]; int? read_up_to = row[db.conversation.read_up_to]; if (read_up_to != null) this.read_up_to = db.get_message_by_id(read_up_to); + notify_setting = (NotifySetting) row[db.conversation.notification]; + send_typing = (Setting) row[db.conversation.send_typing]; + send_marker = (Setting) row[db.conversation.send_marker]; notify.connect(on_update); } @@ -62,7 +73,7 @@ public class Conversation : Object { .value(db.conversation.jid_id, db.get_jid_id(counterpart)) .value(db.conversation.type_, type_) .value(db.conversation.encryption, encryption) - //.value(conversation.read_up_to, new_conversation.read_up_to) + .value(db.conversation.read_up_to, read_up_to.id) .value(db.conversation.active, active); if (counterpart.is_full()) { insert.value(db.conversation.resource, counterpart.resourcepart); @@ -70,10 +81,35 @@ public class Conversation : Object { if (last_active != null) { insert.value(db.conversation.last_active, (long) last_active.to_unix()); } + insert.value(db.conversation.notification, notify_setting); + insert.value(db.conversation.send_typing, send_typing); + insert.value(db.conversation.send_marker, send_marker); id = (int) insert.perform(); notify.connect(on_update); } + public NotifySetting get_notification_setting(StreamInteractor stream_interactor) { + Xmpp.Core.XmppStream? stream = stream_interactor.get_stream(account); + if (notify_setting != NotifySetting.DEFAULT) return notify_setting; + if (!Settings.instance().notifications) return NotifySetting.OFF; + if (type_ == Type.GROUPCHAT) { + bool members_only = stream.get_flag(Xmpp.Xep.Muc.Flag.IDENTITY).has_room_feature(counterpart.bare_jid.to_string(), Xmpp.Xep.Muc.Feature.MEMBERS_ONLY); + return members_only ? NotifySetting.ON : NotifySetting.HIGHLIGHT; + } else { + return NotifySetting.ON; + } + } + + public Setting get_send_typing_setting() { + if (send_typing != Setting.DEFAULT) return send_typing; + return Settings.instance().send_typing ? Setting.ON : Setting.OFF; + } + + public Setting get_send_marker_setting() { + if (send_marker != Setting.DEFAULT) return send_marker; + return Settings.instance().send_marker ? Setting.ON : Setting.OFF; + } + public bool equals(Conversation? conversation) { if (conversation == null) return false; return equals_func(this, conversation); @@ -110,6 +146,12 @@ public class Conversation : Object { update.set_null(db.conversation.last_active); } break; + case "notify-setting": + update.set(db.conversation.notification, notify_setting); break; + case "send-typing": + update.set(db.conversation.send_typing, send_typing); break; + case "send-marker": + update.set(db.conversation.send_marker, send_marker); break; } update.perform(); } diff --git a/libdino/src/service/chat_interaction.vala b/libdino/src/service/chat_interaction.vala index 891abf29..f13623ea 100644 --- a/libdino/src/service/chat_interaction.vala +++ b/libdino/src/service/chat_interaction.vala @@ -135,7 +135,7 @@ public class ChatInteraction : StreamInteractionModule, Object { private void send_chat_marker(Conversation conversation, Entities.Message message, string marker) { Core.XmppStream stream = stream_interactor.get_stream(conversation.account); - if (stream != null && Settings.instance().send_read && + if (stream != null && Settings.instance().send_marker && Xep.ChatMarkers.Module.requests_marking(message.stanza)) { stream.get_module(Xep.ChatMarkers.Module.IDENTITY).send_marker(stream, message.stanza.from, message.stanza_id, message.get_type_string(), marker); } @@ -143,7 +143,7 @@ public class ChatInteraction : StreamInteractionModule, Object { private void send_chat_state_notification(Conversation conversation, string state) { Core.XmppStream stream = stream_interactor.get_stream(conversation.account); - if (stream != null && Settings.instance().send_read && + if (stream != null && Settings.instance().send_typing && conversation.type_ != Conversation.Type.GROUPCHAT) { stream.get_module(Xep.ChatStateNotifications.Module.IDENTITY).send_state(stream, conversation.counterpart.to_string(), state); } diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 19ca0b06..ae8d5412 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -6,7 +6,7 @@ using Dino.Entities; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 2; + private const int VERSION = 3; public class AccountTable : Table { public Column id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -86,10 +86,13 @@ public class Database : Qlite.Database { public Column type_ = new Column.Integer("type"); public Column encryption = new Column.Integer("encryption"); public Column read_up_to = new Column.Integer("read_up_to"); + public Column notification = new Column.Integer("notification") { min_version=3 }; + public Column send_typing = new Column.Integer("send_typing") { min_version=3 }; + public Column send_marker = new Column.Integer("send_marker") { min_version=3 }; internal ConversationTable(Database db) { base(db, "conversation"); - init({id, account_id, jid_id, resource, active, last_active, type_, encryption, read_up_to}); + init({id, account_id, jid_id, resource, active, last_active, type_, encryption, read_up_to, notification, send_typing, send_marker}); } } diff --git a/libdino/src/settings.vala b/libdino/src/settings.vala index 3700583b..45d2192f 100644 --- a/libdino/src/settings.vala +++ b/libdino/src/settings.vala @@ -4,9 +4,19 @@ public class Settings { private GLib.Settings gsettings; - public bool send_read { - get { return gsettings.get_boolean("send-read"); } - set { gsettings.set_boolean("send-read", value); } + public bool send_typing { + get { return gsettings.get_boolean("send-typing"); } + set { gsettings.set_boolean("send-typing", value); } + } + + public bool send_marker { + get { return gsettings.get_boolean("send-marker"); } + set { gsettings.set_boolean("send-marker", value); } + } + + public bool notifications { + get { return gsettings.get_boolean("notifications"); } + set { gsettings.set_boolean("notifications", value); } } public bool convert_utf8_smileys { diff --git a/main/data/settings_dialog.ui b/main/data/settings_dialog.ui index 42448cc1..9d18bdc7 100644 --- a/main/data/settings_dialog.ui +++ b/main/data/settings_dialog.ui @@ -18,8 +18,8 @@ 10 True - - Send typing notifications and message marker + + Send typing notifications True @@ -29,6 +29,30 @@ 1 + + + Send message marker + True + + + 0 + 1 + 1 + 1 + + + + + Notify when a new message arrives + True + + + 0 + 2 + 1 + 1 + + Convert smileys to emojis @@ -36,7 +60,7 @@ 0 - 1 + 3 1 1 diff --git a/main/src/ui/notifications.vala b/main/src/ui/notifications.vala index e5eda271..32e8d7ad 100644 --- a/main/src/ui/notifications.vala +++ b/main/src/ui/notifications.vala @@ -31,6 +31,8 @@ public class Notifications : Object { } private void on_message_received(Entities.Message message, Conversation conversation) { + if (!should_notify_message(message, conversation)) return; + if (!notifications.has_key(conversation)) { notifications[conversation] = new Notify.Notification("", null, null); notifications[conversation].set_hint("transient", true); @@ -74,7 +76,7 @@ public class Notifications : Object { AddConversation.Chat.AddContactDialog dialog = new AddConversation.Chat.AddContactDialog(stream_interactor); dialog.jid = jid.bare_jid.to_string(); dialog.account = account; - dialog.show(); + dialog.present(); } try { notification.close(); @@ -90,6 +92,14 @@ public class Notifications : Object { notification.show(); } catch (Error error) { } } + + private bool should_notify_message(Entities.Message message, Conversation conversation) { + Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor); + if (notify == Conversation.NotifySetting.OFF) return false; + string? nick = stream_interactor.get_module(MucManager.IDENTITY).get_nick(conversation.counterpart, conversation.account); + if (notify == Conversation.NotifySetting.HIGHLIGHT && nick != null && !message.body.contains(nick)) return false; + return true; + } } } \ No newline at end of file diff --git a/main/src/ui/settings_dialog.vala b/main/src/ui/settings_dialog.vala index 2d3c1efa..44596217 100644 --- a/main/src/ui/settings_dialog.vala +++ b/main/src/ui/settings_dialog.vala @@ -5,7 +5,9 @@ namespace Dino.Ui { [GtkTemplate (ui = "/org/dino-im/settings_dialog.ui")] class SettingsDialog : Dialog { + [GtkChild] private CheckButton typing_checkbutton; [GtkChild] private CheckButton marker_checkbutton; + [GtkChild] private CheckButton notification_checkbutton; [GtkChild] private CheckButton emoji_checkbutton; Dino.Settings settings = Dino.Settings.instance(); @@ -13,10 +15,14 @@ class SettingsDialog : Dialog { public SettingsDialog() { Object(use_header_bar : 1); - marker_checkbutton.active = settings.send_read; + typing_checkbutton.active = settings.send_typing; + marker_checkbutton.active = settings.send_marker; + notification_checkbutton.active = settings.notifications; emoji_checkbutton.active = settings.convert_utf8_smileys; - marker_checkbutton.toggled.connect(() => { settings.send_read = marker_checkbutton.active; }); + typing_checkbutton.toggled.connect(() => { settings.send_typing = typing_checkbutton.active; } ); + marker_checkbutton.toggled.connect(() => { settings.send_marker = marker_checkbutton.active; } ); + notification_checkbutton.toggled.connect(() => { settings.notifications = notification_checkbutton.active; } ); emoji_checkbutton.toggled.connect(() => { settings.convert_utf8_smileys = emoji_checkbutton.active; }); } }