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 --- main/CMakeLists.txt | 15 +- main/data/account_picker_row.ui | 32 +++ main/data/gresource.xml | 4 + main/data/menu_app.ui | 8 +- main/data/preferences_window.ui | 32 +++ main/data/preferences_window_account.ui | 137 +++++++++++ main/data/preferences_window_general.ui | 68 ++++++ main/data/settings_dialog.ui | 74 ------ main/data/style-dark.css | 2 +- main/data/style.css | 4 +- main/meson.build | 8 +- main/src/ui/application.vala | 33 +-- main/src/ui/notifier_freedesktop.vala | 7 +- main/src/ui/notifier_gnotifications.vala | 1 + main/src/ui/settings_dialog.vala | 30 --- main/src/view_model/account_details.vala | 21 ++ main/src/view_model/preferences_window.vala | 109 +++++++++ .../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 +++ 22 files changed, 920 insertions(+), 133 deletions(-) create mode 100644 main/data/account_picker_row.ui create mode 100644 main/data/preferences_window.ui create mode 100644 main/data/preferences_window_account.ui create mode 100644 main/data/preferences_window_general.ui delete mode 100644 main/data/settings_dialog.ui delete mode 100644 main/src/ui/settings_dialog.vala create mode 100644 main/src/view_model/account_details.vala create mode 100644 main/src/view_model/preferences_window.vala 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') diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index ea4de99b..d5fc66be 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -59,6 +59,7 @@ set(RESOURCE_LIST add_conversation/list_row.ui add_conversation/select_jid_fragment.ui + account_picker_row.ui call_widget.ui chat_input.ui conversation_details.ui @@ -86,9 +87,11 @@ set(RESOURCE_LIST message_item_widget_edit_mode.ui occupant_list.ui occupant_list_item.ui + preferences_window.ui + preferences_window_account.ui + preferences_window_general.ui quote.ui search_autocomplete.ui - settings_dialog.ui unified_main_content.ui unified_window_placeholder.ui @@ -155,7 +158,6 @@ SOURCES src/ui/global_search.vala src/ui/notifier_freedesktop.vala src/ui/notifier_gnotifications.vala - src/ui/settings_dialog.vala src/ui/main_window.vala src/ui/main_window_controller.vala @@ -236,10 +238,19 @@ SOURCES src/ui/widgets/fixed_ratio_picture.vala src/ui/widgets/natural_size_increase.vala + src/view_model/account_details.vala src/view_model/conversation_details.vala src/view_model/preferences_row.vala + src/view_model/preferences_window.vala src/windows/conversation_details.vala + + src/windows/preferences_window/account_preferences_subpage.vala + src/windows/preferences_window/accounts_preferences_page.vala + src/windows/preferences_window/encryption_preferences_page.vala + src/windows/preferences_window/general_preferences_page.vala + src/windows/preferences_window/preferences_window.vala + CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi ${CMAKE_BINARY_DIR}/exports/qlite.vapi diff --git a/main/data/account_picker_row.ui b/main/data/account_picker_row.ui new file mode 100644 index 00000000..a67f7b3b --- /dev/null +++ b/main/data/account_picker_row.ui @@ -0,0 +1,32 @@ + + + + \ No newline at end of file diff --git a/main/data/gresource.xml b/main/data/gresource.xml index 282838e0..f436ce68 100644 --- a/main/data/gresource.xml +++ b/main/data/gresource.xml @@ -1,6 +1,7 @@ + account_picker_row.ui add_conversation/add_contact_dialog.ui add_conversation/add_groupchat_dialog.ui add_conversation/conference_details_fragment.ui @@ -66,6 +67,9 @@ occupant_list.ui occupant_list_item.ui quote.ui + preferences_window.ui + preferences_window_account.ui + preferences_window_general.ui search_autocomplete.ui settings_dialog.ui style-dark.css diff --git a/main/data/menu_app.ui b/main/data/menu_app.ui index bb33ff65..9b85634d 100644 --- a/main/data/menu_app.ui +++ b/main/data/menu_app.ui @@ -3,13 +3,7 @@
- app.accounts - Accounts - -
-
- - app.settings + app.preferences Preferences diff --git a/main/data/preferences_window.ui b/main/data/preferences_window.ui new file mode 100644 index 00000000..d262dd76 --- /dev/null +++ b/main/data/preferences_window.ui @@ -0,0 +1,32 @@ + + + + + + \ No newline at end of file diff --git a/main/data/preferences_window_account.ui b/main/data/preferences_window_account.ui new file mode 100644 index 00000000..4280422d --- /dev/null +++ b/main/data/preferences_window_account.ui @@ -0,0 +1,137 @@ + + + + + \ No newline at end of file diff --git a/main/data/preferences_window_general.ui b/main/data/preferences_window_general.ui new file mode 100644 index 00000000..33d1a2c9 --- /dev/null +++ b/main/data/preferences_window_general.ui @@ -0,0 +1,68 @@ + + + + + diff --git a/main/data/settings_dialog.ui b/main/data/settings_dialog.ui deleted file mode 100644 index a8b24135..00000000 --- a/main/data/settings_dialog.ui +++ /dev/null @@ -1,74 +0,0 @@ - - - - diff --git a/main/data/style-dark.css b/main/data/style-dark.css index 3bd0add0..791ae9c8 100644 --- a/main/data/style-dark.css +++ b/main/data/style-dark.css @@ -1,3 +1,3 @@ -.dino-main .overlay-toolbar { +.overlay-toolbar { background-color: shade(@view_bg_color, 1.5); } \ No newline at end of file diff --git a/main/data/style.css b/main/data/style.css index a7a1d8df..5a70ba83 100644 --- a/main/data/style.css +++ b/main/data/style.css @@ -127,13 +127,13 @@ picture.avatar { /* Overlay Toolbar */ -.dino-main .overlay-toolbar { +.overlay-toolbar { padding: 2px; border-radius: 6px; border-spacing: 0; } -.dino-main .overlay-toolbar > * { +.overlay-toolbar > * { margin-top: 0; margin-bottom: 0; } diff --git a/main/meson.build b/main/meson.build index 1b5abcfc..95deabfd 100644 --- a/main/meson.build +++ b/main/meson.build @@ -77,7 +77,6 @@ sources = files( 'src/ui/occupant_menu/list.vala', 'src/ui/occupant_menu/list_row.vala', 'src/ui/occupant_menu/view.vala', - 'src/ui/settings_dialog.vala', 'src/ui/util/accounts_combo_box.vala', 'src/ui/util/config.vala', 'src/ui/util/data_forms.vala', @@ -89,8 +88,15 @@ sources = files( 'src/ui/widgets/date_separator.vala', 'src/ui/widgets/fixed_ratio_picture.vala', 'src/ui/widgets/natural_size_increase.vala', + 'src/view_model/account_details.vala', 'src/view_model/conversation_details.vala', 'src/view_model/preferences_row.vala', + 'src/view_model/preferences_window.vala', + 'src/windows/preferences_window/account_preferences_subpage.vala', + 'src/windows/preferences_window/accounts_preferences_page.vala', + 'src/windows/preferences_window/encryption_preferences_page.vala', + 'src/windows/preferences_window/general_preferences_page.vala', + 'src/windows/preferences_window/preferences_window.vala', 'src/windows/conversation_details.vala', ) sources += gnome.compile_resources( diff --git a/main/src/ui/application.vala b/main/src/ui/application.vala index 2e785224..d0fde297 100644 --- a/main/src/ui/application.vala +++ b/main/src/ui/application.vala @@ -113,13 +113,17 @@ public class Dino.Ui.Application : Adw.Application, Dino.Application { } private void create_actions() { - SimpleAction accounts_action = new SimpleAction("accounts", null); - accounts_action.activate.connect(show_accounts_window); - add_action(accounts_action); - - SimpleAction settings_action = new SimpleAction("settings", null); - settings_action.activate.connect(show_settings_window); - add_action(settings_action); + SimpleAction preferences_action = new SimpleAction("preferences", null); + preferences_action.activate.connect(show_preferences_window); + add_action(preferences_action); + + SimpleAction preferences_account_action = new SimpleAction("preferences-account", VariantType.INT32); + preferences_account_action.activate.connect((variant) => { + Account? account = db.get_account_by_id(variant.get_int32()); + if (account == null) return; + show_preferences_account_window(account); + }); + add_action(preferences_account_action); SimpleAction about_action = new SimpleAction("about", null); about_action.activate.connect(show_about_window); @@ -233,17 +237,16 @@ public class Dino.Ui.Application : Adw.Application, Dino.Application { return Environment.get_variable("GTK_CSD") != "0"; } - private void show_accounts_window() { - ManageAccounts.Dialog dialog = new ManageAccounts.Dialog(stream_interactor, db); - dialog.set_transient_for(get_active_window()); - dialog.account_enabled.connect(add_connection); - dialog.account_disabled.connect(remove_connection); + private void show_preferences_window() { + Ui.PreferencesWindow dialog = new Ui.PreferencesWindow() { transient_for = window }; + dialog.model.populate(db, stream_interactor); dialog.present(); } - private void show_settings_window() { - SettingsDialog dialog = new SettingsDialog(); - dialog.set_transient_for(get_active_window()); + private void show_preferences_account_window(Account account) { + Ui.PreferencesWindow dialog = new Ui.PreferencesWindow() { transient_for = window }; + dialog.model.populate(db, stream_interactor); + dialog.accounts_page.account_chosen(account); dialog.present(); } diff --git a/main/src/ui/notifier_freedesktop.vala b/main/src/ui/notifier_freedesktop.vala index a1df5990..0d263dba 100644 --- a/main/src/ui/notifier_freedesktop.vala +++ b/main/src/ui/notifier_freedesktop.vala @@ -201,8 +201,13 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { HashTable hash_table = new HashTable(null, null); hash_table["desktop-entry"] = new Variant.string(Dino.Application.get_default().get_application_id()); hash_table["category"] = new Variant.string("im.error"); + string[] actions = new string[] {"default", "Open preferences"}; try { - yield dbus_notifications.notify("Dino", 0, "im.dino.Dino", summary, body, new string[]{}, hash_table, -1); + uint32 notification_id = yield dbus_notifications.notify("Dino", 0, "im.dino.Dino", summary, body, actions, hash_table, -1); + + add_action_listener(notification_id, "default", () => { + GLib.Application.get_default().activate_action("preferences-account", new Variant.int32(account.id)); + }); } catch (Error e) { warning("Failed showing connection error notification: %s", e.message); } diff --git a/main/src/ui/notifier_gnotifications.vala b/main/src/ui/notifier_gnotifications.vala index 4d36620d..462cdf70 100644 --- a/main/src/ui/notifier_gnotifications.vala +++ b/main/src/ui/notifier_gnotifications.vala @@ -102,6 +102,7 @@ namespace Dino.Ui { public async void notify_connection_error(Account account, ConnectionManager.ConnectionError error) { Notification notification = new Notification(_("Could not connect to %s").printf(account.bare_jid.domainpart)); + notification.set_default_action_and_target_value("app.preferences-account", new Variant.int32(account.id)); switch (error.source) { case ConnectionManager.ConnectionError.Source.SASL: notification.set_body("Wrong password"); diff --git a/main/src/ui/settings_dialog.vala b/main/src/ui/settings_dialog.vala deleted file mode 100644 index 3635879c..00000000 --- a/main/src/ui/settings_dialog.vala +++ /dev/null @@ -1,30 +0,0 @@ -using Gtk; - -namespace Dino.Ui { - -[GtkTemplate (ui = "/im/dino/Dino/settings_dialog.ui")] -class SettingsDialog : Adw.PreferencesWindow { - - [GtkChild] private unowned Switch typing_switch; - [GtkChild] private unowned Switch marker_switch; - [GtkChild] private unowned Switch notification_switch; - [GtkChild] private unowned Switch emoji_switch; - - Dino.Entities.Settings settings = Dino.Application.get_default().settings; - - public SettingsDialog() { - Object(); - - typing_switch.active = settings.send_typing; - marker_switch.active = settings.send_marker; - notification_switch.active = settings.notifications; - emoji_switch.active = settings.convert_utf8_smileys; - - typing_switch.notify["active"].connect(() => { settings.send_typing = typing_switch.active; } ); - marker_switch.notify["active"].connect(() => { settings.send_marker = marker_switch.active; } ); - notification_switch.notify["active"].connect(() => { settings.notifications = notification_switch.active; } ); - emoji_switch.notify["active"].connect(() => { settings.convert_utf8_smileys = emoji_switch.active; }); - } -} - -} diff --git a/main/src/view_model/account_details.vala b/main/src/view_model/account_details.vala new file mode 100644 index 00000000..a9ddeca5 --- /dev/null +++ b/main/src/view_model/account_details.vala @@ -0,0 +1,21 @@ +using Dino; +using Dino.Entities; +using Xmpp; +using Xmpp.Xep; + +public class Dino.Ui.ViewModel.AccountDetails : Object { + public Entities.Account account { get; set; } + public string bare_jid { owned get { return account.bare_jid.to_string(); } } + public CompatAvatarPictureModel avatar_model { get; set; } + public ConnectionManager.ConnectionState connection_state { get; set; } + public ConnectionManager.ConnectionError? connection_error { get; set; } + + public AccountDetails(Account account, StreamInteractor stream_interactor) { + var account_conv = new Conversation(account.bare_jid, account, Conversation.Type.CHAT); + + this.account = account; + this.avatar_model = new ViewModel.CompatAvatarPictureModel(stream_interactor).set_conversation(account_conv); + this.connection_state = stream_interactor.connection_manager.get_state(account); + this.connection_error = stream_interactor.connection_manager.get_error(account); + } +} \ No newline at end of file diff --git a/main/src/view_model/preferences_window.vala b/main/src/view_model/preferences_window.vala new file mode 100644 index 00000000..9cc5a80e --- /dev/null +++ b/main/src/view_model/preferences_window.vala @@ -0,0 +1,109 @@ +using Dino.Entities; +using Xmpp; +using Xmpp.Xep; +using Gee; + +public class Dino.Ui.ViewModel.PreferencesWindow : Object { + public signal void update(); + + public HashMap account_details = new HashMap(Account.hash_func, Account.equals_func); + public AccountDetails selected_account { get; set; } + public Gtk.SingleSelection active_accounts_selection { get; default=new Gtk.SingleSelection(new GLib.ListStore(typeof(ViewModel.AccountDetails))); } + + public StreamInteractor stream_interactor; + public Database db; + + public GeneralPreferencesPage general_page { get; set; default=new GeneralPreferencesPage(); } + + public void populate(Database db, StreamInteractor stream_interactor) { + this.db = db; + this.stream_interactor = stream_interactor; + + stream_interactor.connection_manager.connection_error.connect((account, error) => { + var account_detail = account_details[account]; + if (account_details != null) { + account_detail.connection_error = error; + } + }); + stream_interactor.connection_manager.connection_state_changed.connect((account, state) => { + var account_detail = account_details[account]; + if (account_details != null) { + account_detail.connection_state = state; + account_detail.connection_error = stream_interactor.connection_manager.get_error(account); + } + }); + stream_interactor.account_added.connect(update_data); + stream_interactor.account_removed.connect(update_data); + + bind_general_page(); + update_data(); + } + + private void update_data() { + // account_details should hold the correct set of accounts (add or remove some if needed), but do not override remaining ones (would destroy bindings) + var current_accounts = db.get_accounts(); + var remove_accounts = new ArrayList(); + foreach (var account in account_details.keys) { + if (!current_accounts.contains(account)) remove_accounts.add(account); + } + foreach (var account in remove_accounts) { + account_details.unset(account); + } + foreach (var account in current_accounts) { + if (!account_details.has_key(account)) { + account_details[account] = new AccountDetails(account, stream_interactor); + } + if (selected_account == null && account.enabled) selected_account = account_details[account]; + } + + // Update account picker model with currently active accounts + var list_model = (GLib.ListStore) active_accounts_selection.model; + list_model.remove_all(); + foreach (var account in stream_interactor.get_accounts()) { + list_model.append(new ViewModel.AccountDetails(account, stream_interactor)); + } + + update(); + } + + public void set_avatar_uri(Account account, string uri) { + stream_interactor.get_module(AvatarManager.IDENTITY).publish(account, uri); + } + + public void remove_avatar(Account account) { + stream_interactor.get_module(AvatarManager.IDENTITY).unset_avatar(account); + } + + public void remove_account(Account account) { + stream_interactor.disconnect_account.begin(account, () => { + account.remove(); + update_data(); + }); + } + + public void reconnect_account(Account account) { + stream_interactor.disconnect_account.begin(account, () => { + stream_interactor.connect_account(account); + }); + } + + public void enable_disable_account(Account account) { + if (account.enabled) { + account.enabled = false; + stream_interactor.disconnect_account.begin(account); + } else { + account.enabled = true; + stream_interactor.connect_account(account); + } + update_data(); + } + + private void bind_general_page() { + var settings = Dino.Application.get_default().settings; + settings.bind_property("send-typing", general_page, "send-typing", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + settings.bind_property("send-marker", general_page, "send-marker", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + settings.bind_property("notifications", general_page, "notifications", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + settings.bind_property("convert-utf8-smileys", general_page, "convert-emojis", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + } +} + 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-54-g00ecf