anotherim-desktop/main/src/ui/chat_input/encryption_button.vala

119 lines
4.4 KiB
Vala
Raw Normal View History

2017-08-02 15:29:55 +00:00
using Gtk;
using Gee;
using Dino.Entities;
namespace Dino.Ui {
2022-02-14 13:55:59 +00:00
public class EncryptionButton {
2017-08-02 15:29:55 +00:00
2019-08-02 01:15:12 +00:00
public signal void encryption_changed(Plugins.EncryptionListEntry? encryption_entry);
2022-02-14 13:55:59 +00:00
private MenuButton menu_button;
2017-08-02 15:29:55 +00:00
private Conversation? conversation;
2022-02-14 13:55:59 +00:00
private CheckButton? button_unencrypted;
private Map<CheckButton, Plugins.EncryptionListEntry> encryption_radios = new HashMap<CheckButton, Plugins.EncryptionListEntry>();
private string? current_icon;
2019-05-26 20:18:08 +00:00
private StreamInteractor stream_interactor;
2022-05-14 12:45:59 +00:00
private SimpleAction action;
2019-05-26 20:18:08 +00:00
2022-02-14 13:55:59 +00:00
public EncryptionButton(StreamInteractor stream_interactor, MenuButton menu_button) {
2019-05-26 20:18:08 +00:00
this.stream_interactor = stream_interactor;
2022-02-14 13:55:59 +00:00
this.menu_button = menu_button;
2022-05-14 12:45:59 +00:00
// Build menu model including "Unencrypted" and all registered encryption entries
Menu menu_model = new Menu();
MenuItem unencrypted_item = new MenuItem(_("Unencrypted"), "enc.encryption");
unencrypted_item.set_action_and_target_value("enc.encryption", new Variant.int32(Encryption.NONE));
menu_model.append_item(unencrypted_item);
Application app = GLib.Application.get_default() as Application;
foreach (var e in app.plugin_registry.encryption_list_entries) {
MenuItem item = new MenuItem(e.name, "enc.encryption");
item.set_action_and_target_value("enc.encryption", new Variant.int32(e.encryption));
menu_model.append_item(item);
}
// Create action to act on menu selections (stateful => radio buttons)
SimpleActionGroup action_group = new SimpleActionGroup();
action = new SimpleAction.stateful("encryption", VariantType.INT32, new Variant.int32(Encryption.NONE));
action.activate.connect((parameter) => {
action.set_state(parameter);
this.conversation.encryption = (Encryption) parameter.get_int32();
});
action_group.insert(action);
menu_button.insert_action_group("enc", action_group);
// Create and set popover menu
Gtk.PopoverMenu popover_menu = new Gtk.PopoverMenu.from_model(menu_model);
menu_button.popover = popover_menu;
stream_interactor.get_module(MucManager.IDENTITY).room_info_updated.connect((account, muc_jid) => {
if (conversation != null && conversation.account.equals(account) && conversation.counterpart.equals(muc_jid)) {
update_visibility();
}
});
2022-02-14 13:55:59 +00:00
menu_button.activate.connect(update_encryption_menu_state);
2017-08-02 15:29:55 +00:00
}
2019-08-02 01:15:12 +00:00
private void encryption_button_toggled() {
2022-02-14 13:55:59 +00:00
foreach (CheckButton e in encryption_radios.keys) {
2017-08-02 15:29:55 +00:00
if (e.get_active()) {
conversation.encryption = encryption_radios[e].encryption;
2019-08-02 01:15:12 +00:00
encryption_changed(encryption_radios[e]);
2017-08-02 15:29:55 +00:00
update_encryption_menu_icon();
return;
}
}
2019-08-02 01:15:12 +00:00
// Selected unencrypted
2017-08-02 15:29:55 +00:00
conversation.encryption = Encryption.NONE;
update_encryption_menu_icon();
2019-08-02 01:15:12 +00:00
encryption_changed(null);
2017-08-02 15:29:55 +00:00
}
private void update_encryption_menu_state() {
2022-05-14 12:45:59 +00:00
action.set_state(new Variant.int32(conversation.encryption));
action.change_state(new Variant.int32(conversation.encryption));
2017-08-02 15:29:55 +00:00
}
private void set_icon(string icon) {
if (icon != current_icon) {
2022-02-14 13:55:59 +00:00
menu_button.set_icon_name(icon);
current_icon = icon;
2017-08-02 15:29:55 +00:00
}
}
private void update_encryption_menu_icon() {
set_icon(conversation.encryption == Encryption.NONE ? "changes-allow-symbolic" : "changes-prevent-symbolic");
}
private void update_visibility() {
2020-06-17 12:30:46 +00:00
if (conversation.encryption != Encryption.NONE) {
2022-02-14 13:55:59 +00:00
menu_button.visible = true;
2020-06-17 12:30:46 +00:00
return;
}
switch (conversation.type_) {
case Conversation.Type.CHAT:
2022-02-14 13:55:59 +00:00
menu_button.visible = true;
2020-06-17 12:30:46 +00:00
break;
case Conversation.Type.GROUPCHAT_PM:
2022-02-14 13:55:59 +00:00
menu_button.visible = false;
2020-06-17 12:30:46 +00:00
break;
case Conversation.Type.GROUPCHAT:
2022-02-14 13:55:59 +00:00
menu_button.visible = stream_interactor.get_module(MucManager.IDENTITY).is_private_room(conversation.account, conversation.counterpart);
2020-06-17 12:30:46 +00:00
break;
}
}
2022-02-14 13:55:59 +00:00
public void set_conversation(Conversation conversation) {
2017-08-02 15:29:55 +00:00
this.conversation = conversation;
update_encryption_menu_state();
update_encryption_menu_icon();
update_visibility();
2017-08-02 15:29:55 +00:00
}
}
2022-02-14 13:55:59 +00:00
}