From 21ae42762d8a57da5cb1ec40b46e7510fc3121ad Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 20 Jun 2024 12:05:20 +0200 Subject: Redesign and rewrite accounts and settings dialog into a combined one --- .../account_preferences_subpage.vala | 264 +++++++++++++++++++++ .../accounts_preferences_page.vala | 75 ++++++ .../encryption_preferences_page.vala | 59 +++++ .../general_preferences_page.vala | 39 +++ .../preferences_window/preferences_window.vala | 31 +++ 5 files changed, 468 insertions(+) create mode 100644 main/src/windows/preferences_window/account_preferences_subpage.vala create mode 100644 main/src/windows/preferences_window/accounts_preferences_page.vala create mode 100644 main/src/windows/preferences_window/encryption_preferences_page.vala create mode 100644 main/src/windows/preferences_window/general_preferences_page.vala create mode 100644 main/src/windows/preferences_window/preferences_window.vala (limited to 'main/src/windows') diff --git a/main/src/windows/preferences_window/account_preferences_subpage.vala b/main/src/windows/preferences_window/account_preferences_subpage.vala new file mode 100644 index 00000000..462fc8e1 --- /dev/null +++ b/main/src/windows/preferences_window/account_preferences_subpage.vala @@ -0,0 +1,264 @@ +using Dino.Entities; +using Xmpp; +using Xmpp.Xep; +using Gee; +using Gtk; +using Gdk; + +[GtkTemplate (ui = "/im/dino/Dino/preferences_window_account.ui")] +public class Dino.Ui.AccountPreferencesSubpage : Gtk.Box { + + [GtkChild] public unowned Button back_button; + [GtkChild] public unowned AvatarPicture avatar; + [GtkChild] public unowned Adw.ActionRow xmpp_address; + [GtkChild] public unowned Adw.ActionRow local_alias; // TODO replace with EntryRow once we require Adw 1.2 + [GtkChild] public unowned Entry local_alias_entry; + [GtkChild] public unowned Adw.ActionRow connection_status; + [GtkChild] public unowned Button enter_password_button; + [GtkChild] public unowned Box avatar_menu_box; + [GtkChild] public unowned Button edit_avatar_button; + [GtkChild] public unowned Button remove_avatar_button; + [GtkChild] public unowned Widget button_container; + [GtkChild] public unowned Button remove_account_button; + [GtkChild] public unowned Button disable_account_button; + + public Account account { get { return model.selected_account.account; } } + public ViewModel.PreferencesWindow model { get; set; } + + private Binding[] bindings = new Binding[0]; + private ulong[] account_notify_ids = new ulong[0]; + private ulong alias_entry_changed = 0; + + construct { + button_container.layout_manager = new NaturalDirectionBoxLayout((BoxLayout)button_container.layout_manager); + back_button.clicked.connect(() => { + var window = (Adw.PreferencesWindow) this.get_root(); + window.close_subpage(); + }); + edit_avatar_button.clicked.connect(() => { + show_select_avatar(); + }); + remove_avatar_button.clicked.connect(() => { + model.remove_avatar(account); + }); + disable_account_button.clicked.connect(() => { + model.enable_disable_account(account); + }); + remove_account_button.clicked.connect(() => { + show_remove_account_dialog(); + }); + enter_password_button.clicked.connect(() => { + + var password = new PasswordEntry() { show_peek_icon=true }; +#if Adw_1_2 + var dialog = new Adw.MessageDialog((Window)this.get_root(), "Enter password for %s".printf(account.bare_jid.to_string()), null); + dialog.response.connect((response) => { + if (response == "connect") { + account.password = password.text; + model.reconnect_account(account); + } + }); + dialog.set_default_response("connect"); + dialog.set_extra_child(password); + dialog.add_response("cancel", _("Cancel")); + dialog.add_response("connect", _("Connect")); +#else + Gtk.MessageDialog dialog = new Gtk.MessageDialog ( + (Window)this.get_root(), Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, + Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, + "Enter password for %s", account.bare_jid.to_string()); + Button ok_button = dialog.get_widget_for_response(ResponseType.OK) as Button; + ok_button.label = _("Connect"); + + dialog.response.connect((response) => { + if (response == ResponseType.OK) { + account.password = password.text; + model.reconnect_account(account); + } + dialog.close(); + }); + dialog.get_content_area().append(password); +#endif + + dialog.present(); + }); + + this.notify["model"].connect(() => { + model.notify["selected-account"].connect(() => { + foreach (var binding in bindings) { + binding.unbind(); + } + + avatar.model = model.selected_account.avatar_model; + xmpp_address.subtitle = account.bare_jid.to_string(); + + if (alias_entry_changed != 0) local_alias_entry.disconnect(alias_entry_changed); + local_alias_entry.text = account.alias ?? ""; + alias_entry_changed = local_alias_entry.changed.connect(() => { + account.alias = local_alias_entry.text; + }); + + bindings += account.bind_property("enabled", disable_account_button, "label", BindingFlags.SYNC_CREATE, (binding, from, ref to) => { + bool enabled_bool = (bool) from; + to = enabled_bool ? _("Disable account") : _("Enable account"); + return true; + }); + bindings += account.bind_property("enabled", avatar_menu_box, "visible", BindingFlags.SYNC_CREATE); + bindings += account.bind_property("enabled", connection_status, "visible", BindingFlags.SYNC_CREATE); + bindings += model.selected_account.bind_property("connection-state", connection_status, "subtitle", BindingFlags.SYNC_CREATE, (binding, from, ref to) => { + to = get_status_label(); + return true; + }); + bindings += model.selected_account.bind_property("connection-error", connection_status, "subtitle", BindingFlags.SYNC_CREATE, (binding, from, ref to) => { + to = get_status_label(); + return true; + }); + bindings += model.selected_account.bind_property("connection-error", enter_password_button, "visible", BindingFlags.SYNC_CREATE, (binding, from, ref to) => { + var error = (ConnectionManager.ConnectionError) from; + to = error != null && error.source == ConnectionManager.ConnectionError.Source.SASL; + return true; + }); + + model.selected_account.notify["connection-error"].connect(() => { + // TODO doesn't work + if (model.selected_account.connection_error != null) { + connection_status.add_css_class("error"); + } else { + connection_status.remove_css_class("error"); + } + }); + if (model.selected_account.connection_error != null) { + connection_status.add_css_class("error"); + } else { + connection_status.remove_css_class("error"); + } + }); + }); + } + + private void show_select_avatar() { + FileChooserNative chooser = new FileChooserNative(_("Select avatar"), (Window)this.get_root(), FileChooserAction.OPEN, _("Select"), _("Cancel")); + FileFilter filter = new FileFilter(); + foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) { + foreach (string mime_type in pixbuf_format.get_mime_types()) { + filter.add_mime_type(mime_type); + } + } + filter.set_filter_name(_("Images")); + chooser.add_filter(filter); + + filter = new FileFilter(); + filter.set_filter_name(_("All files")); + filter.add_pattern("*"); + chooser.add_filter(filter); + + chooser.response.connect(() => { + string uri = chooser.get_file().get_path(); + model.set_avatar_uri(account, uri); + }); + + chooser.show(); + } + + private void show_remove_account_dialog() { + Gtk.MessageDialog msg = new Gtk.MessageDialog ( + (Window)this.get_root(), Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, + Gtk.MessageType.WARNING, Gtk.ButtonsType.OK_CANCEL, + _("Remove account %s?"), account.bare_jid.to_string()); + msg.secondary_text = "You won't be able to access your conversation history anymore."; // TODO remove history! + Button ok_button = msg.get_widget_for_response(ResponseType.OK) as Button; + ok_button.label = _("Remove"); + ok_button.add_css_class("destructive-action"); + msg.response.connect((response) => { + if (response == ResponseType.OK) { + model.remove_account(account); + // Close the account subpage + var window = (Adw.PreferencesWindow) this.get_root(); + window.close_subpage(); +// window.pop_subpage(); + } + msg.close(); + }); + msg.present(); + } + + private string get_status_label() { + string? error_label = get_connection_error_description(); + if (error_label != null) return error_label; + + ConnectionManager.ConnectionState state = model.selected_account.connection_state; + switch (state) { + case ConnectionManager.ConnectionState.CONNECTING: + return _("Connecting…"); + case ConnectionManager.ConnectionState.CONNECTED: + return _("Connected"); + case ConnectionManager.ConnectionState.DISCONNECTED: + return _("Disconnected"); + } + assert_not_reached(); + } + + private string? get_connection_error_description() { + ConnectionManager.ConnectionError? error = model.selected_account.connection_error; + if (error == null) return null; + + switch (error.source) { + case ConnectionManager.ConnectionError.Source.SASL: + return _("Wrong password"); + case ConnectionManager.ConnectionError.Source.TLS: + return _("Invalid TLS certificate"); + } + if (error.identifier != null) { + return _("Error") + ": " + error.identifier; + } else { + return _("Error"); + } + } +} + +public class Dino.Ui.NaturalDirectionBoxLayout : LayoutManager { + private BoxLayout original; + private BoxLayout alternative; + + public NaturalDirectionBoxLayout(BoxLayout original) { + this.original = original; + if (original.orientation == Orientation.HORIZONTAL) { + this.alternative = new BoxLayout(Orientation.VERTICAL); + this.alternative.spacing = this.original.spacing / 2; + } + } + + public override SizeRequestMode get_request_mode(Widget widget) { + return original.orientation == Orientation.HORIZONTAL ? SizeRequestMode.HEIGHT_FOR_WIDTH : SizeRequestMode.WIDTH_FOR_HEIGHT; + } + + public override void allocate(Widget widget, int width, int height, int baseline) { + int blind_minimum, blind_natural, blind_minimum_baseline, blind_natural_baseline; + original.measure(widget, original.orientation, -1, out blind_minimum, out blind_natural, out blind_minimum_baseline, out blind_natural_baseline); + int for_size = (original.orientation == Orientation.HORIZONTAL ? width : height); + if (for_size >= blind_minimum) { + original.allocate(widget, width, height, baseline); + } else { + alternative.allocate(widget, width, height, baseline); + } + } + + public override void measure(Widget widget, Orientation orientation, int for_size, out int minimum, out int natural, out int minimum_baseline, out int natural_baseline) { + if (for_size == -1) { + original.measure(widget, orientation, -1, out minimum, out natural, out minimum_baseline, out natural_baseline); + int alt_minimum, alt_natural, alt_minimum_baseline, alt_natural_baseline; + alternative.measure(widget, orientation, -1, out alt_minimum, out alt_natural, out alt_minimum_baseline, out alt_natural_baseline); + if (alt_minimum < minimum && alt_minimum != -1) minimum = alt_minimum; + if (alt_minimum_baseline < minimum_baseline && alt_minimum_baseline != -1) minimum = alt_minimum_baseline; + } else { + Orientation other_orientation = orientation == Orientation.HORIZONTAL ? Orientation.VERTICAL : Orientation.HORIZONTAL; + int blind_minimum, blind_natural, blind_minimum_baseline, blind_natural_baseline; + original.measure(widget, other_orientation, -1, out blind_minimum, out blind_natural, out blind_minimum_baseline, out blind_natural_baseline); + if (for_size >= blind_minimum) { + original.measure(widget, orientation, for_size, out minimum, out natural, out minimum_baseline, out natural_baseline); + } else { + alternative.measure(widget, orientation, for_size, out minimum, out natural, out minimum_baseline, out natural_baseline); + } + } + } +} \ No newline at end of file diff --git a/main/src/windows/preferences_window/accounts_preferences_page.vala b/main/src/windows/preferences_window/accounts_preferences_page.vala new file mode 100644 index 00000000..7f4c2f1b --- /dev/null +++ b/main/src/windows/preferences_window/accounts_preferences_page.vala @@ -0,0 +1,75 @@ +using Dino.Entities; +using Gee; +using Gtk; + +public class Dino.Ui.PreferencesWindowAccounts : Adw.PreferencesPage { + + public signal void account_chosen(Account account); + + public Adw.PreferencesGroup active_accounts; + public Adw.PreferencesGroup disabled_accounts; + + public ViewModel.PreferencesWindow model { get; set; } + + construct { + this.title = _("Accounts"); + this.icon_name = "system-users-symbolic"; + + this.notify["model"].connect(() => { + model.update.connect(refresh); + }); + } + + private void refresh() { + if (active_accounts != null) this.remove(active_accounts); + if (disabled_accounts != null) this.remove(disabled_accounts); + + active_accounts = new Adw.PreferencesGroup() { title=_("Accounts")}; + disabled_accounts = new Adw.PreferencesGroup() { title=_("Disabled accounts")}; + Button add_account_button = new Button.from_icon_name("list-add-symbolic"); + add_account_button.add_css_class("flat"); + add_account_button.tooltip_text = _("Add Account"); + active_accounts.header_suffix = add_account_button; + + this.add(active_accounts); + this.add(disabled_accounts); + + add_account_button.clicked.connect(() => { + Ui.ManageAccounts.AddAccountDialog add_account_dialog = new Ui.ManageAccounts.AddAccountDialog(model.stream_interactor, model.db); + add_account_dialog.set_transient_for((Window)this.get_root()); + add_account_dialog.added.connect((account) => { + refresh(); + }); + add_account_dialog.present(); + }); + + disabled_accounts.visible = false; // Only display disabled section if it contains accounts + var enabled_account_added = false; + + foreach (ViewModel.AccountDetails account_details in model.account_details.values) { + var row = new Adw.ActionRow() { + title = account_details.bare_jid.to_string() + }; + row.add_prefix(new AvatarPicture() { valign=Align.CENTER, height_request=35, width_request=35, model = account_details.avatar_model }); + row.add_suffix(new Image.from_icon_name("go-next-symbolic")); + row.activatable = true; + + if (account_details.account.enabled) { + active_accounts.add(row); + enabled_account_added = true; + } else { + disabled_accounts.add(row); + disabled_accounts.visible = true; + } + + row.activated.connect(() => { + account_chosen(account_details.account); + }); + } + + // We always have to show the active accounts group for the add new button. Display placeholder if there are no active accounts + if (!enabled_account_added) { + active_accounts.add(new Adw.ActionRow() { title=_("No active accounts") }); + } + } +} diff --git a/main/src/windows/preferences_window/encryption_preferences_page.vala b/main/src/windows/preferences_window/encryption_preferences_page.vala new file mode 100644 index 00000000..2222584d --- /dev/null +++ b/main/src/windows/preferences_window/encryption_preferences_page.vala @@ -0,0 +1,59 @@ +using Dino.Entities; +using Xmpp; +using Xmpp.Xep; +using Gee; +using Gtk; + +//[GtkTemplate (ui = "/im/dino/Dino/preferences_window_encryption.ui")] +public class Dino.Ui.PreferencesWindowEncryption : Adw.PreferencesPage { + + private DropDown drop_down = null; + private Adw.PreferencesGroup accounts_group = new Adw.PreferencesGroup(); + private ArrayList added_widgets = new ArrayList(); + + public ViewModel.PreferencesWindow model { get; set; } + + construct { + this.add(accounts_group); + + this.notify["model"].connect(() => { + this.model.update.connect(() => { + if (drop_down != null) { + accounts_group.remove(drop_down); + drop_down = null; + } + + if (model.active_accounts_selection.get_n_items() > 0) { + drop_down = new DropDown(model.active_accounts_selection, null) { halign=Align.CENTER }; + drop_down.factory = new BuilderListItemFactory.from_resource(null, "/im/dino/Dino/account_picker_row.ui"); + + drop_down.notify["selected-item"].connect(() => { + var account_details = (ViewModel.AccountDetails) drop_down.selected_item; + if (account_details == null) return; + set_account(account_details.account); + }); + + drop_down.selected = 0; + set_account(((ViewModel.AccountDetails)model.active_accounts_selection.get_item(0)).account); + } else { + drop_down = new DropDown.from_strings(new string[] { _("No active accounts")}) { halign=Align.CENTER }; + } + accounts_group.add(drop_down); + }); + }); + } + + public void set_account(Account account) { + foreach (var widget in added_widgets) { + this.remove(widget); + } + added_widgets.clear(); + + Application app = GLib.Application.get_default() as Application; + foreach (Plugins.EncryptionPreferencesEntry e in app.plugin_registry.encryption_preferences_entries) { + var widget = (Adw.PreferencesGroup) e.get_widget(account, Plugins.WidgetType.GTK4); + this.add(widget); + this.added_widgets.add(widget); + } + } +} \ No newline at end of file diff --git a/main/src/windows/preferences_window/general_preferences_page.vala b/main/src/windows/preferences_window/general_preferences_page.vala new file mode 100644 index 00000000..7aa6c2bd --- /dev/null +++ b/main/src/windows/preferences_window/general_preferences_page.vala @@ -0,0 +1,39 @@ +using Gtk; + +public class Dino.Ui.ViewModel.GeneralPreferencesPage : Object { + public bool send_typing { get; set; } + public bool send_marker { get; set; } + public bool notifications { get; set; } + public bool convert_emojis { get; set; } +} + +[GtkTemplate (ui = "/im/dino/Dino/preferences_window_general.ui")] +public class Dino.Ui.GeneralPreferencesPage : Adw.PreferencesPage { + [GtkChild] private unowned Switch typing_switch; + [GtkChild] private unowned Switch marker_switch; + [GtkChild] private unowned Switch notification_switch; + [GtkChild] private unowned Switch emoji_switch; + + public ViewModel.GeneralPreferencesPage model { get; set; default = new ViewModel.GeneralPreferencesPage(); } + private Binding[] model_bindings = new Binding[0]; + + construct { + this.notify["model"].connect(on_model_changed); + } + + private void on_model_changed() { + foreach (Binding binding in model_bindings) { + binding.unbind(); + } + if (model != null) { + model_bindings = new Binding[] { + model.bind_property("send-typing", typing_switch, "active", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL), + model.bind_property("send-marker", marker_switch, "active", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL), + model.bind_property("notifications", notification_switch, "active", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL), + model.bind_property("convert-emojis", emoji_switch, "active", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL) + }; + } else { + model_bindings = new Binding[0]; + } + } +} diff --git a/main/src/windows/preferences_window/preferences_window.vala b/main/src/windows/preferences_window/preferences_window.vala new file mode 100644 index 00000000..e34261e9 --- /dev/null +++ b/main/src/windows/preferences_window/preferences_window.vala @@ -0,0 +1,31 @@ +using Gdk; +using Dino.Entities; +using Xmpp; +using Xmpp.Xep; +using Gee; +using Gtk; + +[GtkTemplate (ui = "/im/dino/Dino/preferences_window.ui")] +public class Dino.Ui.PreferencesWindow : Adw.PreferencesWindow { + [GtkChild] public unowned Dino.Ui.PreferencesWindowAccounts accounts_page; + [GtkChild] public unowned Dino.Ui.PreferencesWindowEncryption encryption_page; + [GtkChild] public unowned Dino.Ui.GeneralPreferencesPage general_page; + public Dino.Ui.AccountPreferencesSubpage account_page = new Dino.Ui.AccountPreferencesSubpage(); + + [GtkChild] public unowned ViewModel.PreferencesWindow model { get; } + + construct { + this.default_height = 500; + this.default_width = 700; + this.can_navigate_back = true; // remove once we require Adw > 1.4 + this.bind_property("model", accounts_page, "model", BindingFlags.SYNC_CREATE); + this.bind_property("model", account_page, "model", BindingFlags.SYNC_CREATE); + this.bind_property("model", encryption_page, "model", BindingFlags.SYNC_CREATE); + + accounts_page.account_chosen.connect((account) => { + model.selected_account = model.account_details[account]; + this.present_subpage(account_page); +// this.present_subpage(new Adw.NavigationPage(account_page, "Account: %s".printf(account.bare_jid.to_string()))); + }); + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2